backend: Rework List()

For a discussion see #1567
This commit is contained in:
Alexander Neumann
2018-01-20 13:43:07 +01:00
parent a3d43a92b3
commit 52230b8f07
8 changed files with 124 additions and 95 deletions

View File

@@ -49,8 +49,13 @@ func TestLayout(t *testing.T) {
}
datafiles := make(map[string]bool)
for id := range be.List(context.TODO(), restic.DataFile) {
datafiles[id] = false
err = be.List(context.TODO(), restic.DataFile, func(fi restic.FileInfo) error {
datafiles[fi.Name] = false
return nil
})
if err != nil {
t.Fatalf("List() returned error %v", err)
}
if len(datafiles) == 0 {

View File

@@ -228,50 +228,53 @@ func isFile(fi os.FileInfo) bool {
// List returns a channel that yields all names of blobs of type t. A
// goroutine is started for this.
func (b *Local) List(ctx context.Context, t restic.FileType) <-chan string {
func (b *Local) List(ctx context.Context, t restic.FileType, fn func(restic.FileInfo) error) error {
debug.Log("List %v", t)
ch := make(chan string)
go func() {
defer close(ch)
basedir, subdirs := b.Basedir(t)
err := fs.Walk(basedir, func(path string, fi os.FileInfo, err error) error {
debug.Log("walk on %v\n", path)
if err != nil {
return err
}
if path == basedir {
return nil
}
if !isFile(fi) {
return nil
}
if fi.IsDir() && !subdirs {
return filepath.SkipDir
}
debug.Log("send %v\n", filepath.Base(path))
select {
case ch <- filepath.Base(path):
case <-ctx.Done():
return nil
}
return nil
})
basedir, subdirs := b.Basedir(t)
err := fs.Walk(basedir, func(path string, fi os.FileInfo, err error) error {
debug.Log("walk on %v\n", path)
if err != nil {
debug.Log("Walk %v", err)
return err
}
}()
return ch
if path == basedir {
return nil
}
if !isFile(fi) {
return nil
}
if fi.IsDir() && !subdirs {
return filepath.SkipDir
}
debug.Log("send %v\n", filepath.Base(path))
rfi := restic.FileInfo{
Name: filepath.Base(path),
Size: fi.Size(),
}
err = fn(rfi)
if err != nil {
return err
}
if ctx.Err() != nil {
return ctx.Err()
}
return nil
})
if err != nil {
debug.Log("Walk %v", err)
return err
}
return ctx.Err()
}
// Delete removes the repository and all files.