1
0
Fork 0
mirror of https://github.com/moby/moby.git synced 2022-11-09 12:21:53 -05:00

Fix filepath.Walk misusage in pkg/directory

Signed-off-by: Akihiro Suda <suda.akihiro@lab.ntt.co.jp>
This commit is contained in:
Akihiro Suda 2016-05-27 02:47:32 +00:00
parent 29dbcbad87
commit cd53ec5244
3 changed files with 27 additions and 2 deletions

View file

@ -183,3 +183,10 @@ func TestMoveToSubdir(t *testing.T) {
t.Fatalf("Results after migration do not equal list of files: expected: %v, got: %v", filesList, results)
}
}
// Test a non-existing directory
func TestSizeNonExistingDirectory(t *testing.T) {
if _, err := Size("/thisdirectoryshouldnotexist/TestSizeNonExistingDirectory"); err == nil {
t.Fatalf("error is expected")
}
}

View file

@ -11,7 +11,16 @@ import (
// Size walks a directory tree and returns its total size in bytes.
func Size(dir string) (size int64, err error) {
data := make(map[uint64]struct{})
err = filepath.Walk(dir, func(d string, fileInfo os.FileInfo, e 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 {
return nil
}
return err
}
// Ignore directory sizes
if fileInfo == nil {
return nil

View file

@ -9,7 +9,16 @@ import (
// Size walks a directory tree and returns its total size in bytes.
func Size(dir string) (size int64, err error) {
err = filepath.Walk(dir, func(d string, fileInfo os.FileInfo, e 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 {
return nil
}
return err
}
// Ignore directory sizes
if fileInfo == nil {
return nil