DirectorySave.cxx 4.35 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
#include "SongSave.hxx"
#include "PlaylistDatabase.hxx"
26
#include "TextFile.hxx"
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 Directory *directory)
47
{
48
	if (!directory->IsRoot()) {
49 50 51
		fprintf(fp, DIRECTORY_MTIME "%lu\n",
			(unsigned long)directory->mtime);

52
		fprintf(fp, "%s%s\n", DIRECTORY_BEGIN, directory->GetPath());
53 54
	}

55
	Directory *cur;
56
	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
	playlist_vector_save(fp, directory->playlists);
73

74 75
	if (!directory->IsRoot())
		fprintf(fp, DIRECTORY_END "%s\n", directory->GetPath());
76 77
}

78
static Directory *
79 80
directory_load_subdir(TextFile &file, Directory *parent, const char *name,
		      GError **error_r)
81 82 83
{
	bool success;

84
	if (parent->FindChild(name) != nullptr) {
85 86 87 88 89
		g_set_error(error_r, directory_quark(), 0,
			    "Duplicate subdirectory '%s'", name);
		return NULL;
	}

90
	Directory *directory = parent->CreateChild(name);
91

92
	const char *line = file.ReadLine();
93
	if (line == NULL) {
94 95
		g_set_error(error_r, directory_quark(), 0,
			    "Unexpected end of file");
96
		directory->Delete();
97 98 99
		return NULL;
	}

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

105
		line = file.ReadLine();
106
		if (line == NULL) {
107 108
			g_set_error(error_r, directory_quark(), 0,
				    "Unexpected end of file");
109
			directory->Delete();
110 111 112 113
			return NULL;
		}
	}

114
	if (!g_str_has_prefix(line, DIRECTORY_BEGIN)) {
115
		g_set_error(error_r, directory_quark(), 0,
116
			    "Malformed line: %s", line);
117
		directory->Delete();
118 119 120
		return NULL;
	}

121
	success = directory_load(file, directory, error_r);
122
	if (!success) {
123
		directory->Delete();
124
		return NULL;
125
	}
126 127 128 129

	return directory;
}

130
bool
131
directory_load(TextFile &file, Directory *directory, GError **error)
132
{
133
	const char *line;
134

135
	while ((line = file.ReadLine()) != NULL &&
136
	       !g_str_has_prefix(line, DIRECTORY_END)) {
137
		if (g_str_has_prefix(line, DIRECTORY_DIR)) {
138
			Directory *subdir =
139
				directory_load_subdir(file, directory,
140
						      line + sizeof(DIRECTORY_DIR) - 1,
141
						      error);
142
			if (subdir == NULL)
143
				return false;
144 145 146 147
		} else if (g_str_has_prefix(line, SONG_BEGIN)) {
			const char *name = line + sizeof(SONG_BEGIN) - 1;
			struct song *song;

148
			if (directory->FindSong(name) != nullptr) {
149 150
				g_set_error(error, directory_quark(), 0,
					    "Duplicate song '%s'", name);
151
				return false;
152 153
			}

154
			song = song_load(file, directory, name, error);
155
			if (song == NULL)
156
				return false;
157

158
			directory->AddSong(song);
159
		} else if (g_str_has_prefix(line, PLAYLIST_META_BEGIN)) {
160 161 162 163
			/* duplicate the name, because
			   playlist_metadata_load() will overwrite the
			   buffer */
			char *name = g_strdup(line + sizeof(PLAYLIST_META_BEGIN) - 1);
164

165 166
			if (!playlist_metadata_load(file, directory->playlists,
						    name, error)) {
167
				g_free(name);
168
				return false;
169 170 171
			}

			g_free(name);
172
		} else {
173
			g_set_error(error, directory_quark(), 0,
174
				    "Malformed line: %s", line);
175
			return false;
176 177
		}
	}
178 179

	return true;
180
}