#define _POSIX_C_SOURCE 200809L #include "mpd_common.h" #include #include #include #include #include #include #include #include #include #include #include #include struct window { time_t start; time_t end; bool has_start; bool has_end; }; struct options { const char *host; const char *path; const char *output; const char *tracks_output; unsigned port; bool include_files; bool indexed; bool relative; struct window *windows; size_t window_count; }; struct atomic_file { FILE *stream; char *temporary_path; const char *final_path; }; static void usage(FILE *stream, const char *program) { fprintf( stream, "Usage: %s [OPTIONS]\n" "Query MPD's database for directories modified in one or more time " "windows.\n\n" " -w, --window START,END epoch-second bounds; '-' means unbounded\n" " -f, --files include songs as well as directories\n" " -i, --indexed prefix results with their window index\n" " -r, --relative strip the requested MPD path prefix\n" " -p, --path URI restrict the lookup to an MPD URI\n" " -o, --output FILE atomically write results to FILE (default: " "all.txt); '-' uses stdout\n" " --tracks-output FILE atomically write every song URI to FILE\n" " -H, --host HOST MPD host (default: $MPD_HOST or localhost)\n" " -P, --port PORT MPD port (default: $MPD_PORT or 6600)\n" " -h, --help show this help\n\n" "One window writes plain URIs. Multiple windows (or --indexed) write\n" "WINDOW_INDEXURI. With no --window, the default is all time.\n" "Date windows use exclusive bounds, like diggah's filesystem backend.\n", program); } static bool parse_unsigned(const char *text, unsigned *value) { char *end = NULL; errno = 0; unsigned long parsed = strtoul(text, &end, 10); if (errno != 0 || end == text || *end != '\0' || parsed > UINT_MAX) return false; *value = (unsigned)parsed; return true; } static bool parse_timestamp(const char *text, time_t *value) { char *end = NULL; errno = 0; intmax_t parsed = strtoimax(text, &end, 10); if (errno != 0 || end == text || *end != '\0') return false; time_t timestamp = (time_t)parsed; if ((intmax_t)timestamp != parsed) return false; *value = timestamp; return true; } static bool append_window(struct options *options, const char *argument) { const char *comma = strchr(argument, ','); if (comma == NULL || strchr(comma + 1, ',') != NULL) { fprintf(stderr, "invalid window '%s' (expected START,END)\n", argument); return false; } size_t start_length = (size_t)(comma - argument); char *start = strndup(argument, start_length); char *end = strdup(comma + 1); if (start == NULL || end == NULL) { perror("allocating window"); free(start); free(end); return false; } struct window window = {0}; window.has_start = strcmp(start, "-") != 0; window.has_end = strcmp(end, "-") != 0; bool valid = true; if (window.has_start && !parse_timestamp(start, &window.start)) valid = false; if (window.has_end && !parse_timestamp(end, &window.end)) valid = false; if (window.has_start && window.has_end && window.start >= window.end) valid = false; free(start); free(end); if (!valid) { fprintf(stderr, "invalid window '%s'\n", argument); return false; } struct window *windows = realloc(options->windows, (options->window_count + 1U) * sizeof(*options->windows)); if (windows == NULL) { perror("allocating windows"); return false; } options->windows = windows; options->windows[options->window_count++] = window; return true; } static bool parse_options(int argc, char **argv, struct options *options) { *options = (struct options){.path = "", .output = "all.txt"}; enum { OPT_TRACKS_OUTPUT = 256 }; static const struct option long_options[] = { {"window", required_argument, NULL, 'w'}, {"files", no_argument, NULL, 'f'}, {"indexed", no_argument, NULL, 'i'}, {"relative", no_argument, NULL, 'r'}, {"path", required_argument, NULL, 'p'}, {"output", required_argument, NULL, 'o'}, {"tracks-output", required_argument, NULL, OPT_TRACKS_OUTPUT}, {"host", required_argument, NULL, 'H'}, {"port", required_argument, NULL, 'P'}, {"help", no_argument, NULL, 'h'}, {NULL, 0, NULL, 0}, }; int option = 0; while ((option = getopt_long(argc, argv, "w:firp:o:H:P:h", long_options, NULL)) != -1) { switch (option) { case 'w': if (!append_window(options, optarg)) return false; break; case 'f': options->include_files = true; break; case 'i': options->indexed = true; break; case 'r': options->relative = true; break; case 'p': options->path = optarg; break; case 'o': options->output = optarg; break; case 'H': options->host = optarg; break; case 'P': if (!parse_unsigned(optarg, &options->port) || options->port == 0U || options->port > 65535U) { fprintf(stderr, "invalid MPD port '%s'\n", optarg); return false; } break; case OPT_TRACKS_OUTPUT: options->tracks_output = optarg; break; case 'h': usage(stdout, argv[0]); exit(0); default: usage(stderr, argv[0]); return false; } } if (optind != argc) { fprintf(stderr, "unexpected argument '%s'\n", argv[optind]); return false; } if (options->output[0] == '\0') { fprintf(stderr, "output path must not be empty\n"); return false; } if (options->tracks_output != NULL && strcmp(options->tracks_output, options->output) == 0) { fprintf(stderr, "--output and --tracks-output must name different files\n"); return false; } if (options->window_count == 0U && !append_window(options, "-,-")) return false; return true; } static struct mpd_connection *connect_mpd(const struct options *options) { return mt_connect_to(options->host, options->port); } static bool atomic_file_open(struct atomic_file *file, const char *path) { *file = (struct atomic_file){.final_path = path}; size_t length = strlen(path) + sizeof(".tmp.XXXXXX"); file->temporary_path = malloc(length); if (file->temporary_path == NULL) { perror("allocating temporary path"); return false; } snprintf(file->temporary_path, length, "%s.tmp.XXXXXX", path); int descriptor = mkstemp(file->temporary_path); if (descriptor < 0) { fprintf(stderr, "opening temporary output for '%s': %s\n", path, strerror(errno)); free(file->temporary_path); file->temporary_path = NULL; return false; } file->stream = fdopen(descriptor, "w"); if (file->stream == NULL) { int error = errno; close(descriptor); unlink(file->temporary_path); errno = error; perror("opening temporary output stream"); free(file->temporary_path); file->temporary_path = NULL; return false; } return true; } static void atomic_file_abort(struct atomic_file *file) { if (file->stream != NULL) fclose(file->stream); if (file->temporary_path != NULL) { unlink(file->temporary_path); free(file->temporary_path); } *file = (struct atomic_file){0}; } static bool atomic_file_commit(struct atomic_file *file) { bool success = true; int write_error = 0; if (fflush(file->stream) != 0) write_error = errno; if (fsync(fileno(file->stream)) != 0 && write_error == 0) write_error = errno; if (fclose(file->stream) != 0 && write_error == 0) write_error = errno; if (write_error != 0) { fprintf(stderr, "writing output file '%s': %s\n", file->final_path, strerror(write_error)); success = false; } file->stream = NULL; if (success && rename(file->temporary_path, file->final_path) != 0) { fprintf(stderr, "installing output file '%s': %s\n", file->final_path, strerror(errno)); success = false; } if (!success) unlink(file->temporary_path); free(file->temporary_path); file->temporary_path = NULL; return success; } static bool in_window(time_t modified, const struct window *window) { if ((window->has_start || window->has_end) && modified == (time_t)0) return false; if (window->has_start && modified <= window->start) return false; if (window->has_end && modified >= window->end) return false; return true; } static const char *relative_uri(const char *uri, const struct options *options) { if (!options->relative || options->path[0] == '\0') return uri; size_t prefix_length = strlen(options->path); while (prefix_length > 0U && options->path[prefix_length - 1U] == '/') prefix_length--; if (strncmp(uri, options->path, prefix_length) != 0) return uri; if (uri[prefix_length] == '/') return uri + prefix_length + 1U; if (uri[prefix_length] == '\0') return "."; return uri; } static bool emit_matches(const struct options *options, FILE *stream, const char *uri, time_t modified) { const char *output_uri = relative_uri(uri, options); for (size_t index = 0; index < options->window_count; index++) { if (!in_window(modified, &options->windows[index])) continue; int written; if (options->indexed || options->window_count > 1U) written = fprintf(stream, "%zu\t%s\n", index, output_uri); else written = fprintf(stream, "%s\n", output_uri); if (written < 0) return false; } return true; } static bool query(const struct options *options) { struct mpd_connection *connection = connect_mpd(options); if (connection == NULL) return false; struct atomic_file results = {0}; FILE *result_stream = stdout; bool results_to_stdout = strcmp(options->output, "-") == 0; if (!results_to_stdout) { if (!atomic_file_open(&results, options->output)) { mpd_connection_free(connection); return false; } result_stream = results.stream; } struct atomic_file tracks = {0}; if (options->tracks_output != NULL && !atomic_file_open(&tracks, options->tracks_output)) { if (results.stream != NULL) atomic_file_abort(&results); mpd_connection_free(connection); return false; } bool success = mpd_send_list_all_meta(connection, options->path); if (!success) { fprintf(stderr, "requesting MPD database: %s\n", mpd_connection_get_error_message(connection)); } struct mpd_entity *entity = NULL; while (success && (entity = mpd_recv_entity(connection)) != NULL) { enum mpd_entity_type type = mpd_entity_get_type(entity); const char *uri = NULL; time_t modified = 0; if (type == MPD_ENTITY_TYPE_DIRECTORY) { const struct mpd_directory *directory = mpd_entity_get_directory(entity); uri = mpd_directory_get_path(directory); modified = mpd_directory_get_last_modified(directory); } else if (type == MPD_ENTITY_TYPE_SONG) { const struct mpd_song *song = mpd_entity_get_song(entity); uri = mpd_song_get_uri(song); modified = mpd_song_get_last_modified(song); if (tracks.stream != NULL && fprintf(tracks.stream, "%s\n", uri) < 0) { fprintf(stderr, "writing track list '%s': %s\n", tracks.final_path, strerror(errno)); success = false; } if (!options->include_files) uri = NULL; } if (success && uri != NULL && !emit_matches(options, result_stream, uri, modified)) { fprintf(stderr, "writing results to '%s': %s\n", options->output, strerror(errno)); success = false; } mpd_entity_free(entity); } if (success && !mpd_response_finish(connection)) { fprintf(stderr, "reading MPD database: %s\n", mpd_connection_get_error_message(connection)); success = false; } if (results_to_stdout) { if (success && fflush(stdout) != 0) { perror("writing results"); success = false; } } else if (results.stream != NULL) { if (success) success = atomic_file_commit(&results); else atomic_file_abort(&results); } if (tracks.stream != NULL) { if (success) success = atomic_file_commit(&tracks); else atomic_file_abort(&tracks); } mpd_connection_free(connection); return success; } int main(int argc, char **argv) { struct options options; if (!parse_options(argc, argv, &options)) { free(options.windows); return 2; } bool success = query(&options); free(options.windows); return success ? 0 : 1; }