2015-04-08 00:52:48 +02:00
|
|
|
package storage
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
|
2015-04-28 00:58:58 +02:00
|
|
|
"github.com/docker/distribution/context"
|
2015-04-08 00:52:48 +02:00
|
|
|
storageDriver "github.com/docker/distribution/registry/storage/driver"
|
|
|
|
)
|
|
|
|
|
2015-10-20 01:42:12 +02:00
|
|
|
// ErrSkipDir is used as a return value from onFileFunc to indicate that
|
2015-04-08 00:52:48 +02:00
|
|
|
// the directory named in the call is to be skipped. It is not returned
|
|
|
|
// as an error by any function.
|
|
|
|
var ErrSkipDir = errors.New("skip this directory")
|
|
|
|
|
|
|
|
// WalkFn is called once per file by Walk
|
|
|
|
// If the returned error is ErrSkipDir and fileInfo refers
|
|
|
|
// to a directory, the directory will not be entered and Walk
|
|
|
|
// will continue the traversal. Otherwise Walk will return
|
|
|
|
type WalkFn func(fileInfo storageDriver.FileInfo) error
|
|
|
|
|
|
|
|
// Walk traverses a filesystem defined within driver, starting
|
|
|
|
// from the given path, calling f on each file
|
2015-04-28 00:58:58 +02:00
|
|
|
func Walk(ctx context.Context, driver storageDriver.StorageDriver, from string, f WalkFn) error {
|
|
|
|
children, err := driver.List(ctx, from)
|
2015-04-08 00:52:48 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, child := range children {
|
2015-04-28 00:58:58 +02:00
|
|
|
fileInfo, err := driver.Stat(ctx, child)
|
2015-04-08 00:52:48 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = f(fileInfo)
|
|
|
|
skipDir := (err == ErrSkipDir)
|
|
|
|
if err != nil && !skipDir {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if fileInfo.IsDir() && !skipDir {
|
2015-12-02 01:24:07 +01:00
|
|
|
if err := Walk(ctx, driver, child, f); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-04-08 00:52:48 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// pushError formats an error type given a path and an error
|
|
|
|
// and pushes it to a slice of errors
|
|
|
|
func pushError(errors []error, path string, err error) []error {
|
|
|
|
return append(errors, fmt.Errorf("%s: %s", path, err))
|
|
|
|
}
|