From 77a8de3beaca98cae1e8ed93cd41c9c36addf4db Mon Sep 17 00:00:00 2001 From: ejurgensen Date: Sun, 2 Feb 2020 14:40:37 -0800 Subject: [PATCH] [scan] Fix db_pl_add() so it always returns correct id --- src/db.c | 14 ++++++-------- src/db.h | 2 +- src/library.c | 2 +- src/library/filescanner.c | 11 ++++++++--- src/library/filescanner_itunes.c | 25 ++++++++++++------------- 5 files changed, 28 insertions(+), 26 deletions(-) diff --git a/src/db.c b/src/db.c index 1d6844b1..98c8d2ec 100644 --- a/src/db.c +++ b/src/db.c @@ -3476,7 +3476,7 @@ db_pl_fetch_bytitlepath(const char *title, const char *path) } int -db_pl_add(struct playlist_info *pli, int *id) +db_pl_add(struct playlist_info *pli) { int ret; @@ -3495,16 +3495,14 @@ db_pl_add(struct playlist_info *pli, int *id) if (ret < 0) return -1; - if (id) + pli->id = (int)sqlite3_last_insert_rowid(hdl); + if (pli->id == 0) { - ret = db_pl_id_bypath(pli->path); - if (ret < 0) - return -1; - - *id = ret; + DPRINTF(E_LOG, L_DB, "Successful playlist insert but no last_insert_rowid!\n"); + return -1; } - DPRINTF(E_DBG, L_DB, "Added playlist %s (path %s)\n", pli->title, pli->path); + DPRINTF(E_DBG, L_DB, "Added playlist %s (path %s) as id %d\n", pli->title, pli->path, pli->id); return 0; } diff --git a/src/db.h b/src/db.h index 4108e3d5..db0c9496 100644 --- a/src/db.h +++ b/src/db.h @@ -673,7 +673,7 @@ struct playlist_info * db_pl_fetch_bytitlepath(const char *title, const char *path); int -db_pl_add(struct playlist_info *pli, int *id); +db_pl_add(struct playlist_info *pli); int db_pl_update(struct playlist_info *pli); diff --git a/src/library.c b/src/library.c index e7200efb..4bbbb305 100644 --- a/src/library.c +++ b/src/library.c @@ -148,7 +148,7 @@ library_playlist_save(struct playlist_info *pli) } if (pli->id == 0) - return db_pl_add(pli, NULL); + return db_pl_add(pli); else return db_pl_update(pli); } diff --git a/src/library/filescanner.c b/src/library/filescanner.c index a1cea855..8a3f3917 100644 --- a/src/library/filescanner.c +++ b/src/library/filescanner.c @@ -464,11 +464,16 @@ playlist_add(const char *path) return -1; ret = library_playlist_save(&pli); - free_pli(&pli, 1); if (ret < 0) - return -1; + { + free_pli(&pli, 1); + return -1; + } - return db_pl_id_bypath(path); + ret = pli.id; + free_pli(&pli, 1); + + return ret; } diff --git a/src/library/filescanner_itunes.c b/src/library/filescanner_itunes.c index 355e1278..71e7f47a 100644 --- a/src/library/filescanner_itunes.c +++ b/src/library/filescanner_itunes.c @@ -42,6 +42,7 @@ #include "logger.h" #include "db.h" +#include "library.h" #include "library/filescanner.h" #include "conffile.h" #include "misc.h" @@ -788,13 +789,11 @@ process_pls(plist_t playlists, const char *file) { plist_t pl; plist_t items; - struct playlist_info *pli; + struct playlist_info pli; char *name; uint64_t id; - int pl_id; uint32_t alen; uint32_t i; - char virtual_path[PATH_MAX]; int ret; alen = plist_array_get_size(playlists); @@ -834,28 +833,28 @@ process_pls(plist_t playlists, const char *file) continue; } - CHECK_NULL(L_SCAN, pli = calloc(1, sizeof(struct playlist_info))); + playlist_fill(&pli, file); - 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); + free(pli.title); + pli.title = strdup(name); + free(pli.virtual_path); + pli.virtual_path = safe_asprintf("/file:%s/%s", file, name); - ret = db_pl_add(pli, &pl_id); - free_pli(pli, 0); + ret = library_playlist_save(&pli); if (ret < 0) { DPRINTF(E_LOG, L_SCAN, "Error adding iTunes playlist '%s' (%s)\n", name, file); + free_pli(&pli, 1); 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", pli.id); - process_pl_items(items, pl_id, name); + process_pl_items(items, pli.id, name); + free_pli(&pli, 1); free(name); } }