Directory.cxx 5.53 KB
Newer Older
1
/*
2
 * Copyright 2003-2016 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 "config.h"
21
#include "Directory.hxx"
22
#include "SongSort.hxx"
23
#include "Song.hxx"
Max Kellermann's avatar
Max Kellermann committed
24
#include "Mount.hxx"
25 26 27 28
#include "db/LightDirectory.hxx"
#include "db/LightSong.hxx"
#include "db/Uri.hxx"
#include "db/DatabaseLock.hxx"
Max Kellermann's avatar
Max Kellermann committed
29
#include "db/Interface.hxx"
30
#include "SongFilter.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
#include "util/Error.hxx"
36

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

41
Directory::Directory(std::string &&_path_utf8, Directory *_parent)
42
	:parent(_parent),
43 44
	 mtime(0),
	 inode(0), device(0),
Max Kellermann's avatar
Max Kellermann committed
45 46
	 path(std::move(_path_utf8)),
	 mounted_database(nullptr)
47
{
Warren Dukes's avatar
Warren Dukes committed
48 49
}

50
Directory::~Directory()
Avuton Olrich's avatar
Avuton Olrich committed
51
{
Max Kellermann's avatar
Max Kellermann committed
52 53
	delete mounted_database;

54
	songs.clear_and_dispose(Song::Disposer());
55
	children.clear_and_dispose(DeleteDisposer());
56
}
57

58
void
59
Directory::Delete()
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
70
{
71
	assert(!IsRoot());
72

73
	return PathTraitsUTF8::GetBase(path.c_str());
74 75
}

76 77
Directory *
Directory::CreateChild(const char *name_utf8)
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 93
const Directory *
Directory::FindChild(const char *name) const
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()
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())
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)
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(Song *song)
168
{
169
	assert(holding_db_lock());
170
	assert(song != nullptr);
171
	assert(song->parent == this);
172

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

void
177
Directory::RemoveSong(Song *song)
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
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 204
gcc_pure
static bool
directory_cmp(const Directory &a, const Directory &b)
205
{
206
	return IcuCollate(a.path.c_str(), b.path.c_str()) < 0;
207 208
}

209
void
210
Directory::Sort()
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
bool
222
Directory::Walk(bool recursive, const SongFilter *filter,
223 224
		VisitDirectory visit_directory, VisitSong visit_song,
		VisitPlaylist visit_playlist,
225
		Error &error) const
Warren Dukes's avatar
Warren Dukes committed
226
{
227
	assert(!error.IsDefined());
228

Max Kellermann's avatar
Max Kellermann committed
229 230 231 232 233 234
	if (IsMount()) {
		assert(IsEmpty());

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

243
	if (visit_song) {
244 245
		for (auto &song : songs){
			const LightSong song2 = song.Export();
246 247
			if ((filter == nullptr || filter->Match(song2)) &&
			    !visit_song(song2, error))
248
				return false;
249
		}
Avuton Olrich's avatar
Avuton Olrich committed
250 251
	}

252
	if (visit_playlist) {
253
		for (const PlaylistInfo &p : playlists)
254
			if (!visit_playlist(p, Export(), error))
255 256 257
				return false;
	}

258
	for (auto &child : children) {
259
		if (visit_directory &&
260
		    !visit_directory(child.Export(), error))
261 262
			return false;

263
		if (recursive &&
264 265 266
		    !child.Walk(recursive, filter,
				visit_directory, visit_song, visit_playlist,
				error))
267
			return false;
268
	}
Avuton Olrich's avatar
Avuton Olrich committed
269

270
	return true;
Warren Dukes's avatar
Warren Dukes committed
271
}
272 273 274 275 276 277

LightDirectory
Directory::Export() const
{
	return LightDirectory(GetPath(), mtime);
}