UpdateWalk.cxx 10.9 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 */
Max Kellermann's avatar
Max Kellermann committed
21 22 23 24 25
#include "UpdateWalk.hxx"
#include "UpdateIO.hxx"
#include "UpdateDatabase.hxx"
#include "UpdateSong.hxx"
#include "UpdateArchive.hxx"
26
#include "UpdateDomain.hxx"
Max Kellermann's avatar
Max Kellermann committed
27
#include "DatabaseLock.hxx"
28
#include "DatabaseSimple.hxx"
29
#include "Directory.hxx"
30
#include "Song.hxx"
31
#include "PlaylistVector.hxx"
32
#include "PlaylistRegistry.hxx"
Max Kellermann's avatar
Max Kellermann committed
33
#include "Mapper.hxx"
Max Kellermann's avatar
Max Kellermann committed
34
#include "ExcludeList.hxx"
35 36
#include "ConfigGlobal.hxx"
#include "ConfigOption.hxx"
37
#include "fs/AllocatedPath.hxx"
38
#include "fs/Traits.hxx"
39
#include "fs/FileSystem.hxx"
40
#include "fs/DirectoryReader.hxx"
41
#include "util/Alloc.hxx"
Max Kellermann's avatar
Max Kellermann committed
42
#include "util/UriUtil.hxx"
43
#include "Log.hxx"
44 45 46 47

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

51 52
bool walk_discard;
bool modified;
53 54 55

#ifndef WIN32

56 57
static constexpr bool DEFAULT_FOLLOW_INSIDE_SYMLINKS = true;
static constexpr bool DEFAULT_FOLLOW_OUTSIDE_SYMLINKS = true;
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83

static bool follow_inside_symlinks;
static bool follow_outside_symlinks;

#endif

void
update_walk_global_init(void)
{
#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
}

void
update_walk_global_finish(void)
{
}

static void
84
directory_set_stat(Directory &dir, const struct stat *st)
85
{
86 87 88
	dir.inode = st->st_ino;
	dir.device = st->st_dev;
	dir.have_stat = true;
89 90
}

91
static void
92
remove_excluded_from_directory(Directory &directory,
93
			       const ExcludeList &exclude_list)
94
{
95 96
	db_lock();

97
	Directory *child, *n;
98
	directory_for_each_child_safe(child, n, directory) {
99
		const auto name_fs = AllocatedPath::FromUTF8(child->GetName());
100

101
		if (name_fs.IsNull() || exclude_list.Check(name_fs)) {
102 103 104 105 106
			delete_directory(child);
			modified = true;
		}
	}

107
	Song *song, *ns;
108
	directory_for_each_song_safe(song, ns, directory) {
109
		assert(song->parent == &directory);
110

111
		const auto name_fs = AllocatedPath::FromUTF8(song->uri);
112
		if (name_fs.IsNull() || exclude_list.Check(name_fs)) {
113 114 115
			delete_song(directory, song);
			modified = true;
		}
116
	}
117 118

	db_unlock();
119 120 121
}

static void
122
purge_deleted_from_directory(Directory &directory)
123
{
124
	Directory *child, *n;
125
	directory_for_each_child_safe(child, n, directory) {
126
		if (directory_exists(*child))
127 128
			continue;

129
		db_lock();
130
		delete_directory(child);
131 132
		db_unlock();

133 134 135
		modified = true;
	}

136
	Song *song, *ns;
137
	directory_for_each_song_safe(song, ns, directory) {
138
		const auto path = map_song_fs(*song);
139
		if (path.IsNull() || !FileExists(path)) {
140
			db_lock();
141
			delete_song(directory, song);
142 143
			db_unlock();

144 145 146
			modified = true;
		}
	}
147

148 149
	for (auto i = directory.playlists.begin(),
		     end = directory.playlists.end();
150 151
	     i != end;) {
		if (!directory_child_is_regular(directory, i->name.c_str())) {
152
			db_lock();
153
			i = directory.playlists.erase(i);
154
			db_unlock();
155 156
		} else
			++i;
157
	}
158 159
}

160
#ifndef WIN32
161
static int
162
update_directory_stat(Directory &directory)
163 164
{
	struct stat st;
165
	if (stat_directory(directory, &st) < 0)
166 167
		return -1;

168
	directory_set_stat(directory, &st);
169 170
	return 0;
}
171
#endif
172 173

static int
174
find_inode_ancestor(Directory *parent, ino_t inode, dev_t device)
175
{
176
#ifndef WIN32
177
	while (parent) {
178
		if (!parent->have_stat && update_directory_stat(*parent) < 0)
179
			return -1;
180

181
		if (parent->inode == inode && parent->device == device) {
182
			LogDebug(update_domain, "recursive directory found");
183 184
			return 1;
		}
185

186 187
		parent = parent->parent;
	}
188 189 190 191 192
#else
	(void)parent;
	(void)inode;
	(void)device;
#endif
193 194 195 196

	return 0;
}

197
static bool
198
update_playlist_file2(Directory &directory,
199 200 201 202 203 204
		      const char *name, const char *suffix,
		      const struct stat *st)
{
	if (!playlist_suffix_supported(suffix))
		return false;

205 206
	PlaylistInfo pi(name, st->st_mtime);

207
	db_lock();
208
	if (directory.playlists.UpdateOrInsert(std::move(pi)))
209 210 211 212 213 214
		modified = true;
	db_unlock();
	return true;
}

static bool
215
update_regular_file(Directory &directory,
216 217 218
		    const char *name, const struct stat *st)
{
	const char *suffix = uri_get_suffix(name);
219
	if (suffix == nullptr)
220
		return false;
221

222
	return update_song_file(directory, name, suffix, st) ||
223
		update_archive_file(directory, name, suffix, st) ||
224
		update_playlist_file2(directory, name, suffix, st);
225 226 227
}

static bool
228
update_directory(Directory &directory, const struct stat *st);
229 230

static void
231
update_directory_child(Directory &directory,
232
		       const char *name, const struct stat *st)
233
{
234
	assert(strchr(name, '/') == nullptr);
235 236 237 238

	if (S_ISREG(st->st_mode)) {
		update_regular_file(directory, name, st);
	} else if (S_ISDIR(st->st_mode)) {
239
		if (find_inode_ancestor(&directory, st->st_ino, st->st_dev))
240 241
			return;

242
		db_lock();
243
		Directory *subdir = directory.MakeChild(name);
244 245
		db_unlock();

246
		assert(&directory == subdir->parent);
247

248
		if (!update_directory(*subdir, st)) {
249
			db_lock();
250
			delete_directory(subdir);
251 252
			db_unlock();
		}
253
	} else {
254 255
		FormatDebug(update_domain,
			    "%s is not a directory, archive or music", name);
256 257 258 259
	}
}

/* we don't look at "." / ".." nor files with newlines in their name */
260
gcc_pure
261
static bool skip_path(Path path_fs)
262
{
263
	const char *path = path_fs.c_str();
264 265
	return (path[0] == '.' && path[1] == 0) ||
		(path[0] == '.' && path[1] == '.' && path[2] == 0) ||
266
		strchr(path, '\n') != nullptr;
267 268
}

269
gcc_pure
270
static bool
271
skip_symlink(const Directory *directory, const char *utf8_name)
272 273
{
#ifndef WIN32
274
	const auto path_fs = map_directory_child_fs(*directory, utf8_name);
275
	if (path_fs.IsNull())
276 277
		return true;

278
	const auto target = ReadLink(path_fs);
279
	if (target.IsNull())
280 281 282 283 284 285 286 287 288 289 290
		/* 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;
	}

291 292
	const char *target_str = target.c_str();

293
	if (PathTraitsFS::IsAbsolute(target_str)) {
294 295
		/* if the symlink points to an absolute path, see if
		   that path is inside the music directory */
296 297
		const char *relative = map_to_relative_path(target_str);
		return relative > target_str
298 299 300
			? !follow_inside_symlinks
			: !follow_outside_symlinks;
	}
301

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

static bool
336
update_directory(Directory &directory, const struct stat *st)
337 338 339 340 341
{
	assert(S_ISDIR(st->st_mode));

	directory_set_stat(directory, st);

342
	const auto path_fs = map_directory_fs(directory);
343
	if (path_fs.IsNull())
344 345
		return false;

346 347 348 349
	DirectoryReader reader(path_fs);
	if (reader.HasFailed()) {
		int error = errno;
		const auto path_utf8 = path_fs.ToUTF8();
350 351 352
		FormatErrno(update_domain, error,
			    "Failed to open directory %s",
			    path_utf8.c_str());
353 354 355
		return false;
	}

356
	ExcludeList exclude_list;
357
	exclude_list.LoadFile(AllocatedPath::Build(path_fs, ".mpdignore"));
358

359
	if (!exclude_list.IsEmpty())
360 361
		remove_excluded_from_directory(directory, exclude_list);

362
	purge_deleted_from_directory(directory);
363

364
	while (reader.ReadEntry()) {
365
		std::string utf8;
366 367
		struct stat st2;

368
		const auto entry = reader.GetEntry();
369 370

		if (skip_path(entry) || exclude_list.Check(entry))
371 372
			continue;

373
		utf8 = entry.ToUTF8();
374
		if (utf8.empty())
Max Kellermann's avatar
Max Kellermann committed
375 376
			continue;

377
		if (skip_symlink(&directory, utf8.c_str())) {
378
			modified |= delete_name_in(directory, utf8.c_str());
379 380 381
			continue;
		}

382 383
		if (stat_directory_child(directory, utf8.c_str(), &st2) == 0)
			update_directory_child(directory, utf8.c_str(), &st2);
384
		else
385
			modified |= delete_name_in(directory, utf8.c_str());
386 387
	}

388
	directory.mtime = st->st_mtime;
389 390 391 392

	return true;
}

393
static Directory *
394
directory_make_child_checked(Directory &parent, const char *name_utf8)
395
{
396
	db_lock();
397
	Directory *directory = parent.FindChild(name_utf8);
398
	db_unlock();
399

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

403
	struct stat st;
404
	if (stat_directory_child(parent, name_utf8, &st) < 0 ||
405
	    find_inode_ancestor(&parent, st.st_ino, st.st_dev))
406
		return nullptr;
407

408
	if (skip_symlink(&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 416 417
	if (conflicting)
		delete_song(parent, conflicting);

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

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

425
static Directory *
426
directory_make_uri_parent_checked(const char *uri)
427
{
428
	Directory *directory = db_get_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 439
		directory = directory_make_child_checked(*directory,
							 name_utf8);
440
		if (directory == nullptr)
441 442
			break;

443
		name_utf8 = slash + 1;
444 445
	}

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

static void
451
update_uri(const char *uri)
452
{
453
	Directory *parent = directory_make_uri_parent_checked(uri);
454
	if (parent == nullptr)
455 456
		return;

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

459
	struct stat st;
460
	if (!skip_symlink(parent, name) &&
461 462
	    stat_directory_child(*parent, name, &st) == 0)
		update_directory_child(*parent, name, &st);
463
	else
464
		modified |= delete_name_in(*parent, name);
465 466 467
}

bool
468
update_walk(const char *path, bool discard)
469
{
470
	walk_discard = discard;
471 472
	modified = false;

473
	if (path != nullptr && !isRootDirectory(path)) {
474
		update_uri(path);
475
	} else {
476
		Directory *directory = db_get_root();
477 478
		struct stat st;

479 480
		if (stat_directory(*directory, &st) == 0)
			update_directory(*directory, &st);
481 482 483 484
	}

	return modified;
}