Directory.cxx 5.48 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright (C) 2003-2015 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/Error.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 43
	 mtime(0),
	 inode(0), device(0),
Max Kellermann's avatar
Max Kellermann committed
44 45
	 path(std::move(_path_utf8)),
	 mounted_database(nullptr)
46
{
Warren Dukes's avatar
Warren Dukes committed
47 48
}

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

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

57
void
58
Directory::Delete()
59
{
60
	assert(holding_db_lock());
61
	assert(parent != nullptr);
62

63 64
	parent->children.erase_and_dispose(parent->children.iterator_to(*this),
					   Disposer());
65 66
}

67
const char *
68
Directory::GetName() const
69
{
70
	assert(!IsRoot());
71

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

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

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

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

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

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

100
	return nullptr;
101
}
102

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

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

112
		if (child->IsEmpty())
113 114 115
			child = children.erase_and_dispose(child, Disposer());
		else
			++child;
Warren Dukes's avatar
Warren Dukes committed
116 117 118
	}
}

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

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

128
	char *duplicated = xstrdup(uri), *name = duplicated;
129

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

136
		if (slash != nullptr)
137 138
			*slash = '\0';

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

		d = tmp;

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

		name = slash + 1;
153
	}
Warren Dukes's avatar
Warren Dukes committed
154

155
	free(duplicated);
156

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

	return { d, rest };
162 163
}

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

171
	songs.push_back(*song);
172 173 174
}

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

181
	songs.erase(songs.iterator_to(*song));
182 183
}

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

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

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

197
	return nullptr;
198 199
}

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

207
void
208
Directory::Sort()
Avuton Olrich's avatar
Avuton Olrich committed
209
{
210 211
	assert(holding_db_lock());

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

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

219
bool
220
Directory::Walk(bool recursive, const SongFilter *filter,
221 222
		VisitDirectory visit_directory, VisitSong visit_song,
		VisitPlaylist visit_playlist,
223
		Error &error) const
Warren Dukes's avatar
Warren Dukes committed
224
{
225
	assert(!error.IsDefined());
226

Max Kellermann's avatar
Max Kellermann committed
227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242
	if (IsMount()) {
		assert(IsEmpty());

		/* TODO: eliminate this unlock/lock; it is necessary
		   because the child's SimpleDatabasePlugin::Visit()
		   call will lock it again */
		db_unlock();
		bool result = WalkMount(GetPath(), *mounted_database,
					recursive, filter,
					visit_directory, visit_song,
					visit_playlist,
					error);
		db_lock();
		return result;
	}

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);
}