Files
overlord/Overlord-Client/cmd/agent/handlers/files_windows.go
T
2026-05-21 09:51:12 -05:00

111 lines
2.2 KiB
Go

//go:build windows
// +build windows
package handlers
import (
"fmt"
"os"
"os/exec"
"syscall"
"unsafe"
"overlord-client/cmd/agent/wire"
)
func DiskUsage(path string) (int64, int64, string, bool) {
kernel32 := syscall.NewLazyDLL("kernel32.dll")
getDiskFreeSpaceExW := kernel32.NewProc("GetDiskFreeSpaceExW")
getVolumeInformationW := kernel32.NewProc("GetVolumeInformationW")
root, err := syscall.UTF16PtrFromString(path)
if err != nil {
return 0, 0, "", false
}
var freeAvail, totalBytes, totalFree uint64
r1, _, _ := getDiskFreeSpaceExW.Call(
uintptr(unsafe.Pointer(root)),
uintptr(unsafe.Pointer(&freeAvail)),
uintptr(unsafe.Pointer(&totalBytes)),
uintptr(unsafe.Pointer(&totalFree)),
)
if r1 == 0 {
return 0, 0, "", false
}
fsType := ""
var fsNameBuf [32]uint16
r2, _, _ := getVolumeInformationW.Call(
uintptr(unsafe.Pointer(root)),
0, 0, 0, 0, 0,
uintptr(unsafe.Pointer(&fsNameBuf[0])),
uintptr(len(fsNameBuf)),
)
if r2 != 0 {
fsType = syscall.UTF16ToString(fsNameBuf[:])
}
return int64(freeAvail), int64(totalBytes), fsType, true
}
func enrichFileEntry(entry *wire.FileEntry, info os.FileInfo) {
if d, ok := info.Sys().(*syscall.Win32FileAttributeData); ok {
entry.Attrs = d.FileAttributes
}
}
func ChangeFilePermissions(path string, mode string) error {
return fmt.Errorf("chmod not supported on Windows")
}
func ExecuteFile(path string) error {
ext := ""
lastDot := -1
for i := len(path) - 1; i >= 0; i-- {
if path[i] == '.' {
lastDot = i
break
}
if path[i] == '/' || path[i] == '\\' {
break
}
}
if lastDot >= 0 {
ext = path[lastDot:]
}
if _, err := os.Stat(path); err != nil {
return fmt.Errorf("file not found: %s", path)
}
switch ext {
case ".exe", ".com":
return execCommand(path)
case ".bat", ".cmd":
return execCommand("cmd.exe", "/c", path)
case ".ps1":
return execCommand("powershell.exe", "-ExecutionPolicy", "Bypass", "-File", path)
case ".vbs":
return execCommand("wscript.exe", path)
case ".py":
return execCommand("python", path)
case ".js":
return execCommand("node", path)
default:
return fmt.Errorf("unsupported file type: %s", ext)
}
}
func execCommand(name string, args ...string) error {
cmd := exec.Command(name, args...)
return cmd.Start()
}