From 8dbfaae1b662d0516751cc2e2bbbd681aa89dcca Mon Sep 17 00:00:00 2001
From: partisan <none@noone.no>
Date: Sat, 31 May 2025 21:44:31 +0200
Subject: [PATCH] Fixed windows compatibility

---
 .gitignore  |  1 +
 config.go   | 23 -----------------------
 disk.go     | 27 +++++++++++++++++++++++++++
 disk_win.go | 36 ++++++++++++++++++++++++++++++++++++
 4 files changed, 64 insertions(+), 23 deletions(-)
 create mode 100644 disk.go
 create mode 100644 disk_win.go

diff --git a/.gitignore b/.gitignore
index 5f5aeab..c731c6b 100644
--- a/.gitignore
+++ b/.gitignore
@@ -6,4 +6,5 @@ cache/
 *.min.js
 *.min.css
 qgato
+qgato.exe
 test.py
\ No newline at end of file
diff --git a/config.go b/config.go
index 0cda67d..c9f40b6 100644
--- a/config.go
+++ b/config.go
@@ -4,10 +4,8 @@ import (
 	"bufio"
 	"fmt"
 	"os"
-	"path/filepath"
 	"strconv"
 	"strings"
-	"syscall"
 	"time"
 
 	"github.com/shirou/gopsutil/mem"
@@ -537,27 +535,6 @@ func parseMaxUsageDrive(value string, cachePath string) uint64 {
 	return 0
 }
 
-// Get total disk space of the system where cachePath resides
-func getTotalDiskSpace(cachePath string) uint64 {
-	var stat syscall.Statfs_t
-
-	// Get filesystem stats for the cache path
-	absPath, err := filepath.Abs(cachePath)
-	if err != nil {
-		printErr("Failed to resolve absolute path for: %s", cachePath)
-		return 0
-	}
-
-	err = syscall.Statfs(absPath, &stat)
-	if err != nil {
-		printErr("Failed to retrieve filesystem stats for: %s", absPath)
-		return 0
-	}
-
-	// Total disk space in bytes
-	return stat.Blocks * uint64(stat.Bsize)
-}
-
 // Helper to format bytes back to human-readable string
 func formatMaxUsage(bytes uint64) string {
 	const GiB = 1024 * 1024 * 1024
diff --git a/disk.go b/disk.go
new file mode 100644
index 0000000..0eafde9
--- /dev/null
+++ b/disk.go
@@ -0,0 +1,27 @@
+//go:build !windows
+// +build !windows
+
+package main
+
+import (
+	"path/filepath"
+	"syscall"
+)
+
+func getTotalDiskSpace(cachePath string) uint64 {
+	var stat syscall.Statfs_t
+
+	absPath, err := filepath.Abs(cachePath)
+	if err != nil {
+		printErr("Failed to resolve absolute path for: %s", cachePath)
+		return 0
+	}
+
+	err = syscall.Statfs(absPath, &stat)
+	if err != nil {
+		printErr("Failed to retrieve filesystem stats for: %s", absPath)
+		return 0
+	}
+
+	return stat.Blocks * uint64(stat.Bsize)
+}
diff --git a/disk_win.go b/disk_win.go
new file mode 100644
index 0000000..394549d
--- /dev/null
+++ b/disk_win.go
@@ -0,0 +1,36 @@
+//go:build windows
+// +build windows
+
+package main
+
+import (
+	"syscall"
+	"unsafe"
+)
+
+func getTotalDiskSpace(path string) uint64 {
+	kernel32 := syscall.NewLazyDLL("kernel32.dll")
+	getDiskFreeSpaceExW := kernel32.NewProc("GetDiskFreeSpaceExW")
+
+	lpDirectoryName, err := syscall.UTF16PtrFromString(path)
+	if err != nil {
+		printErr("Failed to encode path for Windows API: %v", err)
+		return 0
+	}
+
+	var freeBytesAvailable, totalNumberOfBytes, totalNumberOfFreeBytes uint64
+
+	r1, _, err := getDiskFreeSpaceExW.Call(
+		uintptr(unsafe.Pointer(lpDirectoryName)),
+		uintptr(unsafe.Pointer(&freeBytesAvailable)),
+		uintptr(unsafe.Pointer(&totalNumberOfBytes)),
+		uintptr(unsafe.Pointer(&totalNumberOfFreeBytes)),
+	)
+
+	if r1 == 0 {
+		printErr("GetDiskFreeSpaceExW failed: %v", err)
+		return 0
+	}
+
+	return totalNumberOfBytes
+}