DirectorySave.cxx 4.13 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright (C) 2003-2014 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
#include "fs/TextFile.hxx"
28
#include "util/StringUtil.hxx"
29
#include "util/NumberParser.hxx"
30 31
#include "util/Error.hxx"
#include "util/Domain.hxx"
32

33
#include <stddef.h>
34

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

40
static constexpr Domain directory_domain("directory");
41

42
void
43
directory_save(FILE *fp, const Directory &directory)
44
{
45
	if (!directory.IsRoot()) {
46
		fprintf(fp, DIRECTORY_MTIME "%lu\n",
47
			(unsigned long)directory.mtime);
48

49
		fprintf(fp, "%s%s\n", DIRECTORY_BEGIN, directory.GetPath());
50 51
	}

52
	Directory *cur;
53
	directory_for_each_child(cur, directory) {
54
		fprintf(fp, DIRECTORY_DIR "%s\n", cur->GetName());
55

56
		directory_save(fp, *cur);
57 58 59

		if (ferror(fp))
			return;
60 61
	}

62
	Song *song;
63
	directory_for_each_song(song, directory)
64
		song_save(fp, *song);
65

66
	playlist_vector_save(fp, directory.playlists);
67

68 69
	if (!directory.IsRoot())
		fprintf(fp, DIRECTORY_END "%s\n", directory.GetPath());
70 71
}

72
static Directory *
73
directory_load_subdir(TextFile &file, Directory &parent, const char *name,
74
		      Error &error)
75 76 77
{
	bool success;

78
	if (parent.FindChild(name) != nullptr) {
79 80
		error.Format(directory_domain,
			     "Duplicate subdirectory '%s'", name);
81
		return nullptr;
82 83
	}

84
	Directory *directory = parent.CreateChild(name);
85

86
	const char *line = file.ReadLine();
87
	if (line == nullptr) {
88
		error.Set(directory_domain, "Unexpected end of file");
89
		directory->Delete();
90
		return nullptr;
91 92
	}

93
	if (StringStartsWith(line, DIRECTORY_MTIME)) {
94
		directory->mtime =
95
			ParseUint64(line + sizeof(DIRECTORY_MTIME) - 1);
96

97
		line = file.ReadLine();
98
		if (line == nullptr) {
99
			error.Set(directory_domain, "Unexpected end of file");
100
			directory->Delete();
101
			return nullptr;
102 103 104
		}
	}

105
	if (!StringStartsWith(line, DIRECTORY_BEGIN)) {
106
		error.Format(directory_domain, "Malformed line: %s", line);
107
		directory->Delete();
108
		return nullptr;
109 110
	}

111
	success = directory_load(file, *directory, error);
112
	if (!success) {
113
		directory->Delete();
114
		return nullptr;
115
	}
116 117 118 119

	return directory;
}

120
bool
121
directory_load(TextFile &file, Directory &directory, Error &error)
122
{
123
	const char *line;
124

125
	while ((line = file.ReadLine()) != nullptr &&
126 127
	       !StringStartsWith(line, DIRECTORY_END)) {
		if (StringStartsWith(line, DIRECTORY_DIR)) {
128
			Directory *subdir =
129
				directory_load_subdir(file, directory,
130
						      line + sizeof(DIRECTORY_DIR) - 1,
131
						      error);
132
			if (subdir == nullptr)
133
				return false;
134
		} else if (StringStartsWith(line, SONG_BEGIN)) {
135 136
			const char *name = line + sizeof(SONG_BEGIN) - 1;

137
			if (directory.FindSong(name) != nullptr) {
138 139
				error.Format(directory_domain,
					     "Duplicate song '%s'", name);
140
				return false;
141 142
			}

143
			DetachedSong *song = song_load(file, name, error);
144
			if (song == nullptr)
145
				return false;
146

147
			directory.AddSong(Song::NewFrom(std::move(*song),
148
							directory));
149
			delete song;
150
		} else if (StringStartsWith(line, PLAYLIST_META_BEGIN)) {
151
			const char *name = line + sizeof(PLAYLIST_META_BEGIN) - 1;
152
			if (!playlist_metadata_load(file, directory.playlists,
153
						    name, error))
154
				return false;
155
		} else {
156 157
			error.Format(directory_domain,
				     "Malformed line: %s", line);
158
			return false;
159 160
		}
	}
161 162

	return true;
163
}