DirectorySave.cxx 4.96 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright (C) 2003-2014 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.hxx"
24
#include "SongSave.hxx"
25
#include "DetachedSong.hxx"
26
#include "PlaylistDatabase.hxx"
27 28
#include "fs/io/TextFile.hxx"
#include "fs/io/BufferedOutputStream.hxx"
29
#include "util/StringUtil.hxx"
30
#include "util/NumberParser.hxx"
31 32
#include "util/Error.hxx"
#include "util/Domain.hxx"
33

34
#include <stddef.h>
35
#include <string.h>
36

37
#define DIRECTORY_DIR "directory: "
38
#define DIRECTORY_TYPE "type: "
39
#define DIRECTORY_MTIME "mtime: "
40 41 42
#define DIRECTORY_BEGIN "begin: "
#define DIRECTORY_END "end: "

43
static constexpr Domain directory_domain("directory");
44

45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
gcc_const
static const char *
DeviceToTypeString(unsigned device)
{
	switch (device) {
	case DEVICE_INARCHIVE:
		return "archive";

	case DEVICE_CONTAINER:
		return "container";

	default:
		return nullptr;
	}
}

gcc_pure
static unsigned
ParseTypeString(const char *type)
{
	if (strcmp(type, "archive") == 0)
		return DEVICE_INARCHIVE;
	else if (strcmp(type, "container") == 0)
		return DEVICE_CONTAINER;
	else
		return 0;
}

73
void
74
directory_save(BufferedOutputStream &os, const Directory &directory)
75
{
76
	if (!directory.IsRoot()) {
77 78
		const char *type = DeviceToTypeString(directory.device);
		if (type != nullptr)
79
			os.Format(DIRECTORY_TYPE "%s\n", type);
80

81
		if (directory.mtime != 0)
82 83
			os.Format(DIRECTORY_MTIME "%lu\n",
				  (unsigned long)directory.mtime);
84

85
		os.Format("%s%s\n", DIRECTORY_BEGIN, directory.GetPath());
86 87
	}

88
	for (const auto &child : directory.children) {
89
		os.Format(DIRECTORY_DIR "%s\n", child.GetName());
90

91
		if (!child.IsMount())
92
			directory_save(os, child);
93

94
		if (!os.Check())
95
			return;
96 97
	}

98
	for (const auto &song : directory.songs)
99
		song_save(os, song);
100

101
	playlist_vector_save(os, directory.playlists);
102

103
	if (!directory.IsRoot())
104
		os.Format(DIRECTORY_END "%s\n", directory.GetPath());
105 106
}

107 108 109 110 111 112
static bool
ParseLine(Directory &directory, const char *line)
{
	if (StringStartsWith(line, DIRECTORY_MTIME)) {
		directory.mtime =
			ParseUint64(line + sizeof(DIRECTORY_MTIME) - 1);
113 114 115
	} else if (StringStartsWith(line, DIRECTORY_TYPE)) {
		directory.device =
			ParseTypeString(line + sizeof(DIRECTORY_TYPE) - 1);
116 117 118 119 120 121
	} else
		return false;

	return true;
}

122
static Directory *
123
directory_load_subdir(TextFile &file, Directory &parent, const char *name,
124
		      Error &error)
125 126 127
{
	bool success;

128
	if (parent.FindChild(name) != nullptr) {
129 130
		error.Format(directory_domain,
			     "Duplicate subdirectory '%s'", name);
131
		return nullptr;
132 133
	}

134
	Directory *directory = parent.CreateChild(name);
135

136 137
	while (true) {
		const char *line = file.ReadLine();
138
		if (line == nullptr) {
139
			error.Set(directory_domain, "Unexpected end of file");
140
			directory->Delete();
141
			return nullptr;
142 143
		}

144 145 146 147 148 149 150 151 152
		if (StringStartsWith(line, DIRECTORY_BEGIN))
			break;

		if (!ParseLine(*directory, line)) {
			error.Format(directory_domain,
				     "Malformed line: %s", line);
			directory->Delete();
			return nullptr;
		}
153 154
	}

155
	success = directory_load(file, *directory, error);
156
	if (!success) {
157
		directory->Delete();
158
		return nullptr;
159
	}
160 161 162 163

	return directory;
}

164
bool
165
directory_load(TextFile &file, Directory &directory, Error &error)
166
{
167
	const char *line;
168

169
	while ((line = file.ReadLine()) != nullptr &&
170 171
	       !StringStartsWith(line, DIRECTORY_END)) {
		if (StringStartsWith(line, DIRECTORY_DIR)) {
172
			Directory *subdir =
173
				directory_load_subdir(file, directory,
174
						      line + sizeof(DIRECTORY_DIR) - 1,
175
						      error);
176
			if (subdir == nullptr)
177
				return false;
178
		} else if (StringStartsWith(line, SONG_BEGIN)) {
179 180
			const char *name = line + sizeof(SONG_BEGIN) - 1;

181
			if (directory.FindSong(name) != nullptr) {
182 183
				error.Format(directory_domain,
					     "Duplicate song '%s'", name);
184
				return false;
185 186
			}

187
			DetachedSong *song = song_load(file, name, error);
188
			if (song == nullptr)
189
				return false;
190

191
			directory.AddSong(Song::NewFrom(std::move(*song),
192
							directory));
193
			delete song;
194
		} else if (StringStartsWith(line, PLAYLIST_META_BEGIN)) {
195
			const char *name = line + sizeof(PLAYLIST_META_BEGIN) - 1;
196
			if (!playlist_metadata_load(file, directory.playlists,
197
						    name, error))
198
				return false;
199
		} else {
200 201
			error.Format(directory_domain,
				     "Malformed line: %s", line);
202
			return false;
203 204
		}
	}
205 206

	return true;
207
}