DirectorySave.cxx 4.56 KB
Newer Older
1
/*
2
 * Copyright 2003-2018 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 "util/ChronoUtil.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
gcc_const
static const char *
43
DeviceToTypeString(unsigned device) noexcept
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
{
	switch (device) {
	case DEVICE_INARCHIVE:
		return "archive";

	case DEVICE_CONTAINER:
		return "container";

	default:
		return nullptr;
	}
}

gcc_pure
static unsigned
59
ParseTypeString(const char *type) noexcept
60 61 62 63 64 65 66 67 68
{
	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 (!IsNegative(directory.mtime))
78
			os.Format(DIRECTORY_MTIME "%lu\n",
79
				  (unsigned long)std::chrono::system_clock::to_time_t(directory.mtime));
80

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

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

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

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

95
	playlist_vector_save(os, directory.playlists);
96

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

101 102 103
static bool
ParseLine(Directory &directory, const char *line)
{
104 105
	const char *p;
	if ((p = StringAfterPrefix(line, DIRECTORY_MTIME))) {
106 107 108
		const auto mtime = ParseUint64(p);
		if (mtime > 0)
			directory.mtime = std::chrono::system_clock::from_time_t(mtime);
109 110
	} 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
{
120 121
	if (parent.FindChild(name) != nullptr)
		throw FormatRuntimeError("Duplicate subdirectory '%s'", name);
122

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

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

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

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

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

	return directory;
}

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

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

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

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

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

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