Directory.cxx 5.71 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
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(std::string_view _uri) noexcept
131
{
132
	assert(holding_db_lock());
133

134
	if (isRootDirectory(_uri))
135
		return { this, _uri, {} };
Warren Dukes's avatar
Warren Dukes committed
136

137
	StringView uri(_uri);
138

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

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

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

		d = tmp;

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

158
	return { d, _uri.substr(0, uri.data - _uri.data()), uri };
159 160
}

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

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

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

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

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

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

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

194
	return nullptr;
195 196
}

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

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

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

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

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

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

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

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

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

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

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