Directory.cxx 5.39 KB
Newer Older
1
/*
2
 * Copyright 2003-2018 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

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

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

46
Directory::~Directory()
Avuton Olrich's avatar
Avuton Olrich committed
47
{
Max Kellermann's avatar
Max Kellermann committed
48 49
	delete mounted_database;

50
	songs.clear_and_dispose(Song::Disposer());
51
	children.clear_and_dispose(DeleteDisposer());
52
}
53

54
void
55
Directory::Delete()
56
{
57
	assert(holding_db_lock());
58
	assert(parent != nullptr);
59

60
	parent->children.erase_and_dispose(parent->children.iterator_to(*this),
61
					   DeleteDisposer());
62 63
}

64
const char *
65
Directory::GetName() const noexcept
66
{
67
	assert(!IsRoot());
68

69
	return PathTraitsUTF8::GetBase(path.c_str());
70 71
}

72 73
Directory *
Directory::CreateChild(const char *name_utf8)
74
{
75
	assert(holding_db_lock());
76
	assert(name_utf8 != nullptr);
77 78
	assert(*name_utf8 != 0);

79 80 81
	std::string path_utf8 = IsRoot()
		? std::string(name_utf8)
		: PathTraitsUTF8::Build(GetPath(), name_utf8);
82

83
	Directory *child = new Directory(std::move(path_utf8), this);
84
	children.push_back(*child);
85
	return child;
86 87
}

88
const Directory *
89
Directory::FindChild(const char *name) const noexcept
Avuton Olrich's avatar
Avuton Olrich committed
90
{
91
	assert(holding_db_lock());
92

93 94 95
	for (const auto &child : children)
		if (strcmp(child.GetName(), name) == 0)
			return &child;
96

97
	return nullptr;
98
}
99

100
void
101
Directory::PruneEmpty() noexcept
102
{
103 104
	assert(holding_db_lock());

105 106
	for (auto child = children.begin(), end = children.end();
	     child != end;) {
107
		child->PruneEmpty();
108

109
		if (child->IsEmpty() && !child->IsMount())
110 111
			child = children.erase_and_dispose(child,
							   DeleteDisposer());
112 113
		else
			++child;
Warren Dukes's avatar
Warren Dukes committed
114 115 116
	}
}

117
Directory::LookupResult
118
Directory::LookupDirectory(const char *uri) noexcept
119
{
120
	assert(holding_db_lock());
121
	assert(uri != nullptr);
122

123
	if (isRootDirectory(uri))
124
		return { this, nullptr };
Warren Dukes's avatar
Warren Dukes committed
125

126
	char *duplicated = xstrdup(uri), *name = duplicated;
127

128
	Directory *d = this;
129
	while (true) {
130
		char *slash = strchr(name, '/');
131
		if (slash == name)
132
			break;
133

134
		if (slash != nullptr)
135 136
			*slash = '\0';

137 138 139 140 141 142 143 144 145 146
		Directory *tmp = d->FindChild(name);
		if (tmp == nullptr)
			/* not found */
			break;

		d = tmp;

		if (slash == nullptr) {
			/* found everything */
			name = nullptr;
147
			break;
148
		}
149 150

		name = slash + 1;
151
	}
Warren Dukes's avatar
Warren Dukes committed
152

153
	free(duplicated);
154

155 156 157 158 159
	const char *rest = name == nullptr
		? nullptr
		: uri + (name - duplicated);

	return { d, rest };
160 161
}

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

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

void
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 181
}

182
const Song *
183
Directory::FindSong(const char *name_utf8) const noexcept
184
{
185
	assert(holding_db_lock());
186
	assert(name_utf8 != nullptr);
187

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

191 192
		if (strcmp(song.uri, name_utf8) == 0)
			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.c_str(), b.path.c_str()) < 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
		VisitDirectory visit_directory, VisitSong visit_song,
220
		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);
}