directory: directory_load() returns GError

Do error reporting with GLib's GError library in this library, too.
This commit is contained in:
Max Kellermann 2009-03-02 15:42:42 +01:00
parent c0ffec2fd1
commit a1561252d0
3 changed files with 53 additions and 19 deletions

View File

@ -282,6 +282,7 @@ db_load(GError **error)
struct stat st; struct stat st;
char buffer[100]; char buffer[100];
bool found_charset = false, found_version = false; bool found_charset = false, found_version = false;
bool success;
assert(database_path != NULL); assert(database_path != NULL);
assert(music_root != NULL); assert(music_root != NULL);
@ -357,9 +358,12 @@ db_load(GError **error)
g_debug("reading DB"); g_debug("reading DB");
directory_load(fp, music_root); success = directory_load(fp, music_root, error);
while (fclose(fp) && errno == EINTR) ; while (fclose(fp) && errno == EINTR) ;
if (!success)
return false;
stats_update(); stats_update();
if (stat(database_path, &st) == 0) if (stat(database_path, &st) == 0)

View File

@ -19,12 +19,9 @@
#include "directory_save.h" #include "directory_save.h"
#include "directory.h" #include "directory.h"
#include "song.h" #include "song.h"
#include "log.h"
#include "path.h" #include "path.h"
#include "song_save.h" #include "song_save.h"
#include <glib.h>
#include <assert.h> #include <assert.h>
#include <string.h> #include <string.h>
@ -32,6 +29,15 @@
#define DIRECTORY_BEGIN "begin: " #define DIRECTORY_BEGIN "begin: "
#define DIRECTORY_END "end: " #define DIRECTORY_END "end: "
/**
* The quark used for GError.domain.
*/
static inline GQuark
directory_quark(void)
{
return g_quark_from_static_string("directory");
}
/* TODO error checking */ /* TODO error checking */
int int
directory_save(FILE *fp, struct directory *directory) directory_save(FILE *fp, struct directory *directory)
@ -71,12 +77,13 @@ directory_save(FILE *fp, struct directory *directory)
return 0; return 0;
} }
void bool
directory_load(FILE *fp, struct directory *directory) directory_load(FILE *fp, struct directory *directory, GError **error)
{ {
char buffer[MPD_PATH_MAX * 2]; char buffer[MPD_PATH_MAX * 2];
char key[MPD_PATH_MAX * 2]; char key[MPD_PATH_MAX * 2];
char *name; char *name;
bool success;
while (fgets(buffer, sizeof(buffer), fp) while (fgets(buffer, sizeof(buffer), fp)
&& !g_str_has_prefix(buffer, DIRECTORY_END)) { && !g_str_has_prefix(buffer, DIRECTORY_END)) {
@ -85,25 +92,38 @@ directory_load(FILE *fp, struct directory *directory)
g_strchomp(buffer); g_strchomp(buffer);
strcpy(key, &(buffer[strlen(DIRECTORY_DIR)])); strcpy(key, &(buffer[strlen(DIRECTORY_DIR)]));
if (!fgets(buffer, sizeof(buffer), fp)) if (!fgets(buffer, sizeof(buffer), fp)) {
FATAL("Error reading db, fgets\n"); g_set_error(error, directory_quark(), 0,
"Unexpected end of file");
return false;
}
if (g_str_has_prefix(buffer, DIRECTORY_MTIME)) { if (g_str_has_prefix(buffer, DIRECTORY_MTIME)) {
directory->mtime = directory->mtime =
g_ascii_strtoull(buffer + sizeof(DIRECTORY_MTIME) - 1, g_ascii_strtoull(buffer + sizeof(DIRECTORY_MTIME) - 1,
NULL, 10); NULL, 10);
if (!fgets(buffer, sizeof(buffer), fp)) if (!fgets(buffer, sizeof(buffer), fp)) {
FATAL("Error reading db, fgets\n"); g_set_error(error, directory_quark(), 0,
"Unexpected end of file");
return false;
}
}
if (!g_str_has_prefix(buffer, DIRECTORY_BEGIN)) {
g_set_error(error, directory_quark(), 0,
"Malformed line: %s", buffer);
return false;
} }
if (!g_str_has_prefix(buffer, DIRECTORY_BEGIN))
FATAL("Error reading db at line: %s\n", buffer);
g_strchomp(buffer); g_strchomp(buffer);
name = &(buffer[strlen(DIRECTORY_BEGIN)]); name = &(buffer[strlen(DIRECTORY_BEGIN)]);
if (!g_str_has_prefix(name, directory->path) != 0) if (!g_str_has_prefix(name, directory->path) != 0) {
FATAL("Wrong path in database: '%s' in '%s'\n", g_set_error(error, directory_quark(), 0,
"Wrong path in database: '%s' in '%s'",
name, directory->path); name, directory->path);
return false;
}
subdir = directory_get_child(directory, name); subdir = directory_get_child(directory, name);
if (subdir != NULL) { if (subdir != NULL) {
@ -112,11 +132,18 @@ directory_load(FILE *fp, struct directory *directory)
subdir = directory_new(name, directory); subdir = directory_new(name, directory);
dirvec_add(&directory->children, subdir); dirvec_add(&directory->children, subdir);
} }
directory_load(fp, subdir);
success = directory_load(fp, subdir, error);
if (!success)
return false;
} else if (g_str_has_prefix(buffer, SONG_BEGIN)) { } else if (g_str_has_prefix(buffer, SONG_BEGIN)) {
readSongInfoIntoList(fp, &directory->songs, directory); readSongInfoIntoList(fp, &directory->songs, directory);
} else { } else {
FATAL("Unknown line in db: %s\n", buffer); g_set_error(error, directory_quark(), 0,
"Malformed line: %s", buffer);
return false;
} }
} }
return true;
} }

View File

@ -19,6 +19,9 @@
#ifndef MPD_DIRECTORY_SAVE_H #ifndef MPD_DIRECTORY_SAVE_H
#define MPD_DIRECTORY_SAVE_H #define MPD_DIRECTORY_SAVE_H
#include <glib.h>
#include <stdbool.h>
#include <stdio.h> #include <stdio.h>
struct directory; struct directory;
@ -26,7 +29,7 @@ struct directory;
int int
directory_save(FILE *fp, struct directory *directory); directory_save(FILE *fp, struct directory *directory);
void bool
directory_load(FILE *fp, struct directory *directory); directory_load(FILE *fp, struct directory *directory, GError **error);
#endif #endif