diff --git a/cmd/buildkitd/config/gcpolicy_windows.go b/cmd/buildkitd/config/gcpolicy_windows.go index 55ce4dd77278c..687d9539c9086 100644 --- a/cmd/buildkitd/config/gcpolicy_windows.go +++ b/cmd/buildkitd/config/gcpolicy_windows.go @@ -3,10 +3,36 @@ package config +import "golang.org/x/sys/windows" + func DetectDefaultGCCap() DiskSpace { - return DiskSpace{Bytes: defaultCap} + return DiskSpace{Percentage: 10} } func (d DiskSpace) AsBytes(root string) int64 { - return d.Bytes + if d.Bytes != 0 { + return d.Bytes + } + if d.Percentage == 0 { + return 0 + } + + rootUTF16, err := windows.UTF16FromString(root) + if err != nil { + return defaultCap + } + var freeAvailableBytes uint64 + var totalBytes uint64 + var totalFreeBytes uint64 + + if err := windows.GetDiskFreeSpaceEx( + &rootUTF16[0], + &freeAvailableBytes, + &totalBytes, + &totalFreeBytes); err != nil { + return defaultCap + } + + avail := int64(totalBytes) * d.Percentage / 100 + return avail }