performance_windows.go 1.0 KB

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