DirectorySave.cxx 4.73 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2021 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 "DirectorySave.hxx"
21
#include "Directory.hxx"
22
#include "Song.hxx"
23
#include "SongSave.hxx"
24
#include "song/DetachedSong.hxx"
25
#include "PlaylistDatabase.hxx"
26 27
#include "fs/io/TextFile.hxx"
#include "fs/io/BufferedOutputStream.hxx"
28
#include "time/ChronoUtil.hxx"
29
#include "util/StringAPI.hxx"
30
#include "util/StringCompare.hxx"
31
#include "util/NumberParser.hxx"
32
#include "util/RuntimeError.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 43
gcc_const
static const char *
44
DeviceToTypeString(unsigned device) noexcept
45 46 47 48 49 50 51 52
{
	switch (device) {
	case DEVICE_INARCHIVE:
		return "archive";

	case DEVICE_CONTAINER:
		return "container";

53 54 55
	case DEVICE_PLAYLIST:
		return "playlist";

56 57 58 59 60 61 62
	default:
		return nullptr;
	}
}

gcc_pure
static unsigned
63
ParseTypeString(const char *type) noexcept
64
{
65
	if (StringIsEqual(type, "archive"))
66
		return DEVICE_INARCHIVE;
67
	else if (StringIsEqual(type, "container"))
68
		return DEVICE_CONTAINER;
69 70
	else if (StringIsEqual(type, "playlist"))
		return DEVICE_PLAYLIST;
71 72 73 74
	else
		return 0;
}

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

83
		if (!IsNegative(directory.mtime))
84
			os.Format(DIRECTORY_MTIME "%lu\n",
85
				  (unsigned long)std::chrono::system_clock::to_time_t(directory.mtime));
86

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

90
	for (const auto &child : directory.children) {
91 92
		if (child.IsMount())
			continue;
93

94 95
		os.Format(DIRECTORY_DIR "%s\n", child.GetName());
		directory_save(os, child);
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
static bool
ParseLine(Directory &directory, const char *line)
{
110 111
	const char *p;
	if ((p = StringAfterPrefix(line, DIRECTORY_MTIME))) {
112 113 114
		const auto mtime = ParseUint64(p);
		if (mtime > 0)
			directory.mtime = std::chrono::system_clock::from_time_t(mtime);
115 116
	} else if ((p = StringAfterPrefix(line, DIRECTORY_TYPE))) {
		directory.device = ParseTypeString(p);
117 118 119 120 121 122
	} else
		return false;

	return true;
}

123
static Directory *
124
directory_load_subdir(TextFile &file, Directory &parent, std::string_view name)
125
{
126
	if (parent.FindChild(name) != nullptr)
127 128
		throw FormatRuntimeError("Duplicate subdirectory '%.*s'",
					 int(name.size()), name.data());
129

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

132 133 134 135 136
	try {
		while (true) {
			const char *line = file.ReadLine();
			if (line == nullptr)
				throw std::runtime_error("Unexpected end of file");
137

138 139
			if (StringStartsWith(line, DIRECTORY_BEGIN))
				break;
140

141 142
			if (!ParseLine(*directory, line))
				throw FormatRuntimeError("Malformed line: %s", line);
143
		}
144

145 146
		directory_load(file, *directory);
	} catch (...) {
147
		directory->Delete();
148
		throw;
149
	}
150 151 152 153

	return directory;
}

154 155
void
directory_load(TextFile &file, Directory &directory)
156
{
157
	const char *line;
158

159
	while ((line = file.ReadLine()) != nullptr &&
160
	       !StringStartsWith(line, DIRECTORY_END)) {
161 162
		const char *p;
		if ((p = StringAfterPrefix(line, DIRECTORY_DIR))) {
163
			directory_load_subdir(file, directory, p);
164 165
		} else if ((p = StringAfterPrefix(line, SONG_BEGIN))) {
			const char *name = p;
166

167 168
			if (directory.FindSong(name) != nullptr)
				throw FormatRuntimeError("Duplicate song '%s'", name);
169

170
			std::string target;
171
			auto detached_song = song_load(file, name,
172
						       &target);
173

174
			auto song = std::make_unique<Song>(std::move(detached_song),
175
							   directory);
176
			song->target = std::move(target);
177

178
			directory.AddSong(std::move(song));
179 180
		} else if ((p = StringAfterPrefix(line, PLAYLIST_META_BEGIN))) {
			const char *name = p;
181
			playlist_metadata_load(file, directory.playlists, name);
182
		} else {
183
			throw FormatRuntimeError("Malformed line: %s", line);
184 185 186
		}
	}
}