pkg/directory: minor refactor of Size()
- separate exported function from implementation, to allow for GoDoc to be
maintained in a single location.
- don't use named return variables (no "bare" return, and potentially shadowing
variables)
- reverse the `os.IsNotExist(err) && d != dir` condition, putting the "lighter"
`d != dir` first.
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
(cherry picked from commit bd6217bb74
)
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
parent
e0b105623e
commit
92b96ac2ed
3 changed files with 20 additions and 12 deletions
|
@ -1,6 +1,7 @@
|
|||
package directory // import "github.com/docker/docker/pkg/directory"
|
||||
|
||||
import (
|
||||
"context"
|
||||
"os"
|
||||
"path/filepath"
|
||||
)
|
||||
|
@ -22,3 +23,8 @@ func MoveToSubdir(oldpath, subdir string) error {
|
|||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Size walks a directory tree and returns its total size in bytes.
|
||||
func Size(ctx context.Context, dir string) (int64, error) {
|
||||
return calcSize(ctx, dir)
|
||||
}
|
||||
|
|
|
@ -10,14 +10,15 @@ import (
|
|||
"syscall"
|
||||
)
|
||||
|
||||
// Size walks a directory tree and returns its total size in bytes.
|
||||
func Size(ctx context.Context, dir string) (size int64, err error) {
|
||||
// calcSize walks a directory tree and returns its total size in bytes.
|
||||
func calcSize(ctx context.Context, dir string) (int64, error) {
|
||||
var size int64
|
||||
data := make(map[uint64]struct{})
|
||||
err = filepath.Walk(dir, func(d string, fileInfo os.FileInfo, err error) error {
|
||||
err := filepath.Walk(dir, func(d string, fileInfo os.FileInfo, err error) error {
|
||||
if err != nil {
|
||||
// if dir does not exist, Size() returns the error.
|
||||
// if dir/x disappeared while walking, Size() ignores dir/x.
|
||||
if os.IsNotExist(err) && d != dir {
|
||||
// if dir does not exist, Size() returns the error.
|
||||
if d != dir && os.IsNotExist(err) {
|
||||
return nil
|
||||
}
|
||||
return err
|
||||
|
@ -51,5 +52,5 @@ func Size(ctx context.Context, dir string) (size int64, err error) {
|
|||
|
||||
return nil
|
||||
})
|
||||
return
|
||||
return size, err
|
||||
}
|
||||
|
|
|
@ -6,13 +6,14 @@ import (
|
|||
"path/filepath"
|
||||
)
|
||||
|
||||
// Size walks a directory tree and returns its total size in bytes.
|
||||
func Size(ctx context.Context, dir string) (size int64, err error) {
|
||||
err = filepath.Walk(dir, func(d string, fileInfo os.FileInfo, err error) error {
|
||||
// calcSize walks a directory tree and returns its total calcSize in bytes.
|
||||
func calcSize(ctx context.Context, dir string) (int64, error) {
|
||||
var size int64
|
||||
err := filepath.Walk(dir, func(d string, fileInfo os.FileInfo, err error) error {
|
||||
if err != nil {
|
||||
// if dir does not exist, Size() returns the error.
|
||||
// if dir/x disappeared while walking, Size() ignores dir/x.
|
||||
if os.IsNotExist(err) && d != dir {
|
||||
// if dir does not exist, Size() returns the error.
|
||||
if d != dir && os.IsNotExist(err) {
|
||||
return nil
|
||||
}
|
||||
return err
|
||||
|
@ -38,5 +39,5 @@ func Size(ctx context.Context, dir string) (size int64, err error) {
|
|||
|
||||
return nil
|
||||
})
|
||||
return
|
||||
return size, err
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue