DirectorySave.cxx 4.26 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2017 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
#include "util/RuntimeError.hxx"
32

33
#include <string.h>
34

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

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

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

77
		if (directory.mtime != 0)
78 79
			os.Format(DIRECTORY_MTIME "%lu\n",
				  (unsigned long)directory.mtime);
80

81
		os.Format("%s%s\n", DIRECTORY_BEGIN, directory.GetPath());
82 83
	}

84
	for (const auto &child : directory.children) {
85
		os.Format(DIRECTORY_DIR "%s\n", child.GetName());
86

87
		if (!child.IsMount())
88
			directory_save(os, child);
89 90
	}

91
	for (const auto &song : directory.songs)
92
		song_save(os, song);
93

94
	playlist_vector_save(os, directory.playlists);
95

96
	if (!directory.IsRoot())
97
		os.Format(DIRECTORY_END "%s\n", directory.GetPath());
98 99
}

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

	return true;
}

114
static Directory *
115
directory_load_subdir(TextFile &file, Directory &parent, const char *name)
116
{
117 118
	if (parent.FindChild(name) != nullptr)
		throw FormatRuntimeError("Duplicate subdirectory '%s'", name);
119

120
	Directory *directory = parent.CreateChild(name);
121

122 123 124 125 126
	try {
		while (true) {
			const char *line = file.ReadLine();
			if (line == nullptr)
				throw std::runtime_error("Unexpected end of file");
127

128 129
			if (StringStartsWith(line, DIRECTORY_BEGIN))
				break;
130

131 132
			if (!ParseLine(*directory, line))
				throw FormatRuntimeError("Malformed line: %s", line);
133
		}
134

135 136
		directory_load(file, *directory);
	} catch (...) {
137
		directory->Delete();
138
		throw;
139
	}
140 141 142 143

	return directory;
}

144 145
void
directory_load(TextFile &file, Directory &directory)
146
{
147
	const char *line;
148

149
	while ((line = file.ReadLine()) != nullptr &&
150
	       !StringStartsWith(line, DIRECTORY_END)) {
151 152
		const char *p;
		if ((p = StringAfterPrefix(line, DIRECTORY_DIR))) {
153
			directory_load_subdir(file, directory, p);
154 155
		} else if ((p = StringAfterPrefix(line, SONG_BEGIN))) {
			const char *name = p;
156

157 158
			if (directory.FindSong(name) != nullptr)
				throw FormatRuntimeError("Duplicate song '%s'", name);
159

160
			DetachedSong *song = song_load(file, name);
161

162
			directory.AddSong(Song::NewFrom(std::move(*song),
163
							directory));
164
			delete song;
165 166
		} else if ((p = StringAfterPrefix(line, PLAYLIST_META_BEGIN))) {
			const char *name = p;
167
			playlist_metadata_load(file, directory.playlists, name);
168
		} else {
169
			throw FormatRuntimeError("Malformed line: %s", line);
170 171 172
		}
	}
}