directory_save: moved code to directory_load_subdir()

This commit is contained in:
Max Kellermann 2009-11-01 15:34:12 +01:00
parent ad01e1249b
commit 22279127f9

View File

@ -78,64 +78,78 @@ directory_save(FILE *fp, struct directory *directory)
return 0;
}
bool
directory_load(FILE *fp, struct directory *directory, GError **error)
static struct directory *
directory_load_subdir(FILE *fp, struct directory *parent, const char *name,
GError **error_r)
{
char buffer[MPD_PATH_MAX * 2];
char key[MPD_PATH_MAX * 2];
char *name;
struct directory *directory;
bool success;
while (fgets(buffer, sizeof(buffer), fp)
&& !g_str_has_prefix(buffer, DIRECTORY_END)) {
if (g_str_has_prefix(buffer, DIRECTORY_DIR)) {
struct directory *subdir;
g_strchomp(buffer);
strcpy(key, &(buffer[strlen(DIRECTORY_DIR)]));
if (!fgets(buffer, sizeof(buffer), fp)) {
g_set_error(error, directory_quark(), 0,
g_set_error(error_r, directory_quark(), 0,
"Unexpected end of file");
return false;
return NULL;
}
if (g_str_has_prefix(buffer, DIRECTORY_MTIME)) {
directory->mtime =
parent->mtime =
g_ascii_strtoull(buffer + sizeof(DIRECTORY_MTIME) - 1,
NULL, 10);
if (!fgets(buffer, sizeof(buffer), fp)) {
g_set_error(error, directory_quark(), 0,
g_set_error(error_r, directory_quark(), 0,
"Unexpected end of file");
return false;
return NULL;
}
}
if (!g_str_has_prefix(buffer, DIRECTORY_BEGIN)) {
g_set_error(error, directory_quark(), 0,
g_set_error(error_r, directory_quark(), 0,
"Malformed line: %s", buffer);
return false;
return NULL;
}
g_strchomp(buffer);
name = &(buffer[strlen(DIRECTORY_BEGIN)]);
if (!g_str_has_prefix(name, directory->path) != 0) {
g_set_error(error, directory_quark(), 0,
if (!g_str_has_prefix(name, parent->path) != 0) {
g_set_error(error_r, directory_quark(), 0,
"Wrong path in database: '%s' in '%s'",
name, directory->path);
return false;
name, parent->path);
return NULL;
}
subdir = directory_get_child(directory, name);
if (subdir != NULL) {
assert(subdir->parent == directory);
directory = directory_get_child(parent, name);
if (directory != NULL) {
assert(directory->parent == parent);
} else {
subdir = directory_new(name, directory);
dirvec_add(&directory->children, subdir);
directory = directory_new(name, parent);
dirvec_add(&parent->children, directory);
}
success = directory_load(fp, subdir, error);
success = directory_load(fp, directory, error_r);
if (!success)
return NULL;
return directory;
}
bool
directory_load(FILE *fp, struct directory *directory, GError **error)
{
char buffer[MPD_PATH_MAX * 2];
bool success;
while (fgets(buffer, sizeof(buffer), fp)
&& !g_str_has_prefix(buffer, DIRECTORY_END)) {
g_strchomp(buffer);
if (g_str_has_prefix(buffer, DIRECTORY_DIR)) {
struct directory *subdir =
directory_load_subdir(fp, directory,
buffer + sizeof(DIRECTORY_DIR) - 1,
error);
if (subdir == NULL)
return false;
} else if (g_str_has_prefix(buffer, SONG_BEGIN)) {
success = songvec_load(fp, &directory->songs,