github.com/anuvu/storage@v1.12.13/pkg/directory/directory_windows.go (about) 1 // +build windows 2 3 package directory 4 5 import ( 6 "os" 7 "path/filepath" 8 ) 9 10 // Size walks a directory tree and returns its total size in bytes. 11 func Size(dir string) (size int64, err error) { 12 err = filepath.Walk(dir, func(d string, fileInfo os.FileInfo, err error) error { 13 if err != nil { 14 // if dir does not exist, Size() returns the error. 15 // if dir/x disappeared while walking, Size() ignores dir/x. 16 if os.IsNotExist(err) && d != dir { 17 return nil 18 } 19 return err 20 } 21 22 // Ignore directory sizes 23 if fileInfo == nil { 24 return nil 25 } 26 27 s := fileInfo.Size() 28 if fileInfo.IsDir() || s == 0 { 29 return nil 30 } 31 32 size += s 33 34 return nil 35 }) 36 return 37 }