database: db_load() returns GError

Do error reporting with GLib's GError library.
This commit is contained in:
Max Kellermann 2009-03-02 15:42:21 +01:00
parent eb5b3ce553
commit c0ffec2fd1
3 changed files with 48 additions and 17 deletions

View File

@ -49,6 +49,15 @@ static struct directory *music_root;
static time_t database_mtime; 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 void
db_init(const char *path) db_init(const char *path)
{ {
@ -267,7 +276,7 @@ db_save(void)
} }
bool bool
db_load(void) db_load(GError **error)
{ {
FILE *fp = NULL; FILE *fp = NULL;
struct stat st; struct stat st;
@ -281,21 +290,24 @@ db_load(void)
music_root = directory_new("", NULL); music_root = directory_new("", NULL);
while (!(fp = fopen(database_path, "r")) && errno == EINTR) ; while (!(fp = fopen(database_path, "r")) && errno == EINTR) ;
if (fp == NULL) { if (fp == NULL) {
g_warning("unable to open db file \"%s\": %s", g_set_error(error, db_quark(), errno,
database_path, strerror(errno)); "Failed to open database file \"%s\": %s",
database_path, strerror(errno));
return false; return false;
} }
/* get initial info */ /* get initial info */
if (!fgets(buffer, sizeof(buffer), fp)) if (!fgets(buffer, sizeof(buffer), fp)) {
g_error("Error reading db, fgets"); fclose(fp);
g_set_error(error, db_quark(), 0, "Unexpected end of file");
return false;
}
g_strchomp(buffer); g_strchomp(buffer);
if (0 != strcmp(DIRECTORY_INFO_BEGIN, buffer)) { if (0 != strcmp(DIRECTORY_INFO_BEGIN, buffer)) {
g_warning("db info not found in db file; " fclose(fp);
"you should recreate the db using --create-db"); g_set_error(error, db_quark(), 0, "Database corrupted");
while (fclose(fp) && errno == EINTR) ;
return false; return false;
} }
@ -304,14 +316,23 @@ db_load(void)
g_strchomp(buffer); g_strchomp(buffer);
if (g_str_has_prefix(buffer, DIRECTORY_MPD_VERSION)) { if (g_str_has_prefix(buffer, DIRECTORY_MPD_VERSION)) {
if (found_version) if (found_version) {
g_error("already found version in db"); fclose(fp);
g_set_error(error, db_quark(), 0,
"Duplicate version line");
return false;
}
found_version = true; found_version = true;
} else if (g_str_has_prefix(buffer, DIRECTORY_FS_CHARSET)) { } else if (g_str_has_prefix(buffer, DIRECTORY_FS_CHARSET)) {
const char *new_charset, *old_charset; const char *new_charset, *old_charset;
if (found_charset) if (found_charset) {
g_error("already found fs charset in db"); fclose(fp);
g_set_error(error, db_quark(), 0,
"Duplicate charset line");
return false;
}
found_charset = true; found_charset = true;
@ -319,15 +340,19 @@ db_load(void)
old_charset = path_get_fs_charset(); old_charset = path_get_fs_charset();
if (old_charset != NULL if (old_charset != NULL
&& strcmp(new_charset, old_charset)) { && strcmp(new_charset, old_charset)) {
fclose(fp);
g_message("Existing database has charset \"%s\" " g_message("Existing database has charset \"%s\" "
"instead of \"%s\"; " "instead of \"%s\"; "
"discarding database file", "discarding database file",
new_charset, old_charset); new_charset, old_charset);
return false; return false;
} }
} else } else {
g_error("unknown line in db info: %s", fclose(fp);
buffer); g_set_error(error, db_quark(), 0,
"Malformed line: %s", buffer);
return false;
}
} }
g_debug("reading DB"); g_debug("reading DB");

View File

@ -20,6 +20,8 @@
#ifndef MPD_DATABASE_H #ifndef MPD_DATABASE_H
#define MPD_DATABASE_H #define MPD_DATABASE_H
#include <glib.h>
#include <sys/time.h> #include <sys/time.h>
#include <stdbool.h> #include <stdbool.h>
@ -66,7 +68,7 @@ bool
db_save(void); db_save(void);
bool bool
db_load(void); db_load(GError **error);
time_t time_t
db_get_mtime(void); db_get_mtime(void);

View File

@ -94,6 +94,7 @@ openDB(const Options *options)
{ {
const char *path = config_get_path(CONF_DB_FILE); const char *path = config_get_path(CONF_DB_FILE);
bool ret; bool ret;
GError *error = NULL;
if (!mapper_has_music_directory()) { if (!mapper_has_music_directory()) {
if (path != NULL) if (path != NULL)
@ -112,8 +113,11 @@ openDB(const Options *options)
/* don't attempt to load the old database */ /* don't attempt to load the old database */
return false; return false;
ret = db_load(); ret = db_load(&error);
if (!ret) { if (!ret) {
g_warning("Failed to load database: %s", error->message);
g_error_free(error);
if (options->createDB < 0) if (options->createDB < 0)
g_error("can't open db file and using " g_error("can't open db file and using "
"\"--no-create-db\" command line option"); "\"--no-create-db\" command line option");