mirror of
https://github.com/amadvance/snapraid.git
synced 2026-11-21 07:26:29 -05:00
When excluding files print also the rule excluding them
This commit is contained in:
parent
a435fabf90
commit
83e1629868
5 changed files with 80 additions and 33 deletions
|
|
@ -125,8 +125,36 @@ void filter_free(struct snapraid_filter* filter)
|
|||
free(filter);
|
||||
}
|
||||
|
||||
static int filter_apply(struct snapraid_filter* filter, const char* path, const char* name, int is_dir)
|
||||
const char* filter_type(struct snapraid_filter* filter)
|
||||
{
|
||||
if (filter->direction < 0) {
|
||||
if (filter->is_disk)
|
||||
return "exclude (disk)";
|
||||
if (filter->is_path)
|
||||
return "exclude (path)";
|
||||
if (filter->is_dir)
|
||||
return "exclude (dir)";
|
||||
return "exclude (file)";
|
||||
} else {
|
||||
if (filter->is_disk)
|
||||
return "include (disk)";
|
||||
if (filter->is_path)
|
||||
return "include (path)";
|
||||
if (filter->is_dir)
|
||||
return "include (dir)";
|
||||
return "include (file)";
|
||||
}
|
||||
}
|
||||
|
||||
const char* filter_pattern(struct snapraid_filter* filter)
|
||||
{
|
||||
return filter->pattern;
|
||||
}
|
||||
|
||||
static int filter_apply(struct snapraid_filter* filter, struct snapraid_filter** reason, const char* path, const char* name, int is_dir)
|
||||
{
|
||||
int ret = 0;
|
||||
|
||||
/* matches dirs with dirs and files with files */
|
||||
if (filter->is_dir && !is_dir)
|
||||
return 0;
|
||||
|
|
@ -136,16 +164,19 @@ static int filter_apply(struct snapraid_filter* filter, const char* path, const
|
|||
if (filter->is_path) {
|
||||
/* skip initial slash, as always missing from the path */
|
||||
if (fnmatch(filter->pattern + 1, path, FNM_PATHNAME | FNM_CASEINSENSITIVE_FOR_WIN) == 0)
|
||||
return filter->direction;
|
||||
ret = filter->direction;
|
||||
} else {
|
||||
if (fnmatch(filter->pattern, name, FNM_CASEINSENSITIVE_FOR_WIN) == 0)
|
||||
return filter->direction;
|
||||
ret = filter->direction;
|
||||
}
|
||||
|
||||
return 0;
|
||||
if (reason != 0 && ret < 0)
|
||||
*reason = filter;
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
static int filter_recurse(struct snapraid_filter* filter, const char* const_path, int is_dir)
|
||||
static int filter_recurse(struct snapraid_filter* filter, struct snapraid_filter** reason, const char* const_path, int is_dir)
|
||||
{
|
||||
char path[PATH_MAX];
|
||||
char* name;
|
||||
|
|
@ -161,7 +192,7 @@ static int filter_recurse(struct snapraid_filter* filter, const char* const_path
|
|||
path[i] = 0;
|
||||
|
||||
/* filter the directory */
|
||||
if (filter_apply(filter, path, name, 1) != 0)
|
||||
if (filter_apply(filter, reason, path, name, 1) != 0)
|
||||
return filter->direction;
|
||||
|
||||
/* restore the slash */
|
||||
|
|
@ -173,13 +204,13 @@ static int filter_recurse(struct snapraid_filter* filter, const char* const_path
|
|||
}
|
||||
|
||||
/* filter the final file */
|
||||
if (filter_apply(filter, path, name, is_dir) != 0)
|
||||
if (filter_apply(filter, reason, path, name, is_dir) != 0)
|
||||
return filter->direction;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int filter_element(tommy_list* filterlist, const char* disk, const char* sub, int is_dir)
|
||||
static int filter_element(tommy_list* filterlist, struct snapraid_filter** reason, const char* disk, const char* sub, int is_dir)
|
||||
{
|
||||
tommy_node* i;
|
||||
|
||||
|
|
@ -195,8 +226,10 @@ static int filter_element(tommy_list* filterlist, const char* disk, const char*
|
|||
ret = filter->direction;
|
||||
else
|
||||
ret = 0;
|
||||
if (reason != 0 && ret < 0)
|
||||
*reason = filter;
|
||||
} else {
|
||||
ret = filter_recurse(filter, sub, is_dir);
|
||||
ret = filter_recurse(filter, reason, sub, is_dir);
|
||||
}
|
||||
|
||||
if (ret > 0) {
|
||||
|
|
@ -208,6 +241,8 @@ static int filter_element(tommy_list* filterlist, const char* disk, const char*
|
|||
} else {
|
||||
/* default is opposite of the last filter */
|
||||
direction = -filter->direction;
|
||||
if (reason != 0 && direction < 0)
|
||||
*reason = filter;
|
||||
/* continue with the next one */
|
||||
}
|
||||
}
|
||||
|
|
@ -224,14 +259,14 @@ static int filter_element(tommy_list* filterlist, const char* disk, const char*
|
|||
return 0;
|
||||
}
|
||||
|
||||
int filter_path(tommy_list* filterlist, const char* disk, const char* sub)
|
||||
int filter_path(tommy_list* filterlist, struct snapraid_filter** reason, const char* disk, const char* sub)
|
||||
{
|
||||
return filter_element(filterlist, disk, sub, 0);
|
||||
return filter_element(filterlist, reason, disk, sub, 0);
|
||||
}
|
||||
|
||||
int filter_dir(tommy_list* filterlist, const char* disk, const char* sub)
|
||||
int filter_dir(tommy_list* filterlist, struct snapraid_filter** reason, const char* disk, const char* sub)
|
||||
{
|
||||
return filter_element(filterlist, disk, sub, 1);
|
||||
return filter_element(filterlist, reason, disk, sub, 1);
|
||||
}
|
||||
|
||||
int filter_existence(int filter_missing, const char* dir, const char* sub)
|
||||
|
|
|
|||
|
|
@ -426,6 +426,16 @@ struct snapraid_filter* filter_alloc_disk(int is_include, const char* pattern);
|
|||
*/
|
||||
void filter_free(struct snapraid_filter* filter);
|
||||
|
||||
/**
|
||||
* Filter type description.
|
||||
*/
|
||||
const char* filter_type(struct snapraid_filter* filter);
|
||||
|
||||
/**
|
||||
* Filter patter description.
|
||||
*/
|
||||
const char* filter_pattern(struct snapraid_filter* filter);
|
||||
|
||||
/**
|
||||
* Filter hidden files.
|
||||
* Return !=0 if it matches and it should be excluded.
|
||||
|
|
@ -444,7 +454,7 @@ static inline int filter_hidden(int enable, struct dirent* dd)
|
|||
* For each element of the path all the filters are applied, until the first one that matches.
|
||||
* Return !=0 if it should be excluded.
|
||||
*/
|
||||
int filter_path(tommy_list* filterlist, const char* disk, const char* sub);
|
||||
int filter_path(tommy_list* filterlist, struct snapraid_filter** reason, const char* disk, const char* sub);
|
||||
|
||||
/**
|
||||
* Filter a file/link/dir if missing.
|
||||
|
|
@ -464,7 +474,7 @@ int filter_correctness(int filter_error, tommy_arrayblkof* infoarr, struct snapr
|
|||
* For each element of the path all the filters are applied, until the first one that matches.
|
||||
* Return !=0 if should be excluded.
|
||||
*/
|
||||
int filter_dir(tommy_list* filterlist, const char* disk, const char* sub);
|
||||
int filter_dir(tommy_list* filterlist, struct snapraid_filter** reason, const char* disk, const char* sub);
|
||||
|
||||
/**
|
||||
* Filters a path if it's a content file.
|
||||
|
|
|
|||
|
|
@ -1180,6 +1180,7 @@ static int scan_dir(struct snapraid_scan* scan, int is_diff, const char* dir, co
|
|||
while (node != 0) {
|
||||
char path_next[PATH_MAX];
|
||||
char sub_next[PATH_MAX];
|
||||
struct snapraid_filter* reason = 0;
|
||||
struct dirent_sorted* dd = node->data;
|
||||
const char* name = dd->d_name;
|
||||
struct stat* st;
|
||||
|
|
@ -1238,7 +1239,7 @@ static int scan_dir(struct snapraid_scan* scan, int is_diff, const char* dir, co
|
|||
}
|
||||
|
||||
if (type == 0) { /* REG */
|
||||
if (filter_path(&state->filterlist, disk->name, sub_next) == 0) {
|
||||
if (filter_path(&state->filterlist, &reason, disk->name, sub_next) == 0) {
|
||||
|
||||
/* late stat, if not yet called */
|
||||
if (!st)
|
||||
|
|
@ -1261,11 +1262,11 @@ static int scan_dir(struct snapraid_scan* scan, int is_diff, const char* dir, co
|
|||
processed = 1;
|
||||
} else {
|
||||
if (state->opt.verbose) {
|
||||
printf("Excluding file '%s'\n", path_next);
|
||||
printf("Excluding file '%s' for rule '%s %s'\n", path_next, filter_type(reason), filter_pattern(reason));
|
||||
}
|
||||
}
|
||||
} else if (type == 1) { /* LNK */
|
||||
if (filter_path(&state->filterlist, disk->name, sub_next) == 0) {
|
||||
if (filter_path(&state->filterlist, &reason, disk->name, sub_next) == 0) {
|
||||
char subnew[PATH_MAX];
|
||||
int ret;
|
||||
|
||||
|
|
@ -1291,11 +1292,11 @@ static int scan_dir(struct snapraid_scan* scan, int is_diff, const char* dir, co
|
|||
processed = 1;
|
||||
} else {
|
||||
if (state->opt.verbose) {
|
||||
printf("Excluding link '%s'\n", path_next);
|
||||
printf("Excluding link '%s' for rule '%s %s'\n", path_next, filter_type(reason), filter_pattern(reason));
|
||||
}
|
||||
}
|
||||
} else if (type == 2) { /* DIR */
|
||||
if (filter_dir(&state->filterlist, disk->name, sub_next) == 0) {
|
||||
if (filter_dir(&state->filterlist, &reason, disk->name, sub_next) == 0) {
|
||||
#ifndef _WIN32
|
||||
/* late stat, if not yet called */
|
||||
if (!st)
|
||||
|
|
@ -1323,11 +1324,11 @@ static int scan_dir(struct snapraid_scan* scan, int is_diff, const char* dir, co
|
|||
}
|
||||
} else {
|
||||
if (state->opt.verbose) {
|
||||
printf("Excluding directory '%s'\n", path_next);
|
||||
printf("Excluding directory '%s' for rule '%s %s'\n", path_next, filter_type(reason), filter_pattern(reason));
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if (filter_path(&state->filterlist, disk->name, sub_next) == 0) {
|
||||
if (filter_path(&state->filterlist, &reason, disk->name, sub_next) == 0) {
|
||||
/* late stat, if not yet called */
|
||||
if (!st)
|
||||
st = DSTAT(path_next, dd, &st_buf);
|
||||
|
|
@ -1335,7 +1336,7 @@ static int scan_dir(struct snapraid_scan* scan, int is_diff, const char* dir, co
|
|||
fprintf(stderr, "WARNING! Ignoring special '%s' file '%s'\n", stat_desc(st), path_next);
|
||||
} else {
|
||||
if (state->opt.verbose) {
|
||||
printf("Excluding special file '%s'\n", path_next);
|
||||
printf("Excluding special file '%s' for rule '%s %s'\n", path_next, filter_type(reason), filter_pattern(reason));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -165,6 +165,7 @@ static void search_dir(struct snapraid_state* state, struct snapraid_disk* disk,
|
|||
while (1) {
|
||||
char path_next[PATH_MAX];
|
||||
char sub_next[PATH_MAX];
|
||||
struct snapraid_filter* reason = 0;
|
||||
struct stat st;
|
||||
const char* name;
|
||||
struct dirent* dd;
|
||||
|
|
@ -234,21 +235,21 @@ static void search_dir(struct snapraid_state* state, struct snapraid_disk* disk,
|
|||
#endif
|
||||
|
||||
if (S_ISREG(st.st_mode)) {
|
||||
if (disk == 0 || filter_path(&state->filterlist, disk->name, sub_next) == 0) {
|
||||
if (disk == 0 || filter_path(&state->filterlist, &reason, disk->name, sub_next) == 0) {
|
||||
search_file(state, path_next, st.st_size, st.st_mtime, STAT_NSEC(&st));
|
||||
} else {
|
||||
if (state->opt.verbose) {
|
||||
printf("Excluding link '%s'\n", path_next);
|
||||
printf("Excluding link '%s' for rule '%s %s'\n", path_next, filter_type(reason), filter_pattern(reason));
|
||||
}
|
||||
}
|
||||
} else if (S_ISDIR(st.st_mode)) {
|
||||
if (disk == 0 || filter_dir(&state->filterlist, disk->name, sub_next) == 0) {
|
||||
if (disk == 0 || filter_dir(&state->filterlist, &reason, disk->name, sub_next) == 0) {
|
||||
pathslash(path_next, sizeof(path_next));
|
||||
pathslash(sub_next, sizeof(sub_next));
|
||||
search_dir(state, disk, path_next, sub_next);
|
||||
} else {
|
||||
if (state->opt.verbose) {
|
||||
printf("Excluding directory '%s'\n", path_next);
|
||||
printf("Excluding directory '%s' for rule '%s %s'\n", path_next, filter_type(reason), filter_pattern(reason));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -4554,8 +4554,8 @@ void state_filter(struct snapraid_state* state, tommy_list* filterlist_file, tom
|
|||
for (j = tommy_list_head(&disk->filelist); j != 0; j = j->next) {
|
||||
struct snapraid_file* file = j->data;
|
||||
|
||||
if (filter_path(filterlist_disk, disk->name, file->sub) != 0
|
||||
|| filter_path(filterlist_file, disk->name, file->sub) != 0
|
||||
if (filter_path(filterlist_disk, 0, disk->name, file->sub) != 0
|
||||
|| filter_path(filterlist_file, 0, disk->name, file->sub) != 0
|
||||
|| filter_existence(filter_missing, disk->dir, file->sub) != 0
|
||||
|| filter_correctness(filter_error, &state->infoarr, file) != 0
|
||||
) {
|
||||
|
|
@ -4567,8 +4567,8 @@ void state_filter(struct snapraid_state* state, tommy_list* filterlist_file, tom
|
|||
for (j = tommy_list_head(&disk->linklist); j != 0; j = j->next) {
|
||||
struct snapraid_link* link = j->data;
|
||||
|
||||
if (filter_path(filterlist_disk, disk->name, link->sub) != 0
|
||||
|| filter_path(filterlist_file, disk->name, link->sub) != 0
|
||||
if (filter_path(filterlist_disk, 0, disk->name, link->sub) != 0
|
||||
|| filter_path(filterlist_file, 0, disk->name, link->sub) != 0
|
||||
|| filter_existence(filter_missing, disk->dir, link->sub) != 0
|
||||
) {
|
||||
link_flag_set(link, FILE_IS_EXCLUDED);
|
||||
|
|
@ -4579,8 +4579,8 @@ void state_filter(struct snapraid_state* state, tommy_list* filterlist_file, tom
|
|||
for (j = tommy_list_head(&disk->dirlist); j != 0; j = j->next) {
|
||||
struct snapraid_dir* dir = j->data;
|
||||
|
||||
if (filter_dir(filterlist_disk, disk->name, dir->sub) != 0
|
||||
|| filter_dir(filterlist_file, disk->name, dir->sub) != 0
|
||||
if (filter_dir(filterlist_disk, 0, disk->name, dir->sub) != 0
|
||||
|| filter_dir(filterlist_file, 0, disk->name, dir->sub) != 0
|
||||
|| filter_existence(filter_missing, disk->dir, dir->sub) != 0
|
||||
) {
|
||||
dir_flag_set(dir, FILE_IS_EXCLUDED);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue