UpdateWalk.cxx 10.8 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright (C) 2003-2013 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/Path.hxx"
38
#include "fs/FileSystem.hxx"
39
#include "fs/DirectoryReader.hxx"
Max Kellermann's avatar
Max Kellermann committed
40
#include "util/UriUtil.hxx"
41
#include "Log.hxx"
42 43 44 45 46 47 48 49 50 51 52 53

#include <glib.h>

#include <assert.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <dirent.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>

54 55
bool walk_discard;
bool modified;
56 57 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 84 85 86 87 88

#ifndef WIN32

enum {
	DEFAULT_FOLLOW_INSIDE_SYMLINKS = true,
	DEFAULT_FOLLOW_OUTSIDE_SYMLINKS = true,
};

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
89
directory_set_stat(Directory *dir, const struct stat *st)
90 91 92
{
	dir->inode = st->st_ino;
	dir->device = st->st_dev;
93
	dir->have_stat = true;
94 95
}

96
static void
97 98
remove_excluded_from_directory(Directory *directory,
			       const ExcludeList &exclude_list)
99
{
100 101
	db_lock();

102
	Directory *child, *n;
103
	directory_for_each_child_safe(child, n, directory) {
104
		const Path name_fs = Path::FromUTF8(child->GetName());
105

106
		if (name_fs.IsNull() || exclude_list.Check(name_fs)) {
107 108 109 110 111
			delete_directory(child);
			modified = true;
		}
	}

112
	Song *song, *ns;
113 114
	directory_for_each_song_safe(song, ns, directory) {
		assert(song->parent == directory);
115

116
		const Path name_fs = Path::FromUTF8(song->uri);
117
		if (name_fs.IsNull() || exclude_list.Check(name_fs)) {
118 119 120
			delete_song(directory, song);
			modified = true;
		}
121
	}
122 123

	db_unlock();
124 125 126
}

static void
127
purge_deleted_from_directory(Directory *directory)
128
{
129
	Directory *child, *n;
130 131
	directory_for_each_child_safe(child, n, directory) {
		if (directory_exists(child))
132 133
			continue;

134
		db_lock();
135
		delete_directory(child);
136 137
		db_unlock();

138 139 140
		modified = true;
	}

141
	Song *song, *ns;
142
	directory_for_each_song_safe(song, ns, directory) {
143
		const Path path = map_song_fs(song);
144
		if (path.IsNull() || !FileExists(path)) {
145
			db_lock();
146
			delete_song(directory, song);
147 148
			db_unlock();

149 150 151
			modified = true;
		}
	}
152

153 154 155 156
	for (auto i = directory->playlists.begin(),
		     end = directory->playlists.end();
	     i != end;) {
		if (!directory_child_is_regular(directory, i->name.c_str())) {
157
			db_lock();
158
			i = directory->playlists.erase(i);
159
			db_unlock();
160 161
		} else
			++i;
162
	}
163 164
}

165
#ifndef WIN32
166
static int
167
update_directory_stat(Directory *directory)
168 169
{
	struct stat st;
170
	if (stat_directory(directory, &st) < 0)
171 172
		return -1;

173
	directory_set_stat(directory, &st);
174 175
	return 0;
}
176
#endif
177 178

static int
179
find_inode_ancestor(Directory *parent, ino_t inode, dev_t device)
180
{
181
#ifndef WIN32
182
	while (parent) {
183
		if (!parent->have_stat && update_directory_stat(parent) < 0)
184
			return -1;
185

186
		if (parent->inode == inode && parent->device == device) {
187
			LogDebug(update_domain, "recursive directory found");
188 189
			return 1;
		}
190

191 192
		parent = parent->parent;
	}
193 194 195 196 197
#else
	(void)parent;
	(void)inode;
	(void)device;
#endif
198 199 200 201

	return 0;
}

202
static bool
203
update_playlist_file2(Directory *directory,
204 205 206 207 208 209
		      const char *name, const char *suffix,
		      const struct stat *st)
{
	if (!playlist_suffix_supported(suffix))
		return false;

210 211
	PlaylistInfo pi(name, st->st_mtime);

212
	db_lock();
213
	if (directory->playlists.UpdateOrInsert(std::move(pi)))
214 215 216 217 218 219
		modified = true;
	db_unlock();
	return true;
}

static bool
220
update_regular_file(Directory *directory,
221 222 223 224
		    const char *name, const struct stat *st)
{
	const char *suffix = uri_get_suffix(name);
	if (suffix == NULL)
225
		return false;
226

227
	return update_song_file(directory, name, suffix, st) ||
228
		update_archive_file(directory, name, suffix, st) ||
229
		update_playlist_file2(directory, name, suffix, st);
230 231 232
}

static bool
233
update_directory(Directory *directory, const struct stat *st);
234 235

static void
236
update_directory_child(Directory *directory,
237
		       const char *name, const struct stat *st)
238 239 240 241 242 243
{
	assert(strchr(name, '/') == NULL);

	if (S_ISREG(st->st_mode)) {
		update_regular_file(directory, name, st);
	} else if (S_ISDIR(st->st_mode)) {
244
		if (find_inode_ancestor(directory, st->st_ino, st->st_dev))
245 246
			return;

247
		db_lock();
248
		Directory *subdir = directory->MakeChild(name);
249 250
		db_unlock();

251 252
		assert(directory == subdir->parent);

253
		if (!update_directory(subdir, st)) {
254
			db_lock();
255
			delete_directory(subdir);
256 257
			db_unlock();
		}
258
	} else {
259 260
		FormatDebug(update_domain,
			    "%s is not a directory, archive or music", name);
261 262 263 264
	}
}

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

274
gcc_pure
275
static bool
276
skip_symlink(const Directory *directory, const char *utf8_name)
277 278
{
#ifndef WIN32
279 280
	const Path path_fs = map_directory_child_fs(directory, utf8_name);
	if (path_fs.IsNull())
281 282
		return true;

283 284
	const Path target = ReadLink(path_fs);
	if (target.IsNull())
285 286 287 288 289 290 291 292 293 294 295
		/* 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;
	}

296 297
	const char *target_str = target.c_str();

298
	if (Path::IsAbsoluteFS(target_str)) {
299 300
		/* if the symlink points to an absolute path, see if
		   that path is inside the music directory */
301 302
		const char *relative = map_to_relative_path(target_str);
		return relative > target_str
303 304 305
			? !follow_inside_symlinks
			: !follow_outside_symlinks;
	}
306

307
	const char *p = target_str;
308
	while (*p == '.') {
309
		if (p[1] == '.' && Path::IsSeparatorFS(p[2])) {
310 311 312 313 314 315 316 317 318
			/* "../" moves to parent directory */
			directory = directory->parent;
			if (directory == NULL) {
				/* we have moved outside the music
				   directory - skip this symlink
				   if such symlinks are not allowed */
				return !follow_outside_symlinks;
			}
			p += 3;
319
		} else if (Path::IsSeparatorFS(p[1]))
320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340
			/* 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
341
update_directory(Directory *directory, const struct stat *st)
342 343 344 345 346
{
	assert(S_ISDIR(st->st_mode));

	directory_set_stat(directory, st);

347 348
	const Path path_fs = map_directory_fs(directory);
	if (path_fs.IsNull())
349 350
		return false;

351 352 353 354
	DirectoryReader reader(path_fs);
	if (reader.HasFailed()) {
		int error = errno;
		const auto path_utf8 = path_fs.ToUTF8();
355 356 357
		FormatErrno(update_domain, error,
			    "Failed to open directory %s",
			    path_utf8.c_str());
358 359 360
		return false;
	}

361
	ExcludeList exclude_list;
362
	exclude_list.LoadFile(Path::Build(path_fs, ".mpdignore"));
363

364
	if (!exclude_list.IsEmpty())
365 366
		remove_excluded_from_directory(directory, exclude_list);

367
	purge_deleted_from_directory(directory);
368

369
	while (reader.ReadEntry()) {
370
		std::string utf8;
371 372
		struct stat st2;

373 374 375
		const Path entry = reader.GetEntry();

		if (skip_path(entry) || exclude_list.Check(entry))
376 377
			continue;

378
		utf8 = entry.ToUTF8();
379
		if (utf8.empty())
Max Kellermann's avatar
Max Kellermann committed
380 381
			continue;

382 383
		if (skip_symlink(directory, utf8.c_str())) {
			modified |= delete_name_in(directory, utf8.c_str());
384 385 386
			continue;
		}

387 388
		if (stat_directory_child(directory, utf8.c_str(), &st2) == 0)
			update_directory_child(directory, utf8.c_str(), &st2);
389
		else
390
			modified |= delete_name_in(directory, utf8.c_str());
391 392 393 394 395 396 397
	}

	directory->mtime = st->st_mtime;

	return true;
}

398 399
static Directory *
directory_make_child_checked(Directory *parent, const char *name_utf8)
400
{
401
	db_lock();
402
	Directory *directory = parent->FindChild(name_utf8);
403
	db_unlock();
404

405 406 407
	if (directory != NULL)
		return directory;

408
	struct stat st;
409
	if (stat_directory_child(parent, name_utf8, &st) < 0 ||
410
	    find_inode_ancestor(parent, st.st_ino, st.st_dev))
411 412
		return NULL;

Max Kellermann's avatar
Max Kellermann committed
413
	if (skip_symlink(parent, name_utf8))
414 415
		return NULL;

416 417
	/* if we're adding directory paths, make sure to delete filenames
	   with potentially the same name */
418
	db_lock();
419
	Song *conflicting = parent->FindSong(name_utf8);
420 421 422
	if (conflicting)
		delete_song(parent, conflicting);

423
	directory = parent->CreateChild(name_utf8);
424 425
	db_unlock();

426 427 428 429
	directory_set_stat(directory, &st);
	return directory;
}

430
static Directory *
431
directory_make_uri_parent_checked(const char *uri)
432
{
433
	Directory *directory = db_get_root();
434
	char *duplicated = g_strdup(uri);
435
	char *name_utf8 = duplicated, *slash;
436

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

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

443
		directory = directory_make_child_checked(directory, name_utf8);
444
		if (directory == NULL)
445 446
			break;

447
		name_utf8 = slash + 1;
448 449 450 451 452 453 454
	}

	g_free(duplicated);
	return directory;
}

static void
455
update_uri(const char *uri)
456
{
457
	Directory *parent = directory_make_uri_parent_checked(uri);
458 459 460
	if (parent == NULL)
		return;

461
	char *name = g_path_get_basename(uri);
462

463
	struct stat st;
464 465
	if (!skip_symlink(parent, name) &&
	    stat_directory_child(parent, name, &st) == 0)
466
		update_directory_child(parent, name, &st);
467
	else
468
		modified |= delete_name_in(parent, name);
469 470 471 472 473

	g_free(name);
}

bool
474
update_walk(const char *path, bool discard)
475
{
476
	walk_discard = discard;
477 478 479
	modified = false;

	if (path != NULL && !isRootDirectory(path)) {
480
		update_uri(path);
481
	} else {
482
		Directory *directory = db_get_root();
483 484 485
		struct stat st;

		if (stat_directory(directory, &st) == 0)
486
			update_directory(directory, &st);
487 488 489 490
	}

	return modified;
}