DirectorySave.cxx 4.8 KB
Newer Older
1
/*
2
 * Copyright 2003-2016 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/StringCompare.hxx"
30
#include "util/NumberParser.hxx"
31 32
#include "util/Error.hxx"
#include "util/Domain.hxx"
33

34
#include <string.h>
35

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

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

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
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;
}

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

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

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

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

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

94
	for (const auto &song : directory.songs)
95
		song_save(os, song);
96

97
	playlist_vector_save(os, directory.playlists);
98

99
	if (!directory.IsRoot())
100
		os.Format(DIRECTORY_END "%s\n", directory.GetPath());
101 102
}

103 104 105
static bool
ParseLine(Directory &directory, const char *line)
{
106 107 108 109 110
	const char *p;
	if ((p = StringAfterPrefix(line, DIRECTORY_MTIME))) {
		directory.mtime = ParseUint64(p);
	} else if ((p = StringAfterPrefix(line, DIRECTORY_TYPE))) {
		directory.device = ParseTypeString(p);
111 112 113 114 115 116
	} else
		return false;

	return true;
}

117
static Directory *
118
directory_load_subdir(TextFile &file, Directory &parent, const char *name,
119
		      Error &error)
120 121 122
{
	bool success;

123
	if (parent.FindChild(name) != nullptr) {
124 125
		error.Format(directory_domain,
			     "Duplicate subdirectory '%s'", name);
126
		return nullptr;
127 128
	}

129
	Directory *directory = parent.CreateChild(name);
130

131 132
	while (true) {
		const char *line = file.ReadLine();
133
		if (line == nullptr) {
134
			error.Set(directory_domain, "Unexpected end of file");
135
			directory->Delete();
136
			return nullptr;
137 138
		}

139 140 141 142 143 144 145 146 147
		if (StringStartsWith(line, DIRECTORY_BEGIN))
			break;

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

150
	success = directory_load(file, *directory, error);
151
	if (!success) {
152
		directory->Delete();
153
		return nullptr;
154
	}
155 156 157 158

	return directory;
}

159
bool
160
directory_load(TextFile &file, Directory &directory, Error &error)
161
{
162
	const char *line;
163

164
	while ((line = file.ReadLine()) != nullptr &&
165
	       !StringStartsWith(line, DIRECTORY_END)) {
166 167
		const char *p;
		if ((p = StringAfterPrefix(line, DIRECTORY_DIR))) {
168
			Directory *subdir =
169
				directory_load_subdir(file, directory,
170
						      p, error);
171
			if (subdir == nullptr)
172
				return false;
173 174
		} else if ((p = StringAfterPrefix(line, SONG_BEGIN))) {
			const char *name = p;
175

176
			if (directory.FindSong(name) != nullptr) {
177 178
				error.Format(directory_domain,
					     "Duplicate song '%s'", name);
179
				return false;
180 181
			}

182
			DetachedSong *song = song_load(file, name, error);
183
			if (song == nullptr)
184
				return false;
185

186
			directory.AddSong(Song::NewFrom(std::move(*song),
187
							directory));
188
			delete song;
189 190
		} else if ((p = StringAfterPrefix(line, PLAYLIST_META_BEGIN))) {
			const char *name = p;
191
			if (!playlist_metadata_load(file, directory.playlists,
192
						    name, error))
193
				return false;
194
		} else {
195 196
			error.Format(directory_domain,
				     "Malformed line: %s", line);
197
			return false;
198 199
		}
	}
200 201

	return true;
202
}