Skip to content

Instantly share code, notes, and snippets.

@thomasdullien
Created August 25, 2021 14:35
Show Gist options
  • Save thomasdullien/72189200a1b58fa3f248547f988e5721 to your computer and use it in GitHub Desktop.
Save thomasdullien/72189200a1b58fa3f248547f988e5721 to your computer and use it in GitHub Desktop.
GetDirUsage from fs.go doing recursive directory walks
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