ExcludeList: convert to a class
This commit is contained in:
@@ -34,8 +34,8 @@ extern "C" {
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
|
|
||||||
GSList *
|
bool
|
||||||
exclude_list_load(const char *path_fs)
|
ExcludeList::LoadFile(const char *path_fs)
|
||||||
{
|
{
|
||||||
assert(path_fs != NULL);
|
assert(path_fs != NULL);
|
||||||
|
|
||||||
@@ -48,10 +48,9 @@ exclude_list_load(const char *path_fs)
|
|||||||
g_free(path_utf8);
|
g_free(path_utf8);
|
||||||
}
|
}
|
||||||
|
|
||||||
return NULL;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
GSList *list = NULL;
|
|
||||||
char line[1024];
|
char line[1024];
|
||||||
while (fgets(line, sizeof(line), file) != NULL) {
|
while (fgets(line, sizeof(line), file) != NULL) {
|
||||||
char *p = strchr(line, '#');
|
char *p = strchr(line, '#');
|
||||||
@@ -60,37 +59,24 @@ exclude_list_load(const char *path_fs)
|
|||||||
|
|
||||||
p = g_strstrip(line);
|
p = g_strstrip(line);
|
||||||
if (*p != 0)
|
if (*p != 0)
|
||||||
list = g_slist_prepend(list, g_pattern_spec_new(p));
|
patterns.emplace_front(p);
|
||||||
}
|
}
|
||||||
|
|
||||||
fclose(file);
|
fclose(file);
|
||||||
|
|
||||||
return list;
|
return true;
|
||||||
}
|
|
||||||
|
|
||||||
void
|
|
||||||
exclude_list_free(GSList *list)
|
|
||||||
{
|
|
||||||
while (list != NULL) {
|
|
||||||
GPatternSpec *pattern = (GPatternSpec *)list->data;
|
|
||||||
g_pattern_spec_free(pattern);
|
|
||||||
list = g_slist_remove(list, list->data);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool
|
bool
|
||||||
exclude_list_check(GSList *list, const char *name_fs)
|
ExcludeList::Check(const char *name_fs) const
|
||||||
{
|
{
|
||||||
assert(name_fs != NULL);
|
assert(name_fs != NULL);
|
||||||
|
|
||||||
/* XXX include full path name in check */
|
/* XXX include full path name in check */
|
||||||
|
|
||||||
for (; list != NULL; list = list->next) {
|
for (const auto &i : patterns)
|
||||||
GPatternSpec *pattern = (GPatternSpec *)list->data;
|
if (i.Check(name_fs))
|
||||||
|
|
||||||
if (g_pattern_match_string(pattern, name_fs))
|
|
||||||
return true;
|
return true;
|
||||||
}
|
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
@@ -25,25 +25,54 @@
|
|||||||
#ifndef MPD_EXCLUDE_H
|
#ifndef MPD_EXCLUDE_H
|
||||||
#define MPD_EXCLUDE_H
|
#define MPD_EXCLUDE_H
|
||||||
|
|
||||||
|
#include "gcc.h"
|
||||||
|
|
||||||
|
#include <forward_list>
|
||||||
|
|
||||||
#include <glib.h>
|
#include <glib.h>
|
||||||
|
|
||||||
/**
|
class ExcludeList {
|
||||||
* Loads and parses a .mpdignore file.
|
class Pattern {
|
||||||
*/
|
GPatternSpec *pattern;
|
||||||
GSList *
|
|
||||||
exclude_list_load(const char *path_fs);
|
|
||||||
|
|
||||||
/**
|
public:
|
||||||
* Frees a list returned by exclude_list_load().
|
Pattern(const char *_pattern)
|
||||||
*/
|
:pattern(g_pattern_spec_new(_pattern)) {}
|
||||||
void
|
|
||||||
exclude_list_free(GSList *list);
|
Pattern(Pattern &&other)
|
||||||
|
:pattern(other.pattern) {
|
||||||
|
other.pattern = nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
~Pattern() {
|
||||||
|
g_pattern_spec_free(pattern);
|
||||||
|
}
|
||||||
|
|
||||||
|
gcc_pure
|
||||||
|
bool Check(const char *name_fs) const {
|
||||||
|
return g_pattern_match_string(pattern, name_fs);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
std::forward_list<Pattern> patterns;
|
||||||
|
|
||||||
|
public:
|
||||||
|
gcc_pure
|
||||||
|
bool IsEmpty() const {
|
||||||
|
return patterns.empty();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Loads and parses a .mpdignore file.
|
||||||
|
*/
|
||||||
|
bool LoadFile(const char *path_fs);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Checks whether one of the patterns in the .mpdignore file matches
|
||||||
|
* the specified file name.
|
||||||
|
*/
|
||||||
|
bool Check(const char *name_fs) const;
|
||||||
|
};
|
||||||
|
|
||||||
/**
|
|
||||||
* Checks whether one of the patterns in the .mpdignore file matches
|
|
||||||
* the specified file name.
|
|
||||||
*/
|
|
||||||
bool
|
|
||||||
exclude_list_check(GSList *list, const char *name_fs);
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
@@ -95,7 +95,8 @@ directory_set_stat(Directory *dir, const struct stat *st)
|
|||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
remove_excluded_from_directory(Directory *directory, GSList *exclude_list)
|
remove_excluded_from_directory(Directory *directory,
|
||||||
|
const ExcludeList &exclude_list)
|
||||||
{
|
{
|
||||||
db_lock();
|
db_lock();
|
||||||
|
|
||||||
@@ -103,7 +104,7 @@ remove_excluded_from_directory(Directory *directory, GSList *exclude_list)
|
|||||||
directory_for_each_child_safe(child, n, directory) {
|
directory_for_each_child_safe(child, n, directory) {
|
||||||
char *name_fs = utf8_to_fs_charset(child->GetName());
|
char *name_fs = utf8_to_fs_charset(child->GetName());
|
||||||
|
|
||||||
if (exclude_list_check(exclude_list, name_fs)) {
|
if (exclude_list.Check(name_fs)) {
|
||||||
delete_directory(child);
|
delete_directory(child);
|
||||||
modified = true;
|
modified = true;
|
||||||
}
|
}
|
||||||
@@ -116,7 +117,7 @@ remove_excluded_from_directory(Directory *directory, GSList *exclude_list)
|
|||||||
assert(song->parent == directory);
|
assert(song->parent == directory);
|
||||||
|
|
||||||
char *name_fs = utf8_to_fs_charset(song->uri);
|
char *name_fs = utf8_to_fs_charset(song->uri);
|
||||||
if (exclude_list_check(exclude_list, name_fs)) {
|
if (exclude_list.Check(name_fs)) {
|
||||||
delete_song(directory, song);
|
delete_song(directory, song);
|
||||||
modified = true;
|
modified = true;
|
||||||
}
|
}
|
||||||
@@ -371,12 +372,13 @@ update_directory(Directory *directory, const struct stat *st)
|
|||||||
}
|
}
|
||||||
|
|
||||||
char *exclude_path_fs = g_build_filename(path_fs, ".mpdignore", NULL);
|
char *exclude_path_fs = g_build_filename(path_fs, ".mpdignore", NULL);
|
||||||
GSList *exclude_list = exclude_list_load(exclude_path_fs);
|
ExcludeList exclude_list;
|
||||||
|
exclude_list.LoadFile(exclude_path_fs);
|
||||||
g_free(exclude_path_fs);
|
g_free(exclude_path_fs);
|
||||||
|
|
||||||
g_free(path_fs);
|
g_free(path_fs);
|
||||||
|
|
||||||
if (exclude_list != NULL)
|
if (!exclude_list.IsEmpty())
|
||||||
remove_excluded_from_directory(directory, exclude_list);
|
remove_excluded_from_directory(directory, exclude_list);
|
||||||
|
|
||||||
purge_deleted_from_directory(directory);
|
purge_deleted_from_directory(directory);
|
||||||
@@ -386,8 +388,7 @@ update_directory(Directory *directory, const struct stat *st)
|
|||||||
char *utf8;
|
char *utf8;
|
||||||
struct stat st2;
|
struct stat st2;
|
||||||
|
|
||||||
if (skip_path(ent->d_name) ||
|
if (skip_path(ent->d_name) || exclude_list.Check(ent->d_name))
|
||||||
exclude_list_check(exclude_list, ent->d_name))
|
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
utf8 = fs_charset_to_utf8(ent->d_name);
|
utf8 = fs_charset_to_utf8(ent->d_name);
|
||||||
@@ -408,8 +409,6 @@ update_directory(Directory *directory, const struct stat *st)
|
|||||||
g_free(utf8);
|
g_free(utf8);
|
||||||
}
|
}
|
||||||
|
|
||||||
exclude_list_free(exclude_list);
|
|
||||||
|
|
||||||
closedir(dir);
|
closedir(dir);
|
||||||
|
|
||||||
directory->mtime = st->st_mtime;
|
directory->mtime = st->st_mtime;
|
||||||
|
Reference in New Issue
Block a user