From 0a533d950814f720002f9ac3be642855b72f1846 Mon Sep 17 00:00:00 2001 From: cnsldv Date: Mon, 15 May 2017 08:08:54 +0200 Subject: [PATCH] Only refresh music DB if changes were made --- src/main.c | 8 ++++++-- src/music.c | 40 ++++++++++++++++++++++++++++------------ src/music.h | 5 +++-- 3 files changed, 37 insertions(+), 16 deletions(-) diff --git a/src/main.c b/src/main.c index 254b846..fc870fd 100644 --- a/src/main.c +++ b/src/main.c @@ -73,6 +73,7 @@ int main(void) int key = 0; int idx; int done = 0; + int madd = 0, mrem = 0; SceAppUtilInitParam init_param; SceAppUtilBootParam boot_param; @@ -102,10 +103,13 @@ int main(void) printf("Managing music, please wait\n"); idx = 0; while (music_dirs[idx]) { - add_music(music_dirs[idx]); + madd += add_music(music_dirs[idx]); idx++; } - clean_music(); + mrem = clean_music(); + if (madd > 0 || mrem > 0) { + refresh_music_db(); + } psvDebugScreenSetFgColor(COLOR_GREEN); printf("Done.\n"); psvDebugScreenSetFgColor(COLOR_WHITE); diff --git a/src/music.c b/src/music.c index 6cb7448..44f27c9 100644 --- a/src/music.c +++ b/src/music.c @@ -229,7 +229,7 @@ static int add_music_int(sqlite3 *db, const char *dir, int added) return added; } -void add_music(const char *dir) +int add_music(const char *dir) { int added; sqlite3 *db; @@ -242,21 +242,13 @@ void add_music(const char *dir) sqlite3_exec(db, "BEGIN", 0, 0, 0); added = add_music_int(db, dir, 0); printf("Added %d tracks\n", added); - - sqlite3_stmt *stmt; - const char *sql = refresh_db_sql; - ret = sqlite3_prepare_v2(db, sql, -1, &stmt, 0); - if (ret != SQLITE_OK) { - printf("Failed to execute %s, error %s\n", sql, sqlite3_errmsg(db)); - goto fail; - } - sqlite3_step(stmt); - sqlite3_exec(db, "COMMIT", 0, 0, 0); fail: + sqlite3_exec(db, "COMMIT", 0, 0, 0); sqlite3_close(db); + return added; } -void clean_music(void) +int clean_music(void) { int removed = 0; sqlite3 *db; @@ -297,6 +289,7 @@ void clean_music(void) sqlite3_close(db); printf("Removed %d tracks\n", removed); + return removed; } void empty_music(void) @@ -324,3 +317,26 @@ void empty_music(void) sqlite3_close(db); } +void refresh_music_db(void) +{ + sqlite3 *db; + int ret = sqlite3_open(MUSIC_DB, &db); + if (ret) { + printf("Failed to open the database: %s\n", sqlite3_errmsg(db)); + goto fail; + } + + sqlite3_stmt *stmt; + const char *sql = refresh_db_sql; + ret = sqlite3_prepare_v2(db, sql, -1, &stmt, 0); + if (ret != SQLITE_OK) { + printf("Failed to execute %s, error %s\n", sql, sqlite3_errmsg(db)); + goto fail; + } + sqlite3_step(stmt); + sqlite3_finalize(stmt); + +fail: + sqlite3_close(db); +} + diff --git a/src/music.h b/src/music.h index 5f7e928..a21d15b 100644 --- a/src/music.h +++ b/src/music.h @@ -2,9 +2,10 @@ #ifndef _MUSIC_H_ #define _MUSIC_H_ -void add_music(const char *dir); -void clean_music(void); +int add_music(const char *dir); +int clean_music(void); void empty_music(void); +void refresh_music_db(void); #endif