forked from TrueCloudLab/restic
375c2a56de
name old time/op new time/op delta FilterPatterns/Relative-4 30.3ms ±10% 23.6ms ±20% -22.12% (p=0.000 n=10+10) FilterPatterns/Absolute-4 49.0ms ± 3% 32.3ms ± 8% -33.94% (p=0.000 n=8+10) FilterPatterns/Wildcard-4 345ms ± 9% 334ms ±17% ~ (p=0.315 n=10+10) FilterPatterns/ManyNoMatch-4 3.93s ± 2% 0.71s ± 7% -81.98% (p=0.000 n=9+10) name old alloc/op new alloc/op delta FilterPatterns/Relative-4 4.63MB ± 0% 3.57MB ± 0% -22.98% (p=0.000 n=9+9) FilterPatterns/Absolute-4 8.54MB ± 0% 3.57MB ± 0% -58.20% (p=0.000 n=10+10) FilterPatterns/Wildcard-4 146MB ± 0% 141MB ± 0% -2.93% (p=0.000 n=9+9) FilterPatterns/ManyNoMatch-4 907MB ± 0% 4MB ± 0% -99.61% (p=0.000 n=9+9) name old allocs/op new allocs/op delta FilterPatterns/Relative-4 66.6k ± 0% 22.2k ± 0% -66.67% (p=0.000 n=10+10) FilterPatterns/Absolute-4 88.7k ± 0% 22.2k ± 0% -75.00% (p=0.000 n=10+10) FilterPatterns/Wildcard-4 1.70M ± 0% 1.63M ± 0% -3.92% (p=0.000 n=10+10) FilterPatterns/ManyNoMatch-4 4.46M ± 0% 0.02M ± 0% -99.50% (p=0.000 n=10+10)
217 lines
4.9 KiB
Go
217 lines
4.9 KiB
Go
package filter
|
|
|
|
import (
|
|
"path/filepath"
|
|
"strings"
|
|
|
|
"github.com/restic/restic/internal/errors"
|
|
)
|
|
|
|
// ErrBadString is returned when Match is called with the empty string as the
|
|
// second argument.
|
|
var ErrBadString = errors.New("filter.Match: string is empty")
|
|
|
|
// Pattern represents a preparsed filter pattern
|
|
type Pattern []string
|
|
|
|
func prepareStr(str string) ([]string, error) {
|
|
if str == "" {
|
|
return nil, ErrBadString
|
|
}
|
|
|
|
// convert file path separator to '/'
|
|
if filepath.Separator != '/' {
|
|
str = strings.Replace(str, string(filepath.Separator), "/", -1)
|
|
}
|
|
|
|
return strings.Split(str, "/"), nil
|
|
}
|
|
|
|
func preparePattern(pattern string) Pattern {
|
|
pattern = filepath.Clean(pattern)
|
|
|
|
// convert file path separator to '/'
|
|
if filepath.Separator != '/' {
|
|
pattern = strings.Replace(pattern, string(filepath.Separator), "/", -1)
|
|
}
|
|
|
|
return strings.Split(pattern, "/")
|
|
}
|
|
|
|
// 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.
|
|
//
|
|
// In addition patterns suitable for filepath.Match, pattern accepts a
|
|
// recursive wildcard '**', which greedily matches an arbitrary number of
|
|
// intermediate directories.
|
|
func Match(pattern, str string) (matched bool, err error) {
|
|
if pattern == "" {
|
|
return true, nil
|
|
}
|
|
|
|
patterns := preparePattern(pattern)
|
|
strs, err := prepareStr(str)
|
|
|
|
if err != nil {
|
|
return false, err
|
|
}
|
|
|
|
return match(patterns, strs)
|
|
}
|
|
|
|
// 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.
|
|
//
|
|
// In addition patterns suitable for filepath.Match, pattern accepts a
|
|
// recursive wildcard '**', which greedily matches an arbitrary number of
|
|
// intermediate directories.
|
|
func ChildMatch(pattern, str string) (matched bool, err error) {
|
|
if pattern == "" {
|
|
return true, nil
|
|
}
|
|
|
|
patterns := preparePattern(pattern)
|
|
strs, err := prepareStr(str)
|
|
|
|
if err != nil {
|
|
return false, err
|
|
}
|
|
|
|
return childMatch(patterns, strs)
|
|
}
|
|
|
|
func childMatch(patterns Pattern, strs []string) (matched bool, err error) {
|
|
if patterns[0] != "" {
|
|
// relative pattern can always be nested down
|
|
return true, nil
|
|
}
|
|
|
|
ok, pos := hasDoubleWildcard(patterns)
|
|
if ok && len(strs) >= pos {
|
|
// cut off at the double wildcard
|
|
strs = strs[:pos]
|
|
}
|
|
|
|
// match path against absolute pattern prefix
|
|
l := 0
|
|
if len(strs) > len(patterns) {
|
|
l = len(patterns)
|
|
} else {
|
|
l = len(strs)
|
|
}
|
|
return match(patterns[0:l], strs)
|
|
}
|
|
|
|
func hasDoubleWildcard(list Pattern) (ok bool, pos int) {
|
|
for i, item := range list {
|
|
if item == "**" {
|
|
return true, i
|
|
}
|
|
}
|
|
|
|
return false, 0
|
|
}
|
|
|
|
func match(patterns Pattern, strs []string) (matched bool, err error) {
|
|
if ok, pos := hasDoubleWildcard(patterns); ok {
|
|
// gradually expand '**' into separate wildcards
|
|
for i := 0; i <= len(strs)-len(patterns)+1; i++ {
|
|
newPat := make(Pattern, pos)
|
|
copy(newPat, patterns[:pos])
|
|
for k := 0; k < i; k++ {
|
|
newPat = append(newPat, "*")
|
|
}
|
|
newPat = append(newPat, patterns[pos+1:]...)
|
|
|
|
matched, err := match(newPat, strs)
|
|
if err != nil {
|
|
return false, err
|
|
}
|
|
|
|
if matched {
|
|
return true, nil
|
|
}
|
|
}
|
|
|
|
return false, nil
|
|
}
|
|
|
|
if len(patterns) == 0 && len(strs) == 0 {
|
|
return true, nil
|
|
}
|
|
|
|
if len(patterns) <= len(strs) {
|
|
outer:
|
|
for offset := len(strs) - len(patterns); offset >= 0; offset-- {
|
|
|
|
for i := len(patterns) - 1; i >= 0; i-- {
|
|
ok, err := filepath.Match(patterns[i], strs[offset+i])
|
|
if err != nil {
|
|
return false, errors.Wrap(err, "Match")
|
|
}
|
|
|
|
if !ok {
|
|
continue outer
|
|
}
|
|
}
|
|
|
|
return true, nil
|
|
}
|
|
}
|
|
|
|
return false, nil
|
|
}
|
|
|
|
// ParsePatterns prepares a list of patterns for use with List.
|
|
func ParsePatterns(patterns []string) []Pattern {
|
|
patpat := make([]Pattern, 0)
|
|
for _, pat := range patterns {
|
|
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.
|
|
func List(patterns []Pattern, str string) (matched bool, childMayMatch bool, err error) {
|
|
if len(patterns) == 0 {
|
|
return false, false, nil
|
|
}
|
|
|
|
strs, err := prepareStr(str)
|
|
if err != nil {
|
|
return false, false, err
|
|
}
|
|
for _, pat := range patterns {
|
|
m, err := match(pat, strs)
|
|
if err != nil {
|
|
return false, false, err
|
|
}
|
|
|
|
c, err := childMatch(pat, strs)
|
|
if err != nil {
|
|
return false, false, err
|
|
}
|
|
|
|
matched = matched || m
|
|
childMayMatch = childMayMatch || c
|
|
|
|
if matched && childMayMatch {
|
|
return true, true, nil
|
|
}
|
|
}
|
|
|
|
return matched, childMayMatch, nil
|
|
}
|