[scan] Fix db_pl_add() so it always returns correct id

This commit is contained in:
ejurgensen 2020-02-02 14:40:37 -08:00
parent d94cf3f07f
commit 77a8de3bea
5 changed files with 28 additions and 26 deletions

View File

@ -3476,7 +3476,7 @@ db_pl_fetch_bytitlepath(const char *title, const char *path)
} }
int int
db_pl_add(struct playlist_info *pli, int *id) db_pl_add(struct playlist_info *pli)
{ {
int ret; int ret;
@ -3495,16 +3495,14 @@ db_pl_add(struct playlist_info *pli, int *id)
if (ret < 0) if (ret < 0)
return -1; return -1;
if (id) pli->id = (int)sqlite3_last_insert_rowid(hdl);
if (pli->id == 0)
{ {
ret = db_pl_id_bypath(pli->path); DPRINTF(E_LOG, L_DB, "Successful playlist insert but no last_insert_rowid!\n");
if (ret < 0)
return -1; return -1;
*id = ret;
} }
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; return 0;
} }

View File

@ -673,7 +673,7 @@ struct playlist_info *
db_pl_fetch_bytitlepath(const char *title, const char *path); db_pl_fetch_bytitlepath(const char *title, const char *path);
int int
db_pl_add(struct playlist_info *pli, int *id); db_pl_add(struct playlist_info *pli);
int int
db_pl_update(struct playlist_info *pli); db_pl_update(struct playlist_info *pli);

View File

@ -148,7 +148,7 @@ library_playlist_save(struct playlist_info *pli)
} }
if (pli->id == 0) if (pli->id == 0)
return db_pl_add(pli, NULL); return db_pl_add(pli);
else else
return db_pl_update(pli); return db_pl_update(pli);
} }

View File

@ -464,11 +464,16 @@ playlist_add(const char *path)
return -1; return -1;
ret = library_playlist_save(&pli); ret = library_playlist_save(&pli);
free_pli(&pli, 1);
if (ret < 0) if (ret < 0)
{
free_pli(&pli, 1);
return -1; return -1;
}
return db_pl_id_bypath(path); ret = pli.id;
free_pli(&pli, 1);
return ret;
} }

View File

@ -42,6 +42,7 @@
#include "logger.h" #include "logger.h"
#include "db.h" #include "db.h"
#include "library.h"
#include "library/filescanner.h" #include "library/filescanner.h"
#include "conffile.h" #include "conffile.h"
#include "misc.h" #include "misc.h"
@ -788,13 +789,11 @@ process_pls(plist_t playlists, const char *file)
{ {
plist_t pl; plist_t pl;
plist_t items; plist_t items;
struct playlist_info *pli; struct playlist_info pli;
char *name; char *name;
uint64_t id; uint64_t id;
int pl_id;
uint32_t alen; uint32_t alen;
uint32_t i; uint32_t i;
char virtual_path[PATH_MAX];
int ret; int ret;
alen = plist_array_get_size(playlists); alen = plist_array_get_size(playlists);
@ -834,28 +833,28 @@ process_pls(plist_t playlists, const char *file)
continue; continue;
} }
CHECK_NULL(L_SCAN, pli = calloc(1, sizeof(struct playlist_info))); playlist_fill(&pli, file);
pli->type = PL_PLAIN; free(pli.title);
pli->title = strdup(name); pli.title = strdup(name);
pli->path = strdup(file); free(pli.virtual_path);
snprintf(virtual_path, sizeof(virtual_path), "/file:%s/%s", file, name); pli.virtual_path = safe_asprintf("/file:%s/%s", file, name);
pli->virtual_path = strdup(virtual_path);
ret = db_pl_add(pli, &pl_id); ret = library_playlist_save(&pli);
free_pli(pli, 0);
if (ret < 0) if (ret < 0)
{ {
DPRINTF(E_LOG, L_SCAN, "Error adding iTunes playlist '%s' (%s)\n", name, file); DPRINTF(E_LOG, L_SCAN, "Error adding iTunes playlist '%s' (%s)\n", name, file);
free_pli(&pli, 1);
free(name); free(name);
continue; 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); free(name);
} }
} }