Skip to content

Commit

Permalink
fix: Account for hardlinks
Browse files Browse the repository at this point in the history
  • Loading branch information
matbme authored and taukakao committed Jul 30, 2024
1 parent 6a62354 commit 734e9dd
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions core/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"io/fs"
"os"
"os/exec"
"syscall"
)

var abrootDir = "/etc/abroot"
Expand Down Expand Up @@ -134,6 +135,7 @@ func getDirSize(path string) (int64, error) {
return 0, fmt.Errorf("%s is not a directory", path)
}

inodes := map[uint64]bool{}
var totalSize int64 = 0

dfs := os.DirFS(path)
Expand All @@ -143,11 +145,21 @@ func getDirSize(path string) (int64, error) {
}

if !d.IsDir() {
fileInfo, err := d.Info()
fileinfo, err := d.Info()
if err != nil {
return err
}
totalSize += fileInfo.Size()

fileinfoSys := fileinfo.Sys().(*syscall.Stat_t)
if fileinfoSys.Nlink > 1 {
if _, ok := inodes[fileinfoSys.Ino]; !ok {
totalSize += fileinfo.Size()
inodes[fileinfoSys.Ino] = true
}
} else {
totalSize += fileinfo.Size()
inodes[fileinfoSys.Ino] = true
}
}

return nil
Expand Down

0 comments on commit 734e9dd

Please sign in to comment.