From 68875ba600c635f985aa8deea7f940816a5e7645 Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Fri, 9 Sep 2011 23:28:27 +0200 Subject: database: return GError on failure --- src/database.c | 45 ++++++++++++++++++++++++++++----------------- src/database.h | 4 ++-- src/main.c | 5 +++-- src/update.c | 10 ++++++++-- 4 files changed, 41 insertions(+), 23 deletions(-) (limited to 'src') diff --git a/src/database.c b/src/database.c index 660d9768a..3899e32dd 100644 --- a/src/database.c +++ b/src/database.c @@ -151,7 +151,7 @@ db_walk(const char *name, } bool -db_check(void) +db_check(GError **error_r) { struct stat st; @@ -167,22 +167,27 @@ db_check(void) /* Check that the parent part of the path is a directory */ if (stat(dirPath, &st) < 0) { g_free(dirPath); - g_warning("Couldn't stat parent directory of db file " - "\"%s\": %s", database_path, strerror(errno)); + g_set_error(error_r, db_quark(), errno, + "Couldn't stat parent directory of db file " + "\"%s\": %s", + database_path, g_strerror(errno)); return false; } if (!S_ISDIR(st.st_mode)) { g_free(dirPath); - g_warning("Couldn't create db file \"%s\" because the " - "parent path is not a directory", database_path); + g_set_error(error_r, db_quark(), 0, + "Couldn't create db file \"%s\" because the " + "parent path is not a directory", + database_path); return false; } /* Check if we can write to the directory */ if (access(dirPath, X_OK | W_OK)) { - g_warning("Can't create db file in \"%s\": %s", - dirPath, strerror(errno)); + g_set_error(error_r, db_quark(), errno, + "Can't create db file in \"%s\": %s", + dirPath, g_strerror(errno)); g_free(dirPath); return false; } @@ -194,20 +199,24 @@ db_check(void) /* Path exists, now check if it's a regular file */ if (stat(database_path, &st) < 0) { - g_warning("Couldn't stat db file \"%s\": %s", - database_path, strerror(errno)); + g_set_error(error_r, db_quark(), errno, + "Couldn't stat db file \"%s\": %s", + database_path, g_strerror(errno)); return false; } if (!S_ISREG(st.st_mode)) { - g_warning("db file \"%s\" is not a regular file", database_path); + g_set_error(error_r, db_quark(), 0, + "db file \"%s\" is not a regular file", + database_path); return false; } /* And check that we can write to it */ if (access(database_path, R_OK | W_OK)) { - g_warning("Can't open db file \"%s\" for reading/writing: %s", - database_path, strerror(errno)); + g_set_error(error_r, db_quark(), errno, + "Can't open db file \"%s\" for reading/writing: %s", + database_path, g_strerror(errno)); return false; } @@ -215,7 +224,7 @@ db_check(void) } bool -db_save(void) +db_save(GError **error_r) { FILE *fp; struct stat st; @@ -234,8 +243,9 @@ db_save(void) fp = fopen(database_path, "w"); if (!fp) { - g_warning("unable to write to db file \"%s\": %s", - database_path, strerror(errno)); + g_set_error(error_r, db_quark(), errno, + "unable to write to db file \"%s\": %s", + database_path, g_strerror(errno)); return false; } @@ -253,8 +263,9 @@ db_save(void) directory_save(fp, music_root); if (ferror(fp)) { - g_warning("Failed to write to database file: %s", - strerror(errno)); + g_set_error(error_r, db_quark(), errno, + "Failed to write to database file: %s", + g_strerror(errno)); fclose(fp); return false; } diff --git a/src/database.h b/src/database.h index 75233e3a2..66423f46f 100644 --- a/src/database.h +++ b/src/database.h @@ -62,10 +62,10 @@ int db_walk(const char *name, int (*forEachDir)(struct directory *, void *), void *data); bool -db_check(void); +db_check(GError **error_r); bool -db_save(void); +db_save(GError **error_r); bool db_load(GError **error); diff --git a/src/main.c b/src/main.c index a57a19a81..e73f5ade0 100644 --- a/src/main.c +++ b/src/main.c @@ -182,9 +182,10 @@ glue_db_init_and_load(void) if (!ret) { g_warning("Failed to load database: %s", error->message); g_error_free(error); + error = NULL; - if (!db_check()) - exit(EXIT_FAILURE); + if (!db_check(&error)) + MPD_ERROR("%s", error->message); db_clear(); diff --git a/src/update.c b/src/update.c index 8fe6023ae..1a3060653 100644 --- a/src/update.c +++ b/src/update.c @@ -68,8 +68,14 @@ static void * update_task(void *_path) modified = update_walk(path, discard); - if (modified || !db_exists()) - db_save(); + if (modified || !db_exists()) { + GError *error = NULL; + if (!db_save(&error)) { + g_warning("Failed to save database: %s", + error->message); + g_error_free(error); + } + } if (path != NULL && *path != 0) g_debug("finished: %s", path); -- cgit v1.2.3