system_monitor_windows.go 1016 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. //go:build windows
  2. package common
  3. import (
  4. "os"
  5. "syscall"
  6. "unsafe"
  7. )
  8. // GetDiskSpaceInfo 获取缓存目录所在磁盘的空间信息 (Windows)
  9. func GetDiskSpaceInfo() DiskSpaceInfo {
  10. cachePath := GetDiskCachePath()
  11. if cachePath == "" {
  12. cachePath = os.TempDir()
  13. }
  14. info := DiskSpaceInfo{}
  15. kernel32 := syscall.NewLazyDLL("kernel32.dll")
  16. getDiskFreeSpaceEx := kernel32.NewProc("GetDiskFreeSpaceExW")
  17. var freeBytesAvailable, totalBytes, totalFreeBytes uint64
  18. pathPtr, err := syscall.UTF16PtrFromString(cachePath)
  19. if err != nil {
  20. return info
  21. }
  22. ret, _, _ := getDiskFreeSpaceEx.Call(
  23. uintptr(unsafe.Pointer(pathPtr)),
  24. uintptr(unsafe.Pointer(&freeBytesAvailable)),
  25. uintptr(unsafe.Pointer(&totalBytes)),
  26. uintptr(unsafe.Pointer(&totalFreeBytes)),
  27. )
  28. if ret == 0 {
  29. return info
  30. }
  31. info.Total = totalBytes
  32. info.Free = freeBytesAvailable
  33. info.Used = totalBytes - totalFreeBytes
  34. if info.Total > 0 {
  35. info.UsedPercent = float64(info.Used) / float64(info.Total) * 100
  36. }
  37. return info
  38. }