directory_save.c 4.81 KB
Newer Older
1
/*
2
 * Copyright (C) 2003-2010 The Music Player Daemon Project
3
 * http://www.musicpd.org
4 5 6 7 8 9 10 11 12 13
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
14 15 16 17
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 19
 */

20
#include "config.h"
21 22 23
#include "directory_save.h"
#include "directory.h"
#include "song.h"
24
#include "text_file.h"
25
#include "song_save.h"
26
#include "playlist_database.h"
27

Max Kellermann's avatar
Max Kellermann committed
28 29 30
#include <assert.h>
#include <string.h>

31
#define DIRECTORY_MTIME "mtime: "
32 33 34
#define DIRECTORY_BEGIN "begin: "
#define DIRECTORY_END "end: "

35 36 37 38 39 40 41 42 43
/**
 * The quark used for GError.domain.
 */
static inline GQuark
directory_quark(void)
{
	return g_quark_from_static_string("directory");
}

44
void
45 46 47 48 49
directory_save(FILE *fp, struct directory *directory)
{
	struct dirvec *children = &directory->children;
	size_t i;

50
	if (!directory_is_root(directory)) {
51 52 53
		fprintf(fp, DIRECTORY_MTIME "%lu\n",
			(unsigned long)directory->mtime);

54 55
		fprintf(fp, "%s%s\n", DIRECTORY_BEGIN,
			directory_get_path(directory));
56 57 58 59
	}

	for (i = 0; i < children->nr; ++i) {
		struct directory *cur = children->base[i];
60
		char *base = g_path_get_basename(cur->path);
61

62
		fprintf(fp, DIRECTORY_DIR "%s\n", base);
63
		g_free(base);
64 65 66 67 68

		directory_save(fp, cur);

		if (ferror(fp))
			return;
69 70 71 72
	}

	songvec_save(fp, &directory->songs);

73 74
	playlist_vector_save(fp, &directory->playlists);

75 76 77
	if (!directory_is_root(directory))
		fprintf(fp, DIRECTORY_END "%s\n",
			directory_get_path(directory));
78 79
}

80 81
static struct directory *
directory_load_subdir(FILE *fp, struct directory *parent, const char *name,
82
		      GString *buffer, GError **error_r)
83 84
{
	struct directory *directory;
85
	const char *line;
86 87
	bool success;

88 89 90 91 92 93
	if (directory_get_child(parent, name) != NULL) {
		g_set_error(error_r, directory_quark(), 0,
			    "Duplicate subdirectory '%s'", name);
		return NULL;
	}

94 95 96 97 98 99 100 101 102
	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);
	}

103 104
	line = read_text_line(fp, buffer);
	if (line == NULL) {
105 106
		g_set_error(error_r, directory_quark(), 0,
			    "Unexpected end of file");
107
		directory_free(directory);
108 109 110
		return NULL;
	}

111
	if (g_str_has_prefix(line, DIRECTORY_MTIME)) {
112
		directory->mtime =
113
			g_ascii_strtoull(line + sizeof(DIRECTORY_MTIME) - 1,
114 115
					 NULL, 10);

116 117
		line = read_text_line(fp, buffer);
		if (line == NULL) {
118 119
			g_set_error(error_r, directory_quark(), 0,
				    "Unexpected end of file");
120
			directory_free(directory);
121 122 123 124
			return NULL;
		}
	}

125
	if (!g_str_has_prefix(line, DIRECTORY_BEGIN)) {
126
		g_set_error(error_r, directory_quark(), 0,
127
			    "Malformed line: %s", line);
128
		directory_free(directory);
129 130 131
		return NULL;
	}

132
	success = directory_load(fp, directory, buffer, error_r);
133 134
	if (!success) {
		directory_free(directory);
135
		return NULL;
136
	}
137 138 139 140

	return directory;
}

141
bool
142 143
directory_load(FILE *fp, struct directory *directory,
	       GString *buffer, GError **error)
144
{
145
	const char *line;
146

147
	while ((line = read_text_line(fp, buffer)) != NULL &&
148
	       !g_str_has_prefix(line, DIRECTORY_END)) {
149
		if (g_str_has_prefix(line, DIRECTORY_DIR)) {
150 151
			struct directory *subdir =
				directory_load_subdir(fp, directory,
152 153
						      line + sizeof(DIRECTORY_DIR) - 1,
						      buffer, error);
154
			if (subdir == NULL)
155
				return false;
156 157

			dirvec_add(&directory->children, subdir);
158 159 160 161 162 163 164 165 166 167 168 169 170
		} 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,
					    "Duplicate song '%s'", name);
				return NULL;
			}

			song = song_load(fp, directory, name,
					 buffer, error);
			if (song == NULL)
171
				return false;
172 173

			songvec_add(&directory->songs, song);
174
		} else if (g_str_has_prefix(line, PLAYLIST_META_BEGIN)) {
175 176 177 178
			/* duplicate the name, because
			   playlist_metadata_load() will overwrite the
			   buffer */
			char *name = g_strdup(line + sizeof(PLAYLIST_META_BEGIN) - 1);
179 180

			if (!playlist_metadata_load(fp, &directory->playlists,
181 182
						    name, buffer, error)) {
				g_free(name);
183
				return false;
184 185 186
			}

			g_free(name);
187
		} else {
188
			g_set_error(error, directory_quark(), 0,
189
				    "Malformed line: %s", line);
190
			return false;
191 192
		}
	}
193 194

	return true;
195
}