Commit 68875ba6 authored by Max Kellermann's avatar Max Kellermann

database: return GError on failure

parent 2119a16e
......@@ -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;
}
......
......@@ -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);
......
......@@ -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();
......
......@@ -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);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment