func BenchmarkFilterPatterns(b *testing.B) { patterns := []string{ "sdk/*", "*.html", } lines := extractTestLines(b) var c uint b.ResetTimer() for i := 0; i < b.N; i++ { c = 0 for _, line := range lines { match, err := filter.List(patterns, line) if err != nil { b.Fatal(err) } if match { c++ } } if c != 22185 { b.Fatalf("wrong number of matches: expected 22185, got %d", c) } } }
func TestMatchList(t *testing.T) { for i, test := range filterListTests { match, err := filter.List(test.patterns, test.path) if err != nil { t.Errorf("test %d failed: expected no error for patterns %q, but error returned: %v", i, test.patterns, err) continue } if match != test.match { t.Errorf("test %d: filter.MatchList(%q, %q): expected %v, got %v", i, test.patterns, test.path, test.match, match) } } }
func ExampleMatchList() { match, _ := filter.List([]string{"*.c", "*.go"}, "/home/user/file.go") fmt.Printf("match: %v\n", match) // Output: // match: true }
func runRestore(opts RestoreOptions, gopts GlobalOptions, args []string) error { if len(args) != 1 { return errors.Fatalf("no snapshot ID specified") } if opts.Target == "" { return errors.Fatal("please specify a directory to restore to (--target)") } if len(opts.Exclude) > 0 && len(opts.Include) > 0 { return errors.Fatal("exclude and include patterns are mutually exclusive") } snapshotIDString := args[0] debug.Log("restore %v to %v", snapshotIDString, opts.Target) repo, err := OpenRepository(gopts) if err != nil { return err } if !gopts.NoLock { lock, err := lockRepo(repo) defer unlockRepo(lock) if err != nil { return err } } err = repo.LoadIndex() if err != nil { return err } var id restic.ID if snapshotIDString == "latest" { id, err = restic.FindLatestSnapshot(repo, opts.Paths, opts.Host) if err != nil { Exitf(1, "latest snapshot for criteria not found: %v Paths:%v Host:%v", err, opts.Paths, opts.Host) } } else { id, err = restic.FindSnapshot(repo, snapshotIDString) if err != nil { Exitf(1, "invalid id %q: %v", snapshotIDString, err) } } res, err := restic.NewRestorer(repo, id) if err != nil { Exitf(2, "creating restorer failed: %v\n", err) } res.Error = func(dir string, node *restic.Node, err error) error { Warnf("error for %s: %+v\n", dir, err) return nil } selectExcludeFilter := func(item string, dstpath string, node *restic.Node) bool { matched, err := filter.List(opts.Exclude, item) if err != nil { Warnf("error for exclude pattern: %v", err) } return !matched } selectIncludeFilter := func(item string, dstpath string, node *restic.Node) bool { matched, err := filter.List(opts.Include, item) if err != nil { Warnf("error for include pattern: %v", err) } return matched } if len(opts.Exclude) > 0 { res.SelectFilter = selectExcludeFilter } else if len(opts.Include) > 0 { res.SelectFilter = selectIncludeFilter } Verbosef("restoring %s to %s\n", res.Snapshot(), opts.Target) return res.RestoreTo(opts.Target) }
func runBackup(opts BackupOptions, gopts GlobalOptions, args []string) error { target, err := readLinesFromFile(opts.FilesFrom) if err != nil { return err } // merge files from files-from into normal args so we can reuse the normal // args checks and have the ability to use both files-from and args at the // same time args = append(args, target...) if len(args) == 0 { return errors.Fatalf("wrong number of parameters") } for _, d := range args { if a, err := filepath.Abs(d); err == nil { d = a } target = append(target, d) } target, err = filterExisting(target) if err != nil { return err } // allowed devices var allowedDevs map[uint64]struct{} if opts.ExcludeOtherFS { allowedDevs, err = gatherDevices(target) if err != nil { return err } debug.Log("allowed devices: %v\n", allowedDevs) } repo, err := OpenRepository(gopts) if err != nil { return err } lock, err := lockRepo(repo) defer unlockRepo(lock) if err != nil { return err } err = repo.LoadIndex() if err != nil { return err } var parentSnapshotID *restic.ID // Force using a parent if !opts.Force && opts.Parent != "" { id, err := restic.FindSnapshot(repo, opts.Parent) if err != nil { return errors.Fatalf("invalid id %q: %v", opts.Parent, err) } parentSnapshotID = &id } // Find last snapshot to set it as parent, if not already set if !opts.Force && parentSnapshotID == nil { id, err := restic.FindLatestSnapshot(repo, target, "") if err == nil { parentSnapshotID = &id } else if err != restic.ErrNoSnapshotFound { return err } } if parentSnapshotID != nil { Verbosef("using parent snapshot %v\n", parentSnapshotID.Str()) } Verbosef("scan %v\n", target) // add patterns from file if opts.ExcludeFile != "" { file, err := fs.Open(opts.ExcludeFile) if err != nil { Warnf("error reading exclude patterns: %v", err) return nil } scanner := bufio.NewScanner(file) for scanner.Scan() { line := scanner.Text() if !strings.HasPrefix(line, "#") { line = os.ExpandEnv(line) opts.Excludes = append(opts.Excludes, line) } } } 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 } if !opts.ExcludeOtherFS || fi == nil { return true } id, err := fs.DeviceID(fi) if err != nil { // This should never happen because gatherDevices() would have // errored out earlier. If it still does that's a reason to panic. panic(err) } _, found := allowedDevs[id] if !found { debug.Log("path %q on disallowed device %d", item, id) return false } return true } stat, err := archiver.Scan(target, selectFilter, newScanProgress(gopts)) if err != nil { return err } arch := archiver.New(repo) arch.Excludes = opts.Excludes arch.SelectFilter = selectFilter arch.Error = func(dir string, fi os.FileInfo, err error) error { // TODO: make ignoring errors configurable Warnf("%s\rerror for %s: %v\n", ClearLine(), dir, err) return nil } _, id, err := arch.Snapshot(newArchiveProgress(gopts, stat), target, opts.Tags, parentSnapshotID) if err != nil { return err } Verbosef("snapshot %s saved\n", id.Str()) return nil }
func (cmd CmdRestore) Execute(args []string) error { if len(args) != 1 { return fmt.Errorf("wrong number of arguments, Usage: %s", cmd.Usage()) } if cmd.Target == "" { return errors.New("please specify a directory to restore to (--target)") } if len(cmd.Exclude) > 0 && len(cmd.Include) > 0 { return errors.New("exclude and include patterns are mutually exclusive") } snapshotIDString := args[0] debug.Log("restore", "restore %v to %v", snapshotIDString, cmd.Target) repo, err := cmd.global.OpenRepository() if err != nil { return err } if !cmd.global.NoLock { lock, err := lockRepo(repo) defer unlockRepo(lock) if err != nil { return err } } err = repo.LoadIndex() if err != nil { return err } id, err := restic.FindSnapshot(repo, snapshotIDString) if err != nil { cmd.global.Exitf(1, "invalid id %q: %v", snapshotIDString, err) } res, err := restic.NewRestorer(repo, id) if err != nil { cmd.global.Exitf(2, "creating restorer failed: %v\n", err) } res.Error = func(dir string, node *restic.Node, err error) error { cmd.global.Warnf("error for %s: %+v\n", dir, err) return nil } selectExcludeFilter := func(item string, dstpath string, node *restic.Node) bool { matched, err := filter.List(cmd.Exclude, item) if err != nil { cmd.global.Warnf("error for exclude pattern: %v", err) } return !matched } selectIncludeFilter := func(item string, dstpath string, node *restic.Node) bool { matched, err := filter.List(cmd.Include, item) if err != nil { cmd.global.Warnf("error for include pattern: %v", err) } return matched } if len(cmd.Exclude) > 0 { res.SelectFilter = selectExcludeFilter } else if len(cmd.Include) > 0 { res.SelectFilter = selectIncludeFilter } cmd.global.Verbosef("restoring %s to %s\n", res.Snapshot(), cmd.Target) err = res.RestoreTo(cmd.Target) if err != nil { return err } return nil }
func (cmd CmdBackup) Execute(args []string) error { if len(args) == 0 { return fmt.Errorf("wrong number of parameters, Usage: %s", cmd.Usage()) } target := make([]string, 0, len(args)) for _, d := range args { if a, err := filepath.Abs(d); err == nil { d = a } target = append(target, d) } target, err := filterExisting(target) if err != nil { return err } repo, err := cmd.global.OpenRepository() if err != nil { return err } lock, err := lockRepo(repo) defer unlockRepo(lock) if err != nil { return err } err = repo.LoadIndex() if err != nil { return err } var parentSnapshotID *backend.ID // Force using a parent if !cmd.Force && cmd.Parent != "" { id, err := restic.FindSnapshot(repo, cmd.Parent) if err != nil { return fmt.Errorf("invalid id %q: %v", cmd.Parent, err) } parentSnapshotID = &id } // Find last snapshot to set it as parent, if not already set if !cmd.Force && parentSnapshotID == nil { id, err := findLatestSnapshot(repo, target) if err == nil { parentSnapshotID = &id } else if err != errNoSnapshotFound { return err } } if parentSnapshotID != nil { cmd.global.Verbosef("using parent snapshot %v\n", parentSnapshotID.Str()) } cmd.global.Verbosef("scan %v\n", target) selectFilter := func(item string, fi os.FileInfo) bool { matched, err := filter.List(cmd.Excludes, item) if err != nil { cmd.global.Warnf("error for exclude pattern: %v", err) } if matched { debug.Log("backup.Execute", "path %q excluded by a filter", item) } return !matched } stat, err := restic.Scan(target, selectFilter, cmd.newScanProgress()) if err != nil { return err } arch := restic.NewArchiver(repo) arch.Excludes = cmd.Excludes arch.SelectFilter = selectFilter arch.Error = func(dir string, fi os.FileInfo, err error) error { // TODO: make ignoring errors configurable cmd.global.Warnf("\x1b[2K\rerror for %s: %v\n", dir, err) return nil } _, id, err := arch.Snapshot(cmd.newArchiveProgress(stat), target, parentSnapshotID) if err != nil { return err } cmd.global.Verbosef("snapshot %s saved\n", id.Str()) return nil }