Walk.cxx 11.2 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright (C) 2003-2014 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 26 27
#include "db/DatabaseLock.hxx"
#include "db/Directory.hxx"
#include "db/Song.hxx"
28
#include "db/PlaylistVector.hxx"
29
#include "db/Uri.hxx"
30
#include "storage/StorageInterface.hxx"
31
#include "playlist/PlaylistRegistry.hxx"
Max Kellermann's avatar
Max Kellermann committed
32
#include "ExcludeList.hxx"
33 34
#include "config/ConfigGlobal.hxx"
#include "config/ConfigOption.hxx"
35
#include "fs/AllocatedPath.hxx"
36
#include "fs/Traits.hxx"
37
#include "fs/FileSystem.hxx"
38
#include "fs/Charset.hxx"
39
#include "storage/FileInfo.hxx"
40
#include "util/Alloc.hxx"
Max Kellermann's avatar
Max Kellermann committed
41
#include "util/UriUtil.hxx"
42
#include "util/Error.hxx"
43
#include "Log.hxx"
44 45 46 47

#include <assert.h>
#include <sys/stat.h>
#include <string.h>
48
#include <stdlib.h>
49
#include <errno.h>
50
#include <memory>
51

52
UpdateWalk::UpdateWalk(EventLoop &_loop, DatabaseListener &_listener,
53
		       Storage &_storage)
54
	:storage(_storage),
55
	 editor(_loop, _listener)
56 57 58 59 60 61 62 63 64 65 66 67 68
{
#ifndef WIN32
	follow_inside_symlinks =
		config_get_bool(CONF_FOLLOW_INSIDE_SYMLINKS,
				DEFAULT_FOLLOW_INSIDE_SYMLINKS);

	follow_outside_symlinks =
		config_get_bool(CONF_FOLLOW_OUTSIDE_SYMLINKS,
				DEFAULT_FOLLOW_OUTSIDE_SYMLINKS);
#endif
}

static void
69
directory_set_stat(Directory &dir, const FileInfo &info)
70
{
71 72
	dir.inode = info.inode;
	dir.device = info.device;
73
	dir.have_stat = true;
74 75
}

76 77 78
inline void
UpdateWalk::RemoveExcludedFromDirectory(Directory &directory,
					const ExcludeList &exclude_list)
79
{
80 81
	db_lock();

82
	Directory *child, *n;
83
	directory_for_each_child_safe(child, n, directory) {
84
		const auto name_fs = AllocatedPath::FromUTF8(child->GetName());
85

86
		if (name_fs.IsNull() || exclude_list.Check(name_fs)) {
87
			editor.DeleteDirectory(child);
88 89 90 91
			modified = true;
		}
	}

92
	Song *song, *ns;
93
	directory_for_each_song_safe(song, ns, directory) {
94
		assert(song->parent == &directory);
95

96
		const auto name_fs = AllocatedPath::FromUTF8(song->uri);
97
		if (name_fs.IsNull() || exclude_list.Check(name_fs)) {
98
			editor.DeleteSong(directory, song);
99 100
			modified = true;
		}
101
	}
102 103

	db_unlock();
104 105
}

106 107
inline void
UpdateWalk::PurgeDeletedFromDirectory(Directory &directory)
108
{
109
	Directory *child, *n;
110
	directory_for_each_child_safe(child, n, directory) {
111
		if (DirectoryExists(storage, *child))
112 113
			continue;

114
		editor.LockDeleteDirectory(child);
115

116 117 118
		modified = true;
	}

119
	Song *song, *ns;
120
	directory_for_each_song_safe(song, ns, directory) {
121 122
		if (!directory_child_is_regular(storage, directory,
						song->uri)) {
123
			editor.LockDeleteSong(directory, song);
124

125 126 127
			modified = true;
		}
	}
128

129 130
	for (auto i = directory.playlists.begin(),
		     end = directory.playlists.end();
131
	     i != end;) {
132 133
		if (!directory_child_is_regular(storage, directory,
						i->name.c_str())) {
134
			db_lock();
135
			i = directory.playlists.erase(i);
136
			db_unlock();
137 138
		} else
			++i;
139
	}
140 141
}

142
#ifndef WIN32
143
static bool
144
update_directory_stat(Storage &storage, Directory &directory)
145
{
146 147 148
	FileInfo info;
	if (!GetInfo(storage, directory.GetPath(), info))
		return false;
149

150 151
	directory_set_stat(directory, info);
	return true;
152
}
153
#endif
154

155 156 157 158 159 160
/**
 * 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
 */
161
static int
162 163
FindAncestorLoop(Storage &storage, Directory *parent,
		 unsigned inode, unsigned device)
164
{
165
#ifndef WIN32
166 167 168 169 170
	if (device == 0 && inode == 0)
		/* can't detect loops if the Storage does not support
		   these numbers */
		return 0;

171
	while (parent) {
172 173
		if (!parent->have_stat &&
		    !update_directory_stat(storage, *parent))
174
			return -1;
175

176
		if (parent->inode == inode && parent->device == device) {
177
			LogDebug(update_domain, "recursive directory found");
178 179
			return 1;
		}
180

181 182
		parent = parent->parent;
	}
183
#else
184
	(void)storage;
185 186 187 188
	(void)parent;
	(void)inode;
	(void)device;
#endif
189 190 191 192

	return 0;
}

193 194 195
inline bool
UpdateWalk::UpdatePlaylistFile(Directory &directory,
			       const char *name, const char *suffix,
196
			       const FileInfo &info)
197 198 199 200
{
	if (!playlist_suffix_supported(suffix))
		return false;

201
	PlaylistInfo pi(name, info.mtime);
202

203
	db_lock();
204
	if (directory.playlists.UpdateOrInsert(std::move(pi)))
205 206 207 208 209
		modified = true;
	db_unlock();
	return true;
}

210 211
inline bool
UpdateWalk::UpdateRegularFile(Directory &directory,
212
			      const char *name, const FileInfo &info)
213 214
{
	const char *suffix = uri_get_suffix(name);
215
	if (suffix == nullptr)
216
		return false;
217

218 219 220
	return UpdateSongFile(directory, name, suffix, info) ||
		UpdateArchiveFile(directory, name, suffix, info) ||
		UpdatePlaylistFile(directory, name, suffix, info);
221 222
}

223 224
void
UpdateWalk::UpdateDirectoryChild(Directory &directory,
225
				 const char *name, const FileInfo &info)
226
{
227
	assert(strchr(name, '/') == nullptr);
228

229 230 231
	if (info.IsRegular()) {
		UpdateRegularFile(directory, name, info);
	} else if (info.IsDirectory()) {
232
		if (FindAncestorLoop(storage, &directory,
233
					info.inode, info.device))
234 235
			return;

236
		db_lock();
237
		Directory *subdir = directory.MakeChild(name);
238 239
		db_unlock();

240
		assert(&directory == subdir->parent);
241

242
		if (!UpdateDirectory(*subdir, info))
243
			editor.LockDeleteDirectory(subdir);
244
	} else {
245 246
		FormatDebug(update_domain,
			    "%s is not a directory, archive or music", name);
247 248 249 250
	}
}

/* we don't look at "." / ".." nor files with newlines in their name */
251
gcc_pure
252 253
static bool
skip_path(const char *name_utf8)
254
{
255
	return strchr(name_utf8, '\n') != nullptr;
256 257
}

258
gcc_pure
259 260 261
bool
UpdateWalk::SkipSymlink(const Directory *directory,
			const char *utf8_name) const
262 263
{
#ifndef WIN32
264 265
	const auto path_fs = storage.MapChildFS(directory->GetPath(),
						utf8_name);
266
	if (path_fs.IsNull())
267 268
		/* not a local file: don't skip */
		return false;
269

270
	const auto target = ReadLink(path_fs);
271
	if (target.IsNull())
272 273 274 275 276 277 278 279 280 281 282
		/* don't skip if this is not a symlink */
		return errno != EINVAL;

	if (!follow_inside_symlinks && !follow_outside_symlinks) {
		/* ignore all symlinks */
		return true;
	} else if (follow_inside_symlinks && follow_outside_symlinks) {
		/* consider all symlinks */
		return false;
	}

283 284
	const char *target_str = target.c_str();

285
	if (PathTraitsFS::IsAbsolute(target_str)) {
286 287
		/* if the symlink points to an absolute path, see if
		   that path is inside the music directory */
288 289 290 291 292 293 294
		const auto target_utf8 = PathToUTF8(target_str);
		if (target_utf8.empty())
			return true;

		const char *relative =
			storage.MapToRelativeUTF8(target_utf8.c_str());
		return relative != nullptr
295 296 297
			? !follow_inside_symlinks
			: !follow_outside_symlinks;
	}
298

299
	const char *p = target_str;
300
	while (*p == '.') {
301
		if (p[1] == '.' && PathTraitsFS::IsSeparator(p[2])) {
302 303
			/* "../" moves to parent directory */
			directory = directory->parent;
304
			if (directory == nullptr) {
305 306 307 308 309 310
				/* we have moved outside the music
				   directory - skip this symlink
				   if such symlinks are not allowed */
				return !follow_outside_symlinks;
			}
			p += 3;
311
		} else if (PathTraitsFS::IsSeparator(p[1]))
312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331
			/* 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*/
	return !follow_inside_symlinks;
#else
	/* no symlink checking on WIN32 */

	(void)directory;
	(void)utf8_name;

	return false;
#endif
}

332
bool
333
UpdateWalk::UpdateDirectory(Directory &directory, const FileInfo &info)
334
{
335
	assert(info.IsDirectory());
336

337
	directory_set_stat(directory, info);
338

339
	Error error;
340 341
	const std::auto_ptr<StorageDirectoryReader> reader(storage.OpenDirectory(directory.GetPath(), error));
	if (reader.get() == nullptr) {
342
		LogError(error);
343 344 345
		return false;
	}

346
	ExcludeList exclude_list;
347 348 349 350 351 352 353

	{
		const auto exclude_path_fs =
			storage.MapChildFS(directory.GetPath(), ".mpdignore");
		if (!exclude_path_fs.IsNull())
			exclude_list.LoadFile(exclude_path_fs);
	}
354

355
	if (!exclude_list.IsEmpty())
356
		RemoveExcludedFromDirectory(directory, exclude_list);
357

358
	PurgeDeletedFromDirectory(directory);
359

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

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

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

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

382
		UpdateDirectoryChild(directory, name_utf8, info2);
383 384
	}

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

	return true;
}

390
inline Directory *
391 392 393
UpdateWalk::DirectoryMakeChildChecked(Directory &parent,
				      const char *uri_utf8,
				      const char *name_utf8)
394
{
395
	db_lock();
396
	Directory *directory = parent.FindChild(name_utf8);
397
	db_unlock();
398

399
	if (directory != nullptr)
400 401
		return directory;

402 403
	FileInfo info;
	if (!GetInfo(storage, uri_utf8, info) ||
404
	    FindAncestorLoop(storage, &parent, info.inode, info.device))
405
		return nullptr;
406

407
	if (SkipSymlink(&parent, name_utf8))
408
		return nullptr;
409

410 411
	/* if we're adding directory paths, make sure to delete filenames
	   with potentially the same name */
412
	db_lock();
413
	Song *conflicting = parent.FindSong(name_utf8);
414
	if (conflicting)
415
		editor.DeleteSong(parent, conflicting);
416

417
	directory = parent.CreateChild(name_utf8);
418 419
	db_unlock();

420
	directory_set_stat(*directory, info);
421 422 423
	return directory;
}

424
inline Directory *
425
UpdateWalk::DirectoryMakeUriParentChecked(Directory &root, const char *uri)
426
{
427
	Directory *directory = &root;
428
	char *duplicated = xstrdup(uri);
429
	char *name_utf8 = duplicated, *slash;
430

431
	while ((slash = strchr(name_utf8, '/')) != nullptr) {
432 433
		*slash = 0;

434 435 436
		if (*name_utf8 == 0)
			continue;

437
		directory = DirectoryMakeChildChecked(*directory,
438
						      duplicated,
439
						      name_utf8);
440
		if (directory == nullptr)
441 442
			break;

443
		name_utf8 = slash + 1;
444 445
	}

446
	free(duplicated);
447 448 449
	return directory;
}

450
inline void
451
UpdateWalk::UpdateUri(Directory &root, const char *uri)
452
{
453
	Directory *parent = DirectoryMakeUriParentChecked(root, uri);
454
	if (parent == nullptr)
455 456
		return;

457
	const char *name = PathTraitsUTF8::GetBase(uri);
458

459
	if (SkipSymlink(parent, name)) {
460
		modified |= editor.DeleteNameIn(*parent, name);
461 462 463 464 465 466 467 468 469 470
		return;
	}

	FileInfo info;
	if (!GetInfo(storage, uri, info)) {
		modified |= editor.DeleteNameIn(*parent, name);
		return;
	}

	UpdateDirectoryChild(*parent, name, info);
471 472 473
}

bool
474
UpdateWalk::Walk(Directory &root, const char *path, bool discard)
475
{
476
	walk_discard = discard;
477 478
	modified = false;

479
	if (path != nullptr && !isRootDirectory(path)) {
480
		UpdateUri(root, path);
481
	} else {
482 483 484
		FileInfo info;
		if (!GetInfo(storage, "", info))
			return false;
485

486
		UpdateDirectory(root, info);
487 488 489 490
	}

	return modified;
}