DirectorySave.cxx 4.51 KB
Newer Older
1
/*
2
 * Copyright (C) 2003-2013 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
#include "DirectorySave.hxx"
22
#include "Directory.hxx"
23
#include "song.h"
24 25 26 27
#include "SongSave.hxx"
#include "PlaylistDatabase.hxx"

extern "C" {
28
#include "text_file.h"
29
}
30

Max Kellermann's avatar
Max Kellermann committed
31 32 33
#include <assert.h>
#include <string.h>

34
#define DIRECTORY_DIR "directory: "
35
#define DIRECTORY_MTIME "mtime: "
36 37 38
#define DIRECTORY_BEGIN "begin: "
#define DIRECTORY_END "end: "

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

48
void
49
directory_save(FILE *fp, const struct directory *directory)
50
{
51
	if (!directory->IsRoot()) {
52 53 54
		fprintf(fp, DIRECTORY_MTIME "%lu\n",
			(unsigned long)directory->mtime);

55
		fprintf(fp, "%s%s\n", DIRECTORY_BEGIN, directory->GetPath());
56 57
	}

58 59
	struct directory *cur;
	directory_for_each_child(cur, directory) {
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 73
	struct song *song;
	directory_for_each_song(song, directory)
		song_save(fp, song);
74

75 76
	playlist_vector_save(fp, &directory->playlists);

77 78
	if (!directory->IsRoot())
		fprintf(fp, DIRECTORY_END "%s\n", directory->GetPath());
79 80
}

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

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

94
	struct directory *directory = parent->CreateChild(name);
95

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

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

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

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

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

	return directory;
}

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

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

153
			if (directory->FindSong(name) != nullptr) {
154 155
				g_set_error(error, directory_quark(), 0,
					    "Duplicate song '%s'", name);
156
				return false;
157 158 159 160 161
			}

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

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

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

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

	return true;
186
}