Directory.cxx 5.7 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2020 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/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
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
	StringView uri(_uri);
139

140
	Directory *d = this;
141 142 143
	do {
		auto s = uri.Split(PathTraitsUTF8::SEPARATOR);
		if (s.first.empty())
144
			break;
145

146 147
		const auto name = s.first;
		const auto rest = s.second;
148

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

		d = tmp;

156 157
		uri = rest;
	} while (uri != nullptr);
158

159
	return { d, uri.data };
160 161
}

162
void
163
Directory::AddSong(SongPtr song) noexcept
164
{
165
	assert(holding_db_lock());
166
	assert(song != nullptr);
167
	assert(&song->parent == this);
168

169
	songs.push_back(*song.release());
170 171
}

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

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

183
const Song *
184
Directory::FindSong(std::string_view name_utf8) const noexcept
185
{
186
	assert(holding_db_lock());
187

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

191
		if (song.filename == name_utf8)
192
			return &song;
193 194
	}

195
	return nullptr;
196 197
}

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

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

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

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

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

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

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

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

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

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

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