1
0
Fork 0
mirror of https://github.com/restic/restic.git synced 2024-12-26 01:37:12 +00:00

Merge pull request #290 from bchapuis/fix-289

Load the index and search subtree
This commit is contained in:
Alexander Neumann 2015-09-01 21:08:21 +02:00
commit 76b1f017c0
2 changed files with 38 additions and 5 deletions

View file

@ -94,7 +94,7 @@ func (c CmdFind) findInTree(repo *repository.Repository, id backend.ID, path str
} }
if node.Type == "dir" { 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 { if err != nil {
return nil, err return nil, err
} }
@ -122,11 +122,10 @@ func (c CmdFind) findInSnapshot(repo *repository.Repository, id backend.ID) erro
if len(results) == 0 { if len(results) == 0 {
return nil return nil
} }
c.global.Verbosef("found %d matching entries in snapshot %s\n", len(results), id)
fmt.Printf("found %d matching entries in snapshot %s\n", len(results), id)
for _, res := range results { for _, res := range results {
res.node.Name = filepath.Join(res.path, res.node.Name) 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 return nil
@ -138,7 +137,7 @@ func (CmdFind) Usage() string {
func (c CmdFind) Execute(args []string) error { func (c CmdFind) Execute(args []string) error {
if len(args) != 1 { 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 var err error
@ -168,6 +167,11 @@ func (c CmdFind) Execute(args []string) error {
return err return err
} }
err = repo.LoadIndex()
if err != nil {
return err
}
c.pattern = args[0] c.pattern = args[0]
if c.Snapshot != "" { if c.Snapshot != "" {

View file

@ -100,6 +100,16 @@ func cmdLs(t testing.TB, global GlobalOptions, snapshotID string) []string {
return strings.Split(string(buf.Bytes()), "\n") 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) { func TestBackup(t *testing.T) {
withTestEnvironment(t, func(env *testEnvironment, global GlobalOptions) { withTestEnvironment(t, func(env *testEnvironment, global GlobalOptions) {
datafile := filepath.Join("testdata", "backup-data.tar.gz") 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") "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)
})
}