From 8897377a541428b4a3f3be4e61d4d97b90fbfdaa Mon Sep 17 00:00:00 2001 From: Nick Craig-Wood Date: Tue, 27 Oct 2020 13:08:20 +0000 Subject: [PATCH] filter: Make `--exclude "dir/"` equivalent to `--exclude "dir/**"` Rclone uses directory exclusions to cut down the listing it has to do, so before this fix `--exclude dir/` would make sure nothing in `dir/` was scanned, **except** if --fast-list was used, in which case only the directory was excluded and everything within it was included. This is rather unexpected, so this patch makes `--exclude dir/` be equivalent to `--exclude dir/**`, meaning that excluding a directory excludes it and its contents. We can't do the same for --include without changing the semantics of filtering slightly. Fixes #3375 --- fs/filter/filter.go | 4 ++++ fs/filter/filter_test.go | 15 +++++++++++++++ 2 files changed, 19 insertions(+) diff --git a/fs/filter/filter.go b/fs/filter/filter.go index 1714dabf5..304ab87f8 100644 --- a/fs/filter/filter.go +++ b/fs/filter/filter.go @@ -267,6 +267,10 @@ func (f *Filter) addDirGlobs(Include bool, glob string) error { func (f *Filter) Add(Include bool, glob string) error { isDirRule := strings.HasSuffix(glob, "/") isFileRule := !isDirRule + // Make excluding "dir/" equivalent to excluding "dir/**" + if isDirRule && !Include { + glob += "**" + } if strings.Contains(glob, "**") { isDirRule, isFileRule = true, true } diff --git a/fs/filter/filter_test.go b/fs/filter/filter_test.go index 1fc4ef671..3d5439fe2 100644 --- a/fs/filter/filter_test.go +++ b/fs/filter/filter_test.go @@ -523,6 +523,21 @@ func TestFilterAddDirRuleOrFileRule(t *testing.T) { + (^|/)potato$ --- Directory filter rules --- + ^.*$`, + }, + { + false, + "potato/", + `--- File filter rules --- +- (^|/)potato/.*$ +--- Directory filter rules --- +- (^|/)potato/.*$`, + }, + { + true, + "potato/", + `--- File filter rules --- +--- Directory filter rules --- ++ (^|/)potato/$`, }, { false,