fix: nLink is unreliable on all filesystems (#19187)

ext4, xfs support this behavior however
btrfs, nfs may not support it properly.

in-case when we see Nlink < 2 then we know
that we need to fallback on readdir()

fixes a regression from #19100

fixes #19181
This commit is contained in:
Harshavardhana 2024-03-04 15:58:35 -08:00 committed by GitHub
parent 9a4d003ac7
commit e385f54185
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -20,7 +20,9 @@
package cmd package cmd
import "syscall" import (
"syscall"
)
// Returns true if no error and there is no object or prefix inside this directory // Returns true if no error and there is no object or prefix inside this directory
func isDirEmpty(dirname string) bool { func isDirEmpty(dirname string) bool {
@ -28,5 +30,17 @@ func isDirEmpty(dirname string) bool {
if err := syscall.Stat(dirname, &stat); err != nil { if err := syscall.Stat(dirname, &stat); err != nil {
return false return false
} }
return stat.Mode&syscall.S_IFMT == syscall.S_IFDIR && stat.Nlink < 3 if stat.Mode&syscall.S_IFMT == syscall.S_IFDIR && stat.Nlink == 2 {
return true
}
// On filesystems such as btrfs, nfs this is not true, so fallback
// to performing readdir() instead.
if stat.Mode&syscall.S_IFMT == syscall.S_IFDIR && stat.Nlink < 2 {
entries, err := readDirN(dirname, 1)
if err != nil {
return false
}
return len(entries) == 0
}
return false
} }