Skip to content

Commit

Permalink
fixing the windows build issue
Browse files Browse the repository at this point in the history
- Fixed the issue with the windows build
- Added a strategy to test all build targets during PR
  • Loading branch information
cjlapao committed Jan 7, 2025
1 parent c5a7780 commit 9cca609
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 16 deletions.
9 changes: 9 additions & 0 deletions .github/workflows/pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
25 changes: 25 additions & 0 deletions src/catalog/cacheservice/disk_space_unix.go
Original file line number Diff line number Diff line change
@@ -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
}
46 changes: 46 additions & 0 deletions src/catalog/cacheservice/disk_space_windows.go
Original file line number Diff line number Diff line change
@@ -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
}
16 changes: 0 additions & 16 deletions src/catalog/cacheservice/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"os"
"path/filepath"
"strings"
"syscall"
"time"

"github.com/Parallels/prl-devops-service/basecontext"
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 9cca609

Please sign in to comment.