Rework pattern excludes

This commit is contained in:
Alexander Neumann 2017-09-10 14:34:28 +02:00
parent 4a0129fc2b
commit 0dfdf02885
2 changed files with 31 additions and 17 deletions

View file

@ -15,7 +15,6 @@ 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/restic"
)
@ -416,39 +415,34 @@ func runBackup(opts BackupOptions, gopts GlobalOptions, args []string) error {
Verbosef("scan %v\n", target)
// rejectFuncs collect functions that can reject items from the backup
var rejectFuncs []RejectFunc
// add patterns from file
if len(opts.ExcludeFiles) > 0 {
opts.Excludes = append(opts.Excludes, readExcludePatternsFromFiles(opts.ExcludeFiles)...)
}
if len(opts.Excludes) > 0 {
rejectFuncs = append(rejectFuncs, rejectByPattern(opts.Excludes))
}
if opts.ExcludeCaches {
opts.ExcludeIfPresent = append(opts.ExcludeIfPresent, "CACHEDIR.TAG:Signature: 8a477f597d28d172789f06886806bc55")
}
var excludesByFile []RejectFunc
for _, spec := range opts.ExcludeIfPresent {
f, err := rejectIfPresent(spec)
if err != nil {
return err
}
excludesByFile = append(excludesByFile, f)
rejectFuncs = append(rejectFuncs, f)
}
selectFilter := func(item string, fi os.FileInfo) bool {
matched, _, err := filter.List(opts.Excludes, item)
if err != nil {
Warnf("error for exclude pattern: %v", err)
}
if matched {
debug.Log("path %q excluded by a filter", item)
return false
}
for _, excludeByFile := range excludesByFile {
if excludeByFile(item, fi) {
debug.Log("path %q excluded by tagfile", item)
for _, reject := range rejectFuncs {
if reject(item, fi) {
return false
}
}

View file

@ -9,6 +9,8 @@ import (
"strings"
"github.com/restic/restic/internal/debug"
"github.com/restic/restic/internal/errors"
"github.com/restic/restic/internal/filter"
"github.com/restic/restic/internal/fs"
)
@ -17,6 +19,24 @@ import (
// should be excluded (rejected) from the backup.
type RejectFunc func(filename string, fi os.FileInfo) bool
// rejectByPattern returns a RejectFunc which rejects files that match
// one of the patterns.
func rejectByPattern(patterns []string) RejectFunc {
return func(item string, fi os.FileInfo) bool {
matched, _, err := filter.List(patterns, item)
if err != nil {
Warnf("error for exclude pattern: %v", err)
}
if matched {
debug.Log("path %q excluded by a filter", item)
return true
}
return false
}
}
// rejectIfPresent returns a RejectFunc which itself returns whether a path
// should be excluded. The RejectFunc considers a file to be excluded when
// it resides in a directory with an exclusion file, that is specified by
@ -24,7 +44,7 @@ type RejectFunc func(filename string, fi os.FileInfo) bool
// non-nil if the filename component of excludeFileSpec is empty.
func rejectIfPresent(excludeFileSpec string) (RejectFunc, error) {
if excludeFileSpec == "" {
return func(string, os.FileInfo) bool { return false }, nil
return nil, errors.New("name for exclusion tagfile is empty")
}
colon := strings.Index(excludeFileSpec, ":")
if colon == 0 {