diff options
Diffstat (limited to 'src/directory_save.c')
-rw-r--r-- | src/directory_save.c | 179 |
1 files changed, 112 insertions, 67 deletions
diff --git a/src/directory_save.c b/src/directory_save.c index 132508447..55896c289 100644 --- a/src/directory_save.c +++ b/src/directory_save.c @@ -1,5 +1,5 @@ /* - * Copyright (C) 2003-2009 The Music Player Daemon Project + * Copyright (C) 2003-2010 The Music Player Daemon Project * http://www.musicpd.org * * This program is free software; you can redistribute it and/or modify @@ -17,11 +17,13 @@ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */ +#include "config.h" #include "directory_save.h" #include "directory.h" #include "song.h" -#include "path.h" +#include "text_file.h" #include "song_save.h" +#include "playlist_database.h" #include <assert.h> #include <string.h> @@ -39,109 +41,152 @@ directory_quark(void) return g_quark_from_static_string("directory"); } -/* TODO error checking */ -int +void directory_save(FILE *fp, struct directory *directory) { struct dirvec *children = &directory->children; size_t i; - int retv; if (!directory_is_root(directory)) { fprintf(fp, DIRECTORY_MTIME "%lu\n", (unsigned long)directory->mtime); - retv = fprintf(fp, "%s%s\n", DIRECTORY_BEGIN, - directory_get_path(directory)); - if (retv < 0) - return -1; + fprintf(fp, "%s%s\n", DIRECTORY_BEGIN, + directory_get_path(directory)); } for (i = 0; i < children->nr; ++i) { struct directory *cur = children->base[i]; char *base = g_path_get_basename(cur->path); - retv = fprintf(fp, DIRECTORY_DIR "%s\n", base); + fprintf(fp, DIRECTORY_DIR "%s\n", base); g_free(base); - if (retv < 0) - return -1; - if (directory_save(fp, cur) < 0) - return -1; + + directory_save(fp, cur); + + if (ferror(fp)) + return; } songvec_save(fp, &directory->songs); - if (!directory_is_root(directory) && - fprintf(fp, DIRECTORY_END "%s\n", - directory_get_path(directory)) < 0) - return -1; - return 0; + playlist_vector_save(fp, &directory->playlists); + + if (!directory_is_root(directory)) + fprintf(fp, DIRECTORY_END "%s\n", + directory_get_path(directory)); } -bool -directory_load(FILE *fp, struct directory *directory, GError **error) +static struct directory * +directory_load_subdir(FILE *fp, struct directory *parent, const char *name, + GString *buffer, GError **error_r) { - char buffer[MPD_PATH_MAX * 2]; - char key[MPD_PATH_MAX * 2]; - char *name; + struct directory *directory; + const char *line; 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; + if (directory_get_child(parent, name) != NULL) { + g_set_error(error_r, directory_quark(), 0, + "Duplicate subdirectory '%s'", name); + return NULL; + } - g_strchomp(buffer); - strcpy(key, &(buffer[strlen(DIRECTORY_DIR)])); - if (!fgets(buffer, sizeof(buffer), fp)) { - g_set_error(error, directory_quark(), 0, - "Unexpected end of file"); - return false; - } + if (directory_is_root(parent)) { + directory = directory_new(name, parent); + } else { + char *path = g_strconcat(directory_get_path(parent), "/", + name, NULL); + directory = directory_new(path, parent); + g_free(path); + } - if (g_str_has_prefix(buffer, DIRECTORY_MTIME)) { - directory->mtime = - g_ascii_strtoull(buffer + sizeof(DIRECTORY_MTIME) - 1, - NULL, 10); + line = read_text_line(fp, buffer); + if (line == NULL) { + g_set_error(error_r, directory_quark(), 0, + "Unexpected end of file"); + directory_free(directory); + return NULL; + } - if (!fgets(buffer, sizeof(buffer), fp)) { - g_set_error(error, directory_quark(), 0, - "Unexpected end of file"); - return false; - } - } + if (g_str_has_prefix(line, DIRECTORY_MTIME)) { + directory->mtime = + g_ascii_strtoull(line + sizeof(DIRECTORY_MTIME) - 1, + NULL, 10); + + line = read_text_line(fp, buffer); + if (line == NULL) { + g_set_error(error_r, directory_quark(), 0, + "Unexpected end of file"); + directory_free(directory); + return NULL; + } + } - if (!g_str_has_prefix(buffer, DIRECTORY_BEGIN)) { - g_set_error(error, directory_quark(), 0, - "Malformed line: %s", buffer); + if (!g_str_has_prefix(line, DIRECTORY_BEGIN)) { + g_set_error(error_r, directory_quark(), 0, + "Malformed line: %s", line); + directory_free(directory); + return NULL; + } + + success = directory_load(fp, directory, buffer, error_r); + if (!success) { + directory_free(directory); + return NULL; + } + + return directory; +} + +bool +directory_load(FILE *fp, struct directory *directory, + GString *buffer, GError **error) +{ + const char *line; + + while ((line = read_text_line(fp, buffer)) != NULL && + !g_str_has_prefix(line, DIRECTORY_END)) { + if (g_str_has_prefix(line, DIRECTORY_DIR)) { + struct directory *subdir = + directory_load_subdir(fp, directory, + line + sizeof(DIRECTORY_DIR) - 1, + buffer, error); + if (subdir == NULL) return false; - } - g_strchomp(buffer); - name = &(buffer[strlen(DIRECTORY_BEGIN)]); - if (!g_str_has_prefix(name, directory->path) != 0) { + dirvec_add(&directory->children, subdir); + } else if (g_str_has_prefix(line, SONG_BEGIN)) { + const char *name = line + sizeof(SONG_BEGIN) - 1; + struct song *song; + + if (songvec_find(&directory->songs, name) != NULL) { g_set_error(error, directory_quark(), 0, - "Wrong path in database: '%s' in '%s'", - name, directory->path); - return false; + "Duplicate song '%s'", name); + return NULL; } - subdir = directory_get_child(directory, name); - if (subdir != NULL) { - assert(subdir->parent == directory); - } else { - subdir = directory_new(name, directory); - dirvec_add(&directory->children, subdir); - } + song = song_load(fp, directory, name, + buffer, error); + if (song == NULL) + return false; - success = directory_load(fp, subdir, error); - if (!success) + songvec_add(&directory->songs, song); + } else if (g_str_has_prefix(line, PLAYLIST_META_BEGIN)) { + /* duplicate the name, because + playlist_metadata_load() will overwrite the + buffer */ + char *name = g_strdup(line + sizeof(PLAYLIST_META_BEGIN) - 1); + + if (!playlist_metadata_load(fp, &directory->playlists, + name, buffer, error)) { + g_free(name); return false; - } else if (g_str_has_prefix(buffer, SONG_BEGIN)) { - readSongInfoIntoList(fp, &directory->songs, directory); + } + + g_free(name); } else { g_set_error(error, directory_quark(), 0, - "Malformed line: %s", buffer); + "Malformed line: %s", line); return false; } } |