From 48a589eb79b715e94759bb397ecdf24eb59fe058 Mon Sep 17 00:00:00 2001 From: GitCaddy Date: Sun, 11 Jan 2026 19:29:27 +0000 Subject: [PATCH] fix: add cross-platform disk detection for Windows/macOS builds MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Split detectDiskSpace() into platform-specific files with build tags - disk_unix.go: Uses unix.Statfs for Linux and macOS - disk_windows.go: Uses windows.GetDiskFreeSpaceEx for Windows - Fixes Windows cross-compilation build errors 🤖 Generated with Claude Code --- internal/pkg/envcheck/capabilities.go | 28 ----------------------- internal/pkg/envcheck/disk_unix.go | 32 +++++++++++++++++++++++++++ internal/pkg/envcheck/disk_windows.go | 31 ++++++++++++++++++++++++++ 3 files changed, 63 insertions(+), 28 deletions(-) create mode 100644 internal/pkg/envcheck/disk_unix.go create mode 100644 internal/pkg/envcheck/disk_windows.go diff --git a/internal/pkg/envcheck/capabilities.go b/internal/pkg/envcheck/capabilities.go index d5b7da7..36e66ff 100644 --- a/internal/pkg/envcheck/capabilities.go +++ b/internal/pkg/envcheck/capabilities.go @@ -14,7 +14,6 @@ import ( "time" "github.com/docker/docker/client" - "golang.org/x/sys/unix" ) // DiskInfo holds disk space information @@ -164,33 +163,6 @@ func generateSuggestedLabels(cap *RunnerCapabilities) []string { return labels } -// detectDiskSpace detects disk space on the root filesystem -func detectDiskSpace() *DiskInfo { - var stat unix.Statfs_t - - // Get stats for root filesystem (or current working directory) - path := "/" - if runtime.GOOS == "windows" { - path = "C:\\" - } - - err := unix.Statfs(path, &stat) - if err != nil { - return nil - } - - total := stat.Blocks * uint64(stat.Bsize) - free := stat.Bavail * uint64(stat.Bsize) - used := total - free - usedPercent := float64(used) / float64(total) * 100 - - return &DiskInfo{ - Total: total, - Free: free, - Used: used, - UsedPercent: usedPercent, - } -} // ToJSON converts capabilities to JSON string for transmission func (c *RunnerCapabilities) ToJSON() string { diff --git a/internal/pkg/envcheck/disk_unix.go b/internal/pkg/envcheck/disk_unix.go new file mode 100644 index 0000000..cc10e16 --- /dev/null +++ b/internal/pkg/envcheck/disk_unix.go @@ -0,0 +1,32 @@ +//go:build unix + +// Copyright 2026 The Gitea Authors. All rights reserved. +// SPDX-License-Identifier: MIT + +package envcheck + +import ( + "golang.org/x/sys/unix" +) + +// detectDiskSpace detects disk space on the root filesystem (Unix version) +func detectDiskSpace() *DiskInfo { + var stat unix.Statfs_t + + err := unix.Statfs("/", &stat) + if err != nil { + return nil + } + + total := stat.Blocks * uint64(stat.Bsize) + free := stat.Bavail * uint64(stat.Bsize) + used := total - free + usedPercent := float64(used) / float64(total) * 100 + + return &DiskInfo{ + Total: total, + Free: free, + Used: used, + UsedPercent: usedPercent, + } +} diff --git a/internal/pkg/envcheck/disk_windows.go b/internal/pkg/envcheck/disk_windows.go new file mode 100644 index 0000000..06ac06a --- /dev/null +++ b/internal/pkg/envcheck/disk_windows.go @@ -0,0 +1,31 @@ +//go:build windows + +// Copyright 2026 The Gitea Authors. All rights reserved. +// SPDX-License-Identifier: MIT + +package envcheck + +import ( + "golang.org/x/sys/windows" +) + +// detectDiskSpace detects disk space on the C: drive (Windows version) +func detectDiskSpace() *DiskInfo { + var freeBytesAvailable, totalNumberOfBytes, totalNumberOfFreeBytes uint64 + + path := windows.StringToUTF16Ptr("C:\\") + err := windows.GetDiskFreeSpaceEx(path, &freeBytesAvailable, &totalNumberOfBytes, &totalNumberOfFreeBytes) + if err != nil { + return nil + } + + used := totalNumberOfBytes - totalNumberOfFreeBytes + usedPercent := float64(used) / float64(totalNumberOfBytes) * 100 + + return &DiskInfo{ + Total: totalNumberOfBytes, + Free: totalNumberOfFreeBytes, + Used: used, + UsedPercent: usedPercent, + } +}