Directory.cxx 5.87 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
#include "util/StringCompare.hxx"
36

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

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

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

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

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

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

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

73 74 75 76 77 78 79 80 81
	if (parent->IsRoot())
		return path.c_str();

	assert(StringAfterPrefix(path.c_str(), parent->path.c_str()) != nullptr);
	assert(*StringAfterPrefix(path.c_str(), parent->path.c_str()) == PathTraitsUTF8::SEPARATOR);

	/* strip the parent directory path and the slash separator
	   from this directory's path, and the base name remains */
	return path.c_str() + parent->path.length() + 1;
82 83
}

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

91 92 93
	std::string path_utf8 = IsRoot()
		? std::string(name_utf8)
		: PathTraitsUTF8::Build(GetPath(), name_utf8);
94

95
	Directory *child = new Directory(std::move(path_utf8), this);
96
	children.push_back(*child);
97
	return child;
98 99
}

100
const Directory *
101
Directory::FindChild(const char *name) const noexcept
Avuton Olrich's avatar
Avuton Olrich committed
102
{
103
	assert(holding_db_lock());
104

105 106 107
	for (const auto &child : children)
		if (strcmp(child.GetName(), name) == 0)
			return &child;
108

109
	return nullptr;
110
}
111

112
void
113
Directory::PruneEmpty() noexcept
114
{
115 116
	assert(holding_db_lock());

117 118
	for (auto child = children.begin(), end = children.end();
	     child != end;) {
119
		child->PruneEmpty();
120

121
		if (child->IsEmpty() && !child->IsMount())
122 123
			child = children.erase_and_dispose(child,
							   DeleteDisposer());
124 125
		else
			++child;
Warren Dukes's avatar
Warren Dukes committed
126 127 128
	}
}

129
Directory::LookupResult
130
Directory::LookupDirectory(const char *uri) noexcept
131
{
132
	assert(holding_db_lock());
133
	assert(uri != nullptr);
134

135
	if (isRootDirectory(uri))
136
		return { this, nullptr };
Warren Dukes's avatar
Warren Dukes committed
137

138
	char *duplicated = xstrdup(uri), *name = duplicated;
139

140
	Directory *d = this;
141
	while (true) {
142
		char *slash = strchr(name, '/');
143
		if (slash == name)
144
			break;
145

146
		if (slash != nullptr)
147 148
			*slash = '\0';

149 150 151 152 153 154 155 156 157 158
		Directory *tmp = d->FindChild(name);
		if (tmp == nullptr)
			/* not found */
			break;

		d = tmp;

		if (slash == nullptr) {
			/* found everything */
			name = nullptr;
159
			break;
160
		}
161 162

		name = slash + 1;
163
	}
Warren Dukes's avatar
Warren Dukes committed
164

165
	free(duplicated);
166

167 168 169 170 171
	const char *rest = name == nullptr
		? nullptr
		: uri + (name - duplicated);

	return { d, rest };
172 173
}

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

181
	songs.push_back(*song);
182 183 184
}

void
185
Directory::RemoveSong(Song *song) noexcept
186
{
187
	assert(holding_db_lock());
188
	assert(song != nullptr);
189
	assert(song->parent == this);
190

191
	songs.erase(songs.iterator_to(*song));
192 193
}

194
const Song *
195
Directory::FindSong(const char *name_utf8) const noexcept
196
{
197
	assert(holding_db_lock());
198
	assert(name_utf8 != nullptr);
199

200 201
	for (auto &song : songs) {
		assert(song.parent == this);
202

203 204
		if (strcmp(song.uri, name_utf8) == 0)
			return &song;
205 206
	}

207
	return nullptr;
208 209
}

210 211
gcc_pure
static bool
212
directory_cmp(const Directory &a, const Directory &b) noexcept
213
{
214
	return IcuCollate(a.path.c_str(), b.path.c_str()) < 0;
215 216
}

217
void
218
Directory::Sort() noexcept
Avuton Olrich's avatar
Avuton Olrich committed
219
{
220 221
	assert(holding_db_lock());

222 223
	children.sort(directory_cmp);
	song_list_sort(songs);
Avuton Olrich's avatar
Avuton Olrich committed
224

225 226
	for (auto &child : children)
		child.Sort();
Warren Dukes's avatar
Warren Dukes committed
227 228
}

229
void
230
Directory::Walk(bool recursive, const SongFilter *filter,
231
		VisitDirectory visit_directory, VisitSong visit_song,
232
		VisitPlaylist visit_playlist) const
Warren Dukes's avatar
Warren Dukes committed
233
{
Max Kellermann's avatar
Max Kellermann committed
234 235 236 237 238 239
	if (IsMount()) {
		assert(IsEmpty());

		/* TODO: eliminate this unlock/lock; it is necessary
		   because the child's SimpleDatabasePlugin::Visit()
		   call will lock it again */
240
		const ScopeDatabaseUnlock unlock;
241
		WalkMount(GetPath(), *mounted_database,
242
			  "", DatabaseSelection("", recursive, filter),
243 244 245
			  visit_directory, visit_song,
			  visit_playlist);
		return;
Max Kellermann's avatar
Max Kellermann committed
246 247
	}

248
	if (visit_song) {
249 250
		for (auto &song : songs){
			const LightSong song2 = song.Export();
251 252
			if (filter == nullptr || filter->Match(song2))
				visit_song(song2);
253
		}
Avuton Olrich's avatar
Avuton Olrich committed
254 255
	}

256
	if (visit_playlist) {
257
		for (const PlaylistInfo &p : playlists)
258
			visit_playlist(p, Export());
259 260
	}

261
	for (auto &child : children) {
262 263
		if (visit_directory)
			visit_directory(child.Export());
264

265 266 267 268
		if (recursive)
			child.Walk(recursive, filter,
				   visit_directory, visit_song,
				   visit_playlist);
269
	}
Warren Dukes's avatar
Warren Dukes committed
270
}
271 272

LightDirectory
273
Directory::Export() const noexcept
274 275 276
{
	return LightDirectory(GetPath(), mtime);
}