int ecmd_flush(int argc, char *argv[]) { char ch; char *time_format = "%Y %m %d %H:%M:%S"; optreset = 1; optind = 0; while ((ch = getopt(argc, argv, "t:")) != -1) { switch (ch) { case 't': if ((time_format = strdup(optarg)) == NULL) err(1, "%s: strdup of time_format failed", argv[0]); break; case '?': case 'h': default: errx(1, "usage: %s [-t format]", argv[0]); } } medialib_load(db_file, playlist_dir); medialib_db_flush(stdout, time_format); medialib_destroy(); return 0; }
static void ecmd_flush_exec(UNUSED int argc, UNUSED char **argv) { medialib_load(db_file, playlist_dir); medialib_db_flush(stdout, time_format); medialib_destroy(); }
int ecmd_rmfile(int argc, char *argv[]) { char *filename; char input[255]; bool forced; bool found; int found_idx; int i; if (argc < 2 || argc > 3) errx(1, "usage: -e %s [-f] filename|URL", argv[0]); /* get filename and if this remove is forced */ forced = false; if (argc == 3) { if (strcmp(argv[1], "-f") != 0) errx(1, "usage: -e %s [-f] filename|URL", argv[0]); else forced = true; filename = argv[2]; } else filename = argv[1]; /* load database and search for record */ medialib_load(db_file, playlist_dir); found = false; found_idx = -1; for (i = 0; i < mdb.library->nfiles && !found; i++) { if (strcmp(filename, mdb.library->files[i]->filename) == 0) { found = true; found_idx = i; } } /* if not found then error */ if (!found) { i = (forced ? 0 : 1); errx(i, "%s: %s: No such file or URL", argv[0], filename); } /* if not forced, prompt user if they are sure */ if (!forced) { printf("Are you sure you want to delete '%s'? [y/n] ", filename); if (fgets(input, sizeof(input), stdin) == NULL || (strcasecmp(input, "yes\n") != 0 && strcasecmp(input, "y\n") != 0)) errx(1, "%s: operation canceled. Database unchanged.", argv[0]); } playlist_files_remove(mdb.library, found_idx, 1, false); medialib_db_save(db_file); medialib_destroy(); return 0; }
static void ecmd_update_exec(UNUSED int argc, UNUSED char **argv) { printf("Loading existing database...\n"); medialib_load(db_file, playlist_dir); printf("Updating existing database...\n"); medialib_db_update(show_skipped, force_update); medialib_destroy(); }
static void ecmd_add_exec(UNUSED int argc, char **argv) { printf("Loading existing database...\n"); medialib_load(db_file, playlist_dir); printf("Scanning directories for files to add to database...\n"); medialib_db_scan_dirs(argv); medialib_destroy(); }
int cmd_reload(int argc, char *argv[]) { char *db_file; char *playlist_dir; if (argc != 2) { paint_error("usage: %s [ db | conf ]", argv[0]); return 1; } /* reload database or config file */ if (strcasecmp(argv[1], "db") == 0) { db_file = strdup(mdb.db_file); playlist_dir = strdup(mdb.playlist_dir); if (db_file == NULL || playlist_dir == NULL) err(1, "cmd_reload: strdup(3) failed"); /* stop playback TODO investigate a nice way around this */ player_stop(); /* reload db */ medialib_destroy(); medialib_load(db_file, playlist_dir); /* sort entries */ qsort(mdb.library->files, mdb.library->nfiles, sizeof(meta_info*), mi_compare); free(db_file); free(playlist_dir); /* re-setup ui basics */ playing_playlist = NULL; setup_viewing_playlist(mdb.library); ui.library->voffset = 0; ui.library->nrows = mdb.nplaylists; ui.library->crow = 0; paint_all(); } else if (strcasecmp(argv[1], "conf") == 0) { load_config(); paint_message("configuration reloaded"); } else { paint_error("usage: %s [ db | conf ]", argv[0]); return 2; } return 0; }
int ecmd_add(int argc, char *argv[]) { if (argc == 1) errx(1, "usage: -e %s /path/to/filesORdirs [ ... ] ", argv[0]); printf("Loading existing database...\n"); medialib_load(db_file, playlist_dir); printf("Scanning directories for files to add to database...\n"); medialib_db_scan_dirs(argv + 1); medialib_destroy(); return 0; }
int ecmd_update(int argc, char *argv[]) { bool show_skipped = false; if (argc == 2 && strcmp(argv[1], "-s") == 0) show_skipped = true; if (argc != 1 && !show_skipped) errx(1, "usage: -e %s [-s]", argv[0]); printf("Loading existing database...\n"); medialib_load(db_file, playlist_dir); printf("Updating existing database...\n"); medialib_db_update(show_skipped); medialib_destroy(); return 0; }
static void ecmd_check_show_db(const char *path) { bool found; char realfile[PATH_MAX]; int i; meta_info *mi; if (show_database == false) return; if (realpath(path, realfile) == NULL) { warn("realpath failed for %s: skipping", path); return; } /* 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", path); 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(); }
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; }