Skip to content

Commit

Permalink
Fix 'OpenVirtualDiskParameters' BOOL fields
Browse files Browse the repository at this point in the history
While reworking the vhd package I'd mistakingly replaced some of the fields
in the OpenVersion2 structure with the incorrect types. They're defined as
Windows BOOLS which is just a type alias for an int, and I'd put their type as
go bools.

As the type is already passed into some functions, to avoid a breaking change just
convert the incorrect type to a new structure with the correct definition internally.
Truthfully this turns out a bit better as supplying a bool makes more sense and is
more go friendly.

Signed-off-by: Daniel Canter <dcanter@microsoft.com>
  • Loading branch information
dcantah committed Dec 1, 2021
1 parent 60c1574 commit d61cb15
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 9 deletions.
42 changes: 35 additions & 7 deletions vhd/vhd.go
@@ -1,3 +1,4 @@
//go:build windows
// +build windows

package vhd
Expand All @@ -14,7 +15,7 @@ import (
//go:generate go run mksyscall_windows.go -output zvhd_windows.go vhd.go

//sys createVirtualDisk(virtualStorageType *VirtualStorageType, path string, virtualDiskAccessMask uint32, securityDescriptor *uintptr, createVirtualDiskFlags uint32, providerSpecificFlags uint32, parameters *CreateVirtualDiskParameters, overlapped *syscall.Overlapped, handle *syscall.Handle) (win32err error) = virtdisk.CreateVirtualDisk
//sys openVirtualDisk(virtualStorageType *VirtualStorageType, path string, virtualDiskAccessMask uint32, openVirtualDiskFlags uint32, parameters *OpenVirtualDiskParameters, handle *syscall.Handle) (win32err error) = virtdisk.OpenVirtualDisk
//sys openVirtualDisk(virtualStorageType *VirtualStorageType, path string, virtualDiskAccessMask uint32, openVirtualDiskFlags uint32, parameters *openVirtualDiskParameters, handle *syscall.Handle) (win32err error) = virtdisk.OpenVirtualDisk
//sys attachVirtualDisk(handle syscall.Handle, securityDescriptor *uintptr, attachVirtualDiskFlag uint32, providerSpecificFlags uint32, parameters *AttachVirtualDiskParameters, overlapped *syscall.Overlapped) (win32err error) = virtdisk.AttachVirtualDisk
//sys detachVirtualDisk(handle syscall.Handle, detachVirtualDiskFlags uint32, providerSpecificFlags uint32) (win32err error) = virtdisk.DetachVirtualDisk
//sys getVirtualDiskPhysicalPath(handle syscall.Handle, diskPathSizeInBytes *uint32, buffer *uint16) (win32err error) = virtdisk.GetVirtualDiskPhysicalPath
Expand Down Expand Up @@ -62,13 +63,27 @@ type OpenVirtualDiskParameters struct {
Version2 OpenVersion2
}

// The OpenVersion2 structure was mistakingly changed to have the GetInfoOnly and ReadOnly fields be go bools instead
// of int32's (they're defined as Windows BOOL's so int32 is the correct type). Fix this mistake here by making the right structure we need to pass
// and not exporting it. We'll translate to the correct type as needed in any of the Open___ calls.
type openVersion2 struct {
getInfoOnly int32
readOnly int32
resiliencyGUID guid.GUID
}

type openVirtualDiskParameters struct {
version uint32
version2 openVersion2
}

type AttachVersion2 struct {
RestrictedOffset uint64
RestrictedLength uint64
}

type AttachVirtualDiskParameters struct {
Version uint32 // Must always be set to 2
Version uint32
Version2 AttachVersion2
}

Expand Down Expand Up @@ -146,10 +161,7 @@ func CreateVhdx(path string, maxSizeInGb, blockSizeInMb uint32) error {
return err
}

if err := syscall.CloseHandle(handle); err != nil {
return err
}
return nil
return syscall.CloseHandle(handle)
}

// DetachVirtualDisk detaches a virtual hard disk by handle.
Expand Down Expand Up @@ -234,16 +246,32 @@ func OpenVirtualDiskWithParameters(vhdPath string, virtualDiskAccessMask Virtual
var (
handle syscall.Handle
defaultType VirtualStorageType
getInfoOnly int32
readOnly int32
)
if parameters.Version != 2 {
return handle, fmt.Errorf("only version 2 VHDs are supported, found version: %d", parameters.Version)
}
if parameters.Version2.GetInfoOnly {
getInfoOnly = 1
}
if parameters.Version2.ReadOnly {
readOnly = 1
}
params := &openVirtualDiskParameters{
version: parameters.Version,
version2: openVersion2{
getInfoOnly,
readOnly,
parameters.Version2.ResiliencyGUID,
},
}
if err := openVirtualDisk(
&defaultType,
vhdPath,
uint32(virtualDiskAccessMask),
uint32(openVirtualDiskFlags),
parameters,
params,
&handle,
); err != nil {
return 0, errors.Wrap(err, "failed to open virtual disk")
Expand Down
4 changes: 2 additions & 2 deletions vhd/zvhd_windows.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit d61cb15

Please sign in to comment.