2015-09-27 17:13:20 +02:00
|
|
|
// rsync style glob parser
|
|
|
|
|
2018-01-12 17:30:54 +01:00
|
|
|
package filter
|
2015-09-27 17:13:20 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"regexp"
|
|
|
|
"strings"
|
2016-06-12 16:06:02 +02:00
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
2015-09-27 17:13:20 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// globToRegexp converts an rsync style glob to a regexp
|
|
|
|
//
|
|
|
|
// documented in filtering.md
|
|
|
|
func globToRegexp(glob string) (*regexp.Regexp, error) {
|
|
|
|
var re bytes.Buffer
|
|
|
|
if strings.HasPrefix(glob, "/") {
|
|
|
|
glob = glob[1:]
|
|
|
|
_, _ = re.WriteRune('^')
|
|
|
|
} else {
|
|
|
|
_, _ = re.WriteString("(^|/)")
|
|
|
|
}
|
|
|
|
consecutiveStars := 0
|
|
|
|
insertStars := func() error {
|
|
|
|
if consecutiveStars > 0 {
|
|
|
|
switch consecutiveStars {
|
|
|
|
case 1:
|
|
|
|
_, _ = re.WriteString(`[^/]*`)
|
|
|
|
case 2:
|
|
|
|
_, _ = re.WriteString(`.*`)
|
|
|
|
default:
|
2016-06-12 16:06:02 +02:00
|
|
|
return errors.Errorf("too many stars in %q", glob)
|
2015-09-27 17:13:20 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
consecutiveStars = 0
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
inBraces := false
|
|
|
|
inBrackets := 0
|
|
|
|
slashed := false
|
|
|
|
for _, c := range glob {
|
|
|
|
if slashed {
|
|
|
|
_, _ = re.WriteRune(c)
|
|
|
|
slashed = false
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if c != '*' {
|
|
|
|
err := insertStars()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if inBrackets > 0 {
|
|
|
|
_, _ = re.WriteRune(c)
|
|
|
|
if c == '[' {
|
|
|
|
inBrackets++
|
|
|
|
}
|
|
|
|
if c == ']' {
|
|
|
|
inBrackets--
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
switch c {
|
|
|
|
case '\\':
|
|
|
|
_, _ = re.WriteRune(c)
|
|
|
|
slashed = true
|
|
|
|
case '*':
|
|
|
|
consecutiveStars++
|
|
|
|
case '?':
|
|
|
|
_, _ = re.WriteString(`[^/]`)
|
|
|
|
case '[':
|
|
|
|
_, _ = re.WriteRune(c)
|
|
|
|
inBrackets++
|
|
|
|
case ']':
|
2016-06-12 16:06:02 +02:00
|
|
|
return nil, errors.Errorf("mismatched ']' in glob %q", glob)
|
2015-09-27 17:13:20 +02:00
|
|
|
case '{':
|
|
|
|
if inBraces {
|
2016-06-12 16:06:02 +02:00
|
|
|
return nil, errors.Errorf("can't nest '{' '}' in glob %q", glob)
|
2015-09-27 17:13:20 +02:00
|
|
|
}
|
|
|
|
inBraces = true
|
|
|
|
_, _ = re.WriteRune('(')
|
|
|
|
case '}':
|
|
|
|
if !inBraces {
|
2016-06-12 16:06:02 +02:00
|
|
|
return nil, errors.Errorf("mismatched '{' and '}' in glob %q", glob)
|
2015-09-27 17:13:20 +02:00
|
|
|
}
|
|
|
|
_, _ = re.WriteRune(')')
|
|
|
|
inBraces = false
|
|
|
|
case ',':
|
|
|
|
if inBraces {
|
|
|
|
_, _ = re.WriteRune('|')
|
|
|
|
} else {
|
|
|
|
_, _ = re.WriteRune(c)
|
|
|
|
}
|
|
|
|
case '.', '+', '(', ')', '|', '^', '$': // regexp meta characters not dealt with above
|
|
|
|
_, _ = re.WriteRune('\\')
|
|
|
|
_, _ = re.WriteRune(c)
|
|
|
|
default:
|
|
|
|
_, _ = re.WriteRune(c)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
err := insertStars()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if inBrackets > 0 {
|
2016-06-12 16:06:02 +02:00
|
|
|
return nil, errors.Errorf("mismatched '[' and ']' in glob %q", glob)
|
2015-09-27 17:13:20 +02:00
|
|
|
}
|
|
|
|
if inBraces {
|
2016-06-12 16:06:02 +02:00
|
|
|
return nil, errors.Errorf("mismatched '{' and '}' in glob %q", glob)
|
2015-09-27 17:13:20 +02:00
|
|
|
}
|
|
|
|
_, _ = re.WriteRune('$')
|
|
|
|
result, err := regexp.Compile(re.String())
|
|
|
|
if err != nil {
|
2016-06-12 16:06:02 +02:00
|
|
|
return nil, errors.Wrapf(err, "bad glob pattern %q (regexp %q)", glob, re.String())
|
2015-09-27 17:13:20 +02:00
|
|
|
}
|
|
|
|
return result, nil
|
|
|
|
}
|
2016-05-16 18:14:04 +02:00
|
|
|
|
|
|
|
var (
|
|
|
|
// Can't deal with / or ** in {}
|
|
|
|
tooHardRe = regexp.MustCompile(`{[^{}]*(\*\*|/)[^{}]*}`)
|
|
|
|
|
|
|
|
// Squash all /
|
|
|
|
squashSlash = regexp.MustCompile(`/{2,}`)
|
|
|
|
)
|
|
|
|
|
|
|
|
// globToDirGlobs takes a file glob and turns it into a series of
|
|
|
|
// directory globs. When matched with a directory (with a trailing /)
|
|
|
|
// this should answer the question as to whether this glob could be in
|
|
|
|
// this directory.
|
|
|
|
func globToDirGlobs(glob string) (out []string) {
|
|
|
|
if tooHardRe.MatchString(glob) {
|
|
|
|
// Can't figure this one out so return any directory might match
|
|
|
|
out = append(out, "/**")
|
|
|
|
return out
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get rid of multiple /s
|
|
|
|
glob = squashSlash.ReplaceAllString(glob, "/")
|
|
|
|
|
|
|
|
// Split on / or **
|
|
|
|
// (** can contain /)
|
|
|
|
for {
|
|
|
|
i := strings.LastIndex(glob, "/")
|
|
|
|
j := strings.LastIndex(glob, "**")
|
|
|
|
what := ""
|
|
|
|
if j > i {
|
|
|
|
i = j
|
|
|
|
what = "**"
|
|
|
|
}
|
|
|
|
if i < 0 {
|
|
|
|
if len(out) == 0 {
|
|
|
|
out = append(out, "/**")
|
|
|
|
}
|
|
|
|
break
|
|
|
|
}
|
|
|
|
glob = glob[:i]
|
|
|
|
newGlob := glob + what + "/"
|
|
|
|
if len(out) == 0 || out[len(out)-1] != newGlob {
|
|
|
|
out = append(out, newGlob)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return out
|
|
|
|
}
|