From c0ffec2fd1add50dae44c9e73a3c47d7d3998d15 Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Mon, 2 Mar 2009 15:42:21 +0100 Subject: database: db_load() returns GError Do error reporting with GLib's GError library. --- src/database.c | 55 ++++++++++++++++++++++++++++++++++++++++--------------- src/database.h | 4 +++- src/main.c | 6 +++++- 3 files changed, 48 insertions(+), 17 deletions(-) (limited to 'src') diff --git a/src/database.c b/src/database.c index d19ce834b..814811454 100644 --- a/src/database.c +++ b/src/database.c @@ -49,6 +49,15 @@ static struct directory *music_root; static time_t database_mtime; +/** + * The quark used for GError.domain. + */ +static inline GQuark +db_quark(void) +{ + return g_quark_from_static_string("database"); +} + void db_init(const char *path) { @@ -267,7 +276,7 @@ db_save(void) } bool -db_load(void) +db_load(GError **error) { FILE *fp = NULL; struct stat st; @@ -281,21 +290,24 @@ db_load(void) music_root = directory_new("", NULL); while (!(fp = fopen(database_path, "r")) && errno == EINTR) ; if (fp == NULL) { - g_warning("unable to open db file \"%s\": %s", - database_path, strerror(errno)); + g_set_error(error, db_quark(), errno, + "Failed to open database file \"%s\": %s", + database_path, strerror(errno)); return false; } /* get initial info */ - if (!fgets(buffer, sizeof(buffer), fp)) - g_error("Error reading db, fgets"); + if (!fgets(buffer, sizeof(buffer), fp)) { + fclose(fp); + g_set_error(error, db_quark(), 0, "Unexpected end of file"); + return false; + } g_strchomp(buffer); if (0 != strcmp(DIRECTORY_INFO_BEGIN, buffer)) { - g_warning("db info not found in db file; " - "you should recreate the db using --create-db"); - while (fclose(fp) && errno == EINTR) ; + fclose(fp); + g_set_error(error, db_quark(), 0, "Database corrupted"); return false; } @@ -304,14 +316,23 @@ db_load(void) g_strchomp(buffer); if (g_str_has_prefix(buffer, DIRECTORY_MPD_VERSION)) { - if (found_version) - g_error("already found version in db"); + if (found_version) { + fclose(fp); + g_set_error(error, db_quark(), 0, + "Duplicate version line"); + return false; + } + found_version = true; } else if (g_str_has_prefix(buffer, DIRECTORY_FS_CHARSET)) { const char *new_charset, *old_charset; - if (found_charset) - g_error("already found fs charset in db"); + if (found_charset) { + fclose(fp); + g_set_error(error, db_quark(), 0, + "Duplicate charset line"); + return false; + } found_charset = true; @@ -319,15 +340,19 @@ db_load(void) old_charset = path_get_fs_charset(); if (old_charset != NULL && strcmp(new_charset, old_charset)) { + fclose(fp); g_message("Existing database has charset \"%s\" " "instead of \"%s\"; " "discarding database file", new_charset, old_charset); return false; } - } else - g_error("unknown line in db info: %s", - buffer); + } else { + fclose(fp); + g_set_error(error, db_quark(), 0, + "Malformed line: %s", buffer); + return false; + } } g_debug("reading DB"); diff --git a/src/database.h b/src/database.h index 211cdc619..57e0e4b92 100644 --- a/src/database.h +++ b/src/database.h @@ -20,6 +20,8 @@ #ifndef MPD_DATABASE_H #define MPD_DATABASE_H +#include + #include #include @@ -66,7 +68,7 @@ bool db_save(void); bool -db_load(void); +db_load(GError **error); time_t db_get_mtime(void); diff --git a/src/main.c b/src/main.c index 1b84f68b9..cad9e5904 100644 --- a/src/main.c +++ b/src/main.c @@ -94,6 +94,7 @@ openDB(const Options *options) { const char *path = config_get_path(CONF_DB_FILE); bool ret; + GError *error = NULL; if (!mapper_has_music_directory()) { if (path != NULL) @@ -112,8 +113,11 @@ openDB(const Options *options) /* don't attempt to load the old database */ return false; - ret = db_load(); + ret = db_load(&error); if (!ret) { + g_warning("Failed to load database: %s", error->message); + g_error_free(error); + if (options->createDB < 0) g_error("can't open db file and using " "\"--no-create-db\" command line option"); -- cgit v1.2.3