Don't add hard links size twice
This commit is contained in:
parent
f68965e7c9
commit
51c2d40639
|
@ -1,6 +1,7 @@
|
||||||
package filesystem
|
package filesystem
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"slices"
|
||||||
"sync"
|
"sync"
|
||||||
"sync/atomic"
|
"sync/atomic"
|
||||||
"syscall"
|
"syscall"
|
||||||
|
@ -166,6 +167,8 @@ func (fs *Filesystem) DirectorySize(dir string) (int64, error) {
|
||||||
var size int64
|
var size int64
|
||||||
var st syscall.Stat_t
|
var st syscall.Stat_t
|
||||||
|
|
||||||
|
var hardLinks []uint64
|
||||||
|
|
||||||
err = godirwalk.Walk(d, &godirwalk.Options{
|
err = godirwalk.Walk(d, &godirwalk.Options{
|
||||||
Unsorted: true,
|
Unsorted: true,
|
||||||
Callback: func(p string, e *godirwalk.Dirent) error {
|
Callback: func(p string, e *godirwalk.Dirent) error {
|
||||||
|
@ -184,6 +187,16 @@ func (fs *Filesystem) DirectorySize(dir string) (int64, error) {
|
||||||
|
|
||||||
if !e.IsDir() {
|
if !e.IsDir() {
|
||||||
_ = syscall.Lstat(p, &st)
|
_ = 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)
|
atomic.AddInt64(&size, st.Size)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user