Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion internal/stat.go
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ func find(dir string, filter func(info fs.FileInfo) bool) ([]*file, error) {
if !entry.IsDir() {
fileChan <- &file{
name: entry.Name(),
size: fileInfo.Size(),
size: diskSize(fileInfo, filepath.Join(dir, entry.Name())),
}
continue
}
Expand Down
15 changes: 15 additions & 0 deletions internal/sys_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,21 @@

package internal

import (
"os"
"syscall"
)

func sysFilter(dir string) bool {
return "/dev" != dir
}

// diskSize returns the actual number of bytes allocated on disk for the file,
// i.e. allocated blocks (st_blocks * 512), matching `du`'s default behavior.
// For sparse files this is smaller than the apparent logical size.
func diskSize(info os.FileInfo, _ string) int64 {
if stat, ok := info.Sys().(*syscall.Stat_t); ok {
return stat.Blocks * 512 // st_blocks is always in 512-byte units (POSIX)
}
return info.Size()
}
15 changes: 15 additions & 0 deletions internal/sys_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,21 @@

package internal

import (
"os"
"syscall"
)

func sysFilter(dir string) bool {
return dir != "/proc"
}

// diskSize returns the actual number of bytes allocated on disk for the file,
// i.e. allocated blocks (st_blocks * 512), matching `du`'s default behavior.
// For sparse files this is smaller than the apparent logical size.
func diskSize(info os.FileInfo, _ string) int64 {
if stat, ok := info.Sys().(*syscall.Stat_t); ok {
return stat.Blocks * 512 // st_blocks is always in 512-byte units (POSIX)
}
return info.Size()
}
33 changes: 33 additions & 0 deletions internal/sys_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,39 @@

package internal

import (
"os"
"syscall"
"unsafe"
)

var (
modKernel32 = syscall.NewLazyDLL("kernel32.dll")
procGetCompressedFileSizeW = modKernel32.NewProc("GetCompressedFileSizeW")
)

func sysFilter(_ string) bool {
return true
}

// diskSize returns the actual number of bytes allocated on disk for the file
// via GetCompressedFileSizeW, matching `du`'s default behavior. For sparse or
// compressed files this excludes unallocated holes, so it is smaller than the
// apparent logical size returned by os.FileInfo.Size().
func diskSize(info os.FileInfo, name string) int64 {
p, err := syscall.UTF16PtrFromString(name)
if err != nil {
return info.Size()
}

var high uint32
low, _, _ := procGetCompressedFileSizeW.Call(
uintptr(unsafe.Pointer(p)),
uintptr(unsafe.Pointer(&high)),
)
if uint32(low) == 0xFFFFFFFF { // INVALID_FILE_SIZE → call failed
return info.Size()
}

return int64(uint64(high)<<32 | uint64(uint32(low)))
}
Loading