ExcludeList.cxx 1.86 KB
Newer Older
1

2
/*
3
 * Copyright (C) 2003-2013 The Music Player Daemon Project
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
 * 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.
 *
 */

26
#include "config.h"
Max Kellermann's avatar
Max Kellermann committed
27
#include "ExcludeList.hxx"
28
#include "fs/Path.hxx"
29
#include "fs/FileSystem.hxx"
30 31
#include "util/Domain.hxx"
#include "Log.hxx"
32 33 34 35 36

#include <assert.h>
#include <string.h>
#include <errno.h>

37 38
static constexpr Domain exclude_list_domain("exclude_list");

39
bool
40
ExcludeList::LoadFile(Path path_fs)
41
{
42
	FILE *file = FOpen(path_fs, FOpenMode::ReadText);
43
	if (file == nullptr) {
44 45
		const int e = errno;
		if (e != ENOENT) {
46
			const auto path_utf8 = path_fs.ToUTF8();
47 48 49
			FormatErrno(exclude_list_domain,
				    "Failed to open %s",
				    path_utf8.c_str());
50 51
		}

52
		return false;
53 54
	}

55
	char line[1024];
56
	while (fgets(line, sizeof(line), file) != nullptr) {
57
		char *p = strchr(line, '#');
58
		if (p != nullptr)
59 60 61 62
			*p = 0;

		p = g_strstrip(line);
		if (*p != 0)
63
			patterns.emplace_front(p);
64 65 66 67
	}

	fclose(file);

68
	return true;
69 70 71
}

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

	/* XXX include full path name in check */

78
	for (const auto &i : patterns)
79
		if (i.Check(name_fs.c_str()))
80 81 82 83
			return true;

	return false;
}