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

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

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

46
Directory::~Directory() noexcept
Avuton Olrich's avatar
Avuton Olrich committed
47
{
48 49 50 51 52
	if (mounted_database != nullptr) {
		mounted_database->Close();
		mounted_database.reset();
	}

53
	songs.clear_and_dispose(Song::Disposer());
54
	children.clear_and_dispose(DeleteDisposer());
55
}
56

57
void
58
Directory::Delete() noexcept
59
{
60
	assert(holding_db_lock());
61
	assert(parent != nullptr);
62

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

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

72
	return PathTraitsUTF8::GetBase(path.c_str());
73 74
}

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

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

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

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

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

100
	return nullptr;
101
}
102

103
void
104
Directory::PruneEmpty() noexcept
105
{
106 107
	assert(holding_db_lock());

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

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

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

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

129
	char *duplicated = xstrdup(uri), *name = duplicated;
130

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

137
		if (slash != nullptr)
138 139
			*slash = '\0';

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

		d = tmp;

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

		name = slash + 1;
154
	}
Warren Dukes's avatar
Warren Dukes committed
155

156
	free(duplicated);
157

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

	return { d, rest };
163 164
}

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

172
	songs.push_back(*song);
173 174 175
}

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

182
	songs.erase(songs.iterator_to(*song));
183 184
}

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

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

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

198
	return nullptr;
199 200
}

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

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

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

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

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

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

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

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

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

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

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