Directory.cxx 5.73 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
Warren Dukes's avatar
Warren Dukes committed
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.
Warren Dukes's avatar
Warren Dukes committed
18 19
 */

20
#include "config.h"
21
#include "Directory.hxx"
22
#include "LightDirectory.hxx"
23
#include "SongFilter.hxx"
24
#include "PlaylistVector.hxx"
Max Kellermann's avatar
Max Kellermann committed
25
#include "db/DatabaseLock.hxx"
26
#include "SongSort.hxx"
27
#include "Song.hxx"
28
#include "LightSong.hxx"
Max Kellermann's avatar
Max Kellermann committed
29
#include "fs/Traits.hxx"
30
#include "util/Alloc.hxx"
31
#include "util/Error.hxx"
32 33

extern "C" {
34
#include "util/list_sort.h"
35
}
36

37 38
#include <glib.h>

39 40
#include <assert.h>
#include <string.h>
41
#include <stdlib.h>
42

43
Directory::Directory(const char *_path_utf8, Directory *_parent)
44 45 46
	:parent(_parent),
	 mtime(0), have_stat(false),
	 path(_path_utf8)
47
{
48 49
	INIT_LIST_HEAD(&children);
	INIT_LIST_HEAD(&songs);
Warren Dukes's avatar
Warren Dukes committed
50 51
}

52
Directory::~Directory()
Avuton Olrich's avatar
Avuton Olrich committed
53
{
54
	Song *song, *ns;
55
	directory_for_each_song_safe(song, ns, *this)
56
		song->Free();
57

58
	Directory *child, *n;
59
	directory_for_each_child_safe(child, n, *this)
60
		delete child;
61
}
62

63
void
64
Directory::Delete()
65
{
66
	assert(holding_db_lock());
67
	assert(parent != nullptr);
68

69
	list_del(&siblings);
70
	delete this;
71 72
}

73
const char *
74
Directory::GetName() const
75
{
76
	assert(!IsRoot());
77

78
	return PathTraitsUTF8::GetBase(path.c_str());
79 80
}

81 82
Directory *
Directory::CreateChild(const char *name_utf8)
83
{
84
	assert(holding_db_lock());
85
	assert(name_utf8 != nullptr);
86 87 88 89
	assert(*name_utf8 != 0);

	char *allocated;
	const char *path_utf8;
90
	if (IsRoot()) {
91
		allocated = nullptr;
92 93
		path_utf8 = name_utf8;
	} else {
94
		allocated = g_strconcat(GetPath(),
95
					"/", name_utf8, nullptr);
96 97 98
		path_utf8 = allocated;
	}

99
	Directory *child = new Directory(path_utf8, this);
100 101
	g_free(allocated);

102 103
	list_add_tail(&child->siblings, &children);
	return child;
104 105
}

106 107
const Directory *
Directory::FindChild(const char *name) const
Avuton Olrich's avatar
Avuton Olrich committed
108
{
109
	assert(holding_db_lock());
110

111
	const Directory *child;
112
	directory_for_each_child(child, *this)
113
		if (strcmp(child->GetName(), name) == 0)
114 115
			return child;

116
	return nullptr;
117
}
118

119
void
120
Directory::PruneEmpty()
121
{
122 123
	assert(holding_db_lock());

124
	Directory *child, *n;
125
	directory_for_each_child_safe(child, n, *this) {
126
		child->PruneEmpty();
127

128 129
		if (child->IsEmpty())
			child->Delete();
Warren Dukes's avatar
Warren Dukes committed
130 131 132
	}
}

133 134
Directory *
Directory::LookupDirectory(const char *uri)
135
{
136
	assert(holding_db_lock());
137
	assert(uri != nullptr);
138

139
	if (isRootDirectory(uri))
140
		return this;
Warren Dukes's avatar
Warren Dukes committed
141

142
	char *duplicated = xstrdup(uri), *name = duplicated;
143

144
	Directory *d = this;
145
	while (1) {
146 147
		char *slash = strchr(name, '/');
		if (slash == name) {
148
			d = nullptr;
149
			break;
150 151
		}

152
		if (slash != nullptr)
153 154
			*slash = '\0';

155
		d = d->FindChild(name);
156
		if (d == nullptr || slash == nullptr)
157
			break;
158 159

		name = slash + 1;
160
	}
Warren Dukes's avatar
Warren Dukes committed
161

162
	free(duplicated);
163

164
	return d;
165 166
}

167
void
168
Directory::AddSong(Song *song)
169
{
170
	assert(holding_db_lock());
171
	assert(song != nullptr);
172
	assert(song->parent == this);
173

174
	list_add_tail(&song->siblings, &songs);
175 176 177
}

void
178
Directory::RemoveSong(Song *song)
179
{
180
	assert(holding_db_lock());
181
	assert(song != nullptr);
182
	assert(song->parent == this);
183

184
	list_del(&song->siblings);
185 186
}

187
const Song *
188
Directory::FindSong(const char *name_utf8) const
189
{
190
	assert(holding_db_lock());
191
	assert(name_utf8 != nullptr);
192

193
	Song *song;
194
	directory_for_each_song(song, *this) {
195
		assert(song->parent == this);
196

197
		if (strcmp(song->uri, name_utf8) == 0)
198 199 200
			return song;
	}

201
	return nullptr;
202 203
}

204
Song *
205
Directory::LookupSong(const char *uri)
206 207 208
{
	char *duplicated, *base;

209
	assert(holding_db_lock());
210
	assert(uri != nullptr);
211

212
	duplicated = xstrdup(uri);
213 214
	base = strrchr(duplicated, '/');

215
	Directory *d = this;
216
	if (base != nullptr) {
217
		*base++ = 0;
218 219
		d = d->LookupDirectory(duplicated);
		if (d == nullptr) {
220
			free(duplicated);
221
			return nullptr;
222 223 224 225
		}
	} else
		base = duplicated;

226
	Song *song = d->FindSong(base);
227
	assert(song == nullptr || song->parent == d);
228

229
	free(duplicated);
230 231 232 233
	return song;

}

234
static int
235
directory_cmp(gcc_unused void *priv,
236 237
	      struct list_head *_a, struct list_head *_b)
{
238 239
	const Directory *a = (const Directory *)_a;
	const Directory *b = (const Directory *)_b;
240
	return g_utf8_collate(a->path.c_str(), b->path.c_str());
241 242
}

243
void
244
Directory::Sort()
Avuton Olrich's avatar
Avuton Olrich committed
245
{
246 247
	assert(holding_db_lock());

248
	list_sort(nullptr, &children, directory_cmp);
249
	song_list_sort(&songs);
Avuton Olrich's avatar
Avuton Olrich committed
250

251
	Directory *child;
252
	directory_for_each_child(child, *this)
253
		child->Sort();
Warren Dukes's avatar
Warren Dukes committed
254 255
}

256
bool
257
Directory::Walk(bool recursive, const SongFilter *filter,
258 259
		VisitDirectory visit_directory, VisitSong visit_song,
		VisitPlaylist visit_playlist,
260
		Error &error) const
Warren Dukes's avatar
Warren Dukes committed
261
{
262
	assert(!error.IsDefined());
263

264
	if (visit_song) {
265
		Song *song;
266
		directory_for_each_song(song, *this) {
267
			const LightSong song2 = song->Export();
268 269
			if ((filter == nullptr || filter->Match(song2)) &&
			    !visit_song(song2, error))
270
				return false;
271
		}
Avuton Olrich's avatar
Avuton Olrich committed
272 273
	}

274
	if (visit_playlist) {
275
		for (const PlaylistInfo &p : playlists)
276
			if (!visit_playlist(p, Export(), error))
277 278 279
				return false;
	}

280
	Directory *child;
281
	directory_for_each_child(child, *this) {
282
		if (visit_directory &&
283
		    !visit_directory(child->Export(), error))
284 285
			return false;

286
		if (recursive &&
287
		    !child->Walk(recursive, filter,
288
				 visit_directory, visit_song, visit_playlist,
289
				 error))
290
			return false;
291
	}
Avuton Olrich's avatar
Avuton Olrich committed
292

293
	return true;
Warren Dukes's avatar
Warren Dukes committed
294
}
295 296 297 298 299 300

LightDirectory
Directory::Export() const
{
	return LightDirectory(GetPath(), mtime);
}