From 9cca60912807ad3a2aa8983af4a1095fb84494b9 Mon Sep 17 00:00:00 2001 From: Carlos Lapao Date: Tue, 7 Jan 2025 11:31:05 +0000 Subject: [PATCH] fixing the windows build issue - Fixed the issue with the windows build - Added a strategy to test all build targets during PR --- .github/workflows/pr.yml | 9 ++++ src/catalog/cacheservice/disk_space_unix.go | 25 ++++++++++ .../cacheservice/disk_space_windows.go | 46 +++++++++++++++++++ src/catalog/cacheservice/main.go | 16 ------- 4 files changed, 80 insertions(+), 16 deletions(-) create mode 100644 src/catalog/cacheservice/disk_space_unix.go create mode 100644 src/catalog/cacheservice/disk_space_windows.go diff --git a/.github/workflows/pr.yml b/.github/workflows/pr.yml index 18a8707..1b4d7ab 100644 --- a/.github/workflows/pr.yml +++ b/.github/workflows/pr.yml @@ -8,6 +8,15 @@ on: jobs: build: runs-on: ubuntu-latest + strategy: + fail-fast: false + matrix: + # build and publish in parallel: linux/386, linux/amd64, linux/arm64, windows/386, windows/amd64, darwin/amd64, darwin/arm64 + goos: [linux, windows, darwin] + goarch: ["386", amd64, arm64] + exclude: + - goarch: "386" + goos: darwin steps: - uses: actions/checkout@v1 - name: Setup Go 1.21.x diff --git a/src/catalog/cacheservice/disk_space_unix.go b/src/catalog/cacheservice/disk_space_unix.go new file mode 100644 index 0000000..678a60e --- /dev/null +++ b/src/catalog/cacheservice/disk_space_unix.go @@ -0,0 +1,25 @@ +//go:build linux || darwin +// +build linux darwin + +package cacheservice + +import ( + "syscall" + + "github.com/Parallels/prl-devops-service/errors" +) + +func (cs *CacheService) getFreeDiskSpace() (int64, error) { + // We will be getting the free disk space from the disk + var stat syscall.Statfs_t + err := syscall.Statfs(cs.cacheFolder, &stat) + if err != nil { + return 0, errors.NewFromErrorWithCode(err, 500) + } + + // Available blocks * size per block = available space in bytes + diskFreeSpaceInBytes := int64(stat.Bavail) * int64(stat.Bsize) + diskFreeSpaceInMb := diskFreeSpaceInBytes / (1024 * 1024) + + return diskFreeSpaceInMb, nil +} diff --git a/src/catalog/cacheservice/disk_space_windows.go b/src/catalog/cacheservice/disk_space_windows.go new file mode 100644 index 0000000..6a1a7ec --- /dev/null +++ b/src/catalog/cacheservice/disk_space_windows.go @@ -0,0 +1,46 @@ +package cacheservice + +import ( + "errors" + "syscall" + "unsafe" +) + +func (cs *CacheService) getFreeDiskSpace() (int64, error) { + // Convert Go string to UTF16 pointer + lpDirectoryName, err := syscall.UTF16PtrFromString(cs.cacheFolder) + if err != nil { + return 0, err + } + + // Load the kernel32 DLL and find the procedure + kernel32 := syscall.MustLoadDLL("kernel32.dll") + defer kernel32.Release() + + procGetDiskFreeSpaceExW := kernel32.MustFindProc("GetDiskFreeSpaceExW") + + var freeBytesAvailable, totalNumberOfBytes, totalNumberOfFreeBytes uint64 + + // Call the Windows API + r, _, callErr := procGetDiskFreeSpaceExW.Call( + uintptr(unsafe.Pointer(lpDirectoryName)), + uintptr(unsafe.Pointer(&freeBytesAvailable)), + uintptr(unsafe.Pointer(&totalNumberOfBytes)), + uintptr(unsafe.Pointer(&totalNumberOfFreeBytes)), + ) + if r == 0 { + // The call failed + if callErr != syscall.Errno(0) { + return 0, callErr + } + return 0, errors.New("GetDiskFreeSpaceExW failed, unknown error") + } + + // freeBytesAvailable is the number of bytes available to the *caller* (which + // can differ from the total free bytes if quotas are in use, but in most + // cases is the same). + // Convert bytes -> MB + diskFreeSpaceInMB := freeBytesAvailable / (1024 * 1024) + + return int64(diskFreeSpaceInMB), nil +} diff --git a/src/catalog/cacheservice/main.go b/src/catalog/cacheservice/main.go index 6697c2d..f1d75b8 100644 --- a/src/catalog/cacheservice/main.go +++ b/src/catalog/cacheservice/main.go @@ -6,7 +6,6 @@ import ( "os" "path/filepath" "strings" - "syscall" "time" "github.com/Parallels/prl-devops-service/basecontext" @@ -150,21 +149,6 @@ func (cs *CacheService) notify(message string) { cs.notifications.NotifyInfo(message) } -func (cs *CacheService) getFreeDiskSpace() (int64, error) { - // We will be getting the free disk space from the disk - var stat syscall.Statfs_t - err := syscall.Statfs(cs.cacheFolder, &stat) - if err != nil { - return 0, errors.NewFromErrorWithCode(err, 500) - } - - // Available blocks * size per block = available space in bytes - diskFreeSpaceInBytes := int64(stat.Bavail) * int64(stat.Bsize) - diskFreeSpaceInMb := diskFreeSpaceInBytes / (1024 * 1024) - - return diskFreeSpaceInMb, nil -} - func (cs *CacheService) getCacheTotalSize() (int64, error) { // We will be getting the total size of the cache folder totalSize, err := helpers.DirSize(cs.cacheFolder)