UpdateWalk.cxx 10.7 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"
Max Kellermann's avatar
Max Kellermann committed
26
#include "DatabaseLock.hxx"
27
#include "DatabaseSimple.hxx"
28
#include "Directory.hxx"
29
#include "Song.hxx"
30
#include "PlaylistVector.hxx"
31
#include "PlaylistRegistry.hxx"
Max Kellermann's avatar
Max Kellermann committed
32
#include "Mapper.hxx"
Max Kellermann's avatar
Max Kellermann committed
33
#include "ExcludeList.hxx"
34
#include "conf.h"
35
#include "fs/Path.hxx"
36
#include "fs/FileSystem.hxx"
37
#include "fs/DirectoryReader.hxx"
Max Kellermann's avatar
Max Kellermann committed
38
#include "util/UriUtil.hxx"
39 40 41 42 43 44 45 46 47 48 49 50

#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>

51 52 53
#undef G_LOG_DOMAIN
#define G_LOG_DOMAIN "update"

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 G_OS_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 G_OS_WIN32
182
	while (parent) {
183
		if (!parent->have_stat && update_directory_stat(parent) < 0)
184
			return -1;
185

186 187 188 189
		if (parent->inode == inode && parent->device == device) {
			g_debug("recursive directory found");
			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 259 260 261 262 263
	} else {
		g_debug("update: %s is not a directory, archive or music", name);
	}
}

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

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

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

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

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

306
	const char *p = target_str;
307
	while (*p == '.') {
308
		if (p[1] == '.' && G_IS_DIR_SEPARATOR(p[2])) {
309 310 311 312 313 314 315 316 317
			/* "../" 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;
318
		} else if (G_IS_DIR_SEPARATOR(p[1]))
319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339
			/* 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
340
update_directory(Directory *directory, const struct stat *st)
341 342 343 344 345
{
	assert(S_ISDIR(st->st_mode));

	directory_set_stat(directory, st);

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

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

359
	ExcludeList exclude_list;
360
	exclude_list.LoadFile(Path::Build(path_fs, ".mpdignore"));
361

362
	if (!exclude_list.IsEmpty())
363 364
		remove_excluded_from_directory(directory, exclude_list);

365
	purge_deleted_from_directory(directory);
366

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

371 372 373
		const Path entry = reader.GetEntry();

		if (skip_path(entry) || exclude_list.Check(entry))
374 375
			continue;

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

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

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

	directory->mtime = st->st_mtime;

	return true;
}

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

403 404 405
	if (directory != NULL)
		return directory;

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

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

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

421
	directory = parent->CreateChild(name_utf8);
422 423
	db_unlock();

424 425 426 427
	directory_set_stat(directory, &st);
	return directory;
}

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

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

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

441
		directory = directory_make_child_checked(directory, name_utf8);
442
		if (directory == NULL)
443 444
			break;

445
		name_utf8 = slash + 1;
446 447 448 449 450 451 452
	}

	g_free(duplicated);
	return directory;
}

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

459
	char *name = g_path_get_basename(uri);
460

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

	g_free(name);
}

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

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

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

	return modified;
}