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 (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 } 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 err } 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 != "" { parentSnapshotID, err = restic.FindSnapshot(repo, cmd.Parent) if err != nil { return fmt.Errorf("invalid id %q: %v", cmd.Parent, err) } cmd.global.Verbosef("found parent snapshot %v\n", parentSnapshotID.Str()) } // Find last snapshot to set it as parent, if not already set if !cmd.Force && parentSnapshotID == nil { parentSnapshotID, err = findLatestSnapshot(repo, target) if err != nil { return err } if parentSnapshotID != nil { cmd.global.Verbosef("using parent snapshot %v\n", parentSnapshotID) } } 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) } 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 }
func ExampleMatchList() { match, _ := filter.List([]string{"*.c", "*.go"}, "/home/user/file.go") fmt.Printf("match: %v\n", match) // Output: // match: true }