Directory.cxx 6.11 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2021 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 "ExportedSong.hxx"
22
#include "SongSort.hxx"
23
#include "Song.hxx"
Max Kellermann's avatar
Max Kellermann committed
24
#include "Mount.hxx"
25 26 27
#include "db/LightDirectory.hxx"
#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/DeleteDisposer.hxx"
34
#include "util/StringCompare.hxx"
35
#include "util/StringView.hxx"
36

37 38
#include <cassert>

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

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

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

55
	songs.clear_and_dispose(DeleteDisposer());
56
	children.clear_and_dispose(DeleteDisposer());
57
}
58

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

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

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

74 75 76 77 78 79 80 81 82
	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;
83 84
}

85
Directory *
86
Directory::CreateChild(std::string_view name_utf8) noexcept
87
{
88
	assert(holding_db_lock());
89
	assert(!name_utf8.empty());
90

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

Max Kellermann's avatar
Max Kellermann committed
95
	auto *child = new Directory(std::move(path_utf8), this);
96
	children.push_back(*child);
97
	return child;
98 99
}

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

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

109
	return nullptr;
110
}
111

112 113
Song *
Directory::LookupTargetSong(std::string_view _target) noexcept
114 115 116 117 118
{
	StringView target{_target};

	if (target.SkipPrefix("../")) {
		if (parent == nullptr)
119
			return nullptr;
120

121
		return parent->LookupTargetSong(target);
122 123 124
	}

	/* sorry for the const_cast ... */
125 126
	const auto lr = LookupDirectory(target);
	return lr.directory->FindSong(lr.rest);
127 128
}

129
void
130
Directory::PruneEmpty() noexcept
131
{
132 133
	assert(holding_db_lock());

134 135
	for (auto child = children.begin(), end = children.end();
	     child != end;) {
136
		child->PruneEmpty();
137

138
		if (child->IsEmpty() && !child->IsMount())
139 140
			child = children.erase_and_dispose(child,
							   DeleteDisposer());
141 142
		else
			++child;
Warren Dukes's avatar
Warren Dukes committed
143 144 145
	}
}

146
Directory::LookupResult
147
Directory::LookupDirectory(std::string_view _uri) noexcept
148
{
149
	assert(holding_db_lock());
150

151
	if (isRootDirectory(_uri))
152
		return { this, _uri, {} };
Warren Dukes's avatar
Warren Dukes committed
153

154
	StringView uri(_uri);
155

156
	Directory *d = this;
157
	do {
158 159
		auto [name, rest] = uri.Split(PathTraitsUTF8::SEPARATOR);
		if (name.empty())
160
			break;
161

162 163 164 165 166 167 168
		Directory *tmp = d->FindChild(name);
		if (tmp == nullptr)
			/* not found */
			break;

		d = tmp;

169 170
		uri = rest;
	} while (uri != nullptr);
171

172
	return { d, _uri.substr(0, uri.data - _uri.data()), uri };
173 174
}

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

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

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

192
	songs.erase(songs.iterator_to(*song));
193
	return SongPtr(song);
194 195
}

196
const Song *
197
Directory::FindSong(std::string_view name_utf8) const noexcept
198
{
199
	assert(holding_db_lock());
200

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

204
		if (song.filename == name_utf8)
205
			return &song;
206 207
	}

208
	return nullptr;
209 210
}

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

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

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

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

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

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

250
	if (visit_song) {
251 252 253 254
		for (auto &song : songs) {
			if (hide_playlist_targets && song.in_playlist)
				continue;

255
			const auto song2 = song.Export();
256 257
			if (filter == nullptr || filter->Match(song2))
				visit_song(song2);
258
		}
Avuton Olrich's avatar
Avuton Olrich committed
259 260
	}

261
	if (visit_playlist) {
262
		for (const PlaylistInfo &p : playlists)
263
			visit_playlist(p, Export());
264 265
	}

266
	for (auto &child : children) {
267 268
		if (visit_directory)
			visit_directory(child.Export());
269

270 271
		if (recursive)
			child.Walk(recursive, filter,
272
				   hide_playlist_targets,
273 274
				   visit_directory, visit_song,
				   visit_playlist);
275
	}
Warren Dukes's avatar
Warren Dukes committed
276
}
277 278

LightDirectory
279
Directory::Export() const noexcept
280
{
281
	return {GetPath(), mtime};
282
}