static void ecmd_check_show_sanitized(const char *path) { int i; meta_info *mi; if (show_sanitized == false) return; if ((mi = mi_extract(path)) == NULL) { warnx("Failed to extract any meta-information from '%s'", path); return; } mi_sanitize(mi); printf("\tThe SANITIZED meta-information from the file is:\n"); for (i = 0; i < MI_NUM_CINFO; i++) printf("\t%10.10s: '%s'\n", MI_CINFO_NAMES[i], mi->cinfo[i]); }
int ecmd_addurl(int argc, char *argv[]) { meta_info *m; bool found; int found_idx; char input[255]; int field, i; if (argc != 2) errx(1, "usage: -e %s filename|URL", argv[0]); /* start new record, set filename */ m = mi_new(); m->is_url = true; if ((m->filename = strdup(argv[1])) == NULL) err(1, "%s: strdup failed (filename)", argv[0]); /* get fields from user */ for (field = 0; field < MI_NUM_CINFO; field++) { printf("%10.10s: ", MI_CINFO_NAMES[field]); if (fgets(input, sizeof(input), stdin) == NULL) { warnx("Operation canceled. Database unchanged."); mi_free(m); return 0; } if (input[strlen(input) - 1] == '\n') input[strlen(input) - 1] = '\0'; if ((m->cinfo[field] = strdup(input)) == NULL) err(1, "%s: strdup failed (field)", argv[0]); } /* load existing database and see if file/URL already exists */ medialib_load(db_file, playlist_dir); /* does the URL already exist in the database? */ found = false; found_idx = -1; for (i = 0; i < mdb.library->nfiles && !found; i++) { if (strcmp(m->filename, mdb.library->files[i]->filename) == 0) { found = true; found_idx = i; } } if (found) { printf("Warning: file/URL '%s' already in the database.\n", argv[0]); printf("Do you want to replace the existing record? [y/n] "); if (fgets(input, sizeof(input), stdin) == NULL || (strcasecmp(input, "yes\n") != 0 && strcasecmp(input, "y\n") != 0)) { warnx("Operation Canceled. Database unchanged."); mi_free(m); medialib_destroy(); return 0; } mi_sanitize(m); playlist_file_replace(mdb.library, found_idx, m); } else { mi_sanitize(m); playlist_files_append(mdb.library, &m, 1, false); } medialib_db_save(db_file); medialib_destroy(); return 0; }
int ecmd_check(int argc, char *argv[]) { meta_info *mi; bool show_raw, show_sanitized, show_database; bool found; char ch; char realfile[PATH_MAX]; char **files; int nfiles; int f, i; if (argc < 3) errx(1, "usage: -e %s [-rsd] file1 [ file2 ... ]", argv[0]); /* parse options to see which version to show */ show_raw = false; show_sanitized = false; show_database = false; optreset = 1; optind = 0; while ((ch = getopt(argc, argv, "rsd")) != -1) { switch (ch) { case 'r': show_raw = true; break; case 's': show_sanitized = true; break; case 'd': show_database = true; break; case 'h': case '?': default: errx(1, "usage: -e %s [-rsd] file1 [ file2 ... ]", argv[0]); } } files = argv + optind; nfiles = argc - optind; if (!show_raw && !show_sanitized && !show_database) errx(1, "%s: must specify at least one of -r, -s, or -d", argv[0]); if (nfiles == 0) errx(1, "%s: must provide at least one file to check.", argv[0]); /* scan through files... */ for (f = 0; f < nfiles; f++) { printf("Checking: '%s'\n", files[f]); /* show raw or sanitized information */ if (show_raw || show_sanitized) { mi = mi_extract(files[f]); if (mi == NULL) warnx("Failed to extract any meta-information from '%s'", files[f]); else { /* show raw info */ if (show_raw) { printf("\tThe RAW meta-information from the file is:\n"); for (i = 0; i < MI_NUM_CINFO; i++) printf("\t%10.10s: '%s'\n", MI_CINFO_NAMES[i], mi->cinfo[i]); } /* show sanitized info */ if (show_sanitized) { mi_sanitize(mi); printf("\tThe SANITIZED meta-information from the file is:\n"); for (i = 0; i < MI_NUM_CINFO; i++) printf("\t%10.10s: '%s'\n", MI_CINFO_NAMES[i], mi->cinfo[i]); } } } /* check if it's in the database */ if (show_database) { /* get absolute filename */ if (realpath(files[f], realfile) == NULL) { warn("%s: realpath failed for %s: skipping", argv[0], files[f]); continue; } /* check if file is in database */ medialib_load(db_file, playlist_dir); found = false; mi = NULL; for (i = 0; i < mdb.library->nfiles && !found; i++) { if (strcmp(realfile, mdb.library->files[i]->filename) == 0) { found = true; mi = mdb.library->files[i]; } } if (!found) warnx("File '%s' does NOT exist in the database", files[f]); else { printf("\tThe meta-information in the DATABASE is:\n"); for (i = 0; i < MI_NUM_CINFO; i++) printf("\t%10.10s: '%s'\n", MI_CINFO_NAMES[i], mi->cinfo[i]); } medialib_destroy(); } } return 0; }