Walk.cxx 11.3 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 75
}

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

82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
	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;
			}
		});
101 102

	db_unlock();
103 104
}

105 106
inline void
UpdateWalk::PurgeDeletedFromDirectory(Directory &directory)
107
{
108 109 110
	directory.ForEachChildSafe([&](Directory &child){
			if (DirectoryExists(storage, child))
				return;
111

112
			editor.LockDeleteDirectory(&child);
113

114 115
			modified = true;
		});
116

117 118 119 120
	directory.ForEachSongSafe([&](Song &song){
			if (!directory_child_is_regular(storage, directory,
							song.uri)) {
				editor.LockDeleteSong(directory, &song);
121

122 123 124
				modified = true;
			}
		});
125

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

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

147 148
	directory_set_stat(directory, info);
	return true;
149
}
150
#endif
151

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

168
	while (parent) {
169
		if (parent->device == 0 && parent->inode == 0 &&
170
		    !update_directory_stat(storage, *parent))
171
			return -1;
172

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

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

	return 0;
}

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

198
	PlaylistInfo pi(name, info.mtime);
199

200
	db_lock();
201
	if (directory.playlists.UpdateOrInsert(std::move(pi)))
202 203 204 205 206
		modified = true;
	db_unlock();
	return true;
}

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

215 216 217
	return UpdateSongFile(directory, name, suffix, info) ||
		UpdateArchiveFile(directory, name, suffix, info) ||
		UpdatePlaylistFile(directory, name, suffix, info);
218 219
}

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

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

233
		db_lock();
234
		Directory *subdir = directory.MakeChild(name);
235 236
		db_unlock();

237
		assert(&directory == subdir->parent);
238

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

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

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

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

280 281
	const char *target_str = target.c_str();

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

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

329
bool
330
UpdateWalk::UpdateDirectory(Directory &directory, const FileInfo &info)
331
{
332
	assert(info.IsDirectory());
333

334
	directory_set_stat(directory, info);
335

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

343
	ExcludeList exclude_list;
344 345 346 347 348 349 350

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

352
	if (!exclude_list.IsEmpty())
353
		RemoveExcludedFromDirectory(directory, exclude_list);
354

355
	PurgeDeletedFromDirectory(directory);
356

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

362 363 364 365 366 367 368 369
		{
			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
370
			continue;
371
		}
Max Kellermann's avatar
Max Kellermann committed
372

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

379
		UpdateDirectoryChild(directory, name_utf8, info2);
380 381
	}

382
	directory.mtime = info.mtime;
383 384 385 386

	return true;
}

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

Max Kellermann's avatar
Max Kellermann committed
396 397 398 399
	if (directory != nullptr) {
		if (directory->IsMount())
			directory = nullptr;

400
		return directory;
Max Kellermann's avatar
Max Kellermann committed
401
	}
402

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

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

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

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

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

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

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

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

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

444
		name_utf8 = slash + 1;
445 446
	}

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

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

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

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

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

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

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

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

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

	return modified;
}