2
2
mirror of https://github.com/octoleo/restic.git synced 2024-11-10 15:21:03 +00:00

filter: Cleanup variable naming

This commit is contained in:
Michael Eischer 2021-09-17 23:04:37 +02:00 committed by Alexander Neumann
parent 5f145f0c7e
commit 12606b575f

View File

@ -28,20 +28,20 @@ func prepareStr(str string) ([]string, error) {
return splitPath(str), nil return splitPath(str), nil
} }
func preparePattern(pattern string) Pattern { func preparePattern(patternStr string) Pattern {
parts := splitPath(filepath.Clean(pattern)) pathParts := splitPath(filepath.Clean(patternStr))
patterns := make([]patternPart, len(parts)) parts := make([]patternPart, len(pathParts))
for i, part := range parts { for i, part := range pathParts {
isSimple := !strings.ContainsAny(part, "\\[]*?") isSimple := !strings.ContainsAny(part, "\\[]*?")
// Replace "**" with the empty string to get faster comparisons // Replace "**" with the empty string to get faster comparisons
// (length-check only) in hasDoubleWildcard. // (length-check only) in hasDoubleWildcard.
if part == "**" { if part == "**" {
part = "" part = ""
} }
patterns[i] = patternPart{part, isSimple} parts[i] = patternPart{part, isSimple}
} }
return Pattern{patterns} return Pattern{parts}
} }
// Split p into path components. Assuming p has been Cleaned, no component // Split p into path components. Assuming p has been Cleaned, no component
@ -64,19 +64,19 @@ func splitPath(p string) []string {
// In addition patterns suitable for filepath.Match, pattern accepts a // In addition patterns suitable for filepath.Match, pattern accepts a
// recursive wildcard '**', which greedily matches an arbitrary number of // recursive wildcard '**', which greedily matches an arbitrary number of
// intermediate directories. // intermediate directories.
func Match(pattern, str string) (matched bool, err error) { func Match(patternStr, str string) (matched bool, err error) {
if pattern == "" { if patternStr == "" {
return true, nil return true, nil
} }
patterns := preparePattern(pattern) pattern := preparePattern(patternStr)
strs, err := prepareStr(str) strs, err := prepareStr(str)
if err != nil { if err != nil {
return false, err return false, err
} }
return match(patterns, strs) return match(pattern, strs)
} }
// ChildMatch returns true if children of str can match the pattern. When the pattern is // ChildMatch returns true if children of str can match the pattern. When the pattern is
@ -89,28 +89,28 @@ func Match(pattern, str string) (matched bool, err error) {
// In addition patterns suitable for filepath.Match, pattern accepts a // In addition patterns suitable for filepath.Match, pattern accepts a
// recursive wildcard '**', which greedily matches an arbitrary number of // recursive wildcard '**', which greedily matches an arbitrary number of
// intermediate directories. // intermediate directories.
func ChildMatch(pattern, str string) (matched bool, err error) { func ChildMatch(patternStr, str string) (matched bool, err error) {
if pattern == "" { if patternStr == "" {
return true, nil return true, nil
} }
patterns := preparePattern(pattern) pattern := preparePattern(patternStr)
strs, err := prepareStr(str) strs, err := prepareStr(str)
if err != nil { if err != nil {
return false, err return false, err
} }
return childMatch(patterns, strs) return childMatch(pattern, strs)
} }
func childMatch(patterns Pattern, strs []string) (matched bool, err error) { func childMatch(pattern Pattern, strs []string) (matched bool, err error) {
if patterns.parts[0].pattern != "/" { if pattern.parts[0].pattern != "/" {
// relative pattern can always be nested down // relative pattern can always be nested down
return true, nil return true, nil
} }
ok, pos := hasDoubleWildcard(patterns) ok, pos := hasDoubleWildcard(pattern)
if ok && len(strs) >= pos { if ok && len(strs) >= pos {
// cut off at the double wildcard // cut off at the double wildcard
strs = strs[:pos] strs = strs[:pos]
@ -118,12 +118,12 @@ func childMatch(patterns Pattern, strs []string) (matched bool, err error) {
// match path against absolute pattern prefix // match path against absolute pattern prefix
l := 0 l := 0
if len(strs) > len(patterns.parts) { if len(strs) > len(pattern.parts) {
l = len(patterns.parts) l = len(pattern.parts)
} else { } else {
l = len(strs) l = len(strs)
} }
return match(Pattern{patterns.parts[0:l]}, strs) return match(Pattern{pattern.parts[0:l]}, strs)
} }
func hasDoubleWildcard(list Pattern) (ok bool, pos int) { func hasDoubleWildcard(list Pattern) (ok bool, pos int) {
@ -136,20 +136,20 @@ func hasDoubleWildcard(list Pattern) (ok bool, pos int) {
return false, 0 return false, 0
} }
func match(patterns Pattern, strs []string) (matched bool, err error) { func match(pattern Pattern, strs []string) (matched bool, err error) {
if ok, pos := hasDoubleWildcard(patterns); ok { if ok, pos := hasDoubleWildcard(pattern); ok {
// gradually expand '**' into separate wildcards // gradually expand '**' into separate wildcards
newPat := make([]patternPart, len(strs)) newPat := make([]patternPart, len(strs))
// copy static prefix once // copy static prefix once
copy(newPat, patterns.parts[:pos]) copy(newPat, pattern.parts[:pos])
for i := 0; i <= len(strs)-len(patterns.parts)+1; i++ { for i := 0; i <= len(strs)-len(pattern.parts)+1; i++ {
// limit to static prefix and already appended '*' // limit to static prefix and already appended '*'
newPat := newPat[:pos+i] newPat := newPat[:pos+i]
// in the first iteration the wildcard expands to nothing // in the first iteration the wildcard expands to nothing
if i > 0 { if i > 0 {
newPat[pos+i-1] = patternPart{"*", false} newPat[pos+i-1] = patternPart{"*", false}
} }
newPat = append(newPat, patterns.parts[pos+1:]...) newPat = append(newPat, pattern.parts[pos+1:]...)
matched, err := match(Pattern{newPat}, strs) matched, err := match(Pattern{newPat}, strs)
if err != nil { if err != nil {
@ -164,20 +164,20 @@ func match(patterns Pattern, strs []string) (matched bool, err error) {
return false, nil return false, nil
} }
if len(patterns.parts) == 0 && len(strs) == 0 { if len(pattern.parts) == 0 && len(strs) == 0 {
return true, nil return true, nil
} }
// an empty pattern never matches a non-empty path // an empty pattern never matches a non-empty path
if len(patterns.parts) == 0 { if len(pattern.parts) == 0 {
return false, nil return false, nil
} }
if len(patterns.parts) <= len(strs) { if len(pattern.parts) <= len(strs) {
minOffset := 0 minOffset := 0
maxOffset := len(strs) - len(patterns.parts) maxOffset := len(strs) - len(pattern.parts)
// special case absolute patterns // special case absolute patterns
if patterns.parts[0].pattern == "/" { if pattern.parts[0].pattern == "/" {
maxOffset = 0 maxOffset = 0
} else if strs[0] == "/" { } else if strs[0] == "/" {
// skip absolute path marker if pattern is not rooted // skip absolute path marker if pattern is not rooted
@ -186,12 +186,12 @@ func match(patterns Pattern, strs []string) (matched bool, err error) {
outer: outer:
for offset := maxOffset; offset >= minOffset; offset-- { for offset := maxOffset; offset >= minOffset; offset-- {
for i := len(patterns.parts) - 1; i >= 0; i-- { for i := len(pattern.parts) - 1; i >= 0; i-- {
var ok bool var ok bool
if patterns.parts[i].isSimple { if pattern.parts[i].isSimple {
ok = patterns.parts[i].pattern == strs[offset+i] ok = pattern.parts[i].pattern == strs[offset+i]
} else { } else {
ok, err = filepath.Match(patterns.parts[i].pattern, strs[offset+i]) ok, err = filepath.Match(pattern.parts[i].pattern, strs[offset+i])
if err != nil { if err != nil {
return false, errors.Wrap(err, "Match") return false, errors.Wrap(err, "Match")
} }
@ -210,9 +210,9 @@ func match(patterns Pattern, strs []string) (matched bool, err error) {
} }
// ParsePatterns prepares a list of patterns for use with List. // ParsePatterns prepares a list of patterns for use with List.
func ParsePatterns(patterns []string) []Pattern { func ParsePatterns(pattern []string) []Pattern {
patpat := make([]Pattern, 0) patpat := make([]Pattern, 0)
for _, pat := range patterns { for _, pat := range pattern {
if pat == "" { if pat == "" {
continue continue
} }