diff --git a/src/db.c b/src/db.c index 2eacaef3..8e11e9c5 100644 --- a/src/db.c +++ b/src/db.c @@ -1524,6 +1524,26 @@ db_files_get_count(void) return db_get_count("SELECT COUNT(*) FROM files f WHERE f.disabled = 0;"); } +int +db_files_get_count_bypathpattern(char *path) +{ + char *query; + int count; + + query = sqlite3_mprintf("SELECT COUNT(*) FROM files f WHERE f.path LIKE '%%%q';", path); + if (!query) + { + DPRINTF(E_LOG, L_DB, "Out of memory making count query string.\n"); + + return; + } + + count = db_get_count(query); + sqlite3_free(query); + + return count; +} + void db_files_update_songalbumid(void) { diff --git a/src/db.h b/src/db.h index 4e96d0e2..99bb9586 100644 --- a/src/db.h +++ b/src/db.h @@ -319,6 +319,9 @@ db_query_fetch_string_sort(struct query_params *qp, char **string, char **sortst int db_files_get_count(void); +int +db_files_get_count_bypathpattern(char *path); + void db_files_update_songalbumid(void); diff --git a/src/filescanner_m3u.c b/src/filescanner_m3u.c index bd90b297..0031cfb4 100644 --- a/src/filescanner_m3u.c +++ b/src/filescanner_m3u.c @@ -171,17 +171,34 @@ scan_m3u_playlist(char *file, time_t mtime) buf[i] = '/'; } - entry = buf; - while (entry && !(mfi_id = db_file_id_bypathpattern(entry))) + /* Now search for the library item where the path has closest match to playlist item */ + /* Succes is when we find an unambiguous match, or when we no longer can expand the */ + /* the path to refine our search. */ + entry = NULL; + do { - DPRINTF(E_DBG, L_SCAN, "Playlist entry is now %s\n", entry); - entry = strchr(entry + 1, '/'); - } + ptr = strrchr(buf, '/'); + if (entry) + *(entry - 1) = '/'; + if (ptr) + { + *ptr = '\0'; + entry = ptr + 1; + } + else + entry = buf; - if (entry && mfi_id > 0) + DPRINTF(E_SPAM, L_SCAN, "Playlist entry is now %s\n", entry); + ret = db_files_get_count_bypathpattern(entry); + + } while (ptr && (ret > 1)); + + if (ret > 0) { + mfi_id = db_file_id_bypathpattern(entry); DPRINTF(E_DBG, L_SCAN, "Found playlist entry match, id is %d, entry is %s\n", mfi_id, entry); - filename = db_file_path_byid(mfi_id); + + filename = db_file_path_byid(mfi_id); if (!filename) { DPRINTF(E_LOG, L_SCAN, "Playlist entry %s matches file id %d, but file path is missing.\n", entry, mfi_id); @@ -190,7 +207,11 @@ scan_m3u_playlist(char *file, time_t mtime) } } else - continue; + { + DPRINTF(E_DBG, L_SCAN, "No match for playlist entry %s\n", entry); + + continue; + } } ret = db_pl_add_item_bypath(pl_id, filename);