From 4df4c50c35b04f4ec42efa4d394e034733afeb61 Mon Sep 17 00:00:00 2001 From: ejurgensen Date: Wed, 14 Jan 2015 21:50:25 +0100 Subject: [PATCH] Add option to exclude paths from scanning based on regex --- configure.ac | 1 + forked-daapd.conf | 8 +++++++- src/conffile.c | 1 + src/filescanner.c | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 55 insertions(+), 1 deletion(-) diff --git a/configure.ac b/configure.ac index 0632255c..5e38681b 100644 --- a/configure.ac +++ b/configure.ac @@ -40,6 +40,7 @@ AC_CHECK_HEADERS([sys/wait.h]) AC_CHECK_HEADERS([sys/param.h]) AC_CHECK_HEADERS([sys/select.h]) AC_CHECK_HEADERS([dirent.h]) +AC_CHECK_HEADERS([regex.h]) AC_CHECK_FUNCS(posix_fadvise) AC_CHECK_FUNCS(strptime) AC_CHECK_FUNCS(strtok_r) diff --git a/forked-daapd.conf b/forked-daapd.conf index efd169a3..870b3d16 100644 --- a/forked-daapd.conf +++ b/forked-daapd.conf @@ -102,6 +102,12 @@ library { # scan time. By default .db, .ini, .db-journal and .pdf are ignored. # filetypes_ignore = { ".db", ".ini", ".db-journal", ".pdf" } + # File paths the scanner should ignore + # If you want to exclude files on a more advanced basis you can enter + # one or more POSIX regular expressions, and any file with a matching + # path will be ignored. +# filepath_ignore = { "myregex" } + # Disable startup file scanning # When forked-daapd starts it will do an initial file scan of your # library (and then watch it for changes). If you are sure your library @@ -109,7 +115,7 @@ library { # initial file scan and save some system ressources. Disabling this scan # may lead to forked-daapd's database coming out of sync with the # library. If that happens read the instructions in the README on how - # to trigger a full rescan. + # to trigger a rescan. # filescan_disable = false # Should iTunes metadata override ours? diff --git a/src/conffile.c b/src/conffile.c index 77a3b9ab..b9155deb 100644 --- a/src/conffile.c +++ b/src/conffile.c @@ -78,6 +78,7 @@ static cfg_opt_t sec_library[] = CFG_STR_LIST("artwork_basenames", "{artwork,cover,Folder}", CFGF_NONE), CFG_BOOL("artwork_individual", cfg_false, CFGF_NONE), CFG_STR_LIST("filetypes_ignore", "{.db,.ini,.db-journal,.pdf}", CFGF_NONE), + CFG_STR_LIST("filepath_ignore", NULL, CFGF_NONE), CFG_BOOL("filescan_disable", cfg_false, CFGF_NONE), CFG_BOOL("itunes_overrides", cfg_false, CFGF_NONE), CFG_STR_LIST("no_transcode", NULL, CFGF_NONE), diff --git a/src/filescanner.c b/src/filescanner.c index ae90c051..0a5dab05 100644 --- a/src/filescanner.c +++ b/src/filescanner.c @@ -53,6 +53,10 @@ # include #endif +#ifdef HAVE_REGEX_H +# include +#endif + #include #include "logger.h" @@ -172,6 +176,43 @@ pop_dir(struct stacked_dir **s) return ret; } +#ifdef HAVE_REGEX_H +/* Checks if the file path is configured to be ignored */ +static int +file_path_ignore(const char *path) +{ + cfg_t *lib; + regex_t regex; + int n; + int i; + int ret; + + lib = cfg_getsec(cfg, "library"); + n = cfg_size(lib, "filepath_ignore"); + + for (i = 0; i < n; i++) + { + ret = regcomp(®ex, cfg_getnstr(lib, "filepath_ignore", i), 0); + if (ret != 0) + { + DPRINTF(E_LOG, L_SCAN, "Could not compile regex for matching with file path\n"); + return 0; + } + + ret = regexec(®ex, path, 0, NULL, 0); + regfree(®ex); + + if (ret == 0) + { + DPRINTF(E_DBG, L_SCAN, "Regex match: %s\n", path); + return 1; + } + } + + return 0; +} +#endif + /* Checks if the file extension is in the ignore list */ static int file_type_ignore(const char *ext) @@ -203,6 +244,11 @@ file_type_get(const char *path) { else filename++; +#ifdef HAVE_REGEX_H + if (file_path_ignore(path)) + return FILE_IGNORE; +#endif + ext = strrchr(path, '.'); if (!ext || (strlen(ext) == 1)) return FILE_REGULAR;