Directory.cxx 5.54 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"
23
#include "Disposer.hxx"
Max Kellermann's avatar
Max Kellermann committed
24
#include "Mount.hxx"
25
#include "db/LightDirectory.hxx"
26
#include "song/LightSong.hxx"
27 28
#include "db/Uri.hxx"
#include "db/DatabaseLock.hxx"
Max Kellermann's avatar
Max Kellermann committed
29
#include "db/Interface.hxx"
30
#include "db/Selection.hxx"
31
#include "song/Filter.hxx"
32
#include "lib/icu/Collate.hxx"
Max Kellermann's avatar
Max Kellermann committed
33
#include "fs/Traits.hxx"
34
#include "util/Alloc.hxx"
35
#include "util/DeleteDisposer.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(SongDisposer());
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
	return PathTraitsUTF8::GetBase(path.c_str());
74 75
}

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

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

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

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

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

101
	return nullptr;
102
}
103

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

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

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

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

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

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

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

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

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

		d = tmp;

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

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

157
	free(duplicated);
158

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

	return { d, rest };
164 165
}

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

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

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

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

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

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

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

199
	return nullptr;
200 201
}

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

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

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

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

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

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

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

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

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

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

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