From 70365422b4afeb9a7cab944ee4e070d9acf33fb2 Mon Sep 17 00:00:00 2001 From: ejurgensen Date: Wed, 14 Jan 2015 22:06:27 +0100 Subject: [PATCH] Don't add duplicates (same fname, artist, album and title), issue #85 --- src/db.c | 17 +++++++++++++++++ src/db.h | 3 +++ src/filescanner.c | 13 +++++++++++++ 3 files changed, 33 insertions(+) diff --git a/src/db.c b/src/db.c index b94d77c2..9b0739d4 100644 --- a/src/db.c +++ b/src/db.c @@ -2359,6 +2359,23 @@ db_file_fetch_byid(int id) #undef Q_TMPL } +int +db_file_is_duplicate(struct media_file_info *mfi) +{ +#define Q_TMPL "SELECT f.id FROM files f WHERE f.title = TRIM(%Q) AND f.artist = TRIM(%Q) AND f.album = TRIM(%Q) AND f.fname = '%q';" + char *query; + int id; + + query = sqlite3_mprintf(Q_TMPL, mfi->title, mfi->artist, mfi->album, mfi->fname); + + id = db_file_id_byquery(query); + + sqlite3_free(query); + + return ((id > 0) && (id != mfi->id)); +#undef Q_TMPL +} + int db_file_add(struct media_file_info *mfi) { diff --git a/src/db.h b/src/db.h index a7d72bf4..b45186d1 100644 --- a/src/db.h +++ b/src/db.h @@ -394,6 +394,9 @@ db_file_stamp_bypath(char *path, time_t *stamp, int *id); struct media_file_info * db_file_fetch_byid(int id); +int +db_file_is_duplicate(struct media_file_info *mfi); + int db_file_add(struct media_file_info *mfi); diff --git a/src/filescanner.c b/src/filescanner.c index 0a5dab05..21057fce 100644 --- a/src/filescanner.c +++ b/src/filescanner.c @@ -678,6 +678,19 @@ filescanner_process_media(char *path, time_t mtime, off_t size, int type, struct fixup_tags(mfi); + if (db_file_is_duplicate(mfi)) + { + DPRINTF(E_LOG, L_SCAN, "Skipping '%s' (duplicate filename, artist, album and title)\n", mfi->path); + + /* Can't just disable because db_file_ping() will reenable on startup */ + if (mfi->id > 0) + { + db_file_delete_bypath(mfi->path); + cache_artwork_delete_by_path(mfi->path); + } + goto out; + } + if (mfi->id == 0) db_file_add(mfi); else