diff --git a/cmd/restic/cmd_find.go b/cmd/restic/cmd_find.go index 253806efd..fa2d7859e 100644 --- a/cmd/restic/cmd_find.go +++ b/cmd/restic/cmd_find.go @@ -94,7 +94,7 @@ func (c CmdFind) findInTree(repo *repository.Repository, id backend.ID, path str } if node.Type == "dir" { - subdirResults, err := c.findInTree(repo, id, filepath.Join(path, node.Name)) + subdirResults, err := c.findInTree(repo, *node.Subtree, filepath.Join(path, node.Name)) if err != nil { return nil, err } @@ -122,11 +122,10 @@ func (c CmdFind) findInSnapshot(repo *repository.Repository, id backend.ID) erro if len(results) == 0 { return nil } - - fmt.Printf("found %d matching entries in snapshot %s\n", len(results), id) + c.global.Verbosef("found %d matching entries in snapshot %s\n", len(results), id) for _, res := range results { res.node.Name = filepath.Join(res.path, res.node.Name) - fmt.Printf(" %s\n", res.node) + c.global.Printf(" %s\n", res.node) } return nil @@ -138,7 +137,7 @@ func (CmdFind) Usage() string { func (c CmdFind) Execute(args []string) error { if len(args) != 1 { - return fmt.Errorf("invalid number of arguments, Usage: %s", c.Usage()) + return fmt.Errorf("wrong number of arguments, Usage: %s", c.Usage()) } var err error @@ -168,6 +167,11 @@ func (c CmdFind) Execute(args []string) error { return err } + err = repo.LoadIndex() + if err != nil { + return err + } + c.pattern = args[0] if c.Snapshot != "" { diff --git a/cmd/restic/integration_test.go b/cmd/restic/integration_test.go index 68266723b..360adca0f 100644 --- a/cmd/restic/integration_test.go +++ b/cmd/restic/integration_test.go @@ -100,6 +100,16 @@ func cmdLs(t testing.TB, global GlobalOptions, snapshotID string) []string { return strings.Split(string(buf.Bytes()), "\n") } +func cmdFind(t testing.TB, global GlobalOptions, pattern string) []string { + var buf bytes.Buffer + global.stdout = &buf + + cmd := &CmdFind{global: &global} + OK(t, cmd.Execute([]string{pattern})) + + return strings.Split(string(buf.Bytes()), "\n") +} + func TestBackup(t *testing.T) { withTestEnvironment(t, func(env *testEnvironment, global GlobalOptions) { datafile := filepath.Join("testdata", "backup-data.tar.gz") @@ -617,3 +627,22 @@ func TestRestoreNoMetadataOnIgnoredIntermediateDirs(t *testing.T) { "meta data of intermediate directory hasn't been restore") }) } + +func TestFind(t *testing.T) { + withTestEnvironment(t, func(env *testEnvironment, global GlobalOptions) { + datafile := filepath.Join("testdata", "backup-data.tar.gz") + cmdInit(t, global) + SetupTarTestFixture(t, env.testdata, datafile) + cmdBackup(t, global, []string{env.testdata}, nil) + cmdCheck(t, global) + + results := cmdFind(t, global, "unexistingfile") + Assert(t, len(results) != 0, "unexisting file found in repo (%v)", datafile) + + results = cmdFind(t, global, "testfile") + Assert(t, len(results) != 1, "file not found in repo (%v)", datafile) + + results = cmdFind(t, global, "test") + Assert(t, len(results) < 2, "less than two file found in repo (%v)", datafile) + }) +}