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, ...@@ -151,7 +151,7 @@ db_walk(const char *name,
} }
bool bool
db_check(void) db_check(GError **error_r)
{ {
struct stat st; struct stat st;
...@@ -167,22 +167,27 @@ db_check(void) ...@@ -167,22 +167,27 @@ db_check(void)
/* Check that the parent part of the path is a directory */ /* Check that the parent part of the path is a directory */
if (stat(dirPath, &st) < 0) { if (stat(dirPath, &st) < 0) {
g_free(dirPath); g_free(dirPath);
g_warning("Couldn't stat parent directory of db file " g_set_error(error_r, db_quark(), errno,
"\"%s\": %s", database_path, strerror(errno)); "Couldn't stat parent directory of db file "
"\"%s\": %s",
database_path, g_strerror(errno));
return false; return false;
} }
if (!S_ISDIR(st.st_mode)) { if (!S_ISDIR(st.st_mode)) {
g_free(dirPath); g_free(dirPath);
g_warning("Couldn't create db file \"%s\" because the " g_set_error(error_r, db_quark(), 0,
"parent path is not a directory", database_path); "Couldn't create db file \"%s\" because the "
"parent path is not a directory",
database_path);
return false; return false;
} }
/* Check if we can write to the directory */ /* Check if we can write to the directory */
if (access(dirPath, X_OK | W_OK)) { if (access(dirPath, X_OK | W_OK)) {
g_warning("Can't create db file in \"%s\": %s", g_set_error(error_r, db_quark(), errno,
dirPath, strerror(errno)); "Can't create db file in \"%s\": %s",
dirPath, g_strerror(errno));
g_free(dirPath); g_free(dirPath);
return false; return false;
} }
...@@ -194,20 +199,24 @@ db_check(void) ...@@ -194,20 +199,24 @@ db_check(void)
/* Path exists, now check if it's a regular file */ /* Path exists, now check if it's a regular file */
if (stat(database_path, &st) < 0) { if (stat(database_path, &st) < 0) {
g_warning("Couldn't stat db file \"%s\": %s", g_set_error(error_r, db_quark(), errno,
database_path, strerror(errno)); "Couldn't stat db file \"%s\": %s",
database_path, g_strerror(errno));
return false; return false;
} }
if (!S_ISREG(st.st_mode)) { 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; return false;
} }
/* And check that we can write to it */ /* And check that we can write to it */
if (access(database_path, R_OK | W_OK)) { if (access(database_path, R_OK | W_OK)) {
g_warning("Can't open db file \"%s\" for reading/writing: %s", g_set_error(error_r, db_quark(), errno,
database_path, strerror(errno)); "Can't open db file \"%s\" for reading/writing: %s",
database_path, g_strerror(errno));
return false; return false;
} }
...@@ -215,7 +224,7 @@ db_check(void) ...@@ -215,7 +224,7 @@ db_check(void)
} }
bool bool
db_save(void) db_save(GError **error_r)
{ {
FILE *fp; FILE *fp;
struct stat st; struct stat st;
...@@ -234,8 +243,9 @@ db_save(void) ...@@ -234,8 +243,9 @@ db_save(void)
fp = fopen(database_path, "w"); fp = fopen(database_path, "w");
if (!fp) { if (!fp) {
g_warning("unable to write to db file \"%s\": %s", g_set_error(error_r, db_quark(), errno,
database_path, strerror(errno)); "unable to write to db file \"%s\": %s",
database_path, g_strerror(errno));
return false; return false;
} }
...@@ -253,8 +263,9 @@ db_save(void) ...@@ -253,8 +263,9 @@ db_save(void)
directory_save(fp, music_root); directory_save(fp, music_root);
if (ferror(fp)) { if (ferror(fp)) {
g_warning("Failed to write to database file: %s", g_set_error(error_r, db_quark(), errno,
strerror(errno)); "Failed to write to database file: %s",
g_strerror(errno));
fclose(fp); fclose(fp);
return false; return false;
} }
......
...@@ -62,10 +62,10 @@ int db_walk(const char *name, ...@@ -62,10 +62,10 @@ int db_walk(const char *name,
int (*forEachDir)(struct directory *, void *), void *data); int (*forEachDir)(struct directory *, void *), void *data);
bool bool
db_check(void); db_check(GError **error_r);
bool bool
db_save(void); db_save(GError **error_r);
bool bool
db_load(GError **error); db_load(GError **error);
......
...@@ -182,9 +182,10 @@ glue_db_init_and_load(void) ...@@ -182,9 +182,10 @@ glue_db_init_and_load(void)
if (!ret) { if (!ret) {
g_warning("Failed to load database: %s", error->message); g_warning("Failed to load database: %s", error->message);
g_error_free(error); g_error_free(error);
error = NULL;
if (!db_check()) if (!db_check(&error))
exit(EXIT_FAILURE); MPD_ERROR("%s", error->message);
db_clear(); db_clear();
......
...@@ -68,8 +68,14 @@ static void * update_task(void *_path) ...@@ -68,8 +68,14 @@ static void * update_task(void *_path)
modified = update_walk(path, discard); modified = update_walk(path, discard);
if (modified || !db_exists()) if (modified || !db_exists()) {
db_save(); 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) if (path != NULL && *path != 0)
g_debug("finished: %s", path); 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