DirectorySave.cxx 4.58 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 "song/DetachedSong.hxx"
26
#include "PlaylistDatabase.hxx"
27 28
#include "fs/io/TextFile.hxx"
#include "fs/io/BufferedOutputStream.hxx"
29
#include "util/ChronoUtil.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 53 54 55 56 57 58 59
{
	switch (device) {
	case DEVICE_INARCHIVE:
		return "archive";

	case DEVICE_CONTAINER:
		return "container";

	default:
		return nullptr;
	}
}

gcc_pure
static unsigned
60
ParseTypeString(const char *type) noexcept
61 62 63 64 65 66 67 68 69
{
	if (strcmp(type, "archive") == 0)
		return DEVICE_INARCHIVE;
	else if (strcmp(type, "container") == 0)
		return DEVICE_CONTAINER;
	else
		return 0;
}

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

78
		if (!IsNegative(directory.mtime))
79
			os.Format(DIRECTORY_MTIME "%lu\n",
80
				  (unsigned long)std::chrono::system_clock::to_time_t(directory.mtime));
81

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

85
	for (const auto &child : directory.children) {
86 87
		if (child.IsMount())
			continue;
88

89 90
		os.Format(DIRECTORY_DIR "%s\n", child.GetName());
		directory_save(os, child);
91 92
	}

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

96
	playlist_vector_save(os, directory.playlists);
97

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

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

	return true;
}

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

124
	Directory *directory = parent.CreateChild(name);
125

126 127 128 129 130
	try {
		while (true) {
			const char *line = file.ReadLine();
			if (line == nullptr)
				throw std::runtime_error("Unexpected end of file");
131

132 133
			if (StringStartsWith(line, DIRECTORY_BEGIN))
				break;
134

135 136
			if (!ParseLine(*directory, line))
				throw FormatRuntimeError("Malformed line: %s", line);
137
		}
138

139 140
		directory_load(file, *directory);
	} catch (...) {
141
		directory->Delete();
142
		throw;
143
	}
144 145 146 147

	return directory;
}

148 149
void
directory_load(TextFile &file, Directory &directory)
150
{
151
	const char *line;
152

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

161 162
			if (directory.FindSong(name) != nullptr)
				throw FormatRuntimeError("Duplicate song '%s'", name);
163

164 165 166
			auto audio_format = AudioFormat::Undefined();
			auto detached_song = song_load(file, name,
						       &audio_format);
167

168 169 170 171 172
			auto song = Song::NewFrom(std::move(*detached_song),
						  directory);
			song->audio_format = audio_format;

			directory.AddSong(song);
173 174
		} else if ((p = StringAfterPrefix(line, PLAYLIST_META_BEGIN))) {
			const char *name = p;
175
			playlist_metadata_load(file, directory.playlists, name);
176
		} else {
177
			throw FormatRuntimeError("Malformed line: %s", line);
178 179 180
		}
	}
}