Walk.cxx 10.6 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 26 27
#include "db/DatabaseLock.hxx"
#include "db/Directory.hxx"
#include "db/Song.hxx"
28
#include "db/PlaylistVector.hxx"
29
#include "db/Uri.hxx"
30
#include "playlist/PlaylistRegistry.hxx"
Max Kellermann's avatar
Max Kellermann committed
31
#include "Mapper.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/DirectoryReader.hxx"
39
#include "util/Alloc.hxx"
Max Kellermann's avatar
Max Kellermann committed
40
#include "util/UriUtil.hxx"
41
#include "Log.hxx"
42 43 44 45

#include <assert.h>
#include <sys/stat.h>
#include <string.h>
46
#include <stdlib.h>
47 48
#include <errno.h>

49 50
UpdateWalk::UpdateWalk(EventLoop &_loop, DatabaseListener &_listener)
	:editor(_loop, _listener)
51 52 53 54 55 56 57 58 59 60 61 62 63
{
#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
64
directory_set_stat(Directory &dir, const struct stat *st)
65
{
66 67 68
	dir.inode = st->st_ino;
	dir.device = st->st_dev;
	dir.have_stat = true;
69 70
}

71 72 73
inline void
UpdateWalk::RemoveExcludedFromDirectory(Directory &directory,
					const ExcludeList &exclude_list)
74
{
75 76
	db_lock();

77
	Directory *child, *n;
78
	directory_for_each_child_safe(child, n, directory) {
79
		const auto name_fs = AllocatedPath::FromUTF8(child->GetName());
80

81
		if (name_fs.IsNull() || exclude_list.Check(name_fs)) {
82
			editor.DeleteDirectory(child);
83 84 85 86
			modified = true;
		}
	}

87
	Song *song, *ns;
88
	directory_for_each_song_safe(song, ns, directory) {
89
		assert(song->parent == &directory);
90

91
		const auto name_fs = AllocatedPath::FromUTF8(song->uri);
92
		if (name_fs.IsNull() || exclude_list.Check(name_fs)) {
93
			editor.DeleteSong(directory, song);
94 95
			modified = true;
		}
96
	}
97 98

	db_unlock();
99 100
}

101 102
inline void
UpdateWalk::PurgeDeletedFromDirectory(Directory &directory)
103
{
104
	Directory *child, *n;
105
	directory_for_each_child_safe(child, n, directory) {
106
		if (directory_exists(*child))
107 108
			continue;

109
		editor.LockDeleteDirectory(child);
110

111 112 113
		modified = true;
	}

114
	Song *song, *ns;
115
	directory_for_each_song_safe(song, ns, directory) {
116
		const auto path = map_song_fs(*song);
117
		if (path.IsNull() || !FileExists(path)) {
118
			editor.LockDeleteSong(directory, song);
119

120 121 122
			modified = true;
		}
	}
123

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

136
#ifndef WIN32
137
static int
138
update_directory_stat(Directory &directory)
139 140
{
	struct stat st;
141
	if (stat_directory(directory, &st) < 0)
142 143
		return -1;

144
	directory_set_stat(directory, &st);
145 146
	return 0;
}
147
#endif
148 149

static int
150
find_inode_ancestor(Directory *parent, ino_t inode, dev_t device)
151
{
152
#ifndef WIN32
153
	while (parent) {
154
		if (!parent->have_stat && update_directory_stat(*parent) < 0)
155
			return -1;
156

157
		if (parent->inode == inode && parent->device == device) {
158
			LogDebug(update_domain, "recursive directory found");
159 160
			return 1;
		}
161

162 163
		parent = parent->parent;
	}
164 165 166 167 168
#else
	(void)parent;
	(void)inode;
	(void)device;
#endif
169 170 171 172

	return 0;
}

173 174 175 176
inline bool
UpdateWalk::UpdatePlaylistFile(Directory &directory,
			       const char *name, const char *suffix,
			       const struct stat *st)
177 178 179 180
{
	if (!playlist_suffix_supported(suffix))
		return false;

181 182
	PlaylistInfo pi(name, st->st_mtime);

183
	db_lock();
184
	if (directory.playlists.UpdateOrInsert(std::move(pi)))
185 186 187 188 189
		modified = true;
	db_unlock();
	return true;
}

190 191 192
inline bool
UpdateWalk::UpdateRegularFile(Directory &directory,
			      const char *name, const struct stat *st)
193 194
{
	const char *suffix = uri_get_suffix(name);
195
	if (suffix == nullptr)
196
		return false;
197

198 199 200
	return UpdateSongFile(directory, name, suffix, st) ||
		UpdateArchiveFile(directory, name, suffix, st) ||
		UpdatePlaylistFile(directory, name, suffix, st);
201 202
}

203 204 205
void
UpdateWalk::UpdateDirectoryChild(Directory &directory,
				 const char *name, const struct stat *st)
206
{
207
	assert(strchr(name, '/') == nullptr);
208 209

	if (S_ISREG(st->st_mode)) {
210
		UpdateRegularFile(directory, name, st);
211
	} else if (S_ISDIR(st->st_mode)) {
212
		if (find_inode_ancestor(&directory, st->st_ino, st->st_dev))
213 214
			return;

215
		db_lock();
216
		Directory *subdir = directory.MakeChild(name);
217 218
		db_unlock();

219
		assert(&directory == subdir->parent);
220

221 222
		if (!UpdateDirectory(*subdir, st))
			editor.LockDeleteDirectory(subdir);
223
	} else {
224 225
		FormatDebug(update_domain,
			    "%s is not a directory, archive or music", name);
226 227 228 229
	}
}

/* we don't look at "." / ".." nor files with newlines in their name */
230
gcc_pure
231
static bool skip_path(Path path_fs)
232
{
233
	const char *path = path_fs.c_str();
234 235
	return (path[0] == '.' && path[1] == 0) ||
		(path[0] == '.' && path[1] == '.' && path[2] == 0) ||
236
		strchr(path, '\n') != nullptr;
237 238
}

239
gcc_pure
240 241 242
bool
UpdateWalk::SkipSymlink(const Directory *directory,
			const char *utf8_name) const
243 244
{
#ifndef WIN32
245
	const auto path_fs = map_directory_child_fs(*directory, utf8_name);
246
	if (path_fs.IsNull())
247 248
		return true;

249
	const auto target = ReadLink(path_fs);
250
	if (target.IsNull())
251 252 253 254 255 256 257 258 259 260 261
		/* 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;
	}

262 263
	const char *target_str = target.c_str();

264
	if (PathTraitsFS::IsAbsolute(target_str)) {
265 266
		/* if the symlink points to an absolute path, see if
		   that path is inside the music directory */
267 268
		const char *relative = map_to_relative_path(target_str);
		return relative > target_str
269 270 271
			? !follow_inside_symlinks
			: !follow_outside_symlinks;
	}
272

273
	const char *p = target_str;
274
	while (*p == '.') {
275
		if (p[1] == '.' && PathTraitsFS::IsSeparator(p[2])) {
276 277
			/* "../" moves to parent directory */
			directory = directory->parent;
278
			if (directory == nullptr) {
279 280 281 282 283 284
				/* we have moved outside the music
				   directory - skip this symlink
				   if such symlinks are not allowed */
				return !follow_outside_symlinks;
			}
			p += 3;
285
		} else if (PathTraitsFS::IsSeparator(p[1]))
286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305
			/* 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
}

306 307
bool
UpdateWalk::UpdateDirectory(Directory &directory, const struct stat *st)
308 309 310 311 312
{
	assert(S_ISDIR(st->st_mode));

	directory_set_stat(directory, st);

313
	const auto path_fs = map_directory_fs(directory);
314
	if (path_fs.IsNull())
315 316
		return false;

317 318 319 320
	DirectoryReader reader(path_fs);
	if (reader.HasFailed()) {
		int error = errno;
		const auto path_utf8 = path_fs.ToUTF8();
321 322 323
		FormatErrno(update_domain, error,
			    "Failed to open directory %s",
			    path_utf8.c_str());
324 325 326
		return false;
	}

327
	ExcludeList exclude_list;
328
	exclude_list.LoadFile(AllocatedPath::Build(path_fs, ".mpdignore"));
329

330
	if (!exclude_list.IsEmpty())
331
		RemoveExcludedFromDirectory(directory, exclude_list);
332

333
	PurgeDeletedFromDirectory(directory);
334

335
	while (reader.ReadEntry()) {
336
		const auto entry = reader.GetEntry();
337 338

		if (skip_path(entry) || exclude_list.Check(entry))
339 340
			continue;

341
		const std::string utf8 = entry.ToUTF8();
342
		if (utf8.empty())
Max Kellermann's avatar
Max Kellermann committed
343 344
			continue;

345 346
		if (SkipSymlink(&directory, utf8.c_str())) {
			modified |= editor.DeleteNameIn(directory, utf8.c_str());
347 348 349
			continue;
		}

350
		struct stat st2;
351
		if (stat_directory_child(directory, utf8.c_str(), &st2) == 0)
352
			UpdateDirectoryChild(directory, utf8.c_str(), &st2);
353
		else
354
			modified |= editor.DeleteNameIn(directory, utf8.c_str());
355 356
	}

357
	directory.mtime = st->st_mtime;
358 359 360 361

	return true;
}

362 363
inline Directory *
UpdateWalk::DirectoryMakeChildChecked(Directory &parent, const char *name_utf8)
364
{
365
	db_lock();
366
	Directory *directory = parent.FindChild(name_utf8);
367
	db_unlock();
368

369
	if (directory != nullptr)
370 371
		return directory;

372
	struct stat st;
373
	if (stat_directory_child(parent, name_utf8, &st) < 0 ||
374
	    find_inode_ancestor(&parent, st.st_ino, st.st_dev))
375
		return nullptr;
376

377
	if (SkipSymlink(&parent, name_utf8))
378
		return nullptr;
379

380 381
	/* if we're adding directory paths, make sure to delete filenames
	   with potentially the same name */
382
	db_lock();
383
	Song *conflicting = parent.FindSong(name_utf8);
384
	if (conflicting)
385
		editor.DeleteSong(parent, conflicting);
386

387
	directory = parent.CreateChild(name_utf8);
388 389
	db_unlock();

390
	directory_set_stat(*directory, &st);
391 392 393
	return directory;
}

394
inline Directory *
395
UpdateWalk::DirectoryMakeUriParentChecked(Directory &root, const char *uri)
396
{
397
	Directory *directory = &root;
398
	char *duplicated = xstrdup(uri);
399
	char *name_utf8 = duplicated, *slash;
400

401
	while ((slash = strchr(name_utf8, '/')) != nullptr) {
402 403
		*slash = 0;

404 405 406
		if (*name_utf8 == 0)
			continue;

407 408
		directory = DirectoryMakeChildChecked(*directory,
						      name_utf8);
409
		if (directory == nullptr)
410 411
			break;

412
		name_utf8 = slash + 1;
413 414
	}

415
	free(duplicated);
416 417 418
	return directory;
}

419
inline void
420
UpdateWalk::UpdateUri(Directory &root, const char *uri)
421
{
422
	Directory *parent = DirectoryMakeUriParentChecked(root, uri);
423
	if (parent == nullptr)
424 425
		return;

426
	const char *name = PathTraitsUTF8::GetBase(uri);
427

428
	struct stat st;
429
	if (!SkipSymlink(parent, name) &&
430
	    stat_directory_child(*parent, name, &st) == 0)
431
		UpdateDirectoryChild(*parent, name, &st);
432
	else
433
		modified |= editor.DeleteNameIn(*parent, name);
434 435 436
}

bool
437
UpdateWalk::Walk(Directory &root, const char *path, bool discard)
438
{
439
	walk_discard = discard;
440 441
	modified = false;

442
	if (path != nullptr && !isRootDirectory(path)) {
443
		UpdateUri(root, path);
444 445 446
	} else {
		struct stat st;

447 448
		if (stat_directory(root, &st) == 0)
			UpdateDirectory(root, &st);
449 450 451 452
	}

	return modified;
}