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;
/**
* 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");

View File

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

View File

@ -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");