ExcludeList.cxx 2 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2017 The Music Player Daemon Project
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
 * 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.
 */

/*
 * The .mpdignore backend code.
 *
 */

25
#include "config.h"
Max Kellermann's avatar
Max Kellermann committed
26
#include "ExcludeList.hxx"
27
#include "fs/Path.hxx"
28
#include "fs/NarrowPath.hxx"
29
#include "fs/io/TextFile.hxx"
30
#include "system/Error.hxx"
31
#include "Log.hxx"
32

33 34
#include <stdexcept>

35 36 37
#include <assert.h>
#include <string.h>

38
bool
39
ExcludeList::LoadFile(Path path_fs)
40
try {
41
#ifdef HAVE_CLASS_GLOB
42
	TextFile file(path_fs);
43

44 45
	char *line;
	while ((line = file.ReadLine()) != nullptr) {
46
		char *p = strchr(line, '#');
47
		if (p != nullptr)
48 49
			*p = 0;

50
		p = Strip(line);
51
		if (*p != 0)
52
			patterns.emplace_front(p);
53
	}
54
#else
55
	/* not implemented */
56 57
	(void)path_fs;
#endif
58

59
	return true;
60 61 62 63 64 65 66
} catch (const std::system_error &e) {
	if (!IsFileNotFound(e))
		LogError(e);
	return false;
} catch (const std::exception &e) {
	LogError(e);
	return false;
67 68 69
}

bool
70
ExcludeList::Check(Path name_fs) const
71
{
72
	assert(!name_fs.IsNull());
73 74 75

	/* XXX include full path name in check */

76
#ifdef HAVE_CLASS_GLOB
77 78 79 80 81 82
	if (parent != nullptr) {
		if (parent->Check(name_fs)) {
			return true;
		}
	}

83 84 85 86 87 88 89
	for (const auto &i : patterns) {
		try {
			if (i.Check(NarrowPath(name_fs).c_str()))
				return true;
		} catch (const std::runtime_error &) {
		}
	}
90
#else
91
	/* not implemented */
92 93
	(void)name_fs;
#endif
94 95 96

	return false;
}