0

I want to add the system call GetDiskFreeSpaceExW to the go systemcalls and extend the currently available syscalls for hcsshim: https://github.com/microsoft/hcsshim/blob/master/zsyscall_windows.go

I saw that there is a way of generating them, but it is still unclear how the syntax will be for adding another DLL and what struct types have to be used.

The current hcs.go which is used to generate the system calls for hcsshim:

// Shim for the Host Compute Service (HCS) to manage Windows Server
// containers and Hyper-V containers.

package hcs

import (
    "syscall"
)

//go:generate go run ../../mksyscall_windows.go -output zsyscall_windows.go hcs.go

//sys hcsEnumerateComputeSystems(query string, computeSystems **uint16, result **uint16) (hr error) = vmcompute.HcsEnumerateComputeSystems?
//sys hcsCreateComputeSystem(id string, configuration string, identity syscall.Handle, computeSystem *hcsSystem, result **uint16) (hr error) = vmcompute.HcsCreateComputeSystem?
//sys hcsOpenComputeSystem(id string, computeSystem *hcsSystem, result **uint16) (hr error) = vmcompute.HcsOpenComputeSystem?
//sys hcsCloseComputeSystem(computeSystem hcsSystem) (hr error) = vmcompute.HcsCloseComputeSystem?
//sys hcsStartComputeSystem(computeSystem hcsSystem, options string, result **uint16) (hr error) = vmcompute.HcsStartComputeSystem?
//sys hcsShutdownComputeSystem(computeSystem hcsSystem, options string, result **uint16) (hr error) = vmcompute.HcsShutdownComputeSystem?
//sys hcsTerminateComputeSystem(computeSystem hcsSystem, options string, result **uint16) (hr error) = vmcompute.HcsTerminateComputeSystem?
//sys hcsPauseComputeSystem(computeSystem hcsSystem, options string, result **uint16) (hr error) = vmcompute.HcsPauseComputeSystem?
//sys hcsResumeComputeSystem(computeSystem hcsSystem, options string, result **uint16) (hr error) = vmcompute.HcsResumeComputeSystem?
//sys hcsGetComputeSystemProperties(computeSystem hcsSystem, propertyQuery string, properties **uint16, result **uint16) (hr error) = vmcompute.HcsGetComputeSystemProperties?
//sys hcsModifyComputeSystem(computeSystem hcsSystem, configuration string, result **uint16) (hr error) = vmcompute.HcsModifyComputeSystem?
//sys hcsRegisterComputeSystemCallback(computeSystem hcsSystem, callback uintptr, context uintptr, callbackHandle *hcsCallback) (hr error) = vmcompute.HcsRegisterComputeSystemCallback?
//sys hcsUnregisterComputeSystemCallback(callbackHandle hcsCallback) (hr error) = vmcompute.HcsUnregisterComputeSystemCallback?

//sys hcsCreateProcess(computeSystem hcsSystem, processParameters string, processInformation *hcsProcessInformation, process *hcsProcess, result **uint16) (hr error) = vmcompute.HcsCreateProcess?
//sys hcsOpenProcess(computeSystem hcsSystem, pid uint32, process *hcsProcess, result **uint16) (hr error) = vmcompute.HcsOpenProcess?
//sys hcsCloseProcess(process hcsProcess) (hr error) = vmcompute.HcsCloseProcess?
//sys hcsTerminateProcess(process hcsProcess, result **uint16) (hr error) = vmcompute.HcsTerminateProcess?
//sys hcsSignalProcess(process hcsProcess, options string, result **uint16) (hr error) = vmcompute.HcsSignalProcess?
//sys hcsGetProcessInfo(process hcsProcess, processInformation *hcsProcessInformation, result **uint16) (hr error) = vmcompute.HcsGetProcessInfo?
//sys hcsGetProcessProperties(process hcsProcess, processProperties **uint16, result **uint16) (hr error) = vmcompute.HcsGetProcessProperties?
//sys hcsModifyProcess(process hcsProcess, settings string, result **uint16) (hr error) = vmcompute.HcsModifyProcess?
//sys hcsGetServiceProperties(propertyQuery string, properties **uint16, result **uint16) (hr error) = vmcompute.HcsGetServiceProperties?
//sys hcsRegisterProcessCallback(process hcsProcess, callback uintptr, context uintptr, callbackHandle *hcsCallback) (hr error) = vmcompute.HcsRegisterProcessCallback?
//sys hcsUnregisterProcessCallback(callbackHandle hcsCallback) (hr error) = vmcompute.HcsUnregisterProcessCallback?

//sys hcsGetDiskFreeSpaceExW(string lpDirectoryName, **uint16 lpFreeBytesAvailableToCaller, **uint16 lpTotalNumberOfBytes, **uint16 lpTotalNumberOfFreeBytes) = 

type hcsSystem syscall.Handle
type hcsProcess syscall.Handle
type hcsCallback syscall.Handle

type hcsProcessInformation struct {
    ProcessId uint32
    Reserved  uint32
    StdInput  syscall.Handle
    StdOutput syscall.Handle
    StdError  syscall.Handle
}

As result I want to have an working syscall to obtain disk usage and disk space.

semaph0r
  • 305
  • 1
  • 2
  • 12
  • Possible duplicate of [How to get total size of a drive in Windows](https://stackoverflow.com/questions/54664751/how-to-get-total-size-of-a-drive-in-windows/54666920#54666920). – icza May 10 '19 at 12:29
  • Your link is a good point, found similar code so far, thank you. To clarify my question. I want to get the system call for obtaining the disk usage to be generated like the function prototypes from above, to integrate it into the existing codebase. – semaph0r May 10 '19 at 12:38

1 Answers1

0

Create a file like this:

//go:generate mkwinsyscall -output zfree.go free.go
//sys getDiskFreeSpace(directoryName string, availableToCaller *int, numberOfBytes *int, numberOfFreeBytes *int) (err error) = GetDiskFreeSpaceExW
package main

func main() {
   var availableToCaller, numberOfBytes, numberOfFreeBytes int
   getDiskFreeSpace(
      `C:\`, &availableToCaller, &numberOfBytes, &numberOfFreeBytes,
   )
   println("availableToCaller", availableToCaller)
   println("numberOfBytes", numberOfBytes)
   println("numberOfFreeBytes", numberOfFreeBytes)
}

Then build [1]:

go mod init free
go generate
go mod tidy
go build

Alternatively, you can use the premade wrapper [2], but I prefer mine, as you don't have to worry about the string conversion.

  1. https://github.com/golang/sys/tree/master/windows/mkwinsyscall
  2. https://pkg.go.dev/golang.org/x/sys/windows#GetDiskFreeSpaceEx
Nimantha
  • 6,405
  • 6
  • 28
  • 69
Zombo
  • 1
  • 62
  • 391
  • 407