Skip to content

Commit

Permalink
fix: only count hard-links once when calculating filesystem usage (pt…
Browse files Browse the repository at this point in the history
  • Loading branch information
EpicPlayerA10 authored and dannyhpy committed Jul 5, 2024
1 parent 39e55e7 commit 0649892
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions server/filesystem/disk_space.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package filesystem

import (
"golang.org/x/sys/unix"
"slices"
"sync"
"sync/atomic"
"time"
Expand Down Expand Up @@ -164,6 +166,8 @@ func (fs *Filesystem) DirectorySize(root string) (int64, error) {
return 0, err
}

var hardLinks []uint64

var size atomic.Int64
err = fs.unixFS.WalkDirat(dirfd, name, func(dirfd int, name, _ string, d ufs.DirEntry, err error) error {
if err != nil {
Expand All @@ -180,8 +184,16 @@ func (fs *Filesystem) DirectorySize(root string) (int64, error) {
return errors.Wrap(err, "lstatat err")
}

// TODO: detect if info is a hard-link and de-duplicate it.
// ref; https://github.com/pterodactyl/wings/pull/181/files
var sysFileInfo = info.Sys().(*unix.Stat_t)
if sysFileInfo.Nlink > 1 {
// Hard links have the same inode number
if slices.Contains(hardLinks, sysFileInfo.Ino) {
// Don't add hard links size twice
return nil
} else {
hardLinks = append(hardLinks, sysFileInfo.Ino)
}
}

size.Add(info.Size())
return nil
Expand Down

0 comments on commit 0649892

Please sign in to comment.