Directory.cxx 5.75 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 "Uri.hxx"
24
#include "SongFilter.hxx"
25
#include "PlaylistVector.hxx"
Max Kellermann's avatar
Max Kellermann committed
26
#include "db/DatabaseLock.hxx"
27
#include "SongSort.hxx"
28
#include "Song.hxx"
29
#include "LightSong.hxx"
Max Kellermann's avatar
Max Kellermann committed
30
#include "fs/Traits.hxx"
31
#include "util/Alloc.hxx"
32
#include "util/Error.hxx"
33 34

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

38 39
#include <glib.h>

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

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

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

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

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

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

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

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

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

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

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

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

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

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

117
	return nullptr;
118
}
119

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

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

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

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

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

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

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

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

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

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

163
	free(duplicated);
164

165
	return d;
166 167
}

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

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

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

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

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

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

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

202
	return nullptr;
203 204
}

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

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

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

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

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

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

}

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

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

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

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

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

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

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

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

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

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

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