2015-07-13 20:05:21 +00:00
|
|
|
package filter
|
|
|
|
|
|
|
|
import (
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
2016-08-21 15:46:23 +00:00
|
|
|
|
2017-07-23 12:21:03 +00:00
|
|
|
"github.com/restic/restic/internal/errors"
|
2015-07-13 20:05:21 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// ErrBadString is returned when Match is called with the empty string as the
|
|
|
|
// second argument.
|
|
|
|
var ErrBadString = errors.New("filter.Match: string is empty")
|
|
|
|
|
2020-10-07 18:55:43 +00:00
|
|
|
type patternPart struct {
|
2020-10-08 09:00:25 +00:00
|
|
|
pattern string // First is "/" for absolute pattern; "" for "**".
|
2020-10-07 18:55:43 +00:00
|
|
|
isSimple bool
|
|
|
|
}
|
|
|
|
|
2020-10-07 12:39:51 +00:00
|
|
|
// Pattern represents a preparsed filter pattern
|
2021-09-17 21:01:58 +00:00
|
|
|
type Pattern struct {
|
2022-05-07 18:51:52 +00:00
|
|
|
original string
|
2019-07-02 19:36:23 +00:00
|
|
|
parts []patternPart
|
|
|
|
isNegated bool
|
2021-09-17 21:01:58 +00:00
|
|
|
}
|
2020-10-07 12:27:59 +00:00
|
|
|
|
|
|
|
func prepareStr(str string) ([]string, error) {
|
|
|
|
if str == "" {
|
|
|
|
return nil, ErrBadString
|
|
|
|
}
|
2020-10-08 09:00:25 +00:00
|
|
|
return splitPath(str), nil
|
2020-10-07 12:27:59 +00:00
|
|
|
}
|
|
|
|
|
2021-09-17 21:04:37 +00:00
|
|
|
func preparePattern(patternStr string) Pattern {
|
2019-07-02 19:36:23 +00:00
|
|
|
var negate bool
|
2022-05-07 18:51:52 +00:00
|
|
|
|
|
|
|
originalPattern := patternStr
|
|
|
|
|
2019-07-02 19:36:23 +00:00
|
|
|
if patternStr[0] == '!' {
|
|
|
|
negate = true
|
|
|
|
patternStr = patternStr[1:]
|
|
|
|
}
|
|
|
|
|
2021-09-17 21:04:37 +00:00
|
|
|
pathParts := splitPath(filepath.Clean(patternStr))
|
|
|
|
parts := make([]patternPart, len(pathParts))
|
|
|
|
for i, part := range pathParts {
|
2020-10-07 18:55:43 +00:00
|
|
|
isSimple := !strings.ContainsAny(part, "\\[]*?")
|
2020-10-08 09:00:25 +00:00
|
|
|
// Replace "**" with the empty string to get faster comparisons
|
|
|
|
// (length-check only) in hasDoubleWildcard.
|
|
|
|
if part == "**" {
|
|
|
|
part = ""
|
|
|
|
}
|
2021-09-17 21:04:37 +00:00
|
|
|
parts[i] = patternPart{part, isSimple}
|
2020-10-07 18:55:43 +00:00
|
|
|
}
|
|
|
|
|
2022-05-07 18:51:52 +00:00
|
|
|
return Pattern{originalPattern, parts, negate}
|
2020-10-07 12:27:59 +00:00
|
|
|
}
|
|
|
|
|
2020-10-08 09:00:25 +00:00
|
|
|
// Split p into path components. Assuming p has been Cleaned, no component
|
|
|
|
// will be empty. For absolute paths, the first component is "/".
|
|
|
|
func splitPath(p string) []string {
|
|
|
|
parts := strings.Split(filepath.ToSlash(p), "/")
|
|
|
|
if parts[0] == "" {
|
|
|
|
parts[0] = "/"
|
|
|
|
}
|
|
|
|
return parts
|
|
|
|
}
|
|
|
|
|
2015-07-13 20:05:21 +00:00
|
|
|
// Match returns true if str matches the pattern. When the pattern is
|
|
|
|
// malformed, filepath.ErrBadPattern is returned. The empty pattern matches
|
|
|
|
// everything, when str is the empty string ErrBadString is returned.
|
|
|
|
//
|
|
|
|
// Pattern can be a combination of patterns suitable for filepath.Match, joined
|
|
|
|
// by filepath.Separator.
|
2017-09-04 21:38:48 +00:00
|
|
|
//
|
|
|
|
// In addition patterns suitable for filepath.Match, pattern accepts a
|
|
|
|
// recursive wildcard '**', which greedily matches an arbitrary number of
|
|
|
|
// intermediate directories.
|
2021-09-17 21:04:37 +00:00
|
|
|
func Match(patternStr, str string) (matched bool, err error) {
|
|
|
|
if patternStr == "" {
|
2015-07-13 20:05:21 +00:00
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
2021-09-17 21:04:37 +00:00
|
|
|
pattern := preparePattern(patternStr)
|
2020-10-07 12:27:59 +00:00
|
|
|
strs, err := prepareStr(str)
|
2016-04-17 19:54:12 +00:00
|
|
|
|
2020-10-07 12:27:59 +00:00
|
|
|
if err != nil {
|
|
|
|
return false, err
|
2015-08-17 09:48:24 +00:00
|
|
|
}
|
|
|
|
|
2021-09-17 21:04:37 +00:00
|
|
|
return match(pattern, strs)
|
2015-07-13 20:05:21 +00:00
|
|
|
}
|
|
|
|
|
2017-06-16 14:46:16 +00:00
|
|
|
// ChildMatch returns true if children of str can match the pattern. When the pattern is
|
|
|
|
// malformed, filepath.ErrBadPattern is returned. The empty pattern matches
|
|
|
|
// everything, when str is the empty string ErrBadString is returned.
|
|
|
|
//
|
|
|
|
// Pattern can be a combination of patterns suitable for filepath.Match, joined
|
|
|
|
// by filepath.Separator.
|
2017-09-04 21:38:48 +00:00
|
|
|
//
|
|
|
|
// In addition patterns suitable for filepath.Match, pattern accepts a
|
|
|
|
// recursive wildcard '**', which greedily matches an arbitrary number of
|
|
|
|
// intermediate directories.
|
2021-09-17 21:04:37 +00:00
|
|
|
func ChildMatch(patternStr, str string) (matched bool, err error) {
|
|
|
|
if patternStr == "" {
|
2017-06-16 14:46:16 +00:00
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
2021-09-17 21:04:37 +00:00
|
|
|
pattern := preparePattern(patternStr)
|
2020-10-07 12:27:59 +00:00
|
|
|
strs, err := prepareStr(str)
|
2017-06-16 14:46:16 +00:00
|
|
|
|
2020-10-07 12:27:59 +00:00
|
|
|
if err != nil {
|
|
|
|
return false, err
|
2017-06-16 14:46:16 +00:00
|
|
|
}
|
|
|
|
|
2021-09-17 21:04:37 +00:00
|
|
|
return childMatch(pattern, strs)
|
2017-06-16 14:46:16 +00:00
|
|
|
}
|
|
|
|
|
2021-09-17 21:04:37 +00:00
|
|
|
func childMatch(pattern Pattern, strs []string) (matched bool, err error) {
|
|
|
|
if pattern.parts[0].pattern != "/" {
|
2017-06-16 14:46:16 +00:00
|
|
|
// relative pattern can always be nested down
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
2021-09-17 21:04:37 +00:00
|
|
|
ok, pos := hasDoubleWildcard(pattern)
|
2018-06-09 21:12:51 +00:00
|
|
|
if ok && len(strs) >= pos {
|
|
|
|
// cut off at the double wildcard
|
|
|
|
strs = strs[:pos]
|
|
|
|
}
|
|
|
|
|
2017-06-16 14:46:16 +00:00
|
|
|
// match path against absolute pattern prefix
|
|
|
|
l := 0
|
2021-09-17 21:04:37 +00:00
|
|
|
if len(strs) > len(pattern.parts) {
|
|
|
|
l = len(pattern.parts)
|
2017-06-16 14:46:16 +00:00
|
|
|
} else {
|
|
|
|
l = len(strs)
|
|
|
|
}
|
2022-05-07 18:51:52 +00:00
|
|
|
return match(Pattern{pattern.original, pattern.parts[0:l], pattern.isNegated}, strs)
|
2017-06-16 14:46:16 +00:00
|
|
|
}
|
|
|
|
|
2020-10-07 12:39:51 +00:00
|
|
|
func hasDoubleWildcard(list Pattern) (ok bool, pos int) {
|
2021-09-17 21:01:58 +00:00
|
|
|
for i, item := range list.parts {
|
2020-10-08 09:00:25 +00:00
|
|
|
if item.pattern == "" {
|
2015-07-13 20:51:35 +00:00
|
|
|
return true, i
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false, 0
|
|
|
|
}
|
|
|
|
|
2021-09-17 21:04:37 +00:00
|
|
|
func match(pattern Pattern, strs []string) (matched bool, err error) {
|
|
|
|
if ok, pos := hasDoubleWildcard(pattern); ok {
|
2015-07-13 20:51:35 +00:00
|
|
|
// gradually expand '**' into separate wildcards
|
2021-09-17 21:01:58 +00:00
|
|
|
newPat := make([]patternPart, len(strs))
|
2020-10-07 13:51:17 +00:00
|
|
|
// copy static prefix once
|
2021-09-17 21:04:37 +00:00
|
|
|
copy(newPat, pattern.parts[:pos])
|
|
|
|
for i := 0; i <= len(strs)-len(pattern.parts)+1; i++ {
|
2020-10-07 13:51:17 +00:00
|
|
|
// limit to static prefix and already appended '*'
|
|
|
|
newPat := newPat[:pos+i]
|
|
|
|
// in the first iteration the wildcard expands to nothing
|
|
|
|
if i > 0 {
|
2020-10-07 18:55:43 +00:00
|
|
|
newPat[pos+i-1] = patternPart{"*", false}
|
2015-07-13 20:51:35 +00:00
|
|
|
}
|
2021-09-17 21:04:37 +00:00
|
|
|
newPat = append(newPat, pattern.parts[pos+1:]...)
|
2015-07-13 20:51:35 +00:00
|
|
|
|
2022-05-07 18:51:52 +00:00
|
|
|
matched, err := match(Pattern{pattern.original, newPat, pattern.isNegated}, strs)
|
2015-07-13 20:51:35 +00:00
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if matched {
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
2021-09-17 21:04:37 +00:00
|
|
|
if len(pattern.parts) == 0 && len(strs) == 0 {
|
2015-07-13 20:05:21 +00:00
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
2021-05-14 20:14:05 +00:00
|
|
|
// an empty pattern never matches a non-empty path
|
2021-09-17 21:04:37 +00:00
|
|
|
if len(pattern.parts) == 0 {
|
2021-05-14 20:14:05 +00:00
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
2021-09-17 21:04:37 +00:00
|
|
|
if len(pattern.parts) <= len(strs) {
|
2020-10-09 14:11:05 +00:00
|
|
|
minOffset := 0
|
2021-09-17 21:04:37 +00:00
|
|
|
maxOffset := len(strs) - len(pattern.parts)
|
2019-07-06 20:12:24 +00:00
|
|
|
// special case absolute patterns
|
2021-09-17 21:04:37 +00:00
|
|
|
if pattern.parts[0].pattern == "/" {
|
2019-07-06 20:12:24 +00:00
|
|
|
maxOffset = 0
|
2020-10-09 14:11:05 +00:00
|
|
|
} else if strs[0] == "/" {
|
|
|
|
// skip absolute path marker if pattern is not rooted
|
|
|
|
minOffset = 1
|
2019-07-06 20:12:24 +00:00
|
|
|
}
|
2015-07-13 20:05:21 +00:00
|
|
|
outer:
|
2020-10-09 14:11:05 +00:00
|
|
|
for offset := maxOffset; offset >= minOffset; offset-- {
|
2015-07-13 20:05:21 +00:00
|
|
|
|
2021-09-17 21:04:37 +00:00
|
|
|
for i := len(pattern.parts) - 1; i >= 0; i-- {
|
2020-10-07 18:55:43 +00:00
|
|
|
var ok bool
|
2021-09-17 21:04:37 +00:00
|
|
|
if pattern.parts[i].isSimple {
|
|
|
|
ok = pattern.parts[i].pattern == strs[offset+i]
|
2020-10-07 18:55:43 +00:00
|
|
|
} else {
|
2021-09-17 21:04:37 +00:00
|
|
|
ok, err = filepath.Match(pattern.parts[i].pattern, strs[offset+i])
|
2020-10-07 18:55:43 +00:00
|
|
|
if err != nil {
|
|
|
|
return false, errors.Wrap(err, "Match")
|
|
|
|
}
|
2015-07-13 20:05:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if !ok {
|
|
|
|
continue outer
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
2020-10-07 12:39:51 +00:00
|
|
|
// ParsePatterns prepares a list of patterns for use with List.
|
2021-09-17 21:04:37 +00:00
|
|
|
func ParsePatterns(pattern []string) []Pattern {
|
2020-10-07 12:39:51 +00:00
|
|
|
patpat := make([]Pattern, 0)
|
2021-09-17 21:04:37 +00:00
|
|
|
for _, pat := range pattern {
|
2020-10-07 12:39:51 +00:00
|
|
|
if pat == "" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
pats := preparePattern(pat)
|
|
|
|
patpat = append(patpat, pats)
|
|
|
|
}
|
|
|
|
return patpat
|
|
|
|
}
|
|
|
|
|
|
|
|
// List returns true if str matches one of the patterns. Empty patterns are ignored.
|
2020-10-07 17:46:41 +00:00
|
|
|
func List(patterns []Pattern, str string) (matched bool, err error) {
|
|
|
|
matched, _, err = list(patterns, false, str)
|
|
|
|
return matched, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// ListWithChild returns true if str matches one of the patterns. Empty patterns are ignored.
|
|
|
|
func ListWithChild(patterns []Pattern, str string) (matched bool, childMayMatch bool, err error) {
|
|
|
|
return list(patterns, true, str)
|
|
|
|
}
|
|
|
|
|
2019-07-02 19:36:23 +00:00
|
|
|
// list returns true if str matches one of the patterns. Empty patterns are ignored.
|
|
|
|
// Patterns prefixed by "!" are negated: any matching file excluded by a previous pattern
|
|
|
|
// will become included again.
|
2020-10-07 17:46:41 +00:00
|
|
|
func list(patterns []Pattern, checkChildMatches bool, str string) (matched bool, childMayMatch bool, err error) {
|
2020-10-07 12:27:59 +00:00
|
|
|
if len(patterns) == 0 {
|
|
|
|
return false, false, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
strs, err := prepareStr(str)
|
|
|
|
if err != nil {
|
|
|
|
return false, false, err
|
|
|
|
}
|
2021-09-17 21:13:49 +00:00
|
|
|
|
|
|
|
hasNegatedPattern := false
|
|
|
|
for _, pat := range patterns {
|
|
|
|
hasNegatedPattern = hasNegatedPattern || pat.isNegated
|
|
|
|
}
|
|
|
|
|
2015-07-13 20:05:21 +00:00
|
|
|
for _, pat := range patterns {
|
2020-10-07 12:39:51 +00:00
|
|
|
m, err := match(pat, strs)
|
2015-07-13 20:05:21 +00:00
|
|
|
if err != nil {
|
2017-06-16 14:46:16 +00:00
|
|
|
return false, false, err
|
2015-07-13 20:05:21 +00:00
|
|
|
}
|
|
|
|
|
2020-10-07 17:46:41 +00:00
|
|
|
var c bool
|
|
|
|
if checkChildMatches {
|
|
|
|
c, err = childMatch(pat, strs)
|
|
|
|
if err != nil {
|
|
|
|
return false, false, err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
c = true
|
2017-06-16 14:46:16 +00:00
|
|
|
}
|
|
|
|
|
2019-07-02 19:36:23 +00:00
|
|
|
if pat.isNegated {
|
|
|
|
matched = matched && !m
|
|
|
|
childMayMatch = childMayMatch && !m
|
|
|
|
} else {
|
|
|
|
matched = matched || m
|
|
|
|
childMayMatch = childMayMatch || c
|
2021-09-17 21:13:49 +00:00
|
|
|
|
|
|
|
if matched && childMayMatch && !hasNegatedPattern {
|
|
|
|
// without negative patterns the result cannot change any more
|
|
|
|
break
|
|
|
|
}
|
2015-07-13 20:05:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-16 14:46:16 +00:00
|
|
|
return matched, childMayMatch, nil
|
2015-07-13 20:05:21 +00:00
|
|
|
}
|