Directory.cxx 5.41 KB
Newer Older
1
/*
2
 * Copyright 2003-2018 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 "SongSort.hxx"
23
#include "Song.hxx"
Max Kellermann's avatar
Max Kellermann committed
24
#include "Mount.hxx"
25
#include "db/LightDirectory.hxx"
26
#include "song/LightSong.hxx"
27 28
#include "db/Uri.hxx"
#include "db/DatabaseLock.hxx"
Max Kellermann's avatar
Max Kellermann committed
29
#include "db/Interface.hxx"
30
#include "db/Selection.hxx"
31
#include "song/Filter.hxx"
32
#include "lib/icu/Collate.hxx"
Max Kellermann's avatar
Max Kellermann committed
33
#include "fs/Traits.hxx"
34
#include "util/Alloc.hxx"
35
#include "util/DeleteDisposer.hxx"
36

37 38
#include <assert.h>
#include <string.h>
39
#include <stdlib.h>
40

41
Directory::Directory(std::string &&_path_utf8, Directory *_parent)
42
	:parent(_parent),
43
	 path(std::move(_path_utf8))
44
{
Warren Dukes's avatar
Warren Dukes committed
45 46
}

47
Directory::~Directory()
Avuton Olrich's avatar
Avuton Olrich committed
48
{
Max Kellermann's avatar
Max Kellermann committed
49 50
	delete mounted_database;

51
	songs.clear_and_dispose(Song::Disposer());
52
	children.clear_and_dispose(DeleteDisposer());
53
}
54

55
void
56
Directory::Delete()
57
{
58
	assert(holding_db_lock());
59
	assert(parent != nullptr);
60

61
	parent->children.erase_and_dispose(parent->children.iterator_to(*this),
62
					   DeleteDisposer());
63 64
}

65
const char *
66
Directory::GetName() const noexcept
67
{
68
	assert(!IsRoot());
69

70
	return PathTraitsUTF8::GetBase(path.c_str());
71 72
}

73 74
Directory *
Directory::CreateChild(const char *name_utf8)
75
{
76
	assert(holding_db_lock());
77
	assert(name_utf8 != nullptr);
78 79
	assert(*name_utf8 != 0);

80 81 82
	std::string path_utf8 = IsRoot()
		? std::string(name_utf8)
		: PathTraitsUTF8::Build(GetPath(), name_utf8);
83

84
	Directory *child = new Directory(std::move(path_utf8), this);
85
	children.push_back(*child);
86
	return child;
87 88
}

89
const Directory *
90
Directory::FindChild(const char *name) const noexcept
Avuton Olrich's avatar
Avuton Olrich committed
91
{
92
	assert(holding_db_lock());
93

94 95 96
	for (const auto &child : children)
		if (strcmp(child.GetName(), name) == 0)
			return &child;
97

98
	return nullptr;
99
}
100

101
void
102
Directory::PruneEmpty() noexcept
103
{
104 105
	assert(holding_db_lock());

106 107
	for (auto child = children.begin(), end = children.end();
	     child != end;) {
108
		child->PruneEmpty();
109

110
		if (child->IsEmpty() && !child->IsMount())
111 112
			child = children.erase_and_dispose(child,
							   DeleteDisposer());
113 114
		else
			++child;
Warren Dukes's avatar
Warren Dukes committed
115 116 117
	}
}

118
Directory::LookupResult
119
Directory::LookupDirectory(const char *uri) noexcept
120
{
121
	assert(holding_db_lock());
122
	assert(uri != nullptr);
123

124
	if (isRootDirectory(uri))
125
		return { this, nullptr };
Warren Dukes's avatar
Warren Dukes committed
126

127
	char *duplicated = xstrdup(uri), *name = duplicated;
128

129
	Directory *d = this;
130
	while (true) {
131
		char *slash = strchr(name, '/');
132
		if (slash == name)
133
			break;
134

135
		if (slash != nullptr)
136 137
			*slash = '\0';

138 139 140 141 142 143 144 145 146 147
		Directory *tmp = d->FindChild(name);
		if (tmp == nullptr)
			/* not found */
			break;

		d = tmp;

		if (slash == nullptr) {
			/* found everything */
			name = nullptr;
148
			break;
149
		}
150 151

		name = slash + 1;
152
	}
Warren Dukes's avatar
Warren Dukes committed
153

154
	free(duplicated);
155

156 157 158 159 160
	const char *rest = name == nullptr
		? nullptr
		: uri + (name - duplicated);

	return { d, rest };
161 162
}

163
void
164
Directory::AddSong(Song *song)
165
{
166
	assert(holding_db_lock());
167
	assert(song != nullptr);
168
	assert(song->parent == this);
169

170
	songs.push_back(*song);
171 172 173
}

void
174
Directory::RemoveSong(Song *song) noexcept
175
{
176
	assert(holding_db_lock());
177
	assert(song != nullptr);
178
	assert(song->parent == this);
179

180
	songs.erase(songs.iterator_to(*song));
181 182
}

183
const Song *
184
Directory::FindSong(const char *name_utf8) const noexcept
185
{
186
	assert(holding_db_lock());
187
	assert(name_utf8 != nullptr);
188

189 190
	for (auto &song : songs) {
		assert(song.parent == this);
191

192 193
		if (strcmp(song.uri, name_utf8) == 0)
			return &song;
194 195
	}

196
	return nullptr;
197 198
}

199 200
gcc_pure
static bool
201
directory_cmp(const Directory &a, const Directory &b) noexcept
202
{
203
	return IcuCollate(a.path.c_str(), b.path.c_str()) < 0;
204 205
}

206
void
207
Directory::Sort() noexcept
Avuton Olrich's avatar
Avuton Olrich committed
208
{
209 210
	assert(holding_db_lock());

211 212
	children.sort(directory_cmp);
	song_list_sort(songs);
Avuton Olrich's avatar
Avuton Olrich committed
213

214 215
	for (auto &child : children)
		child.Sort();
Warren Dukes's avatar
Warren Dukes committed
216 217
}

218
void
219
Directory::Walk(bool recursive, const SongFilter *filter,
220
		VisitDirectory visit_directory, VisitSong visit_song,
221
		VisitPlaylist visit_playlist) const
Warren Dukes's avatar
Warren Dukes committed
222
{
Max Kellermann's avatar
Max Kellermann committed
223 224 225 226 227 228
	if (IsMount()) {
		assert(IsEmpty());

		/* TODO: eliminate this unlock/lock; it is necessary
		   because the child's SimpleDatabasePlugin::Visit()
		   call will lock it again */
229
		const ScopeDatabaseUnlock unlock;
230
		WalkMount(GetPath(), *mounted_database,
231
			  "", DatabaseSelection("", recursive, filter),
232 233 234
			  visit_directory, visit_song,
			  visit_playlist);
		return;
Max Kellermann's avatar
Max Kellermann committed
235 236
	}

237
	if (visit_song) {
238 239
		for (auto &song : songs){
			const LightSong song2 = song.Export();
240 241
			if (filter == nullptr || filter->Match(song2))
				visit_song(song2);
242
		}
Avuton Olrich's avatar
Avuton Olrich committed
243 244
	}

245
	if (visit_playlist) {
246
		for (const PlaylistInfo &p : playlists)
247
			visit_playlist(p, Export());
248 249
	}

250
	for (auto &child : children) {
251 252
		if (visit_directory)
			visit_directory(child.Export());
253

254 255 256 257
		if (recursive)
			child.Walk(recursive, filter,
				   visit_directory, visit_song,
				   visit_playlist);
258
	}
Warren Dukes's avatar
Warren Dukes committed
259
}
260 261

LightDirectory
262
Directory::Export() const noexcept
263 264 265
{
	return LightDirectory(GetPath(), mtime);
}