directory_save.c 4.6 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_DIR "directory: "
32
#define DIRECTORY_MTIME "mtime: "
33 34 35
#define DIRECTORY_BEGIN "begin: "
#define DIRECTORY_END "end: "

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

45
void
46
directory_save(FILE *fp, const struct directory *directory)
47
{
48
	if (!directory_is_root(directory)) {
49 50 51
		fprintf(fp, DIRECTORY_MTIME "%lu\n",
			(unsigned long)directory->mtime);

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

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

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

		directory_save(fp, cur);

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

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

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
	const char *line;
85 86
	bool success;

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

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

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

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

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

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

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

	return directory;
}

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

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

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

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

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

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

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

	return true;
185
}