New .snapraidignore feature

This commit is contained in:
Andrea Mazzoleni 2026-01-10 16:30:35 +01:00
parent 6a912b4726
commit 36cef9ee89
52 changed files with 2278 additions and 158 deletions

View file

@ -42,7 +42,7 @@ void content_free(struct snapraid_content* content)
free(content);
}
struct snapraid_filter* filter_alloc_file(int direction, const char* pattern)
struct snapraid_filter* filter_alloc_file(int direction, const char* root, const char* pattern)
{
struct snapraid_filter* filter;
char* i;
@ -53,11 +53,13 @@ struct snapraid_filter* filter_alloc_file(int direction, const char* pattern)
filter = malloc_nofail(sizeof(struct snapraid_filter));
pathimport(filter->pattern, sizeof(filter->pattern), pattern);
pathimport(filter->root, sizeof(filter->root), root);
filter->direction = direction;
/* find first and last slash */
first = 0;
last = 0;
/* reject invalid tokens, like "<empty>", ".", ".." and more dots */
token_is_valid = 0;
token_is_filled = 0;
@ -145,7 +147,7 @@ struct snapraid_filter* filter_alloc_disk(int direction, const char* pattern)
return filter;
}
void filter_free(struct snapraid_filter* filter)
void filter_free(void* filter)
{
free(filter);
}
@ -200,9 +202,16 @@ static int filter_recurse(struct snapraid_filter* filter, struct snapraid_filter
char* name;
unsigned i;
if (filter->root[0] != 0) { /* if it's a local filter */
if (!path_is_root_of(filter->root, const_path)) /* applies it only if it matches the root */
return 0;
/* advance the path to compare to make it local */
const_path += strlen(filter->root);
}
pathcpy(path, sizeof(path), const_path);
/* filter for all the directories */
/* filter all the dir/subdir until we reach the final file/dir */
name = path;
for (i = 0; path[i] != 0; ++i) {
if (path[i] == '/') {
@ -221,7 +230,7 @@ static int filter_recurse(struct snapraid_filter* filter, struct snapraid_filter
}
}
/* filter the final file */
/* filter the final file/dir */
if (filter_apply(filter, reason, path, name, is_dir) != 0)
return filter->direction;
@ -232,7 +241,7 @@ static int filter_element(tommy_list* filterlist, struct snapraid_filter** reaso
{
tommy_node* i;
int direction = 1; /* by default include all */
int default_direction = 1; /* by default include all */
/* for each filter */
for (i = tommy_list_head(filterlist); i != 0; i = i->next) {
@ -256,10 +265,10 @@ static int filter_element(tommy_list* filterlist, struct snapraid_filter** reaso
} else if (ret < 0) {
/* exclude the file */
return -1;
} else {
/* default is opposite of the last filter */
direction = -filter->direction;
if (reason != 0 && direction < 0)
} else if (filter->root[0] == 0) {
/* default is opposite of the last global rule (not counting local rules) */
default_direction = -filter->direction;
if (reason != 0 && default_direction < 0)
*reason = filter;
/* continue with the next one */
}
@ -270,8 +279,8 @@ static int filter_element(tommy_list* filterlist, struct snapraid_filter** reaso
if (is_def_include)
return 0;
/* files are excluded/included depending of the last rule processed */
if (direction < 0)
/* files are excluded/included depending of the last rule processed (not counting local filters) */
if (default_direction < 0)
return -1;
return 0;

View file

@ -60,9 +60,10 @@ struct snapraid_content {
*/
struct snapraid_filter {
char pattern[PATH_MAX]; /**< Filter pattern. */
int is_disk; /**< If the pattern is a disk one. */
int is_path; /**< If the pattern is only for the complete path. */
int is_dir; /**< If the pattern is only for dir. */
char root[PATH_MAX]; /**< If empty, it's a global pattern. If not empty, it's a local pattern that applies only to that dir. */
int is_disk; /**< If the pattern is a disk one, otherwise it's a file pattern */
int is_path; /**< If the pattern is a complete path, otherwise it's just a file name */
int is_dir; /**< If the pattern is only for dir, otherwise it's only for file. */
int direction; /**< If it's an inclusion (=1) or an exclusion (=-1). */
tommy_node node; /**< Next node in the list. */
};
@ -374,7 +375,7 @@ struct snapraid_disk {
/**
* Mutex for protecting the scan process.
*
* It's used during the scan process to protect the stampset to identity copy of files
* It's used during the scan process to protect the stampset to identify copy of files
*/
thread_mutex_t stamp_mutex;
#endif
@ -500,7 +501,7 @@ void content_free(struct snapraid_content* content);
/**
* Allocate a filter pattern for files and directories.
*/
struct snapraid_filter* filter_alloc_file(int is_include, const char* pattern);
struct snapraid_filter* filter_alloc_file(int is_include, const char* root, const char* pattern);
/**
* Allocate a filter pattern for disks.
@ -510,7 +511,7 @@ struct snapraid_filter* filter_alloc_disk(int is_include, const char* pattern);
/**
* Deallocate an exclusion.
*/
void filter_free(struct snapraid_filter* filter);
void filter_free(void* filter);
/**
* Filter type description.

View file

@ -51,6 +51,7 @@ struct snapraid_scan {
tommy_list file_insert_list; /**< Files to insert. */
tommy_list link_insert_list; /**< Links to insert. */
tommy_list dir_insert_list; /**< Dirs to insert. */
tommy_list local_filter_list; /**< Filter list specific for the disk. */
/* nodes for data structures */
tommy_node node;
@ -73,6 +74,7 @@ static struct snapraid_scan* scan_alloc(struct snapraid_state* state, struct sna
tommy_list_init(&scan->file_insert_list);
tommy_list_init(&scan->link_insert_list);
tommy_list_init(&scan->dir_insert_list);
tommy_list_init(&scan->local_filter_list);
scan->is_diff = is_diff;
scan->need_write = 0;
@ -88,7 +90,7 @@ static void scan_free(struct snapraid_scan* scan)
#if HAVE_THREAD
thread_mutex_destroy(&scan->disk->stamp_mutex);
#endif
tommy_list_foreach(&scan->local_filter_list, filter_free);
free(scan);
}
@ -1346,6 +1348,10 @@ static int scan_sub(struct snapraid_scan* scan, int level, int is_diff, char* pa
/* insert in the list */
tommy_list_insert_tail(&list, &entry->node, entry);
/* process ignore files */
if (strcmp(".snapraidignore", dd->d_name) == 0)
state_load_ignore_file(&scan->local_filter_list, path_next, sub_next);
}
if (closedir(d) != 0) {
@ -1434,7 +1440,8 @@ static int scan_sub(struct snapraid_scan* scan, int level, int is_diff, char* pa
}
if (type == 0) { /* REG */
if (filter_path(&state->filterlist, &reason, disk->name, sub_next) == 0) {
if (filter_path(&state->filterlist, &reason, disk->name, sub_next) == 0
&& filter_path(&scan->local_filter_list, &reason, disk->name, sub_next) == 0) {
/* late stat, if not yet called */
if (!st)
@ -1459,7 +1466,8 @@ static int scan_sub(struct snapraid_scan* scan, int level, int is_diff, char* pa
msg_verbose("Excluding file '%s' for rule '%s'\n", path_next, filter_type(reason, tmp, PATH_MAX));
}
} else if (type == 1) { /* LNK */
if (filter_path(&state->filterlist, &reason, disk->name, sub_next) == 0) {
if (filter_path(&state->filterlist, &reason, disk->name, sub_next) == 0
&& filter_path(&scan->local_filter_list, &reason, disk->name, sub_next) == 0) {
int ret;
ret = readlink(path_next, tmp, PATH_MAX);
@ -1488,7 +1496,8 @@ static int scan_sub(struct snapraid_scan* scan, int level, int is_diff, char* pa
msg_verbose("Excluding link '%s' for rule '%s'\n", path_next, filter_type(reason, tmp, PATH_MAX));
}
} else if (type == 2) { /* DIR */
if (filter_subdir(&state->filterlist, &reason, disk->name, sub_next) == 0) {
if (filter_subdir(&state->filterlist, &reason, disk->name, sub_next) == 0
&& filter_subdir(&scan->local_filter_list, &reason, disk->name, sub_next) == 0) {
#ifndef _WIN32
/* late stat, if not yet called */
if (!st)
@ -1517,7 +1526,8 @@ static int scan_sub(struct snapraid_scan* scan, int level, int is_diff, char* pa
msg_verbose("Excluding directory '%s' for rule '%s'\n", path_next, filter_type(reason, tmp, PATH_MAX));
}
} else {
if (filter_path(&state->filterlist, &reason, disk->name, sub_next) == 0) {
if (filter_path(&state->filterlist, &reason, disk->name, sub_next) == 0
&& filter_path(&scan->local_filter_list, &reason, disk->name, sub_next) == 0) {
/* late stat, if not yet called */
if (!st)
st = DSTAT(path_next, dd, &st_buf);

View file

@ -996,7 +996,7 @@ int snapraid_main(int argc, char* argv[])
pathimport(conf, sizeof(conf), optarg);
break;
case 'f' : {
struct snapraid_filter* filter = filter_alloc_file(1, optarg);
struct snapraid_filter* filter = filter_alloc_file(1, "", optarg);
if (!filter) {
/* LCOV_EXCL_START */
log_fatal("Invalid filter specification '%s'\n", optarg);

View file

@ -1230,7 +1230,7 @@ void state_config(struct snapraid_state* state, const char* path, const char* co
/* LCOV_EXCL_STOP */
}
filter = filter_alloc_file(-1, buffer);
filter = filter_alloc_file(-1, "", buffer);
if (!filter) {
/* LCOV_EXCL_START */
log_fatal("Invalid 'exclude' specification '%s' in '%s' at line %u\n", buffer, path, line);
@ -1257,7 +1257,7 @@ void state_config(struct snapraid_state* state, const char* path, const char* co
/* LCOV_EXCL_STOP */
}
filter = filter_alloc_file(1, buffer);
filter = filter_alloc_file(1, "", buffer);
if (!filter) {
/* LCOV_EXCL_START */
log_fatal("Invalid 'include' specification '%s' in '%s' at line %u\n", buffer, path, line);
@ -5113,3 +5113,71 @@ int state_flush(struct snapraid_state* state, struct snapraid_io* io, struct sna
return 0;
}
void state_load_ignore_file(tommy_list* filter_list, const char* path, const char* sub)
{
STREAM* f;
int line;
f = sopen_read(path, 0);
if (!f) {
/* LCOV_EXCL_START */
log_error("Error opening the ignore file '%s'. %s.\n", path, strerror(errno));
/* LCOV_EXCL_STOP */
}
line = 1;
while (1) {
char buffer[PATH_MAX];
int ret;
int c;
/* skip initial spaces */
sgetspace(f);
/* get the whole line */
ret = sgetline(f, buffer, sizeof(buffer));
if (ret < 0) {
/* LCOV_EXCL_START */
log_error("Too long line in '%s' at line %u\n", path, line);
/* LCOV_EXCL_STOP */
}
if (buffer[0] == 0) {
/* allow empty lines */
} else if (buffer[0] == '#') {
/* ignore comment lines */
} else {
struct snapraid_filter* filter;
filter = filter_alloc_file(-1, sub, buffer);
if (!filter) {
/* LCOV_EXCL_START */
log_error("Invalid ignore specification '%s' in '%s' at line %u\n", buffer, path, line);
/* LCOV_EXCL_STOP */
}
tommy_list_insert_tail(filter_list, &filter->node, filter);
}
/* next line */
c = sgeteol(f);
if (c == EOF) {
break;
}
if (c != '\n') {
/* LCOV_EXCL_START */
log_error("Extra data in '%s' at line %u\n", path, line);
/* LCOV_EXCL_STOP */
}
++line;
}
if (serror(f)) {
/* LCOV_EXCL_START */
log_error("Error reading the ignore file '%s' at line %u\n", path, line);
/* LCOV_EXCL_STOP */
}
sclose(f);
}

View file

@ -403,6 +403,11 @@ void state_thermal_cooldown(struct snapraid_state* state);
*/
int state_thermal_begin(struct snapraid_state* state, time_t now);
/**
* Load the ignore file .snapraidignore
*/
void state_load_ignore_file(tommy_list* filter_list, const char* path, const char* sub);
/****************************************************************************/
/* misc */

View file

@ -911,8 +911,8 @@ void pathimport(char* dst, size_t size, const char* src)
pathcpy(dst, size, src);
#ifdef _WIN32
/* convert the Windows dir separator '\' to C '/', */
/* and the Windows escaping char '^' to the fnmatch '\' */
/* convert the Windows dir separator '\' to C '/', */
/* and the Windows escaping char '^' to the fnmatch '\' */
while (*dst) {
switch (*dst) {
case '\\' :
@ -1013,6 +1013,30 @@ int pathcmp(const char* a, const char* b)
#endif
}
int path_is_root_of(const char* root, const char* path)
{
while (*root && *path) {
#ifdef _WIN32
/* case insensitive compare in Windows */
char r = tolower((unsigned char)*root);
char p = tolower((unsigned char)*path);
if (r == '\\')
r = '/';
if (p == '\\')
p = '/';
if (r != p)
return 0;
#else
if (*root != *path)
return 0;
#endif
++root;
++path;
}
return *root == 0 && *path != 0;
}
/****************************************************************************/
/* file-system */

View file

@ -352,6 +352,15 @@ void pathcut(char* dst);
*/
int pathcmp(const char* a, const char* b);
/**
* Check if the root is matching the start of the specified path.
*
* The root MUST end with a /.
*
* In Windows it's case insensitive and assumes \ equal at /.
*/
int path_is_root_of(const char* root, const char* path);
/****************************************************************************/
/* file-system */