From 51c2d40639be9c33bac397afded8042668c35856 Mon Sep 17 00:00:00 2001 From: EpicPlayerA10 Date: Thu, 29 Feb 2024 17:10:17 +0100 Subject: [PATCH] Don't add hard links size twice --- server/filesystem/disk_space.go | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/server/filesystem/disk_space.go b/server/filesystem/disk_space.go index f7f2c7b..30b0f99 100644 --- a/server/filesystem/disk_space.go +++ b/server/filesystem/disk_space.go @@ -1,6 +1,7 @@ package filesystem import ( + "slices" "sync" "sync/atomic" "syscall" @@ -166,6 +167,8 @@ func (fs *Filesystem) DirectorySize(dir string) (int64, error) { var size int64 var st syscall.Stat_t + var hardLinks []uint64 + err = godirwalk.Walk(d, &godirwalk.Options{ Unsorted: true, Callback: func(p string, e *godirwalk.Dirent) error { @@ -184,6 +187,16 @@ func (fs *Filesystem) DirectorySize(dir string) (int64, error) { if !e.IsDir() { _ = syscall.Lstat(p, &st) + + // Hard links have the same inode number + if slices.Contains(hardLinks, st.Ino) { + // Don't add hard links size twice + return godirwalk.SkipThis + } + if st.Nlink > 1 { + hardLinks = append(hardLinks, st.Ino) + } + atomic.AddInt64(&size, st.Size) }