diff --git a/minidlna.c b/minidlna.c index 0901007..b216ec8 100644 --- a/minidlna.c +++ b/minidlna.c @@ -335,43 +335,67 @@ open_db(sqlite3 **sq3) static void check_db(sqlite3 *db, int new_db, pid_t *scanner_pid) { - int ret; + struct media_dir_s *media_path = NULL; char cmd[PATH_MAX*2]; - struct media_dir_s *media_path = NULL, *last_path; - struct album_art_name_s *art_names, *last_name; + char **result; + int i, rows = 0; + int ret; - /* Check if any new media dirs appeared */ - if( !new_db ) + if (!new_db) { + /* Check if any new media dirs appeared */ media_path = media_dirs; - while( media_path ) + while (media_path) { ret = sql_get_int_field(db, "SELECT ID from DETAILS where PATH = %Q", media_path->path); if (ret < 1) - break; + { + ret = 1; + goto rescan; + } media_path = media_path->next; } + /* Check if any media dirs disappeared */ + sql_get_table(db, "SELECT VALUE from SETTINGS where KEY = 'media_dir'", &result, &rows, NULL); + for (i=1; i <= rows; i++) + { + media_path = media_dirs; + while (media_path) + { + if (strcmp(result[i], media_path->path) == 0) + break; + media_path = media_path->next; + } + if (!media_path) + { + ret = 2; + sqlite3_free_table(result); + goto rescan; + } + } + sqlite3_free_table(result); } - if( media_path ) - ret = 1; - else - ret = db_upgrade(db); - if( ret != 0 ) + + ret = db_upgrade(db); + if (ret != 0) { - if( ret < 0 ) +rescan: + if (ret < 0) DPRINTF(E_WARN, L_GENERAL, "Creating new database at %s/files.db\n", db_path); - else if( ret == 1 ) + else if (ret == 1) DPRINTF(E_WARN, L_GENERAL, "New media_dir detected; rescanning...\n"); + else if (ret == 2) + DPRINTF(E_WARN, L_GENERAL, "Removed media_dir detected; rescanning...\n"); else DPRINTF(E_WARN, L_GENERAL, "Database version mismatch; need to recreate...\n"); sqlite3_close(db); snprintf(cmd, sizeof(cmd), "rm -rf %s/files.db %s/art_cache", db_path, db_path); - if( system(cmd) != 0 ) + if (system(cmd) != 0) DPRINTF(E_FATAL, L_GENERAL, "Failed to clean old file cache! Exiting...\n"); open_db(&db); - if( CreateDatabase() != 0 ) + if (CreateDatabase() != 0) DPRINTF(E_FATAL, L_GENERAL, "ERROR: Failed to create sqlite database! Exiting...\n"); #if USE_FORK scanning = 1; @@ -1053,7 +1077,7 @@ main(int argc, char **argv) ret = open_db(NULL); if (ret == 0) { - updateID = sql_get_int_field(db, "SELECT UPDATE_ID from SETTINGS"); + updateID = sql_get_int_field(db, "SELECT VALUE from SETTINGS where KEY = 'UPDATE_ID'"); if (updateID == -1) ret = -1; } @@ -1335,7 +1359,7 @@ shutdown: if (inotify_thread) pthread_join(inotify_thread, NULL); - sql_exec(db, "UPDATE SETTINGS set UPDATE_ID = %u", updateID); + sql_exec(db, "UPDATE SETTINGS set VALUE = '%u' where KEY = 'UPDATE_ID'", updateID); sqlite3_close(db); upnpevents_removeSubscribers(); diff --git a/scanner.c b/scanner.c index 07c1479..e6a47f7 100644 --- a/scanner.c +++ b/scanner.c @@ -567,7 +567,7 @@ CreateDatabase(void) ret = sql_exec(db, create_settingsTable_sqlite); if( ret != SQLITE_OK ) goto sql_failed; - ret = sql_exec(db, "INSERT into SETTINGS values (0, 0)"); + ret = sql_exec(db, "INSERT into SETTINGS values ('UPDATE_ID', '0')"); if( ret != SQLITE_OK ) goto sql_failed; for( i=0; containers[i]; i=i+3 ) @@ -790,6 +790,7 @@ start_scanner() strncpyt(name, media_path->path, sizeof(name)); GetFolderMetadata(basename(name), media_path->path, NULL, NULL, 0); ScanDirectory(media_path->path, NULL, media_path->types); + sql_exec(db, "INSERT into SETTINGS values (%Q, %Q)", "media_dir", media_path->path); media_path = media_path->next; } #ifdef READYNAS diff --git a/scanner_sqlite.h b/scanner_sqlite.h index 4d14d42..3a9aba1 100644 --- a/scanner_sqlite.h +++ b/scanner_sqlite.h @@ -80,8 +80,8 @@ char create_playlistTable_sqlite[] = "CREATE TABLE PLAYLISTS (" ");"; char create_settingsTable_sqlite[] = "CREATE TABLE SETTINGS (" - "UPDATE_ID INTEGER PRIMARY KEY DEFAULT 0, " - "FLAGS INTEGER DEFAULT 0" + "KEY TEXT NOT NULL, " + "VALUE TEXT" ");"; diff --git a/sql.c b/sql.c index df69158..1955acc 100644 --- a/sql.c +++ b/sql.c @@ -204,7 +204,6 @@ int db_upgrade(sqlite3 *db) { int db_vers; - int ret; db_vers = sql_get_int_field(db, "PRAGMA user_version"); @@ -214,31 +213,8 @@ db_upgrade(sqlite3 *db) return -2; if (db_vers < 1) return -1; - if (db_vers < 5) - return 5; - if (db_vers < 6) - { - DPRINTF(E_WARN, L_DB_SQL, "Updating DB version to v%d.\n", 6); - ret = sql_exec(db, "CREATE TABLE BOOKMARKS (" - "ID INTEGER PRIMARY KEY, " - "SEC INTEGER)"); - if( ret != SQLITE_OK ) - return 6; - } - if (db_vers < 7) - { - DPRINTF(E_WARN, L_DB_SQL, "Updating DB version to v%d.\n", 7); - ret = sql_exec(db, "ALTER TABLE DETAILS ADD rotation INTEGER"); - if( ret != SQLITE_OK ) - return 7; - } - if (db_vers < 8) - { - DPRINTF(E_WARN, L_DB_SQL, "Updating DB version to v%d.\n", 8); - ret = sql_exec(db, "UPDATE DETAILS set DLNA_PN = replace(DLNA_PN, ';DLNA.ORG_OP=01;DLNA.ORG_CI=0', '')"); - if( ret != SQLITE_OK ) - return 8; - } + if (db_vers < 9) + return 9; sql_exec(db, "PRAGMA user_version = %d", DB_VERSION); return 0; diff --git a/upnpglobalvars.h b/upnpglobalvars.h index 2f6ec58..e9d8e54 100644 --- a/upnpglobalvars.h +++ b/upnpglobalvars.h @@ -66,7 +66,7 @@ #define CLIENT_CACHE_SLOTS 20 #define USE_FORK 1 -#define DB_VERSION 8 +#define DB_VERSION 9 #ifdef ENABLE_NLS #define _(string) gettext(string)