github.com/demonoid81/moby@v0.0.0-20200517203328-62dd8e17c460/pkg/directory/directory_windows.go (about) 1 package directory // import "github.com/demonoid81/moby/pkg/directory" 2 3 import ( 4 "context" 5 "os" 6 "path/filepath" 7 ) 8 9 // Size walks a directory tree and returns its total size in bytes. 10 func Size(ctx context.Context, dir string) (size int64, err error) { 11 err = filepath.Walk(dir, func(d string, fileInfo os.FileInfo, err error) error { 12 if err != nil { 13 // if dir does not exist, Size() returns the error. 14 // if dir/x disappeared while walking, Size() ignores dir/x. 15 if os.IsNotExist(err) && d != dir { 16 return nil 17 } 18 return err 19 } 20 21 select { 22 case <-ctx.Done(): 23 return ctx.Err() 24 default: 25 } 26 27 // Ignore directory sizes 28 if fileInfo == nil { 29 return nil 30 } 31 32 s := fileInfo.Size() 33 if fileInfo.IsDir() || s == 0 { 34 return nil 35 } 36 37 size += s 38 39 return nil 40 }) 41 return 42 }