directory_save.c 4.56 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright (C) 2003-2011 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
directory_save(FILE *fp, const struct directory *directory)
46
{
47
	if (!directory_is_root(directory)) {
48 49 50
		fprintf(fp, DIRECTORY_MTIME "%lu\n",
			(unsigned long)directory->mtime);

51 52
		fprintf(fp, "%s%s\n", DIRECTORY_BEGIN,
			directory_get_path(directory));
53 54
	}

55 56
	struct directory *cur;
	directory_for_each_child(cur, directory) {
57
		char *base = g_path_get_basename(cur->path);
58

59
		fprintf(fp, DIRECTORY_DIR "%s\n", base);
60
		g_free(base);
61 62 63 64 65

		directory_save(fp, cur);

		if (ferror(fp))
			return;
66 67
	}

68 69 70
	struct song *song;
	directory_for_each_song(song, directory)
		song_save(fp, song);
71

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

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

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

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

92
	struct directory *directory = directory_new_child(parent, name);
93

94 95
	line = read_text_line(fp, buffer);
	if (line == NULL) {
96 97
		g_set_error(error_r, directory_quark(), 0,
			    "Unexpected end of file");
98
		directory_delete(directory);
99 100 101
		return NULL;
	}

102
	if (g_str_has_prefix(line, DIRECTORY_MTIME)) {
103
		directory->mtime =
104
			g_ascii_strtoull(line + sizeof(DIRECTORY_MTIME) - 1,
105 106
					 NULL, 10);

107 108
		line = read_text_line(fp, buffer);
		if (line == NULL) {
109 110
			g_set_error(error_r, directory_quark(), 0,
				    "Unexpected end of file");
111
			directory_delete(directory);
112 113 114 115
			return NULL;
		}
	}

116
	if (!g_str_has_prefix(line, DIRECTORY_BEGIN)) {
117
		g_set_error(error_r, directory_quark(), 0,
118
			    "Malformed line: %s", line);
119
		directory_delete(directory);
120 121 122
		return NULL;
	}

123
	success = directory_load(fp, directory, buffer, error_r);
124
	if (!success) {
125
		directory_delete(directory);
126
		return NULL;
127
	}
128 129 130 131

	return directory;
}

132
bool
133 134
directory_load(FILE *fp, struct directory *directory,
	       GString *buffer, GError **error)
135
{
136
	const char *line;
137

138
	while ((line = read_text_line(fp, buffer)) != NULL &&
139
	       !g_str_has_prefix(line, DIRECTORY_END)) {
140
		if (g_str_has_prefix(line, DIRECTORY_DIR)) {
141 142
			struct directory *subdir =
				directory_load_subdir(fp, directory,
143 144
						      line + sizeof(DIRECTORY_DIR) - 1,
						      buffer, error);
145
			if (subdir == NULL)
146
				return false;
147 148 149 150
		} else if (g_str_has_prefix(line, SONG_BEGIN)) {
			const char *name = line + sizeof(SONG_BEGIN) - 1;
			struct song *song;

151
			if (directory_get_song(directory, name) != NULL) {
152 153 154 155 156 157 158 159
				g_set_error(error, directory_quark(), 0,
					    "Duplicate song '%s'", name);
				return NULL;
			}

			song = song_load(fp, directory, name,
					 buffer, error);
			if (song == NULL)
160
				return false;
161

162
			directory_add_song(directory, song);
163
		} else if (g_str_has_prefix(line, PLAYLIST_META_BEGIN)) {
164 165 166 167
			/* duplicate the name, because
			   playlist_metadata_load() will overwrite the
			   buffer */
			char *name = g_strdup(line + sizeof(PLAYLIST_META_BEGIN) - 1);
168 169

			if (!playlist_metadata_load(fp, &directory->playlists,
170 171
						    name, buffer, error)) {
				g_free(name);
172
				return false;
173 174 175
			}

			g_free(name);
176
		} else {
177
			g_set_error(error, directory_quark(), 0,
178
				    "Malformed line: %s", line);
179
			return false;
180 181
		}
	}
182 183

	return true;
184
}