Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix disk space check for windows #722

Merged
merged 1 commit into from
Jan 30, 2025
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
36 changes: 16 additions & 20 deletions libwallet/utils/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"regexp"
"runtime"
"strings"
"syscall"
"time"

"gioui.org/app"
Expand Down Expand Up @@ -194,31 +193,28 @@ func GetFreeDiskSpace() (uint64, error) {
var path string
var err error
switch runtime.GOOS {
case "android", "ios":
path, err = app.DataDir()
if err != nil {
return 0, err
case "android", "ios", "linux", "darwin", "windows":
if runtime.GOOS == "android" || runtime.GOOS == "ios" {
path, err = app.DataDir()
if err != nil {
return 0, err
}
} else {
path, err = os.UserHomeDir()
if err != nil {
return 0, err
}
}
case "linux", "darwin", "windows":
path, err = os.UserHomeDir()

freeSpace, err := DiskSpace(path)
if err != nil {
return 0, err
}
default:
return 0, nil
}

var stat syscall.Statfs_t
return freeSpace, nil

// Get file system statistics for the specified path
err = syscall.Statfs(path, &stat)
if err != nil {
return 0, err
default:
return 0, nil
}

// Calculate available space: free blocks * block size
freeBytes := stat.Bavail * uint64(stat.Bsize)

// Convert bytes to MB
return freeBytes / (1024 * 1024), nil
}
26 changes: 26 additions & 0 deletions libwallet/utils/disk_general.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
//go:build linux || android || ios || darwin
// +build linux android ios darwin

package utils

import (
"syscall"
)

// DiskSpace returns the available disk space in MB for the specified path
func DiskSpace(path string) (uint64, error) {

var stat syscall.Statfs_t

// Get file system statistics for the specified path
err := syscall.Statfs(path, &stat)
if err != nil {
return 0, err
}

// Calculate available space: free blocks * block size
freeBytes := stat.Bavail * uint64(stat.Bsize)

// Convert bytes to MB
return freeBytes / (1024 * 1024), nil
}
30 changes: 30 additions & 0 deletions libwallet/utils/disk_windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
//go:build windows
// +build windows

package utils

import (
"golang.org/x/sys/windows"
)

// DiskSpace returns the total and free disk space in bytes for the specified directory
func DiskSpace(dir string) (free uint64, err error) {
var (
directoryName = windows.StringToUTF16Ptr(dir)
freeBytesAvailableToCaller uint64
totalNumberOfBytes uint64
totalNumberOfFreeBytes uint64
)
err = windows.GetDiskFreeSpaceEx(
directoryName,
&freeBytesAvailableToCaller,
&totalNumberOfBytes,
&totalNumberOfFreeBytes,
)
if err != nil {
return 0, err
}

// Convert bytes to MB
return totalNumberOfFreeBytes / (1024 * 1024), nil
}
Loading