Walk.cxx 11.9 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2017 The Music Player Daemon Project
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
 * http://www.musicpd.org
 *
 * 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.
 *
 * 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.
 */

20
#include "config.h" /* must be first for large file support */
21
#include "Walk.hxx"
Max Kellermann's avatar
Max Kellermann committed
22
#include "UpdateIO.hxx"
23
#include "Editor.hxx"
24
#include "UpdateDomain.hxx"
Max Kellermann's avatar
Max Kellermann committed
25
#include "db/DatabaseLock.hxx"
26
#include "db/PlaylistVector.hxx"
27
#include "db/Uri.hxx"
28 29
#include "db/plugins/simple/Directory.hxx"
#include "db/plugins/simple/Song.hxx"
30
#include "storage/StorageInterface.hxx"
31
#include "playlist/PlaylistRegistry.hxx"
Max Kellermann's avatar
Max Kellermann committed
32
#include "ExcludeList.hxx"
33
#include "fs/AllocatedPath.hxx"
34
#include "fs/Traits.hxx"
35
#include "fs/FileSystem.hxx"
36
#include "storage/FileInfo.hxx"
37 38
#include "input/InputStream.hxx"
#include "input/Error.hxx"
39
#include "util/Alloc.hxx"
40
#include "util/StringCompare.hxx"
Max Kellermann's avatar
Max Kellermann committed
41
#include "util/UriUtil.hxx"
42
#include "Log.hxx"
43

44 45 46
#include <stdexcept>
#include <memory>

47 48
#include <assert.h>
#include <string.h>
49
#include <stdlib.h>
50 51
#include <errno.h>

52 53
UpdateWalk::UpdateWalk(const UpdateConfig &_config,
		       EventLoop &_loop, DatabaseListener &_listener,
54
		       Storage &_storage) noexcept
55
	:config(_config), cancel(false),
56
	 storage(_storage),
57
	 editor(_loop, _listener)
58 59 60 61
{
}

static void
62
directory_set_stat(Directory &dir, const StorageFileInfo &info)
63
{
64 65
	dir.inode = info.inode;
	dir.device = info.device;
66 67
}

68 69
inline void
UpdateWalk::RemoveExcludedFromDirectory(Directory &directory,
70
					const ExcludeList &exclude_list) noexcept
71
{
72
	const ScopeDatabaseLock protect;
73

74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
	directory.ForEachChildSafe([&](Directory &child){
			const auto name_fs =
				AllocatedPath::FromUTF8(child.GetName());

			if (name_fs.IsNull() || exclude_list.Check(name_fs)) {
				editor.DeleteDirectory(&child);
				modified = true;
			}
		});

	directory.ForEachSongSafe([&](Song &song){
			assert(song.parent == &directory);

			const auto name_fs = AllocatedPath::FromUTF8(song.uri);
			if (name_fs.IsNull() || exclude_list.Check(name_fs)) {
				editor.DeleteSong(directory, &song);
				modified = true;
			}
		});
93 94
}

95
inline void
96
UpdateWalk::PurgeDeletedFromDirectory(Directory &directory) noexcept
97
{
98
	directory.ForEachChildSafe([&](Directory &child){
99
			if (child.IsMount() || DirectoryExists(storage, child))
100
				return;
101

102
			editor.LockDeleteDirectory(&child);
103

104 105
			modified = true;
		});
106

107 108 109 110
	directory.ForEachSongSafe([&](Song &song){
			if (!directory_child_is_regular(storage, directory,
							song.uri)) {
				editor.LockDeleteSong(directory, &song);
111

112 113 114
				modified = true;
			}
		});
115

116 117
	for (auto i = directory.playlists.begin(),
		     end = directory.playlists.end();
118
	     i != end;) {
119 120
		if (!directory_child_is_regular(storage, directory,
						i->name.c_str())) {
121
			const ScopeDatabaseLock protect;
122
			i = directory.playlists.erase(i);
123 124
		} else
			++i;
125
	}
126 127
}

128
#ifndef _WIN32
129
static bool
130
update_directory_stat(Storage &storage, Directory &directory) noexcept
131
{
132
	StorageFileInfo info;
133 134
	if (!GetInfo(storage, directory.GetPath(), info))
		return false;
135

136 137
	directory_set_stat(directory, info);
	return true;
138
}
139
#endif
140

141 142 143 144 145 146
/**
 * Check the ancestors of the given #Directory and see if there's one
 * with the same device/inode number, building a loop.
 *
 * @return 1 if a loop was found, 0 if not, -1 on I/O error
 */
147
static int
148
FindAncestorLoop(Storage &storage, Directory *parent,
149
		 unsigned inode, unsigned device) noexcept
150
{
151
#ifndef _WIN32
152 153 154 155 156
	if (device == 0 && inode == 0)
		/* can't detect loops if the Storage does not support
		   these numbers */
		return 0;

157
	while (parent) {
158
		if (parent->device == 0 && parent->inode == 0 &&
159
		    !update_directory_stat(storage, *parent))
160
			return -1;
161

162
		if (parent->inode == inode && parent->device == device) {
163
			LogDebug(update_domain, "recursive directory found");
164 165
			return 1;
		}
166

167 168
		parent = parent->parent;
	}
169
#else
170
	(void)storage;
171 172 173 174
	(void)parent;
	(void)inode;
	(void)device;
#endif
175 176 177 178

	return 0;
}

179 180 181
inline bool
UpdateWalk::UpdatePlaylistFile(Directory &directory,
			       const char *name, const char *suffix,
182
			       const StorageFileInfo &info) noexcept
183 184 185 186
{
	if (!playlist_suffix_supported(suffix))
		return false;

187
	PlaylistInfo pi(name, info.mtime);
188

189
	const ScopeDatabaseLock protect;
190
	if (directory.playlists.UpdateOrInsert(std::move(pi)))
191 192 193 194
		modified = true;
	return true;
}

195 196
inline bool
UpdateWalk::UpdateRegularFile(Directory &directory,
197 198
			      const char *name,
			      const StorageFileInfo &info) noexcept
199 200
{
	const char *suffix = uri_get_suffix(name);
201
	if (suffix == nullptr)
202
		return false;
203

204 205 206
	return UpdateSongFile(directory, name, suffix, info) ||
		UpdateArchiveFile(directory, name, suffix, info) ||
		UpdatePlaylistFile(directory, name, suffix, info);
207 208
}

209 210
void
UpdateWalk::UpdateDirectoryChild(Directory &directory,
211
				 const ExcludeList &exclude_list,
212
				 const char *name, const StorageFileInfo &info) noexcept
213
try {
214
	assert(strchr(name, '/') == nullptr);
215

216 217 218
	if (info.IsRegular()) {
		UpdateRegularFile(directory, name, info);
	} else if (info.IsDirectory()) {
219
		if (FindAncestorLoop(storage, &directory,
220
					info.inode, info.device))
221 222
			return;

223 224 225 226 227
		Directory *subdir;
		{
			const ScopeDatabaseLock protect;
			subdir = directory.MakeChild(name);
		}
228

229
		assert(&directory == subdir->parent);
230

231
		if (!UpdateDirectory(*subdir, exclude_list, info))
232
			editor.LockDeleteDirectory(subdir);
233
	} else {
234 235
		FormatDebug(update_domain,
			    "%s is not a directory, archive or music", name);
236
	}
237 238
} catch (const std::exception &e) {
	LogError(e);
239 240 241
}

/* we don't look at "." / ".." nor files with newlines in their name */
242
gcc_pure
243
static bool
244
skip_path(const char *name_utf8) noexcept
245
{
246
	return strchr(name_utf8, '\n') != nullptr;
247 248
}

249
gcc_pure
250 251
bool
UpdateWalk::SkipSymlink(const Directory *directory,
252
			const char *utf8_name) const noexcept
253
{
254
#ifndef _WIN32
255 256
	const auto path_fs = storage.MapChildFS(directory->GetPath(),
						utf8_name);
257
	if (path_fs.IsNull())
258 259
		/* not a local file: don't skip */
		return false;
260

261
	const auto target = ReadLink(path_fs);
262
	if (target.IsNull())
263 264 265
		/* don't skip if this is not a symlink */
		return errno != EINVAL;

266 267
	if (!config.follow_inside_symlinks &&
	    !config.follow_outside_symlinks) {
268 269
		/* ignore all symlinks */
		return true;
270 271
	} else if (config.follow_inside_symlinks &&
		   config.follow_outside_symlinks) {
272 273 274 275
		/* consider all symlinks */
		return false;
	}

276
	if (target.IsAbsolute()) {
277 278
		/* if the symlink points to an absolute path, see if
		   that path is inside the music directory */
279
		const auto target_utf8 = target.ToUTF8();
280 281 282 283 284 285
		if (target_utf8.empty())
			return true;

		const char *relative =
			storage.MapToRelativeUTF8(target_utf8.c_str());
		return relative != nullptr
286 287
			? !config.follow_inside_symlinks
			: !config.follow_outside_symlinks;
288
	}
289

290
	const char *p = target.c_str();
291
	while (*p == '.') {
292
		if (p[1] == '.' && PathTraitsFS::IsSeparator(p[2])) {
293 294
			/* "../" moves to parent directory */
			directory = directory->parent;
295
			if (directory == nullptr) {
296 297 298
				/* we have moved outside the music
				   directory - skip this symlink
				   if such symlinks are not allowed */
299
				return !config.follow_outside_symlinks;
300 301
			}
			p += 3;
302
		} else if (PathTraitsFS::IsSeparator(p[1]))
303 304 305 306 307 308 309 310 311
			/* eliminate "./" */
			p += 2;
		else
			break;
	}

	/* we are still in the music directory, so this symlink points
	   to a song which is already in the database - skip according
	   to the follow_inside_symlinks param*/
312
	return !config.follow_inside_symlinks;
313 314 315 316 317 318 319 320 321 322
#else
	/* no symlink checking on WIN32 */

	(void)directory;
	(void)utf8_name;

	return false;
#endif
}

323
bool
324 325
UpdateWalk::UpdateDirectory(Directory &directory,
			    const ExcludeList &exclude_list,
326
			    const StorageFileInfo &info) noexcept
327
{
328
	assert(info.IsDirectory());
329

330
	directory_set_stat(directory, info);
331

332 333 334
	std::unique_ptr<StorageDirectoryReader> reader;

	try {
335
		reader = storage.OpenDirectory(directory.GetPath());
336 337
	} catch (...) {
		LogError(std::current_exception());
338 339 340
		return false;
	}

341
	ExcludeList child_exclude_list(exclude_list);
342

343 344 345 346
	try {
		Mutex mutex;
		auto is = InputStream::OpenReady(PathTraitsUTF8::Build(storage.MapUTF8(directory.GetPath()).c_str(),
								       ".mpdignore").c_str(),
347
						 mutex);
348 349 350 351
		child_exclude_list.Load(std::move(is));
	} catch (...) {
		if (!IsFileNotFound(std::current_exception()))
			LogError(std::current_exception());
352
	}
353

354 355
	if (!child_exclude_list.IsEmpty())
		RemoveExcludedFromDirectory(directory, child_exclude_list);
356

357
	PurgeDeletedFromDirectory(directory);
358

359
	const char *name_utf8;
360
	while (!cancel && (name_utf8 = reader->Read()) != nullptr) {
361
		if (skip_path(name_utf8))
362 363
			continue;

364 365
		{
			const auto name_fs = AllocatedPath::FromUTF8(name_utf8);
366
			if (name_fs.IsNull() || child_exclude_list.Check(name_fs))
367 368 369 370 371
				continue;
		}

		if (SkipSymlink(&directory, name_utf8)) {
			modified |= editor.DeleteNameIn(directory, name_utf8);
Max Kellermann's avatar
Max Kellermann committed
372
			continue;
373
		}
Max Kellermann's avatar
Max Kellermann committed
374

375
		StorageFileInfo info2;
376 377
		if (!GetInfo(*reader, info2)) {
			modified |= editor.DeleteNameIn(directory, name_utf8);
378 379 380
			continue;
		}

381
		UpdateDirectoryChild(directory, child_exclude_list, name_utf8, info2);
382 383
	}

384
	directory.mtime = info.mtime;
385 386 387 388

	return true;
}

389
inline Directory *
390 391
UpdateWalk::DirectoryMakeChildChecked(Directory &parent,
				      const char *uri_utf8,
392
				      const char *name_utf8) noexcept
393
{
394 395 396 397 398
	Directory *directory;
	{
		const ScopeDatabaseLock protect;
		directory = parent.FindChild(name_utf8);
	}
399

Max Kellermann's avatar
Max Kellermann committed
400 401 402 403
	if (directory != nullptr) {
		if (directory->IsMount())
			directory = nullptr;

404
		return directory;
Max Kellermann's avatar
Max Kellermann committed
405
	}
406

407
	StorageFileInfo info;
408
	if (!GetInfo(storage, uri_utf8, info) ||
409
	    FindAncestorLoop(storage, &parent, info.inode, info.device))
410
		return nullptr;
411

412
	if (SkipSymlink(&parent, name_utf8))
413
		return nullptr;
414

415 416
	/* if we're adding directory paths, make sure to delete filenames
	   with potentially the same name */
417 418 419 420 421
	{
		const ScopeDatabaseLock protect;
		Song *conflicting = parent.FindSong(name_utf8);
		if (conflicting)
			editor.DeleteSong(parent, conflicting);
422

423 424
		directory = parent.CreateChild(name_utf8);
	}
425

426
	directory_set_stat(*directory, info);
427 428 429
	return directory;
}

430
inline Directory *
431 432
UpdateWalk::DirectoryMakeUriParentChecked(Directory &root,
					  const char *uri) noexcept
433
{
434
	Directory *directory = &root;
435
	char *duplicated = xstrdup(uri);
436
	char *name_utf8 = duplicated, *slash;
437

438
	while ((slash = strchr(name_utf8, '/')) != nullptr) {
439 440
		*slash = 0;

441
		if (StringIsEmpty(name_utf8))
442 443
			continue;

444
		directory = DirectoryMakeChildChecked(*directory,
445
						      duplicated,
446
						      name_utf8);
447
		if (directory == nullptr)
448 449
			break;

450
		name_utf8 = slash + 1;
451 452
	}

453
	free(duplicated);
454 455 456
	return directory;
}

457
inline void
458
UpdateWalk::UpdateUri(Directory &root, const char *uri) noexcept
459
try {
460
	Directory *parent = DirectoryMakeUriParentChecked(root, uri);
461
	if (parent == nullptr)
462 463
		return;

464
	const char *name = PathTraitsUTF8::GetBase(uri);
465

466
	if (SkipSymlink(parent, name)) {
467
		modified |= editor.DeleteNameIn(*parent, name);
468 469 470
		return;
	}

471
	StorageFileInfo info;
472 473 474 475 476
	if (!GetInfo(storage, uri, info)) {
		modified |= editor.DeleteNameIn(*parent, name);
		return;
	}

477 478 479
	ExcludeList exclude_list;

	UpdateDirectoryChild(*parent, exclude_list, name, info);
480 481
} catch (const std::exception &e) {
	LogError(e);
482 483 484
}

bool
485
UpdateWalk::Walk(Directory &root, const char *path, bool discard) noexcept
486
{
487
	walk_discard = discard;
488 489
	modified = false;

490
	if (path != nullptr && !isRootDirectory(path)) {
491
		UpdateUri(root, path);
492
	} else {
493
		StorageFileInfo info;
494 495
		if (!GetInfo(storage, "", info))
			return false;
496

497 498 499
		ExcludeList exclude_list;

		UpdateDirectory(root, exclude_list, info);
500 501 502 503
	}

	return modified;
}