From 47585d0d0361f4f7ca7b294881ed909fc34d1521 Mon Sep 17 00:00:00 2001 From: ejurgensen Date: Sat, 14 Oct 2017 10:14:09 +0200 Subject: [PATCH] [db] Add methods db_mprintf() and db_snprintf() which wrap sqlite --- src/db.c | 42 ++++++++++++++++++++++++++++++ src/db.h | 8 +++++- src/library/filescanner_playlist.c | 14 +++++----- 3 files changed, 56 insertions(+), 8 deletions(-) diff --git a/src/db.c b/src/db.c index 66b7a119..b58732d0 100644 --- a/src/db.c +++ b/src/db.c @@ -353,6 +353,48 @@ db_escape_string(const char *str) return ret; } +// Basically a wrapper for sqlite3_mprintf() +char * +db_mprintf(const char *fmt, ...) +{ + char *query; + char *ret; + va_list va; + + va_start(va, fmt); + ret = sqlite3_vmprintf(fmt, va); + if (!ret) + { + DPRINTF(E_FATAL, L_MISC, "Out of memory for db_mprintf\n"); + abort(); + } + va_end(va); + + query = strdup(ret); + sqlite3_free(ret); + + return query; +} + +int +db_snprintf(char *s, int n, const char *fmt, ...) +{ + char *ret; + va_list va; + + if (n < 2) + return -1; + + va_start(va, fmt); + ret = sqlite3_vsnprintf(n, s, fmt, va); + va_end(va); + + if (!ret || (strlen(ret) == n - 1)) + return -1; + + return 0; +} + void free_pi(struct pairing_info *pi, int content_only) { diff --git a/src/db.h b/src/db.h index 29932ff9..1fea4dfe 100644 --- a/src/db.h +++ b/src/db.h @@ -424,7 +424,13 @@ struct db_queue_item }; char * -db_escape_string(const char *str); +db_escape_string(const char *str); // TODO Remove this, use db_mprintf instead + +char * +db_mprintf(const char *fmt, ...); + +int +db_snprintf(char *s, int n, const char *fmt, ...); void free_pi(struct pairing_info *pi, int content_only); diff --git a/src/library/filescanner_playlist.c b/src/library/filescanner_playlist.c index 611da973..47e83e90 100644 --- a/src/library/filescanner_playlist.c +++ b/src/library/filescanner_playlist.c @@ -149,13 +149,6 @@ process_regular_file(int pl_id, char *path) path[i] = '/'; } - ret = snprintf(filter, sizeof(filter), "(f.fname = '%s')", filename_from_path(path)); // TODO make case insensitive? - if (ret < 0 || ret >= sizeof(filter)) - { - DPRINTF(E_LOG, L_SCAN, "Playlist contains bad filename: '%s'\n", path); - return -1; - } - // Fast path - only works if there are not multiple items in the lib with the // same filename /* ret = db_pl_add_item_byfile(pl_id, filename_from_path(path)); @@ -165,6 +158,13 @@ process_regular_file(int pl_id, char *path) DPRINTF(E_DBG, L_SCAN, "Fast path adding '%s' to playlist did not work (ret=%d), now searching\n", path, ret); */ + ret = db_snprintf(filter, sizeof(filter), "f.fname = '%q'", filename_from_path(path)); // TODO make case insensitive? + if (ret < 0) + { + DPRINTF(E_LOG, L_SCAN, "Path in playlist is too long: '%s'\n", path); + return -1; + } + memset(&qp, 0, sizeof(struct query_params)); qp.type = Q_BROWSE_PATH;