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

View File

@ -19,12 +19,9 @@
#include "directory_save.h"
#include "directory.h"
#include "song.h"
#include "log.h"
#include "path.h"
#include "song_save.h"
#include <glib.h>
#include <assert.h>
#include <string.h>
@ -32,6 +29,15 @@
#define DIRECTORY_BEGIN "begin: "
#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 */
int
directory_save(FILE *fp, struct directory *directory)
@ -71,12 +77,13 @@ directory_save(FILE *fp, struct directory *directory)
return 0;
}
void
directory_load(FILE *fp, struct directory *directory)
bool
directory_load(FILE *fp, struct directory *directory, GError **error)
{
char buffer[MPD_PATH_MAX * 2];
char key[MPD_PATH_MAX * 2];
char *name;
bool success;
while (fgets(buffer, sizeof(buffer), fp)
&& !g_str_has_prefix(buffer, DIRECTORY_END)) {
@ -85,25 +92,38 @@ directory_load(FILE *fp, struct directory *directory)
g_strchomp(buffer);
strcpy(key, &(buffer[strlen(DIRECTORY_DIR)]));
if (!fgets(buffer, sizeof(buffer), fp))
FATAL("Error reading db, fgets\n");
if (!fgets(buffer, sizeof(buffer), fp)) {
g_set_error(error, directory_quark(), 0,
"Unexpected end of file");
return false;
}
if (g_str_has_prefix(buffer, DIRECTORY_MTIME)) {
directory->mtime =
g_ascii_strtoull(buffer + sizeof(DIRECTORY_MTIME) - 1,
NULL, 10);
if (!fgets(buffer, sizeof(buffer), fp))
FATAL("Error reading db, fgets\n");
if (!fgets(buffer, sizeof(buffer), fp)) {
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);
name = &(buffer[strlen(DIRECTORY_BEGIN)]);
if (!g_str_has_prefix(name, directory->path) != 0)
FATAL("Wrong path in database: '%s' in '%s'\n",
name, directory->path);
if (!g_str_has_prefix(name, directory->path) != 0) {
g_set_error(error, directory_quark(), 0,
"Wrong path in database: '%s' in '%s'",
name, directory->path);
return false;
}
subdir = directory_get_child(directory, name);
if (subdir != NULL) {
@ -112,11 +132,18 @@ directory_load(FILE *fp, struct directory *directory)
subdir = directory_new(name, directory);
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)) {
readSongInfoIntoList(fp, &directory->songs, directory);
} 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
#define MPD_DIRECTORY_SAVE_H
#include <glib.h>
#include <stdbool.h>
#include <stdio.h>
struct directory;
@ -26,7 +29,7 @@ struct directory;
int
directory_save(FILE *fp, struct directory *directory);
void
directory_load(FILE *fp, struct directory *directory);
bool
directory_load(FILE *fp, struct directory *directory, GError **error);
#endif