Allow multiple exclude-if-present

This commit is contained in:
Alexander Neumann 2017-09-09 21:24:29 +02:00
parent ea75509d6e
commit c22c582546
1 changed files with 16 additions and 12 deletions

View File

@ -59,7 +59,7 @@ type BackupOptions struct {
Excludes []string Excludes []string
ExcludeFiles []string ExcludeFiles []string
ExcludeOtherFS bool ExcludeOtherFS bool
ExcludeIfPresent string ExcludeIfPresent []string
ExcludeCaches bool ExcludeCaches bool
Stdin bool Stdin bool
StdinFilename string StdinFilename string
@ -79,7 +79,7 @@ func init() {
f.StringArrayVarP(&backupOptions.Excludes, "exclude", "e", nil, "exclude a `pattern` (can be specified multiple times)") f.StringArrayVarP(&backupOptions.Excludes, "exclude", "e", nil, "exclude a `pattern` (can be specified multiple times)")
f.StringArrayVar(&backupOptions.ExcludeFiles, "exclude-file", nil, "read exclude patterns from a `file` (can be specified multiple times)") f.StringArrayVar(&backupOptions.ExcludeFiles, "exclude-file", nil, "read exclude patterns from a `file` (can be specified multiple times)")
f.BoolVarP(&backupOptions.ExcludeOtherFS, "one-file-system", "x", false, "exclude other file systems") f.BoolVarP(&backupOptions.ExcludeOtherFS, "one-file-system", "x", false, "exclude other file systems")
f.StringVar(&backupOptions.ExcludeIfPresent, "exclude-if-present", "", "takes filename[:header], exclude contents of directories containing filename (except filename itself) if header of that file is as provided") f.StringArrayVar(&backupOptions.ExcludeIfPresent, "exclude-if-present", nil, "takes filename[:header], exclude contents of directories containing filename (except filename itself) if header of that file is as provided (can be specified multiple times)")
f.BoolVar(&backupOptions.ExcludeCaches, "exclude-caches", false, `excludes cache directories that are marked with a CACHEDIR.TAG file`) f.BoolVar(&backupOptions.ExcludeCaches, "exclude-caches", false, `excludes cache directories that are marked with a CACHEDIR.TAG file`)
f.BoolVar(&backupOptions.Stdin, "stdin", false, "read backup from stdin") f.BoolVar(&backupOptions.Stdin, "stdin", false, "read backup from stdin")
f.StringVar(&backupOptions.StdinFilename, "stdin-filename", "stdin", "file name to use when reading from stdin") f.StringVar(&backupOptions.StdinFilename, "stdin-filename", "stdin", "file name to use when reading from stdin")
@ -423,17 +423,19 @@ func runBackup(opts BackupOptions, gopts GlobalOptions, args []string) error {
} }
if opts.ExcludeCaches { if opts.ExcludeCaches {
if opts.ExcludeIfPresent != "" { opts.ExcludeIfPresent = append(opts.ExcludeIfPresent, "CACHEDIR.TAG:Signature: 8a477f597d28d172789f06886806bc55")
return fmt.Errorf("cannot have --exclude-caches defined at the same time as --exclude-if-present")
}
opts.ExcludeIfPresent = "CACHEDIR.TAG:Signature: 8a477f597d28d172789f06886806bc55"
} }
excludeByFile, err := excludeByFile(opts.ExcludeIfPresent) var excludesByFile []FilenameCheck
for _, spec := range opts.ExcludeIfPresent {
f, err := excludeByFile(spec)
if err != nil { if err != nil {
return err return err
} }
excludesByFile = append(excludesByFile, f)
}
selectFilter := func(item string, fi os.FileInfo) bool { selectFilter := func(item string, fi os.FileInfo) bool {
matched, _, err := filter.List(opts.Excludes, item) matched, _, err := filter.List(opts.Excludes, item)
if err != nil { if err != nil {
@ -445,10 +447,12 @@ func runBackup(opts BackupOptions, gopts GlobalOptions, args []string) error {
return false return false
} }
for _, excludeByFile := range excludesByFile {
if excludeByFile(item) { if excludeByFile(item) {
debug.Log("path %q excluded by tagfile", item) debug.Log("path %q excluded by tagfile", item)
return false return false
} }
}
if !opts.ExcludeOtherFS || fi == nil { if !opts.ExcludeOtherFS || fi == nil {
return true return true