[scan] Fix bugs related to iTunes XML playlists

1) They would get disabled after rescan
2) Playlists and playlistitems didn't get cleaned up
This commit is contained in:
ejurgensen 2018-01-25 22:26:00 +01:00
parent e1fb3607e9
commit c23f2c01b2
2 changed files with 56 additions and 67 deletions

View File

@ -3120,13 +3120,23 @@ db_pl_delete(int id)
void void
db_pl_delete_bypath(const char *path) db_pl_delete_bypath(const char *path)
{ {
int id; int i;
int ret;
char *query;
char *queries_tmpl[] =
{
"DELETE FROM playlistitems WHERE playlistid IN (SELECT id FROM playlists WHERE path = '%q');",
"DELETE FROM playlists WHERE path = '%q';",
};
id = db_pl_id_bypath(path); for (i = 0; i < (sizeof(queries_tmpl) / sizeof(queries_tmpl[0])); i++)
if (id < 0) {
return; query = sqlite3_mprintf(queries_tmpl[i], path);
db_pl_delete(id); ret = db_query_run(query, 1, 0);
if (ret == 0)
DPRINTF(E_DBG, L_DB, "Purged %d rows\n", sqlite3_changes(hdl));
}
} }
void void

View File

@ -648,7 +648,6 @@ process_tracks(plist_t tracks)
return nloaded; return nloaded;
} }
static void static void
process_pl_items(plist_t items, int pl_id, const char *name) process_pl_items(plist_t items, int pl_id, const char *name)
{ {
@ -780,19 +779,6 @@ process_pls(plist_t playlists, const char *file)
continue; continue;
} }
pli = db_pl_fetch_bytitlepath(name, file);
if (pli)
{
pl_id = pli->id;
free_pli(pli, 0);
db_pl_ping(pl_id);
db_pl_clear_items(pl_id);
}
else
pl_id = 0;
ret = get_dictval_array_from_key(pl, "Playlist Items", &items); ret = get_dictval_array_from_key(pl, "Playlist Items", &items);
if (ret < 0) if (ret < 0)
{ {
@ -802,35 +788,26 @@ process_pls(plist_t playlists, const char *file)
continue; continue;
} }
if (pl_id == 0) CHECK_NULL(L_SCAN, pli = calloc(1, sizeof(struct playlist_info)));
pli->type = PL_PLAIN;
pli->title = strdup(name);
pli->path = strdup(file);
snprintf(virtual_path, sizeof(virtual_path), "/file:%s/%s", file, name);
pli->virtual_path = strdup(virtual_path);
ret = db_pl_add(pli, &pl_id);
free_pli(pli, 0);
if (ret < 0)
{ {
pli = calloc(1, sizeof(struct playlist_info)); DPRINTF(E_LOG, L_SCAN, "Error adding iTunes playlist '%s' (%s)\n", name, file);
if (!pli)
{
DPRINTF(E_LOG, L_SCAN, "Out of memory\n");
return; free(name);
} continue;
pli->type = PL_PLAIN;
pli->title = strdup(name);
pli->path = strdup(file);
snprintf(virtual_path, sizeof(virtual_path), "/file:%s/%s", file, name);
pli->virtual_path = strdup(virtual_path);
ret = db_pl_add(pli, &pl_id);
free_pli(pli, 0);
if (ret < 0)
{
DPRINTF(E_LOG, L_SCAN, "Error adding iTunes playlist '%s' (%s)\n", name, file);
free(name);
continue;
}
DPRINTF(E_INFO, L_SCAN, "Added playlist as id %d\n", pl_id);
} }
DPRINTF(E_INFO, L_SCAN, "Added playlist as id %d\n", pl_id);
process_pl_items(items, pl_id, name); process_pl_items(items, pl_id, name);
free(name); free(name);
@ -854,9 +831,6 @@ scan_itunes_itml(const char *file, time_t mtime, int dir_id)
pli = db_pl_fetch_bytitlepath(file, file); pli = db_pl_fetch_bytitlepath(file, file);
if (pli) if (pli)
{ {
db_pl_ping(pli->id);
db_pl_disable_bypath(file, STRIP_NONE, 0);
// mtime == db_timestamp is also treated as a modification because some editors do // mtime == db_timestamp is also treated as a modification because some editors do
// stuff like 1) close the file with no changes (leading us to update db_timestamp), // stuff like 1) close the file with no changes (leading us to update db_timestamp),
// 2) copy over a modified version from a tmp file (which may result in a mtime that // 2) copy over a modified version from a tmp file (which may result in a mtime that
@ -872,32 +846,37 @@ scan_itunes_itml(const char *file, time_t mtime, int dir_id)
} }
DPRINTF(E_LOG, L_SCAN, "Modified iTunes XML found, processing '%s'\n", file); DPRINTF(E_LOG, L_SCAN, "Modified iTunes XML found, processing '%s'\n", file);
// Clear out everything, we will recreate
db_pl_delete_bypath(file);
free_pli(pli, 0);
} }
else else
{ {
DPRINTF(E_LOG, L_SCAN, "New iTunes XML found, processing: '%s'\n", file); DPRINTF(E_LOG, L_SCAN, "New iTunes XML found, processing: '%s'\n", file);
CHECK_NULL(L_SCAN, pli = calloc(1, sizeof(struct playlist_info)));
pli->type = PL_PLAIN;
pli->title = strdup(file);
pli->path = strdup(file);
snprintf(buf, sizeof(buf), "/file:%s", file);
pli->virtual_path = strip_extension(buf);
pli->directory_id = dir_id;
ret = db_pl_add(pli, (int *)&pli->id);
if (ret < 0)
{
DPRINTF(E_LOG, L_SCAN, "Error adding iTunes XML playlist '%s'\n", file);
free_pli(pli, 0);
return;
}
db_pl_disable_bypath(file, STRIP_NONE, 0);
} }
CHECK_NULL(L_SCAN, pli = calloc(1, sizeof(struct playlist_info)));
pli->type = PL_PLAIN;
pli->title = strdup(file);
pli->path = strdup(file);
snprintf(buf, sizeof(buf), "/file:%s", file);
pli->virtual_path = strip_extension(buf);
pli->directory_id = dir_id;
ret = db_pl_add(pli, (int *)&pli->id);
if (ret < 0)
{
DPRINTF(E_LOG, L_SCAN, "Error adding iTunes XML meta playlist '%s'\n", file);
free_pli(pli, 0);
return;
}
// Disable, only used for saving timestamp
db_pl_disable_bypath(file, STRIP_NONE, 0);
free_pli(pli, 0); free_pli(pli, 0);
fd = open(file, O_RDONLY); fd = open(file, O_RDONLY);