DirectorySave.cxx 4.21 KB
Newer Older
1
/*
2
 * Copyright (C) 2003-2013 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 25
#include "SongSave.hxx"
#include "PlaylistDatabase.hxx"
26
#include "TextFile.hxx"
27
#include "util/NumberParser.hxx"
28 29
#include "util/Error.hxx"
#include "util/Domain.hxx"
30

31 32
#include <glib.h>

Max Kellermann's avatar
Max Kellermann committed
33 34 35
#include <assert.h>
#include <string.h>

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

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

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

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

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

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

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

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

67
	playlist_vector_save(fp, directory.playlists);
68

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

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

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

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

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

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

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

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

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

	return directory;
}

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

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

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

145
			song = song_load(file, &directory, name, error);
146
			if (song == nullptr)
147
				return false;
148

149
			directory.AddSong(song);
150
		} else if (g_str_has_prefix(line, PLAYLIST_META_BEGIN)) {
151 152 153 154
			/* duplicate the name, because
			   playlist_metadata_load() will overwrite the
			   buffer */
			char *name = g_strdup(line + sizeof(PLAYLIST_META_BEGIN) - 1);
155

156
			if (!playlist_metadata_load(file, directory.playlists,
157
						    name, error)) {
158
				g_free(name);
159
				return false;
160 161 162
			}

			g_free(name);
163
		} else {
164 165
			error.Format(directory_domain,
				     "Malformed line: %s", line);
166
			return false;
167 168
		}
	}
169 170

	return true;
171
}