Created
August 25, 2021 14:35
-
-
Save thomasdullien/72189200a1b58fa3f248547f988e5721 to your computer and use it in GitHub Desktop.
GetDirUsage from fs.go doing recursive directory walks
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func GetDirUsage(dir string) (UsageInfo, error) { | |
var usage UsageInfo | |
if dir == "" { | |
return usage, fmt.Errorf("invalid directory") | |
} | |
rootInfo, err := os.Stat(dir) | |
if err != nil { | |
return usage, fmt.Errorf("could not stat %q to get inode usage: %v", dir, err) | |
} | |
rootStat, ok := rootInfo.Sys().(*syscall.Stat_t) | |
if !ok { | |
return usage, fmt.Errorf("unsuported fileinfo for getting inode usage of %q", dir) | |
} | |
rootDevID := rootStat.Dev | |
// dedupedInode stores inodes that could be duplicates (nlink > 1) | |
dedupedInodes := make(map[uint64]struct{}) | |
err = filepath.Walk(dir, func(path string, info os.FileInfo, err error) error { |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment