Skip to content

"rsync for cloud storage" - Google Drive, S3, Dropbox, Backblaze B2, One Drive, Swift, Hubic, Wasabi, Google Cloud Storage, Azure Blob, Azure Files, Yandex Files

filter: make [!...] in globs a negated character class

kevin9327c434df94d1c3
diff --git a/fs/filter/filter_test.go b/fs/filter/filter_test.go
index 64d20bfbe..cb2a9c139 100644
--- a/fs/filter/filter_test.go
+++ b/fs/filter/filter_test.go
@@ -626,6 +626,25 @@ func TestNewFilterMatchesRegexp(t *testing.T) {
assert.False(t, f.InActive())
}
+func TestNewFilterMatchesNegatedCharacterClass(t *testing.T) {
+ f, err := NewFilter(nil)
+ require.NoError(t, err)
+ add := func(s string) {
+ err := f.AddRule(s)
+ require.NoError(t, err)
+ }
+ add("+ file[!a].txt")
+ add("- *")
+ testInclude(t, f, []includeTest{
+ {"fileb.txt", 100, 0, true},
+ {"sub/fileb.txt", 100, 0, true},
+ {"filea.txt", 100, 0, false},
+ {"file!.txt", 100, 0, true},
+ {"file/.txt", 100, 0, false},
+ })
+ assert.False(t, f.InActive())
+}
+
func TestNewFilterHashFilter(t *testing.T) {
const e1 = "filé1.jpg" // one of the unicode E characters
const e2 = "filé1.jpg" // a different unicode E character
diff --git a/fs/filter/glob.go b/fs/filter/glob.go
index ac93f4c7d..d7b214076 100644
--- a/fs/filter/glob.go
+++ b/fs/filter/glob.go
@@ -85,6 +85,8 @@ func globToRegexp(glob string, pathMode bool, addAnchors bool, ignoreCase bool)
}
braceDepth := 0
inBrackets := 0
+ bracketStart := false // just opened a character class
+ bracketNegated := false // just read the ! of a [! character class
slashed := false
inRegexp := false // inside {{ ... }}
inRegexpEnd := false // have received }} waiting for more
@@ -125,6 +127,23 @@ func globToRegexp(glob string, pathMode bool, addAnchors bool, ignoreCase bool)
}
}
if inBrackets > 0 {
+ if bracketStart && c == '!' {
+ // [!...] is a negated character class in glob
+ // syntax, which is [^...] in a regexp. Like ?, it
+ // does not match the / separator in path mode.
+ bracketStart = false
+ bracketNegated = true
+ _ = re.WriteByte('^')
+ if pathMode {
+ _ = re.WriteByte('/')
+ }
+ continue
+ }
+ if bracketNegated && c == ']' {
+ return nil, fmt.Errorf("empty character class in glob %q", glob)
+ }
+ bracketStart = false
+ bracketNegated = false
_, _ = re.WriteRune(c)
if c == '[' {
inBrackets++
@@ -149,6 +168,7 @@ func globToRegexp(glob string, pathMode bool, addAnchors bool, ignoreCase bool)
case '[':
_, _ = re.WriteRune(c)
inBrackets++
+ bracketStart = true
case ']':
return nil, fmt.Errorf("mismatched ']' in glob %q", glob)
case '{':
diff --git a/fs/filter/glob_test.go b/fs/filter/glob_test.go
index f1b8e7dc9..9ccc6cdf2 100644
--- a/fs/filter/glob_test.go
+++ b/fs/filter/glob_test.go
@@ -21,6 +21,10 @@ func TestGlobStringToRegexp(t *testing.T) {
{`potat[oa]`, `potat[oa]`, ``},
{`potat[a-z]or`, `potat[a-z]or`, ``},
{`potat[[:alpha:]]or`, `potat[[:alpha:]]or`, ``},
+ {`potat[!oa]`, `potat[^oa]`, ``},
+ {`potat[![:alpha:]]or`, `potat[^[:alpha:]]or`, ``},
+ {`potat[a!]`, `potat[a!]`, ``},
+ {`potat[!]`, ``, `empty character class`},
{`'.' '+' '(' ')' '|' '^' '$'`, `'\.' '\+' '\(' '\)' '\|' '\^' '\$'`, ``},
{`*.jpg`, `.*\.jpg`, ``},
{`a{b,c,d}e`, `a(b|c|d)e`, ``},
@@ -91,6 +95,10 @@ func TestGlobPathToRegexp(t *testing.T) {
{`potat[oa]`, `(^|/)potat[oa]$`, ``},
{`potat[a-z]or`, `(^|/)potat[a-z]or$`, ``},
{`potat[[:alpha:]]or`, `(^|/)potat[[:alpha:]]or$`, ``},
+ {`potat[!oa]`, `(^|/)potat[^/oa]$`, ``},
+ {`potat[![:alpha:]]or`, `(^|/)potat[^/[:alpha:]]or$`, ``},
+ {`potat[a!]`, `(^|/)potat[a!]$`, ``},
+ {`potat[!]`, ``, `empty character class`},
{`'.' '+' '(' ')' '|' '^' '$'`, `(^|/)'\.' '\+' '\(' '\)' '\|' '\^' '\$'$`, ``},
{`*.jpg`, `(^|/)[^/]*\.jpg$`, ``},
{`a{b,c,d}e`, `(^|/)a(b|c|d)e$`, ``},