Walk.cxx 11.4 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
#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 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 55
	:cancel(false),
	 storage(_storage),
56
	 editor(_loop, _listener)
57 58 59 60 61 62 63 64 65 66 67 68 69
{
#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
70
directory_set_stat(Directory &dir, const FileInfo &info)
71
{
72 73
	dir.inode = info.inode;
	dir.device = info.device;
74
	dir.have_stat = true;
75 76
}

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

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

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

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

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

	db_unlock();
105 106
}

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

115
		editor.LockDeleteDirectory(child);
116

117 118 119
		modified = true;
	}

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

126 127 128
			modified = true;
		}
	}
129

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

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

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

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

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

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

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

	return 0;
}

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

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

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

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

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

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

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

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

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

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

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

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

271
	const auto target = ReadLink(path_fs);
272
	if (target.IsNull())
273 274 275 276 277 278 279 280 281 282 283
		/* 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;
	}

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

286
	if (PathTraitsFS::IsAbsolute(target_str)) {
287 288
		/* if the symlink points to an absolute path, see if
		   that path is inside the music directory */
289 290 291 292 293 294 295
		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
296 297 298
			? !follow_inside_symlinks
			: !follow_outside_symlinks;
	}
299

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

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

338
	directory_set_stat(directory, info);
339

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

347
	ExcludeList exclude_list;
348 349 350 351 352 353 354

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

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

359
	PurgeDeletedFromDirectory(directory);
360

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

366 367 368 369 370 371 372 373
		{
			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
374
			continue;
375
		}
Max Kellermann's avatar
Max Kellermann committed
376

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

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

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

	return true;
}

391
inline Directory *
392 393 394
UpdateWalk::DirectoryMakeChildChecked(Directory &parent,
				      const char *uri_utf8,
				      const char *name_utf8)
395
{
396
	db_lock();
397
	Directory *directory = parent.FindChild(name_utf8);
398
	db_unlock();
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 408
	FileInfo info;
	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
	db_lock();
418
	Song *conflicting = parent.FindSong(name_utf8);
419
	if (conflicting)
420
		editor.DeleteSong(parent, conflicting);
421

422
	directory = parent.CreateChild(name_utf8);
423 424
	db_unlock();

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

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

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

439 440 441
		if (*name_utf8 == 0)
			continue;

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

448
		name_utf8 = slash + 1;
449 450
	}

451
	free(duplicated);
452 453 454
	return directory;
}

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

462
	const char *name = PathTraitsUTF8::GetBase(uri);
463

464
	if (SkipSymlink(parent, name)) {
465
		modified |= editor.DeleteNameIn(*parent, name);
466 467 468 469 470 471 472 473 474 475
		return;
	}

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

	UpdateDirectoryChild(*parent, name, info);
476 477 478
}

bool
479
UpdateWalk::Walk(Directory &root, const char *path, bool discard)
480
{
481
	walk_discard = discard;
482 483
	modified = false;

484
	if (path != nullptr && !isRootDirectory(path)) {
485
		UpdateUri(root, path);
486
	} else {
487 488 489
		FileInfo info;
		if (!GetInfo(storage, "", info))
			return false;
490

491
		UpdateDirectory(root, info);
492 493 494 495
	}

	return modified;
}