Validate exclude patterns

This commit is contained in:
Lorenz Bausch 2022-05-07 20:52:29 +02:00
parent e7fd200237
commit 9fb81c4246
No known key found for this signature in database
GPG Key ID: FF6F200261C01807
2 changed files with 40 additions and 0 deletions

View File

@ -20,6 +20,7 @@ import (
"github.com/restic/restic/internal/archiver"
"github.com/restic/restic/internal/debug"
"github.com/restic/restic/internal/errors"
"github.com/restic/restic/internal/filter"
"github.com/restic/restic/internal/fs"
"github.com/restic/restic/internal/repository"
"github.com/restic/restic/internal/restic"
@ -298,6 +299,11 @@ func collectRejectByNameFuncs(opts BackupOptions, repo *repository.Repository, t
if err != nil {
return nil, err
}
if valid, invalidPatterns := filter.ValidatePatterns(excludes); !valid {
return nil, errors.Fatalf("--exclude-file: invalid pattern(s) provided:\n%s", strings.Join(invalidPatterns, "\n"))
}
opts.Excludes = append(opts.Excludes, excludes...)
}
@ -306,14 +312,27 @@ func collectRejectByNameFuncs(opts BackupOptions, repo *repository.Repository, t
if err != nil {
return nil, err
}
if valid, invalidPatterns := filter.ValidatePatterns(excludes); !valid {
return nil, errors.Fatalf("--iexclude-file: invalid pattern(s) provided:\n%s", strings.Join(invalidPatterns, "\n"))
}
opts.InsensitiveExcludes = append(opts.InsensitiveExcludes, excludes...)
}
if len(opts.InsensitiveExcludes) > 0 {
if valid, invalidPatterns := filter.ValidatePatterns(opts.InsensitiveExcludes); !valid {
return nil, errors.Fatalf("--iexclude: invalid pattern(s) provided:\n%s", strings.Join(invalidPatterns, "\n"))
}
fs = append(fs, rejectByInsensitivePattern(opts.InsensitiveExcludes))
}
if len(opts.Excludes) > 0 {
if valid, invalidPatterns := filter.ValidatePatterns(opts.Excludes); !valid {
return nil, errors.Fatalf("--exclude: invalid pattern(s) provided:\n%s", strings.Join(invalidPatterns, "\n"))
}
fs = append(fs, rejectByPattern(opts.Excludes))
}

View File

@ -220,6 +220,27 @@ func match(pattern Pattern, strs []string) (matched bool, err error) {
return false, nil
}
// ValidatePatterns validates a slice of patterns.
// Returns true if all patterns are valid - false otherwise, along with the invalid patterns.
func ValidatePatterns(patterns []string) (allValid bool, invalidPatterns []string) {
invalidPatterns = make([]string, 0)
for _, Pattern := range ParsePatterns(patterns) {
// Validate all pattern parts
for _, part := range Pattern.parts {
// Validate the pattern part by trying to match it against itself
if _, validErr := filepath.Match(part.pattern, part.pattern); validErr != nil {
invalidPatterns = append(invalidPatterns, Pattern.original)
// If a single part is invalid, stop processing this pattern
continue
}
}
}
return len(invalidPatterns) == 0, invalidPatterns
}
// ParsePatterns prepares a list of patterns for use with List.
func ParsePatterns(pattern []string) []Pattern {
patpat := make([]Pattern, 0)