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; return 0;
} }
bool static struct directory *
directory_load(FILE *fp, struct directory *directory, GError **error) directory_load_subdir(FILE *fp, struct directory *parent, const char *name,
GError **error_r)
{ {
char buffer[MPD_PATH_MAX * 2]; char buffer[MPD_PATH_MAX * 2];
char key[MPD_PATH_MAX * 2]; struct directory *directory;
char *name;
bool success; 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)) { 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"); "Unexpected end of file");
return false; return NULL;
} }
if (g_str_has_prefix(buffer, DIRECTORY_MTIME)) { if (g_str_has_prefix(buffer, DIRECTORY_MTIME)) {
directory->mtime = parent->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)) {
g_set_error(error, directory_quark(), 0, g_set_error(error_r, directory_quark(), 0,
"Unexpected end of file"); "Unexpected end of file");
return false; return NULL;
} }
} }
if (!g_str_has_prefix(buffer, DIRECTORY_BEGIN)) { 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); "Malformed line: %s", buffer);
return false; return NULL;
} }
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, parent->path) != 0) {
g_set_error(error, directory_quark(), 0, g_set_error(error_r, directory_quark(), 0,
"Wrong path in database: '%s' in '%s'", "Wrong path in database: '%s' in '%s'",
name, directory->path); name, parent->path);
return false; return NULL;
} }
subdir = directory_get_child(directory, name); directory = directory_get_child(parent, name);
if (subdir != NULL) { if (directory != NULL) {
assert(subdir->parent == directory); assert(directory->parent == parent);
} else { } else {
subdir = directory_new(name, directory); directory = directory_new(name, parent);
dirvec_add(&directory->children, subdir); dirvec_add(&parent->children, directory);
} }
success = directory_load(fp, subdir, error); success = directory_load(fp, directory, error_r);
if (!success) 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; return false;
} else if (g_str_has_prefix(buffer, SONG_BEGIN)) { } else if (g_str_has_prefix(buffer, SONG_BEGIN)) {
success = songvec_load(fp, &directory->songs, success = songvec_load(fp, &directory->songs,