Rework pattern excludes
This commit is contained in:
parent
4a0129fc2b
commit
0dfdf02885
2 changed files with 31 additions and 17 deletions
|
@ -15,7 +15,6 @@ import (
|
||||||
"github.com/restic/restic/internal/archiver"
|
"github.com/restic/restic/internal/archiver"
|
||||||
"github.com/restic/restic/internal/debug"
|
"github.com/restic/restic/internal/debug"
|
||||||
"github.com/restic/restic/internal/errors"
|
"github.com/restic/restic/internal/errors"
|
||||||
"github.com/restic/restic/internal/filter"
|
|
||||||
"github.com/restic/restic/internal/fs"
|
"github.com/restic/restic/internal/fs"
|
||||||
"github.com/restic/restic/internal/restic"
|
"github.com/restic/restic/internal/restic"
|
||||||
)
|
)
|
||||||
|
@ -416,39 +415,34 @@ func runBackup(opts BackupOptions, gopts GlobalOptions, args []string) error {
|
||||||
|
|
||||||
Verbosef("scan %v\n", target)
|
Verbosef("scan %v\n", target)
|
||||||
|
|
||||||
|
// rejectFuncs collect functions that can reject items from the backup
|
||||||
|
var rejectFuncs []RejectFunc
|
||||||
|
|
||||||
// add patterns from file
|
// add patterns from file
|
||||||
if len(opts.ExcludeFiles) > 0 {
|
if len(opts.ExcludeFiles) > 0 {
|
||||||
opts.Excludes = append(opts.Excludes, readExcludePatternsFromFiles(opts.ExcludeFiles)...)
|
opts.Excludes = append(opts.Excludes, readExcludePatternsFromFiles(opts.ExcludeFiles)...)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if len(opts.Excludes) > 0 {
|
||||||
|
rejectFuncs = append(rejectFuncs, rejectByPattern(opts.Excludes))
|
||||||
|
}
|
||||||
|
|
||||||
if opts.ExcludeCaches {
|
if opts.ExcludeCaches {
|
||||||
opts.ExcludeIfPresent = append(opts.ExcludeIfPresent, "CACHEDIR.TAG:Signature: 8a477f597d28d172789f06886806bc55")
|
opts.ExcludeIfPresent = append(opts.ExcludeIfPresent, "CACHEDIR.TAG:Signature: 8a477f597d28d172789f06886806bc55")
|
||||||
}
|
}
|
||||||
|
|
||||||
var excludesByFile []RejectFunc
|
|
||||||
for _, spec := range opts.ExcludeIfPresent {
|
for _, spec := range opts.ExcludeIfPresent {
|
||||||
f, err := rejectIfPresent(spec)
|
f, err := rejectIfPresent(spec)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
excludesByFile = append(excludesByFile, f)
|
rejectFuncs = append(rejectFuncs, f)
|
||||||
}
|
}
|
||||||
|
|
||||||
selectFilter := func(item string, fi os.FileInfo) bool {
|
selectFilter := func(item string, fi os.FileInfo) bool {
|
||||||
matched, _, err := filter.List(opts.Excludes, item)
|
for _, reject := range rejectFuncs {
|
||||||
if err != nil {
|
if reject(item, fi) {
|
||||||
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)
|
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,6 +9,8 @@ import (
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/restic/restic/internal/debug"
|
"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/fs"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -17,6 +19,24 @@ import (
|
||||||
// should be excluded (rejected) from the backup.
|
// should be excluded (rejected) from the backup.
|
||||||
type RejectFunc func(filename string, fi os.FileInfo) bool
|
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
|
// rejectIfPresent returns a RejectFunc which itself returns whether a path
|
||||||
// should be excluded. The RejectFunc considers a file to be excluded when
|
// 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
|
// 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.
|
// non-nil if the filename component of excludeFileSpec is empty.
|
||||||
func rejectIfPresent(excludeFileSpec string) (RejectFunc, error) {
|
func rejectIfPresent(excludeFileSpec string) (RejectFunc, error) {
|
||||||
if excludeFileSpec == "" {
|
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, ":")
|
colon := strings.Index(excludeFileSpec, ":")
|
||||||
if colon == 0 {
|
if colon == 0 {
|
||||||
|
|
Loading…
Reference in a new issue