From 0c936c9beaf75c1ae528a837d468d22fad427cd9 Mon Sep 17 00:00:00 2001 From: chme Date: Sat, 7 Feb 2015 07:54:38 +0100 Subject: [PATCH] Do not trigger a new rescan if one is already running; fix log domains in filescanner; default to "file:" for virtual paths (fixes segfaults on pipes) --- src/filescanner.c | 57 +++++++++++++++++++++++++++++++++++++---------- src/filescanner.h | 3 +++ src/mpd.c | 14 ++++++++++++ 3 files changed, 62 insertions(+), 12 deletions(-) diff --git a/src/filescanner.c b/src/filescanner.c index 6d4f6fcd..f7e8a0a2 100644 --- a/src/filescanner.c +++ b/src/filescanner.c @@ -143,6 +143,9 @@ static struct stacked_dir *dirstack; /* Count of files scanned during a bulk scan */ static int counter; +/* Flag for scan in progress */ +static int scanning; + /* Forward */ static void bulk_scan(int flags); @@ -165,14 +168,14 @@ send_command(struct filescanner_command *cmd) if (!cmd->func) { - DPRINTF(E_LOG, L_CACHE, "BUG: cmd->func is NULL!\n"); + DPRINTF(E_LOG, L_SCAN, "BUG: cmd->func is NULL!\n"); return -1; } ret = write(cmd_pipe[1], &cmd, sizeof(cmd)); if (ret != sizeof(cmd)) { - DPRINTF(E_LOG, L_CACHE, "Could not send command: %s\n", strerror(errno)); + DPRINTF(E_LOG, L_SCAN, "Could not send command: %s\n", strerror(errno)); return -1; } @@ -737,12 +740,7 @@ filescanner_process_media(char *path, time_t mtime, off_t size, int type, struct fixup_tags(mfi); - if (type & F_SCAN_TYPE_FILE) - { - snprintf(virtual_path, PATH_MAX, "/file:%s", mfi->path); - mfi->virtual_path = strdup(virtual_path); - } - else if (type & F_SCAN_TYPE_URL) + if (type & F_SCAN_TYPE_URL) { snprintf(virtual_path, PATH_MAX, "/http:/%s", mfi->title); mfi->virtual_path = strdup(virtual_path); @@ -752,6 +750,11 @@ filescanner_process_media(char *path, time_t mtime, off_t size, int type, struct snprintf(virtual_path, PATH_MAX, "/spotify:/%s/%s/%s", mfi->album_artist, mfi->album, mfi->title); mfi->virtual_path = strdup(virtual_path); } + else + { + snprintf(virtual_path, PATH_MAX, "/file:%s", mfi->path); + mfi->virtual_path = strdup(virtual_path); + } if (mfi->id == 0) db_file_add(mfi); @@ -1129,6 +1132,9 @@ bulk_scan(int flags) time_t end; int i; + // Set global flag to avoid queued scan requests + scanning = 1; + start = time(NULL); playlists = NULL; @@ -1200,6 +1206,9 @@ bulk_scan(int flags) DPRINTF(E_DBG, L_SCAN, "Running post library scan jobs\n"); db_hook_post_scan(); } + + // Set scan in progress flag to FALSE + scanning = 0; } @@ -1945,7 +1954,7 @@ command_cb(int fd, short what, void *arg) ret = read(cmd_pipe[0], &cmd, sizeof(cmd)); if (ret != sizeof(cmd)) { - DPRINTF(E_LOG, L_CACHE, "Could not read command! (read %d): %s\n", ret, (ret < 0) ? strerror(errno) : "-no error-"); + DPRINTF(E_LOG, L_SCAN, "Could not read command! (read %d): %s\n", ret, (ret < 0) ? strerror(errno) : "-no error-"); goto readd; } @@ -2004,10 +2013,17 @@ filescanner_trigger_initscan(void) { struct filescanner_command *cmd; + if (scanning) + { + DPRINTF(E_INFO, L_SCAN, "Scan already running, ignoring request to trigger a new init scan\n"); + return; + } + + cmd = (struct filescanner_command *)malloc(sizeof(struct filescanner_command)); if (!cmd) { - DPRINTF(E_LOG, L_CACHE, "Could not allocate cache_command\n"); + DPRINTF(E_LOG, L_SCAN, "Could not allocate cache_command\n"); return; } @@ -2025,10 +2041,16 @@ filescanner_trigger_fullrescan(void) { struct filescanner_command *cmd; + if (scanning) + { + DPRINTF(E_INFO, L_SCAN, "Scan already running, ignoring request to trigger a new init scan\n"); + return; + } + cmd = (struct filescanner_command *)malloc(sizeof(struct filescanner_command)); if (!cmd) { - DPRINTF(E_LOG, L_CACHE, "Could not allocate cache_command\n"); + DPRINTF(E_LOG, L_SCAN, "Could not allocate cache_command\n"); return; } @@ -2041,6 +2063,16 @@ filescanner_trigger_fullrescan(void) nonblock_command(cmd); } +/* + * Query the status of the filescanner + * @return 1 if scan is running, otherwise 0 + */ +int +filescanner_scanning(void) +{ + return scanning; +} + /* Thread: main */ int filescanner_init(void) @@ -2048,6 +2080,7 @@ filescanner_init(void) int ret; scan_exit = 0; + scanning = 0; evbase_scan = event_base_new(); if (!evbase_scan) @@ -2100,7 +2133,7 @@ filescanner_init(void) # endif if (ret < 0) { - DPRINTF(E_LOG, L_CACHE, "Could not create command pipe: %s\n", strerror(errno)); + DPRINTF(E_LOG, L_SCAN, "Could not create command pipe: %s\n", strerror(errno)); goto cmd_fail; } diff --git a/src/filescanner.h b/src/filescanner.h index d186661c..8814f16c 100644 --- a/src/filescanner.h +++ b/src/filescanner.h @@ -42,4 +42,7 @@ filescanner_trigger_initscan(void); void filescanner_trigger_fullrescan(void); +int +filescanner_scanning(void); + #endif /* !__FILESCANNER_H__ */ diff --git a/src/mpd.c b/src/mpd.c index 9f29ddab..022570fc 100644 --- a/src/mpd.c +++ b/src/mpd.c @@ -533,6 +533,20 @@ mpd_command_status(struct evbuffer *evbuf, int argc, char **argv, char **errmsg) status.nextsong_id); } + if (filescanner_scanning()) + { + evbuffer_add(evbuf, "updating_db: 1\n", 15); + } + + if (status.status != PLAY_STOPPED) + { + evbuffer_add_printf(evbuf, + "nextsong: %d\n" + "nextsongid: %d\n", + status.nextsong_pos_pl, + status.nextsong_id); + } + return 0; }