From daf865b1a25c4a7d46dc641f92a90fbc74a6fb8a Mon Sep 17 00:00:00 2001 From: Yevhen Vydolob Date: Wed, 22 Oct 2025 11:03:51 +0300 Subject: [PATCH 1/4] Update dependencies Signed-off-by: Yevhen Vydolob --- go.mod | 1 - go.sum | 3 - vendor/github.com/go-ole/go-ole/.travis.yml | 8 - vendor/github.com/go-ole/go-ole/ChangeLog.md | 49 --- vendor/github.com/go-ole/go-ole/LICENSE | 21 - vendor/github.com/go-ole/go-ole/README.md | 46 --- vendor/github.com/go-ole/go-ole/SECURITY.md | 13 - vendor/github.com/go-ole/go-ole/appveyor.yml | 68 --- vendor/github.com/go-ole/go-ole/com.go | 386 ------------------ vendor/github.com/go-ole/go-ole/com_func.go | 174 -------- vendor/github.com/go-ole/go-ole/connect.go | 192 --------- vendor/github.com/go-ole/go-ole/constants.go | 153 ------- vendor/github.com/go-ole/go-ole/error.go | 51 --- vendor/github.com/go-ole/go-ole/error_func.go | 8 - .../github.com/go-ole/go-ole/error_windows.go | 24 -- vendor/github.com/go-ole/go-ole/guid.go | 284 ------------- .../go-ole/go-ole/iconnectionpoint.go | 20 - .../go-ole/go-ole/iconnectionpoint_func.go | 21 - .../go-ole/go-ole/iconnectionpoint_windows.go | 43 -- .../go-ole/iconnectionpointcontainer.go | 17 - .../go-ole/iconnectionpointcontainer_func.go | 11 - .../iconnectionpointcontainer_windows.go | 25 -- vendor/github.com/go-ole/go-ole/idispatch.go | 94 ----- .../go-ole/go-ole/idispatch_func.go | 19 - .../go-ole/go-ole/idispatch_windows.go | 203 --------- .../github.com/go-ole/go-ole/ienumvariant.go | 19 - .../go-ole/go-ole/ienumvariant_func.go | 19 - .../go-ole/go-ole/ienumvariant_windows.go | 63 --- .../github.com/go-ole/go-ole/iinspectable.go | 18 - .../go-ole/go-ole/iinspectable_func.go | 15 - .../go-ole/go-ole/iinspectable_windows.go | 72 ---- .../go-ole/go-ole/iprovideclassinfo.go | 21 - .../go-ole/go-ole/iprovideclassinfo_func.go | 7 - .../go-ole/iprovideclassinfo_windows.go | 21 - vendor/github.com/go-ole/go-ole/itypeinfo.go | 34 -- .../go-ole/go-ole/itypeinfo_func.go | 7 - .../go-ole/go-ole/itypeinfo_windows.go | 21 - vendor/github.com/go-ole/go-ole/iunknown.go | 57 --- .../github.com/go-ole/go-ole/iunknown_func.go | 19 - .../go-ole/go-ole/iunknown_windows.go | 58 --- vendor/github.com/go-ole/go-ole/ole.go | 190 --------- vendor/github.com/go-ole/go-ole/safearray.go | 27 -- .../go-ole/go-ole/safearray_func.go | 211 ---------- .../go-ole/go-ole/safearray_windows.go | 337 --------------- .../go-ole/go-ole/safearrayconversion.go | 140 ------- .../go-ole/go-ole/safearrayslices.go | 33 -- vendor/github.com/go-ole/go-ole/utility.go | 101 ----- vendor/github.com/go-ole/go-ole/variables.go | 15 - vendor/github.com/go-ole/go-ole/variant.go | 105 ----- .../github.com/go-ole/go-ole/variant_386.go | 11 - .../github.com/go-ole/go-ole/variant_amd64.go | 12 - .../github.com/go-ole/go-ole/variant_arm.go | 11 - .../github.com/go-ole/go-ole/variant_arm64.go | 13 - .../go-ole/go-ole/variant_date_386.go | 22 - .../go-ole/go-ole/variant_date_amd64.go | 20 - .../go-ole/go-ole/variant_date_arm.go | 22 - .../go-ole/go-ole/variant_date_arm64.go | 23 -- .../go-ole/go-ole/variant_ppc64le.go | 12 - .../github.com/go-ole/go-ole/variant_s390x.go | 12 - vendor/github.com/go-ole/go-ole/vt_string.go | 58 --- vendor/github.com/go-ole/go-ole/winrt.go | 99 ----- vendor/github.com/go-ole/go-ole/winrt_doc.go | 36 -- vendor/modules.txt | 1 - 63 files changed, 3896 deletions(-) delete mode 100644 vendor/github.com/go-ole/go-ole/.travis.yml delete mode 100644 vendor/github.com/go-ole/go-ole/ChangeLog.md delete mode 100644 vendor/github.com/go-ole/go-ole/LICENSE delete mode 100644 vendor/github.com/go-ole/go-ole/README.md delete mode 100644 vendor/github.com/go-ole/go-ole/SECURITY.md delete mode 100644 vendor/github.com/go-ole/go-ole/appveyor.yml delete mode 100644 vendor/github.com/go-ole/go-ole/com.go delete mode 100644 vendor/github.com/go-ole/go-ole/com_func.go delete mode 100644 vendor/github.com/go-ole/go-ole/connect.go delete mode 100644 vendor/github.com/go-ole/go-ole/constants.go delete mode 100644 vendor/github.com/go-ole/go-ole/error.go delete mode 100644 vendor/github.com/go-ole/go-ole/error_func.go delete mode 100644 vendor/github.com/go-ole/go-ole/error_windows.go delete mode 100644 vendor/github.com/go-ole/go-ole/guid.go delete mode 100644 vendor/github.com/go-ole/go-ole/iconnectionpoint.go delete mode 100644 vendor/github.com/go-ole/go-ole/iconnectionpoint_func.go delete mode 100644 vendor/github.com/go-ole/go-ole/iconnectionpoint_windows.go delete mode 100644 vendor/github.com/go-ole/go-ole/iconnectionpointcontainer.go delete mode 100644 vendor/github.com/go-ole/go-ole/iconnectionpointcontainer_func.go delete mode 100644 vendor/github.com/go-ole/go-ole/iconnectionpointcontainer_windows.go delete mode 100644 vendor/github.com/go-ole/go-ole/idispatch.go delete mode 100644 vendor/github.com/go-ole/go-ole/idispatch_func.go delete mode 100644 vendor/github.com/go-ole/go-ole/idispatch_windows.go delete mode 100644 vendor/github.com/go-ole/go-ole/ienumvariant.go delete mode 100644 vendor/github.com/go-ole/go-ole/ienumvariant_func.go delete mode 100644 vendor/github.com/go-ole/go-ole/ienumvariant_windows.go delete mode 100644 vendor/github.com/go-ole/go-ole/iinspectable.go delete mode 100644 vendor/github.com/go-ole/go-ole/iinspectable_func.go delete mode 100644 vendor/github.com/go-ole/go-ole/iinspectable_windows.go delete mode 100644 vendor/github.com/go-ole/go-ole/iprovideclassinfo.go delete mode 100644 vendor/github.com/go-ole/go-ole/iprovideclassinfo_func.go delete mode 100644 vendor/github.com/go-ole/go-ole/iprovideclassinfo_windows.go delete mode 100644 vendor/github.com/go-ole/go-ole/itypeinfo.go delete mode 100644 vendor/github.com/go-ole/go-ole/itypeinfo_func.go delete mode 100644 vendor/github.com/go-ole/go-ole/itypeinfo_windows.go delete mode 100644 vendor/github.com/go-ole/go-ole/iunknown.go delete mode 100644 vendor/github.com/go-ole/go-ole/iunknown_func.go delete mode 100644 vendor/github.com/go-ole/go-ole/iunknown_windows.go delete mode 100644 vendor/github.com/go-ole/go-ole/ole.go delete mode 100644 vendor/github.com/go-ole/go-ole/safearray.go delete mode 100644 vendor/github.com/go-ole/go-ole/safearray_func.go delete mode 100644 vendor/github.com/go-ole/go-ole/safearray_windows.go delete mode 100644 vendor/github.com/go-ole/go-ole/safearrayconversion.go delete mode 100644 vendor/github.com/go-ole/go-ole/safearrayslices.go delete mode 100644 vendor/github.com/go-ole/go-ole/utility.go delete mode 100644 vendor/github.com/go-ole/go-ole/variables.go delete mode 100644 vendor/github.com/go-ole/go-ole/variant.go delete mode 100644 vendor/github.com/go-ole/go-ole/variant_386.go delete mode 100644 vendor/github.com/go-ole/go-ole/variant_amd64.go delete mode 100644 vendor/github.com/go-ole/go-ole/variant_arm.go delete mode 100644 vendor/github.com/go-ole/go-ole/variant_arm64.go delete mode 100644 vendor/github.com/go-ole/go-ole/variant_date_386.go delete mode 100644 vendor/github.com/go-ole/go-ole/variant_date_amd64.go delete mode 100644 vendor/github.com/go-ole/go-ole/variant_date_arm.go delete mode 100644 vendor/github.com/go-ole/go-ole/variant_date_arm64.go delete mode 100644 vendor/github.com/go-ole/go-ole/variant_ppc64le.go delete mode 100644 vendor/github.com/go-ole/go-ole/variant_s390x.go delete mode 100644 vendor/github.com/go-ole/go-ole/vt_string.go delete mode 100644 vendor/github.com/go-ole/go-ole/winrt.go delete mode 100644 vendor/github.com/go-ole/go-ole/winrt_doc.go diff --git a/go.mod b/go.mod index 17ab729..2aca02b 100644 --- a/go.mod +++ b/go.mod @@ -5,7 +5,6 @@ go 1.23.3 toolchain go1.23.9 require ( - github.com/go-ole/go-ole v1.3.0 github.com/onsi/ginkgo/v2 v2.27.2 github.com/onsi/gomega v1.38.2 github.com/schollz/progressbar/v3 v3.18.0 diff --git a/go.sum b/go.sum index f0c0630..58c2d6d 100644 --- a/go.sum +++ b/go.sum @@ -14,8 +14,6 @@ github.com/gkampitakis/go-snaps v0.5.15 h1:amyJrvM1D33cPHwVrjo9jQxX8g/7E2wYdZ+01 github.com/gkampitakis/go-snaps v0.5.15/go.mod h1:HNpx/9GoKisdhw9AFOBT1N7DBs9DiHo/hGheFGBZ+mc= github.com/go-logr/logr v1.4.3 h1:CjnDlHq8ikf6E492q6eKboGOC0T8CDaOvkHCIg8idEI= github.com/go-logr/logr v1.4.3/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY= -github.com/go-ole/go-ole v1.3.0 h1:Dt6ye7+vXGIKZ7Xtk4s6/xVdGDQynvom7xCFEdWr6uE= -github.com/go-ole/go-ole v1.3.0/go.mod h1:5LS6F96DhAwUc7C+1HLexzMXY1xGRSryjyPPKW6zv78= github.com/go-task/slim-sprig/v3 v3.0.0 h1:sUs3vkvUymDpBKi3qH1YSqBQk9+9D/8M2mN1vB6EwHI= github.com/go-task/slim-sprig/v3 v3.0.0/go.mod h1:W848ghGpv3Qj3dhTPRyJypKRiqCdHZiAzKg9hl15HA8= github.com/goccy/go-yaml v1.18.0 h1:8W7wMFS12Pcas7KU+VVkaiCng+kG8QiFeFwzFb+rwuw= @@ -80,7 +78,6 @@ golang.org/x/net v0.43.0/go.mod h1:vhO1fvI4dGsIjh73sWfUVjj3N7CA9WkKJNQm2svM6Jg= golang.org/x/sync v0.16.0 h1:ycBJEhp9p4vXvUZNszeOq0kGTPghopOL8q0fq3vstxw= golang.org/x/sync v0.16.0/go.mod h1:1dzgHSNfp02xaA81J2MS99Qcpr2w7fw1gpm99rleRqA= golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.35.0 h1:vz1N37gP5bs89s7He8XuIYXpyY0+QlsKmzipCbUtyxI= golang.org/x/sys v0.35.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k= golang.org/x/term v0.34.0 h1:O/2T7POpk0ZZ7MAzMeWFSg6S5IpWd/RXDlM9hgM3DR4= diff --git a/vendor/github.com/go-ole/go-ole/.travis.yml b/vendor/github.com/go-ole/go-ole/.travis.yml deleted file mode 100644 index 28f740c..0000000 --- a/vendor/github.com/go-ole/go-ole/.travis.yml +++ /dev/null @@ -1,8 +0,0 @@ -language: go -sudo: false - -go: - - 1.9.x - - 1.10.x - - 1.11.x - - tip diff --git a/vendor/github.com/go-ole/go-ole/ChangeLog.md b/vendor/github.com/go-ole/go-ole/ChangeLog.md deleted file mode 100644 index 4ba6a8c..0000000 --- a/vendor/github.com/go-ole/go-ole/ChangeLog.md +++ /dev/null @@ -1,49 +0,0 @@ -# Version 1.x.x - -* **Add more test cases and reference new test COM server project.** (Placeholder for future additions) - -# Version 1.2.0-alphaX - -**Minimum supported version is now Go 1.4. Go 1.1 support is deprecated, but should still build.** - - * Added CI configuration for Travis-CI and AppVeyor. - * Added test InterfaceID and ClassID for the COM Test Server project. - * Added more inline documentation (#83). - * Added IEnumVARIANT implementation (#88). - * Added IEnumVARIANT test cases (#99, #100, #101). - * Added support for retrieving `time.Time` from VARIANT (#92). - * Added test case for IUnknown (#64). - * Added test case for IDispatch (#64). - * Added test cases for scalar variants (#64, #76). - -# Version 1.1.1 - - * Fixes for Linux build. - * Fixes for Windows build. - -# Version 1.1.0 - -The change to provide building on all platforms is a new feature. The increase in minor version reflects that and allows those who wish to stay on 1.0.x to continue to do so. Support for 1.0.x will be limited to bug fixes. - - * Move GUID out of variables.go into its own file to make new documentation available. - * Move OleError out of ole.go into its own file to make new documentation available. - * Add documentation to utility functions. - * Add documentation to variant receiver functions. - * Add documentation to ole structures. - * Make variant available to other systems outside of Windows. - * Make OLE structures available to other systems outside of Windows. - -## New Features - - * Library should now be built on all platforms supported by Go. Library will NOOP on any platform that is not Windows. - * More functions are now documented and available on godoc.org. - -# Version 1.0.1 - - 1. Fix package references from repository location change. - -# Version 1.0.0 - -This version is stable enough for use. The COM API is still incomplete, but provides enough functionality for accessing COM servers using IDispatch interface. - -There is no changelog for this version. Check commits for history. diff --git a/vendor/github.com/go-ole/go-ole/LICENSE b/vendor/github.com/go-ole/go-ole/LICENSE deleted file mode 100644 index 623ec06..0000000 --- a/vendor/github.com/go-ole/go-ole/LICENSE +++ /dev/null @@ -1,21 +0,0 @@ -The MIT License (MIT) - -Copyright © 2013-2017 Yasuhiro Matsumoto, - -Permission is hereby granted, free of charge, to any person obtaining a copy of -this software and associated documentation files (the “Software”), to deal in -the Software without restriction, including without limitation the rights to -use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies -of the Software, and to permit persons to whom the Software is furnished to do -so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in all -copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -SOFTWARE. diff --git a/vendor/github.com/go-ole/go-ole/README.md b/vendor/github.com/go-ole/go-ole/README.md deleted file mode 100644 index 7b57755..0000000 --- a/vendor/github.com/go-ole/go-ole/README.md +++ /dev/null @@ -1,46 +0,0 @@ -# Go OLE - -[![Build status](https://ci.appveyor.com/api/projects/status/qr0u2sf7q43us9fj?svg=true)](https://ci.appveyor.com/project/jacobsantos/go-ole-jgs28) -[![Build Status](https://travis-ci.org/go-ole/go-ole.svg?branch=master)](https://travis-ci.org/go-ole/go-ole) -[![GoDoc](https://godoc.org/github.com/go-ole/go-ole?status.svg)](https://godoc.org/github.com/go-ole/go-ole) - -Go bindings for Windows COM using shared libraries instead of cgo. - -By Yasuhiro Matsumoto. - -## Install - -To experiment with go-ole, you can just compile and run the example program: - -``` -go get github.com/go-ole/go-ole -cd /path/to/go-ole/ -go test - -cd /path/to/go-ole/example/excel -go run excel.go -``` - -## Continuous Integration - -Continuous integration configuration has been added for both Travis-CI and AppVeyor. You will have to add these to your own account for your fork in order for it to run. - -**Travis-CI** - -Travis-CI was added to check builds on Linux to ensure that `go get` works when cross building. Currently, Travis-CI is not used to test cross-building, but this may be changed in the future. It is also not currently possible to test the library on Linux, since COM API is specific to Windows and it is not currently possible to run a COM server on Linux or even connect to a remote COM server. - -**AppVeyor** - -AppVeyor is used to build on Windows using the (in-development) test COM server. It is currently only used to test the build and ensure that the code works on Windows. It will be used to register a COM server and then run the test cases based on the test COM server. - -The tests currently do run and do pass and this should be maintained with commits. - -## Versioning - -Go OLE uses [semantic versioning](http://semver.org) for version numbers, which is similar to the version contract of the Go language. Which means that the major version will always maintain backwards compatibility with minor versions. Minor versions will only add new additions and changes. Fixes will always be in patch. - -This contract should allow you to upgrade to new minor and patch versions without breakage or modifications to your existing code. Leave a ticket, if there is breakage, so that it could be fixed. - -## LICENSE - -Under the MIT License: http://mattn.mit-license.org/2013 diff --git a/vendor/github.com/go-ole/go-ole/SECURITY.md b/vendor/github.com/go-ole/go-ole/SECURITY.md deleted file mode 100644 index dac2815..0000000 --- a/vendor/github.com/go-ole/go-ole/SECURITY.md +++ /dev/null @@ -1,13 +0,0 @@ -# Security Policy - -## Supported Versions - -Security updates are applied only to the latest release. - -## Reporting a Vulnerability - -If you have discovered a security vulnerability in this project, please report it privately. **Do not disclose it as a public issue.** This gives us time to work with you to fix the issue before public exposure, reducing the chance that the exploit will be used before a patch is released. - -Please disclose it at [security advisory](https://github.com/go-ole/go-ole/security/advisories/new). - -This project is maintained by a team of volunteers on a reasonable-effort basis. As such, please give us at least 90 days to work on a fix before public exposure. diff --git a/vendor/github.com/go-ole/go-ole/appveyor.yml b/vendor/github.com/go-ole/go-ole/appveyor.yml deleted file mode 100644 index 8df7fa2..0000000 --- a/vendor/github.com/go-ole/go-ole/appveyor.yml +++ /dev/null @@ -1,68 +0,0 @@ -# Notes: -# - Minimal appveyor.yml file is an empty file. All sections are optional. -# - Indent each level of configuration with 2 spaces. Do not use tabs! -# - All section names are case-sensitive. -# - Section names should be unique on each level. - -version: "1.3.0.{build}-alpha-{branch}" - -os: Visual Studio 2019 - -build: off - -skip_tags: true - -clone_folder: c:\gopath\src\github.com\go-ole\go-ole - -environment: - GOPATH: c:\gopath - GOROOT: c:\go - DOWNLOADPLATFORM: "x64" - -before_test: - # - Download COM Server - - ps: Start-FileDownload "https://github.com/go-ole/test-com-server/releases/download/v1.0.2/test-com-server-${env:DOWNLOADPLATFORM}.zip" - - 7z e test-com-server-%DOWNLOADPLATFORM%.zip -oc:\gopath\src\github.com\go-ole\go-ole > NUL - - c:\gopath\src\github.com\go-ole\go-ole\build\register-assembly.bat - -test_script: - - go test -v -cover ./... - # go vet has false positives on unsafe.Pointer with windows/sys. Disabling since it is recommended to use go test instead. - # - go vet ./... - -branches: - only: - - master - - v1.2 - - v1.1 - - v1.0 - -matrix: - allow_failures: - - environment: - GOROOT: C:\go-x86 - DOWNLOADPLATFORM: "x86" - - environment: - GOROOT: C:\go118 - DOWNLOADPLATFORM: "x64" - - environment: - GOROOT: C:\go118-x86 - DOWNLOADPLATFORM: "x86" - -install: - - go version - - go env - - go get -u golang.org/x/tools/cmd/cover - - go get -u golang.org/x/tools/cmd/godoc - - go get -u golang.org/x/tools/cmd/stringer - -build_script: - - cd c:\gopath\src\github.com\go-ole\go-ole - - go get -v -t ./... - - go build - -# disable automatic tests -test: on - -# disable deployment -deploy: off diff --git a/vendor/github.com/go-ole/go-ole/com.go b/vendor/github.com/go-ole/go-ole/com.go deleted file mode 100644 index cabbac0..0000000 --- a/vendor/github.com/go-ole/go-ole/com.go +++ /dev/null @@ -1,386 +0,0 @@ -// +build windows - -package ole - -import ( - "syscall" - "unicode/utf16" - "unsafe" -) - -var ( - procCoInitialize = modole32.NewProc("CoInitialize") - procCoInitializeEx = modole32.NewProc("CoInitializeEx") - procCoInitializeSecurity = modole32.NewProc("CoInitializeSecurity") - procCoUninitialize = modole32.NewProc("CoUninitialize") - procCoCreateInstance = modole32.NewProc("CoCreateInstance") - procCoTaskMemFree = modole32.NewProc("CoTaskMemFree") - procCLSIDFromProgID = modole32.NewProc("CLSIDFromProgID") - procCLSIDFromString = modole32.NewProc("CLSIDFromString") - procStringFromCLSID = modole32.NewProc("StringFromCLSID") - procStringFromIID = modole32.NewProc("StringFromIID") - procIIDFromString = modole32.NewProc("IIDFromString") - procCoGetObject = modole32.NewProc("CoGetObject") - procGetUserDefaultLCID = modkernel32.NewProc("GetUserDefaultLCID") - procCopyMemory = modkernel32.NewProc("RtlMoveMemory") - procVariantInit = modoleaut32.NewProc("VariantInit") - procVariantClear = modoleaut32.NewProc("VariantClear") - procVariantTimeToSystemTime = modoleaut32.NewProc("VariantTimeToSystemTime") - procSysAllocString = modoleaut32.NewProc("SysAllocString") - procSysAllocStringLen = modoleaut32.NewProc("SysAllocStringLen") - procSysFreeString = modoleaut32.NewProc("SysFreeString") - procSysStringLen = modoleaut32.NewProc("SysStringLen") - procCreateDispTypeInfo = modoleaut32.NewProc("CreateDispTypeInfo") - procCreateStdDispatch = modoleaut32.NewProc("CreateStdDispatch") - procGetActiveObject = modoleaut32.NewProc("GetActiveObject") - - procGetMessageW = moduser32.NewProc("GetMessageW") - procDispatchMessageW = moduser32.NewProc("DispatchMessageW") -) - -// This is to enable calling COM Security initialization multiple times -var bSecurityInit bool = false - -// coInitialize initializes COM library on current thread. -// -// MSDN documentation suggests that this function should not be called. Call -// CoInitializeEx() instead. The reason has to do with threading and this -// function is only for single-threaded apartments. -// -// That said, most users of the library have gotten away with just this -// function. If you are experiencing threading issues, then use -// CoInitializeEx(). -func coInitialize() (err error) { - // http://msdn.microsoft.com/en-us/library/windows/desktop/ms678543(v=vs.85).aspx - // Suggests that no value should be passed to CoInitialized. - // Could just be Call() since the parameter is optional. <-- Needs testing to be sure. - hr, _, _ := procCoInitialize.Call(uintptr(0)) - if hr != 0 { - err = NewError(hr) - } - return -} - -// coInitializeEx initializes COM library with concurrency model. -func coInitializeEx(coinit uint32) (err error) { - // http://msdn.microsoft.com/en-us/library/windows/desktop/ms695279(v=vs.85).aspx - // Suggests that the first parameter is not only optional but should always be NULL. - hr, _, _ := procCoInitializeEx.Call(uintptr(0), uintptr(coinit)) - if hr != 0 { - err = NewError(hr) - } - return -} - -// coInitializeSecurity: Registers security and sets the default security values -// for the process. -func coInitializeSecurity(cAuthSvc int32, - dwAuthnLevel uint32, - dwImpLevel uint32, - dwCapabilities uint32) (err error) { - // Check COM Security initialization has done previously - if !bSecurityInit { - // https://learn.microsoft.com/en-us/windows/win32/api/combaseapi/nf-combaseapi-coinitializesecurity - hr, _, _ := procCoInitializeSecurity.Call( - uintptr(0), // Allow *all* VSS writers to communicate back! - uintptr(cAuthSvc), // Default COM authentication service - uintptr(0), // Default COM authorization service - uintptr(0), // Reserved parameter - uintptr(dwAuthnLevel), // Strongest COM authentication level - uintptr(dwImpLevel), // Minimal impersonation abilities - uintptr(0), // Default COM authentication settings - uintptr(dwCapabilities), // Cloaking - uintptr(0)) // eserved parameter - if hr != 0 { - err = NewError(hr) - } else { - // COM Security initialization done make global flag true. - bSecurityInit = true - } - } - return -} - -// CoInitialize initializes COM library on current thread. -// -// MSDN documentation suggests that this function should not be called. Call -// CoInitializeEx() instead. The reason has to do with threading and this -// function is only for single-threaded apartments. -// -// That said, most users of the library have gotten away with just this -// function. If you are experiencing threading issues, then use -// CoInitializeEx(). -func CoInitialize(p uintptr) (err error) { - // p is ignored and won't be used. - // Avoid any variable not used errors. - p = uintptr(0) - return coInitialize() -} - -// CoInitializeEx initializes COM library with concurrency model. -func CoInitializeEx(p uintptr, coinit uint32) (err error) { - // Avoid any variable not used errors. - p = uintptr(0) - return coInitializeEx(coinit) -} - -// CoUninitialize uninitializes COM Library. -func CoUninitialize() { - procCoUninitialize.Call() -} - -// CoInitializeSecurity: Registers security and sets the default security values -// for the process. -func CoInitializeSecurity(cAuthSvc int32, - dwAuthnLevel uint32, - dwImpLevel uint32, - dwCapabilities uint32) (err error) { - return coInitializeSecurity(cAuthSvc, dwAuthnLevel, dwImpLevel, dwCapabilities) -} - -// CoTaskMemFree frees memory pointer. -func CoTaskMemFree(memptr uintptr) { - procCoTaskMemFree.Call(memptr) -} - -// CLSIDFromProgID retrieves Class Identifier with the given Program Identifier. -// -// The Programmatic Identifier must be registered, because it will be looked up -// in the Windows Registry. The registry entry has the following keys: CLSID, -// Insertable, Protocol and Shell -// (https://msdn.microsoft.com/en-us/library/dd542719(v=vs.85).aspx). -// -// programID identifies the class id with less precision and is not guaranteed -// to be unique. These are usually found in the registry under -// HKEY_LOCAL_MACHINE\SOFTWARE\Classes, usually with the format of -// "Program.Component.Version" with version being optional. -// -// CLSIDFromProgID in Windows API. -func CLSIDFromProgID(progId string) (clsid *GUID, err error) { - var guid GUID - lpszProgID := uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(progId))) - hr, _, _ := procCLSIDFromProgID.Call(lpszProgID, uintptr(unsafe.Pointer(&guid))) - if hr != 0 { - err = NewError(hr) - } - clsid = &guid - return -} - -// CLSIDFromString retrieves Class ID from string representation. -// -// This is technically the string version of the GUID and will convert the -// string to object. -// -// CLSIDFromString in Windows API. -func CLSIDFromString(str string) (clsid *GUID, err error) { - var guid GUID - lpsz := uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(str))) - hr, _, _ := procCLSIDFromString.Call(lpsz, uintptr(unsafe.Pointer(&guid))) - if hr != 0 { - err = NewError(hr) - } - clsid = &guid - return -} - -// StringFromCLSID returns GUID formated string from GUID object. -func StringFromCLSID(clsid *GUID) (str string, err error) { - var p *uint16 - hr, _, _ := procStringFromCLSID.Call(uintptr(unsafe.Pointer(clsid)), uintptr(unsafe.Pointer(&p))) - if hr != 0 { - err = NewError(hr) - } - str = LpOleStrToString(p) - return -} - -// IIDFromString returns GUID from program ID. -func IIDFromString(progId string) (clsid *GUID, err error) { - var guid GUID - lpsz := uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(progId))) - hr, _, _ := procIIDFromString.Call(lpsz, uintptr(unsafe.Pointer(&guid))) - if hr != 0 { - err = NewError(hr) - } - clsid = &guid - return -} - -// StringFromIID returns GUID formatted string from GUID object. -func StringFromIID(iid *GUID) (str string, err error) { - var p *uint16 - hr, _, _ := procStringFromIID.Call(uintptr(unsafe.Pointer(iid)), uintptr(unsafe.Pointer(&p))) - if hr != 0 { - err = NewError(hr) - } - str = LpOleStrToString(p) - return -} - -// CreateInstance of single uninitialized object with GUID. -func CreateInstance(clsid *GUID, iid *GUID) (unk *IUnknown, err error) { - if iid == nil { - iid = IID_IUnknown - } - hr, _, _ := procCoCreateInstance.Call( - uintptr(unsafe.Pointer(clsid)), - 0, - CLSCTX_SERVER, - uintptr(unsafe.Pointer(iid)), - uintptr(unsafe.Pointer(&unk))) - if hr != 0 { - err = NewError(hr) - } - return -} - -// GetActiveObject retrieves pointer to active object. -func GetActiveObject(clsid *GUID, iid *GUID) (unk *IUnknown, err error) { - if iid == nil { - iid = IID_IUnknown - } - hr, _, _ := procGetActiveObject.Call( - uintptr(unsafe.Pointer(clsid)), - uintptr(unsafe.Pointer(iid)), - uintptr(unsafe.Pointer(&unk))) - if hr != 0 { - err = NewError(hr) - } - return -} - -type BindOpts struct { - CbStruct uint32 - GrfFlags uint32 - GrfMode uint32 - TickCountDeadline uint32 -} - -// GetObject retrieves pointer to active object. -func GetObject(programID string, bindOpts *BindOpts, iid *GUID) (unk *IUnknown, err error) { - if bindOpts != nil { - bindOpts.CbStruct = uint32(unsafe.Sizeof(BindOpts{})) - } - if iid == nil { - iid = IID_IUnknown - } - hr, _, _ := procCoGetObject.Call( - uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(programID))), - uintptr(unsafe.Pointer(bindOpts)), - uintptr(unsafe.Pointer(iid)), - uintptr(unsafe.Pointer(&unk))) - if hr != 0 { - err = NewError(hr) - } - return -} - -// VariantInit initializes variant. -func VariantInit(v *VARIANT) (err error) { - hr, _, _ := procVariantInit.Call(uintptr(unsafe.Pointer(v))) - if hr != 0 { - err = NewError(hr) - } - return -} - -// VariantClear clears value in Variant settings to VT_EMPTY. -func VariantClear(v *VARIANT) (err error) { - hr, _, _ := procVariantClear.Call(uintptr(unsafe.Pointer(v))) - if hr != 0 { - err = NewError(hr) - } - return -} - -// SysAllocString allocates memory for string and copies string into memory. -func SysAllocString(v string) (ss *int16) { - pss, _, _ := procSysAllocString.Call(uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(v)))) - ss = (*int16)(unsafe.Pointer(pss)) - return -} - -// SysAllocStringLen copies up to length of given string returning pointer. -func SysAllocStringLen(v string) (ss *int16) { - utf16 := utf16.Encode([]rune(v + "\x00")) - ptr := &utf16[0] - - pss, _, _ := procSysAllocStringLen.Call(uintptr(unsafe.Pointer(ptr)), uintptr(len(utf16)-1)) - ss = (*int16)(unsafe.Pointer(pss)) - return -} - -// SysFreeString frees string system memory. This must be called with SysAllocString. -func SysFreeString(v *int16) (err error) { - hr, _, _ := procSysFreeString.Call(uintptr(unsafe.Pointer(v))) - if hr != 0 { - err = NewError(hr) - } - return -} - -// SysStringLen is the length of the system allocated string. -func SysStringLen(v *int16) uint32 { - l, _, _ := procSysStringLen.Call(uintptr(unsafe.Pointer(v))) - return uint32(l) -} - -// CreateStdDispatch provides default IDispatch implementation for IUnknown. -// -// This handles default IDispatch implementation for objects. It haves a few -// limitations with only supporting one language. It will also only return -// default exception codes. -func CreateStdDispatch(unk *IUnknown, v uintptr, ptinfo *IUnknown) (disp *IDispatch, err error) { - hr, _, _ := procCreateStdDispatch.Call( - uintptr(unsafe.Pointer(unk)), - v, - uintptr(unsafe.Pointer(ptinfo)), - uintptr(unsafe.Pointer(&disp))) - if hr != 0 { - err = NewError(hr) - } - return -} - -// CreateDispTypeInfo provides default ITypeInfo implementation for IDispatch. -// -// This will not handle the full implementation of the interface. -func CreateDispTypeInfo(idata *INTERFACEDATA) (pptinfo *IUnknown, err error) { - hr, _, _ := procCreateDispTypeInfo.Call( - uintptr(unsafe.Pointer(idata)), - uintptr(GetUserDefaultLCID()), - uintptr(unsafe.Pointer(&pptinfo))) - if hr != 0 { - err = NewError(hr) - } - return -} - -// copyMemory moves location of a block of memory. -func copyMemory(dest unsafe.Pointer, src unsafe.Pointer, length uint32) { - procCopyMemory.Call(uintptr(dest), uintptr(src), uintptr(length)) -} - -// GetUserDefaultLCID retrieves current user default locale. -func GetUserDefaultLCID() (lcid uint32) { - ret, _, _ := procGetUserDefaultLCID.Call() - lcid = uint32(ret) - return -} - -// GetMessage in message queue from runtime. -// -// This function appears to block. PeekMessage does not block. -func GetMessage(msg *Msg, hwnd uint32, MsgFilterMin uint32, MsgFilterMax uint32) (ret int32, err error) { - r0, _, err := procGetMessageW.Call(uintptr(unsafe.Pointer(msg)), uintptr(hwnd), uintptr(MsgFilterMin), uintptr(MsgFilterMax)) - ret = int32(r0) - return -} - -// DispatchMessage to window procedure. -func DispatchMessage(msg *Msg) (ret int32) { - r0, _, _ := procDispatchMessageW.Call(uintptr(unsafe.Pointer(msg))) - ret = int32(r0) - return -} diff --git a/vendor/github.com/go-ole/go-ole/com_func.go b/vendor/github.com/go-ole/go-ole/com_func.go deleted file mode 100644 index cef539d..0000000 --- a/vendor/github.com/go-ole/go-ole/com_func.go +++ /dev/null @@ -1,174 +0,0 @@ -// +build !windows - -package ole - -import ( - "time" - "unsafe" -) - -// coInitialize initializes COM library on current thread. -// -// MSDN documentation suggests that this function should not be called. Call -// CoInitializeEx() instead. The reason has to do with threading and this -// function is only for single-threaded apartments. -// -// That said, most users of the library have gotten away with just this -// function. If you are experiencing threading issues, then use -// CoInitializeEx(). -func coInitialize() error { - return NewError(E_NOTIMPL) -} - -// coInitializeEx initializes COM library with concurrency model. -func coInitializeEx(coinit uint32) error { - return NewError(E_NOTIMPL) -} - -// CoInitialize initializes COM library on current thread. -// -// MSDN documentation suggests that this function should not be called. Call -// CoInitializeEx() instead. The reason has to do with threading and this -// function is only for single-threaded apartments. -// -// That said, most users of the library have gotten away with just this -// function. If you are experiencing threading issues, then use -// CoInitializeEx(). -func CoInitialize(p uintptr) error { - return NewError(E_NOTIMPL) -} - -// CoInitializeEx initializes COM library with concurrency model. -func CoInitializeEx(p uintptr, coinit uint32) error { - return NewError(E_NOTIMPL) -} - -// CoUninitialize uninitializes COM Library. -func CoUninitialize() {} - -// CoTaskMemFree frees memory pointer. -func CoTaskMemFree(memptr uintptr) {} - -// CLSIDFromProgID retrieves Class Identifier with the given Program Identifier. -// -// The Programmatic Identifier must be registered, because it will be looked up -// in the Windows Registry. The registry entry has the following keys: CLSID, -// Insertable, Protocol and Shell -// (https://msdn.microsoft.com/en-us/library/dd542719(v=vs.85).aspx). -// -// programID identifies the class id with less precision and is not guaranteed -// to be unique. These are usually found in the registry under -// HKEY_LOCAL_MACHINE\SOFTWARE\Classes, usually with the format of -// "Program.Component.Version" with version being optional. -// -// CLSIDFromProgID in Windows API. -func CLSIDFromProgID(progId string) (*GUID, error) { - return nil, NewError(E_NOTIMPL) -} - -// CLSIDFromString retrieves Class ID from string representation. -// -// This is technically the string version of the GUID and will convert the -// string to object. -// -// CLSIDFromString in Windows API. -func CLSIDFromString(str string) (*GUID, error) { - return nil, NewError(E_NOTIMPL) -} - -// StringFromCLSID returns GUID formated string from GUID object. -func StringFromCLSID(clsid *GUID) (string, error) { - return "", NewError(E_NOTIMPL) -} - -// IIDFromString returns GUID from program ID. -func IIDFromString(progId string) (*GUID, error) { - return nil, NewError(E_NOTIMPL) -} - -// StringFromIID returns GUID formatted string from GUID object. -func StringFromIID(iid *GUID) (string, error) { - return "", NewError(E_NOTIMPL) -} - -// CreateInstance of single uninitialized object with GUID. -func CreateInstance(clsid *GUID, iid *GUID) (*IUnknown, error) { - return nil, NewError(E_NOTIMPL) -} - -// GetActiveObject retrieves pointer to active object. -func GetActiveObject(clsid *GUID, iid *GUID) (*IUnknown, error) { - return nil, NewError(E_NOTIMPL) -} - -// VariantInit initializes variant. -func VariantInit(v *VARIANT) error { - return NewError(E_NOTIMPL) -} - -// VariantClear clears value in Variant settings to VT_EMPTY. -func VariantClear(v *VARIANT) error { - return NewError(E_NOTIMPL) -} - -// SysAllocString allocates memory for string and copies string into memory. -func SysAllocString(v string) *int16 { - u := int16(0) - return &u -} - -// SysAllocStringLen copies up to length of given string returning pointer. -func SysAllocStringLen(v string) *int16 { - u := int16(0) - return &u -} - -// SysFreeString frees string system memory. This must be called with SysAllocString. -func SysFreeString(v *int16) error { - return NewError(E_NOTIMPL) -} - -// SysStringLen is the length of the system allocated string. -func SysStringLen(v *int16) uint32 { - return uint32(0) -} - -// CreateStdDispatch provides default IDispatch implementation for IUnknown. -// -// This handles default IDispatch implementation for objects. It haves a few -// limitations with only supporting one language. It will also only return -// default exception codes. -func CreateStdDispatch(unk *IUnknown, v uintptr, ptinfo *IUnknown) (*IDispatch, error) { - return nil, NewError(E_NOTIMPL) -} - -// CreateDispTypeInfo provides default ITypeInfo implementation for IDispatch. -// -// This will not handle the full implementation of the interface. -func CreateDispTypeInfo(idata *INTERFACEDATA) (*IUnknown, error) { - return nil, NewError(E_NOTIMPL) -} - -// copyMemory moves location of a block of memory. -func copyMemory(dest unsafe.Pointer, src unsafe.Pointer, length uint32) {} - -// GetUserDefaultLCID retrieves current user default locale. -func GetUserDefaultLCID() uint32 { - return uint32(0) -} - -// GetMessage in message queue from runtime. -// -// This function appears to block. PeekMessage does not block. -func GetMessage(msg *Msg, hwnd uint32, MsgFilterMin uint32, MsgFilterMax uint32) (int32, error) { - return int32(0), NewError(E_NOTIMPL) -} - -// DispatchMessage to window procedure. -func DispatchMessage(msg *Msg) int32 { - return int32(0) -} - -func GetVariantDate(value uint64) (time.Time, error) { - return time.Now(), NewError(E_NOTIMPL) -} diff --git a/vendor/github.com/go-ole/go-ole/connect.go b/vendor/github.com/go-ole/go-ole/connect.go deleted file mode 100644 index b2ac2ec..0000000 --- a/vendor/github.com/go-ole/go-ole/connect.go +++ /dev/null @@ -1,192 +0,0 @@ -package ole - -// Connection contains IUnknown for fluent interface interaction. -// -// Deprecated. Use oleutil package instead. -type Connection struct { - Object *IUnknown // Access COM -} - -// Initialize COM. -func (*Connection) Initialize() (err error) { - return coInitialize() -} - -// Uninitialize COM. -func (*Connection) Uninitialize() { - CoUninitialize() -} - -// Create IUnknown object based first on ProgId and then from String. -func (c *Connection) Create(progId string) (err error) { - var clsid *GUID - clsid, err = CLSIDFromProgID(progId) - if err != nil { - clsid, err = CLSIDFromString(progId) - if err != nil { - return - } - } - - unknown, err := CreateInstance(clsid, IID_IUnknown) - if err != nil { - return - } - c.Object = unknown - - return -} - -// Release IUnknown object. -func (c *Connection) Release() { - c.Object.Release() -} - -// Load COM object from list of programIDs or strings. -func (c *Connection) Load(names ...string) (errors []error) { - var tempErrors []error = make([]error, len(names)) - var numErrors int = 0 - for _, name := range names { - err := c.Create(name) - if err != nil { - tempErrors = append(tempErrors, err) - numErrors += 1 - continue - } - break - } - - copy(errors, tempErrors[0:numErrors]) - return -} - -// Dispatch returns Dispatch object. -func (c *Connection) Dispatch() (object *Dispatch, err error) { - dispatch, err := c.Object.QueryInterface(IID_IDispatch) - if err != nil { - return - } - object = &Dispatch{dispatch} - return -} - -// Dispatch stores IDispatch object. -type Dispatch struct { - Object *IDispatch // Dispatch object. -} - -// Call method on IDispatch with parameters. -func (d *Dispatch) Call(method string, params ...interface{}) (result *VARIANT, err error) { - id, err := d.GetId(method) - if err != nil { - return - } - - result, err = d.Invoke(id, DISPATCH_METHOD, params) - return -} - -// MustCall method on IDispatch with parameters. -func (d *Dispatch) MustCall(method string, params ...interface{}) (result *VARIANT) { - id, err := d.GetId(method) - if err != nil { - panic(err) - } - - result, err = d.Invoke(id, DISPATCH_METHOD, params) - if err != nil { - panic(err) - } - - return -} - -// Get property on IDispatch with parameters. -func (d *Dispatch) Get(name string, params ...interface{}) (result *VARIANT, err error) { - id, err := d.GetId(name) - if err != nil { - return - } - result, err = d.Invoke(id, DISPATCH_PROPERTYGET, params) - return -} - -// MustGet property on IDispatch with parameters. -func (d *Dispatch) MustGet(name string, params ...interface{}) (result *VARIANT) { - id, err := d.GetId(name) - if err != nil { - panic(err) - } - - result, err = d.Invoke(id, DISPATCH_PROPERTYGET, params) - if err != nil { - panic(err) - } - return -} - -// Set property on IDispatch with parameters. -func (d *Dispatch) Set(name string, params ...interface{}) (result *VARIANT, err error) { - id, err := d.GetId(name) - if err != nil { - return - } - result, err = d.Invoke(id, DISPATCH_PROPERTYPUT, params) - return -} - -// MustSet property on IDispatch with parameters. -func (d *Dispatch) MustSet(name string, params ...interface{}) (result *VARIANT) { - id, err := d.GetId(name) - if err != nil { - panic(err) - } - - result, err = d.Invoke(id, DISPATCH_PROPERTYPUT, params) - if err != nil { - panic(err) - } - return -} - -// GetId retrieves ID of name on IDispatch. -func (d *Dispatch) GetId(name string) (id int32, err error) { - var dispid []int32 - dispid, err = d.Object.GetIDsOfName([]string{name}) - if err != nil { - return - } - id = dispid[0] - return -} - -// GetIds retrieves all IDs of names on IDispatch. -func (d *Dispatch) GetIds(names ...string) (dispid []int32, err error) { - dispid, err = d.Object.GetIDsOfName(names) - return -} - -// Invoke IDispatch on DisplayID of dispatch type with parameters. -// -// There have been problems where if send cascading params..., it would error -// out because the parameters would be empty. -func (d *Dispatch) Invoke(id int32, dispatch int16, params []interface{}) (result *VARIANT, err error) { - if len(params) < 1 { - result, err = d.Object.Invoke(id, dispatch) - } else { - result, err = d.Object.Invoke(id, dispatch, params...) - } - return -} - -// Release IDispatch object. -func (d *Dispatch) Release() { - d.Object.Release() -} - -// Connect initializes COM and attempts to load IUnknown based on given names. -func Connect(names ...string) (connection *Connection) { - connection.Initialize() - connection.Load(names...) - return -} diff --git a/vendor/github.com/go-ole/go-ole/constants.go b/vendor/github.com/go-ole/go-ole/constants.go deleted file mode 100644 index fd0c6d7..0000000 --- a/vendor/github.com/go-ole/go-ole/constants.go +++ /dev/null @@ -1,153 +0,0 @@ -package ole - -const ( - CLSCTX_INPROC_SERVER = 1 - CLSCTX_INPROC_HANDLER = 2 - CLSCTX_LOCAL_SERVER = 4 - CLSCTX_INPROC_SERVER16 = 8 - CLSCTX_REMOTE_SERVER = 16 - CLSCTX_ALL = CLSCTX_INPROC_SERVER | CLSCTX_INPROC_HANDLER | CLSCTX_LOCAL_SERVER - CLSCTX_INPROC = CLSCTX_INPROC_SERVER | CLSCTX_INPROC_HANDLER - CLSCTX_SERVER = CLSCTX_INPROC_SERVER | CLSCTX_LOCAL_SERVER | CLSCTX_REMOTE_SERVER -) - -const ( - COINIT_APARTMENTTHREADED = 0x2 - COINIT_MULTITHREADED = 0x0 - COINIT_DISABLE_OLE1DDE = 0x4 - COINIT_SPEED_OVER_MEMORY = 0x8 -) - -const ( - DISPATCH_METHOD = 1 - DISPATCH_PROPERTYGET = 2 - DISPATCH_PROPERTYPUT = 4 - DISPATCH_PROPERTYPUTREF = 8 -) - -const ( - S_OK = 0x00000000 - E_UNEXPECTED = 0x8000FFFF - E_NOTIMPL = 0x80004001 - E_OUTOFMEMORY = 0x8007000E - E_INVALIDARG = 0x80070057 - E_NOINTERFACE = 0x80004002 - E_POINTER = 0x80004003 - E_HANDLE = 0x80070006 - E_ABORT = 0x80004004 - E_FAIL = 0x80004005 - E_ACCESSDENIED = 0x80070005 - E_PENDING = 0x8000000A - - CO_E_CLASSSTRING = 0x800401F3 -) - -const ( - CC_FASTCALL = iota - CC_CDECL - CC_MSCPASCAL - CC_PASCAL = CC_MSCPASCAL - CC_MACPASCAL - CC_STDCALL - CC_FPFASTCALL - CC_SYSCALL - CC_MPWCDECL - CC_MPWPASCAL - CC_MAX = CC_MPWPASCAL -) - -type VT uint16 - -const ( - VT_EMPTY VT = 0x0 - VT_NULL VT = 0x1 - VT_I2 VT = 0x2 - VT_I4 VT = 0x3 - VT_R4 VT = 0x4 - VT_R8 VT = 0x5 - VT_CY VT = 0x6 - VT_DATE VT = 0x7 - VT_BSTR VT = 0x8 - VT_DISPATCH VT = 0x9 - VT_ERROR VT = 0xa - VT_BOOL VT = 0xb - VT_VARIANT VT = 0xc - VT_UNKNOWN VT = 0xd - VT_DECIMAL VT = 0xe - VT_I1 VT = 0x10 - VT_UI1 VT = 0x11 - VT_UI2 VT = 0x12 - VT_UI4 VT = 0x13 - VT_I8 VT = 0x14 - VT_UI8 VT = 0x15 - VT_INT VT = 0x16 - VT_UINT VT = 0x17 - VT_VOID VT = 0x18 - VT_HRESULT VT = 0x19 - VT_PTR VT = 0x1a - VT_SAFEARRAY VT = 0x1b - VT_CARRAY VT = 0x1c - VT_USERDEFINED VT = 0x1d - VT_LPSTR VT = 0x1e - VT_LPWSTR VT = 0x1f - VT_RECORD VT = 0x24 - VT_INT_PTR VT = 0x25 - VT_UINT_PTR VT = 0x26 - VT_FILETIME VT = 0x40 - VT_BLOB VT = 0x41 - VT_STREAM VT = 0x42 - VT_STORAGE VT = 0x43 - VT_STREAMED_OBJECT VT = 0x44 - VT_STORED_OBJECT VT = 0x45 - VT_BLOB_OBJECT VT = 0x46 - VT_CF VT = 0x47 - VT_CLSID VT = 0x48 - VT_BSTR_BLOB VT = 0xfff - VT_VECTOR VT = 0x1000 - VT_ARRAY VT = 0x2000 - VT_BYREF VT = 0x4000 - VT_RESERVED VT = 0x8000 - VT_ILLEGAL VT = 0xffff - VT_ILLEGALMASKED VT = 0xfff - VT_TYPEMASK VT = 0xfff -) - -const ( - DISPID_UNKNOWN = -1 - DISPID_VALUE = 0 - DISPID_PROPERTYPUT = -3 - DISPID_NEWENUM = -4 - DISPID_EVALUATE = -5 - DISPID_CONSTRUCTOR = -6 - DISPID_DESTRUCTOR = -7 - DISPID_COLLECT = -8 -) - -const ( - TKIND_ENUM = 1 - TKIND_RECORD = 2 - TKIND_MODULE = 3 - TKIND_INTERFACE = 4 - TKIND_DISPATCH = 5 - TKIND_COCLASS = 6 - TKIND_ALIAS = 7 - TKIND_UNION = 8 - TKIND_MAX = 9 -) - -// Safe Array Feature Flags - -const ( - FADF_AUTO = 0x0001 - FADF_STATIC = 0x0002 - FADF_EMBEDDED = 0x0004 - FADF_FIXEDSIZE = 0x0010 - FADF_RECORD = 0x0020 - FADF_HAVEIID = 0x0040 - FADF_HAVEVARTYPE = 0x0080 - FADF_BSTR = 0x0100 - FADF_UNKNOWN = 0x0200 - FADF_DISPATCH = 0x0400 - FADF_VARIANT = 0x0800 - FADF_RESERVED = 0xF008 -) diff --git a/vendor/github.com/go-ole/go-ole/error.go b/vendor/github.com/go-ole/go-ole/error.go deleted file mode 100644 index 096b456..0000000 --- a/vendor/github.com/go-ole/go-ole/error.go +++ /dev/null @@ -1,51 +0,0 @@ -package ole - -// OleError stores COM errors. -type OleError struct { - hr uintptr - description string - subError error -} - -// NewError creates new error with HResult. -func NewError(hr uintptr) *OleError { - return &OleError{hr: hr} -} - -// NewErrorWithDescription creates new COM error with HResult and description. -func NewErrorWithDescription(hr uintptr, description string) *OleError { - return &OleError{hr: hr, description: description} -} - -// NewErrorWithSubError creates new COM error with parent error. -func NewErrorWithSubError(hr uintptr, description string, err error) *OleError { - return &OleError{hr: hr, description: description, subError: err} -} - -// Code is the HResult. -func (v *OleError) Code() uintptr { - return uintptr(v.hr) -} - -// String description, either manually set or format message with error code. -func (v *OleError) String() string { - if v.description != "" { - return errstr(int(v.hr)) + " (" + v.description + ")" - } - return errstr(int(v.hr)) -} - -// Error implements error interface. -func (v *OleError) Error() string { - return v.String() -} - -// Description retrieves error summary, if there is one. -func (v *OleError) Description() string { - return v.description -} - -// SubError returns parent error, if there is one. -func (v *OleError) SubError() error { - return v.subError -} diff --git a/vendor/github.com/go-ole/go-ole/error_func.go b/vendor/github.com/go-ole/go-ole/error_func.go deleted file mode 100644 index 8a2ffaa..0000000 --- a/vendor/github.com/go-ole/go-ole/error_func.go +++ /dev/null @@ -1,8 +0,0 @@ -// +build !windows - -package ole - -// errstr converts error code to string. -func errstr(errno int) string { - return "" -} diff --git a/vendor/github.com/go-ole/go-ole/error_windows.go b/vendor/github.com/go-ole/go-ole/error_windows.go deleted file mode 100644 index d0e8e68..0000000 --- a/vendor/github.com/go-ole/go-ole/error_windows.go +++ /dev/null @@ -1,24 +0,0 @@ -// +build windows - -package ole - -import ( - "fmt" - "syscall" - "unicode/utf16" -) - -// errstr converts error code to string. -func errstr(errno int) string { - // ask windows for the remaining errors - var flags uint32 = syscall.FORMAT_MESSAGE_FROM_SYSTEM | syscall.FORMAT_MESSAGE_ARGUMENT_ARRAY | syscall.FORMAT_MESSAGE_IGNORE_INSERTS - b := make([]uint16, 300) - n, err := syscall.FormatMessage(flags, 0, uint32(errno), 0, b, nil) - if err != nil { - return fmt.Sprintf("error %d (FormatMessage failed with: %v)", errno, err) - } - // trim terminating \r and \n - for ; n > 0 && (b[n-1] == '\n' || b[n-1] == '\r'); n-- { - } - return string(utf16.Decode(b[:n])) -} diff --git a/vendor/github.com/go-ole/go-ole/guid.go b/vendor/github.com/go-ole/go-ole/guid.go deleted file mode 100644 index 8d20f68..0000000 --- a/vendor/github.com/go-ole/go-ole/guid.go +++ /dev/null @@ -1,284 +0,0 @@ -package ole - -var ( - // IID_NULL is null Interface ID, used when no other Interface ID is known. - IID_NULL = NewGUID("{00000000-0000-0000-0000-000000000000}") - - // IID_IUnknown is for IUnknown interfaces. - IID_IUnknown = NewGUID("{00000000-0000-0000-C000-000000000046}") - - // IID_IDispatch is for IDispatch interfaces. - IID_IDispatch = NewGUID("{00020400-0000-0000-C000-000000000046}") - - // IID_IEnumVariant is for IEnumVariant interfaces - IID_IEnumVariant = NewGUID("{00020404-0000-0000-C000-000000000046}") - - // IID_IConnectionPointContainer is for IConnectionPointContainer interfaces. - IID_IConnectionPointContainer = NewGUID("{B196B284-BAB4-101A-B69C-00AA00341D07}") - - // IID_IConnectionPoint is for IConnectionPoint interfaces. - IID_IConnectionPoint = NewGUID("{B196B286-BAB4-101A-B69C-00AA00341D07}") - - // IID_IInspectable is for IInspectable interfaces. - IID_IInspectable = NewGUID("{AF86E2E0-B12D-4C6A-9C5A-D7AA65101E90}") - - // IID_IProvideClassInfo is for IProvideClassInfo interfaces. - IID_IProvideClassInfo = NewGUID("{B196B283-BAB4-101A-B69C-00AA00341D07}") -) - -// These are for testing and not part of any library. -var ( - // IID_ICOMTestString is for ICOMTestString interfaces. - // - // {E0133EB4-C36F-469A-9D3D-C66B84BE19ED} - IID_ICOMTestString = NewGUID("{E0133EB4-C36F-469A-9D3D-C66B84BE19ED}") - - // IID_ICOMTestInt8 is for ICOMTestInt8 interfaces. - // - // {BEB06610-EB84-4155-AF58-E2BFF53680B4} - IID_ICOMTestInt8 = NewGUID("{BEB06610-EB84-4155-AF58-E2BFF53680B4}") - - // IID_ICOMTestInt16 is for ICOMTestInt16 interfaces. - // - // {DAA3F9FA-761E-4976-A860-8364CE55F6FC} - IID_ICOMTestInt16 = NewGUID("{DAA3F9FA-761E-4976-A860-8364CE55F6FC}") - - // IID_ICOMTestInt32 is for ICOMTestInt32 interfaces. - // - // {E3DEDEE7-38A2-4540-91D1-2EEF1D8891B0} - IID_ICOMTestInt32 = NewGUID("{E3DEDEE7-38A2-4540-91D1-2EEF1D8891B0}") - - // IID_ICOMTestInt64 is for ICOMTestInt64 interfaces. - // - // {8D437CBC-B3ED-485C-BC32-C336432A1623} - IID_ICOMTestInt64 = NewGUID("{8D437CBC-B3ED-485C-BC32-C336432A1623}") - - // IID_ICOMTestFloat is for ICOMTestFloat interfaces. - // - // {BF1ED004-EA02-456A-AA55-2AC8AC6B054C} - IID_ICOMTestFloat = NewGUID("{BF1ED004-EA02-456A-AA55-2AC8AC6B054C}") - - // IID_ICOMTestDouble is for ICOMTestDouble interfaces. - // - // {BF908A81-8687-4E93-999F-D86FAB284BA0} - IID_ICOMTestDouble = NewGUID("{BF908A81-8687-4E93-999F-D86FAB284BA0}") - - // IID_ICOMTestBoolean is for ICOMTestBoolean interfaces. - // - // {D530E7A6-4EE8-40D1-8931-3D63B8605010} - IID_ICOMTestBoolean = NewGUID("{D530E7A6-4EE8-40D1-8931-3D63B8605010}") - - // IID_ICOMEchoTestObject is for ICOMEchoTestObject interfaces. - // - // {6485B1EF-D780-4834-A4FE-1EBB51746CA3} - IID_ICOMEchoTestObject = NewGUID("{6485B1EF-D780-4834-A4FE-1EBB51746CA3}") - - // IID_ICOMTestTypes is for ICOMTestTypes interfaces. - // - // {CCA8D7AE-91C0-4277-A8B3-FF4EDF28D3C0} - IID_ICOMTestTypes = NewGUID("{CCA8D7AE-91C0-4277-A8B3-FF4EDF28D3C0}") - - // CLSID_COMEchoTestObject is for COMEchoTestObject class. - // - // {3C24506A-AE9E-4D50-9157-EF317281F1B0} - CLSID_COMEchoTestObject = NewGUID("{3C24506A-AE9E-4D50-9157-EF317281F1B0}") - - // CLSID_COMTestScalarClass is for COMTestScalarClass class. - // - // {865B85C5-0334-4AC6-9EF6-AACEC8FC5E86} - CLSID_COMTestScalarClass = NewGUID("{865B85C5-0334-4AC6-9EF6-AACEC8FC5E86}") -) - -const hextable = "0123456789ABCDEF" -const emptyGUID = "{00000000-0000-0000-0000-000000000000}" - -// GUID is Windows API specific GUID type. -// -// This exists to match Windows GUID type for direct passing for COM. -// Format is in xxxxxxxx-xxxx-xxxx-xxxxxxxxxxxxxxxx. -type GUID struct { - Data1 uint32 - Data2 uint16 - Data3 uint16 - Data4 [8]byte -} - -// NewGUID converts the given string into a globally unique identifier that is -// compliant with the Windows API. -// -// The supplied string may be in any of these formats: -// -// XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX -// XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX -// {XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX} -// -// The conversion of the supplied string is not case-sensitive. -func NewGUID(guid string) *GUID { - d := []byte(guid) - var d1, d2, d3, d4a, d4b []byte - - switch len(d) { - case 38: - if d[0] != '{' || d[37] != '}' { - return nil - } - d = d[1:37] - fallthrough - case 36: - if d[8] != '-' || d[13] != '-' || d[18] != '-' || d[23] != '-' { - return nil - } - d1 = d[0:8] - d2 = d[9:13] - d3 = d[14:18] - d4a = d[19:23] - d4b = d[24:36] - case 32: - d1 = d[0:8] - d2 = d[8:12] - d3 = d[12:16] - d4a = d[16:20] - d4b = d[20:32] - default: - return nil - } - - var g GUID - var ok1, ok2, ok3, ok4 bool - g.Data1, ok1 = decodeHexUint32(d1) - g.Data2, ok2 = decodeHexUint16(d2) - g.Data3, ok3 = decodeHexUint16(d3) - g.Data4, ok4 = decodeHexByte64(d4a, d4b) - if ok1 && ok2 && ok3 && ok4 { - return &g - } - return nil -} - -func decodeHexUint32(src []byte) (value uint32, ok bool) { - var b1, b2, b3, b4 byte - var ok1, ok2, ok3, ok4 bool - b1, ok1 = decodeHexByte(src[0], src[1]) - b2, ok2 = decodeHexByte(src[2], src[3]) - b3, ok3 = decodeHexByte(src[4], src[5]) - b4, ok4 = decodeHexByte(src[6], src[7]) - value = (uint32(b1) << 24) | (uint32(b2) << 16) | (uint32(b3) << 8) | uint32(b4) - ok = ok1 && ok2 && ok3 && ok4 - return -} - -func decodeHexUint16(src []byte) (value uint16, ok bool) { - var b1, b2 byte - var ok1, ok2 bool - b1, ok1 = decodeHexByte(src[0], src[1]) - b2, ok2 = decodeHexByte(src[2], src[3]) - value = (uint16(b1) << 8) | uint16(b2) - ok = ok1 && ok2 - return -} - -func decodeHexByte64(s1 []byte, s2 []byte) (value [8]byte, ok bool) { - var ok1, ok2, ok3, ok4, ok5, ok6, ok7, ok8 bool - value[0], ok1 = decodeHexByte(s1[0], s1[1]) - value[1], ok2 = decodeHexByte(s1[2], s1[3]) - value[2], ok3 = decodeHexByte(s2[0], s2[1]) - value[3], ok4 = decodeHexByte(s2[2], s2[3]) - value[4], ok5 = decodeHexByte(s2[4], s2[5]) - value[5], ok6 = decodeHexByte(s2[6], s2[7]) - value[6], ok7 = decodeHexByte(s2[8], s2[9]) - value[7], ok8 = decodeHexByte(s2[10], s2[11]) - ok = ok1 && ok2 && ok3 && ok4 && ok5 && ok6 && ok7 && ok8 - return -} - -func decodeHexByte(c1, c2 byte) (value byte, ok bool) { - var n1, n2 byte - var ok1, ok2 bool - n1, ok1 = decodeHexChar(c1) - n2, ok2 = decodeHexChar(c2) - value = (n1 << 4) | n2 - ok = ok1 && ok2 - return -} - -func decodeHexChar(c byte) (byte, bool) { - switch { - case '0' <= c && c <= '9': - return c - '0', true - case 'a' <= c && c <= 'f': - return c - 'a' + 10, true - case 'A' <= c && c <= 'F': - return c - 'A' + 10, true - } - - return 0, false -} - -// String converts the GUID to string form. It will adhere to this pattern: -// -// {XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX} -// -// If the GUID is nil, the string representation of an empty GUID is returned: -// -// {00000000-0000-0000-0000-000000000000} -func (guid *GUID) String() string { - if guid == nil { - return emptyGUID - } - - var c [38]byte - c[0] = '{' - putUint32Hex(c[1:9], guid.Data1) - c[9] = '-' - putUint16Hex(c[10:14], guid.Data2) - c[14] = '-' - putUint16Hex(c[15:19], guid.Data3) - c[19] = '-' - putByteHex(c[20:24], guid.Data4[0:2]) - c[24] = '-' - putByteHex(c[25:37], guid.Data4[2:8]) - c[37] = '}' - return string(c[:]) -} - -func putUint32Hex(b []byte, v uint32) { - b[0] = hextable[byte(v>>24)>>4] - b[1] = hextable[byte(v>>24)&0x0f] - b[2] = hextable[byte(v>>16)>>4] - b[3] = hextable[byte(v>>16)&0x0f] - b[4] = hextable[byte(v>>8)>>4] - b[5] = hextable[byte(v>>8)&0x0f] - b[6] = hextable[byte(v)>>4] - b[7] = hextable[byte(v)&0x0f] -} - -func putUint16Hex(b []byte, v uint16) { - b[0] = hextable[byte(v>>8)>>4] - b[1] = hextable[byte(v>>8)&0x0f] - b[2] = hextable[byte(v)>>4] - b[3] = hextable[byte(v)&0x0f] -} - -func putByteHex(dst, src []byte) { - for i := 0; i < len(src); i++ { - dst[i*2] = hextable[src[i]>>4] - dst[i*2+1] = hextable[src[i]&0x0f] - } -} - -// IsEqualGUID compares two GUID. -// -// Not constant time comparison. -func IsEqualGUID(guid1 *GUID, guid2 *GUID) bool { - return guid1.Data1 == guid2.Data1 && - guid1.Data2 == guid2.Data2 && - guid1.Data3 == guid2.Data3 && - guid1.Data4[0] == guid2.Data4[0] && - guid1.Data4[1] == guid2.Data4[1] && - guid1.Data4[2] == guid2.Data4[2] && - guid1.Data4[3] == guid2.Data4[3] && - guid1.Data4[4] == guid2.Data4[4] && - guid1.Data4[5] == guid2.Data4[5] && - guid1.Data4[6] == guid2.Data4[6] && - guid1.Data4[7] == guid2.Data4[7] -} diff --git a/vendor/github.com/go-ole/go-ole/iconnectionpoint.go b/vendor/github.com/go-ole/go-ole/iconnectionpoint.go deleted file mode 100644 index 9e6c49f..0000000 --- a/vendor/github.com/go-ole/go-ole/iconnectionpoint.go +++ /dev/null @@ -1,20 +0,0 @@ -package ole - -import "unsafe" - -type IConnectionPoint struct { - IUnknown -} - -type IConnectionPointVtbl struct { - IUnknownVtbl - GetConnectionInterface uintptr - GetConnectionPointContainer uintptr - Advise uintptr - Unadvise uintptr - EnumConnections uintptr -} - -func (v *IConnectionPoint) VTable() *IConnectionPointVtbl { - return (*IConnectionPointVtbl)(unsafe.Pointer(v.RawVTable)) -} diff --git a/vendor/github.com/go-ole/go-ole/iconnectionpoint_func.go b/vendor/github.com/go-ole/go-ole/iconnectionpoint_func.go deleted file mode 100644 index 5414dc3..0000000 --- a/vendor/github.com/go-ole/go-ole/iconnectionpoint_func.go +++ /dev/null @@ -1,21 +0,0 @@ -// +build !windows - -package ole - -import "unsafe" - -func (v *IConnectionPoint) GetConnectionInterface(piid **GUID) int32 { - return int32(0) -} - -func (v *IConnectionPoint) Advise(unknown *IUnknown) (uint32, error) { - return uint32(0), NewError(E_NOTIMPL) -} - -func (v *IConnectionPoint) Unadvise(cookie uint32) error { - return NewError(E_NOTIMPL) -} - -func (v *IConnectionPoint) EnumConnections(p *unsafe.Pointer) (err error) { - return NewError(E_NOTIMPL) -} diff --git a/vendor/github.com/go-ole/go-ole/iconnectionpoint_windows.go b/vendor/github.com/go-ole/go-ole/iconnectionpoint_windows.go deleted file mode 100644 index 32bc183..0000000 --- a/vendor/github.com/go-ole/go-ole/iconnectionpoint_windows.go +++ /dev/null @@ -1,43 +0,0 @@ -// +build windows - -package ole - -import ( - "syscall" - "unsafe" -) - -func (v *IConnectionPoint) GetConnectionInterface(piid **GUID) int32 { - // XXX: This doesn't look like it does what it's supposed to - return release((*IUnknown)(unsafe.Pointer(v))) -} - -func (v *IConnectionPoint) Advise(unknown *IUnknown) (cookie uint32, err error) { - hr, _, _ := syscall.Syscall( - v.VTable().Advise, - 3, - uintptr(unsafe.Pointer(v)), - uintptr(unsafe.Pointer(unknown)), - uintptr(unsafe.Pointer(&cookie))) - if hr != 0 { - err = NewError(hr) - } - return -} - -func (v *IConnectionPoint) Unadvise(cookie uint32) (err error) { - hr, _, _ := syscall.Syscall( - v.VTable().Unadvise, - 2, - uintptr(unsafe.Pointer(v)), - uintptr(cookie), - 0) - if hr != 0 { - err = NewError(hr) - } - return -} - -func (v *IConnectionPoint) EnumConnections(p *unsafe.Pointer) error { - return NewError(E_NOTIMPL) -} diff --git a/vendor/github.com/go-ole/go-ole/iconnectionpointcontainer.go b/vendor/github.com/go-ole/go-ole/iconnectionpointcontainer.go deleted file mode 100644 index 165860d..0000000 --- a/vendor/github.com/go-ole/go-ole/iconnectionpointcontainer.go +++ /dev/null @@ -1,17 +0,0 @@ -package ole - -import "unsafe" - -type IConnectionPointContainer struct { - IUnknown -} - -type IConnectionPointContainerVtbl struct { - IUnknownVtbl - EnumConnectionPoints uintptr - FindConnectionPoint uintptr -} - -func (v *IConnectionPointContainer) VTable() *IConnectionPointContainerVtbl { - return (*IConnectionPointContainerVtbl)(unsafe.Pointer(v.RawVTable)) -} diff --git a/vendor/github.com/go-ole/go-ole/iconnectionpointcontainer_func.go b/vendor/github.com/go-ole/go-ole/iconnectionpointcontainer_func.go deleted file mode 100644 index 5dfa42a..0000000 --- a/vendor/github.com/go-ole/go-ole/iconnectionpointcontainer_func.go +++ /dev/null @@ -1,11 +0,0 @@ -// +build !windows - -package ole - -func (v *IConnectionPointContainer) EnumConnectionPoints(points interface{}) error { - return NewError(E_NOTIMPL) -} - -func (v *IConnectionPointContainer) FindConnectionPoint(iid *GUID, point **IConnectionPoint) error { - return NewError(E_NOTIMPL) -} diff --git a/vendor/github.com/go-ole/go-ole/iconnectionpointcontainer_windows.go b/vendor/github.com/go-ole/go-ole/iconnectionpointcontainer_windows.go deleted file mode 100644 index ad30d79..0000000 --- a/vendor/github.com/go-ole/go-ole/iconnectionpointcontainer_windows.go +++ /dev/null @@ -1,25 +0,0 @@ -// +build windows - -package ole - -import ( - "syscall" - "unsafe" -) - -func (v *IConnectionPointContainer) EnumConnectionPoints(points interface{}) error { - return NewError(E_NOTIMPL) -} - -func (v *IConnectionPointContainer) FindConnectionPoint(iid *GUID, point **IConnectionPoint) (err error) { - hr, _, _ := syscall.Syscall( - v.VTable().FindConnectionPoint, - 3, - uintptr(unsafe.Pointer(v)), - uintptr(unsafe.Pointer(iid)), - uintptr(unsafe.Pointer(point))) - if hr != 0 { - err = NewError(hr) - } - return -} diff --git a/vendor/github.com/go-ole/go-ole/idispatch.go b/vendor/github.com/go-ole/go-ole/idispatch.go deleted file mode 100644 index d4af124..0000000 --- a/vendor/github.com/go-ole/go-ole/idispatch.go +++ /dev/null @@ -1,94 +0,0 @@ -package ole - -import "unsafe" - -type IDispatch struct { - IUnknown -} - -type IDispatchVtbl struct { - IUnknownVtbl - GetTypeInfoCount uintptr - GetTypeInfo uintptr - GetIDsOfNames uintptr - Invoke uintptr -} - -func (v *IDispatch) VTable() *IDispatchVtbl { - return (*IDispatchVtbl)(unsafe.Pointer(v.RawVTable)) -} - -func (v *IDispatch) GetIDsOfName(names []string) (dispid []int32, err error) { - dispid, err = getIDsOfName(v, names) - return -} - -func (v *IDispatch) Invoke(dispid int32, dispatch int16, params ...interface{}) (result *VARIANT, err error) { - result, err = invoke(v, dispid, dispatch, params...) - return -} - -func (v *IDispatch) GetTypeInfoCount() (c uint32, err error) { - c, err = getTypeInfoCount(v) - return -} - -func (v *IDispatch) GetTypeInfo() (tinfo *ITypeInfo, err error) { - tinfo, err = getTypeInfo(v) - return -} - -// GetSingleIDOfName is a helper that returns single display ID for IDispatch name. -// -// This replaces the common pattern of attempting to get a single name from the list of available -// IDs. It gives the first ID, if it is available. -func (v *IDispatch) GetSingleIDOfName(name string) (displayID int32, err error) { - var displayIDs []int32 - displayIDs, err = v.GetIDsOfName([]string{name}) - if err != nil { - return - } - displayID = displayIDs[0] - return -} - -// InvokeWithOptionalArgs accepts arguments as an array, works like Invoke. -// -// Accepts name and will attempt to retrieve Display ID to pass to Invoke. -// -// Passing params as an array is a workaround that could be fixed in later versions of Go that -// prevent passing empty params. During testing it was discovered that this is an acceptable way of -// getting around not being able to pass params normally. -func (v *IDispatch) InvokeWithOptionalArgs(name string, dispatch int16, params []interface{}) (result *VARIANT, err error) { - displayID, err := v.GetSingleIDOfName(name) - if err != nil { - return - } - - if len(params) < 1 { - result, err = v.Invoke(displayID, dispatch) - } else { - result, err = v.Invoke(displayID, dispatch, params...) - } - - return -} - -// CallMethod invokes named function with arguments on object. -func (v *IDispatch) CallMethod(name string, params ...interface{}) (*VARIANT, error) { - return v.InvokeWithOptionalArgs(name, DISPATCH_METHOD, params) -} - -// GetProperty retrieves the property with the name with the ability to pass arguments. -// -// Most of the time you will not need to pass arguments as most objects do not allow for this -// feature. Or at least, should not allow for this feature. Some servers don't follow best practices -// and this is provided for those edge cases. -func (v *IDispatch) GetProperty(name string, params ...interface{}) (*VARIANT, error) { - return v.InvokeWithOptionalArgs(name, DISPATCH_PROPERTYGET, params) -} - -// PutProperty attempts to mutate a property in the object. -func (v *IDispatch) PutProperty(name string, params ...interface{}) (*VARIANT, error) { - return v.InvokeWithOptionalArgs(name, DISPATCH_PROPERTYPUT, params) -} diff --git a/vendor/github.com/go-ole/go-ole/idispatch_func.go b/vendor/github.com/go-ole/go-ole/idispatch_func.go deleted file mode 100644 index b8fbbe3..0000000 --- a/vendor/github.com/go-ole/go-ole/idispatch_func.go +++ /dev/null @@ -1,19 +0,0 @@ -// +build !windows - -package ole - -func getIDsOfName(disp *IDispatch, names []string) ([]int32, error) { - return []int32{}, NewError(E_NOTIMPL) -} - -func getTypeInfoCount(disp *IDispatch) (uint32, error) { - return uint32(0), NewError(E_NOTIMPL) -} - -func getTypeInfo(disp *IDispatch) (*ITypeInfo, error) { - return nil, NewError(E_NOTIMPL) -} - -func invoke(disp *IDispatch, dispid int32, dispatch int16, params ...interface{}) (*VARIANT, error) { - return nil, NewError(E_NOTIMPL) -} diff --git a/vendor/github.com/go-ole/go-ole/idispatch_windows.go b/vendor/github.com/go-ole/go-ole/idispatch_windows.go deleted file mode 100644 index 649c073..0000000 --- a/vendor/github.com/go-ole/go-ole/idispatch_windows.go +++ /dev/null @@ -1,203 +0,0 @@ -//go:build windows -// +build windows - -package ole - -import ( - "math/big" - "syscall" - "time" - "unsafe" -) - -func getIDsOfName(disp *IDispatch, names []string) (dispid []int32, err error) { - wnames := make([]*uint16, len(names)) - for i := 0; i < len(names); i++ { - wnames[i] = syscall.StringToUTF16Ptr(names[i]) - } - dispid = make([]int32, len(names)) - namelen := uint32(len(names)) - hr, _, _ := syscall.Syscall6( - disp.VTable().GetIDsOfNames, - 6, - uintptr(unsafe.Pointer(disp)), - uintptr(unsafe.Pointer(IID_NULL)), - uintptr(unsafe.Pointer(&wnames[0])), - uintptr(namelen), - uintptr(GetUserDefaultLCID()), - uintptr(unsafe.Pointer(&dispid[0]))) - if hr != 0 { - err = NewError(hr) - } - return -} - -func getTypeInfoCount(disp *IDispatch) (c uint32, err error) { - hr, _, _ := syscall.Syscall( - disp.VTable().GetTypeInfoCount, - 2, - uintptr(unsafe.Pointer(disp)), - uintptr(unsafe.Pointer(&c)), - 0) - if hr != 0 { - err = NewError(hr) - } - return -} - -func getTypeInfo(disp *IDispatch) (tinfo *ITypeInfo, err error) { - hr, _, _ := syscall.Syscall( - disp.VTable().GetTypeInfo, - 3, - uintptr(unsafe.Pointer(disp)), - uintptr(GetUserDefaultLCID()), - uintptr(unsafe.Pointer(&tinfo))) - if hr != 0 { - err = NewError(hr) - } - return -} - -func invoke(disp *IDispatch, dispid int32, dispatch int16, params ...interface{}) (result *VARIANT, err error) { - var dispparams DISPPARAMS - - if dispatch&DISPATCH_PROPERTYPUT != 0 { - dispnames := [1]int32{DISPID_PROPERTYPUT} - dispparams.rgdispidNamedArgs = uintptr(unsafe.Pointer(&dispnames[0])) - dispparams.cNamedArgs = 1 - } else if dispatch&DISPATCH_PROPERTYPUTREF != 0 { - dispnames := [1]int32{DISPID_PROPERTYPUT} - dispparams.rgdispidNamedArgs = uintptr(unsafe.Pointer(&dispnames[0])) - dispparams.cNamedArgs = 1 - } - var vargs []VARIANT - if len(params) > 0 { - vargs = make([]VARIANT, len(params)) - for i, v := range params { - //n := len(params)-i-1 - n := len(params) - i - 1 - VariantInit(&vargs[n]) - switch vv := v.(type) { - case bool: - if vv { - vargs[n] = NewVariant(VT_BOOL, 0xffff) - } else { - vargs[n] = NewVariant(VT_BOOL, 0) - } - case *bool: - vargs[n] = NewVariant(VT_BOOL|VT_BYREF, int64(uintptr(unsafe.Pointer(v.(*bool))))) - case uint8: - vargs[n] = NewVariant(VT_I1, int64(v.(uint8))) - case *uint8: - vargs[n] = NewVariant(VT_I1|VT_BYREF, int64(uintptr(unsafe.Pointer(v.(*uint8))))) - case int8: - vargs[n] = NewVariant(VT_I1, int64(v.(int8))) - case *int8: - vargs[n] = NewVariant(VT_I1|VT_BYREF, int64(uintptr(unsafe.Pointer(v.(*int8))))) - case int16: - vargs[n] = NewVariant(VT_I2, int64(v.(int16))) - case *int16: - vargs[n] = NewVariant(VT_I2|VT_BYREF, int64(uintptr(unsafe.Pointer(v.(*int16))))) - case uint16: - vargs[n] = NewVariant(VT_UI2, int64(v.(uint16))) - case *uint16: - vargs[n] = NewVariant(VT_UI2|VT_BYREF, int64(uintptr(unsafe.Pointer(v.(*uint16))))) - case int32: - vargs[n] = NewVariant(VT_I4, int64(v.(int32))) - case *int32: - vargs[n] = NewVariant(VT_I4|VT_BYREF, int64(uintptr(unsafe.Pointer(v.(*int32))))) - case uint32: - vargs[n] = NewVariant(VT_UI4, int64(v.(uint32))) - case *uint32: - vargs[n] = NewVariant(VT_UI4|VT_BYREF, int64(uintptr(unsafe.Pointer(v.(*uint32))))) - case int64: - vargs[n] = NewVariant(VT_I8, int64(v.(int64))) - case *int64: - vargs[n] = NewVariant(VT_I8|VT_BYREF, int64(uintptr(unsafe.Pointer(v.(*int64))))) - case uint64: - vargs[n] = NewVariant(VT_UI8, int64(uintptr(v.(uint64)))) - case *uint64: - vargs[n] = NewVariant(VT_UI8|VT_BYREF, int64(uintptr(unsafe.Pointer(v.(*uint64))))) - case int: - vargs[n] = NewVariant(VT_I4, int64(v.(int))) - case *int: - vargs[n] = NewVariant(VT_I4|VT_BYREF, int64(uintptr(unsafe.Pointer(v.(*int))))) - case uint: - vargs[n] = NewVariant(VT_UI4, int64(v.(uint))) - case *uint: - vargs[n] = NewVariant(VT_UI4|VT_BYREF, int64(uintptr(unsafe.Pointer(v.(*uint))))) - case float32: - vargs[n] = NewVariant(VT_R4, *(*int64)(unsafe.Pointer(&vv))) - case *float32: - vargs[n] = NewVariant(VT_R4|VT_BYREF, int64(uintptr(unsafe.Pointer(v.(*float32))))) - case float64: - vargs[n] = NewVariant(VT_R8, *(*int64)(unsafe.Pointer(&vv))) - case *float64: - vargs[n] = NewVariant(VT_R8|VT_BYREF, int64(uintptr(unsafe.Pointer(v.(*float64))))) - case *big.Int: - vargs[n] = NewVariant(VT_DECIMAL, v.(*big.Int).Int64()) - case string: - vargs[n] = NewVariant(VT_BSTR, int64(uintptr(unsafe.Pointer(SysAllocStringLen(v.(string)))))) - case *string: - vargs[n] = NewVariant(VT_BSTR|VT_BYREF, int64(uintptr(unsafe.Pointer(v.(*string))))) - case time.Time: - s := vv.Format("2006-01-02 15:04:05") - vargs[n] = NewVariant(VT_BSTR, int64(uintptr(unsafe.Pointer(SysAllocStringLen(s))))) - case *time.Time: - s := vv.Format("2006-01-02 15:04:05") - vargs[n] = NewVariant(VT_BSTR|VT_BYREF, int64(uintptr(unsafe.Pointer(&s)))) - case *IDispatch: - vargs[n] = NewVariant(VT_DISPATCH, int64(uintptr(unsafe.Pointer(v.(*IDispatch))))) - case **IDispatch: - vargs[n] = NewVariant(VT_DISPATCH|VT_BYREF, int64(uintptr(unsafe.Pointer(v.(**IDispatch))))) - case nil: - vargs[n] = NewVariant(VT_NULL, 0) - case *VARIANT: - vargs[n] = NewVariant(VT_VARIANT|VT_BYREF, int64(uintptr(unsafe.Pointer(v.(*VARIANT))))) - case []byte: - safeByteArray := safeArrayFromByteSlice(v.([]byte)) - vargs[n] = NewVariant(VT_ARRAY|VT_UI1, int64(uintptr(unsafe.Pointer(safeByteArray)))) - defer VariantClear(&vargs[n]) - case []string: - safeByteArray := safeArrayFromStringSlice(v.([]string)) - vargs[n] = NewVariant(VT_ARRAY|VT_BSTR, int64(uintptr(unsafe.Pointer(safeByteArray)))) - defer VariantClear(&vargs[n]) - default: - panic("unknown type") - } - } - dispparams.rgvarg = uintptr(unsafe.Pointer(&vargs[0])) - dispparams.cArgs = uint32(len(params)) - } - - result = new(VARIANT) - var excepInfo EXCEPINFO - VariantInit(result) - hr, _, _ := syscall.Syscall9( - disp.VTable().Invoke, - 9, - uintptr(unsafe.Pointer(disp)), - uintptr(dispid), - uintptr(unsafe.Pointer(IID_NULL)), - uintptr(GetUserDefaultLCID()), - uintptr(dispatch), - uintptr(unsafe.Pointer(&dispparams)), - uintptr(unsafe.Pointer(result)), - uintptr(unsafe.Pointer(&excepInfo)), - 0) - if hr != 0 { - excepInfo.renderStrings() - excepInfo.Clear() - err = NewErrorWithSubError(hr, excepInfo.description, excepInfo) - } - for i, varg := range vargs { - n := len(params) - i - 1 - if varg.VT == VT_BSTR && varg.Val != 0 { - SysFreeString(((*int16)(unsafe.Pointer(uintptr(varg.Val))))) - } - if varg.VT == (VT_BSTR|VT_BYREF) && varg.Val != 0 { - *(params[n].(*string)) = LpOleStrToString(*(**uint16)(unsafe.Pointer(uintptr(varg.Val)))) - } - } - return -} diff --git a/vendor/github.com/go-ole/go-ole/ienumvariant.go b/vendor/github.com/go-ole/go-ole/ienumvariant.go deleted file mode 100644 index 2433897..0000000 --- a/vendor/github.com/go-ole/go-ole/ienumvariant.go +++ /dev/null @@ -1,19 +0,0 @@ -package ole - -import "unsafe" - -type IEnumVARIANT struct { - IUnknown -} - -type IEnumVARIANTVtbl struct { - IUnknownVtbl - Next uintptr - Skip uintptr - Reset uintptr - Clone uintptr -} - -func (v *IEnumVARIANT) VTable() *IEnumVARIANTVtbl { - return (*IEnumVARIANTVtbl)(unsafe.Pointer(v.RawVTable)) -} diff --git a/vendor/github.com/go-ole/go-ole/ienumvariant_func.go b/vendor/github.com/go-ole/go-ole/ienumvariant_func.go deleted file mode 100644 index c148481..0000000 --- a/vendor/github.com/go-ole/go-ole/ienumvariant_func.go +++ /dev/null @@ -1,19 +0,0 @@ -// +build !windows - -package ole - -func (enum *IEnumVARIANT) Clone() (*IEnumVARIANT, error) { - return nil, NewError(E_NOTIMPL) -} - -func (enum *IEnumVARIANT) Reset() error { - return NewError(E_NOTIMPL) -} - -func (enum *IEnumVARIANT) Skip(celt uint) error { - return NewError(E_NOTIMPL) -} - -func (enum *IEnumVARIANT) Next(celt uint) (VARIANT, uint, error) { - return NewVariant(VT_NULL, int64(0)), 0, NewError(E_NOTIMPL) -} diff --git a/vendor/github.com/go-ole/go-ole/ienumvariant_windows.go b/vendor/github.com/go-ole/go-ole/ienumvariant_windows.go deleted file mode 100644 index 4781f3b..0000000 --- a/vendor/github.com/go-ole/go-ole/ienumvariant_windows.go +++ /dev/null @@ -1,63 +0,0 @@ -// +build windows - -package ole - -import ( - "syscall" - "unsafe" -) - -func (enum *IEnumVARIANT) Clone() (cloned *IEnumVARIANT, err error) { - hr, _, _ := syscall.Syscall( - enum.VTable().Clone, - 2, - uintptr(unsafe.Pointer(enum)), - uintptr(unsafe.Pointer(&cloned)), - 0) - if hr != 0 { - err = NewError(hr) - } - return -} - -func (enum *IEnumVARIANT) Reset() (err error) { - hr, _, _ := syscall.Syscall( - enum.VTable().Reset, - 1, - uintptr(unsafe.Pointer(enum)), - 0, - 0) - if hr != 0 { - err = NewError(hr) - } - return -} - -func (enum *IEnumVARIANT) Skip(celt uint) (err error) { - hr, _, _ := syscall.Syscall( - enum.VTable().Skip, - 2, - uintptr(unsafe.Pointer(enum)), - uintptr(celt), - 0) - if hr != 0 { - err = NewError(hr) - } - return -} - -func (enum *IEnumVARIANT) Next(celt uint) (array VARIANT, length uint, err error) { - hr, _, _ := syscall.Syscall6( - enum.VTable().Next, - 4, - uintptr(unsafe.Pointer(enum)), - uintptr(celt), - uintptr(unsafe.Pointer(&array)), - uintptr(unsafe.Pointer(&length)), - 0, - 0) - if hr != 0 { - err = NewError(hr) - } - return -} diff --git a/vendor/github.com/go-ole/go-ole/iinspectable.go b/vendor/github.com/go-ole/go-ole/iinspectable.go deleted file mode 100644 index f4a19e2..0000000 --- a/vendor/github.com/go-ole/go-ole/iinspectable.go +++ /dev/null @@ -1,18 +0,0 @@ -package ole - -import "unsafe" - -type IInspectable struct { - IUnknown -} - -type IInspectableVtbl struct { - IUnknownVtbl - GetIIds uintptr - GetRuntimeClassName uintptr - GetTrustLevel uintptr -} - -func (v *IInspectable) VTable() *IInspectableVtbl { - return (*IInspectableVtbl)(unsafe.Pointer(v.RawVTable)) -} diff --git a/vendor/github.com/go-ole/go-ole/iinspectable_func.go b/vendor/github.com/go-ole/go-ole/iinspectable_func.go deleted file mode 100644 index 348829b..0000000 --- a/vendor/github.com/go-ole/go-ole/iinspectable_func.go +++ /dev/null @@ -1,15 +0,0 @@ -// +build !windows - -package ole - -func (v *IInspectable) GetIids() ([]*GUID, error) { - return []*GUID{}, NewError(E_NOTIMPL) -} - -func (v *IInspectable) GetRuntimeClassName() (string, error) { - return "", NewError(E_NOTIMPL) -} - -func (v *IInspectable) GetTrustLevel() (uint32, error) { - return uint32(0), NewError(E_NOTIMPL) -} diff --git a/vendor/github.com/go-ole/go-ole/iinspectable_windows.go b/vendor/github.com/go-ole/go-ole/iinspectable_windows.go deleted file mode 100644 index 4519a4a..0000000 --- a/vendor/github.com/go-ole/go-ole/iinspectable_windows.go +++ /dev/null @@ -1,72 +0,0 @@ -// +build windows - -package ole - -import ( - "bytes" - "encoding/binary" - "reflect" - "syscall" - "unsafe" -) - -func (v *IInspectable) GetIids() (iids []*GUID, err error) { - var count uint32 - var array uintptr - hr, _, _ := syscall.Syscall( - v.VTable().GetIIds, - 3, - uintptr(unsafe.Pointer(v)), - uintptr(unsafe.Pointer(&count)), - uintptr(unsafe.Pointer(&array))) - if hr != 0 { - err = NewError(hr) - return - } - defer CoTaskMemFree(array) - - iids = make([]*GUID, count) - byteCount := count * uint32(unsafe.Sizeof(GUID{})) - slicehdr := reflect.SliceHeader{Data: array, Len: int(byteCount), Cap: int(byteCount)} - byteSlice := *(*[]byte)(unsafe.Pointer(&slicehdr)) - reader := bytes.NewReader(byteSlice) - for i := range iids { - guid := GUID{} - err = binary.Read(reader, binary.LittleEndian, &guid) - if err != nil { - return - } - iids[i] = &guid - } - return -} - -func (v *IInspectable) GetRuntimeClassName() (s string, err error) { - var hstring HString - hr, _, _ := syscall.Syscall( - v.VTable().GetRuntimeClassName, - 2, - uintptr(unsafe.Pointer(v)), - uintptr(unsafe.Pointer(&hstring)), - 0) - if hr != 0 { - err = NewError(hr) - return - } - s = hstring.String() - DeleteHString(hstring) - return -} - -func (v *IInspectable) GetTrustLevel() (level uint32, err error) { - hr, _, _ := syscall.Syscall( - v.VTable().GetTrustLevel, - 2, - uintptr(unsafe.Pointer(v)), - uintptr(unsafe.Pointer(&level)), - 0) - if hr != 0 { - err = NewError(hr) - } - return -} diff --git a/vendor/github.com/go-ole/go-ole/iprovideclassinfo.go b/vendor/github.com/go-ole/go-ole/iprovideclassinfo.go deleted file mode 100644 index 25f3a6f..0000000 --- a/vendor/github.com/go-ole/go-ole/iprovideclassinfo.go +++ /dev/null @@ -1,21 +0,0 @@ -package ole - -import "unsafe" - -type IProvideClassInfo struct { - IUnknown -} - -type IProvideClassInfoVtbl struct { - IUnknownVtbl - GetClassInfo uintptr -} - -func (v *IProvideClassInfo) VTable() *IProvideClassInfoVtbl { - return (*IProvideClassInfoVtbl)(unsafe.Pointer(v.RawVTable)) -} - -func (v *IProvideClassInfo) GetClassInfo() (cinfo *ITypeInfo, err error) { - cinfo, err = getClassInfo(v) - return -} diff --git a/vendor/github.com/go-ole/go-ole/iprovideclassinfo_func.go b/vendor/github.com/go-ole/go-ole/iprovideclassinfo_func.go deleted file mode 100644 index 7e3cb63..0000000 --- a/vendor/github.com/go-ole/go-ole/iprovideclassinfo_func.go +++ /dev/null @@ -1,7 +0,0 @@ -// +build !windows - -package ole - -func getClassInfo(disp *IProvideClassInfo) (tinfo *ITypeInfo, err error) { - return nil, NewError(E_NOTIMPL) -} diff --git a/vendor/github.com/go-ole/go-ole/iprovideclassinfo_windows.go b/vendor/github.com/go-ole/go-ole/iprovideclassinfo_windows.go deleted file mode 100644 index 2ad0163..0000000 --- a/vendor/github.com/go-ole/go-ole/iprovideclassinfo_windows.go +++ /dev/null @@ -1,21 +0,0 @@ -// +build windows - -package ole - -import ( - "syscall" - "unsafe" -) - -func getClassInfo(disp *IProvideClassInfo) (tinfo *ITypeInfo, err error) { - hr, _, _ := syscall.Syscall( - disp.VTable().GetClassInfo, - 2, - uintptr(unsafe.Pointer(disp)), - uintptr(unsafe.Pointer(&tinfo)), - 0) - if hr != 0 { - err = NewError(hr) - } - return -} diff --git a/vendor/github.com/go-ole/go-ole/itypeinfo.go b/vendor/github.com/go-ole/go-ole/itypeinfo.go deleted file mode 100644 index dd3c5e2..0000000 --- a/vendor/github.com/go-ole/go-ole/itypeinfo.go +++ /dev/null @@ -1,34 +0,0 @@ -package ole - -import "unsafe" - -type ITypeInfo struct { - IUnknown -} - -type ITypeInfoVtbl struct { - IUnknownVtbl - GetTypeAttr uintptr - GetTypeComp uintptr - GetFuncDesc uintptr - GetVarDesc uintptr - GetNames uintptr - GetRefTypeOfImplType uintptr - GetImplTypeFlags uintptr - GetIDsOfNames uintptr - Invoke uintptr - GetDocumentation uintptr - GetDllEntry uintptr - GetRefTypeInfo uintptr - AddressOfMember uintptr - CreateInstance uintptr - GetMops uintptr - GetContainingTypeLib uintptr - ReleaseTypeAttr uintptr - ReleaseFuncDesc uintptr - ReleaseVarDesc uintptr -} - -func (v *ITypeInfo) VTable() *ITypeInfoVtbl { - return (*ITypeInfoVtbl)(unsafe.Pointer(v.RawVTable)) -} diff --git a/vendor/github.com/go-ole/go-ole/itypeinfo_func.go b/vendor/github.com/go-ole/go-ole/itypeinfo_func.go deleted file mode 100644 index 8364a65..0000000 --- a/vendor/github.com/go-ole/go-ole/itypeinfo_func.go +++ /dev/null @@ -1,7 +0,0 @@ -// +build !windows - -package ole - -func (v *ITypeInfo) GetTypeAttr() (*TYPEATTR, error) { - return nil, NewError(E_NOTIMPL) -} diff --git a/vendor/github.com/go-ole/go-ole/itypeinfo_windows.go b/vendor/github.com/go-ole/go-ole/itypeinfo_windows.go deleted file mode 100644 index 54782b3..0000000 --- a/vendor/github.com/go-ole/go-ole/itypeinfo_windows.go +++ /dev/null @@ -1,21 +0,0 @@ -// +build windows - -package ole - -import ( - "syscall" - "unsafe" -) - -func (v *ITypeInfo) GetTypeAttr() (tattr *TYPEATTR, err error) { - hr, _, _ := syscall.Syscall( - uintptr(v.VTable().GetTypeAttr), - 2, - uintptr(unsafe.Pointer(v)), - uintptr(unsafe.Pointer(&tattr)), - 0) - if hr != 0 { - err = NewError(hr) - } - return -} diff --git a/vendor/github.com/go-ole/go-ole/iunknown.go b/vendor/github.com/go-ole/go-ole/iunknown.go deleted file mode 100644 index 108f28e..0000000 --- a/vendor/github.com/go-ole/go-ole/iunknown.go +++ /dev/null @@ -1,57 +0,0 @@ -package ole - -import "unsafe" - -type IUnknown struct { - RawVTable *interface{} -} - -type IUnknownVtbl struct { - QueryInterface uintptr - AddRef uintptr - Release uintptr -} - -type UnknownLike interface { - QueryInterface(iid *GUID) (disp *IDispatch, err error) - AddRef() int32 - Release() int32 -} - -func (v *IUnknown) VTable() *IUnknownVtbl { - return (*IUnknownVtbl)(unsafe.Pointer(v.RawVTable)) -} - -func (v *IUnknown) PutQueryInterface(interfaceID *GUID, obj interface{}) error { - return reflectQueryInterface(v, v.VTable().QueryInterface, interfaceID, obj) -} - -func (v *IUnknown) IDispatch(interfaceID *GUID) (dispatch *IDispatch, err error) { - err = v.PutQueryInterface(interfaceID, &dispatch) - return -} - -func (v *IUnknown) IEnumVARIANT(interfaceID *GUID) (enum *IEnumVARIANT, err error) { - err = v.PutQueryInterface(interfaceID, &enum) - return -} - -func (v *IUnknown) QueryInterface(iid *GUID) (*IDispatch, error) { - return queryInterface(v, iid) -} - -func (v *IUnknown) MustQueryInterface(iid *GUID) (disp *IDispatch) { - unk, err := queryInterface(v, iid) - if err != nil { - panic(err) - } - return unk -} - -func (v *IUnknown) AddRef() int32 { - return addRef(v) -} - -func (v *IUnknown) Release() int32 { - return release(v) -} diff --git a/vendor/github.com/go-ole/go-ole/iunknown_func.go b/vendor/github.com/go-ole/go-ole/iunknown_func.go deleted file mode 100644 index d0a62cf..0000000 --- a/vendor/github.com/go-ole/go-ole/iunknown_func.go +++ /dev/null @@ -1,19 +0,0 @@ -// +build !windows - -package ole - -func reflectQueryInterface(self interface{}, method uintptr, interfaceID *GUID, obj interface{}) (err error) { - return NewError(E_NOTIMPL) -} - -func queryInterface(unk *IUnknown, iid *GUID) (disp *IDispatch, err error) { - return nil, NewError(E_NOTIMPL) -} - -func addRef(unk *IUnknown) int32 { - return 0 -} - -func release(unk *IUnknown) int32 { - return 0 -} diff --git a/vendor/github.com/go-ole/go-ole/iunknown_windows.go b/vendor/github.com/go-ole/go-ole/iunknown_windows.go deleted file mode 100644 index ede5bb8..0000000 --- a/vendor/github.com/go-ole/go-ole/iunknown_windows.go +++ /dev/null @@ -1,58 +0,0 @@ -// +build windows - -package ole - -import ( - "reflect" - "syscall" - "unsafe" -) - -func reflectQueryInterface(self interface{}, method uintptr, interfaceID *GUID, obj interface{}) (err error) { - selfValue := reflect.ValueOf(self).Elem() - objValue := reflect.ValueOf(obj).Elem() - - hr, _, _ := syscall.Syscall( - method, - 3, - selfValue.UnsafeAddr(), - uintptr(unsafe.Pointer(interfaceID)), - objValue.Addr().Pointer()) - if hr != 0 { - err = NewError(hr) - } - return -} - -func queryInterface(unk *IUnknown, iid *GUID) (disp *IDispatch, err error) { - hr, _, _ := syscall.Syscall( - unk.VTable().QueryInterface, - 3, - uintptr(unsafe.Pointer(unk)), - uintptr(unsafe.Pointer(iid)), - uintptr(unsafe.Pointer(&disp))) - if hr != 0 { - err = NewError(hr) - } - return -} - -func addRef(unk *IUnknown) int32 { - ret, _, _ := syscall.Syscall( - unk.VTable().AddRef, - 1, - uintptr(unsafe.Pointer(unk)), - 0, - 0) - return int32(ret) -} - -func release(unk *IUnknown) int32 { - ret, _, _ := syscall.Syscall( - unk.VTable().Release, - 1, - uintptr(unsafe.Pointer(unk)), - 0, - 0) - return int32(ret) -} diff --git a/vendor/github.com/go-ole/go-ole/ole.go b/vendor/github.com/go-ole/go-ole/ole.go deleted file mode 100644 index dbd132b..0000000 --- a/vendor/github.com/go-ole/go-ole/ole.go +++ /dev/null @@ -1,190 +0,0 @@ -package ole - -import ( - "fmt" - "strings" - "unsafe" -) - -// DISPPARAMS are the arguments that passed to methods or property. -type DISPPARAMS struct { - rgvarg uintptr - rgdispidNamedArgs uintptr - cArgs uint32 - cNamedArgs uint32 -} - -// EXCEPINFO defines exception info. -type EXCEPINFO struct { - wCode uint16 - wReserved uint16 - bstrSource *uint16 - bstrDescription *uint16 - bstrHelpFile *uint16 - dwHelpContext uint32 - pvReserved uintptr - pfnDeferredFillIn uintptr - scode uint32 - - // Go-specific part. Don't move upper cos it'll break structure layout for native code. - rendered bool - source string - description string - helpFile string -} - -// renderStrings translates BSTR strings to Go ones so `.Error` and `.String` -// could be safely called after `.Clear`. We need this when we can't rely on -// a caller to call `.Clear`. -func (e *EXCEPINFO) renderStrings() { - e.rendered = true - if e.bstrSource == nil { - e.source = "" - } else { - e.source = BstrToString(e.bstrSource) - } - if e.bstrDescription == nil { - e.description = "" - } else { - e.description = BstrToString(e.bstrDescription) - } - if e.bstrHelpFile == nil { - e.helpFile = "" - } else { - e.helpFile = BstrToString(e.bstrHelpFile) - } -} - -// Clear frees BSTR strings inside an EXCEPINFO and set it to NULL. -func (e *EXCEPINFO) Clear() { - freeBSTR := func(s *uint16) { - // SysFreeString don't return errors and is safe for call's on NULL. - // https://docs.microsoft.com/en-us/windows/win32/api/oleauto/nf-oleauto-sysfreestring - _ = SysFreeString((*int16)(unsafe.Pointer(s))) - } - - if e.bstrSource != nil { - freeBSTR(e.bstrSource) - e.bstrSource = nil - } - if e.bstrDescription != nil { - freeBSTR(e.bstrDescription) - e.bstrDescription = nil - } - if e.bstrHelpFile != nil { - freeBSTR(e.bstrHelpFile) - e.bstrHelpFile = nil - } -} - -// WCode return wCode in EXCEPINFO. -func (e EXCEPINFO) WCode() uint16 { - return e.wCode -} - -// SCODE return scode in EXCEPINFO. -func (e EXCEPINFO) SCODE() uint32 { - return e.scode -} - -// String convert EXCEPINFO to string. -func (e EXCEPINFO) String() string { - if !e.rendered { - e.renderStrings() - } - return fmt.Sprintf( - "wCode: %#x, bstrSource: %v, bstrDescription: %v, bstrHelpFile: %v, dwHelpContext: %#x, scode: %#x", - e.wCode, e.source, e.description, e.helpFile, e.dwHelpContext, e.scode, - ) -} - -// Error implements error interface and returns error string. -func (e EXCEPINFO) Error() string { - if !e.rendered { - e.renderStrings() - } - - if e.description != "" { - return strings.TrimSpace(e.description) - } - - code := e.scode - if e.wCode != 0 { - code = uint32(e.wCode) - } - return fmt.Sprintf("%v: %#x", e.source, code) -} - -// PARAMDATA defines parameter data type. -type PARAMDATA struct { - Name *int16 - Vt uint16 -} - -// METHODDATA defines method info. -type METHODDATA struct { - Name *uint16 - Data *PARAMDATA - Dispid int32 - Meth uint32 - CC int32 - CArgs uint32 - Flags uint16 - VtReturn uint32 -} - -// INTERFACEDATA defines interface info. -type INTERFACEDATA struct { - MethodData *METHODDATA - CMembers uint32 -} - -// Point is 2D vector type. -type Point struct { - X int32 - Y int32 -} - -// Msg is message between processes. -type Msg struct { - Hwnd uint32 - Message uint32 - Wparam int32 - Lparam int32 - Time uint32 - Pt Point -} - -// TYPEDESC defines data type. -type TYPEDESC struct { - Hreftype uint32 - VT uint16 -} - -// IDLDESC defines IDL info. -type IDLDESC struct { - DwReserved uint32 - WIDLFlags uint16 -} - -// TYPEATTR defines type info. -type TYPEATTR struct { - Guid GUID - Lcid uint32 - dwReserved uint32 - MemidConstructor int32 - MemidDestructor int32 - LpstrSchema *uint16 - CbSizeInstance uint32 - Typekind int32 - CFuncs uint16 - CVars uint16 - CImplTypes uint16 - CbSizeVft uint16 - CbAlignment uint16 - WTypeFlags uint16 - WMajorVerNum uint16 - WMinorVerNum uint16 - TdescAlias TYPEDESC - IdldescType IDLDESC -} diff --git a/vendor/github.com/go-ole/go-ole/safearray.go b/vendor/github.com/go-ole/go-ole/safearray.go deleted file mode 100644 index a5201b5..0000000 --- a/vendor/github.com/go-ole/go-ole/safearray.go +++ /dev/null @@ -1,27 +0,0 @@ -// Package is meant to retrieve and process safe array data returned from COM. - -package ole - -// SafeArrayBound defines the SafeArray boundaries. -type SafeArrayBound struct { - Elements uint32 - LowerBound int32 -} - -// SafeArray is how COM handles arrays. -type SafeArray struct { - Dimensions uint16 - FeaturesFlag uint16 - ElementsSize uint32 - LocksAmount uint32 - Data uint32 - Bounds [16]byte -} - -// SAFEARRAY is obsolete, exists for backwards compatibility. -// Use SafeArray -type SAFEARRAY SafeArray - -// SAFEARRAYBOUND is obsolete, exists for backwards compatibility. -// Use SafeArrayBound -type SAFEARRAYBOUND SafeArrayBound diff --git a/vendor/github.com/go-ole/go-ole/safearray_func.go b/vendor/github.com/go-ole/go-ole/safearray_func.go deleted file mode 100644 index 0dee670..0000000 --- a/vendor/github.com/go-ole/go-ole/safearray_func.go +++ /dev/null @@ -1,211 +0,0 @@ -// +build !windows - -package ole - -import ( - "unsafe" -) - -// safeArrayAccessData returns raw array pointer. -// -// AKA: SafeArrayAccessData in Windows API. -func safeArrayAccessData(safearray *SafeArray) (uintptr, error) { - return uintptr(0), NewError(E_NOTIMPL) -} - -// safeArrayUnaccessData releases raw array. -// -// AKA: SafeArrayUnaccessData in Windows API. -func safeArrayUnaccessData(safearray *SafeArray) error { - return NewError(E_NOTIMPL) -} - -// safeArrayAllocData allocates SafeArray. -// -// AKA: SafeArrayAllocData in Windows API. -func safeArrayAllocData(safearray *SafeArray) error { - return NewError(E_NOTIMPL) -} - -// safeArrayAllocDescriptor allocates SafeArray. -// -// AKA: SafeArrayAllocDescriptor in Windows API. -func safeArrayAllocDescriptor(dimensions uint32) (*SafeArray, error) { - return nil, NewError(E_NOTIMPL) -} - -// safeArrayAllocDescriptorEx allocates SafeArray. -// -// AKA: SafeArrayAllocDescriptorEx in Windows API. -func safeArrayAllocDescriptorEx(variantType VT, dimensions uint32) (*SafeArray, error) { - return nil, NewError(E_NOTIMPL) -} - -// safeArrayCopy returns copy of SafeArray. -// -// AKA: SafeArrayCopy in Windows API. -func safeArrayCopy(original *SafeArray) (*SafeArray, error) { - return nil, NewError(E_NOTIMPL) -} - -// safeArrayCopyData duplicates SafeArray into another SafeArray object. -// -// AKA: SafeArrayCopyData in Windows API. -func safeArrayCopyData(original *SafeArray, duplicate *SafeArray) error { - return NewError(E_NOTIMPL) -} - -// safeArrayCreate creates SafeArray. -// -// AKA: SafeArrayCreate in Windows API. -func safeArrayCreate(variantType VT, dimensions uint32, bounds *SafeArrayBound) (*SafeArray, error) { - return nil, NewError(E_NOTIMPL) -} - -// safeArrayCreateEx creates SafeArray. -// -// AKA: SafeArrayCreateEx in Windows API. -func safeArrayCreateEx(variantType VT, dimensions uint32, bounds *SafeArrayBound, extra uintptr) (*SafeArray, error) { - return nil, NewError(E_NOTIMPL) -} - -// safeArrayCreateVector creates SafeArray. -// -// AKA: SafeArrayCreateVector in Windows API. -func safeArrayCreateVector(variantType VT, lowerBound int32, length uint32) (*SafeArray, error) { - return nil, NewError(E_NOTIMPL) -} - -// safeArrayCreateVectorEx creates SafeArray. -// -// AKA: SafeArrayCreateVectorEx in Windows API. -func safeArrayCreateVectorEx(variantType VT, lowerBound int32, length uint32, extra uintptr) (*SafeArray, error) { - return nil, NewError(E_NOTIMPL) -} - -// safeArrayDestroy destroys SafeArray object. -// -// AKA: SafeArrayDestroy in Windows API. -func safeArrayDestroy(safearray *SafeArray) error { - return NewError(E_NOTIMPL) -} - -// safeArrayDestroyData destroys SafeArray object. -// -// AKA: SafeArrayDestroyData in Windows API. -func safeArrayDestroyData(safearray *SafeArray) error { - return NewError(E_NOTIMPL) -} - -// safeArrayDestroyDescriptor destroys SafeArray object. -// -// AKA: SafeArrayDestroyDescriptor in Windows API. -func safeArrayDestroyDescriptor(safearray *SafeArray) error { - return NewError(E_NOTIMPL) -} - -// safeArrayGetDim is the amount of dimensions in the SafeArray. -// -// SafeArrays may have multiple dimensions. Meaning, it could be -// multidimensional array. -// -// AKA: SafeArrayGetDim in Windows API. -func safeArrayGetDim(safearray *SafeArray) (*uint32, error) { - u := uint32(0) - return &u, NewError(E_NOTIMPL) -} - -// safeArrayGetElementSize is the element size in bytes. -// -// AKA: SafeArrayGetElemsize in Windows API. -func safeArrayGetElementSize(safearray *SafeArray) (*uint32, error) { - u := uint32(0) - return &u, NewError(E_NOTIMPL) -} - -// safeArrayGetElement retrieves element at given index. -func safeArrayGetElement(safearray *SafeArray, index int32, pv unsafe.Pointer) error { - return NewError(E_NOTIMPL) -} - -// safeArrayGetElement retrieves element at given index and converts to string. -func safeArrayGetElementString(safearray *SafeArray, index int32) (string, error) { - return "", NewError(E_NOTIMPL) -} - -// safeArrayGetIID is the InterfaceID of the elements in the SafeArray. -// -// AKA: SafeArrayGetIID in Windows API. -func safeArrayGetIID(safearray *SafeArray) (*GUID, error) { - return nil, NewError(E_NOTIMPL) -} - -// safeArrayGetLBound returns lower bounds of SafeArray. -// -// SafeArrays may have multiple dimensions. Meaning, it could be -// multidimensional array. -// -// AKA: SafeArrayGetLBound in Windows API. -func safeArrayGetLBound(safearray *SafeArray, dimension uint32) (int32, error) { - return int32(0), NewError(E_NOTIMPL) -} - -// safeArrayGetUBound returns upper bounds of SafeArray. -// -// SafeArrays may have multiple dimensions. Meaning, it could be -// multidimensional array. -// -// AKA: SafeArrayGetUBound in Windows API. -func safeArrayGetUBound(safearray *SafeArray, dimension uint32) (int32, error) { - return int32(0), NewError(E_NOTIMPL) -} - -// safeArrayGetVartype returns data type of SafeArray. -// -// AKA: SafeArrayGetVartype in Windows API. -func safeArrayGetVartype(safearray *SafeArray) (uint16, error) { - return uint16(0), NewError(E_NOTIMPL) -} - -// safeArrayLock locks SafeArray for reading to modify SafeArray. -// -// This must be called during some calls to ensure that another process does not -// read or write to the SafeArray during editing. -// -// AKA: SafeArrayLock in Windows API. -func safeArrayLock(safearray *SafeArray) error { - return NewError(E_NOTIMPL) -} - -// safeArrayUnlock unlocks SafeArray for reading. -// -// AKA: SafeArrayUnlock in Windows API. -func safeArrayUnlock(safearray *SafeArray) error { - return NewError(E_NOTIMPL) -} - -// safeArrayPutElement stores the data element at the specified location in the -// array. -// -// AKA: SafeArrayPutElement in Windows API. -func safeArrayPutElement(safearray *SafeArray, index int64, element uintptr) error { - return NewError(E_NOTIMPL) -} - -// safeArrayGetRecordInfo accesses IRecordInfo info for custom types. -// -// AKA: SafeArrayGetRecordInfo in Windows API. -// -// XXX: Must implement IRecordInfo interface for this to return. -func safeArrayGetRecordInfo(safearray *SafeArray) (interface{}, error) { - return nil, NewError(E_NOTIMPL) -} - -// safeArraySetRecordInfo mutates IRecordInfo info for custom types. -// -// AKA: SafeArraySetRecordInfo in Windows API. -// -// XXX: Must implement IRecordInfo interface for this to return. -func safeArraySetRecordInfo(safearray *SafeArray, recordInfo interface{}) error { - return NewError(E_NOTIMPL) -} diff --git a/vendor/github.com/go-ole/go-ole/safearray_windows.go b/vendor/github.com/go-ole/go-ole/safearray_windows.go deleted file mode 100644 index 0c1b3a1..0000000 --- a/vendor/github.com/go-ole/go-ole/safearray_windows.go +++ /dev/null @@ -1,337 +0,0 @@ -// +build windows - -package ole - -import ( - "unsafe" -) - -var ( - procSafeArrayAccessData = modoleaut32.NewProc("SafeArrayAccessData") - procSafeArrayAllocData = modoleaut32.NewProc("SafeArrayAllocData") - procSafeArrayAllocDescriptor = modoleaut32.NewProc("SafeArrayAllocDescriptor") - procSafeArrayAllocDescriptorEx = modoleaut32.NewProc("SafeArrayAllocDescriptorEx") - procSafeArrayCopy = modoleaut32.NewProc("SafeArrayCopy") - procSafeArrayCopyData = modoleaut32.NewProc("SafeArrayCopyData") - procSafeArrayCreate = modoleaut32.NewProc("SafeArrayCreate") - procSafeArrayCreateEx = modoleaut32.NewProc("SafeArrayCreateEx") - procSafeArrayCreateVector = modoleaut32.NewProc("SafeArrayCreateVector") - procSafeArrayCreateVectorEx = modoleaut32.NewProc("SafeArrayCreateVectorEx") - procSafeArrayDestroy = modoleaut32.NewProc("SafeArrayDestroy") - procSafeArrayDestroyData = modoleaut32.NewProc("SafeArrayDestroyData") - procSafeArrayDestroyDescriptor = modoleaut32.NewProc("SafeArrayDestroyDescriptor") - procSafeArrayGetDim = modoleaut32.NewProc("SafeArrayGetDim") - procSafeArrayGetElement = modoleaut32.NewProc("SafeArrayGetElement") - procSafeArrayGetElemsize = modoleaut32.NewProc("SafeArrayGetElemsize") - procSafeArrayGetIID = modoleaut32.NewProc("SafeArrayGetIID") - procSafeArrayGetLBound = modoleaut32.NewProc("SafeArrayGetLBound") - procSafeArrayGetUBound = modoleaut32.NewProc("SafeArrayGetUBound") - procSafeArrayGetVartype = modoleaut32.NewProc("SafeArrayGetVartype") - procSafeArrayLock = modoleaut32.NewProc("SafeArrayLock") - procSafeArrayPtrOfIndex = modoleaut32.NewProc("SafeArrayPtrOfIndex") - procSafeArrayUnaccessData = modoleaut32.NewProc("SafeArrayUnaccessData") - procSafeArrayUnlock = modoleaut32.NewProc("SafeArrayUnlock") - procSafeArrayPutElement = modoleaut32.NewProc("SafeArrayPutElement") - //procSafeArrayRedim = modoleaut32.NewProc("SafeArrayRedim") // TODO - //procSafeArraySetIID = modoleaut32.NewProc("SafeArraySetIID") // TODO - procSafeArrayGetRecordInfo = modoleaut32.NewProc("SafeArrayGetRecordInfo") - procSafeArraySetRecordInfo = modoleaut32.NewProc("SafeArraySetRecordInfo") -) - -// safeArrayAccessData returns raw array pointer. -// -// AKA: SafeArrayAccessData in Windows API. -// Todo: Test -func safeArrayAccessData(safearray *SafeArray) (element uintptr, err error) { - err = convertHresultToError( - procSafeArrayAccessData.Call( - uintptr(unsafe.Pointer(safearray)), - uintptr(unsafe.Pointer(&element)))) - return -} - -// safeArrayUnaccessData releases raw array. -// -// AKA: SafeArrayUnaccessData in Windows API. -func safeArrayUnaccessData(safearray *SafeArray) (err error) { - err = convertHresultToError(procSafeArrayUnaccessData.Call(uintptr(unsafe.Pointer(safearray)))) - return -} - -// safeArrayAllocData allocates SafeArray. -// -// AKA: SafeArrayAllocData in Windows API. -func safeArrayAllocData(safearray *SafeArray) (err error) { - err = convertHresultToError(procSafeArrayAllocData.Call(uintptr(unsafe.Pointer(safearray)))) - return -} - -// safeArrayAllocDescriptor allocates SafeArray. -// -// AKA: SafeArrayAllocDescriptor in Windows API. -func safeArrayAllocDescriptor(dimensions uint32) (safearray *SafeArray, err error) { - err = convertHresultToError( - procSafeArrayAllocDescriptor.Call(uintptr(dimensions), uintptr(unsafe.Pointer(&safearray)))) - return -} - -// safeArrayAllocDescriptorEx allocates SafeArray. -// -// AKA: SafeArrayAllocDescriptorEx in Windows API. -func safeArrayAllocDescriptorEx(variantType VT, dimensions uint32) (safearray *SafeArray, err error) { - err = convertHresultToError( - procSafeArrayAllocDescriptorEx.Call( - uintptr(variantType), - uintptr(dimensions), - uintptr(unsafe.Pointer(&safearray)))) - return -} - -// safeArrayCopy returns copy of SafeArray. -// -// AKA: SafeArrayCopy in Windows API. -func safeArrayCopy(original *SafeArray) (safearray *SafeArray, err error) { - err = convertHresultToError( - procSafeArrayCopy.Call( - uintptr(unsafe.Pointer(original)), - uintptr(unsafe.Pointer(&safearray)))) - return -} - -// safeArrayCopyData duplicates SafeArray into another SafeArray object. -// -// AKA: SafeArrayCopyData in Windows API. -func safeArrayCopyData(original *SafeArray, duplicate *SafeArray) (err error) { - err = convertHresultToError( - procSafeArrayCopyData.Call( - uintptr(unsafe.Pointer(original)), - uintptr(unsafe.Pointer(duplicate)))) - return -} - -// safeArrayCreate creates SafeArray. -// -// AKA: SafeArrayCreate in Windows API. -func safeArrayCreate(variantType VT, dimensions uint32, bounds *SafeArrayBound) (safearray *SafeArray, err error) { - sa, _, err := procSafeArrayCreate.Call( - uintptr(variantType), - uintptr(dimensions), - uintptr(unsafe.Pointer(bounds))) - safearray = (*SafeArray)(unsafe.Pointer(&sa)) - return -} - -// safeArrayCreateEx creates SafeArray. -// -// AKA: SafeArrayCreateEx in Windows API. -func safeArrayCreateEx(variantType VT, dimensions uint32, bounds *SafeArrayBound, extra uintptr) (safearray *SafeArray, err error) { - sa, _, err := procSafeArrayCreateEx.Call( - uintptr(variantType), - uintptr(dimensions), - uintptr(unsafe.Pointer(bounds)), - extra) - safearray = (*SafeArray)(unsafe.Pointer(sa)) - return -} - -// safeArrayCreateVector creates SafeArray. -// -// AKA: SafeArrayCreateVector in Windows API. -func safeArrayCreateVector(variantType VT, lowerBound int32, length uint32) (safearray *SafeArray, err error) { - sa, _, err := procSafeArrayCreateVector.Call( - uintptr(variantType), - uintptr(lowerBound), - uintptr(length)) - safearray = (*SafeArray)(unsafe.Pointer(sa)) - return -} - -// safeArrayCreateVectorEx creates SafeArray. -// -// AKA: SafeArrayCreateVectorEx in Windows API. -func safeArrayCreateVectorEx(variantType VT, lowerBound int32, length uint32, extra uintptr) (safearray *SafeArray, err error) { - sa, _, err := procSafeArrayCreateVectorEx.Call( - uintptr(variantType), - uintptr(lowerBound), - uintptr(length), - extra) - safearray = (*SafeArray)(unsafe.Pointer(sa)) - return -} - -// safeArrayDestroy destroys SafeArray object. -// -// AKA: SafeArrayDestroy in Windows API. -func safeArrayDestroy(safearray *SafeArray) (err error) { - err = convertHresultToError(procSafeArrayDestroy.Call(uintptr(unsafe.Pointer(safearray)))) - return -} - -// safeArrayDestroyData destroys SafeArray object. -// -// AKA: SafeArrayDestroyData in Windows API. -func safeArrayDestroyData(safearray *SafeArray) (err error) { - err = convertHresultToError(procSafeArrayDestroyData.Call(uintptr(unsafe.Pointer(safearray)))) - return -} - -// safeArrayDestroyDescriptor destroys SafeArray object. -// -// AKA: SafeArrayDestroyDescriptor in Windows API. -func safeArrayDestroyDescriptor(safearray *SafeArray) (err error) { - err = convertHresultToError(procSafeArrayDestroyDescriptor.Call(uintptr(unsafe.Pointer(safearray)))) - return -} - -// safeArrayGetDim is the amount of dimensions in the SafeArray. -// -// SafeArrays may have multiple dimensions. Meaning, it could be -// multidimensional array. -// -// AKA: SafeArrayGetDim in Windows API. -func safeArrayGetDim(safearray *SafeArray) (dimensions *uint32, err error) { - l, _, err := procSafeArrayGetDim.Call(uintptr(unsafe.Pointer(safearray))) - dimensions = (*uint32)(unsafe.Pointer(l)) - return -} - -// safeArrayGetElementSize is the element size in bytes. -// -// AKA: SafeArrayGetElemsize in Windows API. -func safeArrayGetElementSize(safearray *SafeArray) (length *uint32, err error) { - l, _, err := procSafeArrayGetElemsize.Call(uintptr(unsafe.Pointer(safearray))) - length = (*uint32)(unsafe.Pointer(l)) - return -} - -// safeArrayGetElement retrieves element at given index. -func safeArrayGetElement(safearray *SafeArray, index int32, pv unsafe.Pointer) error { - return convertHresultToError( - procSafeArrayGetElement.Call( - uintptr(unsafe.Pointer(safearray)), - uintptr(unsafe.Pointer(&index)), - uintptr(pv))) -} - -// safeArrayGetElementString retrieves element at given index and converts to string. -func safeArrayGetElementString(safearray *SafeArray, index int32) (str string, err error) { - var element *int16 - err = convertHresultToError( - procSafeArrayGetElement.Call( - uintptr(unsafe.Pointer(safearray)), - uintptr(unsafe.Pointer(&index)), - uintptr(unsafe.Pointer(&element)))) - str = BstrToString(*(**uint16)(unsafe.Pointer(&element))) - SysFreeString(element) - return -} - -// safeArrayGetIID is the InterfaceID of the elements in the SafeArray. -// -// AKA: SafeArrayGetIID in Windows API. -func safeArrayGetIID(safearray *SafeArray) (guid *GUID, err error) { - err = convertHresultToError( - procSafeArrayGetIID.Call( - uintptr(unsafe.Pointer(safearray)), - uintptr(unsafe.Pointer(&guid)))) - return -} - -// safeArrayGetLBound returns lower bounds of SafeArray. -// -// SafeArrays may have multiple dimensions. Meaning, it could be -// multidimensional array. -// -// AKA: SafeArrayGetLBound in Windows API. -func safeArrayGetLBound(safearray *SafeArray, dimension uint32) (lowerBound int32, err error) { - err = convertHresultToError( - procSafeArrayGetLBound.Call( - uintptr(unsafe.Pointer(safearray)), - uintptr(dimension), - uintptr(unsafe.Pointer(&lowerBound)))) - return -} - -// safeArrayGetUBound returns upper bounds of SafeArray. -// -// SafeArrays may have multiple dimensions. Meaning, it could be -// multidimensional array. -// -// AKA: SafeArrayGetUBound in Windows API. -func safeArrayGetUBound(safearray *SafeArray, dimension uint32) (upperBound int32, err error) { - err = convertHresultToError( - procSafeArrayGetUBound.Call( - uintptr(unsafe.Pointer(safearray)), - uintptr(dimension), - uintptr(unsafe.Pointer(&upperBound)))) - return -} - -// safeArrayGetVartype returns data type of SafeArray. -// -// AKA: SafeArrayGetVartype in Windows API. -func safeArrayGetVartype(safearray *SafeArray) (varType uint16, err error) { - err = convertHresultToError( - procSafeArrayGetVartype.Call( - uintptr(unsafe.Pointer(safearray)), - uintptr(unsafe.Pointer(&varType)))) - return -} - -// safeArrayLock locks SafeArray for reading to modify SafeArray. -// -// This must be called during some calls to ensure that another process does not -// read or write to the SafeArray during editing. -// -// AKA: SafeArrayLock in Windows API. -func safeArrayLock(safearray *SafeArray) (err error) { - err = convertHresultToError(procSafeArrayLock.Call(uintptr(unsafe.Pointer(safearray)))) - return -} - -// safeArrayUnlock unlocks SafeArray for reading. -// -// AKA: SafeArrayUnlock in Windows API. -func safeArrayUnlock(safearray *SafeArray) (err error) { - err = convertHresultToError(procSafeArrayUnlock.Call(uintptr(unsafe.Pointer(safearray)))) - return -} - -// safeArrayPutElement stores the data element at the specified location in the -// array. -// -// AKA: SafeArrayPutElement in Windows API. -func safeArrayPutElement(safearray *SafeArray, index int64, element uintptr) (err error) { - err = convertHresultToError( - procSafeArrayPutElement.Call( - uintptr(unsafe.Pointer(safearray)), - uintptr(unsafe.Pointer(&index)), - uintptr(unsafe.Pointer(element)))) - return -} - -// safeArrayGetRecordInfo accesses IRecordInfo info for custom types. -// -// AKA: SafeArrayGetRecordInfo in Windows API. -// -// XXX: Must implement IRecordInfo interface for this to return. -func safeArrayGetRecordInfo(safearray *SafeArray) (recordInfo interface{}, err error) { - err = convertHresultToError( - procSafeArrayGetRecordInfo.Call( - uintptr(unsafe.Pointer(safearray)), - uintptr(unsafe.Pointer(&recordInfo)))) - return -} - -// safeArraySetRecordInfo mutates IRecordInfo info for custom types. -// -// AKA: SafeArraySetRecordInfo in Windows API. -// -// XXX: Must implement IRecordInfo interface for this to return. -func safeArraySetRecordInfo(safearray *SafeArray, recordInfo interface{}) (err error) { - err = convertHresultToError( - procSafeArraySetRecordInfo.Call( - uintptr(unsafe.Pointer(safearray)), - uintptr(unsafe.Pointer(&recordInfo)))) - return -} diff --git a/vendor/github.com/go-ole/go-ole/safearrayconversion.go b/vendor/github.com/go-ole/go-ole/safearrayconversion.go deleted file mode 100644 index da73729..0000000 --- a/vendor/github.com/go-ole/go-ole/safearrayconversion.go +++ /dev/null @@ -1,140 +0,0 @@ -// Helper for converting SafeArray to array of objects. - -package ole - -import ( - "unsafe" -) - -type SafeArrayConversion struct { - Array *SafeArray -} - -func (sac *SafeArrayConversion) ToStringArray() (strings []string) { - totalElements, _ := sac.TotalElements(0) - strings = make([]string, totalElements) - - for i := int32(0); i < totalElements; i++ { - strings[int32(i)], _ = safeArrayGetElementString(sac.Array, i) - } - - return -} - -func (sac *SafeArrayConversion) ToByteArray() (bytes []byte) { - totalElements, _ := sac.TotalElements(0) - bytes = make([]byte, totalElements) - - for i := int32(0); i < totalElements; i++ { - safeArrayGetElement(sac.Array, i, unsafe.Pointer(&bytes[int32(i)])) - } - - return -} - -func (sac *SafeArrayConversion) ToValueArray() (values []interface{}) { - totalElements, _ := sac.TotalElements(0) - values = make([]interface{}, totalElements) - vt, _ := safeArrayGetVartype(sac.Array) - - for i := int32(0); i < totalElements; i++ { - switch VT(vt) { - case VT_BOOL: - var v bool - safeArrayGetElement(sac.Array, i, unsafe.Pointer(&v)) - values[i] = v - case VT_I1: - var v int8 - safeArrayGetElement(sac.Array, i, unsafe.Pointer(&v)) - values[i] = v - case VT_I2: - var v int16 - safeArrayGetElement(sac.Array, i, unsafe.Pointer(&v)) - values[i] = v - case VT_I4: - var v int32 - safeArrayGetElement(sac.Array, i, unsafe.Pointer(&v)) - values[i] = v - case VT_I8: - var v int64 - safeArrayGetElement(sac.Array, i, unsafe.Pointer(&v)) - values[i] = v - case VT_UI1: - var v uint8 - safeArrayGetElement(sac.Array, i, unsafe.Pointer(&v)) - values[i] = v - case VT_UI2: - var v uint16 - safeArrayGetElement(sac.Array, i, unsafe.Pointer(&v)) - values[i] = v - case VT_UI4: - var v uint32 - safeArrayGetElement(sac.Array, i, unsafe.Pointer(&v)) - values[i] = v - case VT_UI8: - var v uint64 - safeArrayGetElement(sac.Array, i, unsafe.Pointer(&v)) - values[i] = v - case VT_R4: - var v float32 - safeArrayGetElement(sac.Array, i, unsafe.Pointer(&v)) - values[i] = v - case VT_R8: - var v float64 - safeArrayGetElement(sac.Array, i, unsafe.Pointer(&v)) - values[i] = v - case VT_BSTR: - v , _ := safeArrayGetElementString(sac.Array, i) - values[i] = v - case VT_VARIANT: - var v VARIANT - safeArrayGetElement(sac.Array, i, unsafe.Pointer(&v)) - values[i] = v.Value() - v.Clear() - default: - // TODO - } - } - - return -} - -func (sac *SafeArrayConversion) GetType() (varType uint16, err error) { - return safeArrayGetVartype(sac.Array) -} - -func (sac *SafeArrayConversion) GetDimensions() (dimensions *uint32, err error) { - return safeArrayGetDim(sac.Array) -} - -func (sac *SafeArrayConversion) GetSize() (length *uint32, err error) { - return safeArrayGetElementSize(sac.Array) -} - -func (sac *SafeArrayConversion) TotalElements(index uint32) (totalElements int32, err error) { - if index < 1 { - index = 1 - } - - // Get array bounds - var LowerBounds int32 - var UpperBounds int32 - - LowerBounds, err = safeArrayGetLBound(sac.Array, index) - if err != nil { - return - } - - UpperBounds, err = safeArrayGetUBound(sac.Array, index) - if err != nil { - return - } - - totalElements = UpperBounds - LowerBounds + 1 - return -} - -// Release Safe Array memory -func (sac *SafeArrayConversion) Release() { - safeArrayDestroy(sac.Array) -} diff --git a/vendor/github.com/go-ole/go-ole/safearrayslices.go b/vendor/github.com/go-ole/go-ole/safearrayslices.go deleted file mode 100644 index a9fa885..0000000 --- a/vendor/github.com/go-ole/go-ole/safearrayslices.go +++ /dev/null @@ -1,33 +0,0 @@ -// +build windows - -package ole - -import ( - "unsafe" -) - -func safeArrayFromByteSlice(slice []byte) *SafeArray { - array, _ := safeArrayCreateVector(VT_UI1, 0, uint32(len(slice))) - - if array == nil { - panic("Could not convert []byte to SAFEARRAY") - } - - for i, v := range slice { - safeArrayPutElement(array, int64(i), uintptr(unsafe.Pointer(&v))) - } - return array -} - -func safeArrayFromStringSlice(slice []string) *SafeArray { - array, _ := safeArrayCreateVector(VT_BSTR, 0, uint32(len(slice))) - - if array == nil { - panic("Could not convert []string to SAFEARRAY") - } - // SysAllocStringLen(s) - for i, v := range slice { - safeArrayPutElement(array, int64(i), uintptr(unsafe.Pointer(SysAllocStringLen(v)))) - } - return array -} diff --git a/vendor/github.com/go-ole/go-ole/utility.go b/vendor/github.com/go-ole/go-ole/utility.go deleted file mode 100644 index 99ee82d..0000000 --- a/vendor/github.com/go-ole/go-ole/utility.go +++ /dev/null @@ -1,101 +0,0 @@ -package ole - -import ( - "unicode/utf16" - "unsafe" -) - -// ClassIDFrom retrieves class ID whether given is program ID or application string. -// -// Helper that provides check against both Class ID from Program ID and Class ID from string. It is -// faster, if you know which you are using, to use the individual functions, but this will check -// against available functions for you. -func ClassIDFrom(programID string) (classID *GUID, err error) { - classID, err = CLSIDFromProgID(programID) - if err != nil { - classID, err = CLSIDFromString(programID) - if err != nil { - return - } - } - return -} - -// BytePtrToString converts byte pointer to a Go string. -func BytePtrToString(p *byte) string { - a := (*[10000]uint8)(unsafe.Pointer(p)) - i := 0 - for a[i] != 0 { - i++ - } - return string(a[:i]) -} - -// UTF16PtrToString is alias for LpOleStrToString. -// -// Kept for compatibility reasons. -func UTF16PtrToString(p *uint16) string { - return LpOleStrToString(p) -} - -// LpOleStrToString converts COM Unicode to Go string. -func LpOleStrToString(p *uint16) string { - if p == nil { - return "" - } - - length := lpOleStrLen(p) - a := make([]uint16, length) - - ptr := unsafe.Pointer(p) - - for i := 0; i < int(length); i++ { - a[i] = *(*uint16)(ptr) - ptr = unsafe.Pointer(uintptr(ptr) + 2) - } - - return string(utf16.Decode(a)) -} - -// BstrToString converts COM binary string to Go string. -func BstrToString(p *uint16) string { - if p == nil { - return "" - } - length := SysStringLen((*int16)(unsafe.Pointer(p))) - a := make([]uint16, length) - - ptr := unsafe.Pointer(p) - - for i := 0; i < int(length); i++ { - a[i] = *(*uint16)(ptr) - ptr = unsafe.Pointer(uintptr(ptr) + 2) - } - return string(utf16.Decode(a)) -} - -// lpOleStrLen returns the length of Unicode string. -func lpOleStrLen(p *uint16) (length int64) { - if p == nil { - return 0 - } - - ptr := unsafe.Pointer(p) - - for i := 0; ; i++ { - if 0 == *(*uint16)(ptr) { - length = int64(i) - break - } - ptr = unsafe.Pointer(uintptr(ptr) + 2) - } - return -} - -// convertHresultToError converts syscall to error, if call is unsuccessful. -func convertHresultToError(hr uintptr, r2 uintptr, ignore error) (err error) { - if hr != 0 { - err = NewError(hr) - } - return -} diff --git a/vendor/github.com/go-ole/go-ole/variables.go b/vendor/github.com/go-ole/go-ole/variables.go deleted file mode 100644 index a6add1b..0000000 --- a/vendor/github.com/go-ole/go-ole/variables.go +++ /dev/null @@ -1,15 +0,0 @@ -// +build windows - -package ole - -import ( - "golang.org/x/sys/windows" -) - -var ( - modcombase = windows.NewLazySystemDLL("combase.dll") - modkernel32 = windows.NewLazySystemDLL("kernel32.dll") - modole32 = windows.NewLazySystemDLL("ole32.dll") - modoleaut32 = windows.NewLazySystemDLL("oleaut32.dll") - moduser32 = windows.NewLazySystemDLL("user32.dll") -) diff --git a/vendor/github.com/go-ole/go-ole/variant.go b/vendor/github.com/go-ole/go-ole/variant.go deleted file mode 100644 index a2c8402..0000000 --- a/vendor/github.com/go-ole/go-ole/variant.go +++ /dev/null @@ -1,105 +0,0 @@ -package ole - -import "unsafe" - -// NewVariant returns new variant based on type and value. -func NewVariant(vt VT, val int64) VARIANT { - return VARIANT{VT: vt, Val: val} -} - -// ToIUnknown converts Variant to Unknown object. -func (v *VARIANT) ToIUnknown() *IUnknown { - if v.VT != VT_UNKNOWN { - return nil - } - return (*IUnknown)(unsafe.Pointer(uintptr(v.Val))) -} - -// ToIDispatch converts variant to dispatch object. -func (v *VARIANT) ToIDispatch() *IDispatch { - if v.VT != VT_DISPATCH { - return nil - } - return (*IDispatch)(unsafe.Pointer(uintptr(v.Val))) -} - -// ToArray converts variant to SafeArray helper. -func (v *VARIANT) ToArray() *SafeArrayConversion { - if v.VT != VT_SAFEARRAY { - if v.VT&VT_ARRAY == 0 { - return nil - } - } - var safeArray *SafeArray = (*SafeArray)(unsafe.Pointer(uintptr(v.Val))) - return &SafeArrayConversion{safeArray} -} - -// ToString converts variant to Go string. -func (v *VARIANT) ToString() string { - if v.VT != VT_BSTR { - return "" - } - return BstrToString(*(**uint16)(unsafe.Pointer(&v.Val))) -} - -// Clear the memory of variant object. -func (v *VARIANT) Clear() error { - return VariantClear(v) -} - -// Value returns variant value based on its type. -// -// Currently supported types: 2- and 4-byte integers, strings, bools. -// Note that 64-bit integers, datetimes, and other types are stored as strings -// and will be returned as strings. -// -// Needs to be further converted, because this returns an interface{}. -func (v *VARIANT) Value() interface{} { - switch v.VT { - case VT_I1: - return int8(v.Val) - case VT_UI1: - return uint8(v.Val) - case VT_I2: - return int16(v.Val) - case VT_UI2: - return uint16(v.Val) - case VT_I4: - return int32(v.Val) - case VT_UI4: - return uint32(v.Val) - case VT_I8: - return int64(v.Val) - case VT_UI8: - return uint64(v.Val) - case VT_INT: - return int(v.Val) - case VT_UINT: - return uint(v.Val) - case VT_INT_PTR: - return uintptr(v.Val) // TODO - case VT_UINT_PTR: - return uintptr(v.Val) - case VT_R4: - return *(*float32)(unsafe.Pointer(&v.Val)) - case VT_R8: - return *(*float64)(unsafe.Pointer(&v.Val)) - case VT_BSTR: - return v.ToString() - case VT_DATE: - // VT_DATE type will either return float64 or time.Time. - d := uint64(v.Val) - date, err := GetVariantDate(d) - if err != nil { - return float64(v.Val) - } - return date - case VT_UNKNOWN: - return v.ToIUnknown() - case VT_DISPATCH: - return v.ToIDispatch() - case VT_BOOL: - return (v.Val & 0xffff) != 0 - } - return nil -} diff --git a/vendor/github.com/go-ole/go-ole/variant_386.go b/vendor/github.com/go-ole/go-ole/variant_386.go deleted file mode 100644 index e73736b..0000000 --- a/vendor/github.com/go-ole/go-ole/variant_386.go +++ /dev/null @@ -1,11 +0,0 @@ -// +build 386 - -package ole - -type VARIANT struct { - VT VT // 2 - wReserved1 uint16 // 4 - wReserved2 uint16 // 6 - wReserved3 uint16 // 8 - Val int64 // 16 -} diff --git a/vendor/github.com/go-ole/go-ole/variant_amd64.go b/vendor/github.com/go-ole/go-ole/variant_amd64.go deleted file mode 100644 index dccdde1..0000000 --- a/vendor/github.com/go-ole/go-ole/variant_amd64.go +++ /dev/null @@ -1,12 +0,0 @@ -// +build amd64 - -package ole - -type VARIANT struct { - VT VT // 2 - wReserved1 uint16 // 4 - wReserved2 uint16 // 6 - wReserved3 uint16 // 8 - Val int64 // 16 - _ [8]byte // 24 -} diff --git a/vendor/github.com/go-ole/go-ole/variant_arm.go b/vendor/github.com/go-ole/go-ole/variant_arm.go deleted file mode 100644 index d472454..0000000 --- a/vendor/github.com/go-ole/go-ole/variant_arm.go +++ /dev/null @@ -1,11 +0,0 @@ -// +build arm - -package ole - -type VARIANT struct { - VT VT // 2 - wReserved1 uint16 // 4 - wReserved2 uint16 // 6 - wReserved3 uint16 // 8 - Val int64 // 16 -} diff --git a/vendor/github.com/go-ole/go-ole/variant_arm64.go b/vendor/github.com/go-ole/go-ole/variant_arm64.go deleted file mode 100644 index 78473ce..0000000 --- a/vendor/github.com/go-ole/go-ole/variant_arm64.go +++ /dev/null @@ -1,13 +0,0 @@ -//go:build arm64 -// +build arm64 - -package ole - -type VARIANT struct { - VT VT // 2 - wReserved1 uint16 // 4 - wReserved2 uint16 // 6 - wReserved3 uint16 // 8 - Val int64 // 16 - _ [8]byte // 24 -} diff --git a/vendor/github.com/go-ole/go-ole/variant_date_386.go b/vendor/github.com/go-ole/go-ole/variant_date_386.go deleted file mode 100644 index 1b970f6..0000000 --- a/vendor/github.com/go-ole/go-ole/variant_date_386.go +++ /dev/null @@ -1,22 +0,0 @@ -// +build windows,386 - -package ole - -import ( - "errors" - "syscall" - "time" - "unsafe" -) - -// GetVariantDate converts COM Variant Time value to Go time.Time. -func GetVariantDate(value uint64) (time.Time, error) { - var st syscall.Systemtime - v1 := uint32(value) - v2 := uint32(value >> 32) - r, _, _ := procVariantTimeToSystemTime.Call(uintptr(v1), uintptr(v2), uintptr(unsafe.Pointer(&st))) - if r != 0 { - return time.Date(int(st.Year), time.Month(st.Month), int(st.Day), int(st.Hour), int(st.Minute), int(st.Second), int(st.Milliseconds/1000), time.UTC), nil - } - return time.Now(), errors.New("Could not convert to time, passing current time.") -} diff --git a/vendor/github.com/go-ole/go-ole/variant_date_amd64.go b/vendor/github.com/go-ole/go-ole/variant_date_amd64.go deleted file mode 100644 index 6952f1f..0000000 --- a/vendor/github.com/go-ole/go-ole/variant_date_amd64.go +++ /dev/null @@ -1,20 +0,0 @@ -// +build windows,amd64 - -package ole - -import ( - "errors" - "syscall" - "time" - "unsafe" -) - -// GetVariantDate converts COM Variant Time value to Go time.Time. -func GetVariantDate(value uint64) (time.Time, error) { - var st syscall.Systemtime - r, _, _ := procVariantTimeToSystemTime.Call(uintptr(value), uintptr(unsafe.Pointer(&st))) - if r != 0 { - return time.Date(int(st.Year), time.Month(st.Month), int(st.Day), int(st.Hour), int(st.Minute), int(st.Second), int(st.Milliseconds/1000), time.UTC), nil - } - return time.Now(), errors.New("Could not convert to time, passing current time.") -} diff --git a/vendor/github.com/go-ole/go-ole/variant_date_arm.go b/vendor/github.com/go-ole/go-ole/variant_date_arm.go deleted file mode 100644 index 09ec7b5..0000000 --- a/vendor/github.com/go-ole/go-ole/variant_date_arm.go +++ /dev/null @@ -1,22 +0,0 @@ -// +build windows,arm - -package ole - -import ( - "errors" - "syscall" - "time" - "unsafe" -) - -// GetVariantDate converts COM Variant Time value to Go time.Time. -func GetVariantDate(value uint64) (time.Time, error) { - var st syscall.Systemtime - v1 := uint32(value) - v2 := uint32(value >> 32) - r, _, _ := procVariantTimeToSystemTime.Call(uintptr(v1), uintptr(v2), uintptr(unsafe.Pointer(&st))) - if r != 0 { - return time.Date(int(st.Year), time.Month(st.Month), int(st.Day), int(st.Hour), int(st.Minute), int(st.Second), int(st.Milliseconds/1000), time.UTC), nil - } - return time.Now(), errors.New("Could not convert to time, passing current time.") -} diff --git a/vendor/github.com/go-ole/go-ole/variant_date_arm64.go b/vendor/github.com/go-ole/go-ole/variant_date_arm64.go deleted file mode 100644 index 02b04a0..0000000 --- a/vendor/github.com/go-ole/go-ole/variant_date_arm64.go +++ /dev/null @@ -1,23 +0,0 @@ -//go:build windows && arm64 -// +build windows,arm64 - -package ole - -import ( - "errors" - "syscall" - "time" - "unsafe" -) - -// GetVariantDate converts COM Variant Time value to Go time.Time. -func GetVariantDate(value uint64) (time.Time, error) { - var st syscall.Systemtime - v1 := uint32(value) - v2 := uint32(value >> 32) - r, _, _ := procVariantTimeToSystemTime.Call(uintptr(v1), uintptr(v2), uintptr(unsafe.Pointer(&st))) - if r != 0 { - return time.Date(int(st.Year), time.Month(st.Month), int(st.Day), int(st.Hour), int(st.Minute), int(st.Second), int(st.Milliseconds/1000), time.UTC), nil - } - return time.Now(), errors.New("Could not convert to time, passing current time.") -} diff --git a/vendor/github.com/go-ole/go-ole/variant_ppc64le.go b/vendor/github.com/go-ole/go-ole/variant_ppc64le.go deleted file mode 100644 index 326427a..0000000 --- a/vendor/github.com/go-ole/go-ole/variant_ppc64le.go +++ /dev/null @@ -1,12 +0,0 @@ -// +build ppc64le - -package ole - -type VARIANT struct { - VT VT // 2 - wReserved1 uint16 // 4 - wReserved2 uint16 // 6 - wReserved3 uint16 // 8 - Val int64 // 16 - _ [8]byte // 24 -} diff --git a/vendor/github.com/go-ole/go-ole/variant_s390x.go b/vendor/github.com/go-ole/go-ole/variant_s390x.go deleted file mode 100644 index 9874ca6..0000000 --- a/vendor/github.com/go-ole/go-ole/variant_s390x.go +++ /dev/null @@ -1,12 +0,0 @@ -// +build s390x - -package ole - -type VARIANT struct { - VT VT // 2 - wReserved1 uint16 // 4 - wReserved2 uint16 // 6 - wReserved3 uint16 // 8 - Val int64 // 16 - _ [8]byte // 24 -} diff --git a/vendor/github.com/go-ole/go-ole/vt_string.go b/vendor/github.com/go-ole/go-ole/vt_string.go deleted file mode 100644 index 729b4a0..0000000 --- a/vendor/github.com/go-ole/go-ole/vt_string.go +++ /dev/null @@ -1,58 +0,0 @@ -// generated by stringer -output vt_string.go -type VT; DO NOT EDIT - -package ole - -import "fmt" - -const ( - _VT_name_0 = "VT_EMPTYVT_NULLVT_I2VT_I4VT_R4VT_R8VT_CYVT_DATEVT_BSTRVT_DISPATCHVT_ERRORVT_BOOLVT_VARIANTVT_UNKNOWNVT_DECIMAL" - _VT_name_1 = "VT_I1VT_UI1VT_UI2VT_UI4VT_I8VT_UI8VT_INTVT_UINTVT_VOIDVT_HRESULTVT_PTRVT_SAFEARRAYVT_CARRAYVT_USERDEFINEDVT_LPSTRVT_LPWSTR" - _VT_name_2 = "VT_RECORDVT_INT_PTRVT_UINT_PTR" - _VT_name_3 = "VT_FILETIMEVT_BLOBVT_STREAMVT_STORAGEVT_STREAMED_OBJECTVT_STORED_OBJECTVT_BLOB_OBJECTVT_CFVT_CLSID" - _VT_name_4 = "VT_BSTR_BLOBVT_VECTOR" - _VT_name_5 = "VT_ARRAY" - _VT_name_6 = "VT_BYREF" - _VT_name_7 = "VT_RESERVED" - _VT_name_8 = "VT_ILLEGAL" -) - -var ( - _VT_index_0 = [...]uint8{0, 8, 15, 20, 25, 30, 35, 40, 47, 54, 65, 73, 80, 90, 100, 110} - _VT_index_1 = [...]uint8{0, 5, 11, 17, 23, 28, 34, 40, 47, 54, 64, 70, 82, 91, 105, 113, 122} - _VT_index_2 = [...]uint8{0, 9, 19, 30} - _VT_index_3 = [...]uint8{0, 11, 18, 27, 37, 55, 71, 85, 90, 98} - _VT_index_4 = [...]uint8{0, 12, 21} - _VT_index_5 = [...]uint8{0, 8} - _VT_index_6 = [...]uint8{0, 8} - _VT_index_7 = [...]uint8{0, 11} - _VT_index_8 = [...]uint8{0, 10} -) - -func (i VT) String() string { - switch { - case 0 <= i && i <= 14: - return _VT_name_0[_VT_index_0[i]:_VT_index_0[i+1]] - case 16 <= i && i <= 31: - i -= 16 - return _VT_name_1[_VT_index_1[i]:_VT_index_1[i+1]] - case 36 <= i && i <= 38: - i -= 36 - return _VT_name_2[_VT_index_2[i]:_VT_index_2[i+1]] - case 64 <= i && i <= 72: - i -= 64 - return _VT_name_3[_VT_index_3[i]:_VT_index_3[i+1]] - case 4095 <= i && i <= 4096: - i -= 4095 - return _VT_name_4[_VT_index_4[i]:_VT_index_4[i+1]] - case i == 8192: - return _VT_name_5 - case i == 16384: - return _VT_name_6 - case i == 32768: - return _VT_name_7 - case i == 65535: - return _VT_name_8 - default: - return fmt.Sprintf("VT(%d)", i) - } -} diff --git a/vendor/github.com/go-ole/go-ole/winrt.go b/vendor/github.com/go-ole/go-ole/winrt.go deleted file mode 100644 index 4e9eca7..0000000 --- a/vendor/github.com/go-ole/go-ole/winrt.go +++ /dev/null @@ -1,99 +0,0 @@ -// +build windows - -package ole - -import ( - "reflect" - "syscall" - "unicode/utf8" - "unsafe" -) - -var ( - procRoInitialize = modcombase.NewProc("RoInitialize") - procRoActivateInstance = modcombase.NewProc("RoActivateInstance") - procRoGetActivationFactory = modcombase.NewProc("RoGetActivationFactory") - procWindowsCreateString = modcombase.NewProc("WindowsCreateString") - procWindowsDeleteString = modcombase.NewProc("WindowsDeleteString") - procWindowsGetStringRawBuffer = modcombase.NewProc("WindowsGetStringRawBuffer") -) - -func RoInitialize(thread_type uint32) (err error) { - hr, _, _ := procRoInitialize.Call(uintptr(thread_type)) - if hr != 0 { - err = NewError(hr) - } - return -} - -func RoActivateInstance(clsid string) (ins *IInspectable, err error) { - hClsid, err := NewHString(clsid) - if err != nil { - return nil, err - } - defer DeleteHString(hClsid) - - hr, _, _ := procRoActivateInstance.Call( - uintptr(unsafe.Pointer(hClsid)), - uintptr(unsafe.Pointer(&ins))) - if hr != 0 { - err = NewError(hr) - } - return -} - -func RoGetActivationFactory(clsid string, iid *GUID) (ins *IInspectable, err error) { - hClsid, err := NewHString(clsid) - if err != nil { - return nil, err - } - defer DeleteHString(hClsid) - - hr, _, _ := procRoGetActivationFactory.Call( - uintptr(unsafe.Pointer(hClsid)), - uintptr(unsafe.Pointer(iid)), - uintptr(unsafe.Pointer(&ins))) - if hr != 0 { - err = NewError(hr) - } - return -} - -// HString is handle string for pointers. -type HString uintptr - -// NewHString returns a new HString for Go string. -func NewHString(s string) (hstring HString, err error) { - u16 := syscall.StringToUTF16Ptr(s) - len := uint32(utf8.RuneCountInString(s)) - hr, _, _ := procWindowsCreateString.Call( - uintptr(unsafe.Pointer(u16)), - uintptr(len), - uintptr(unsafe.Pointer(&hstring))) - if hr != 0 { - err = NewError(hr) - } - return -} - -// DeleteHString deletes HString. -func DeleteHString(hstring HString) (err error) { - hr, _, _ := procWindowsDeleteString.Call(uintptr(hstring)) - if hr != 0 { - err = NewError(hr) - } - return -} - -// String returns Go string value of HString. -func (h HString) String() string { - var u16buf uintptr - var u16len uint32 - u16buf, _, _ = procWindowsGetStringRawBuffer.Call( - uintptr(h), - uintptr(unsafe.Pointer(&u16len))) - - u16hdr := reflect.SliceHeader{Data: u16buf, Len: int(u16len), Cap: int(u16len)} - u16 := *(*[]uint16)(unsafe.Pointer(&u16hdr)) - return syscall.UTF16ToString(u16) -} diff --git a/vendor/github.com/go-ole/go-ole/winrt_doc.go b/vendor/github.com/go-ole/go-ole/winrt_doc.go deleted file mode 100644 index 52e6d74..0000000 --- a/vendor/github.com/go-ole/go-ole/winrt_doc.go +++ /dev/null @@ -1,36 +0,0 @@ -// +build !windows - -package ole - -// RoInitialize -func RoInitialize(thread_type uint32) (err error) { - return NewError(E_NOTIMPL) -} - -// RoActivateInstance -func RoActivateInstance(clsid string) (ins *IInspectable, err error) { - return nil, NewError(E_NOTIMPL) -} - -// RoGetActivationFactory -func RoGetActivationFactory(clsid string, iid *GUID) (ins *IInspectable, err error) { - return nil, NewError(E_NOTIMPL) -} - -// HString is handle string for pointers. -type HString uintptr - -// NewHString returns a new HString for Go string. -func NewHString(s string) (hstring HString, err error) { - return HString(uintptr(0)), NewError(E_NOTIMPL) -} - -// DeleteHString deletes HString. -func DeleteHString(hstring HString) (err error) { - return NewError(E_NOTIMPL) -} - -// String returns Go string value of HString. -func (h HString) String() string { - return "" -} diff --git a/vendor/modules.txt b/vendor/modules.txt index 66693df..9f0d83e 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -7,7 +7,6 @@ github.com/go-logr/logr github.com/go-logr/logr/funcr # github.com/go-ole/go-ole v1.3.0 ## explicit; go 1.12 -github.com/go-ole/go-ole # github.com/go-task/slim-sprig/v3 v3.0.0 ## explicit; go 1.20 github.com/go-task/slim-sprig/v3 From 1e47c9d502dfab6f5c8b991566f1951e3acb5969 Mon Sep 17 00:00:00 2001 From: Yevhen Vydolob Date: Tue, 9 Dec 2025 16:36:44 +0200 Subject: [PATCH 2/4] add headers to fix image download in test Signed-off-by: Yevhen Vydolob --- test/e2e/libhvee_test.go | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/test/e2e/libhvee_test.go b/test/e2e/libhvee_test.go index c40dbde..7c1c699 100644 --- a/test/e2e/libhvee_test.go +++ b/test/e2e/libhvee_test.go @@ -27,10 +27,18 @@ func TestLibhvee(t *testing.T) { } func get(endpoint string) ([]byte, error) { - resp, err := http.Get(endpoint) + + getReq, err := http.NewRequest("GET", endpoint, nil) + if err != nil { + return nil, err + } + addHeaders(getReq) + + resp, err := http.DefaultClient.Do(getReq) if err != nil { return nil, err } + if resp.StatusCode != http.StatusOK { Fail(fmt.Sprintf("get %s: status code: %d", endpoint, resp.StatusCode)) } @@ -41,12 +49,30 @@ func get(endpoint string) ([]byte, error) { return body, err } +// these headers are required to bypass anubis bot protection +// maybe subject to change if the bot protection is updated +func addHeaders(getReq *http.Request) { + getReq.Header.Set("Accept", "application/json") + getReq.Header.Set("Accept-Encoding", "gzip, deflate, br") + getReq.Header.Set("Accept-Language", "en-US,en;q=0.9") + getReq.Header.Set("Connection", "keep-alive") + getReq.Header.Set("Referer", "https://kojipkgs.fedoraproject.org/compose/cloud/latest-Fedora-Cloud-41/compose") + getReq.Header.Set("Sec-Ch-Ua-Mobile", "?0") + getReq.Header.Set("Sec-Ch-Ua-Platform", "macOS") + getReq.Header.Set("Sec-Fetch-Dest", "document") + getReq.Header.Set("Sec-Fetch-Mode", "navigate") + getReq.Header.Set("Sec-Fetch-Site", "same-origin") + getReq.Header.Set("Sec-User", "?1") + getReq.Header.Set("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/142.0.0.0 Safari/537.36") +} + func pullWithProgress(endpoint string, dst *os.File) error { fmt.Println("trying to pull: ", endpoint) req, err := http.NewRequest("GET", endpoint, nil) if err != nil { Fail(err.Error()) } + addHeaders(req) resp, err := http.DefaultClient.Do(req) if err != nil { Fail(err.Error()) From a5da454717fb1e47981124bc8bd9d167b18b0a08 Mon Sep 17 00:00:00 2001 From: Yevhen Vydolob Date: Wed, 22 Oct 2025 11:02:27 +0300 Subject: [PATCH 3/4] Use PowerShell command to interact with Hyper-V Replace wmi calls with powershell commands. This should improve error messages and remove need of executing this code with escalating privileges. The workflow should be the same, the most changes is in 'MemorySettings' memory field names and types, it should be set in bytes. And 'ProcessorSettings' has other name for processors count field. Signed-off-by: Yevhen Vydolob --- cmd/createvm/main.go | 32 +- cmd/dumpvms/main.go | 12 +- cmd/kvpctl/main.go | 9 + cmd/managevm/main.go | 72 +++ cmd/updatevm/main.go | 20 +- pkg/hypervctl/diskdrive_settings.go | 168 ++++-- pkg/hypervctl/drive_settings_builder.go | 11 +- pkg/hypervctl/dvddrive_settings.go | 117 +++- pkg/hypervctl/error.go | 171 +----- pkg/hypervctl/ethernet_alloc_settings.go | 50 -- pkg/hypervctl/ethernet_port_settings.go | 109 ---- pkg/hypervctl/helper.go | 44 ++ pkg/hypervctl/kvp.go | 85 --- pkg/hypervctl/memory_settings.go | 209 +++++-- pkg/hypervctl/network_adapter_settings.go | 270 +++++++++ pkg/hypervctl/network_settings_builder.go | 21 +- pkg/hypervctl/processor_settings.go | 290 +++++++--- pkg/hypervctl/resources_settings.go | 135 ----- pkg/hypervctl/scsi_controller.go | 301 ++++++++-- pkg/hypervctl/storage_alloc_settings.go | 61 -- pkg/hypervctl/summary.go | 591 +++++++++++++------- pkg/hypervctl/system_settings.go | 505 ++++++++++++----- pkg/hypervctl/system_settings_builder.go | 83 +-- pkg/hypervctl/vdvd_storage_settings.go | 13 - pkg/hypervctl/vhd.go | 97 +--- pkg/hypervctl/vhd_settings.go | 28 - pkg/hypervctl/vhd_storage_settings.go | 13 - pkg/hypervctl/vm.go | 476 ++++------------ pkg/hypervctl/vm_config.go | 16 - pkg/hypervctl/vmm.go | 204 ++----- pkg/kvp/ginsu/util.go | 2 + pkg/powershell/powershell.go | 134 +++++ pkg/powershell/ps.go | 52 ++ pkg/wmiext/array.go | 205 ------- pkg/wmiext/conversion.go | 474 ---------------- pkg/wmiext/enum.go | 94 ---- pkg/wmiext/error.go | 202 ------- pkg/wmiext/init.go | 108 ---- pkg/wmiext/instance.go | 652 ---------------------- pkg/wmiext/invoke.go | 132 ----- pkg/wmiext/job.go | 53 -- pkg/wmiext/service.go | 416 -------------- test/e2e/basic_test.go | 6 +- winmake.ps1 | 1 + 44 files changed, 2476 insertions(+), 4268 deletions(-) create mode 100644 cmd/managevm/main.go delete mode 100644 pkg/hypervctl/ethernet_alloc_settings.go delete mode 100644 pkg/hypervctl/ethernet_port_settings.go create mode 100644 pkg/hypervctl/helper.go create mode 100644 pkg/hypervctl/network_adapter_settings.go delete mode 100644 pkg/hypervctl/resources_settings.go delete mode 100644 pkg/hypervctl/storage_alloc_settings.go delete mode 100644 pkg/hypervctl/vdvd_storage_settings.go delete mode 100644 pkg/hypervctl/vhd_settings.go delete mode 100644 pkg/hypervctl/vhd_storage_settings.go create mode 100644 pkg/powershell/powershell.go create mode 100644 pkg/powershell/ps.go delete mode 100644 pkg/wmiext/array.go delete mode 100644 pkg/wmiext/conversion.go delete mode 100644 pkg/wmiext/enum.go delete mode 100644 pkg/wmiext/error.go delete mode 100644 pkg/wmiext/init.go delete mode 100644 pkg/wmiext/instance.go delete mode 100644 pkg/wmiext/invoke.go delete mode 100644 pkg/wmiext/job.go delete mode 100644 pkg/wmiext/service.go diff --git a/cmd/createvm/main.go b/cmd/createvm/main.go index 74a1740..88cf2c1 100644 --- a/cmd/createvm/main.go +++ b/cmd/createvm/main.go @@ -1,5 +1,4 @@ //go:build windows -// +build windows package main @@ -9,6 +8,7 @@ import ( "path/filepath" "github.com/containers/libhvee/pkg/hypervctl" + "github.com/containers/libhvee/pkg/powershell" ) func main() { @@ -20,6 +20,14 @@ func main() { return } + if err := powershell.HypervAvailable(); err != nil { + panic(err) + } + + if !powershell.IsHypervAdministrator() { + panic(powershell.ErrNotAdministrator) + } + vmName := os.Args[1] vhdxFile := abs(os.Args[2]) isoFile := abs(os.Args[3]) @@ -37,21 +45,28 @@ func main() { } } + if exists, err := vmm.Exists(vmName); err != nil { + panic(err) + } else if exists { + panic(fmt.Errorf("machine %s already exists", vmName)) + } + // System systemSettings, err := hypervctl.NewSystemSettingsBuilder(). PrepareSystemSettings(vmName, nil). PrepareMemorySettings(func(ms *hypervctl.MemorySettings) { ms.DynamicMemoryEnabled = true - ms.VirtualQuantity = 8192 // Startup memory - ms.Reservation = 1024 // min - ms.Limit = 16384 // max + ms.StartupBytes = 8192 * 1024 * 1024 // 8GB + ms.MinimumBytes = 1024 * 1024 * 1024 // 1GB + ms.MaximumBytes = 16384 * 1024 * 1024 // 16GB }). PrepareProcessorSettings(func(ps *hypervctl.ProcessorSettings) { - ps.VirtualQuantity = 4 // 4 cores + ps.Count = 4 // 4 cores }). Build() if err != nil { + fmt.Fprintf(os.Stderr, "error building system settings: %s\n", err) panic(err) } @@ -60,7 +75,7 @@ func main() { err = hypervctl.NewDriveSettingsBuilder(systemSettings). AddScsiController(). AddSyntheticDiskDrive(0). - DefineVirtualHardDisk(vhdxFile, func(vhdss *hypervctl.VirtualHardDiskStorageSettings) { + DefineVirtualHardDisk(vhdxFile, func(vhdss *hypervctl.HardDiskDriveSettings) { // set extra params like // vhdss.IOPSLimit = 5000 }). @@ -94,11 +109,8 @@ func main() { panic(err) } - if err = vm.AddKeyValuePair("fun", "pair"); err != nil { - panic(err) - } - fmt.Println(vm.Path()) + fmt.Println(vm.Name) fmt.Println("Done!") } diff --git a/cmd/dumpvms/main.go b/cmd/dumpvms/main.go index 438047c..a99da92 100644 --- a/cmd/dumpvms/main.go +++ b/cmd/dumpvms/main.go @@ -1,5 +1,4 @@ //go:build windows -// +build windows package main @@ -9,6 +8,7 @@ import ( "os" "github.com/containers/libhvee/pkg/hypervctl" + "github.com/containers/libhvee/pkg/powershell" ) const ( @@ -31,7 +31,7 @@ func getVms() (string, error) { func dumpSummary() (string, error) { vmms := hypervctl.VirtualMachineManager{} - summs, err := vmms.GetSummaryInformation(hypervctl.SummaryRequestNearAll) + summs, err := vmms.GetSummaryInformation() if err != nil { return "", fmt.Errorf("Could not retrieve virtual machine summaries: %v\n", err) // nolint:staticcheck } @@ -51,6 +51,14 @@ func main() { err error result string ) + if err := powershell.HypervAvailable(); err != nil { + panic(err) + } + + if !powershell.IsHypervAdministrator() { + panic(powershell.ErrNotAdministrator) + } + args := os.Args if len(args) != 2 { printHelp() diff --git a/cmd/kvpctl/main.go b/cmd/kvpctl/main.go index 1d4ee10..87b605a 100644 --- a/cmd/kvpctl/main.go +++ b/cmd/kvpctl/main.go @@ -12,6 +12,7 @@ import ( "github.com/containers/libhvee/pkg/hypervctl" "github.com/containers/libhvee/pkg/kvp/ginsu" + "github.com/containers/libhvee/pkg/powershell" "github.com/sirupsen/logrus" "golang.org/x/sys/windows" ) @@ -68,6 +69,14 @@ func main() { printHelp() } + if err := powershell.HypervAvailable(); err != nil { + panic(err) + } + + if !powershell.IsHypervAdministrator() { + panic(powershell.ErrNotAdministrator) + } + subCmd := getSubCommand(os.Args[2]) if subCmd == unknown { fmt.Printf("error: unknown command %s\n", os.Args[2]) diff --git a/cmd/managevm/main.go b/cmd/managevm/main.go new file mode 100644 index 0000000..899006c --- /dev/null +++ b/cmd/managevm/main.go @@ -0,0 +1,72 @@ +//go:build windows + +package main + +import ( + "fmt" + "os" + + "github.com/containers/libhvee/pkg/hypervctl" + "github.com/containers/libhvee/pkg/powershell" +) + +func main() { + + if err := powershell.HypervAvailable(); err != nil { + panic(err) + } + + if !powershell.IsHypervAdministrator() { + panic(powershell.ErrNotAdministrator) + } + + if len(os.Args) < 2 { + fmt.Printf("Usage: %s \n\n", os.Args[0]) + return + } + + vmName := os.Args[1] + action := os.Args[2] + + vmm := hypervctl.VirtualMachineManager{} + + exists, _, err := vmm.GetMachineExists(vmName) + if err != nil { + panic(err) + } + if !exists { + panic(fmt.Errorf("VM %s does not exist", vmName)) + } + + vm, err := vmm.GetMachine(vmName) + if err != nil { + panic(err) + } + + switch action { + case "remove": + err = vm.Remove(vm.HardDrives[0].Path) + if err != nil { + fmt.Println(err) + panic(err) + } + case "start": + err = vm.Start() + case "stop": + err = vm.Stop() + case "restart": + err = vm.Stop() + if err != nil { + panic(err) + } + err = vm.Start() + if err != nil { + panic(err) + } + case "status": + fmt.Println(vm.GetState()) + } + if err != nil { + panic(err) + } +} diff --git a/cmd/updatevm/main.go b/cmd/updatevm/main.go index 95f6c96..d1d890f 100644 --- a/cmd/updatevm/main.go +++ b/cmd/updatevm/main.go @@ -1,5 +1,4 @@ //go:build windows -// +build windows package main @@ -9,12 +8,21 @@ import ( "strconv" "github.com/containers/libhvee/pkg/hypervctl" + "github.com/containers/libhvee/pkg/powershell" ) func main() { + if err := powershell.HypervAvailable(); err != nil { + panic(err) + } + + if !powershell.IsHypervAdministrator() { + panic(powershell.ErrNotAdministrator) + } + if len(os.Args) < 4 { - fmt.Printf("Usage: %s \n\n", os.Args[0]) + fmt.Printf("Usage: %s \n\n", os.Args[0]) return } @@ -37,12 +45,12 @@ func main() { } err = vm.UpdateProcessorMemSettings(func(ps *hypervctl.ProcessorSettings) { - ps.VirtualQuantity = cores + ps.Count = int64(cores) }, func(ms *hypervctl.MemorySettings) { ms.DynamicMemoryEnabled = false - ms.VirtualQuantity = mem - ms.Limit = mem - ms.Reservation = mem + ms.StartupBytes = mem * 1024 * 1024 + ms.MaximumBytes = mem * 1024 * 1024 + ms.MinimumBytes = mem * 1024 * 1024 }) if err != nil { panic(err) diff --git a/pkg/hypervctl/diskdrive_settings.go b/pkg/hypervctl/diskdrive_settings.go index 5e66930..166c873 100644 --- a/pkg/hypervctl/diskdrive_settings.go +++ b/pkg/hypervctl/diskdrive_settings.go @@ -1,72 +1,160 @@ //go:build windows -// +build windows package hypervctl import ( - "github.com/containers/libhvee/pkg/wmiext" + "fmt" + + "github.com/containers/libhvee/pkg/powershell" ) -const SyntheticDiskDriveType = "Microsoft:Hyper-V:Synthetic Disk Drive" +// HardDiskDriveSettings represents the arguments for the PowerShell cmdlet +// "Add-VMHardDiskDrive". This type can be used to add a new virtual hard disk +// to a Hyper-V virtual machine. +// +// Pointers are used for optional fields to differentiate between a zero value +// and a field that was not explicitly provided. +type HardDiskDriveSettings struct { + // VMName specifies the name of the virtual machine to which the hard disk + // is to be added. + VMName string `json:"VMName,omitempty"` + + // ControllerType specifies the type of controller (IDE or SCSI) for the hard disk. + ControllerType string `json:"ControllerType,omitempty"` + + // ControllerNumber specifies the number of the controller. + ControllerNumber int `json:"ControllerNumber,omitempty"` + + // ControllerLocation specifies the location number on the controller. + ControllerLocation int `json:"ControllerLocation,omitempty"` + + // Path specifies the full path of the hard disk drive file to be added. + Path string `json:"Path,omitempty"` + + // DiskNumber specifies the disk number of an offline physical hard drive + // to be connected as a passthrough disk. + DiskNumber int `json:"DiskNumber,omitempty"` + + // AllowUnverifiedPaths specifies that no error should be thrown if the path + // is not verified for clustered virtual machines. + AllowUnverifiedPaths bool `json:"AllowUnverifiedPaths,omitempty"` + + // Passthru specifies that the added HardDiskDrive object should be + // passed through to the pipeline. + Passthru bool `json:"Passthru,omitempty"` + + // MaximumIOPS specifies the maximum normalized I/O operations per second (IOPS). + MaximumIOPS uint64 `json:"MaximumIOPS,omitempty"` + + // MinimumIOPS specifies the minimum normalized I/O operations per second (IOPS). + MinimumIOPS uint64 `json:"MinimumIOPS,omitempty"` + + // QoSPolicy specifies the name of a storage Quality of Service (QoS) policy. + QoSPolicy string `json:"QoSPolicy,omitempty"` + + // QoSPolicyID specifies the unique ID for a storage QoS policy. + QoSPolicyID string `json:"QoSPolicyID,omitempty"` + + // ResourcePoolName specifies the friendly name of the ISO resource pool. + ResourcePoolName string `json:"ResourcePoolName,omitempty"` + + // SupportPersistentReservations indicates that the hard disk supports SCSI + // persistent reservation semantics for shared disks. + SupportPersistentReservations bool `json:"SupportPersistentReservations,omitempty"` + + // Other optional parameters for remote sessions or confirmation. + ComputerName string `json:"ComputerName,omitempty"` +} type SyntheticDiskDriveSettings struct { - ResourceSettings - systemSettings *SystemSettings + HardDiskDriveSettings controllerSettings *ScsiControllerSettings } -type diskAssociation interface { - setParent(parent string) - setHostResource(resource []string) - Path() string -} -func (d *SyntheticDiskDriveSettings) DefineVirtualHardDisk(vhdxFile string, beforeAdd func(*VirtualHardDiskStorageSettings)) (*VirtualHardDiskStorageSettings, error) { - vhd := &VirtualHardDiskStorageSettings{} +func (d *SyntheticDiskDriveSettings) DefineVirtualHardDisk(vhdxFile string, beforeAdd func(*HardDiskDriveSettings)) (*HardDiskDriveSettings, error) { + vhd := &HardDiskDriveSettings{} - var cb func() if beforeAdd != nil { - cb = func() { - beforeAdd(vhd) - } + beforeAdd(vhd) } + vhd.Path = vhdxFile + vhd.VMName = d.VMName + vhd.ControllerType = d.ControllerType + vhd.ControllerNumber = d.ControllerNumber + vhd.ControllerLocation = d.ControllerLocation + vhd.DiskNumber = d.DiskNumber + vhd.MaximumIOPS = d.MaximumIOPS + vhd.MinimumIOPS = d.MinimumIOPS - if err := createDiskResourceInternal(d.systemSettings.Path(), d.Path(), vhdxFile, vhd, VirtualHardDiskType, cb); err != nil { - return nil, err + cli := vhd.getCLI() + cli = append([]string{"Hyper-V\\Add-VMHardDiskDrive"}, cli...) + _, stderr, err := powershell.Execute(cli...) + if err != nil { + return nil, NewPSError(stderr) } - vhd.driveSettings = d - vhd.systemSettings = d.systemSettings return vhd, nil } -func createDiskResourceInternal(systemPath string, drivePath string, file string, settings diskAssociation, resourceType string, cb func()) error { - var service *wmiext.Service - var err error - if service, err = NewLocalHyperVService(); err != nil { - return err +// GetCLI generates PowerShell Add-VMHardDiskDrive command parameters from HardDiskDriveSettings +func (h *HardDiskDriveSettings) getCLI() []string { + if h.VMName == "" { + return []string{} } - defer service.Close() - if err = populateDefaults(resourceType, settings); err != nil { - return err - } + params := []string{} - settings.setHostResource([]string{file}) - settings.setParent(drivePath) - if cb != nil { - cb() + // VM Name is required + params = append(params, "-VMName", fmt.Sprintf("'%s'", h.VMName)) + + // String parameters + if h.Path != "" { + params = append(params, "-Path", fmt.Sprintf("'%s'", h.Path)) + } + if h.ControllerType != "" { + params = append(params, "-ControllerType", h.ControllerType) + } + if h.QoSPolicy != "" { + params = append(params, "-QoSPolicy", fmt.Sprintf("'%s'", h.QoSPolicy)) + } + if h.QoSPolicyID != "" { + params = append(params, "-QoSPolicyID", fmt.Sprintf("'%s'", h.QoSPolicyID)) + } + if h.ResourcePoolName != "" { + params = append(params, "-ResourcePoolName", fmt.Sprintf("'%s'", h.ResourcePoolName)) + } + if h.ComputerName != "" { + params = append(params, "-ComputerName", fmt.Sprintf("'%s'", h.ComputerName)) } - diskResource, err := createResourceSettingGeneric(settings, resourceType) - if err != nil { - return err + // Numeric parameters (note: some parameters allow 0 as valid value in PowerShell) + if h.ControllerNumber >= 0 { + params = append(params, "-ControllerNumber", fmt.Sprintf("%d", h.ControllerNumber)) + } + if h.ControllerLocation >= 0 { + params = append(params, "-ControllerLocation", fmt.Sprintf("%d", h.ControllerLocation)) + } + if h.DiskNumber > 0 { + params = append(params, "-DiskNumber", fmt.Sprintf("%d", h.DiskNumber)) + } + if h.MaximumIOPS > 0 { + params = append(params, "-MaximumIOPS", fmt.Sprintf("%d", h.MaximumIOPS)) + } + if h.MinimumIOPS > 0 { + params = append(params, "-MinimumIOPS", fmt.Sprintf("%d", h.MinimumIOPS)) } - path, err := addResource(service, systemPath, diskResource) - if err != nil { - return err + // Boolean parameters (switch parameters only need to be present for true values) + if h.AllowUnverifiedPaths { + params = append(params, "-AllowUnverifiedPaths") + } + if h.Passthru { + params = append(params, "-Passthru") + } + if h.SupportPersistentReservations { + params = append(params, "-SupportPersistentReservations") } - return service.GetObjectAsObject(path, settings) + return params } diff --git a/pkg/hypervctl/drive_settings_builder.go b/pkg/hypervctl/drive_settings_builder.go index 21d6747..15c4022 100644 --- a/pkg/hypervctl/drive_settings_builder.go +++ b/pkg/hypervctl/drive_settings_builder.go @@ -1,5 +1,4 @@ //go:build windows -// +build windows package hypervctl @@ -28,13 +27,13 @@ type SyntheticDvdDriveSettingsBuilder struct { type VirtualHardDiskStorageSettingsBuilder struct { driveBuilder *SyntheticDiskDriveSettingsBuilder - diskSettings *VirtualHardDiskStorageSettings + diskSettings *HardDiskDriveSettings err error } type VirtualDvdDiskStorageSettingsBuilder struct { driveBuilder *SyntheticDvdDriveSettingsBuilder - diskSettings *VirtualDvdDiskStorageSettings + diskSettings *DvdDriveSettings err error } @@ -87,7 +86,7 @@ func (builder *ControllerSettingsBuilder) AddSyntheticDvdDrive(slot uint) *Synth } } -func (builder *SyntheticDiskDriveSettingsBuilder) DefineVirtualHardDisk(vhdxFile string, beforeAdd func(*VirtualHardDiskStorageSettings)) *VirtualHardDiskStorageSettingsBuilder { +func (builder *SyntheticDiskDriveSettingsBuilder) DefineVirtualHardDisk(vhdxFile string, beforeAdd func(*HardDiskDriveSettings)) *VirtualHardDiskStorageSettingsBuilder { if builder.err != nil { return &VirtualHardDiskStorageSettingsBuilder{driveBuilder: builder, err: builder.err} } @@ -156,12 +155,12 @@ func (builder *SyntheticDvdDriveSettingsBuilder) Finish() *ControllerSettingsBui return builder.controllerBuilder } -func (builder *VirtualHardDiskStorageSettingsBuilder) Get(s **VirtualHardDiskStorageSettings) *VirtualHardDiskStorageSettingsBuilder { +func (builder *VirtualHardDiskStorageSettingsBuilder) Get(s **HardDiskDriveSettings) *VirtualHardDiskStorageSettingsBuilder { *s = builder.diskSettings return builder } -func (builder *VirtualDvdDiskStorageSettingsBuilder) Get(s **VirtualDvdDiskStorageSettings) *VirtualDvdDiskStorageSettingsBuilder { +func (builder *VirtualDvdDiskStorageSettingsBuilder) Get(s **DvdDriveSettings) *VirtualDvdDiskStorageSettingsBuilder { *s = builder.diskSettings return builder } diff --git a/pkg/hypervctl/dvddrive_settings.go b/pkg/hypervctl/dvddrive_settings.go index 2f66163..45ae52b 100644 --- a/pkg/hypervctl/dvddrive_settings.go +++ b/pkg/hypervctl/dvddrive_settings.go @@ -1,24 +1,119 @@ //go:build windows -// +build windows package hypervctl -const SyntheticDvdDriveType = "Microsoft:Hyper-V:Synthetic DVD Drive" +import ( + "fmt" + + "github.com/containers/libhvee/pkg/powershell" +) + +// DvdDriveSettings represents the arguments for the PowerShell cmdlet +// "Add-VMDvdDrive". This type can be used to add a new DVD drive +// to a Hyper-V virtual machine. +// +// Pointers are used for optional fields to differentiate between a zero value +// and a field that was not explicitly provided. +type DvdDriveSettings struct { + // VMName specifies the name of the virtual machine to which the DVD drive + // is to be added. This is a mandatory parameter in one of the parameter sets. + VMName string `json:"VMName,omitempty"` + + // ControllerNumber specifies the number of the controller. + ControllerNumber int `json:"ControllerNumber,omitempty"` + + // ControllerLocation specifies the location number on the controller. + ControllerLocation int `json:"ControllerLocation,omitempty"` + + // Path specifies the full path to the virtual DVD media file (.iso). + Path string `json:"Path,omitempty"` + + // AllowUnverifiedPaths specifies that no error should be thrown if the path + // is not verified for clustered virtual machines. + AllowUnverifiedPaths bool `json:"AllowUnverifiedPaths,omitempty"` + + // Passthru specifies that the added DvdDrive object should be + // passed through to the pipeline. + Passthru bool `json:"Passthru,omitempty"` + + // ResourcePoolName specifies the friendly name of the ISO resource pool. + ResourcePoolName string `json:"ResourcePoolName,omitempty"` + + // Other optional parameters for remote sessions or confirmation. + ComputerName string `json:"ComputerName,omitempty"` + Confirm bool `json:"Confirm,omitempty"` +} + +// GetCLI generates PowerShell Add-VMDvdDrive command parameters from DvdDriveSettings +func (d *DvdDriveSettings) GetCLI() []string { + if d.VMName == "" { + return []string{} + } + + params := []string{} + + // VM Name is required + params = append(params, "-VMName", fmt.Sprintf("'%s'", d.VMName)) + + // String parameters + if d.Path != "" { + params = append(params, "-Path", fmt.Sprintf("'%s'", d.Path)) + } + if d.ResourcePoolName != "" { + params = append(params, "-ResourcePoolName", fmt.Sprintf("'%s'", d.ResourcePoolName)) + } + if d.ComputerName != "" { + params = append(params, "-ComputerName", fmt.Sprintf("'%s'", d.ComputerName)) + } + + // Numeric parameters (note: some parameters allow 0 as valid value in PowerShell) + if d.ControllerNumber >= 0 { + params = append(params, "-ControllerNumber", fmt.Sprintf("%d", d.ControllerNumber)) + } + if d.ControllerLocation >= 0 { + params = append(params, "-ControllerLocation", fmt.Sprintf("%d", d.ControllerLocation)) + } + + // Boolean parameters (switch parameters only need to be present for true values) + if d.AllowUnverifiedPaths { + params = append(params, "-AllowUnverifiedPaths") + } + if d.Passthru { + params = append(params, "-Passthru") + } + if d.Confirm { + params = append(params, "-Confirm") + } + + return params +} type SyntheticDvdDriveSettings struct { - ResourceSettings - systemSettings *SystemSettings + DvdDriveSettings controllerSettings *ScsiControllerSettings } -func (d *SyntheticDvdDriveSettings) DefineVirtualDvdDisk(imageFile string) (*VirtualDvdDiskStorageSettings, error) { - vdvd := &VirtualDvdDiskStorageSettings{} +func (d *SyntheticDvdDriveSettings) DefineVirtualDvdDisk(imageFile string) (*DvdDriveSettings, error) { + dvd := &DvdDriveSettings{} + + // Copy settings from parent + dvd.VMName = d.VMName + dvd.ControllerNumber = d.ControllerNumber + dvd.ControllerLocation = d.ControllerLocation + dvd.Path = imageFile + dvd.AllowUnverifiedPaths = d.AllowUnverifiedPaths + dvd.Passthru = d.Passthru + dvd.ResourcePoolName = d.ResourcePoolName + dvd.ComputerName = d.ComputerName + dvd.Confirm = d.Confirm - if err := createDiskResourceInternal(d.systemSettings.Path(), d.Path(), imageFile, vdvd, VirtualDvdDiskType, nil); err != nil { - return nil, err + // Generate CLI command and execute + cli := dvd.GetCLI() + cli = append([]string{"Hyper-V\\Add-VMDvdDrive"}, cli...) + _, stderr, err := powershell.Execute(cli...) + if err != nil { + return nil, NewPSError(stderr) } - vdvd.driveSettings = d - vdvd.systemSettings = d.systemSettings - return vdvd, nil + return dvd, nil } diff --git a/pkg/hypervctl/error.go b/pkg/hypervctl/error.go index 2f16971..559f991 100644 --- a/pkg/hypervctl/error.go +++ b/pkg/hypervctl/error.go @@ -1,174 +1,25 @@ //go:build windows -// +build windows package hypervctl import ( "errors" - "fmt" - - "github.com/containers/libhvee/pkg/wmiext" -) - -// VM State errors -var ( - ErrMachineAlreadyRunning = errors.New("machine already running") - ErrMachineNotRunning = errors.New("machine not running") - ErrMachineStateInvalid = errors.New("machine in invalid state for action") - ErrMachineStarting = errors.New("machine is currently starting") + "strings" ) // VM Creation errors var ( - ErrMachineAlreadyExists = errors.New("machine already exists") -) - -type DestroySystemResult int32 - -// VM Destroy Exit Codes -const ( - VMDestroyCompletedwithNoError DestroySystemResult = 0 - VMDestroyNotSupported DestroySystemResult = 1 - VMDestroyFailed DestroySystemResult = 2 - VMDestroyTimeout DestroySystemResult = 3 - VMDestroyInvalidParameter DestroySystemResult = 4 - VMDestroyInvalidState DestroySystemResult = 5 -) - -func (e DestroySystemResult) Reason() string { - switch e { - case VMDestroyNotSupported: - return "not supported" - case VMDestroyFailed: - return "failed" - case VMDestroyTimeout: - return "timeout" - case VMDestroyInvalidParameter: - return "invalid parameter" - case VMDestroyInvalidState: - return "invalid state" - } - return "Unknown" -} - -// Shutdown operation error codes -const ( - ErrShutdownFailed = 32768 - ErrShutdownAccessDenied = 32769 - ErrShutdownNotSupported = 32770 - ErrShutdownStatusUnkown = 32771 - ErrShutdownTimeout = 32772 - ErrShutdownInvalidParameter = 32773 - ErrShutdownSystemInUse = 32774 - ErrShutdownInvalidState = 32775 - ErrShutdownIncorrectData = 32776 - ErrShutdownNotAvailable = 32777 - ErrShutdownOutOfMemory = 32778 - ErrShutdownFileNotFound = 32779 - ErrShutdownNotReady = 32780 - ErrShutdownMachineLocked = 32781 - ErrShutdownInProgress = 32782 -) - -type shutdownCompError struct { - errorCode int - message string -} - -func (s *shutdownCompError) Error() string { - return fmt.Sprintf("%s (%d)", s.message, s.errorCode) -} - -func translateShutdownError(code int) error { - var message string - switch code { - case ErrShutdownFailed: - message = "shutdown failed" - case ErrShutdownAccessDenied: - message = "access was denied" - case ErrShutdownNotSupported: - message = "shutdown not supported by virtual machine" - case ErrShutdownStatusUnkown: - message = "virtual machine status is unknown" - case ErrShutdownTimeout: - message = "timeout starting shutdown" - case ErrShutdownInvalidParameter: - message = "invalid parameter" - case ErrShutdownSystemInUse: - message = "system in use" - case ErrShutdownInvalidState: - message = "virtual machine is in an invalid state for shutdown" - case ErrShutdownIncorrectData: - message = "incorrect data type" - case ErrShutdownNotAvailable: - message = "system is not available" - case ErrShutdownOutOfMemory: - message = "out of memory" - case ErrShutdownFileNotFound: - message = "file not found" - case ErrShutdownMachineLocked: - message = "machine is locked and cannot be shut down without the force option" - case ErrShutdownInProgress: - message = "shutdown is already in progress" - default: - message = "unknown error" - } - - return &shutdownCompError{code, message} -} - -// Modify resource errors -const ( - ErrModifyResourceNotSupported = 1 - ErrModifyResourceFailed = 2 - ErrModifyResourceTimeout = 3 - ErrModifyResourceInvalidParameter = 4 - ErrModifyResourceInvalidState = 5 - ErrModifyResourceIncompatParam = 6 -) - -type modifyResourceError struct { - errorCode int - message string -} - -func (m *modifyResourceError) Error() string { - return fmt.Sprintf("%s (%d)", m.message, m.errorCode) -} - -func translateModifyError(code int) error { - var message string - switch code { - case ErrModifyResourceNotSupported: - message = "virtual machine does not support modification operations" - case ErrModifyResourceFailed: - message = "resource modification failed" - case ErrModifyResourceTimeout: - message = "timeout modifying resource" - case ErrModifyResourceInvalidParameter: - message = "a modify resource operation was passed an invalid parameter" - case ErrModifyResourceInvalidState: - message = "the requested modification could not be applied due to an invalid state" - case ErrModifyResourceIncompatParam: - message = "an incompatible parameter was passed to a modify resource operation" - default: - message = "unknown error" - } - - return &modifyResourceError{code, message} -} - -var ( - ErrHyperVNamespaceMissing = errors.New("HyperV namespace not found, is HyperV enabled?") + ErrMachineAlreadyExists = errors.New("machine already exists") + ErrMachineStateInvalid = errors.New("machine in invalid state for action") + ErrMachineNotRunning = errors.New("machine not running") + ErrMachineAlreadyRunning = errors.New("machine already running") ) -func translateCommonHyperVWmiError(wmiError error) error { - if werr, ok := wmiError.(*wmiext.WmiError); ok { - switch werr.Code() { - case wmiext.WBEM_E_INVALID_NAMESPACE: - return ErrHyperVNamespaceMissing - } +func NewPSError(stderr string) error { + strArr := strings.Split(stderr, "\n") + // take the first line as it contains the error message + if len(strArr) > 0 { + return errors.New(strArr[0]) } - - return wmiError + return errors.New(stderr) } diff --git a/pkg/hypervctl/ethernet_alloc_settings.go b/pkg/hypervctl/ethernet_alloc_settings.go deleted file mode 100644 index 9cdb644..0000000 --- a/pkg/hypervctl/ethernet_alloc_settings.go +++ /dev/null @@ -1,50 +0,0 @@ -//go:build windows -// +build windows - -package hypervctl - -const EthernetPortAllocationResourceType = "Microsoft:Hyper-V:Ethernet Connection" - -type EthernetPortAllocationSettings struct { - InstanceID string // = "Microsoft:GUID\DeviceSpecificData" - Caption string // = "Ethernet Switch Port Settings" - Description string // = "Ethernet Switch Port Settings" - ElementName string - ResourceType uint16 // = 33 - OtherResourceType string - ResourceSubType string - PoolID string - ConsumerVisibility uint16 // = 3 - HostResource []string - AllocationUnits string - VirtualQuantity uint64 - Reservation uint64 - Limit uint64 - Weight uint32 // = 0 - AutomaticAllocation bool - AutomaticDeallocation bool - Parent string - Connection []string - Address string - MappingBehavior uint16 - AddressOnParent string - VirtualQuantityUnits string // = "count" - DesiredVLANEndpointMode uint16 - OtherEndpointMode string - EnabledState uint16 - LastKnownSwitchName string - RequiredFeatures []string - RequiredFeatureHints []string - TestReplicaPoolID string - TestReplicaSwitchName string - CompartmentGuid string -} - -func fetchEthernetPortAllocationSettings() (*EthernetPortAllocationSettings, error) { - settings := &EthernetPortAllocationSettings{} - return settings, populateDefaults(EthernetPortAllocationResourceType, settings) -} - -func creatEthernetPortAllocationSettings(settings *EthernetPortAllocationSettings) (string, error) { - return createResourceSettingGeneric(settings, EthernetPortAllocationResourceType) -} diff --git a/pkg/hypervctl/ethernet_port_settings.go b/pkg/hypervctl/ethernet_port_settings.go deleted file mode 100644 index 294d87e..0000000 --- a/pkg/hypervctl/ethernet_port_settings.go +++ /dev/null @@ -1,109 +0,0 @@ -//go:build windows -// +build windows - -package hypervctl - -import ( - "fmt" - - "github.com/containers/libhvee/pkg/wmiext" -) - -const SyntheticEthernetPortResourceType = "Microsoft:Hyper-V:Synthetic Ethernet Port" -const DefaultSwitchId = "C08CB7B8-9B3C-408E-8E30-5E16A3AEB444" - -type SyntheticEthernetPortSettings struct { - S__PATH string - InstanceID string - Caption string // = "Virtual Ethernet Port Default Settings" - Description string // = "Describes the default settings for the virtual Ethernet port resources." - ElementName string - ResourceType uint16 // = 10 - OtherResourceType string - ResourceSubType string // = "Microsoft:Hyper-V:Synthetic Ethernet Port" - PoolID string - ConsumerVisibility uint16 // = 3 - HostResource []string - AllocationUnits string // = "count" - VirtualQuantity uint64 // = 1 - Reservation uint64 // = 1 - Limit uint64 // = 1 - Weight uint32 // = 0 - AutomaticAllocation bool // = True - AutomaticDeallocation bool // = True - Parent string - Connection []string - Address string - MappingBehavior uint16 - AddressOnParent string - VirtualQuantityUnits string // = "count" - DesiredVLANEndpointMode uint16 - OtherEndpointMode string - VirtualSystemIdentifiers []string - DeviceNamingEnabled bool // = FALSE - AllowPacketDirect bool // = FALSE - StaticMacAddress bool // = False - ClusterMonitored bool // = TRUE - - systemSettings *SystemSettings -} - -func (p *SyntheticEthernetPortSettings) Path() string { - return p.S__PATH -} - -func (p *SyntheticEthernetPortSettings) DefineEthernetPortConnection(switchName string) (*EthernetPortAllocationSettings, error) { - const wqlFormat = "select * from Msvm_VirtualEthernetSwitch where %s = '%s'" - - var wqlProperty, wqlValue string - if len(switchName) > 0 { - wqlProperty = "ElementName" - wqlValue = switchName - } else { - wqlProperty = "Name" - wqlValue = DefaultSwitchId - } - - wql := fmt.Sprintf(wqlFormat, wqlProperty, wqlValue) - - var service *wmiext.Service - var err error - if service, err = NewLocalHyperVService(); err != nil { - return nil, err - } - defer service.Close() - - switchInst, err := service.FindFirstInstance(wql) - if err != nil { - return nil, err - } - defer switchInst.Close() - switchPath, err := switchInst.Path() - if err != nil { - return nil, err - } - - connectSettings, err := fetchEthernetPortAllocationSettings() - if err != nil { - return nil, err - } - - connectSettings.Parent = p.Path() - connectSettings.HostResource = append(connectSettings.HostResource, switchPath) - - resource, err := creatEthernetPortAllocationSettings(connectSettings) - if err != nil { - return nil, err - } - - path, err := addResource(service, p.systemSettings.Path(), resource) - if err != nil { - return nil, err - } - - if err := service.GetObjectAsObject(path, connectSettings); err != nil { - return nil, err - } - - return connectSettings, nil -} diff --git a/pkg/hypervctl/helper.go b/pkg/hypervctl/helper.go new file mode 100644 index 0000000..4fc3852 --- /dev/null +++ b/pkg/hypervctl/helper.go @@ -0,0 +1,44 @@ +//go:build windows + +package hypervctl + +import ( + "encoding/json" + "strings" +) + +// StringOrArray represents a field that can be either a string or an array of strings +type StringOrArray []string + +// UnmarshalJSON implements custom unmarshaling for string or array of strings +func (s *StringOrArray) UnmarshalJSON(data []byte) error { + // Try to unmarshal as an array first + var arr []string + if err := json.Unmarshal(data, &arr); err == nil { + *s = StringOrArray(arr) + return nil + } + + // If that fails, try to unmarshal as a single string + var str string + if err := json.Unmarshal(data, &str); err != nil { + return err + } + + // Wrap the single string in an array + *s = StringOrArray([]string{str}) + return nil +} + +// String returns a comma-separated string of all values +func (s StringOrArray) String() string { + return strings.Join([]string(s), ", ") +} + +// First returns the first element or empty string if empty +func (s StringOrArray) First() string { + if len(s) > 0 { + return s[0] + } + return "" +} diff --git a/pkg/hypervctl/kvp.go b/pkg/hypervctl/kvp.go index 04bed34..bc92b08 100644 --- a/pkg/hypervctl/kvp.go +++ b/pkg/hypervctl/kvp.go @@ -1,33 +1,11 @@ //go:build windows -// +build windows package hypervctl import ( "encoding/xml" - "fmt" "io" "strings" - - "github.com/containers/libhvee/pkg/wmiext" -) - -const ( - KvpOperationFailed = 32768 - KvpAccessDenied = 32769 - KvpNotSupported = 32770 - KvpStatusUnknown = 32771 - KvpTimeoutOccurred = 32772 - KvpIllegalArgument = 32773 - KvpSystemInUse = 32774 - KvpInvalidState = 32775 - KvpIncorrectDataType = 32776 - KvpSystemNotAvailable = 32777 - KvpOutOfMemory = 32778 - KvpNotFound = 32779 - - KvpExchangeDataItemName = "Msvm_KvpExchangeDataItem" - MemorySettingDataName = "Msvm_MemorySettingData" ) type CimKvpItems struct { @@ -43,29 +21,6 @@ type CimKvpItemProperty struct { Value string `xml:"VALUE"` } -type KvpError struct { - ErrorCode int - message string -} - -func (k *KvpError) Error() string { - return fmt.Sprintf("%s (%d)", k.message, k.ErrorCode) -} - -func createKvpItem(service *wmiext.Service, key string, value string) (string, error) { - item, err := service.SpawnInstance(KvpExchangeDataItemName) - if err != nil { - return "", err - } - defer item.Close() - - _ = item.Put("Name", key) - _ = item.Put("Data", value) - _ = item.Put("Source", 0) - itemStr := item.GetCimText() - return itemStr, nil -} - func parseKvpMapXml(kvpXml string) (map[string]string, error) { // Workaround XML decoder's inability to handle multiple root elements r := io.MultiReader( @@ -96,43 +51,3 @@ func parseKvpMapXml(kvpXml string) (map[string]string, error) { return ret, nil } - -func translateKvpError(source error, illegalSuggestion string) error { - j, ok := source.(*wmiext.JobError) - - if !ok { - return source - } - - var message string - switch j.ErrorCode { - case KvpOperationFailed: - message = "Operation failed" - case KvpAccessDenied: - message = "Access denied" - case KvpNotSupported: - message = "Not supported" - case KvpStatusUnknown: - message = "Status is unknown" - case KvpTimeoutOccurred: - message = "Timeout occurred" - case KvpIllegalArgument: - message = "Illegal argument (" + illegalSuggestion + ")" - case KvpSystemInUse: - message = "System is in use" - case KvpInvalidState: - message = "Invalid state for this operation" - case KvpIncorrectDataType: - message = "Incorrect data type" - case KvpSystemNotAvailable: - message = "System is not available" - case KvpOutOfMemory: - message = "Out of memory" - case KvpNotFound: - message = "Not found" - default: - return source - } - - return &KvpError{j.ErrorCode, message} -} diff --git a/pkg/hypervctl/memory_settings.go b/pkg/hypervctl/memory_settings.go index 960694e..f2785c0 100644 --- a/pkg/hypervctl/memory_settings.go +++ b/pkg/hypervctl/memory_settings.go @@ -1,56 +1,181 @@ //go:build windows -// +build windows package hypervctl -import "fmt" +import ( + "encoding/json" + "fmt" + "os" -const MemoryResourceType = "Microsoft:Hyper-V:Memory" + "github.com/containers/libhvee/pkg/powershell" +) +// SetVMMemoryParams represents the arguments for the PowerShell cmdlet +// "Set-VMMemory". This type can be used to configure memory settings +// for a Hyper-V virtual machine. +// +// Pointers are used for optional fields to differentiate between a zero value +// (e.g., an empty string or 0) and a field that was not provided. type MemorySettings struct { - S__PATH string - InstanceID string - Caption string // = "Memory Default Settings" - Description string // = "Describes the default settings for the memory resources." - ElementName string - ResourceType uint16 // = 4 - OtherResourceType string - ResourceSubType string // = "Microsoft:Hyper-V:Memory" - PoolID string - ConsumerVisibility uint16 - HostResource []string - HugePagesEnabled bool - AllocationUnits string // = "byte * 2^20" - VirtualQuantity uint64 - Reservation uint64 - Limit uint64 - Weight uint32 - AutomaticAllocation bool // = True - AutomaticDeallocation bool // = True - Parent string - Connection []string - Address string - MappingBehavior uint16 - AddressOnParent string - VirtualQuantityUnits string // = "byte * 2^20" - DynamicMemoryEnabled bool - TargetMemoryBuffer uint32 - IsVirtualized bool // = True - SwapFilesInUse bool - MaxMemoryBlocksPerNumaNode uint64 - SgxSize uint64 - SgxEnabled bool + // VMName specifies the name of the virtual machine to configure. + // This is typically a required parameter in one of the parameter sets. + VMName string `json:"vmName,omitempty"` + + // DynamicMemoryEnabled is a boolean parameter to enable or disable + // dynamic memory for the virtual machine. + DynamicMemoryEnabled bool `json:"dynamicMemoryEnabled,omitempty"` + + // MinimumBytes specifies the minimum amount of memory, in bytes, + // that a virtual machine can be configured with when Dynamic Memory is enabled. + MinimumBytes uint64 `json:"minimumBytes,omitempty"` + + // StartupBytes specifies the amount of memory, in bytes, that a + // virtual machine will be allocated on startup. + StartupBytes uint64 `json:"startupBytes,omitempty"` + + // MaximumBytes specifies the maximum amount of memory, in bytes, + // that a virtual machine can be configured with when Dynamic Memory is enabled. + MaximumBytes uint64 `json:"maximumBytes,omitempty"` + + // Buffer specifies the percentage of memory to be reserved as a buffer + // within the virtual machine (allowed values 5 to 2000). + Buffer int `json:"buffer,omitempty"` + + // Priority sets the priority for memory availability to this VM + // relative to others (allowed values 0 to 100). + Priority int `json:"priority,omitempty"` + + // ResourcePoolName specifies the name of the memory resource pool + // for the virtual machine. + ResourcePoolName string `json:"resourcePoolName,omitempty"` + + // ComputerName specifies one or more Hyper-V hosts on which to + // configure the VM memory. + ComputerName string `json:"computerName,omitempty"` } -func createMemorySettings(settings *MemorySettings) (string, error) { - str, err := createResourceSettingGeneric(settings, MemoryResourceType) +func fetchDefaultMemorySettings() (*MemorySettings, error) { + settings := &MemorySettings{} + return settings, nil +} + +func updateVMMemory(vmName string, settings *MemorySettings) error { + memoryStr := getMemoryCLI(vmName, settings) + args := append([]string{"Hyper-V\\Set-VMMemory"}, memoryStr...) + _, stderr, err := powershell.Execute(args...) if err != nil { - err = fmt.Errorf("could not create memory settings: %w", err) + fmt.Fprintf(os.Stderr, "error setting vm memory: %s\n", stderr) + return NewPSError(stderr) } - return str, err + return nil } -func fetchDefaultMemorySettings() (*MemorySettings, error) { - settings := &MemorySettings{} - return settings, populateDefaults(MemoryResourceType, settings) +// getMemoryCLI generates PowerShell Set-VMMemory command parameters from MemorySettings +func getMemoryCLI(vmName string, settings *MemorySettings) []string { + if vmName == "" { + fmt.Fprintf(os.Stderr, "settings.VMName is empty\n") + return []string{} + } + + params := []string{} + + // VM Name is required + params = append(params, "-VMName", fmt.Sprintf("'%s'", vmName)) + + // Boolean parameters + if settings.DynamicMemoryEnabled { + params = append(params, "-DynamicMemoryEnabled", "$True") + } else { + params = append(params, "-DynamicMemoryEnabled", "$False") + } + + // Numeric parameters (only add if > 0) + + if settings.StartupBytes > 0 { + params = append(params, "-StartupBytes", fmt.Sprintf("%d", settings.StartupBytes)) + } + + // If Dynamic Memory is enabled, we can set the minimum, maximum, and buffer + if settings.DynamicMemoryEnabled { + if settings.MinimumBytes > 0 { + params = append(params, "-MinimumBytes", fmt.Sprintf("%d", settings.MinimumBytes)) + } + + if settings.MaximumBytes > 0 { + params = append(params, "-MaximumBytes", fmt.Sprintf("%d", settings.MaximumBytes)) + } + if settings.Buffer > 0 { + params = append(params, "-Buffer", fmt.Sprintf("%d", settings.Buffer)) + } + } + if settings.Priority > 0 { + params = append(params, "-Priority", fmt.Sprintf("%d", settings.Priority)) + } + + + // String parameters + if settings.ResourcePoolName != "" { + params = append(params, "-ResourcePoolName", fmt.Sprintf("'%s'", settings.ResourcePoolName)) + } + if settings.ComputerName != "" { + params = append(params, "-ComputerName", fmt.Sprintf("'%s'", settings.ComputerName)) + } + + return params +} + +// GetVMMemoryFromName executes Get-VMMemory PowerShell command and parses JSON output into MemorySettings +func GetVMMemoryFromName(vmName string) (*MemorySettings, error) { + if vmName == "" { + return nil, fmt.Errorf("VM name cannot be empty") + } + + // Execute PowerShell command to get VM memory info as JSON + cmd := fmt.Sprintf("Get-VMMemory -VMName '%s' | ConvertTo-Json", vmName) + stdout, stderr, err := powershell.Execute(cmd) + if err != nil { + return nil, fmt.Errorf("failed to execute PowerShell command: %w, stderr: %s", err, stderr) + } + + // Parse JSON response + var memoryData map[string]interface{} + if err := json.Unmarshal([]byte(stdout), &memoryData); err != nil { + return nil, fmt.Errorf("failed to parse JSON output: %w", err) + } + + // Create MemorySettings instance and map fields + settings := &MemorySettings{ + VMName: vmName, + } + + // Map PowerShell VM memory properties to MemorySettings fields + if val, ok := memoryData["DynamicMemoryEnabled"].(bool); ok { + settings.DynamicMemoryEnabled = val + } + + if val, ok := memoryData["Minimum"].(float64); ok { + settings.MinimumBytes = uint64(val) + } + + if val, ok := memoryData["Startup"].(float64); ok { + settings.StartupBytes = uint64(val) + } + + if val, ok := memoryData["Maximum"].(float64); ok { + settings.MaximumBytes = uint64(val) + } + + if val, ok := memoryData["Buffer"].(float64); ok { + settings.Buffer = int(val) + } + + if val, ok := memoryData["Priority"].(float64); ok { + settings.Priority = int(val) + } + + if val, ok := memoryData["ResourcePoolName"].(string); ok { + settings.ResourcePoolName = val + } + + return settings, nil } diff --git a/pkg/hypervctl/network_adapter_settings.go b/pkg/hypervctl/network_adapter_settings.go new file mode 100644 index 0000000..a9e3c51 --- /dev/null +++ b/pkg/hypervctl/network_adapter_settings.go @@ -0,0 +1,270 @@ +//go:build windows + +package hypervctl + +import ( + "encoding/json" + "fmt" + "strings" + + "github.com/containers/libhvee/pkg/powershell" +) + +const defaultSwitchName = "'Default Switch'" + +// AddVMNetworkAdapterParams represents the arguments for the PowerShell cmdlet +// "Add-VMNetworkAdapter". This type can be used to add a new network adapter +// to a Hyper-V virtual machine. +// +// Pointers are used for optional fields to differentiate between a zero value +// and a field that was not explicitly provided. +type NetworkAdapterParams struct { + // VMName specifies the name of the virtual machine to which the network adapter + // is to be added. This is a mandatory parameter in one of the parameter sets. + VMName string `json:"vmName,omitempty"` + + // Name assigns a name to the new virtual network adapter (default is "Network Adapter"). + Name string `json:"name,omitempty"` + + // SwitchName specifies the name of the virtual switch to connect the new adapter to. + SwitchName string `json:"switchName,omitempty"` + + // ResourcePoolName specifies the friendly name of a resource pool. + ResourcePoolName string `json:"resourcePoolName,omitempty"` + + // StaticMacAddress assigns a specific MAC address to the new adapter. + StaticMacAddress string `json:"staticMacAddress,omitempty"` + + // DynamicMacAddress assigns a dynamically generated MAC address to the new adapter. + // This is a switch parameter, so a pointer to a bool is used. + DynamicMacAddress bool `json:"dynamicMacAddress,omitempty"` + + // IsLegacy specifies if the virtual network adapter is a legacy type. + IsLegacy bool `json:"isLegacy,omitempty"` + + // ManagementOS specifies that the adapter should be added to the management OS. + // This is a switch parameter. + ManagementOS bool `json:"managementOS,omitempty"` + + // DeviceNaming adds a virtual network adapter to a virtual machine. + // This is a switch parameter. + DeviceNaming bool `json:"deviceNaming,omitempty"` + + // Passthru passes the object representing the new network adapter to the pipeline. + Passthru bool `json:"passthru,omitempty"` + + // Other optional parameters for remote sessions or confirmation. + ComputerName string `json:"computerName,omitempty"` + Confirm bool `json:"confirm,omitempty"` +} + +func (n *NetworkAdapterParams) DefineEthernetPortConnection(switchName string) (*NetworkAdapterParams, error) { + n.SwitchName = switchName + adapterStr := n.ToAddVMNetworkAdapterArgs() + args := append([]string{"Hyper-V\\Add-VMNetworkAdapter"}, adapterStr...) + _, stderr, err := powershell.Execute(args...) + if err != nil { + return nil, NewPSError(stderr) + } + + adapters, err := GetVMNetworkAdapterAndParse(n.VMName) + if err != nil { + return nil, err + } + + return adapters[len(adapters)-1], nil +} + +// ToAddVMNetworkAdapterArgs converts NetworkAdapterParams to CLI arguments for Add-VMNetworkAdapter +func (n *NetworkAdapterParams) ToAddVMNetworkAdapterArgs() []string { + var args []string + + // Required parameter - VMName + if n.VMName != "" { + args = append(args, "-VMName", fmt.Sprintf("'%s'", n.VMName)) + } + + // Optional parameters + if n.Name != "" { + args = append(args, "-Name", fmt.Sprintf("'%s'", n.Name)) + } + + if n.SwitchName != "" { + args = append(args, "-SwitchName", fmt.Sprintf("'%s'", n.SwitchName)) + } else { + args = append(args, "-SwitchName", defaultSwitchName) + } + + if n.ResourcePoolName != "" { + args = append(args, "-ResourcePoolName", fmt.Sprintf("'%s'", n.ResourcePoolName)) + } + + if n.StaticMacAddress != "" { + args = append(args, "-StaticMacAddress", fmt.Sprintf("'%s'", n.StaticMacAddress)) + } + + // Switch parameters (boolean flags) + if n.DynamicMacAddress { + args = append(args, "-DynamicMacAddress") + } + + if n.IsLegacy { + args = append(args, "-IsLegacy", "$True") + } + + if n.ManagementOS { + args = append(args, "-ManagementOS") + } + + if n.DeviceNaming { + args = append(args, "-DeviceNaming", "On") + } + + if n.Passthru { + args = append(args, "-Passthru") + } + + if n.ComputerName != "" { + args = append(args, "-ComputerName", fmt.Sprintf("'%s'", n.ComputerName)) + } + + if n.Confirm { + args = append(args, "-Confirm") + } + + return args +} + +// GetVMNetworkAdapterAndParse calls Get-VMNetworkAdapter CLI, parses output and creates NetworkAdapterParams +func GetVMNetworkAdapterAndParse(vmName string) ([]*NetworkAdapterParams, error) { + // Build the Get-VMNetworkAdapter command + var args []string + args = append(args, "Get-VMNetworkAdapter") + + if vmName != "" { + args = append(args, "-VMName", fmt.Sprintf("'%s'", vmName)) + } + + // Convert to JSON for easier parsing + args = append(args, "|", "ConvertTo-Json", "-Depth", "3") + + cmd := strings.Join(args, " ") + + // Execute the PowerShell command + stdout, stderr, err := powershell.Execute(cmd) + if err != nil { + return nil, fmt.Errorf("failed to execute Get-VMNetworkAdapter: %w", NewPSError(stderr)) + } + + // Parse JSON output + stdout = strings.TrimSpace(stdout) + if stdout == "" || stdout == "null" { + return []*NetworkAdapterParams{}, nil + } + + // Handle both single object and array responses + var rawData interface{} + if err := json.Unmarshal([]byte(stdout), &rawData); err != nil { + return nil, fmt.Errorf("failed to parse JSON output: %w", err) + } + + var adapters []*NetworkAdapterParams + + // Check if it's an array or single object + switch data := rawData.(type) { + case []interface{}: + // Multiple adapters + for _, item := range data { + adapter, err := parseNetworkAdapterFromMap(item) + if err != nil { + return nil, fmt.Errorf("failed to parse network adapter: %w", err) + } + adapters = append(adapters, adapter) + } + case map[string]interface{}: + // Single adapter + adapter, err := parseNetworkAdapterFromMap(data) + if err != nil { + return nil, fmt.Errorf("failed to parse network adapter: %w", err) + } + adapters = append(adapters, adapter) + default: + return nil, fmt.Errorf("unexpected JSON structure") + } + + return adapters, nil +} + +// parseNetworkAdapterFromMap converts a map[string]interface{} to NetworkAdapterParams +func parseNetworkAdapterFromMap(data interface{}) (*NetworkAdapterParams, error) { + dataMap, ok := data.(map[string]interface{}) + if !ok { + return nil, fmt.Errorf("expected map[string]interface{}, got %T", data) + } + + adapter := &NetworkAdapterParams{} + + // Map PowerShell property names to NetworkAdapterParams fields + if vmName, exists := dataMap["VMName"]; exists { + if str, ok := vmName.(string); ok { + adapter.VMName = str + } + } + + if name, exists := dataMap["Name"]; exists { + if str, ok := name.(string); ok { + adapter.Name = str + } + } + + if switchName, exists := dataMap["SwitchName"]; exists { + if str, ok := switchName.(string); ok { + adapter.SwitchName = str + } + } + + if poolName, exists := dataMap["PoolName"]; exists { + if str, ok := poolName.(string); ok { + adapter.ResourcePoolName = str + } + } + + if macAddress, exists := dataMap["MacAddress"]; exists { + if str, ok := macAddress.(string); ok { + adapter.StaticMacAddress = str + } + } + + // Check if MAC address is dynamic + if dynamicMac, exists := dataMap["DynamicMacAddressEnabled"]; exists { + if b, ok := dynamicMac.(bool); ok { + adapter.DynamicMacAddress = b + } + } + + if isLegacy, exists := dataMap["IsLegacy"]; exists { + if b, ok := isLegacy.(bool); ok { + adapter.IsLegacy = b + } + } + + if managementOS, exists := dataMap["IsManagementOs"]; exists { + if b, ok := managementOS.(bool); ok { + adapter.ManagementOS = b + } + } + + if deviceNaming, exists := dataMap["DeviceNaming"]; exists { + if f, ok := deviceNaming.(float64); ok { + adapter.DeviceNaming = (f == 0) + } + } + + if computerName, exists := dataMap["ComputerName"]; exists { + if str, ok := computerName.(string); ok { + adapter.ComputerName = str + } + } + + return adapter, nil +} diff --git a/pkg/hypervctl/network_settings_builder.go b/pkg/hypervctl/network_settings_builder.go index ed5abd6..e3855ba 100644 --- a/pkg/hypervctl/network_settings_builder.go +++ b/pkg/hypervctl/network_settings_builder.go @@ -1,5 +1,4 @@ //go:build windows -// +build windows package hypervctl @@ -10,13 +9,13 @@ type NetworkSettingsBuilder struct { type SyntheticEthernetPortSettingsBuilder struct { networkSettingsBuilder *NetworkSettingsBuilder - portSettings *SyntheticEthernetPortSettings + portSettings *NetworkAdapterParams err error } type EthernetPortAllocationSettingsBuilder struct { portSettingsBuilder *SyntheticEthernetPortSettingsBuilder - allocSettings *EthernetPortAllocationSettings + allocSettings *NetworkAdapterParams err error } @@ -24,18 +23,22 @@ func NewNetworkSettingsBuilder(systemSettings *SystemSettings) *NetworkSettingsB return &NetworkSettingsBuilder{systemSettings: systemSettings} } -func (builder *NetworkSettingsBuilder) AddSyntheticEthernetPort(beforeAdd func(*SyntheticEthernetPortSettings)) *SyntheticEthernetPortSettingsBuilder { +func (builder *NetworkSettingsBuilder) AddSyntheticEthernetPort(beforeAdd func(*NetworkAdapterParams)) *SyntheticEthernetPortSettingsBuilder { if builder.err != nil { return &SyntheticEthernetPortSettingsBuilder{networkSettingsBuilder: builder, err: builder.err} } - portSettings, err := builder.systemSettings.AddSyntheticEthernetPort(beforeAdd) - builder.setErr(err) + portSettings := &NetworkAdapterParams{ + VMName: builder.systemSettings.Name, + } + if beforeAdd != nil { + beforeAdd(portSettings) + } return &SyntheticEthernetPortSettingsBuilder{ networkSettingsBuilder: builder, portSettings: portSettings, - err: err, + err: nil, } } @@ -71,12 +74,12 @@ func (builder *SyntheticEthernetPortSettingsBuilder) setErr(err error) { builder.networkSettingsBuilder.setErr(err) } -func (builder *EthernetPortAllocationSettingsBuilder) Get(s **EthernetPortAllocationSettings) *EthernetPortAllocationSettingsBuilder { +func (builder *EthernetPortAllocationSettingsBuilder) Get(s **NetworkAdapterParams) *EthernetPortAllocationSettingsBuilder { *s = builder.allocSettings return builder } -func (builder *SyntheticEthernetPortSettingsBuilder) Get(s **SyntheticEthernetPortSettings) *SyntheticEthernetPortSettingsBuilder { +func (builder *SyntheticEthernetPortSettingsBuilder) Get(s **NetworkAdapterParams) *SyntheticEthernetPortSettingsBuilder { *s = builder.portSettings return builder } diff --git a/pkg/hypervctl/processor_settings.go b/pkg/hypervctl/processor_settings.go index 9c0d6d9..5946419 100644 --- a/pkg/hypervctl/processor_settings.go +++ b/pkg/hypervctl/processor_settings.go @@ -1,98 +1,222 @@ //go:build windows -// +build windows package hypervctl -import "fmt" - -const ProcessorResourceType = "Microsoft:Hyper-V:Processor" - -/* -AllocationUnits : percent / 1000 -AllowACountMCount : True -AutomaticAllocation : True -AutomaticDeallocation : True -Caption : Processor -Connection : -ConsumerVisibility : 3 -CpuGroupId : 00000000-0000-0000-0000-000000000000 -Description : Settings for Microsoft Virtual Processor. -DisableSpeculationControls : False -ElementName : Processor -EnableHostResourceProtection : False -EnableLegacyApicMode : False -EnablePageShattering : 0 -EnablePerfmonIpt : False -EnablePerfmonLbr : False -EnablePerfmonPebs : False -EnablePerfmonPmu : False -ExposeVirtualizationExtensions : False -HideHypervisorPresent : False -HostResource : -HwThreadsPerCore : 0 -InstanceID : Microsoft:B5314955-3924-42BA-ABF9-793993D340A0\b637f346-6a0e-4dec-af52-bd70cb80a21d\0 -Limit : 100000 -LimitCPUID : False -LimitProcessorFeatures : False -MappingBehavior : -MaxNumaNodesPerSocket : 1 -MaxProcessorsPerNumaNode : 4 -OtherResourceType : -Parent : -PoolID : -Reservation : 0 -ResourceSubType : Microsoft:Hyper-V:Processor -ResourceType : 3 -VirtualQuantity : 2 -VirtualQuantityUnits : count -Weight : 100 -*/ +import ( + "encoding/json" + "fmt" + "github.com/containers/libhvee/pkg/powershell" +) + +// ProcessorSettings represents the arguments for the PowerShell cmdlet +// "Set-VMProcessor". This type can be used to configure virtual processor +// settings for a Hyper-V virtual machine. +// +// Pointers are used for optional fields to differentiate between a zero value +// and a field that was not explicitly provided. type ProcessorSettings struct { - S__PATH string - InstanceID string - Caption string // = "Processor" - Description string // = "A logical processor of the hypervisor running on the host computer system." - ElementName string - ResourceType uint16 // = 3 - OtherResourceType string - ResourceSubType string // = "Microsoft:Hyper-V:Processor" - PoolID string - ConsumerVisibility uint16 - HostResource []string - AllocationUnits string // = "percent / 1000" - VirtualQuantity uint64 // = "count" - Reservation uint64 // = 0 - Limit uint64 // = 100000 - Weight uint32 // = 100 - AutomaticAllocation bool // = True - AutomaticDeallocation bool // = True - Parent string - Connection []string - Address string - MappingBehavior uint16 - AddressOnParent string - VirtualQuantityUnits string // = "count" - LimitCPUID bool - HwThreadsPerCore uint64 - LimitProcessorFeatures bool - MaxProcessorsPerNumaNode uint64 - MaxNumaNodesPerSocket uint64 - EnableHostResourceProtection bool - CpuGroupId string - HideHypervisorPresent bool - ExposeVirtualizationExtensions bool + // VMName specifies the name of the virtual machine. This parameter + // is typically used to identify the target VM. + VMName string `json:"vmName,omitempty"` + + // Count specifies the number of virtual processors for the virtual machine. + Count int64 `json:"count,omitempty"` + + // Maximum specifies the maximum percentage of processor resources + // available to the virtual machine (0-100). + Maximum int64 `json:"maximum,omitempty"` + + // Reserve specifies the percentage of processor resources to be reserved + // for the virtual machine (0-100). + Reserve int64 `json:"reserve,omitempty"` + + // RelativeWeight specifies the priority for allocating physical CPU + // time to this VM relative to others (1-10000). + RelativeWeight int `json:"relativeWeight,omitempty"` + + // CompatibilityForMigrationEnabled enables or disables compatibility mode for + // live migration between hosts with different processor versions. + CompatibilityForMigrationEnabled bool `json:"compatibilityForMigrationEnabled,omitempty"` + + // CompatibilityForOlderOperatingSystemsEnabled enables or disables + // compatibility for older guest operating systems. + CompatibilityForOlderOperatingSystemsEnabled bool `json:"compatibilityForOlderOperatingSystemsEnabled,omitempty"` + + // EnableHostResourceProtection specifies whether to enable host resource + // protection to prevent excessive resource consumption. + EnableHostResourceProtection bool `json:"enableHostResourceProtection,omitempty"` + + // ExposeVirtualizationExtensions enables nested virtualization by + // exposing virtualization extensions to the guest VM. + ExposeVirtualizationExtensions bool `json:"exposeVirtualizationExtensions,omitempty"` + + // HwThreadCountPerCore specifies the number of virtual SMT threads + // exposed to the virtual machine. + HwThreadCountPerCore int64 `json:"hwThreadCountPerCore,omitempty"` + + // MaximumCountPerNumaNode specifies the maximum number of processors + // per NUMA node. + MaximumCountPerNumaNode int `json:"maximumCountPerNumaNode,omitempty"` + + // MaximumCountPerNumaSocket specifies the maximum number of sockets + // per NUMA node. + MaximumCountPerNumaSocket int `json:"maximumCountPerNumaSocket,omitempty"` + + // ResourcePoolName specifies the name of the processor resource pool. + ResourcePoolName string `json:"resourcePoolName,omitempty"` + + // ComputerName specifies one or more Hyper-V hosts on which to + // configure the VM processor. + ComputerName string `json:"computerName,omitempty"` } func fetchDefaultProcessorSettings() (*ProcessorSettings, error) { settings := &ProcessorSettings{} - return settings, populateDefaults(ProcessorResourceType, settings) + return settings, nil +} + +func updateVMProcessor(vmName string, settings *ProcessorSettings) error { + processorStr := getProcessorCLI(settings) + args := append([]string{"Hyper-V\\Set-VMProcessor"}, processorStr...) + _, stderr, err := powershell.Execute(args...) + if err != nil { + return NewPSError(stderr) + } + return nil +} + +// getProcessorCLI generates PowerShell Set-VMProcessor command parameters from ProcessorSettings +func getProcessorCLI(settings *ProcessorSettings) []string { + if settings.VMName == "" { + return []string{} + } + + params := []string{} + + // VM Name is required + params = append(params, "-VMName", fmt.Sprintf("'%s'", settings.VMName)) + + // Boolean parameters + if settings.CompatibilityForMigrationEnabled { + params = append(params, "-CompatibilityForMigrationEnabled", "$True") + } + if settings.CompatibilityForOlderOperatingSystemsEnabled { + params = append(params, "-CompatibilityForOlderOperatingSystemsEnabled", "$True") + } + if settings.EnableHostResourceProtection { + params = append(params, "-EnableHostResourceProtection", "$True") + } + if settings.ExposeVirtualizationExtensions { + params = append(params, "-ExposeVirtualizationExtensions", "$True") + } + + // Numeric parameters (only add if > 0) + if settings.Count > 0 { + params = append(params, "-Count", fmt.Sprintf("%d", settings.Count)) + } + if settings.Maximum > 0 { + params = append(params, "-Maximum", fmt.Sprintf("%d", settings.Maximum)) + } + if settings.Reserve > 0 { + params = append(params, "-Reserve", fmt.Sprintf("%d", settings.Reserve)) + } + if settings.RelativeWeight > 0 { + params = append(params, "-RelativeWeight", fmt.Sprintf("%d", settings.RelativeWeight)) + } + if settings.HwThreadCountPerCore > 0 { + params = append(params, "-HwThreadCountPerCore", fmt.Sprintf("%d", settings.HwThreadCountPerCore)) + } + if settings.MaximumCountPerNumaNode > 0 { + params = append(params, "-MaximumCountPerNumaNode", fmt.Sprintf("%d", settings.MaximumCountPerNumaNode)) + } + if settings.MaximumCountPerNumaSocket > 0 { + params = append(params, "-MaximumCountPerNumaSocket", fmt.Sprintf("%d", settings.MaximumCountPerNumaSocket)) + } + + // String parameters + if settings.ResourcePoolName != "" { + params = append(params, "-ResourcePoolName", fmt.Sprintf("'%s'", settings.ResourcePoolName)) + } + if settings.ComputerName != "" { + params = append(params, "-ComputerName", fmt.Sprintf("'%s'", settings.ComputerName)) + } + + return params } -func createProcessorSettings(settings *ProcessorSettings) (string, error) { - str, err := createResourceSettingGeneric(settings, ProcessorResourceType) +// GetVMProcessorFromName executes Get-VMProcessor PowerShell command and parses JSON output into ProcessorSettings +func GetVMProcessorFromName(vmName string) (*ProcessorSettings, error) { + if vmName == "" { + return nil, fmt.Errorf("VM name cannot be empty") + } + + // Execute PowerShell command to get VM processor info as JSON + cmd := fmt.Sprintf("Get-VMProcessor -VMName '%s' | ConvertTo-Json", vmName) + stdout, stderr, err := powershell.Execute(cmd) if err != nil { - err = fmt.Errorf("could not create processor settings: %w", err) + return nil, fmt.Errorf("failed to execute PowerShell command: %w, stderr: %s", err, stderr) + } + + // Parse JSON response + var processorData map[string]interface{} + if err := json.Unmarshal([]byte(stdout), &processorData); err != nil { + return nil, fmt.Errorf("failed to parse JSON output: %w", err) + } + + // Create ProcessorSettings instance and map fields + settings := &ProcessorSettings{ + VMName: vmName, + } + + // Map PowerShell VM processor properties to ProcessorSettings fields + if val, ok := processorData["Count"].(float64); ok { + settings.Count = int64(val) + } + + if val, ok := processorData["Maximum"].(float64); ok { + settings.Maximum = int64(val) + } + + if val, ok := processorData["Reserve"].(float64); ok { + settings.Reserve = int64(val) + } + + if val, ok := processorData["RelativeWeight"].(float64); ok { + settings.RelativeWeight = int(val) } - return str, err + + if val, ok := processorData["CompatibilityForMigrationEnabled"].(bool); ok { + settings.CompatibilityForMigrationEnabled = val + } + + if val, ok := processorData["CompatibilityForOlderOperatingSystemsEnabled"].(bool); ok { + settings.CompatibilityForOlderOperatingSystemsEnabled = val + } + + if val, ok := processorData["EnableHostResourceProtection"].(bool); ok { + settings.EnableHostResourceProtection = val + } + + if val, ok := processorData["ExposeVirtualizationExtensions"].(bool); ok { + settings.ExposeVirtualizationExtensions = val + } + + if val, ok := processorData["HwThreadCountPerCore"].(float64); ok { + settings.HwThreadCountPerCore = int64(val) + } + + if val, ok := processorData["MaximumCountPerNumaNode"].(float64); ok { + settings.MaximumCountPerNumaNode = int(val) + } + + if val, ok := processorData["MaximumCountPerNumaSocket"].(float64); ok { + settings.MaximumCountPerNumaSocket = int(val) + } + + if val, ok := processorData["ResourcePoolName"].(string); ok { + settings.ResourcePoolName = val + } + + return settings, nil } diff --git a/pkg/hypervctl/resources_settings.go b/pkg/hypervctl/resources_settings.go deleted file mode 100644 index eda4b25..0000000 --- a/pkg/hypervctl/resources_settings.go +++ /dev/null @@ -1,135 +0,0 @@ -//go:build windows -// +build windows - -package hypervctl - -import ( - "errors" - "fmt" - - "github.com/containers/libhvee/pkg/wmiext" -) - -type ResourceSettings struct { - S__PATH string - InstanceID string // = "Microsoft:GUID\DeviceSpecificData" - Caption string - Description string - ElementName string - ResourceType uint16 - OtherResourceType string - ResourceSubType string - PoolID string - ConsumerVisibility uint16 - HostResource []string - AllocationUnits string - VirtualQuantity uint64 - Reservation uint64 - Limit uint64 - Weight uint32 - AutomaticAllocation bool - AutomaticDeallocation bool - Parent string - Connection []string - Address string - MappingBehavior uint16 - AddressOnParent string - VirtualQuantityUnits string // = "count" - VirtualSystemIdentifiers []string // = { "GUID" } -} - -func (s *ResourceSettings) setParent(parent string) { - s.Parent = parent -} - -func (s *ResourceSettings) setAddressOnParent(address string) { - s.AddressOnParent = address -} - -func (s *ResourceSettings) Path() string { - return s.S__PATH -} - -func createResourceSettingGeneric(settings interface{}, resourceType string) (string, error) { - var service *wmiext.Service - var err error - if service, err = NewLocalHyperVService(); err != nil { - return "", err - } - - ref, err := findResourceDefaults(service, resourceType) - if err != nil { - return "", err - } - - resource, err := service.GetObject(ref) - if err != nil { - return "", err - } - - defer resource.Close() - resource, err = resource.CloneInstance() - if err != nil { - return "", err - } - defer resource.Close() - - if err = resource.PutAll(settings); err != nil { - return "", err - } - - return resource.GetCimText(), nil -} - -func populateDefaults(subType string, settings interface{}) error { - var service *wmiext.Service - var err error - if service, err = NewLocalHyperVService(); err != nil { - return err - } - defer service.Close() - - ref, err := findResourceDefaults(service, subType) - if err != nil { - return err - } - - return service.GetObjectAsObject(ref, settings) -} - -func findResourceDefaults(service *wmiext.Service, subType string) (string, error) { - wql := fmt.Sprintf("SELECT * FROM Msvm_AllocationCapabilities WHERE ResourceSubType = '%s'", subType) - instance, err := service.FindFirstInstance(wql) - if err != nil { - return "", err - } - defer instance.Close() - - path, err := instance.Path() - if err != nil { - return "", err - } - - enum, err := service.ExecQuery(fmt.Sprintf("references of {%s} where ResultClass = Msvm_SettingsDefineCapabilities", path)) - if err != nil { - return "", err - } - defer enum.Close() - - for { - entry, err := enum.Next() - if err != nil { - return "", err - } - if entry == nil { - return "", errors.New("could not find settings definition for resource") - } - - value, vErr := entry.GetAsUint("ValueRole") - ref, pErr := entry.GetAsString("PartComponent") - entry.Close() - if vErr == nil && pErr == nil && value == 0 { - return ref, nil - } - } -} diff --git a/pkg/hypervctl/scsi_controller.go b/pkg/hypervctl/scsi_controller.go index d38bfac..9c61aab 100644 --- a/pkg/hypervctl/scsi_controller.go +++ b/pkg/hypervctl/scsi_controller.go @@ -1,69 +1,290 @@ //go:build windows -// +build windows package hypervctl import ( + "encoding/json" "fmt" - "github.com/containers/libhvee/pkg/wmiext" + "github.com/containers/libhvee/pkg/powershell" ) -type ScsiControllerSettings struct { - ResourceSettings - systemSettings *SystemSettings +// DriveInfo represents a drive attached to the SCSI controller +type DriveInfo struct { + Path string `json:"Path,omitempty"` + DiskNumber *int `json:"DiskNumber,omitempty"` + MaximumIOPS uint64 `json:"MaximumIOPS,omitempty"` + MinimumIOPS uint64 `json:"MinimumIOPS,omitempty"` + QoSPolicyID string `json:"QoSPolicyID,omitempty"` + SupportPersistentReservations bool `json:"SupportPersistentReservations,omitempty"` + WriteHardeningMethod int `json:"WriteHardeningMethod,omitempty"` + ControllerLocation int `json:"ControllerLocation,omitempty"` + ControllerNumber int `json:"ControllerNumber,omitempty"` + ControllerType int `json:"ControllerType,omitempty"` + Name string `json:"Name,omitempty"` + PoolName string `json:"PoolName,omitempty"` + Id string `json:"Id,omitempty"` + VMId string `json:"VMId,omitempty"` + VMName string `json:"VMName,omitempty"` + VMSnapshotId string `json:"VMSnapshotId,omitempty"` + VMSnapshotName string `json:"VMSnapshotName,omitempty"` + ComputerName string `json:"ComputerName,omitempty"` + IsDeleted bool `json:"IsDeleted,omitempty"` + DvdMediaType *int `json:"DvdMediaType,omitempty"` // Only for DVD drives } -type driveAssociation interface { - setParent(parent string) - setAddressOnParent(address string) +type ScsiControllerSettings struct { + // VMName specifies the name of the virtual machine to which the SCSI controller + // is to be added. This is a mandatory parameter in one of the parameter sets. + VMName string `json:"VMName,omitempty"` + + // Passthru specifies that a Microsoft.HyperV.PowerShell.VMScsiController object + // representing the new SCSI controller should be passed through to the pipeline. + Passthru bool `json:"passthru,omitempty"` + + // ComputerName specifies one or more Hyper-V hosts on which the SCSI controller + // should be added. + ComputerName string `json:"ComputerName,omitempty"` + + Confirm bool `json:"confirm,omitempty"` + + // Fields populated by Update method from Get-VMScsiController JSON output + ControllerNumber int `json:"ControllerNumber,omitempty"` + IsTemplate bool `json:"IsTemplate,omitempty"` + Drives []DriveInfo `json:"Drives,omitempty"` + Name string `json:"Name,omitempty"` + Id string `json:"Id,omitempty"` + VMId string `json:"VMId,omitempty"` + VMSnapshotId string `json:"VMSnapshotId,omitempty"` + VMSnapshotName string `json:"VMSnapshotName,omitempty"` + IsDeleted bool `json:"IsDeleted,omitempty"` + VMCheckpointId string `json:"VMCheckpointId,omitempty"` + VMCheckpointName string `json:"VMCheckpointName,omitempty"` } -func (c *ScsiControllerSettings) AddSyntheticDiskDrive(slot uint) (*SyntheticDiskDriveSettings, error) { - drive := &SyntheticDiskDriveSettings{} - if err := c.createSyntheticDriveInternal(slot, drive, SyntheticDiskDriveType); err != nil { - return nil, err +func (c *ScsiControllerSettings) GenerateAddCommand() []string { + return []string{ + "Hyper-V\\Add-VMScsiController", + "-VMName", c.VMName, } - drive.systemSettings = c.systemSettings - drive.controllerSettings = c - return drive, nil } -func (c *ScsiControllerSettings) AddSyntheticDvdDrive(slot uint) (*SyntheticDvdDriveSettings, error) { - drive := &SyntheticDvdDriveSettings{} - if err := c.createSyntheticDriveInternal(slot, drive, SyntheticDvdDriveType); err != nil { - return nil, err +func (c *ScsiControllerSettings) Update() (*ScsiControllerSettings, error) { + if c.VMName == "" { + return nil, fmt.Errorf("VM name cannot be empty") } - drive.systemSettings = c.systemSettings - drive.controllerSettings = c - return drive, nil + + // Execute PowerShell command to get SCSI controller info as JSON + cmd := fmt.Sprintf("Get-VMScsiController -VMName '%s' | ConvertTo-Json", c.VMName) + stdout, stderr, err := powershell.Execute(cmd) + if err != nil { + return nil, fmt.Errorf("failed to execute PowerShell command: %w, stderr: %s", err, stderr) + } + + // Parse JSON response + var controllerData interface{} + if err := json.Unmarshal([]byte(stdout), &controllerData); err != nil { + return nil, fmt.Errorf("failed to parse JSON output: %w", err) + } + + // Handle both single controller and array of controllers + var controllers []map[string]interface{} + switch data := controllerData.(type) { + case map[string]interface{}: + // Single controller + controllers = []map[string]interface{}{data} + case []interface{}: + // Multiple controllers - convert to map slice + for _, ctrl := range data { + if ctrlMap, ok := ctrl.(map[string]interface{}); ok { + controllers = append(controllers, ctrlMap) + } + } + default: + return nil, fmt.Errorf("unexpected JSON structure") + } + + // Find the first controller (or by specific controller number if needed) + if len(controllers) == 0 { + return nil, fmt.Errorf("no SCSI controllers found for VM: %s", c.VMName) + } + + // Use the first controller for now + controllerMap := controllers[0] + + // Parse the controller data + if err := c.parseControllerData(controllerMap); err != nil { + return nil, fmt.Errorf("failed to parse controller data: %w", err) + } + + return c, nil } -func (c *ScsiControllerSettings) createSyntheticDriveInternal(slot uint, settings driveAssociation, resourceType string) error { - var service *wmiext.Service - var err error - if service, err = NewLocalHyperVService(); err != nil { - return err +// parseControllerData maps the JSON data from Get-VMScsiController to the struct fields +func (c *ScsiControllerSettings) parseControllerData(data map[string]interface{}) error { + // Parse controller-level fields + if val, ok := data["ControllerNumber"].(float64); ok { + c.ControllerNumber = int(val) } - defer service.Close() - if err = populateDefaults(resourceType, settings); err != nil { - return err + if val, ok := data["IsTemplate"].(bool); ok { + c.IsTemplate = val } - settings.setParent(c.Path()) - settings.setAddressOnParent(fmt.Sprintf("%d", slot)) + if val, ok := data["Name"].(string); ok { + c.Name = val + } - driveResource, err := createResourceSettingGeneric(settings, resourceType) - if err != nil { - return err + if val, ok := data["Id"].(string); ok { + c.Id = val } - path, err := addResource(service, c.systemSettings.Path(), driveResource) - if err != nil { - return err + if val, ok := data["VMId"].(string); ok { + c.VMId = val + } + + if val, ok := data["VMName"].(string); ok { + c.VMName = val + } + + if val, ok := data["VMSnapshotId"].(string); ok { + c.VMSnapshotId = val + } + + if val, ok := data["VMSnapshotName"].(string); ok { + c.VMSnapshotName = val } - err = service.GetObjectAsObject(path, settings) - return err + if val, ok := data["ComputerName"].(string); ok { + c.ComputerName = val + } + + if val, ok := data["IsDeleted"].(bool); ok { + c.IsDeleted = val + } + + if val, ok := data["VMCheckpointId"].(string); ok { + c.VMCheckpointId = val + } + + if val, ok := data["VMCheckpointName"].(string); ok { + c.VMCheckpointName = val + } + + // Parse drives array + if drivesData, ok := data["Drives"].([]interface{}); ok { + c.Drives = make([]DriveInfo, 0, len(drivesData)) + for _, driveInterface := range drivesData { + if driveMap, ok := driveInterface.(map[string]interface{}); ok { + drive := DriveInfo{} + + if val, ok := driveMap["Path"].(string); ok { + drive.Path = val + } + + // DiskNumber can be null + if val, ok := driveMap["DiskNumber"].(float64); ok { + diskNum := int(val) + drive.DiskNumber = &diskNum + } + + if val, ok := driveMap["MaximumIOPS"].(float64); ok { + drive.MaximumIOPS = uint64(val) + } + + if val, ok := driveMap["MinimumIOPS"].(float64); ok { + drive.MinimumIOPS = uint64(val) + } + + if val, ok := driveMap["QoSPolicyID"].(string); ok { + drive.QoSPolicyID = val + } + + if val, ok := driveMap["SupportPersistentReservations"].(bool); ok { + drive.SupportPersistentReservations = val + } + + if val, ok := driveMap["WriteHardeningMethod"].(float64); ok { + drive.WriteHardeningMethod = int(val) + } + + if val, ok := driveMap["ControllerLocation"].(float64); ok { + drive.ControllerLocation = int(val) + } + + if val, ok := driveMap["ControllerNumber"].(float64); ok { + drive.ControllerNumber = int(val) + } + + if val, ok := driveMap["ControllerType"].(float64); ok { + drive.ControllerType = int(val) + } + + if val, ok := driveMap["Name"].(string); ok { + drive.Name = val + } + + if val, ok := driveMap["PoolName"].(string); ok { + drive.PoolName = val + } + + if val, ok := driveMap["Id"].(string); ok { + drive.Id = val + } + + if val, ok := driveMap["VMId"].(string); ok { + drive.VMId = val + } + + if val, ok := driveMap["VMName"].(string); ok { + drive.VMName = val + } + + if val, ok := driveMap["VMSnapshotId"].(string); ok { + drive.VMSnapshotId = val + } + + if val, ok := driveMap["VMSnapshotName"].(string); ok { + drive.VMSnapshotName = val + } + + if val, ok := driveMap["ComputerName"].(string); ok { + drive.ComputerName = val + } + + if val, ok := driveMap["IsDeleted"].(bool); ok { + drive.IsDeleted = val + } + + // DvdMediaType is only present for DVD drives + if val, ok := driveMap["DvdMediaType"].(float64); ok { + mediaType := int(val) + drive.DvdMediaType = &mediaType + } + + c.Drives = append(c.Drives, drive) + } + } + } + + return nil +} + +func (c *ScsiControllerSettings) AddSyntheticDiskDrive(slot uint) (*SyntheticDiskDriveSettings, error) { + drive := &SyntheticDiskDriveSettings{} + drive.DiskNumber = int(slot) + drive.VMName = c.VMName + + drive.controllerSettings = c + return drive, nil +} + +func (c *ScsiControllerSettings) AddSyntheticDvdDrive(slot uint) (*SyntheticDvdDriveSettings, error) { + drive := &SyntheticDvdDriveSettings{} + drive.VMName = c.VMName + drive.ControllerLocation = int(slot) + drive.ControllerNumber = c.ControllerNumber + + drive.controllerSettings = c + return drive, nil } diff --git a/pkg/hypervctl/storage_alloc_settings.go b/pkg/hypervctl/storage_alloc_settings.go deleted file mode 100644 index e6baa54..0000000 --- a/pkg/hypervctl/storage_alloc_settings.go +++ /dev/null @@ -1,61 +0,0 @@ -//go:build windows -// +build windows - -package hypervctl - -type StorageAllocationSettings struct { - S__PATH string - InstanceID string - Caption string // = "Hard Disk Image Default Settings" - Description string // = "Describes the default settings for the hard disk image resources" - ElementName string - ResourceType uint16 - OtherResourceType string - ResourceSubType string - PoolID string - ConsumerVisibility uint16 - HostResource []string - AllocationUnits string - VirtualQuantity uint64 - Limit uint64 // = 1 - Weight uint32 - StorageQoSPolicyID string - AutomaticAllocation bool - AutomaticDeallocation bool - Parent string - Connection []string - Address string - MappingBehavior uint16 - AddressOnParent string - VirtualResourceBlockSize uint64 - VirtualQuantityUnits string // = "count(fixed size block)" - Access uint16 - HostResourceBlockSize uint64 - Reservation uint64 - HostExtentStartingAddress uint64 - HostExtentName string - HostExtentNameFormat uint16 - OtherHostExtentNameFormat string - HostExtentNameNamespace uint16 - OtherHostExtentNameNamespace string - IOPSLimit uint64 - IOPSReservation uint64 - IOPSAllocationUnits string - PersistentReservationsSupported bool - CachingMode uint16 - SnapshotId string // = "" - IgnoreFlushes bool - WriteHardeningMethod uint16 -} - -func (s *StorageAllocationSettings) setParent(parent string) { - s.Parent = parent -} - -func (s *StorageAllocationSettings) setHostResource(resource []string) { - s.HostResource = resource -} - -func (s *StorageAllocationSettings) Path() string { - return s.S__PATH -} diff --git a/pkg/hypervctl/summary.go b/pkg/hypervctl/summary.go index c8abcef..24834cb 100644 --- a/pkg/hypervctl/summary.go +++ b/pkg/hypervctl/summary.go @@ -1,202 +1,389 @@ -//go:build windows -// +build windows - -package hypervctl - -import "time" - -const ( - SummaryRequestName = 0 - SummaryRequestElementName = 1 - SummaryRequestCreationTime = 2 - SummaryRequestNotes = 3 - SummaryRequestProcessors = 4 - SummaryRequestSmallThumbnail = 5 - SummaryRequestMediumThumbnail = 6 - SummaryRequestLargeThumbnail = 7 - SummaryRequestAllocatedGPU = 8 - SummaryRequestVirtualSwitchNames = 9 - SummaryRequestVersion = 10 - SummaryRequestShielded = 11 - SummaryRequestEnabledState = 100 - SummaryRequestProcessorLoad = 101 - SummaryRequestProcessorLoadHistory = 102 - SummaryRequestMemoryUsage = 103 - SummaryRequestHeartbeat = 104 - SummaryRequestUptime = 105 - SummaryRequestGuestOperatingSystem = 106 - SummaryRequestSnapshots = 107 - SummaryRequestAsynchronousTasks = 108 - SummaryRequestHealthState = 109 - SummaryRequestOperationalStatus = 110 - SummaryRequestStatusDescriptions = 111 - SummaryRequestMemoryAvailable = 112 - SummaryRequestMemoryBuffer = 113 - SummaryRequestReplicationMode = 114 - SummaryRequestReplicationState = 115 - SummaryRequestReplicationHealth = 116 - SummaryRequestApplicationHealth = 117 - SummaryRequestReplicationStateEx = 118 - SummaryRequestReplicationHealthEx = 119 - SummaryRequestSwapFilesInUse = 120 - SummaryRequestIntegrationServicesVersionState = 121 - SummaryRequestReplicationProvider = 122 - SummaryRequestMemorySpansPhysicalNumaNodes = 123 - SummaryRequestIntegrationServicesVersionState2 = 132 - SummaryRequestOtherEnabledState = 132 -) - -type SummaryRequestSet []uint - -var ( - - // SummaryRequestCommon includes a smaller subset of commonly used fields - SummaryRequestCommon = SummaryRequestSet{ - SummaryRequestName, - SummaryRequestElementName, - SummaryRequestCreationTime, - SummaryRequestNotes, - SummaryRequestProcessors, - SummaryRequestEnabledState, - SummaryRequestProcessorLoad, - SummaryRequestMemoryUsage, - SummaryRequestHeartbeat, - SummaryRequestUptime, - SummaryRequestGuestOperatingSystem, - SummaryRequestHealthState, - SummaryRequestOperationalStatus, - SummaryRequestStatusDescriptions, - SummaryRequestMemoryAvailable, - SummaryRequestMemoryBuffer, - SummaryRequestSwapFilesInUse, - } - - // SummaryRequestNearAll includes everything but load history and thumbnails - SummaryRequestNearAll = SummaryRequestSet{ - SummaryRequestName, - SummaryRequestElementName, - SummaryRequestCreationTime, - SummaryRequestNotes, - SummaryRequestProcessors, - SummaryRequestAllocatedGPU, - SummaryRequestVirtualSwitchNames, - SummaryRequestVersion, - SummaryRequestShielded, - SummaryRequestEnabledState, - SummaryRequestProcessorLoad, - SummaryRequestMemoryUsage, - SummaryRequestHeartbeat, - SummaryRequestUptime, - SummaryRequestGuestOperatingSystem, - SummaryRequestSnapshots, - SummaryRequestAsynchronousTasks, - SummaryRequestHealthState, - SummaryRequestOperationalStatus, - SummaryRequestStatusDescriptions, - SummaryRequestMemoryAvailable, - SummaryRequestMemoryBuffer, - SummaryRequestReplicationMode, - SummaryRequestReplicationState, - SummaryRequestReplicationHealth, - SummaryRequestApplicationHealth, - SummaryRequestReplicationStateEx, - SummaryRequestReplicationHealthEx, - SummaryRequestSwapFilesInUse, - SummaryRequestIntegrationServicesVersionState, - SummaryRequestReplicationProvider, - SummaryRequestMemorySpansPhysicalNumaNodes, - SummaryRequestIntegrationServicesVersionState2, - SummaryRequestOtherEnabledState, - } -) - -// SummaryInformation https://learn.microsoft.com/en-us/windows/win32/hyperv_v2/msvm-summaryinformation -type SummaryInformation struct { - InstanceID string - AllocatedGPU string - Shielded bool - AsynchronousTasks []ConcreteJob - CreationTime time.Time - ElementName string - EnabledState uint16 - OtherEnabledState string - GuestOperatingSystem string - HealthState uint16 - Heartbeat uint16 - MemoryUsage uint64 - MemoryAvailable int32 - AvailableMemoryBuffer int32 - SwapFilesInUse bool - Name string - Notes string - Version string - NumberOfProcessors uint16 - OperationalStatus []uint16 - ProcessorLoad uint16 - ProcessorLoadHistory []uint16 - Snapshots []SystemSettings - StatusDescriptions []string - ThumbnailImage []uint8 - ThumbnailImageHeight uint16 - ThumbnailImageWidth uint16 - UpTime uint64 - ReplicationState uint16 - ReplicationStateEx []uint16 - ReplicationHealth uint16 - ReplicationHealthEx []uint16 - ReplicationMode uint16 - TestReplicaSystem string // REF to CIM_ComputerSystem - ApplicationHealth uint16 - IntegrationServicesVersionState uint16 - MemorySpansPhysicalNumaNodes bool - ReplicationProviderId []string - EnhancedSessionModeState uint16 - VirtualSwitchNames []string - VirtualSystemSubType string - HostComputerSystemName string -} - -// CIM_ConcreteJob https://learn.microsoft.com/en-us/windows/win32/hyperv_v2/msvm-concretejob -type ConcreteJob struct { - InstanceID string - Caption string - Description string - ElementName string - InstallDate time.Time - Name string - OperationalStatus []uint16 // = { 2 } - StatusDescriptions []string // = { "OK" } - Status string - HealthState uint16 // = 5 - CommunicationStatus uint16 - DetailedStatus uint16 - OperatingStatus uint16 - PrimaryStatus uint16 - JobStatus string - TimeSubmitted time.Time - ScheduledStartTime time.Time - StartTime time.Time - ElapsedTime time.Duration - JobRunTimes uint32 - RunMonth uint8 - RunDay int8 - RunDayOfWeek int8 - RunStartInterval time.Time - LocalOrUtcTime uint16 - UntilTime time.Time - Notify string - Owner string - Priority uint32 - PercentComplete uint16 - DeleteOnCompletion bool - ErrorCode uint16 - ErrorDescription string - ErrorSummaryDescription string - RecoveryAction uint16 - OtherRecoveryAction string - JobState uint16 - TimeOfLastStateChange time.Time - TimeBeforeRemoval time.Duration // = - Cancellable bool - JobType uint16 -} +//go:build windows + +package hypervctl + +import ( + "encoding/json" + "fmt" + "regexp" + "strconv" + "strings" + "time" + + "github.com/containers/libhvee/pkg/powershell" +) + +// PowerShellVM represents the structure returned by Get-VM | ConvertTo-Json +type PowerShellVM struct { + ConfigurationLocation string `json:"ConfigurationLocation"` + GuestStatePath string `json:"GuestStatePath"` + SmartPagingFileInUse bool `json:"SmartPagingFileInUse"` + SmartPagingFilePath string `json:"SmartPagingFilePath"` + SnapshotFileLocation string `json:"SnapshotFileLocation"` + AutomaticStartAction int `json:"AutomaticStartAction"` + AutomaticStartDelay int `json:"AutomaticStartDelay"` + AutomaticStopAction int `json:"AutomaticStopAction"` + AutomaticCriticalErrorAction int `json:"AutomaticCriticalErrorAction"` + AutomaticCriticalErrorActionTimeout int `json:"AutomaticCriticalErrorActionTimeout"` + AutomaticCheckpointsEnabled bool `json:"AutomaticCheckpointsEnabled"` + CPUUsage int `json:"CPUUsage"` + MemoryAssigned int64 `json:"MemoryAssigned"` + MemoryDemand int64 `json:"MemoryDemand"` + MemoryStatus string `json:"MemoryStatus"` + NumaAligned *bool `json:"NumaAligned"` + NumaNodesCount int `json:"NumaNodesCount"` + NumaSocketCount int `json:"NumaSocketCount"` + Heartbeat *int `json:"Heartbeat"` + IntegrationServicesState string `json:"IntegrationServicesState"` + IntegrationServicesVersion PowerShellVersion `json:"IntegrationServicesVersion"` + Uptime PowerShellTimeSpan `json:"Uptime"` + OperationalStatus []int `json:"OperationalStatus"` + PrimaryOperationalStatus int `json:"PrimaryOperationalStatus"` + SecondaryOperationalStatus *int `json:"SecondaryOperationalStatus"` + StatusDescriptions []string `json:"StatusDescriptions"` + PrimaryStatusDescription string `json:"PrimaryStatusDescription"` + SecondaryStatusDescription *string `json:"SecondaryStatusDescription"` + Status string `json:"Status"` + ReplicationHealth int `json:"ReplicationHealth"` + ReplicationMode int `json:"ReplicationMode"` + ReplicationState int `json:"ReplicationState"` + ResourceMeteringEnabled bool `json:"ResourceMeteringEnabled"` + CheckpointType int `json:"CheckpointType"` + EnhancedSessionTransportType int `json:"EnhancedSessionTransportType"` + Groups []interface{} `json:"Groups"` + Version string `json:"Version"` + VirtualMachineType int `json:"VirtualMachineType"` + VirtualMachineSubType int `json:"VirtualMachineSubType"` + GuestStateIsolationType int `json:"GuestStateIsolationType"` + Notes string `json:"Notes"` + State EnabledState `json:"State"` + ComPorts []*PowerShellComPort `json:"ComPorts,omitempty"` + ComPort1 *PowerShellComPort `json:"ComPort1,omitempty"` + ComPort2 *PowerShellComPort `json:"ComPort2,omitempty"` + DVDDrives []*PowerShellDvdDrive `json:"DVDDrives"` + FibreChannelHostBusAdapters []interface{} `json:"FibreChannelHostBusAdapters"` + FloppyDrive *PowerShellFloppyDrive `json:"FloppyDrive,omitempty"` + HardDrives []*HardDiskDrive `json:"HardDrives"` + RemoteFxAdapter *interface{} `json:"RemoteFxAdapter,omitempty"` + DynamicMemoryEnabled bool `json:"DynamicMemoryEnabled"` + MemoryMaximum int64 `json:"MemoryMaximum"` + MemoryMinimum int64 `json:"MemoryMinimum"` + MemoryStartup int64 `json:"MemoryStartup"` + ProcessorCount int `json:"ProcessorCount"` + BatteryPassthroughEnabled bool `json:"BatteryPassthroughEnabled"` + Generation int `json:"Generation"` + IsClustered bool `json:"IsClustered"` + ParentSnapshotId *string `json:"ParentSnapshotId"` + ParentSnapshotName *string `json:"ParentSnapshotName"` + Path string `json:"Path"` + SizeOfSystemFiles int64 `json:"SizeOfSystemFiles"` + GuestControlledCacheTypes bool `json:"GuestControlledCacheTypes"` + LowMemoryMappedIoSpace int64 `json:"LowMemoryMappedIoSpace"` + HighMemoryMappedIoSpace int64 `json:"HighMemoryMappedIoSpace"` + HighMemoryMappedIoBaseAddress int64 `json:"HighMemoryMappedIoBaseAddress"` + LockOnDisconnect int `json:"LockOnDisconnect"` + CreationTime string `json:"CreationTime"` // .NET DateTime format like "/Date(1756216458313)/" + Id string `json:"Id"` + Name string `json:"Name"` + NetworkAdapters []*PowerShellNetworkAdapter `json:"NetworkAdapters,omitempty"` + ComputerName string `json:"ComputerName"` + IsDeleted bool `json:"IsDeleted"` + ParentCheckpointId *string `json:"ParentCheckpointId"` + ParentCheckpointName *string `json:"ParentCheckpointName"` + VMName string `json:"VMName"` + VMId string `json:"VMId"` + CheckpointFileLocation string `json:"CheckpointFileLocation"` +} + +type PowerShellVersion struct { + Major int `json:"Major"` + Minor int `json:"Minor"` + Build int `json:"Build"` + Revision int `json:"Revision"` + MajorRevision int `json:"MajorRevision"` + MinorRevision int `json:"MinorRevision"` +} + +type PowerShellTimeSpan struct { + Ticks int64 `json:"Ticks"` + Days int `json:"Days"` + Hours int `json:"Hours"` + Milliseconds int `json:"Milliseconds"` + Minutes int `json:"Minutes"` + Seconds int `json:"Seconds"` + TotalDays float64 `json:"TotalDays"` + TotalHours float64 `json:"TotalHours"` + TotalMilliseconds float64 `json:"TotalMilliseconds"` + TotalMinutes float64 `json:"TotalMinutes"` + TotalSeconds float64 `json:"TotalSeconds"` +} + +type PowerShellComPort struct { + Path string `json:"Path"` + DebuggerMode int `json:"DebuggerMode"` + Name string `json:"Name"` + Id string `json:"Id"` + VMId string `json:"VMId"` + VMName string `json:"VMName"` + VMSnapshotId string `json:"VMSnapshotId"` + VMSnapshotName string `json:"VMSnapshotName"` + ComputerName string `json:"ComputerName"` + IsDeleted bool `json:"IsDeleted"` +} + +type PowerShellFloppyDrive struct { + Path *string `json:"Path"` + PoolName *string `json:"PoolName"` + Name string `json:"Name"` + Id string `json:"Id"` + VMId string `json:"VMId"` + VMName string `json:"VMName"` + VMSnapshotId string `json:"VMSnapshotId"` + VMSnapshotName string `json:"VMSnapshotName"` + ComputerName string `json:"ComputerName"` + IsDeleted bool `json:"IsDeleted"` +} + +type HardDiskDrive struct { + Path string `json:"Path"` + DiskNumber *int `json:"DiskNumber"` + MaximumIOPS int `json:"MaximumIOPS"` + MinimumIOPS int `json:"MinimumIOPS"` + QoSPolicyID string `json:"QoSPolicyID"` + SupportPersistentReservations bool `json:"SupportPersistentReservations"` + WriteHardeningMethod int `json:"WriteHardeningMethod"` + ControllerLocation int `json:"ControllerLocation"` + ControllerNumber int `json:"ControllerNumber"` + ControllerType int `json:"ControllerType"` + Name string `json:"Name"` + PoolName string `json:"PoolName"` + Id string `json:"Id"` + VMId string `json:"VMId"` + VMName string `json:"VMName"` + VMSnapshotId string `json:"VMSnapshotId"` + VMSnapshotName string `json:"VMSnapshotName"` + ComputerName string `json:"ComputerName"` + IsDeleted bool `json:"IsDeleted"` +} + +type PowerShellDvdDrive struct { + DvdMediaType int `json:"DvdMediaType"` + Path string `json:"Path"` + ControllerLocation int `json:"ControllerLocation"` + ControllerNumber int `json:"ControllerNumber"` + ControllerType int `json:"ControllerType"` + Name string `json:"Name"` + PoolName string `json:"PoolName"` + Id string `json:"Id"` + VMId string `json:"VMId"` + VMName string `json:"VMName"` + VMSnapshotId string `json:"VMSnapshotId"` + VMSnapshotName string `json:"VMSnapshotName"` + ComputerName string `json:"ComputerName"` + IsDeleted bool `json:"IsDeleted"` +} + +type PowerShellNetworkAdapter struct { + ClusterMonitored bool `json:"ClusterMonitored"` + MacAddress string `json:"MacAddress"` + MediaType int `json:"MediaType"` + DynamicMacAddressEnabled bool `json:"DynamicMacAddressEnabled"` + InterruptModeration bool `json:"InterruptModeration"` + AllowPacketDirect bool `json:"AllowPacketDirect"` + VirtualSystemIdentifiers StringOrArray `json:"VirtualSystemIdentifiers"` + NumaAwarePlacement bool `json:"NumaAwarePlacement"` + IsLegacy bool `json:"IsLegacy"` + IsSynthetic bool `json:"IsSynthetic"` + IPAddresses StringOrArray `json:"IPAddresses"` + DeviceNaming int `json:"DeviceNaming"` + IovWeight int `json:"IovWeight"` + IovQueuePairsRequested int `json:"IovQueuePairsRequested"` + IovInterruptModeration int `json:"IovInterruptModeration"` + PacketDirectNumProcs int `json:"PacketDirectNumProcs"` + PacketDirectModerationCount int `json:"PacketDirectModerationCount"` + PacketDirectModerationInterval int `json:"PacketDirectModerationInterval"` + IovQueuePairsAssigned int `json:"IovQueuePairsAssigned"` + IovUsage int `json:"IovUsage"` + VirtualFunction *interface{} `json:"VirtualFunction"` + MandatoryFeatureId StringOrArray `json:"MandatoryFeatureId"` + MandatoryFeatureName StringOrArray `json:"MandatoryFeatureName"` + PoolName string `json:"PoolName"` + Connected bool `json:"Connected"` + SwitchName string `json:"SwitchName"` + AdapterId string `json:"AdapterId"` + TestReplicaPoolName string `json:"TestReplicaPoolName"` + TestReplicaSwitchName string `json:"TestReplicaSwitchName"` + StatusDescription string `json:"StatusDescription"` + Status string `json:"Status"` + IsManagementOs bool `json:"IsManagementOs"` + IsExternalAdapter bool `json:"IsExternalAdapter"` + Id string `json:"Id"` + SwitchId string `json:"SwitchId"` + AclList StringOrArray `json:"AclList"` + ExtendedAclList StringOrArray `json:"ExtendedAclList"` + IsolationSetting *interface{} `json:"IsolationSetting"` + RoutingDomainList StringOrArray `json:"RoutingDomainList"` + VlanSetting *interface{} `json:"VlanSetting"` + BandwidthSetting *interface{} `json:"BandwidthSetting"` + CurrentIsolationMode int `json:"CurrentIsolationMode"` + MacAddressSpoofing int `json:"MacAddressSpoofing"` + DhcpGuard int `json:"DhcpGuard"` + RouterGuard int `json:"RouterGuard"` + PortMirroringMode int `json:"PortMirroringMode"` + IeeePriorityTag int `json:"IeeePriorityTag"` + VirtualSubnetId int `json:"VirtualSubnetId"` + DynamicIPAddressLimit int `json:"DynamicIPAddressLimit"` + StormLimit int `json:"StormLimit"` + AllowTeaming int `json:"AllowTeaming"` + FixSpeed10G int `json:"FixSpeed10G"` + VMQWeight int `json:"VMQWeight"` + IPsecOffloadMaxSA int `json:"IPsecOffloadMaxSA"` + VrssEnabled bool `json:"VrssEnabled"` + VrssEnabledRequested bool `json:"VrssEnabledRequested"` + VmmqEnabled bool `json:"VmmqEnabled"` + VmmqEnabledRequested bool `json:"VmmqEnabledRequested"` + VrssMaxQueuePairs int `json:"VrssMaxQueuePairs"` + VrssMaxQueuePairsRequested int `json:"VrssMaxQueuePairsRequested"` + VrssMinQueuePairs int `json:"VrssMinQueuePairs"` + VrssMinQueuePairsRequested int `json:"VrssMinQueuePairsRequested"` + VrssQueueSchedulingMode int `json:"VrssQueueSchedulingMode"` + VrssQueueSchedulingModeRequested int `json:"VrssQueueSchedulingModeRequested"` + VrssExcludePrimaryProcessor bool `json:"VrssExcludePrimaryProcessor"` + VrssExcludePrimaryProcessorRequested bool `json:"VrssExcludePrimaryProcessorRequested"` + VrssIndependentHostSpreading bool `json:"VrssIndependentHostSpreading"` + VrssIndependentHostSpreadingRequested bool `json:"VrssIndependentHostSpreadingRequested"` + VrssVmbusChannelAffinityPolicy int `json:"VrssVmbusChannelAffinityPolicy"` + VrssVmbusChannelAffinityPolicyRequested int `json:"VrssVmbusChannelAffinityPolicyRequested"` + RscEnabled bool `json:"RscEnabled"` + RscEnabledRequested bool `json:"RscEnabledRequested"` + VmqUsage int `json:"VmqUsage"` + IPsecOffloadSAUsage int `json:"IPsecOffloadSAUsage"` + VFDataPathActive bool `json:"VFDataPathActive"` + VMQueue *interface{} `json:"VMQueue"` + BandwidthPercentage int `json:"BandwidthPercentage"` + IsTemplate bool `json:"IsTemplate"` + Name string `json:"Name"` + VMId string `json:"VMId"` + VMName string `json:"VMName"` + VMSnapshotId string `json:"VMSnapshotId"` + VMSnapshotName string `json:"VMSnapshotName"` + ComputerName string `json:"ComputerName"` + IsDeleted bool `json:"IsDeleted"` +} + +type PowerShellIntegrationService struct { + Enabled bool `json:"Enabled"` + OperationalStatus []int `json:"OperationalStatus,omitempty"` + PrimaryOperationalStatus int `json:"PrimaryOperationalStatus"` + PrimaryStatusDescription string `json:"PrimaryStatusDescription"` + SecondaryOperationalStatus int `json:"SecondaryOperationalStatus"` + SecondaryStatusDescription string `json:"SecondaryStatusDescription"` + StatusDescription []string `json:"StatusDescription,omitempty"` + Name string `json:"Name"` + Id string `json:"Id"` + VMId string `json:"VMId"` + VMName string `json:"VMName"` + VMSnapshotId string `json:"VMSnapshotId"` + VMSnapshotName string `json:"VMSnapshotName"` + ComputerName string `json:"ComputerName"` + IsDeleted bool `json:"IsDeleted"` +} + +type PowerShellVMIntegrationService struct { + Enabled bool `json:"Enabled"` + OperationalStatus StringOrArray `json:"OperationalStatus,omitempty"` + PrimaryOperationalStatus int `json:"PrimaryOperationalStatus"` + PrimaryStatusDescription string `json:"PrimaryStatusDescription"` + SecondaryOperationalStatus int `json:"SecondaryOperationalStatus"` + SecondaryStatusDescription string `json:"SecondaryStatusDescription"` + StatusDescription StringOrArray `json:"StatusDescription,omitempty"` + Name string `json:"Name"` + Id string `json:"Id"` + VMId string `json:"VMId"` + VMName string `json:"VMName"` + VMSnapshotId string `json:"VMSnapshotId"` + VMSnapshotName string `json:"VMSnapshotName"` + ComputerName string `json:"ComputerName"` + IsDeleted bool `json:"IsDeleted"` +} + +type PowerShellCimSession struct { + ComputerName *string `json:"ComputerName"` + InstanceId string `json:"InstanceId"` +} + +// parseDotNetDateTime parses .NET DateTime format like "/Date(1756216458313)/" +func parseDotNetDateTime(dateStr string) (time.Time, error) { + // Regular expression to match /Date(timestamp)/ + re := regexp.MustCompile(`^/Date\((\d+)\)/$`) + matches := re.FindStringSubmatch(dateStr) + if len(matches) != 2 { + return time.Time{}, nil // Return zero time for invalid format + } + + timestamp, err := strconv.ParseInt(matches[1], 10, 64) + if err != nil { + return time.Time{}, err + } + + // .NET DateTime ticks are in milliseconds since Unix epoch + return time.Unix(timestamp/1000, (timestamp%1000)*1000000), nil +} + +// GetVMsFromPowerShell executes Get-VM | ConvertTo-Json and parses the result +func GetVMsFromPowerShell() ([]PowerShellVM, error) { + stdout, stderr, err := powershell.Execute("Get-VM | ConvertTo-Json -Depth 5 -Compress") + if err != nil { + return nil, NewPSError(stderr) + } + + // Remove any BOM or extra whitespace + stdout = strings.TrimSpace(stdout) + + // Handle single object response + if !strings.HasPrefix(stdout, "[") { + stdout = "[" + stdout + "]" + } + + var vms []PowerShellVM + if err := json.Unmarshal([]byte(stdout), &vms); err != nil { + return nil, fmt.Errorf("failed to parse JSON output: %w", err) + } + + return vms, nil +} + +// GetVMFromPowerShell executes Get-VM -Name | ConvertTo-Json and parses the result +func GetVMFromPowerShell(name string) (*PowerShellVM, error) { + stdout, stderr, err := powershell.Execute("Get-VM", "-Name", name, "|", "ConvertTo-Json", "-Depth", "5", "-Compress") + if err != nil { + return nil, NewPSError(stderr) + } + + // Remove any BOM or extra whitespace + stdout = strings.TrimSpace(stdout) + + var vm PowerShellVM + if err := json.Unmarshal([]byte(stdout), &vm); err != nil { + return nil, fmt.Errorf("failed to parse JSON output: %w", err) + } + + return &vm, nil +} + +// ConvertToVirtualMachine converts a PowerShellVM to a VirtualMachine instance +func (psvm *PowerShellVM) ConvertToVirtualMachine(vmm *VirtualMachineManager) *VirtualMachine { + + return &VirtualMachine{ + PowerShellVM: *psvm, + vmm: vmm, + } +} + +func (psvm *PowerShellVM) ConvertToCreationTime() time.Time { + creationTime, err := parseDotNetDateTime(psvm.CreationTime) + if err != nil { + return time.Time{} + } + return creationTime +} diff --git a/pkg/hypervctl/system_settings.go b/pkg/hypervctl/system_settings.go index cfb4207..291c9ec 100644 --- a/pkg/hypervctl/system_settings.go +++ b/pkg/hypervctl/system_settings.go @@ -1,207 +1,422 @@ //go:build windows -// +build windows package hypervctl import ( + "encoding/json" "fmt" - "time" - "github.com/containers/libhvee/pkg/wmiext" + "github.com/containers/libhvee/pkg/powershell" ) +// AutomaticStartActionType defines the possible actions for a VM when the Hyper-V host starts. +type AutomaticStartActionType string + +const ( + AutomaticStartActionNothing AutomaticStartActionType = "Nothing" + AutomaticStartActionStart AutomaticStartActionType = "Start" + AutomaticStartActionStartIfRunning AutomaticStartActionType = "StartIfRunning" + AutomaticStartActionNone AutomaticStartActionType = "None" +) + +// AutomaticStopActionType defines the possible actions for a VM when the Hyper-V host stops. +type AutomaticStopActionType string + +const ( + AutomaticStopActionShutDown AutomaticStopActionType = "ShutDown" + AutomaticStopActionSave AutomaticStopActionType = "Save" + AutomaticStopActionTurnOff AutomaticStopActionType = "TurnOff" + AutomaticStopActionNone AutomaticStopActionType = "None" +) + +// AutomaticCriticalErrorActionType defines the possible actions for a VM when a critical error occurs. +type AutomaticCriticalErrorActionType string + +const ( + AutomaticCriticalErrorActionPause AutomaticCriticalErrorActionType = "Pause" + AutomaticCriticalErrorActionNone AutomaticCriticalErrorActionType = "None" +) + +// CheckpointType defines the types of checkpoints to use for a virtual machine. +type CheckpointType string + +const ( + CheckpointTypeDisabled CheckpointType = "Disabled" + CheckpointTypeStandard CheckpointType = "Standard" + CheckpointTypeProduction CheckpointType = "Production" + CheckpointTypeProductionOnly CheckpointType = "ProductionOnly" + CheckpointTypeNone CheckpointType = "None" +) + +type NetworkBootProtocol string + +const ( + IPv4 NetworkBootProtocol = "IPv4" + IPv6 NetworkBootProtocol = "IPv6" +) + +// SystemSettings represents the parameters available for the Set-VM PowerShell cmdlet. +// Each field corresponds to a parameter, using pointer types to indicate optionality +// (nil means the parameter is not set, non-nil means it is set). type SystemSettings struct { - S__PATH string - InstanceID string - Caption string // = "Virtual Machine Settings" - Description string - ElementName string - VirtualSystemIdentifier string - VirtualSystemType string - Notes []string - CreationTime time.Time - ConfigurationID string - ConfigurationDataRoot string - ConfigurationFile string - SnapshotDataRoot string - SuspendDataRoot string - SwapFileDataRoot string - LogDataRoot string - AutomaticStartupAction uint16 // non-zero - AutomaticStartupActionDelay time.Duration - AutomaticStartupActionSequenceNumber uint16 - AutomaticShutdownAction uint16 // non-zero - AutomaticRecoveryAction uint16 // non-zero - RecoveryFile string - BIOSGUID string - BIOSSerialNumber string - BaseBoardSerialNumber string - ChassisSerialNumber string - Architecture string - ChassisAssetTag string - BIOSNumLock bool - BootOrder []uint16 - Parent string - UserSnapshotType uint16 // non-zero - IsSaved bool - AdditionalRecoveryInformation string - AllowFullSCSICommandSet bool - DebugChannelId uint32 - DebugPortEnabled uint16 - DebugPort uint32 - Version string - IncrementalBackupEnabled bool - VirtualNumaEnabled bool - AllowReducedFcRedundancy bool // = False - VirtualSystemSubType string - BootSourceOrder []string - PauseAfterBootFailure bool - NetworkBootPreferredProtocol uint16 // non-zero - GuestControlledCacheTypes bool - AutomaticSnapshotsEnabled bool - IsAutomaticSnapshot bool - GuestStateFile string - GuestStateDataRoot string - LockOnDisconnect bool - ParentPackage string - AutomaticCriticalErrorActionTimeout time.Duration - AutomaticCriticalErrorAction uint16 - ConsoleMode uint16 - SecureBootEnabled bool - SecureBootTemplateId string - LowMmioGapSize uint64 - HighMmioGapSize uint64 - EnhancedSessionTransportType uint16 + // Name specifies the name of the virtual machine to configure. This is a mandatory parameter + // for the PowerShell cmdlet, but here it's treated as optional for flexibility in a Go API. + Name string + + // NewVMName specifies a new name for the virtual machine. + NewVMName string + + // ProcessorCount specifies the number of virtual processors for the virtual machine. + ProcessorCount uint64 + + // DynamicMemory indicates whether dynamic memory is enabled for the virtual machine. + DynamicMemory bool + // StaticMemory indicates whether static memory is enabled for the virtual machine. + StaticMemory bool + + // MemoryMinimumBytes specifies the minimum amount of memory, in bytes, that can be allocated to the virtual machine + // when DynamicMemory is enabled. + MemoryMinimumBytes uint64 + // MemoryMaximumBytes specifies the maximum amount of memory, in bytes, that can be allocated to the virtual machine + // when DynamicMemory is enabled. + MemoryMaximumBytes uint64 + // MemoryStartupBytes specifies the amount of memory, in bytes, that is allocated to the virtual machine when it starts. + MemoryStartupBytes uint64 + + // AutomaticStartAction specifies the action to take when the Hyper-V host starts. + AutomaticStartAction AutomaticStartActionType + // AutomaticStartDelay specifies the delay, in seconds, before the virtual machine automatically starts. + AutomaticStartDelay uint32 + + // AutomaticStopAction specifies the action to take when the Hyper-V host stops. + AutomaticStopAction AutomaticStopActionType + + // AutomaticCriticalErrorAction specifies the action to take when a critical error occurs. + AutomaticCriticalErrorAction AutomaticCriticalErrorActionType + // AutomaticCriticalErrorActionTimeout specifies the timeout, in seconds, for the critical error action. + AutomaticCriticalErrorActionTimeout uint32 + + // AutomaticCheckpointsEnabled indicates whether automatic checkpoints are enabled for the virtual machine. + AutomaticCheckpointsEnabled bool + // CheckpointType specifies the type of checkpoints to use for the virtual machine. + CheckpointType CheckpointType + + // SmartPagingFilePath specifies the path to the smart paging file. + SmartPagingFilePath string + // SnapshotFileLocation specifies the location where snapshot files are stored. + SnapshotFileLocation string + + // AllowUnverifiedPaths indicates whether to allow unverified paths for clustered virtual machines. + AllowUnverifiedPaths bool + + // GuestControlledCacheTypes indicates whether the guest operating system can control cache types. + GuestControlledCacheTypes bool + + // HighMemoryMappedIoSpace specifies the amount of high memory mapped I/O space, in bytes. + HighMemoryMappedIoSpace uint64 + // LowMemoryMappedIoSpace specifies the amount of low memory mapped I/O space, in bytes. + LowMemoryMappedIoSpace uint64 + + // LockOnDisconnect indicates whether the virtual machine should be locked on disconnect. + LockOnDisconnect bool + + // Notes specifies a descriptive note for the virtual machine. + Notes string + + // Passthru indicates that the cmdlet should return the virtual machine object after the operation. + // This is a common PowerShell parameter, not directly a VM configuration. + Passthru bool + + NetworkBootPreferredProtocol NetworkBootProtocol } func DefaultSystemSettings() *SystemSettings { return &SystemSettings{ // setup all non-zero settings - AutomaticStartupAction: 2, // no auto-start - AutomaticShutdownAction: 4, // shutdown - AutomaticRecoveryAction: 3, // restart - UserSnapshotType: 2, // no snapshotting - NetworkBootPreferredProtocol: 4096, // ipv4 for pxe - VirtualSystemSubType: "Microsoft:Hyper-V:SubType:2", + AutomaticStartAction: AutomaticStartActionNothing, // no auto-start + AutomaticStopAction: AutomaticStopActionShutDown, // shutdown + AutomaticCriticalErrorAction: AutomaticCriticalErrorActionNone, // restart + CheckpointType: CheckpointTypeDisabled, // no snapshotting + NetworkBootPreferredProtocol: IPv4, // ipv4 for pxe } } -func (s *SystemSettings) Path() string { - return s.S__PATH -} - func (s *SystemSettings) AddScsiController() (*ScsiControllerSettings, error) { - const scsiControllerType = "Microsoft:Hyper-V:Synthetic SCSI Controller" + controller := &ScsiControllerSettings{} - if err := s.createSystemResourceInternal(controller, scsiControllerType, nil); err != nil { - return nil, err + controller.VMName = s.Name + + cli := controller.GenerateAddCommand() + + _, stderr, err := powershell.Execute(cli...) + if err != nil { + return nil, fmt.Errorf("failed to add SCSI controller: %w", NewPSError(stderr)) + } + + updatedController, err := controller.Update() + if err != nil { + return nil, fmt.Errorf("failed to update SCSI controller: %w", err) } - controller.systemSettings = s - return controller, nil + return updatedController, nil } -func (s *SystemSettings) createSystemResourceInternal(settings interface{}, resourceType string, cb func()) error { - var service *wmiext.Service - var err error - if service, err = NewLocalHyperVService(); err != nil { - return err +func (s *SystemSettings) GetVM() (*SystemSettings, error) { + return GetVMFromName(s.Name) +} + +func getCLI(opts *SystemSettings) []string { + if opts.Name == "" { + return []string{} } - defer service.Close() - if err = populateDefaults(resourceType, settings); err != nil { - return err + params := []string{} + + // VM Name is required + params = append(params, fmt.Sprintf("-Name '%s'", opts.Name)) + + // String parameters + if opts.NewVMName != "" { + params = append(params, fmt.Sprintf("-NewVMName '%s'", opts.NewVMName)) + } + if opts.SmartPagingFilePath != "" { + params = append(params, fmt.Sprintf("-SmartPagingFilePath '%s'", opts.SmartPagingFilePath)) + } + if opts.SnapshotFileLocation != "" { + params = append(params, fmt.Sprintf("-SnapshotFileLocation '%s'", opts.SnapshotFileLocation)) + } + if opts.Notes != "" { + params = append(params, fmt.Sprintf("-Notes '%s'", opts.Notes)) } - if cb != nil { - cb() + // Numeric parameters + if opts.ProcessorCount > 0 { + params = append(params, fmt.Sprintf("-ProcessorCount %d", opts.ProcessorCount)) + } + if opts.AutomaticStartDelay > 0 { + params = append(params, fmt.Sprintf("-AutomaticStartDelay %d", opts.AutomaticStartDelay)) + } + if opts.AutomaticCriticalErrorActionTimeout > 0 { + params = append(params, fmt.Sprintf("-AutomaticCriticalErrorActionTimeout %d", opts.AutomaticCriticalErrorActionTimeout)) } - resourceStr, err := createResourceSettingGeneric(settings, resourceType) - if err != nil { - return err + // Memory parameters (in bytes) + if opts.MemoryStartupBytes > 0 { + params = append(params, fmt.Sprintf("-MemoryStartupBytes %d", opts.MemoryStartupBytes)) + } + if opts.MemoryMinimumBytes > 0 { + params = append(params, fmt.Sprintf("-MemoryMinimumBytes %d", opts.MemoryMinimumBytes)) + } + if opts.MemoryMaximumBytes > 0 { + params = append(params, fmt.Sprintf("-MemoryMaximumBytes %d", opts.MemoryMaximumBytes)) + } + if opts.HighMemoryMappedIoSpace > 0 { + params = append(params, fmt.Sprintf("-HighMemoryMappedIoSpace %d", opts.HighMemoryMappedIoSpace)) + } + if opts.LowMemoryMappedIoSpace > 0 { + params = append(params, fmt.Sprintf("-LowMemoryMappedIoSpace %d", opts.LowMemoryMappedIoSpace)) } - path, err := addResource(service, s.Path(), resourceStr) - if err != nil { - return err + // Boolean parameters + if opts.DynamicMemory { + params = append(params, "-DynamicMemory") + } + if opts.StaticMemory { + params = append(params, "-StaticMemory") + } + if opts.AutomaticCheckpointsEnabled { + params = append(params, "-AutomaticCheckpointsEnabled $True") + } + if opts.AllowUnverifiedPaths { + params = append(params, "-AllowUnverifiedPaths") + } + if opts.LockOnDisconnect { + params = append(params, "-LockOnDisconnect On") + } + if opts.Passthru { + params = append(params, "-Passthru") + } + + // Enum parameters + //we dont need to set the None value + if opts.AutomaticStartAction != "" && opts.AutomaticStartAction != AutomaticStartActionNone { + params = append(params, fmt.Sprintf("-AutomaticStartAction %s", opts.AutomaticStartAction)) + } + if opts.AutomaticStopAction != "" && opts.AutomaticStopAction != AutomaticStopActionNone { + params = append(params, fmt.Sprintf("-AutomaticStopAction %s", opts.AutomaticStopAction)) + } + if opts.AutomaticCriticalErrorAction != "" { + params = append(params, fmt.Sprintf("-AutomaticCriticalErrorAction %s", opts.AutomaticCriticalErrorAction)) + } + if opts.CheckpointType != "" && opts.CheckpointType != CheckpointTypeNone { + params = append(params, fmt.Sprintf("-CheckpointType %s", opts.CheckpointType)) + } + if opts.GuestControlledCacheTypes { + params = append(params, "-GuestControlledCacheTypes $True") + } else { + params = append(params, "-GuestControlledCacheTypes $False") } - err = service.GetObjectAsObject(path, settings) - return err + return params } -func (s *SystemSettings) AddSyntheticEthernetPort(beforeAdd func(*SyntheticEthernetPortSettings)) (*SyntheticEthernetPortSettings, error) { - const networkAdapterType = SyntheticEthernetPortResourceType - port := &SyntheticEthernetPortSettings{} +// GetVMFromName executes Get-VM PowerShell command and parses the JSON output into SystemSettings +func GetVMFromName(vmName string) (*SystemSettings, error) { + if vmName == "" { + return nil, fmt.Errorf("VM name cannot be empty") + } + + // Execute PowerShell command to get VM info as JSON + cmd := fmt.Sprintf("Get-VM -Name '%s' | ConvertTo-Json -Depth 5 -Compress", vmName) + stdout, stderr, err := powershell.Execute(cmd) + if err != nil { + return nil, fmt.Errorf("failed to execute PowerShell command: %w, stderr: %s", err, stderr) + } - var cb func() - if beforeAdd != nil { - cb = func() { - beforeAdd(port) + // Parse JSON response + var vmData map[string]interface{} + if err := json.Unmarshal([]byte(stdout), &vmData); err != nil { + // hyperv allow to have multiple VMs with the same name + // so we need to get the first one + var vms []map[string]interface{} + err := json.Unmarshal([]byte(stdout), &vms) + if err != nil { + return nil, fmt.Errorf("failed to parse JSON output: %w", err) } + if len(vms) == 0 { + return nil, fmt.Errorf("no VMs found with name: %s", vmName) + } + vmData = vms[0] } - if err := s.createSystemResourceInternal(port, networkAdapterType, cb); err != nil { - return nil, err + // Create SystemSettings instance and map fields + settings := &SystemSettings{ + Name: vmName, } - port.systemSettings = s - return port, nil -} + // Map PowerShell VM properties to SystemSettings fields + if val, ok := vmData["ProcessorCount"].(float64); ok { + settings.ProcessorCount = uint64(val) + } -func addResource(service *wmiext.Service, systemSettingPath string, resourceSettings string) (string, error) { - vsms, err := service.GetSingletonInstance(VirtualSystemManagementService) - if err != nil { - return "", err + if val, ok := vmData["DynamicMemoryEnabled"].(bool); ok { + settings.DynamicMemory = val + settings.StaticMemory = !val } - defer vsms.Close() - var res int32 - var resultingSettings []string - var job *wmiext.Instance - err = vsms.BeginInvoke("AddResourceSettings"). - In("AffectedConfiguration", systemSettingPath). - In("ResourceSettings", []string{resourceSettings}). - Execute(). - Out("Job", &job). - Out("ResultingResourceSettings", &resultingSettings). - Out("ReturnValue", &res).End() + if val, ok := vmData["MemoryMinimum"].(float64); ok { + settings.MemoryMinimumBytes = uint64(val) + } - if err != nil { - return "", fmt.Errorf("AddResourceSettings failed: %w", err) + if val, ok := vmData["MemoryMaximum"].(float64); ok { + settings.MemoryMaximumBytes = uint64(val) } - err = waitVMResult(res, service, job, "failed to add resource", nil) + if val, ok := vmData["MemoryStartup"].(float64); ok { + settings.MemoryStartupBytes = uint64(val) + } - if len(resultingSettings) > 0 { - return resultingSettings[0], err + if val, ok := vmData["AutomaticStartAction"].(float64); ok { + settings.AutomaticStartAction = cerateAutomaticStartAction(int(val)) } - return "", err -} + if val, ok := vmData["AutomaticStartDelay"].(float64); ok { + settings.AutomaticStartDelay = uint32(val) + } -func (s *SystemSettings) GetVM() (*VirtualMachine, error) { - var service *wmiext.Service - var err error - if service, err = NewLocalHyperVService(); err != nil { - return nil, err + if val, ok := vmData["AutomaticStopAction"].(float64); ok { + settings.AutomaticStopAction = cerateAutomaticStopAction(int(val)) } - defer service.Close() - inst, err := service.FindFirstRelatedInstance(s.Path(), "Msvm_ComputerSystem") - if err != nil { - return nil, err + if val, ok := vmData["AutomaticCriticalErrorAction"].(float64); ok { + settings.AutomaticCriticalErrorAction = cerateAutomaticCriticalErrorAction(int(val)) + } + + if val, ok := vmData["AutomaticCriticalErrorActionTimeout"].(float64); ok { + settings.AutomaticCriticalErrorActionTimeout = uint32(val) + } + + if val, ok := vmData["AutomaticCheckpointsEnabled"].(bool); ok { + settings.AutomaticCheckpointsEnabled = val + } + + if val, ok := vmData["CheckpointType"].(float64); ok { + settings.CheckpointType = cerateCheckpointType(int(val)) + } + + if val, ok := vmData["SmartPagingFilePath"].(string); ok { + settings.SmartPagingFilePath = val + } + + if val, ok := vmData["SnapshotFileLocation"].(string); ok { + settings.SnapshotFileLocation = val + } + + if val, ok := vmData["LockOnDisconnect"].(float64); ok { + settings.LockOnDisconnect = val !=1 } - defer inst.Close() - vm := &VirtualMachine{} + if val, ok := vmData["Notes"].(string); ok { + settings.Notes = val + } - if err = inst.GetAll(vm); err != nil { - return nil, err + if val, ok := vmData["GuestControlledCacheTypes"].(bool); ok { + settings.GuestControlledCacheTypes = val } - return vm, nil + return settings, nil +} + +func cerateAutomaticStartAction(val int) AutomaticStartActionType { + switch val { + case 2: + return AutomaticStartActionNothing + case 3: + return AutomaticStartActionStartIfRunning + case 4: + return AutomaticStartActionStart + default: + return AutomaticStartActionNone + } +} + +func cerateAutomaticStopAction(val int) AutomaticStopActionType { + switch val { + case 2: + return AutomaticStopActionTurnOff + case 3: + return AutomaticStopActionSave + case 4: + return AutomaticStopActionShutDown + default: + return AutomaticStopActionNone + } +} + +func cerateAutomaticCriticalErrorAction(val int) AutomaticCriticalErrorActionType { + switch val { + case 0: + return AutomaticCriticalErrorActionNone + case 1: + return AutomaticCriticalErrorActionPause + default: + return AutomaticCriticalErrorActionNone + } +} + +func cerateCheckpointType(val int) CheckpointType { + switch val { + case 2: + return CheckpointTypeDisabled + case 3: + return CheckpointTypeProduction + case 4: + return CheckpointTypeProductionOnly + case 5: + return CheckpointTypeStandard + default: + return CheckpointTypeNone + } } diff --git a/pkg/hypervctl/system_settings_builder.go b/pkg/hypervctl/system_settings_builder.go index 76b5995..aec38d7 100644 --- a/pkg/hypervctl/system_settings_builder.go +++ b/pkg/hypervctl/system_settings_builder.go @@ -1,12 +1,12 @@ //go:build windows -// +build windows package hypervctl import ( "fmt" + "os" - "github.com/containers/libhvee/pkg/wmiext" + "github.com/containers/libhvee/pkg/powershell" ) type SystemSettingsBuilder struct { @@ -27,7 +27,7 @@ func (builder *SystemSettingsBuilder) PrepareSystemSettings(name string, beforeA if builder.systemSettings == nil { settings := DefaultSystemSettings() - settings.ElementName = name + settings.Name = name builder.systemSettings = settings } @@ -56,6 +56,8 @@ func (builder *SystemSettingsBuilder) PrepareProcessorSettings(beforeAdd func(*P beforeAdd(builder.processorSettings) } + builder.processorSettings.VMName = builder.systemSettings.Name + return builder } @@ -77,85 +79,48 @@ func (builder *SystemSettingsBuilder) PrepareMemorySettings(beforeAdd func(*Memo beforeAdd(builder.memorySettings) } + builder.memorySettings.VMName = builder.systemSettings.Name + return builder } func (builder *SystemSettingsBuilder) Build() (*SystemSettings, error) { - var service *wmiext.Service - var err error - - if builder.PrepareSystemSettings("unnamed-vm", nil). - PrepareProcessorSettings(nil). - PrepareMemorySettings(nil).err != nil { - return nil, err - } - if service, err = NewLocalHyperVService(); err != nil { - return nil, err - } - defer service.Close() - - systemSettingsInst, err := service.SpawnInstance("Msvm_VirtualSystemSettingData") - if err != nil { - return nil, err - } - defer systemSettingsInst.Close() - - err = systemSettingsInst.PutAll(builder.systemSettings) - if err != nil { - return nil, err - } + var err error + // New-VM add network adapter to the vm, so we need to remove it before setting the vm, as some users(podman) don't want to have a network adapter on the vm by default. + _, stderr, err := powershell.Execute("Hyper-V\\New-VM", "-Name", builder.systemSettings.Name, "-Generation", "2", "|", "Get-VMNetworkAdapter", "|", "Remove-VMNetworkAdapter") - memoryStr, err := createMemorySettings(builder.memorySettings) if err != nil { - return nil, err + return nil, NewPSError(stderr) } - processorStr, err := createProcessorSettings(builder.processorSettings) - if err != nil { - return nil, err - } + cliArgs := getCLI(builder.systemSettings) + args := append([]string{"Hyper-V\\Set-VM"}, cliArgs...) + _, stderr, err = powershell.Execute(args...) - vsms, err := service.GetSingletonInstance(VirtualSystemManagementService) if err != nil { - return nil, err + fmt.Fprintf(os.Stderr, "error setting vm: %s\n", err) + return nil, NewPSError(stderr) } - defer vsms.Close() - systemStr := systemSettingsInst.GetCimText() - - var job *wmiext.Instance - var res int32 - var resultingSystem string - err = vsms.BeginInvoke("DefineSystem"). - In("SystemSettings", systemStr). - In("ResourceSettings", []string{memoryStr, processorStr}). - Execute(). - Out("Job", &job). - Out("ResultingSystem", &resultingSystem). - Out("ReturnValue", &res).End() + _, stderr, err = powershell.Execute("Hyper-V\\Set-VMFirmware", "-VMName", builder.systemSettings.Name, "-PreferredNetworkBootProtocol", string(builder.systemSettings.NetworkBootPreferredProtocol), "-EnableSecureBoot", "Off") if err != nil { - return nil, fmt.Errorf("failed to define system: %w", err) + fmt.Fprintf(os.Stderr, "error setting vm firmware: %s\n", err) + return nil, NewPSError(stderr) } - err = waitVMResult(res, service, job, "failed to define system", nil) + err = updateVMMemory(builder.systemSettings.Name, builder.memorySettings) if err != nil { + fmt.Fprintf(os.Stderr, "error setting vm memory: %s\n", err) return nil, err } - newSettings, err := service.FindFirstRelatedInstance(resultingSystem, "Msvm_VirtualSystemSettingData") - if err != nil { - return nil, err - } - path, err := newSettings.Path() + err = updateVMProcessor(builder.systemSettings.Name, builder.processorSettings) if err != nil { + fmt.Fprintf(os.Stderr, "error setting vm processor: %s\n", err) return nil, err } - if err = service.GetObjectAsObject(path, builder.systemSettings); err != nil { - return nil, err - } - - return builder.systemSettings, nil + return GetVMFromName(builder.systemSettings.Name) } diff --git a/pkg/hypervctl/vdvd_storage_settings.go b/pkg/hypervctl/vdvd_storage_settings.go deleted file mode 100644 index ad5ecea..0000000 --- a/pkg/hypervctl/vdvd_storage_settings.go +++ /dev/null @@ -1,13 +0,0 @@ -//go:build windows -// +build windows - -package hypervctl - -const VirtualDvdDiskType = "Microsoft:Hyper-V:Virtual CD/DVD Disk" - -type VirtualDvdDiskStorageSettings struct { - StorageAllocationSettings - - systemSettings *SystemSettings - driveSettings *SyntheticDvdDriveSettings -} diff --git a/pkg/hypervctl/vhd.go b/pkg/hypervctl/vhd.go index 85b7399..4e97a36 100644 --- a/pkg/hypervctl/vhd.go +++ b/pkg/hypervctl/vhd.go @@ -1,12 +1,13 @@ +//go:build windows + package hypervctl import ( - "encoding/xml" "fmt" "strconv" "strings" - "github.com/containers/libhvee/pkg/wmiext" + "github.com/containers/libhvee/pkg/powershell" "go.podman.io/common/pkg/strongunits" ) @@ -14,95 +15,25 @@ import ( // to change its size. There is no error protection for trying to size a disk // smaller than the current size. func ResizeDisk(diskPath string, newSize strongunits.GiB) error { - var ( - service *wmiext.Service - err error - job *wmiext.Instance - ret int32 - ) - - if service, err = NewLocalHyperVService(); err != nil { - return err - } - defer service.Close() - - imms, err := service.GetSingletonInstance("Msvm_ImageManagementService") + cmd := `Resize-VHD -Path ` + diskPath + ` -SizeBytes ` + strconv.FormatInt(int64(newSize.ToBytes()), 10) + _, stderr, err := powershell.Execute(cmd) if err != nil { - return err + return fmt.Errorf("failed to resize disk: %w", NewPSError(stderr)) } - defer imms.Close() - - err = imms.BeginInvoke("ResizeVirtualHardDisk"). - In("Path", diskPath). - In("MaxInternalSize", int64(newSize.ToBytes())). - Execute(). - Out("Job", &job). - Out("ReturnValue", &ret). - End() - - if err != nil { - return fmt.Errorf("failed to resize disk: %w", err) - } - return waitVMResult(ret, service, job, "failed to resize disk", nil) + return nil } func GetDiskSize(diskPath string) (strongunits.B, error) { - var ( - service *wmiext.Service - err error - job *wmiext.Instance - ret int32 - results string - ) - if service, err = NewLocalHyperVService(); err != nil { - return 0, err - } - defer service.Close() - imms, err := service.GetSingletonInstance("Msvm_ImageManagementService") + cmd := `Get-VHD -Path "` + diskPath + `" | Select-Object -ExpandProperty Size` + sizeStr, stderr, err := powershell.Execute(cmd) if err != nil { - return 0, err - } - defer imms.Close() - - inv := imms.BeginInvoke("GetVirtualHardDiskSettingData"). - In("Path", diskPath). - Execute(). - Out("Job", &job). - Out("ReturnValue", &ret) - - if err := inv.Error(); err != nil { - return 0, fmt.Errorf("failed to get setting data for disk %s: %q", diskPath, err) - } - - if err := waitVMResult(ret, service, job, "failure waiting on result from disk settings", nil); err != nil { - return 0, err + return 0, fmt.Errorf("failed to get disk size: %w", NewPSError(stderr)) } - - err = inv.Out("SettingData", &results).End() + sizeStr = strings.TrimSpace(sizeStr) + size, err := strconv.ParseInt(sizeStr, 10, 64) if err != nil { - return 0, fmt.Errorf("failed to retrieve setting object payload for disk: %q", err) - } - - type CimSingleInstance struct { - XMLName xml.Name `xml:"INSTANCE"` - Properties []CimKvpItemProperty `xml:"PROPERTY"` + return 0, fmt.Errorf("failed to parse disk size: %w", err) } - - diskSettings := CimSingleInstance{} - if err := xml.Unmarshal([]byte(results), &diskSettings); err != nil { - return 0, fmt.Errorf("unable to parse disk settings xml: %q", err) - } - - for _, prop := range diskSettings.Properties { - if strings.EqualFold(prop.Name, "MaxInternalSize") { - size, err := strconv.ParseUint(prop.Value, 10, 64) - if err != nil { - return 0, fmt.Errorf("unable to parse size in disk settings") - } - return strongunits.B(size), nil - } - } - - return 0, fmt.Errorf("disk settings was missing a size value") + return strongunits.B(size), nil } diff --git a/pkg/hypervctl/vhd_settings.go b/pkg/hypervctl/vhd_settings.go deleted file mode 100644 index e6bd8b4..0000000 --- a/pkg/hypervctl/vhd_settings.go +++ /dev/null @@ -1,28 +0,0 @@ -//go:build windows -// +build windows - -package hypervctl - -import "time" - -type VirtualHardDiskSettings struct { - S__PATH string - InstanceID string - Caption string // = "Virtual Hard Disk Setting Data" - Description string // = "Setting Data for a Virtual Hard Disk" - ElementName string - Type uint16 - Format uint16 - Path string - ParentPath string - ParentTimestamp time.Time - ParentIdentifier string - MaxInternalSize uint64 - BlockSize uint32 - LogicalSectorSize uint32 - PhysicalSectorSize uint32 - VirtualDiskId string - DataAlignment uint64 - PmemAddressAbstractionType uint16 - IsPmemCompatible bool -} diff --git a/pkg/hypervctl/vhd_storage_settings.go b/pkg/hypervctl/vhd_storage_settings.go deleted file mode 100644 index 0384408..0000000 --- a/pkg/hypervctl/vhd_storage_settings.go +++ /dev/null @@ -1,13 +0,0 @@ -//go:build windows -// +build windows - -package hypervctl - -const VirtualHardDiskType = "Microsoft:Hyper-V:Virtual Hard Disk" - -type VirtualHardDiskStorageSettings struct { - StorageAllocationSettings - - systemSettings *SystemSettings - driveSettings *SyntheticDiskDriveSettings -} diff --git a/pkg/hypervctl/vm.go b/pkg/hypervctl/vm.go index 7946795..13e5fac 100644 --- a/pkg/hypervctl/vm.go +++ b/pkg/hypervctl/vm.go @@ -1,5 +1,4 @@ //go:build windows -// +build windows package hypervctl @@ -12,7 +11,7 @@ import ( "time" "github.com/containers/libhvee/pkg/kvp/ginsu" - "github.com/containers/libhvee/pkg/wmiext" + "github.com/containers/libhvee/pkg/powershell" ) // delete this when close to being done @@ -21,59 +20,14 @@ var ( ) type VirtualMachine struct { - S__PATH string `json:"-"` - S__CLASS string `json:"-"` - InstanceID string - Caption string - Description string - ElementName string - InstallDate time.Time - OperationalStatus []uint16 - StatusDescriptions []string - Status string - HealthState uint16 - CommunicationStatus uint16 - DetailedStatus uint16 - OperatingStatus uint16 - PrimaryStatus uint16 - EnabledState uint16 - OtherEnabledState string - RequestedState uint16 - EnabledDefault uint16 - TimeOfLastStateChange string - AvailableRequestedStates []uint16 - TransitioningToState uint16 - CreationClassName string - Name string - PrimaryOwnerName string - PrimaryOwnerContact string - Roles []string - NameFormat string - OtherIdentifyingInfo []string - IdentifyingDescriptions []string - Dedicated []uint16 - OtherDedicatedDescriptions []string - ResetCapability uint16 - PowerManagementCapabilities []uint16 - OnTimeInMilliseconds uint64 - ProcessID uint32 - TimeOfLastConfigurationChange string - NumberOfNumaNodes uint16 - ReplicationState uint16 - ReplicationHealth uint16 - ReplicationMode uint16 - FailedOverReplicationType uint16 - LastReplicationType uint16 - LastApplicationConsistentReplicationTime string - LastReplicationTime time.Time - LastSuccessfulBackupTime string - EnhancedSessionModeState uint16 - vmm *VirtualMachineManager + PowerShellVM + vmm *VirtualMachineManager } -func (vm *VirtualMachine) Path() string { - return vm.S__PATH +func (vm *VirtualMachine) GetName() string { + return vm.Name } + func (vm *VirtualMachine) SplitAndAddIgnition(keyPrefix string, ignRdr *bytes.Reader) error { parts, err := ginsu.Dice(ignRdr) if err != nil { @@ -89,17 +43,17 @@ func (vm *VirtualMachine) SplitAndAddIgnition(keyPrefix string, ignRdr *bytes.Re } func (vm *VirtualMachine) AddKeyValuePair(key string, value string) error { - return vm.kvpOperation("AddKvpItems", key, value, false, "key already exists?") + return vm.kvpOperation("AddKvpItems", key, value) } func (vm *VirtualMachine) ModifyKeyValuePair(key string, value string) error { - return vm.kvpOperation("ModifyKvpItems", key, value, false, "key invalid?") + return vm.kvpOperation("ModifyKvpItems", key, value) } func (vm *VirtualMachine) PutKeyValuePair(key string, value string) error { err := vm.AddKeyValuePair(key, value) - kvpError, ok := err.(*KvpError) - if !ok || kvpError.ErrorCode != KvpIllegalArgument { + + if err != nil && !strings.Contains(err.Error(), "The parameter is incorrect. (0x80070057)") { return err } @@ -107,122 +61,87 @@ func (vm *VirtualMachine) PutKeyValuePair(key string, value string) error { } func (vm *VirtualMachine) RemoveKeyValuePair(key string) error { - return vm.kvpOperation("RemoveKvpItems", key, "", false, "key invalid?") + return vm.kvpOperation("RemoveKvpItems", key, "") } func (vm *VirtualMachine) RemoveKeyValuePairNoWait(key string) error { - return vm.kvpOperation("RemoveKvpItems", key, "", true, "key invalid?") + return vm.kvpOperation("RemoveKvpItems", key, "") } func (vm *VirtualMachine) GetKeyValuePairs() (map[string]string, error) { - var service *wmiext.Service - var err error - - if service, err = NewLocalHyperVService(); err != nil { - return nil, err - } - - defer service.Close() - - i, err := service.FindFirstRelatedInstance(vm.Path(), "Msvm_KvpExchangeComponent") - if err != nil { - return nil, err - } - - defer i.Close() - - var path string - path, err = i.GetAsString("__PATH") - if err != nil { - return nil, err - - } - - i, err = service.FindFirstRelatedInstance(path, "Msvm_KvpExchangeComponentSettingData") - if err != nil { - return nil, err - } - defer i.Close() - - s, err := i.GetAsString("HostExchangeItems") + script := getAllKvpItemsScript(vm.Name) + _, stderr, err := powershell.ExecuteAsScript(script) if err != nil { - return nil, err + return nil, NewPSError(stderr) } - - return parseKvpMapXml(s) + return parseKvpMapXml(stderr) } -func (vm *VirtualMachine) kvpOperation(op string, key string, value string, nowait bool, illegalSuggestion string) error { - var service *wmiext.Service - var vsms, job *wmiext.Instance - var ret int32 - var err error - - if service, err = NewLocalHyperVService(); err != nil { - return err - } - defer service.Close() - - vsms, err = service.GetSingletonInstance(VirtualSystemManagementService) - if err != nil { - return err - } - defer vsms.Close() - - itemStr, err := createKvpItem(service, key, value) +func (vm *VirtualMachine) kvpOperation(op string, key string, value string) error { + script := getKvpScript(vm.Name, op, key, value) + _, stderr, err := powershell.ExecuteAsScript(script) if err != nil { - return err - } - - execution := vsms.BeginInvoke(op). - In("TargetSystem", vm.Path()). - In("DataItems", []string{itemStr}). - Execute(). - Out("ReturnValue", &ret). - Out("Job", &job) - - if err := execution.End(); err != nil { - return fmt.Errorf("%s execution failed: %w", op, err) + return NewPSError(stderr) } + return nil - defer job.Close() - if ret == 0 || (nowait && ret == 4096) { - return nil - } +} - if ret == 4096 { - err = wmiext.WaitJob(service, job) - } else { - err = &wmiext.JobError{ErrorCode: int(ret)} +func getKvpScript(vmName string, op string, key string, value string) []string { + return []string{ + `Import-Module CimCmdlets`, + `# Connect to the VM`, + `$vmMgmt = Get-WmiObject -Namespace "root\virtualization\v2" -Class Msvm_VirtualSystemManagementService`, + `$vm = Get-WmiObject -Namespace "root\virtualization\v2" -Class Msvm_ComputerSystem -Filter "ElementName='` + vmName + `'"`, + `# Create KVP data item`, + `$kvpClass = [WMIClass]("\\$($vmMgmt.__SERVER)\$($vmMgmt.__NAMESPACE):Msvm_KvpExchangeDataItem")`, + `$kvpDataItem = $kvpClass.CreateInstance()`, + `$kvpDataItem.Name = "` + key + `"`, + `$kvpDataItem.Data = '` + strings.ReplaceAll(value, "'", "''") + `'`, + `$kvpDataItem.Source = 0`, + + `# Add (or modify) the KVP`, + `$result = $vmMgmt.` + op + `($vm, $kvpDataItem.PSBase.GetText(1))`, + + `# Check if a job was started`, + `if ($result.ReturnValue -eq 4096) {`, + `# Extract the InstanceID from the Job path`, + `$instanceId = [regex]::Match($result.Job, 'InstanceID="(.+?)"').Groups[1].Value`, + + `# Construct the query correctly by using single quotes to escape the quotes in InstanceID`, + `$query = "SELECT * FROM Msvm_ConcreteJob WHERE InstanceID = '$instanceId'"`, + + `# Get the job object using the corrected query`, + `$job = Get-WmiObject -Namespace "root\virtualization\v2" -Query $query`, + + `# Wait for the job to complete`, + `do {`, + `Write-Host "Waiting for the KVP item to be ` + op + `..."`, + `Start-Sleep -Seconds 1`, + + `# Refresh the job object to get its latest state`, + `$job = Get-WmiObject -Namespace "root\virtualization\v2" -Query $query`, + `} until ($job.JobState -ne 4)`, + + `# Check the final status of the job`, + `if ($job.JobState -eq 7) {`, + `Write-Host "Successfully ` + op + ` the KVP item."`, + `} else {`, + `Write-Error "Failed to ` + op + ` the KVP item. JobState: $($job.JobState)"`, + `Write-Error "Error Description: $($job.ErrorDescription)"`, + `}`, + `} else {`, + `Write-Error "The ` + op + ` method did not start an asynchronous job. Return value: $($result.ReturnValue)"`, + `}`, } - return translateKvpError(err, illegalSuggestion) } -func waitVMResult(res int32, service *wmiext.Service, job *wmiext.Instance, errorMsg string, translate func(int) error) error { - var err error - - switch res { - case 0: - return nil - case 4096: - err = wmiext.WaitJob(service, job) - defer job.Close() - default: - if translate != nil { - return translate(int(res)) - } - - return fmt.Errorf("%s (result code %d)", errorMsg, res) +func getAllKvpItemsScript(vmName string) []string { + return []string{ + `$vm = Get-WmiObject -Namespace root\virtualization\v2 -Class Msvm_ComputerSystem -Filter { ElementName='` + vmName + `' }`, + `write-output ($vm.GetRelated("Msvm_KvpExchangeComponent")[0]).GetRelated("Msvm_KvpExchangeComponentSettingData").HostExchangeItems`, } - - if err != nil { - desc, _ := job.GetAsString("ErrorDescription") - desc = strings.ReplaceAll(desc, "\n", " ") - return fmt.Errorf("%s: %w (%s)", errorMsg, err, desc) - } - - return err } func (vm *VirtualMachine) StopWithForce() error { @@ -234,99 +153,45 @@ func (vm *VirtualMachine) Stop() error { } func (vm *VirtualMachine) stop(force bool) error { - if !Enabled.equal(vm.EnabledState) { + if !Enabled.equal(uint16(vm.State)) { return ErrMachineNotRunning } - var ( - err error - res int32 - srv *wmiext.Service - ) - if srv, err = NewLocalHyperVService(); err != nil { - return err - } - wmiInst, err := srv.FindFirstRelatedInstance(vm.Path(), "Msvm_ShutdownComponent") - if err != nil { - return err - } - // https://learn.microsoft.com/en-us/windows/win32/hyperv_v2/msvm-shutdowncomponent-initiateshutdown - err = wmiInst.BeginInvoke("InitiateShutdown"). - In("Reason", "User requested"). - In("Force", force). - Execute(). - Out("ReturnValue", &res).End() - if err != nil { - return err - } - if res != 0 { - return translateShutdownError(int(res)) + args := []string{"Hyper-V\\Stop-VM", "-Name", vm.Name, "-Force"} + if force { + args = append(args, "-TurnOff") } - // Wait for vm to actually *be* down - for i := 0; i < 200; i++ { - refreshVM, err := vm.vmm.GetMachine(vm.ElementName) - if err != nil { - return err - } - if refreshVM.State() == Disabled { - break - } - time.Sleep(50 * time.Millisecond) + _, stderr, err := powershell.Execute(args...) + if err != nil { + return NewPSError(stderr) } return nil } func (vm *VirtualMachine) Start() error { - var ( - srv *wmiext.Service - err error - job *wmiext.Instance - res int32 - ) - if s := vm.EnabledState; !Disabled.equal(s) { - if Enabled.equal(s) { + if s := vm.State; !Disabled.equal(uint16(s)) { + if Enabled.equal(uint16(s)) { return ErrMachineAlreadyRunning - } else if Starting.equal(s) { + } else if Starting.equal(uint16(s)) { return ErrMachineAlreadyRunning } return errors.New("machine not in a state to start") } - if srv, err = getService(srv); err != nil { - return err - } - defer srv.Close() - - instance, err := srv.GetObject(vm.Path()) + _, stderr, err := powershell.Execute("Hyper-V\\Start-VM", "-Name", vm.Name) if err != nil { - return err - } - defer instance.Close() - - // https://learn.microsoft.com/en-us/windows/win32/hyperv_v2/cim-concretejob-requeststatechange - if err := instance.BeginInvoke("RequestStateChange"). - In("RequestedState", uint16(start)). - In("TimeoutPeriod", &time.Time{}). - Execute(). - Out("Job", &job). - Out("ReturnValue", &res).End(); err != nil { - return err + return NewPSError(stderr) } - return waitVMResult(res, srv, job, "failed to start vm", nil) -} - -func getService(_ *wmiext.Service) (*wmiext.Service, error) { - // any reason why when we instantiate a vm, we should NOT just embed a service? - return NewLocalHyperVService() + return nil } func (vm *VirtualMachine) GetConfig(diskPath string) (*HyperVConfig, error) { var ( diskSize uint64 ) - summary, err := vm.GetSummaryInformation(SummaryRequestCommon) + summary, err := vm.GetSummaryInformation() if err != nil { return nil, err } @@ -337,53 +202,34 @@ func (vm *VirtualMachine) GetConfig(diskPath string) (*HyperVConfig, error) { return nil, err } diskSize = uint64(diskPathInfo.Size()) - mem := MemorySettings{} - if err := vm.getMemorySettings(&mem); err != nil { + mem, err := vm.getMemorySettings() + if err != nil { return nil, err } config := HyperVConfig{ Hardware: HardwareConfig{ // TODO we could implement a getProcessorSettings like we did for memory - CPUs: summary.NumberOfProcessors, + CPUs: uint16(summary.ProcessorCount), DiskPath: diskPath, DiskSize: diskSize, - Memory: mem.Limit, + Memory: mem.StartupBytes, }, Status: Statuses{ - Created: vm.InstallDate, + Created: summary.ConvertToCreationTime(), LastUp: time.Time{}, - Running: Enabled.equal(vm.EnabledState), + Running: Enabled == summary.State, Starting: vm.IsStarting(), - State: EnabledState(vm.EnabledState), + State: EnabledState(summary.State), }, } return &config, nil } // GetSummaryInformation returns the live VM summary information for this virtual machine. -// The requestedFields parameter controls which fields of summary information are populated. -// SummaryRequestCommon and SummaryRequestNearAll provide predefined combinations for this -// parameter -func (vm *VirtualMachine) GetSummaryInformation(requestedFields SummaryRequestSet) (*SummaryInformation, error) { - service, err := NewLocalHyperVService() - if err != nil { - return nil, err - } - defer service.Close() - - instance, err := vm.fetchSystemSettingsInstance(service) - if err != nil { - return nil, err - } - defer instance.Close() +func (vm *VirtualMachine) GetSummaryInformation() (*PowerShellVM, error) { - path, err := instance.Path() - if err != nil { - return nil, err - } - - result, err := vm.vmm.getSummaryInformation(path, requestedFields) + result, err := vm.vmm.getSummaryInformation(vm.Name) if err != nil { return nil, err } @@ -419,11 +265,11 @@ func (vmm *VirtualMachineManager) NewVirtualMachine(name string, config *Hardwar // The API seems to require both of these even // when not using dynamic memory - ms.Limit = config.Memory - ms.VirtualQuantity = config.Memory + ms.MaximumBytes = config.Memory * 1024 * 1024 + ms.StartupBytes = config.Memory * 1024 * 1024 }). PrepareProcessorSettings(func(ps *ProcessorSettings) { - ps.VirtualQuantity = uint64(config.CPUs) // 4 cores + ps.Count = int64(config.CPUs) // 4 cores }). Build() if err != nil { @@ -433,7 +279,7 @@ func (vmm *VirtualMachineManager) NewVirtualMachine(name string, config *Hardwar builder := NewDriveSettingsBuilder(systemSettings). AddScsiController(). AddSyntheticDiskDrive(0). - DefineVirtualHardDisk(config.DiskPath, func(vhdss *VirtualHardDiskStorageSettings) { + DefineVirtualHardDisk(config.DiskPath, func(vhdss *HardDiskDriveSettings) { // set extra params like // vhdss.IOPSLimit = 5000 }). @@ -470,149 +316,65 @@ func (vmm *VirtualMachineManager) NewVirtualMachine(name string, config *Hardwar return nil } -func (vm *VirtualMachine) fetchSystemSettingsInstance(service *wmiext.Service) (*wmiext.Instance, error) { - // When a settings snapshot is taken there are multiple associations, use only the realized/active version - return service.FindFirstRelatedInstanceThrough(vm.Path(), "Msvm_VirtualSystemSettingData", "Msvm_SettingsDefineState") -} - -func (vm *VirtualMachine) fetchExistingResourceSettings(service *wmiext.Service, resourceType string, resourceSettings interface{}) error { - const errFmt = "could not fetch resource settings (%s): %w" - // When a settings snapshot is taken there are multiple associations, use only the realized/active version - instance, err := vm.fetchSystemSettingsInstance(service) - if err != nil { - return fmt.Errorf(errFmt, resourceType, err) - } - defer instance.Close() - - path, err := instance.Path() - if err != nil { - return fmt.Errorf(errFmt, resourceType, err) - - } - - return service.FindFirstRelatedObject(path, resourceType, resourceSettings) -} - -func (vm *VirtualMachine) getMemorySettings(m *MemorySettings) error { - service, err := NewLocalHyperVService() +func (vm *VirtualMachine) getMemorySettings() (*MemorySettings, error) { + memory, err := GetVMMemoryFromName(vm.Name) if err != nil { - return err + return nil, err } - defer service.Close() - return vm.fetchExistingResourceSettings(service, "Msvm_MemorySettingData", m) + return memory, nil } // Update processor and/or mem func (vm *VirtualMachine) UpdateProcessorMemSettings(updateProcessor func(*ProcessorSettings), updateMemory func(*MemorySettings)) error { - service, err := NewLocalHyperVService() - if err != nil { - return err - } - defer service.Close() - proc := &ProcessorSettings{} - mem := &MemorySettings{} - - var settings []string if updateProcessor != nil { - err = vm.fetchExistingResourceSettings(service, "Msvm_ProcessorSettingData", proc) + proc, err := GetVMProcessorFromName(vm.Name) + if err != nil { return err } updateProcessor(proc) - processorStr, err := createProcessorSettings(proc) + err = updateVMProcessor(vm.Name, proc) if err != nil { return err } - settings = append(settings, processorStr) } if updateMemory != nil { - if err := vm.getMemorySettings(mem); err != nil { + mem, err := GetVMMemoryFromName(vm.Name) + if err != nil { return err } updateMemory(mem) - memStr, err := createMemorySettings(mem) + err = updateVMMemory(vm.Name, mem) if err != nil { return err } - - settings = append(settings, memStr) } - if len(settings) < 1 { - return nil - } - - vsms, err := service.GetSingletonInstance("Msvm_VirtualSystemManagementService") - if err != nil { - return err - } - defer vsms.Close() - - var job *wmiext.Instance - var res int32 - err = vsms.BeginInvoke("ModifyResourceSettings"). - In("ResourceSettings", settings). - Execute(). - Out("Job", &job). - Out("ReturnValue", &res). - End() - - if err != nil { - return fmt.Errorf("failed to modify resource settings: %w", err) - } - - return waitVMResult(res, service, job, "failed to modify resource settings", translateModifyError) + return nil } func (vm *VirtualMachine) remove() (int32, error) { - var ( - err error - res int32 - srv *wmiext.Service - ) - refreshVM, err := vm.vmm.GetMachine(vm.ElementName) + refreshVM, err := vm.vmm.GetMachine(vm.Name) if err != nil { return 0, err } + // Check for disabled/stopped state - if !Disabled.equal(refreshVM.EnabledState) { + if !Disabled.equal(uint16(refreshVM.GetState())) { return -1, ErrMachineStateInvalid } - if srv, err = NewLocalHyperVService(); err != nil { - return -1, err - } - - vsms, err := srv.GetSingletonInstance("Msvm_VirtualSystemManagementService") + _, stderr, err := powershell.Execute("Hyper-V\\Remove-VM", "-Name", vm.Name, "-Force") if err != nil { - return -1, err - } - defer vsms.Close() - - var ( - job *wmiext.Instance - ) - - // https://learn.microsoft.com/en-us/windows/win32/hyperv_v2/cim-virtualsystemmanagementservice-destroysystem - if err := vsms.BeginInvoke("DestroySystem"). - In("AffectedSystem", vm.Path()). - Execute(). - Out("Job", &job). - Out("ReturnValue", &res).End(); err != nil { - return -1, err - } - - // do i have this correct? you can get an error without a result? - if err := waitVMResult(res, srv, job, "failed to remove vm", nil); err != nil { - return -1, err + return -1, NewPSError(stderr) } - return res, nil + return 0, nil } func (vm *VirtualMachine) Remove(diskPath string) error { @@ -629,10 +391,10 @@ func (vm *VirtualMachine) Remove(diskPath string) error { return nil } -func (vm *VirtualMachine) State() EnabledState { - return EnabledState(vm.EnabledState) +func (vm *VirtualMachine) GetState() EnabledState { + return EnabledState(vm.State) } func (vm *VirtualMachine) IsStarting() bool { - return Starting.equal(vm.EnabledState) + return Starting.equal(uint16(vm.State)) } diff --git a/pkg/hypervctl/vm_config.go b/pkg/hypervctl/vm_config.go index 7f7c4bb..daedaad 100644 --- a/pkg/hypervctl/vm_config.go +++ b/pkg/hypervctl/vm_config.go @@ -1,5 +1,4 @@ //go:build windows -// +build windows package hypervctl @@ -7,21 +6,6 @@ import ( "time" ) -type vmState uint16 - -const ( - // Changes the state to 'Running'. - start vmState = 2 - // Stops the job temporarily. The intention is to subsequently restart the job with 'Start'. It might be possible to - // enter the 'Service' state while suspended. (This is job-specific.) - suspend vmState = 3 //nolint: unused - // Stops the job cleanly, saves data, preserves the state, and shuts down all underlying processes' - // in an orderly manner. - terminate vmState = 4 //nolint: unused - //Terminates the job immediately with no requirement to save data or preserve the state. - kill vmState = 5 //nolint: unused -) - type EnabledState uint16 const ( diff --git a/pkg/hypervctl/vmm.go b/pkg/hypervctl/vmm.go index ea7f239..ed77191 100644 --- a/pkg/hypervctl/vmm.go +++ b/pkg/hypervctl/vmm.go @@ -1,20 +1,23 @@ //go:build windows -// +build windows package hypervctl import ( - "errors" - "fmt" + "strconv" - "github.com/containers/libhvee/pkg/wmiext" + "github.com/containers/libhvee/pkg/powershell" ) -const ( - HyperVNamespace = "root\\virtualization\\v2" - VirtualSystemManagementService = "Msvm_VirtualSystemManagementService" - MsvmComputerSystem = "Msvm_ComputerSystem" -) +func CheckHypervAvailable() error { + if err := powershell.HypervAvailable(); err != nil { + return err + } + + if !powershell.IsHypervAdministrator() { + return powershell.ErrNotAdministrator + } + return nil +} // https://learn.microsoft.com/en-us/windows/win32/hyperv_v2/msvm-computersystem @@ -25,53 +28,15 @@ func NewVirtualMachineManager() *VirtualMachineManager { return &VirtualMachineManager{} } -func NewLocalHyperVService() (*wmiext.Service, error) { - service, err := wmiext.NewLocalService(HyperVNamespace) - if err != nil { - return nil, translateCommonHyperVWmiError(err) - } - - return service, nil -} - func (vmm *VirtualMachineManager) GetAll() ([]*VirtualMachine, error) { - // Fetch through settings to avoid locale sensitive properties - const wql = "Select * From Msvm_VirtualSystemSettingData Where VirtualSystemType = 'Microsoft:Hyper-V:System:Realized'" - - var service *wmiext.Service - var err error - if service, err = NewLocalHyperVService(); err != nil { - return []*VirtualMachine{}, err - } - defer service.Close() - - var enum *wmiext.Enum - if enum, err = service.ExecQuery(wql); err != nil { + psVMs, err := GetVMsFromPowerShell() + if err != nil { return nil, err } - defer enum.Close() - var vms []*VirtualMachine - - for { - settings, err := enum.Next() - if err != nil { - return vms, err - } - - // Finished iterating - if settings == nil { - break - } - - vm, err := vmm.findVMFromSettings(service, settings) - settings.Close() - if err != nil { - return vms, err - } - - vms = append(vms, vm) + vms := make([]*VirtualMachine, 0, len(psVMs)) + for _, psVM := range psVMs { + vms = append(vms, psVM.ConvertToVirtualMachine(vmm)) } - return vms, nil } @@ -98,138 +63,49 @@ func (vmm *VirtualMachineManager) GetMachine(name string) (*VirtualMachine, erro func (vmm *VirtualMachineManager) GetMachineExists(name string) (bool, *VirtualMachine, error) { vm, err := vmm.getMachine(name) if err != nil { - if errors.Is(err, wmiext.ErrNoResults) { - return false, nil, nil - } - return false, nil, err + return false, nil, nil } return true, vm, nil } // getMachine looks up a single VM by name func (vmm *VirtualMachineManager) getMachine(name string) (*VirtualMachine, error) { - const wql = "Select * From Msvm_VirtualSystemSettingData Where VirtualSystemType = 'Microsoft:Hyper-V:System:Realized' And ElementName='%s'" - - vm := &VirtualMachine{} - var service *wmiext.Service - var err error - - if service, err = NewLocalHyperVService(); err != nil { - return vm, err - } - defer service.Close() - - var enum *wmiext.Enum - if enum, err = service.ExecQuery(fmt.Sprintf(wql, name)); err != nil { - return nil, err - } - defer enum.Close() - - settings, err := service.FindFirstInstance(fmt.Sprintf(wql, name)) - if err != nil { - if errors.Is(err, wmiext.ErrNoResults) { - return nil, err - } - return vm, fmt.Errorf("could not find virtual machine %q: %w", name, err) - } - defer settings.Close() - - return vmm.findVMFromSettings(service, settings) -} - -func (vmm *VirtualMachineManager) findVMFromSettings(service *wmiext.Service, settings *wmiext.Instance) (*VirtualMachine, error) { - path, err := settings.Path() + psVM, err := GetVMFromPowerShell(name) if err != nil { return nil, err } - - vm := &VirtualMachine{vmm: vmm} - err = service.FindFirstRelatedObject(path, MsvmComputerSystem, vm) - - return vm, err + return psVM.ConvertToVirtualMachine(vmm), nil } func (*VirtualMachineManager) CreateVhdxFile(path string, maxSize uint64) error { - var service *wmiext.Service - var err error - if service, err = NewLocalHyperVService(); err != nil { - return err - } - defer service.Close() - - settings := &VirtualHardDiskSettings{} - settings.Format = 3 - settings.MaxInternalSize = maxSize - settings.Type = 3 - settings.Path = path - - instance, err := service.CreateInstance("Msvm_VirtualHardDiskSettingData", settings) + _, stderr, err := powershell.Execute("Hyper-V\\New-VHD", "-Path", path, "-SizeBytes", strconv.FormatUint(maxSize, 10)) if err != nil { - return err + return NewPSError(stderr) } - defer instance.Close() - settingsStr := instance.GetCimText() - - imms, err := service.GetSingletonInstance("Msvm_ImageManagementService") - if err != nil { - return err - } - defer imms.Close() - - var job *wmiext.Instance - var ret int32 - err = imms.BeginInvoke("CreateVirtualHardDisk"). - In("VirtualDiskSettingData", settingsStr). - Execute(). - Out("Job", &job). - Out("ReturnValue", &ret). - End() - - if err != nil { - return fmt.Errorf("failed to create vhdx: %w", err) - } - - return waitVMResult(ret, service, job, "failed to create vhdx", nil) + return nil } // GetSummaryInformation returns the live VM summary information for all virtual machines. -// The requestedFields parameter controls which fields of summary information are populated. -// SummaryRequestCommon and SummaryRequestNearAll provide predefined combinations for this -// parameter. -func (vmm *VirtualMachineManager) GetSummaryInformation(requestedFields SummaryRequestSet) ([]SummaryInformation, error) { - return vmm.getSummaryInformation("", requestedFields) +func (vmm *VirtualMachineManager) GetSummaryInformation() ([]PowerShellVM, error) { + return vmm.getSummaryInformation("") } -func (vmm *VirtualMachineManager) getSummaryInformation(settingsPath string, requestedFields SummaryRequestSet) ([]SummaryInformation, error) { - var service *wmiext.Service - var err error - if service, err = NewLocalHyperVService(); err != nil { - return nil, err - } - defer service.Close() - - vsms, err := service.GetSingletonInstance(VirtualSystemManagementService) - if err != nil { - return nil, err - } - defer vsms.Close() - - var summary []SummaryInformation - - inv := vsms.BeginInvoke("GetSummaryInformation"). - In("RequestedInformation", []uint(requestedFields)) - - if len(settingsPath) > 0 { - inv.In("SettingData", []string{settingsPath}) - } +func (vmm *VirtualMachineManager) getSummaryInformation(vmName string) ([]PowerShellVM, error) { + if vmName == "" { + // Get all VMs + psVMs, err := GetVMsFromPowerShell() + if err != nil { + return nil, err + } - err = inv.Execute(). - Out("SummaryInformation", &summary). - End() + return psVMs, nil + } else { + // Get specific VM + psVM, err := GetVMFromPowerShell(vmName) + if err != nil { + return nil, err + } - if err != nil { - return nil, err + return []PowerShellVM{*psVM}, nil } - - return summary, nil } diff --git a/pkg/kvp/ginsu/util.go b/pkg/kvp/ginsu/util.go index 49e5e27..289442c 100644 --- a/pkg/kvp/ginsu/util.go +++ b/pkg/kvp/ginsu/util.go @@ -1,3 +1,5 @@ +//go:build windows + package ginsu // diff --git a/pkg/powershell/powershell.go b/pkg/powershell/powershell.go new file mode 100644 index 0000000..f6d83e7 --- /dev/null +++ b/pkg/powershell/powershell.go @@ -0,0 +1,134 @@ +//go:build windows + +package powershell + +import ( + "bytes" + "os" + "os/exec" + "path/filepath" + "strings" + "syscall" +) + +var isAdminCmds = []string{ + "$currentPrincipal = New-Object Security.Principal.WindowsPrincipal([Security.Principal.WindowsIdentity]::GetCurrent())", + "$currentPrincipal.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)", +} + +func IsAdmin() bool { + cmd := strings.Join(isAdminCmds, ";") + stdOut, _, err := Execute(cmd) + if err != nil { + return false + } + if strings.TrimSpace(stdOut) == "False" { + return false + } + + return true +} + +func Execute(args ...string) (string, string, error) { + // logging.Debugf("Running '%s'", strings.Join(args, " ")) + + powershell, err := exec.LookPath("powershell.exe") + if err != nil { + return "", "", err + } + args = append([]string{"-NoProfile", "-NonInteractive", "-ExecutionPolicy", "RemoteSigned", "-Command", + "$ProgressPreference = 'SilentlyContinue';", "[Console]::OutputEncoding = [System.Text.UTF8Encoding]::new();"}, args...) + cmd := exec.Command(powershell, args...) // #nosec G204 + cmd.SysProcAttr = &syscall.SysProcAttr{HideWindow: true} + + var stdout bytes.Buffer + var stderr bytes.Buffer + cmd.Stdout = &stdout + cmd.Stderr = &stderr + + err = cmd.Run() + + return stdout.String(), stderr.String(), err +} + +func ExecuteAsScript(script []string) (string, string, error) { + scriptContent := strings.Join(script, "\n") + + tempDir, err := os.MkdirTemp("", "crcScripts") + if err != nil { + return "", "", err + } + + // Write a temporary script + /* Add UTF-8 BOM at the beginning of the script so that Windows + * correctly detects the file encoding + */ + filename := filepath.Join(tempDir, "runAsAdmin.ps1") + + // #nosec G306 + if err := os.WriteFile(filename, append([]byte{0xef, 0xbb, 0xbf}, []byte(scriptContent)...), 0600); err != nil { + return "", "", err + } + + defer func() { _ = os.RemoveAll(filename) }() + + stdout, stderr, err := Execute(filename) + if err != nil { + return "", stderr, err + } + return stdout, stderr, nil +} + +func ExecuteAsAdmin(reason, cmd string) (string, string, error) { + powershell, err := exec.LookPath("powershell.exe") + if err != nil { + return "", "", err + } + scriptContent := strings.Join(append(runAsCmds(powershell), cmd), "\n") + + tempDir, err := os.MkdirTemp("", "crcScripts") + if err != nil { + return "", "", err + } + // ignore error + defer func() { _ = os.RemoveAll(tempDir) }() + + // Write a temporary script + /* Add UTF-8 BOM at the beginning of the script so that Windows + * correctly detects the file encoding + */ + filename := filepath.Join(tempDir, "runAsAdmin.ps1") + + // #nosec G306 + if err := os.WriteFile(filename, append([]byte{0xef, 0xbb, 0xbf}, []byte(scriptContent)...), 0600); err != nil { + return "", "", err + } + + // logging.Infof("Will run as admin: %s", reason) + + return Execute(filename) +} + +func runAsCmds(powershell string) []string { + return []string{ + `[Console]::OutputEncoding = [System.Text.UTF8Encoding]::new();`, + `$myWindowsID = [System.Security.Principal.WindowsIdentity]::GetCurrent();`, + `$myWindowsPrincipal = New-Object System.Security.Principal.WindowsPrincipal($myWindowsID);`, + `$adminRole = [System.Security.Principal.WindowsBuiltInRole]::Administrator;`, + `if (-Not ($myWindowsPrincipal.IsInRole($adminRole))) {`, + ` $procInfo = New-Object System.Diagnostics.ProcessStartInfo;`, + ` $procInfo.FileName = "` + powershell + `"`, + ` $procInfo.WindowStyle = [Diagnostics.ProcessWindowStyle]::Hidden`, + ` $procInfo.Arguments = "-ExecutionPolicy RemoteSigned & '" + $script:MyInvocation.MyCommand.Path + "'"`, + ` $procInfo.Verb = "runas";`, + ` $p = New-Object System.Diagnostics.Process`, + ` $p.StartInfo = $procInfo`, + ` $p.Start() | Out-Null`, + ` $p.WaitForExit()`, + ` if ($p.ExitCode -ne 0) {`, + ` throw "Unexpected failure";`, + ` }`, + ` Exit;`, + `}`, + } +} diff --git a/pkg/powershell/ps.go b/pkg/powershell/ps.go new file mode 100644 index 0000000..48d30c7 --- /dev/null +++ b/pkg/powershell/ps.go @@ -0,0 +1,52 @@ +//go:build windows + +package powershell + +import ( + "errors" + "strings" +) + +var ( + ErrPowerShellNotFound = errors.New("powershell was not found in the path") + ErrNotAdministrator = errors.New("hyper-v commands have to be run as an administrator") + ErrNotInstalled = errors.New("hyper-v powershell module is not available") +) + +func cmdOut(args ...string) (string, error) { + stdout, _, err := Execute(args...) + return stdout, err +} + +func HypervAvailable() error { + stdout, err := cmdOut("@(Get-Module -ListAvailable hyper-v).Name | Get-Unique") + if err != nil { + return err + } + + resp := firstLine(stdout) + + if resp != "Hyper-V" { + return ErrNotInstalled + } + + return nil +} + +func IsHypervAdministrator() bool { + stdout, err := cmdOut(`@([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole(([System.Security.Principal.SecurityIdentifier]::new("S-1-5-32-578")))`) + if err != nil { + return false + } + + resp := firstLine(stdout) + return resp == "True" +} + +func firstLine(stdout string) string { + stdout = strings.TrimSpace(stdout) + if len(strings.Split(stdout, "\n")) == 0 { + return stdout + } + return strings.Split(stdout, "\n")[0] +} diff --git a/pkg/wmiext/array.go b/pkg/wmiext/array.go deleted file mode 100644 index 825d0bb..0000000 --- a/pkg/wmiext/array.go +++ /dev/null @@ -1,205 +0,0 @@ -//go:build windows -// +build windows - -package wmiext - -import ( - "errors" - "fmt" - "reflect" - "unsafe" - - "github.com/go-ole/go-ole" -) - -func CreateStringArrayVariant(array []string) (ole.VARIANT, error) { - safeByteArray, err := safeArrayFromStringSlice(array) - if err != nil { - return ole.VARIANT{}, err - } - arrayVariant := ole.NewVariant(ole.VT_ARRAY|ole.VT_BSTR, int64(uintptr(unsafe.Pointer(safeByteArray)))) - return arrayVariant, nil -} - -func CreateNumericArrayVariant(array interface{}, itemType ole.VT) (ole.VARIANT, error) { - safeArray, err := safeArrayFromNumericSlice(array, itemType) - if err != nil { - return ole.VARIANT{}, err - } - arrayVariant := ole.NewVariant(ole.VT_ARRAY|itemType, int64(uintptr(unsafe.Pointer(safeArray)))) - return arrayVariant, nil -} - -// The following safearray routines are unfortunately not yet exported from go-ole, -// so replicate them for now -func safeArrayCreateVector(variantType ole.VT, lowerBound int32, length uint32) (safearray *ole.SafeArray, err error) { - ret, _, _ := procSafeArrayCreateVector.Call( - uintptr(variantType), - uintptr(lowerBound), - uintptr(length)) - - if ret == 0 { // NULL return value - err = fmt.Errorf("could not create safe array") - } - safearray = (*ole.SafeArray)(unsafe.Pointer(ret)) //nolint:govet - return -} - -func safeArrayFromNumericSlice(slice interface{}, itemType ole.VT) (*ole.SafeArray, error) { - sliceType := reflect.TypeOf(slice) - if sliceType.Kind() != reflect.Slice { - return nil, errors.New("expected a slice converting to safe array") - } - - val := reflect.ValueOf(slice) - - array, err := safeArrayCreateVector(itemType, 0, uint32(val.Len())) - if err != nil { - return nil, err - } - - // assignable holders used for conversion - var ( - vui1 uint8 - vui2 uint16 - vui4 uint32 - vui8 uint64 - vi1 int8 - vi2 int16 - vi4 int32 - vi8 int64 - ) - - for i := 0; i < val.Len(); i++ { - var data uintptr - item := val.Index(i) - switch itemType { - case ole.VT_UI1: - data = convertToUnsafeAddr(item, &vui1) - case ole.VT_UI2: - data = convertToUnsafeAddr(item, &vui2) - case ole.VT_UI4: - data = convertToUnsafeAddr(item, &vui4) - case ole.VT_UI8: - data = convertToUnsafeAddr(item, &vui8) - case ole.VT_I1: - data = convertToUnsafeAddr(item, &vi1) - case ole.VT_I2: - data = convertToUnsafeAddr(item, &vi2) - case ole.VT_I4: - data = convertToUnsafeAddr(item, &vi4) - case ole.VT_I8: - data = convertToUnsafeAddr(item, &vi8) - } - - err = safeArrayPutElement(array, int64(i), data) - if err != nil { - _ = safeArrayDestroy(array) - return nil, err - } - } - - return array, nil -} - -func convertToUnsafeAddr(src reflect.Value, target interface{}) uintptr { - val := reflect.ValueOf(target) - val = val.Elem() - val.Set(src.Convert(val.Type())) - return val.UnsafeAddr() -} - -func safeArrayDestroy(safearray *ole.SafeArray) (err error) { - ret, _, _ := procSafeArrayDestroy.Call(uintptr(unsafe.Pointer(safearray))) - - if ret != 0 { - return NewWmiError(ret) - } - - return nil -} - -func safeArrayPutElement(safearray *ole.SafeArray, index int64, element uintptr) (err error) { - - ret, _, _ := procSafeArrayPutElement.Call( - uintptr(unsafe.Pointer(safearray)), - uintptr(unsafe.Pointer(&index)), - element) - - if ret != 0 { - return NewWmiError(ret) - } - - return nil -} - -func safeArrayGetElement(safearray *ole.SafeArray, index int64, element unsafe.Pointer) error { - - ret, _, _ := procSafeArrayGetElement.Call( - uintptr(unsafe.Pointer(safearray)), - uintptr(unsafe.Pointer(&index)), - uintptr(element)) - - if ret != 0 { - return NewWmiError(ret) - } - - return nil -} - -func isVariantValConvertible(variant ole.VARIANT) bool { - if variant.VT == ole.VT_RECORD || variant.VT == ole.VT_VARIANT { - return false - } - return true -} - -func safeArrayGetAsVariantVal(safeArray *ole.SafeArray, index int64, variant ole.VARIANT) (int64, error) { - var block int64 - - if !isVariantValConvertible(variant) { - return 0, fmt.Errorf("numeric call on a non-numeric value: %d", variant.VT) - } - - if err := safeArrayGetElement(safeArray, index, unsafe.Pointer(&block)); err != nil { - return 0, err - } - - switch variant.VT { - case ole.VT_UI1: - return int64(uint64(*(*uint8)(unsafe.Pointer(&block)))), nil - case ole.VT_UI2: - return int64(uint64(*(*uint16)(unsafe.Pointer(&block)))), nil - case ole.VT_UI4: - return int64(uint64(*(*uint32)(unsafe.Pointer(&block)))), nil - case ole.VT_I1: - return int64(*(*int8)(unsafe.Pointer(&block))), nil - case ole.VT_I2: - return int64(*(*int16)(unsafe.Pointer(&block))), nil - case ole.VT_I4: - return int64(*(*int32)(unsafe.Pointer(&block))), nil - case ole.VT_UI8, ole.VT_I8: - fallthrough - case ole.VT_R4, ole.VT_R8: - fallthrough - default: - return block, nil - } -} - -func safeArrayFromStringSlice(slice []string) (*ole.SafeArray, error) { - array, err := safeArrayCreateVector(ole.VT_BSTR, 0, uint32(len(slice))) - - if err != nil { - return nil, err - } - - for i, v := range slice { - err = safeArrayPutElement(array, int64(i), uintptr(unsafe.Pointer(ole.SysAllocStringLen(v)))) - if err != nil { - _ = safeArrayDestroy(array) - return nil, err - } - } - return array, nil -} diff --git a/pkg/wmiext/conversion.go b/pkg/wmiext/conversion.go deleted file mode 100644 index ad4c595..0000000 --- a/pkg/wmiext/conversion.go +++ /dev/null @@ -1,474 +0,0 @@ -//go:build windows -// +build windows - -package wmiext - -import ( - "errors" - "fmt" - "math" - "reflect" - "strconv" - "strings" - "time" - "unsafe" - - "github.com/go-ole/go-ole" -) - -var ( - unixEpoch = time.Unix(0, 0) - zeroTime = time.Time{} -) - -// Automation variants do not follow the OLE rules, instead they use the following mapping: -// sint8 VT_I2 Signed 8-bit integer. -// sint16 VT_I2 Signed 16-bit integer. -// sint32 VT_I4 Signed 32-bit integer. -// sint64 VT_BSTR Signed 64-bit integer in string form. This type follows hexadecimal or decimal format -// -// according to the American National Standards Institute (ANSI) C rules. -// -// real32 VT_R4 4-byte floating-point value that follows the Institute of Electrical and Electronics -// -// Engineers, Inc. (IEEE) standard. -// -// real64 VT_R8 8-byte floating-point value that follows the IEEE standard. -// uint8 VT_UI1 Unsigned 8-bit integer. -// uint16 VT_I4 Unsigned 16-bit integer. -// uint32 VT_I4 Unsigned 32-bit integer. -// uint64 VT_BSTR Unsigned 64-bit integer in string form. This type follows hexadecimal or decimal format -// -// according to ANSI C rules. - -// NewAutomationVariant returns a new VARIANT com -// -//gocyclo:ignore -func NewAutomationVariant(value interface{}) (ole.VARIANT, error) { - switch cast := value.(type) { - case bool: - if cast { - return ole.NewVariant(ole.VT_BOOL, 0xffff), nil - } else { - return ole.NewVariant(ole.VT_BOOL, 0), nil - } - case int8: - return ole.NewVariant(ole.VT_I2, int64(cast)), nil - case []int8: - return CreateNumericArrayVariant(cast, ole.VT_I2) - case int16: - return ole.NewVariant(ole.VT_I2, int64(cast)), nil - case []int16: - return CreateNumericArrayVariant(cast, ole.VT_I2) - case int32: - return ole.NewVariant(ole.VT_I4, int64(cast)), nil - case []int32: - return CreateNumericArrayVariant(cast, ole.VT_I4) - case int64: - s := fmt.Sprintf("%d", cast) - return ole.NewVariant(ole.VT_BSTR, int64(uintptr(unsafe.Pointer(ole.SysAllocStringLen(s))))), nil - case []int64: - strs := make([]string, len(cast)) - for i, num := range cast { - strs[i] = fmt.Sprintf("%d", num) - } - return CreateStringArrayVariant(strs) - case float32: - return ole.NewVariant(ole.VT_R4, int64(math.Float32bits(cast))), nil - case float64: - return ole.NewVariant(ole.VT_R8, int64(math.Float64bits(cast))), nil - case uint8: - return ole.NewVariant(ole.VT_UI1, int64(cast)), nil - case []uint8: - return CreateNumericArrayVariant(cast, ole.VT_UI1) - case uint16: - return ole.NewVariant(ole.VT_I4, int64(cast)), nil - case []uint16: - return CreateNumericArrayVariant(cast, ole.VT_I4) - case uint32: - return ole.NewVariant(ole.VT_I4, int64(cast)), nil - case []uint32: - return CreateNumericArrayVariant(cast, ole.VT_I4) - case uint64: - s := fmt.Sprintf("%d", cast) - return ole.NewVariant(ole.VT_BSTR, int64(uintptr(unsafe.Pointer(ole.SysAllocStringLen(s))))), nil - case []uint64: - strs := make([]string, len(cast)) - for i, num := range cast { - strs[i] = fmt.Sprintf("%d", num) - } - return CreateStringArrayVariant(strs) - - // Assume 32 bit for generic (u)ints - case int: - return ole.NewVariant(ole.VT_I4, int64(cast)), nil - case uint: - return ole.NewVariant(ole.VT_I4, int64(cast)), nil - case []int: - return CreateNumericArrayVariant(cast, ole.VT_I4) - case []uint: - return CreateNumericArrayVariant(cast, ole.VT_I4) - - case string: - return ole.NewVariant(ole.VT_BSTR, int64(uintptr(unsafe.Pointer(ole.SysAllocStringLen(value.(string)))))), nil - case []string: - if len(cast) == 0 { - return ole.NewVariant(ole.VT_NULL, 0), nil - } - return CreateStringArrayVariant(cast) - - case time.Time: - return convertTimeToDataTime(&cast), nil - case *time.Time: - return convertTimeToDataTime(cast), nil - case time.Duration: - return convertDurationToDateTime(cast), nil - case nil: - return ole.NewVariant(ole.VT_NULL, 0), nil - case *ole.IUnknown: - if cast == nil { - return ole.NewVariant(ole.VT_NULL, 0), nil - } - return ole.NewVariant(ole.VT_UNKNOWN, int64(uintptr(unsafe.Pointer(cast)))), nil - case *Instance: - if cast == nil { - return ole.NewVariant(ole.VT_NULL, 0), nil - } - return ole.NewVariant(ole.VT_UNKNOWN, int64(uintptr(unsafe.Pointer(cast.object)))), nil - default: - return ole.VARIANT{}, fmt.Errorf("unsupported type for automation variants %T", value) - } -} - -func convertToGoType(variant *ole.VARIANT, outputValue reflect.Value, outputType reflect.Type) (value interface{}, err error) { - if variant.VT&ole.VT_ARRAY == ole.VT_ARRAY { - return convertVariantToArray(variant, outputType) - } - - if variant.VT == ole.VT_UNKNOWN { - return convertVariantToStruct(variant, outputType) - } - - switch cast := outputValue.Interface().(type) { - case bool: - return variant.Val != 0, nil - case time.Time: - return convertDataTimeToTime(variant) - case *time.Time: - x, err := convertDataTimeToTime(variant) - return &x, err - case time.Duration: - return convertIntervalToDuration(variant) - case uint, uint8, uint16, uint32, uint64, int, int8, int16, int32, int64: - return convertVariantToInt(variant, outputType) - case float32, float64: - return convertVariantToFloat(variant, outputType) - case string: - return variant.ToString(), nil - default: - if variant.VT == ole.VT_NULL { - return nil, nil - } - return nil, fmt.Errorf("could not convert %d to %v", variant.VT, cast) - } -} - -func convertInt64ToInt(value int64, outputType reflect.Type) (interface{}, error) { - switch outputType.Kind() { - case reflect.Int: - return int(value), nil - case reflect.Int8: - return int8(value), nil - case reflect.Int16: - return int16(value), nil - case reflect.Int32: - return int32(value), nil - case reflect.Int64: - return int64(value), nil - case reflect.Uint: - return uint(value), nil - case reflect.Uint8: - return uint8(value), nil - case reflect.Uint16: - return uint16(value), nil - case reflect.Uint32: - return uint32(value), nil - case reflect.Uint64: - return uint64(value), nil - default: - return 0, fmt.Errorf("could not convert int64 to %v", outputType) - } -} - -func convertStringToInt64(str string, unsigned bool) (int64, error) { - if unsigned { - val, err := strconv.ParseUint(str, 0, 64) - return int64(val), err - } - - return strconv.ParseInt(str, 0, 64) -} - -func convertVariantToInt(variant *ole.VARIANT, outputType reflect.Type) (interface{}, error) { - var value int64 - switch variant.VT { - case ole.VT_NULL: - fallthrough - case ole.VT_BOOL: - fallthrough - case ole.VT_I1, ole.VT_I2, ole.VT_I4, ole.VT_I8, ole.VT_INT: - fallthrough - case ole.VT_UI1, ole.VT_UI2, ole.VT_UI4, ole.VT_UI8, ole.VT_UINT: - value = variant.Val - case ole.VT_R4: - // not necessarily a useful conversion but handle it anyway - value = int64(*(*float32)(unsafe.Pointer(&variant.Val))) - case ole.VT_R8: - value = int64(*(*float64)(unsafe.Pointer(&variant.Val))) - case ole.VT_BSTR: - var err error - value, err = convertStringToInt64(variant.ToString(), outputType.Kind() == reflect.Uint64) - if err != nil { - return value, err - } - default: - return nil, fmt.Errorf("could not convert variant type %d to %v", variant.VT, outputType) - } - - return convertInt64ToInt(value, outputType) -} - -func convertVariantToFloat(variant *ole.VARIANT, outputType reflect.Type) (interface{}, error) { - var value float64 - switch variant.VT { - case ole.VT_NULL: - fallthrough - case ole.VT_BOOL: - fallthrough - case ole.VT_I1, ole.VT_I2, ole.VT_I4, ole.VT_I8, ole.VT_INT: - fallthrough - case ole.VT_UI1, ole.VT_UI2, ole.VT_UI4, ole.VT_UI8, ole.VT_UINT: - value = float64(variant.Val) - case ole.VT_R4: - value = float64(*(*float32)(unsafe.Pointer(&variant.Val))) - case ole.VT_R8: - value = *(*float64)(unsafe.Pointer(&variant.Val)) - case ole.VT_BSTR: - var err error - value, err = strconv.ParseFloat(variant.ToString(), 64) - if err != nil { - return value, err - } - default: - return nil, fmt.Errorf("could not convert variant type %d to %v", variant.VT, outputType) - } - - if outputType.Kind() == reflect.Float32 { - return float32(value), nil - } - - return value, nil -} - -func convertVariantToStruct(variant *ole.VARIANT, outputType reflect.Type) (interface{}, error) { - if variant.VT != ole.VT_UNKNOWN { - return nil, fmt.Errorf("could not convert non-IUnknown variant type %d to %v", variant.VT, outputType) - } - - ptr := variant.ToIUnknown() - - var rawInstance struct { - *ole.IUnknown - *IWbemClassObjectVtbl - } - - rawInstance.IUnknown = ptr - rawInstance.IWbemClassObjectVtbl = (*IWbemClassObjectVtbl)(unsafe.Pointer(ptr.RawVTable)) - - instance := (*Instance)(unsafe.Pointer(&rawInstance)) - val := reflect.New(outputType) - err := instance.GetAll(val.Interface()) - return val.Elem().Interface(), err -} - -func convertVariantToArray(variant *ole.VARIANT, outputType reflect.Type) (interface{}, error) { - if variant.VT&ole.VT_ARRAY != ole.VT_ARRAY { - return nil, fmt.Errorf("could not convert non-array variant type %d to %v", variant.VT, outputType) - } - - safeArrayConversion := ole.SafeArrayConversion{Array: *(**ole.SafeArray)(unsafe.Pointer(&variant.Val))} - - arrayLen, err := safeArrayConversion.TotalElements(0) - if err != nil { - return nil, err - } - elemVT := (^ole.VT_ARRAY) & variant.VT - slice := reflect.MakeSlice(reflect.SliceOf(outputType.Elem()), int(arrayLen), int(arrayLen)) - - for i := 0; i < int(arrayLen); i++ { - elemVariant := ole.VARIANT{VT: elemVT} - elemSrc, err := safeArrayGetAsVariantVal(safeArrayConversion.Array, int64(i), elemVariant) - if err != nil { - return nil, err - } - elemVariant.Val = int64(elemSrc) - elemDest, err := convertToGoType(&elemVariant, slice.Index(i), outputType.Elem()) - if err != nil { - return nil, err - } - - slice.Index(i).Set(reflect.ValueOf(elemDest)) - } - - return slice.Interface(), nil -} - -func convertToGenericValue(variant *ole.VARIANT) interface{} { - var result interface{} - if variant.VT&ole.VT_ARRAY == ole.VT_ARRAY { - safeArrayConversion := ole.SafeArrayConversion{Array: *(**ole.SafeArray)(unsafe.Pointer(&variant.Val))} - result = safeArrayConversion.ToValueArray() - } else { - result = variant.Value() - } - return result -} - -func convertTimeToDataTime(time *time.Time) ole.VARIANT { - if time == nil || !time.After(WindowsEpoch) { - return ole.NewVariant(ole.VT_NULL, 0) - } - _, offset := time.Zone() - // convert to minutes - offset /= 60 - //yyyymmddHHMMSS.mmmmmmsUUU - s := fmt.Sprintf("%s%+04d", time.Format("20060102150405.000000"), offset) - return ole.NewVariant(ole.VT_BSTR, int64(uintptr(unsafe.Pointer(ole.SysAllocStringLen(s))))) -} - -func convertDurationToDateTime(duration time.Duration) ole.VARIANT { - const dayTime = time.Second * 86400 - - if duration == 0 { - return ole.NewVariant(ole.VT_NULL, 0) - } - - days := duration / dayTime - duration = duration % dayTime - - hours := duration / time.Hour - duration = duration % time.Hour - - mins := duration / time.Minute - duration = duration % time.Minute - - seconds := duration / time.Second - duration = duration % time.Second - - micros := duration / time.Microsecond - - s := fmt.Sprintf("%08d%02d%02d%02d.%06d:000", days, hours, mins, seconds, micros) - return ole.NewVariant(ole.VT_BSTR, int64(uintptr(unsafe.Pointer(ole.SysAllocStringLen(s))))) -} - -func extractDateTimeString(variant *ole.VARIANT) (string, error) { - switch variant.VT { - case ole.VT_BSTR: - return variant.ToString(), nil - case ole.VT_NULL: - return "", nil - default: - return "", errors.New("variant not compatible with dateTime field") - } -} - -func convertDataTimeToTime(variant *ole.VARIANT) (time.Time, error) { - var err error - dateTime, err := extractDateTimeString(variant) - if err != nil || len(dateTime) == 0 { - return zeroTime, err - } - - dLen := len(dateTime) - if dLen < 5 { - return zeroTime, errors.New("invalid datetime string") - } - - if strings.HasPrefix(dateTime, "00000000000000.000000") { - // Zero time - return zeroTime, nil - } - - zoneStart := dLen - 4 - timePortion := dateTime[0:zoneStart] - - var zoneMinutes int64 - if dateTime[zoneStart] == ':' { - // interval ends in :000 - return parseIntervalTime(dateTime) - } - - zoneSuffix := dateTime[zoneStart:dLen] - zoneMinutes, err = strconv.ParseInt(zoneSuffix, 10, 0) - if err != nil { - return zeroTime, errors.New("invalid datetime string, zone did not parse") - } - - timePortion = fmt.Sprintf("%s%+03d%02d", timePortion, zoneMinutes/60, abs(int(zoneMinutes%60))) - return time.Parse("20060102150405.000000-0700", timePortion) -} - -// parseIntervalTime encodes an interval time as an offset to Unix time -// allowing a duration to be computed without precision loss -func parseIntervalTime(interval string) (time.Time, error) { - if len(interval) < 25 || interval[21:22] != ":" { - return time.Time{}, fmt.Errorf("invalid interval time: %s", interval) - } - - days, err := parseUintChain(interval[0:8], nil) - hours, err := parseUintChain(interval[8:10], err) - mins, err := parseUintChain(interval[10:12], err) - secs, err := parseUintChain(interval[12:14], err) - micros, err := parseUintChain(interval[15:21], err) - - if err != nil { - return time.Time{}, err - } - - var stamp = secs - stamp += days * 86400 - stamp += hours * 3600 - stamp += mins * 60 - - return time.Unix(int64(stamp), int64(micros*1000)), nil -} - -func convertIntervalToDuration(variant *ole.VARIANT) (time.Duration, error) { - var err error - interval, err := extractDateTimeString(variant) - if err != nil || len(interval) == 0 { - return 0, err - } - - t, err := parseIntervalTime(interval) - if err != nil { - return 0, nil - } - - return t.Sub(unixEpoch), nil -} - -func parseUintChain(str string, err error) (uint64, error) { - if err != nil { - return 0, err - } - return strconv.ParseUint(str, 10, 0) -} - -func abs(num int) int { - if num < 0 { - return -num - } - - return num -} diff --git a/pkg/wmiext/enum.go b/pkg/wmiext/enum.go deleted file mode 100644 index 2b6fd69..0000000 --- a/pkg/wmiext/enum.go +++ /dev/null @@ -1,94 +0,0 @@ -//go:build windows -// +build windows - -package wmiext - -import ( - "fmt" - "syscall" - "unsafe" - - "github.com/go-ole/go-ole" -) - -type IEnumWbemClassObjectVtbl struct { - QueryInterface uintptr - AddRef uintptr - Release uintptr - Reset uintptr - Next uintptr - NextAsync uintptr - Clone uintptr - Skip uintptr -} - -type Enum struct { - enum *ole.IUnknown - vTable *IEnumWbemClassObjectVtbl - service *Service -} - -func (e *Enum) Close() { - if e != nil && e.enum != nil { - e.enum.Release() - } -} - -func newEnum(enumerator *ole.IUnknown, service *Service) *Enum { - return &Enum{ - enum: enumerator, - vTable: (*IEnumWbemClassObjectVtbl)(unsafe.Pointer(enumerator.RawVTable)), - service: service, - } -} - -// NextObject obtains the next instance in an enumeration and sets all fields -// of the struct pointer passed through the target parameter. Otherwise, if -// the target parameter is not a struct pointer type, an error will be -// returned. -func NextObject(enum *Enum, target interface{}) (bool, error) { - var err error - - var instance *Instance - if instance, err = enum.Next(); err != nil { - return false, err - } - - if instance == nil { - return true, nil - } - - defer instance.Close() - - return false, instance.GetAll(target) -} - -// Next returns the next object instance in this iteration -func (e *Enum) Next() (instance *Instance, err error) { - var res uintptr - var apObjects *ole.IUnknown - var uReturned uint32 - - res, _, _ = syscall.SyscallN( - e.vTable.Next, // IEnumWbemClassObject::Next() - uintptr(unsafe.Pointer(e.enum)), // IEnumWbemClassObject ptr - uintptr(WBEM_INFINITE), // [in] long lTimeout, - uintptr(1), // [in] ULONG uCount, - uintptr(unsafe.Pointer(&apObjects)), // [out] IWbemClassObject **apObjects, - uintptr(unsafe.Pointer(&uReturned))) // [out] ULONG *puReturned) - if int(res) < 0 { - return nil, NewWmiError(res) - } - - if uReturned < 1 { - switch res { - case WBEM_S_NO_ERROR, WBEM_S_FALSE: - // No more elements - return nil, nil - default: - return nil, fmt.Errorf("failure advancing enumeration (%d)", res) - } - } - - return newInstance(apObjects, e.service), nil -} diff --git a/pkg/wmiext/error.go b/pkg/wmiext/error.go deleted file mode 100644 index ce7a9d1..0000000 --- a/pkg/wmiext/error.go +++ /dev/null @@ -1,202 +0,0 @@ -package wmiext - -import ( - "errors" - "fmt" - "os" - "strings" - "syscall" - "unicode/utf16" - - "golang.org/x/sys/windows" -) - -const ( - WBEM_NO_ERROR = 0 - WBEM_S_NO_ERROR = 0 - WBEM_S_SAME = 0 - WBEM_S_FALSE = 1 - WBEM_S_ALREADY_EXISTS = 0x40001 - WBEM_S_RESET_TO_DEFAULT = 0x40002 - WBEM_S_DIFFERENT = 0x40003 - WBEM_S_TIMEDOUT = 0x40004 - WBEM_S_NO_MORE_DATA = 0x40005 - WBEM_S_OPERATION_CANCELLED = 0x40006 - WBEM_S_PENDING = 0x40007 - WBEM_S_DUPLICATE_OBJECTS = 0x40008 - WBEM_S_ACCESS_DENIED = 0x40009 - WBEM_S_PARTIAL_RESULTS = 0x40010 - WBEM_S_SOURCE_NOT_AVAILABLE = 0x40017 - WBEM_E_FAILED = 0x80041001 - WBEM_E_NOT_FOUND = 0x80041002 - WBEM_E_ACCESS_DENIED = 0x80041003 - WBEM_E_PROVIDER_FAILURE = 0x80041004 - WBEM_E_TYPE_MISMATCH = 0x80041005 - WBEM_E_OUT_OF_MEMORY = 0x80041006 - WBEM_E_INVALID_CONTEXT = 0x80041007 - WBEM_E_INVALID_PARAMETER = 0x80041008 - WBEM_E_NOT_AVAILABLE = 0x80041009 - WBEM_E_CRITICAL_ERROR = 0x8004100a - WBEM_E_INVALID_STREAM = 0x8004100b - WBEM_E_NOT_SUPPORTED = 0x8004100c - WBEM_E_INVALID_SUPERCLASS = 0x8004100d - WBEM_E_INVALID_NAMESPACE = 0x8004100e - WBEM_E_INVALID_OBJECT = 0x8004100f - WBEM_E_INVALID_CLASS = 0x80041010 - WBEM_E_PROVIDER_NOT_FOUND = 0x80041011 - WBEM_E_INVALID_PROVIDER_REGISTRATION = 0x80041012 - WBEM_E_PROVIDER_LOAD_FAILURE = 0x80041013 - WBEM_E_INITIALIZATION_FAILURE = 0x80041014 - WBEM_E_TRANSPORT_FAILURE = 0x80041015 - WBEM_E_INVALID_OPERATION = 0x80041016 - WBEM_E_INVALID_QUERY = 0x80041017 - WBEM_E_INVALID_QUERY_TYPE = 0x80041018 - WBEM_E_ALREADY_EXISTS = 0x80041019 - WBEM_E_OVERRIDE_NOT_ALLOWED = 0x8004101a - WBEM_E_PROPAGATED_QUALIFIER = 0x8004101b - WBEM_E_PROPAGATED_PROPERTY = 0x8004101c - WBEM_E_UNEXPECTED = 0x8004101d - WBEM_E_ILLEGAL_OPERATION = 0x8004101e - WBEM_E_CANNOT_BE_KEY = 0x8004101f - WBEM_E_INCOMPLETE_CLASS = 0x80041020 - WBEM_E_INVALID_SYNTAX = 0x80041021 - WBEM_E_NONDECORATED_OBJECT = 0x80041022 - WBEM_E_READ_ONLY = 0x80041023 - WBEM_E_PROVIDER_NOT_CAPABLE = 0x80041024 - WBEM_E_CLASS_HAS_CHILDREN = 0x80041025 - WBEM_E_CLASS_HAS_INSTANCES = 0x80041026 - WBEM_E_QUERY_NOT_IMPLEMENTED = 0x80041027 - WBEM_E_ILLEGAL_NULL = 0x80041028 - WBEM_E_INVALID_QUALIFIER_TYPE = 0x80041029 - WBEM_E_INVALID_PROPERTY_TYPE = 0x8004102a - WBEM_E_VALUE_OUT_OF_RANGE = 0x8004102b - WBEM_E_CANNOT_BE_SINGLETON = 0x8004102c - WBEM_E_INVALID_CIM_TYPE = 0x8004102d - WBEM_E_INVALID_METHOD = 0x8004102e - WBEM_E_INVALID_METHOD_PARAMETERS = 0x8004102f - WBEM_E_SYSTEM_PROPERTY = 0x80041030 - WBEM_E_INVALID_PROPERTY = 0x80041031 - WBEM_E_CALL_CANCELLED = 0x80041032 - WBEM_E_SHUTTING_DOWN = 0x80041033 - WBEM_E_PROPAGATED_METHOD = 0x80041034 - WBEM_E_UNSUPPORTED_PARAMETER = 0x80041035 - WBEM_E_MISSING_PARAMETER_ID = 0x80041036 - WBEM_E_INVALID_PARAMETER_ID = 0x80041037 - WBEM_E_NONCONSECUTIVE_PARAMETER_IDS = 0x80041038 - WBEM_E_PARAMETER_ID_ON_RETVAL = 0x80041039 - WBEM_E_INVALID_OBJECT_PATH = 0x8004103a - WBEM_E_OUT_OF_DISK_SPACE = 0x8004103b - WBEM_E_BUFFER_TOO_SMALL = 0x8004103c - WBEM_E_UNSUPPORTED_PUT_EXTENSION = 0x8004103d - WBEM_E_UNKNOWN_OBJECT_TYPE = 0x8004103e - WBEM_E_UNKNOWN_PACKET_TYPE = 0x8004103f - WBEM_E_MARSHAL_VERSION_MISMATCH = 0x80041040 - WBEM_E_MARSHAL_INVALID_SIGNATURE = 0x80041041 - WBEM_E_INVALID_QUALIFIER = 0x80041042 - WBEM_E_INVALID_DUPLICATE_PARAMETER = 0x80041043 - WBEM_E_TOO_MUCH_DATA = 0x80041044 - WBEM_E_SERVER_TOO_BUSY = 0x80041045 - WBEM_E_INVALID_FLAVOR = 0x80041046 - WBEM_E_CIRCULAR_REFERENCE = 0x80041047 - WBEM_E_UNSUPPORTED_CLASS_UPDATE = 0x80041048 - WBEM_E_CANNOT_CHANGE_KEY_INHERITANCE = 0x80041049 - WBEM_E_CANNOT_CHANGE_INDEX_INHERITANCE = 0x80041050 - WBEM_E_TOO_MANY_PROPERTIES = 0x80041051 - WBEM_E_UPDATE_TYPE_MISMATCH = 0x80041052 - WBEM_E_UPDATE_OVERRIDE_NOT_ALLOWED = 0x80041053 - WBEM_E_UPDATE_PROPAGATED_METHOD = 0x80041054 - WBEM_E_METHOD_NOT_IMPLEMENTED = 0x80041055 - WBEM_E_METHOD_DISABLED = 0x80041056 - WBEM_E_REFRESHER_BUSY = 0x80041057 - WBEM_E_UNPARSABLE_QUERY = 0x80041058 - WBEM_E_NOT_EVENT_CLASS = 0x80041059 - WBEM_E_MISSING_GROUP_WITHIN = 0x8004105a - WBEM_E_MISSING_AGGREGATION_LIST = 0x8004105b - WBEM_E_PROPERTY_NOT_AN_OBJECT = 0x8004105c - WBEM_E_AGGREGATING_BY_OBJECT = 0x8004105d - WBEM_E_UNINTERPRETABLE_PROVIDER_QUERY = 0x8004105f - WBEM_E_BACKUP_RESTORE_WINMGMT_RUNNING = 0x80041060 - WBEM_E_QUEUE_OVERFLOW = 0x80041061 - WBEM_E_PRIVILEGE_NOT_HELD = 0x80041062 - WBEM_E_INVALID_OPERATOR = 0x80041063 - WBEM_E_LOCAL_CREDENTIALS = 0x80041064 - WBEM_E_CANNOT_BE_ABSTRACT = 0x80041065 - WBEM_E_AMENDED_OBJECT = 0x80041066 - WBEM_E_CLIENT_TOO_SLOW = 0x80041067 - WBEM_E_NULL_SECURITY_DESCRIPTOR = 0x80041068 - WBEM_E_TIMED_OUT = 0x80041069 - WBEM_E_INVALID_ASSOCIATION = 0x8004106a - WBEM_E_AMBIGUOUS_OPERATION = 0x8004106b - WBEM_E_QUOTA_VIOLATION = 0x8004106c - WBEM_E_RESERVED_001 = 0x8004106d - WBEM_E_RESERVED_002 = 0x8004106e - WBEM_E_UNSUPPORTED_LOCALE = 0x8004106f - WBEM_E_HANDLE_OUT_OF_DATE = 0x80041070 - WBEM_E_CONNECTION_FAILED = 0x80041071 - WBEM_E_INVALID_HANDLE_REQUEST = 0x80041072 - WBEM_E_PROPERTY_NAME_TOO_WIDE = 0x80041073 - WBEM_E_CLASS_NAME_TOO_WIDE = 0x80041074 - WBEM_E_METHOD_NAME_TOO_WIDE = 0x80041075 - WBEM_E_QUALIFIER_NAME_TOO_WIDE = 0x80041076 - WBEM_E_RERUN_COMMAND = 0x80041077 - WBEM_E_DATABASE_VER_MISMATCH = 0x80041078 - WBEM_E_VETO_DELETE = 0x80041079 - WBEM_E_VETO_PUT = 0x8004107a - WBEM_E_INVALID_LOCALE = 0x80041080 - WBEM_E_PROVIDER_SUSPENDED = 0x80041081 - WBEM_E_SYNCHRONIZATION_REQUIRED = 0x80041082 - WBEM_E_NO_SCHEMA = 0x80041083 - WBEM_E_PROVIDER_ALREADY_REGISTERED = 0x80041084 - WBEM_E_PROVIDER_NOT_REGISTERED = 0x80041085 - WBEM_E_FATAL_TRANSPORT_ERROR = 0x80041086 - WBEM_E_ENCRYPTED_CONNECTION_REQUIRED = 0x80041087 - WBEM_E_PROVIDER_TIMED_OUT = 0x80041088 - WBEM_E_NO_KEY = 0x80041089 - WBEM_E_PROVIDER_DISABLED = 0x8004108a -) - -var ( - wmiModule syscall.Handle -) - -// VM Lookup errors -var ( - ErrNoResults = errors.New("no results found") -) - -func init() { - file := os.ExpandEnv("${windir}\\system32\\wbem\\wmiutils.dll") - wmiModule, _ = syscall.LoadLibrary(file) -} - -type WmiError struct { - hres uintptr -} - -func NewWmiError(hres uintptr) *WmiError { - return &WmiError{hres} -} - -func (w *WmiError) String() string { - return w.Error() -} - -func (w *WmiError) Code() uintptr { - return w.hres -} - -func (w *WmiError) Error() string { - // ask windows for the remaining errors - var flags uint32 = syscall.FORMAT_MESSAGE_FROM_SYSTEM | - syscall.FORMAT_MESSAGE_FROM_HMODULE | - syscall.FORMAT_MESSAGE_ARGUMENT_ARRAY | - syscall.FORMAT_MESSAGE_IGNORE_INSERTS - - buf := make([]uint16, 300) - n, err := windows.FormatMessage(flags, uintptr(wmiModule), uint32(w.hres), 0, buf, nil) - if err != nil { - return fmt.Sprintf("WMI error [%d]: FormatMessage failed with: %v", w.hres, err) - } - - return fmt.Sprintf("WMI error [%d]: %s", w.hres, strings.TrimRight(string(utf16.Decode(buf[:n])), "\r\n")) -} diff --git a/pkg/wmiext/init.go b/pkg/wmiext/init.go deleted file mode 100644 index 35c5b82..0000000 --- a/pkg/wmiext/init.go +++ /dev/null @@ -1,108 +0,0 @@ -//go:build windows -// +build windows - -package wmiext - -import ( - "github.com/go-ole/go-ole" - "github.com/sirupsen/logrus" - "golang.org/x/sys/windows" -) - -var ( - ole32 = windows.NewLazySystemDLL("ole32.dll") - procCoSetProxyBlanket = ole32.NewProc("CoSetProxyBlanket") - procCoInitializeSecurity = ole32.NewProc("CoInitializeSecurity") - - modoleaut32 = windows.NewLazySystemDLL("oleaut32.dll") - procSafeArrayCreateVector = modoleaut32.NewProc("SafeArrayCreateVector") - procSafeArrayPutElement = modoleaut32.NewProc("SafeArrayPutElement") - procSafeArrayGetElement = modoleaut32.NewProc("SafeArrayGetElement") - procSafeArrayDestroy = modoleaut32.NewProc("SafeArrayDestroy") - - clsidWbemObjectTextSrc = ole.NewGUID("{8d1c559d-84f0-4bb3-a7d5-56a7435a9ba6}") - iidIWbemObjectTextSrc = ole.NewGUID("{bfbf883a-cad7-11d3-a11b-00105a1f515a}") - - wmiWbemTxtLocator *ole.IUnknown - wmiWbemLocator *ole.IUnknown - - clsidWbemLocator = ole.NewGUID("4590f811-1d3a-11d0-891f-00aa004b2e24") - iidIWbemLocator = ole.NewGUID("dc12a687-737f-11cf-884d-00aa004b2e24") -) - -const ( - // WMI Generic flags - WBEM_FLAG_RETURN_WBEM_COMPLETE = 0x0 - WBEM_FLAG_RETURN_IMMEDIATELY = 0x10 - WBEM_FLAG_FORWARD_ONLY = 0x20 - - // WMI Query flags - WBEM_FLAG_SHALLOW = 1 - - // Timeout flags - WBEM_NO_WAIT = 0 - WBEM_INFINITE = 0xFFFFFFFF - - // COM Auth Flags - EOAC_NONE = 0 - - // RPC Authentication - RPC_C_AUTHN_WINNT = 10 - - // RPC Authentication Level - RPC_C_AUTHN_LEVEL_DEFAULT = 0 - RPC_C_AUTHN_LEVEL_CALL = 3 - - // RPC Authorization - RPC_C_AUTHZ_NONE = 0 - - // RPC Impersonation - RPC_C_IMP_LEVEL_IMPERSONATE = 3 -) - -func init() { - var err error - - err = ole.CoInitializeEx(0, ole.COINIT_MULTITHREADED) - if err != nil { - if oleCode, ok := err.(*ole.OleError); ok { - code := oleCode.Code() - // 1 = Already init - if code != 0 && code != 1 { - logrus.Errorf("Unable to initialize COM: %s", err.Error()) - return - } - } - } - - initSecurity() - - wmiWbemLocator, err = ole.CreateInstance(clsidWbemLocator, iidIWbemLocator) - if err != nil { - logrus.Errorf("Could not initialize Wbem components, WMI operations will likely fail %s", err.Error()) - } - - // IID_IWbemObjectTextSrc Obtain the initial locator to WMI - wmiWbemTxtLocator, err = ole.CreateInstance(clsidWbemObjectTextSrc, iidIWbemObjectTextSrc) - if err != nil { - logrus.Errorf("Could not initialize Wbem components, WMI operations will likely fail %s", err.Error()) - } -} - -func initSecurity() { - var svc int32 = -1 - - res, _, _ := procCoInitializeSecurity.Call( // CoInitializeSecurity - uintptr(0), // [in, optional] PSECURITY_DESCRIPTOR pSecDesc, - uintptr(svc), // [in] LONG cAuthSvc, - uintptr(0), // [in, optional] SOLE_AUTHENTICATION_SERVICE *asAuthSvc, - uintptr(0), // [in, optional] void *pReserved1, - uintptr(RPC_C_AUTHN_LEVEL_DEFAULT), // [in] DWORD dwAuthnLevel, - uintptr(RPC_C_IMP_LEVEL_IMPERSONATE), // [in] DWORD dwImpLevel, - uintptr(0), // [in, optional] void *pAuthList, - uintptr(EOAC_NONE), // [in] DWORD dwCapabilities, - uintptr(0)) // [in, optional] void *pReserved3 - if int(res) < 0 { - logrus.Errorf("Unable to initialize COM security: %s", NewWmiError(res).Error()) - } -} diff --git a/pkg/wmiext/instance.go b/pkg/wmiext/instance.go deleted file mode 100644 index b60d636..0000000 --- a/pkg/wmiext/instance.go +++ /dev/null @@ -1,652 +0,0 @@ -//go:build windows -// +build windows - -package wmiext - -import ( - "errors" - "fmt" - "reflect" - "strconv" - "strings" - "syscall" - "time" - "unsafe" - - "github.com/go-ole/go-ole" - "github.com/sirupsen/logrus" -) - -const ( - WmiPathKey = "__PATH" -) - -var ( - WindowsEpoch = time.Date(1601, 1, 1, 0, 0, 0, 0, time.UTC) -) - -type Instance struct { - object *ole.IUnknown - vTable *IWbemClassObjectVtbl - service *Service -} - -type IWbemClassObjectVtbl struct { - QueryInterface uintptr - AddRef uintptr - Release uintptr - GetQualifierSet uintptr - Get uintptr - Put uintptr - Delete uintptr - GetNames uintptr - BeginEnumeration uintptr - Next uintptr - EndEnumeration uintptr - GetPropertyQualifierSet uintptr - Clone uintptr - GetObjectText uintptr - SpawnDerivedClass uintptr - SpawnInstance uintptr - CompareTo uintptr - GetPropertyOrigin uintptr - InheritsFrom uintptr - GetMethod uintptr - PutMethod uintptr - DeleteMethod uintptr - BeginMethodEnumeration uintptr - NextMethod uintptr - EndMethodEnumeration uintptr - GetMethodQualifierSet uintptr - GetMethodOrigin uintptr -} - -type CIMTYPE_ENUMERATION uint32 - -const ( - CIM_ILLEGAL CIMTYPE_ENUMERATION = 0xFFF - CIM_EMPTY CIMTYPE_ENUMERATION = 0 - CIM_SINT8 CIMTYPE_ENUMERATION = 16 - CIM_UINT8 CIMTYPE_ENUMERATION = 17 - CIM_SINT16 CIMTYPE_ENUMERATION = 2 - CIM_UINT16 CIMTYPE_ENUMERATION = 18 - CIM_SINT32 CIMTYPE_ENUMERATION = 3 - CIM_UINT32 CIMTYPE_ENUMERATION = 19 - CIM_SINT64 CIMTYPE_ENUMERATION = 20 - CIM_UINT64 CIMTYPE_ENUMERATION = 21 - CIM_REAL32 CIMTYPE_ENUMERATION = 4 - CIM_REAL64 CIMTYPE_ENUMERATION = 5 - CIM_BOOLEAN CIMTYPE_ENUMERATION = 11 - CIM_STRING CIMTYPE_ENUMERATION = 8 - CIM_DATETIME CIMTYPE_ENUMERATION = 101 - CIM_REFERENCE CIMTYPE_ENUMERATION = 102 - CIM_CHAR16 CIMTYPE_ENUMERATION = 103 - CIM_OBJECT CIMTYPE_ENUMERATION = 13 - CIM_FLAG_ARRAY CIMTYPE_ENUMERATION = 0x2000 -) - -type WBEM_FLAVOR_TYPE uint32 - -const ( - WBEM_FLAVOR_DONT_PROPAGATE WBEM_FLAVOR_TYPE = 0 - WBEM_FLAVOR_FLAG_PROPAGATE_TO_INSTANCE WBEM_FLAVOR_TYPE = 0x1 - WBEM_FLAVOR_FLAG_PROPAGATE_TO_DERIVED_CLASS WBEM_FLAVOR_TYPE = 0x2 - WBEM_FLAVOR_MASK_PROPAGATION WBEM_FLAVOR_TYPE = 0xf - WBEM_FLAVOR_OVERRIDABLE WBEM_FLAVOR_TYPE = 0 - WBEM_FLAVOR_NOT_OVERRIDABLE WBEM_FLAVOR_TYPE = 0x10 - WBEM_FLAVOR_MASK_PERMISSIONS WBEM_FLAVOR_TYPE = 0x10 - WBEM_FLAVOR_ORIGIN_LOCAL WBEM_FLAVOR_TYPE = 0 - WBEM_FLAVOR_ORIGIN_PROPAGATED WBEM_FLAVOR_TYPE = 0x20 - WBEM_FLAVOR_ORIGIN_SYSTEM WBEM_FLAVOR_TYPE = 0x40 - WBEM_FLAVOR_MASK_ORIGIN WBEM_FLAVOR_TYPE = 0x60 - WBEM_FLAVOR_NOT_AMENDED WBEM_FLAVOR_TYPE = 0 - WBEM_FLAVOR_AMENDED WBEM_FLAVOR_TYPE = 0x80 - WBEM_FLAVOR_MASK_AMENDED WBEM_FLAVOR_TYPE = 0x80 -) - -func newInstance(object *ole.IUnknown, service *Service) *Instance { - instance := &Instance{ - object: object, - vTable: (*IWbemClassObjectVtbl)(unsafe.Pointer(object.RawVTable)), - service: service, - } - - return instance -} - -// Close cleans up all memory associated with this instance. -func (i *Instance) Close() { - if i != nil && i.object != nil { - i.object.Release() - } -} - -// GetClassName Gets the WMI class name for this WMI object instance -func (i *Instance) GetClassName() (className string, err error) { - return i.GetAsString(`__CLASS`) -} - -// Path gets the WMI object path of this instance -func (i *Instance) Path() (string, error) { - ref, _, _, err := i.GetAsAny(WmiPathKey) - return ref.(string), err -} - -// IsReferenceProperty returns whether the property is of type CIM_REFERENCE, a string which points to -// an object path of another instance. -func (i *Instance) IsReferenceProperty(name string) (bool, error) { - _, cimType, _, err := i.GetAsAny(name) - return cimType == CIM_REFERENCE, err -} - -// SpawnInstance create a new WMI object instance that is zero-initialized. The returned instance -// will not respect expected default values, which must be populated by other means. -func (i *Instance) SpawnInstance() (instance *Instance, err error) { - var res uintptr - var newUnknown *ole.IUnknown - - res, _, _ = syscall.SyscallN( - i.vTable.SpawnInstance, // IWbemClassObject::SpawnInstance( - uintptr(unsafe.Pointer(i.object)), // IWbemClassObject ptr - uintptr(0), // [in] long lFlags, - uintptr(unsafe.Pointer(&newUnknown))) // [out] IWbemClassObject **ppNewInstance) - if res != 0 { - return nil, NewWmiError(res) - } - - return newInstance(newUnknown, i.service), nil -} - -// CloneInstance create a new cloned copy of this WMI instance. -func (i *Instance) CloneInstance() (*Instance, error) { - classObj := i.object - vTable := (*IWbemClassObjectVtbl)(unsafe.Pointer(classObj.RawVTable)) - var cloned *ole.IUnknown - - ret, _, _ := syscall.SyscallN( - vTable.Clone, // IWbemClassObject::Clone( - uintptr(unsafe.Pointer(classObj)), // IWbemClassObject ptr - uintptr(unsafe.Pointer(&cloned))) // [out] IWbemClassObject **ppCopy) - if ret != 0 { - return nil, NewWmiError(ret) - } - - return newInstance(cloned, i.service), nil -} - -// PutAll sets all fields of this instance to the passed src parameter's fields, converting accordingly. -// The src parameter must be a pointer to a struct, otherwise an error will be returned. -func (i *Instance) PutAll(src interface{}) error { - val := reflect.ValueOf(src) - if val.Kind() == reflect.Pointer { - val = val.Elem() - } - - if val.Kind() != reflect.Struct { - return errors.New("not a struct or pointer to struct") - } - - props, err := i.GetAllProperties() - if err != nil { - return err - } - - return i.instancePutAllTraverse(val, props) -} - -func (i *Instance) instancePutAllTraverse(val reflect.Value, propMap map[string]interface{}) error { - for j := 0; j < val.NumField(); j++ { - fieldVal := val.Field(j) - fieldType := val.Type().Field(j) - - if fieldType.Type.Kind() == reflect.Struct && fieldType.Anonymous { - if err := i.instancePutAllTraverse(fieldVal, propMap); err != nil { - return err - } - continue - } - if strings.HasPrefix(fieldType.Name, "S__") { - continue - } - - if !fieldType.IsExported() { - continue - } - - if _, exists := propMap[fieldType.Name]; !exists { - continue - } - - if fieldVal.Kind() == reflect.String && fieldVal.Len() == 0 { - continue - } - - if err := i.Put(fieldType.Name, fieldVal.Interface()); err != nil { - return err - } - } - - return nil -} - -// Put sets the specified property to the passed Golang value, converting appropriately. -func (i *Instance) Put(name string, value interface{}) (err error) { - var variant ole.VARIANT - - switch cast := value.(type) { - case ole.VARIANT: - variant = cast - case *ole.VARIANT: - variant = *cast - default: - variant, err = NewAutomationVariant(value) - if err != nil { - return err - } - } - - var wszName *uint16 - if wszName, err = syscall.UTF16PtrFromString(name); err != nil { - return - } - - classObj := i.object - vTable := (*IWbemClassObjectVtbl)(unsafe.Pointer(classObj.RawVTable)) - res, _, _ := syscall.SyscallN( - vTable.Put, // IWbemClassObject::Put( - uintptr(unsafe.Pointer(classObj)), // IWbemClassObject ptr - uintptr(unsafe.Pointer(wszName)), // [in] LPCWSTR wszName, - uintptr(0), // [in] long lFlags, - uintptr(unsafe.Pointer(&variant)), // [in] VARIANT *pVal, - uintptr(0)) // [in] CIMTYPE Type) - if res != 0 { - return NewWmiError(res) - } - - _ = variant.Clear() - return -} - -// GetCimText returns the CIM XML representation of this instance. Some WMI methods use a string -// parameter to represent a full complex object, and this method is used to generate -// the expected format. -func (i *Instance) GetCimText() string { - type wmiWbemTxtSrcVtable struct { - QueryInterface uintptr - AddRef uintptr - Release uintptr - GetTxt uintptr - } - const CIM_XML_FORMAT = 1 - - classObj := i.object - - vTable := (*wmiWbemTxtSrcVtable)(unsafe.Pointer(wmiWbemTxtLocator.RawVTable)) - var retString *uint16 - res, _, _ := syscall.SyscallN( - vTable.GetTxt, // IWbemObjectTextSrc::GetText() - uintptr(unsafe.Pointer(wmiWbemLocator)), // IWbemObjectTextSrc ptr - uintptr(0), // [in] long lFlags - uintptr(unsafe.Pointer(classObj)), // [in] IWbemClassObject *pObj - uintptr(CIM_XML_FORMAT), // [in] ULONG uObjTextFormat, - uintptr(0), // [in] IWbemContext *pCtx, - uintptr(unsafe.Pointer(&retString))) // [out] BSTR *strText) - if res != 0 { - return "" - } - itemStr := ole.BstrToString(retString) - return itemStr -} - -// GetAll gets all fields that map to a target struct and populates all struct fields according to -// the expected type information. The target parameter should be a pointer to a struct, and -// will return an error otherwise. -func (i *Instance) GetAll(target interface{}) error { - elem := reflect.ValueOf(target) - if elem.Kind() != reflect.Ptr || elem.IsNil() { - return errors.New("invalid destination type for mapping a WMI instance to an object") - } - - // deref pointer - elem = elem.Elem() - var err error - - if err = i.BeginEnumeration(); err != nil { - return err - } - - properties := make(map[string]*ole.VARIANT) - - for { - var name string - var value *ole.VARIANT - var done bool - - if done, name, value, _, _, err = i.NextAsVariant(); err != nil { - return err - } - - if done { - break - } - - if value != nil { - properties[name] = value - } - } - - defer func() { - for _, v := range properties { - _ = v.Clear() - } - }() - - _ = i.EndEnumeration() - - return i.instanceGetAllPopulate(elem, elem.Type(), properties) -} - -// GetAsAny gets a property and converts it to a Golang type that matches the internal -// variant automation type passed back from WMI. For usage with predictable static -// type mapping, use GetAsString(), GetAsUint(), or GetAll() instead of this method. -func (i *Instance) GetAsAny(name string) (interface{}, CIMTYPE_ENUMERATION, WBEM_FLAVOR_TYPE, error) { - variant, cimType, flavor, err := i.GetAsVariant(name) - if err != nil { - return nil, cimType, flavor, err - } - - defer func() { - if err := variant.Clear(); err != nil { - logrus.Error(err) - } - }() - - // Since there is no type information only perform the stock conversion - result := convertToGenericValue(variant) - - return result, cimType, flavor, err -} - -// GetAsString gets a property value as a string value, converting if necessary -func (i *Instance) GetAsString(name string) (value string, err error) { - variant, _, _, err := i.GetAsVariant(name) - if err != nil || variant == nil { - return "", err - } - defer func() { - if err := variant.Clear(); err != nil { - logrus.Error(err) - } - }() - - // TODO: replace with something better - return fmt.Sprintf("%v", convertToGenericValue(variant)), nil -} - -// GetAsUint gets a property value as a uint value, if conversion is possible. Otherwise, -// returns an error. -func (i *Instance) GetAsUint(name string) (uint, error) { - val, _, _, err := i.GetAsAny(name) - if err != nil { - return 0, err - } - - switch ret := val.(type) { - case int: - return uint(ret), nil - case int8: - return uint(ret), nil - case int16: - return uint(ret), nil - case int32: - return uint(ret), nil - case int64: - return uint(ret), nil - case uint: - return ret, nil - case uint8: - return uint(ret), nil - case uint16: - return uint(ret), nil - case uint32: - return uint(ret), nil - case uint64: - return uint(ret), nil - case string: - parse, err := strconv.ParseUint(ret, 10, 64) - return uint(parse), err - default: - return 0, fmt.Errorf("type conversion from %T on param %s not supported", val, name) - } -} - -// GetAsVariant obtains a specified property value, if it exists. -func (i *Instance) GetAsVariant(name string) (*ole.VARIANT, CIMTYPE_ENUMERATION, WBEM_FLAVOR_TYPE, error) { - var variant ole.VARIANT - var err error - var wszName *uint16 - var cimType CIMTYPE_ENUMERATION - var flavor WBEM_FLAVOR_TYPE - - if wszName, err = syscall.UTF16PtrFromString(name); err != nil { - return nil, 0, 0, err - } - - classObj := i.object - vTable := (*IWbemClassObjectVtbl)(unsafe.Pointer(classObj.RawVTable)) - - res, _, _ := syscall.SyscallN( - vTable.Get, // IWbemClassObject::Get( - uintptr(unsafe.Pointer(classObj)), // IWbemClassObject ptr - uintptr(unsafe.Pointer(wszName)), // [in] LPCWSTR wszName, - uintptr(0), // [in] long lFlags, - uintptr(unsafe.Pointer(&variant)), // [out] VARIANT *pVal, - uintptr(unsafe.Pointer(&cimType)), // [out, optional] CIMTYPE *pType, - uintptr(unsafe.Pointer(&flavor))) // [out, optional] long *plFlavor) - if res != 0 { - return nil, 0, 0, NewWmiError(res) - } - - return &variant, cimType, flavor, nil -} - -// Next retrieves the next property as a Golang type when iterating the properties using an enumerator -// created by BeginEnumeration(). The returned value's type represents the internal automation type -// used by WMI. It is usually preferred to use GetAsXXX(), GetAll(), or GetAll Properties() over this -// method. -func (i *Instance) Next() (done bool, name string, value interface{}, cimType CIMTYPE_ENUMERATION, flavor WBEM_FLAVOR_TYPE, err error) { - var variant *ole.VARIANT - done, name, variant, cimType, flavor, err = i.NextAsVariant() - - if err == nil && !done { - defer func() { - if err := variant.Clear(); err != nil { - logrus.Error(err) - } - }() - value = convertToGenericValue(variant) - } - - return -} - -// NextAsVariant retrieves the next property as a VARIANT type when iterating the properties using an enumerator -// created by BeginEnumeration(). The returned value's type represents the internal automation type -// used by WMI. It is usually preferred to use GetAsXXX(), GetAll(), or GetAllProperties() over this -// method. Callers are responsible for clearing the VARIANT, otherwise associated memory will leak. -func (i *Instance) NextAsVariant() (bool, string, *ole.VARIANT, CIMTYPE_ENUMERATION, WBEM_FLAVOR_TYPE, error) { - var res uintptr - var strName *uint16 - var variant ole.VARIANT - var cimType CIMTYPE_ENUMERATION - var flavor WBEM_FLAVOR_TYPE - - res, _, _ = syscall.SyscallN( - i.vTable.Next, // IWbemClassObject::Next( - uintptr(unsafe.Pointer(i.object)), // IWbemClassObject ptr - uintptr(0), // [in] long lFlags, - uintptr(unsafe.Pointer(&strName)), // [out] BSTR *strName, - uintptr(unsafe.Pointer(&variant)), // [out] VARIANT *pVal, - uintptr(unsafe.Pointer(&cimType)), // [out, optional] CIMTYPE *pType, - uintptr(unsafe.Pointer(&flavor))) // [out, optional] long *plFlavor - if int(res) < 0 { - return false, "", nil, cimType, flavor, NewWmiError(res) - } - - if res == WBEM_S_NO_MORE_DATA { - return true, "", nil, cimType, flavor, nil - } - - defer ole.SysFreeString((*int16)(unsafe.Pointer(strName))) //nolint:errcheck - name := ole.BstrToString(strName) - - return false, name, &variant, cimType, flavor, nil -} - -// GetAllProperties gets all properties on this instance. The returned map is keyed by the field name and the value -// is a Golang type which matches the WMI internal implementation. For static type conversions, -// it's recommended to use either GetAll(), which uses struct fields for type information, or -// the GetAsXXX() methods. -func (i *Instance) GetAllProperties() (map[string]interface{}, error) { - var err error - properties := make(map[string]interface{}) - - if err = i.BeginEnumeration(); err != nil { - return nil, err - } - - defer func() { - if err := i.EndEnumeration(); err != nil { - logrus.Error(err) - } - }() - - for { - var name string - var value interface{} - var done bool - - if done, name, value, _, _, err = i.Next(); err != nil || done { - return properties, err - } - - properties[name] = value - } -} - -// GetMethodParameters returns a WMI class object which represents the [in] method parameters for a method invocation. -// This is an advanced method, used for dynamic introspection or manual method invocation. In most -// cases it is recommended to use BeginInvoke() instead, which constructs the parameter payload -// automatically. -func (i *Instance) GetMethodParameters(method string) (*Instance, error) { - var err error - var res uintptr - var inSignature *ole.IUnknown - - var wszName *uint16 - if wszName, err = syscall.UTF16PtrFromString(method); err != nil { - return nil, err - } - - res, _, _ = syscall.SyscallN( - i.vTable.GetMethod, // IWbemClassObject::GetMethod( - uintptr(unsafe.Pointer(i.object)), // IWbemClassObject ptr - uintptr(unsafe.Pointer(wszName)), // [in] LPCWSTR wszName - uintptr(0), // [in] long lFlags, - uintptr(unsafe.Pointer(&inSignature)), // [out] IWbemClassObject **ppInSignature, - uintptr(0)) // [out] IWbemClassObject **ppOutSignature) - if res != 0 { - return nil, NewWmiError(res) - } - - return newInstance(inSignature, i.service), nil -} - -func (i *Instance) instanceGetAllPopulate(elem reflect.Value, elemType reflect.Type, properties map[string]*ole.VARIANT) error { - var err error - - for j := 0; j < elemType.NumField(); j++ { - fieldType := elemType.Field(j) - fieldVal := elem.Field(j) - - if !fieldType.IsExported() { - continue - } - - if fieldType.Type.Kind() == reflect.Struct && fieldType.Anonymous { - if err := i.instanceGetAllPopulate(fieldVal, fieldType.Type, properties); err != nil { - return err - } - continue - } - - fieldName := fieldType.Name - - if strings.HasPrefix(fieldName, "S__") { - fieldName = fieldName[1:] - } - if variant, ok := properties[fieldName]; ok { - var val interface{} - if val, err = convertToGoType(variant, fieldVal, fieldType.Type); err != nil { - return err - } - - if val != nil { - fieldVal.Set(reflect.ValueOf(val)) - } - } - } - - return nil -} - -// BeginEnumeration begins iterating the property list on this instance. This is an advanced method. -// In most cases, the GetAsXXX() methods, GetAll(), and GetAllProperties() methods should be -// preferred. -func (i *Instance) BeginEnumeration() error { - classObj := i.object - vTable := (*IWbemClassObjectVtbl)(unsafe.Pointer(classObj.RawVTable)) - - result, _, _ := syscall.SyscallN( - vTable.BeginEnumeration, // IWbemClassObject::BeginEnumeration( - uintptr(unsafe.Pointer(classObj)), // IWbemClassObject ptr, - uintptr(0)) // [in] long lEnumFlags) // 0 = defaults - if result != 0 { - return NewWmiError(result) - } - - return nil -} - -// EndEnumeration completes iterating a property list on this instance. This is an advanced method. -// In most cases, the GetAsXXX() methods, GetAll(), and GetAllProperties() methods -// should be preferred. -func (i *Instance) EndEnumeration() error { - res, _, _ := syscall.SyscallN( - i.vTable.EndEnumeration, // IWbemClassObject::EndEnumeration( - uintptr(unsafe.Pointer(i.object))) // IWbemClassObject ptr) - if res != 0 { - return NewWmiError(res) - } - - return nil -} - -// BeginInvoke invokes a method on this Instance. Returns a MethodExecutor builder object -// that is used to construct the input parameters (via calls to In()), perform the -// invocation (using calls to Execute()), retrieve output parameters (via calls to -// Out()), and finally the method return value (using a call to End()) -func (i *Instance) BeginInvoke(method string) *MethodExecutor { - objPath, err := i.Path() - if err != nil { - return &MethodExecutor{err: err} - } - - var class, inParam *Instance - if class, err = i.service.GetClassInstance(i); err == nil { - inParam, err = class.GetMethodParameters(method) - class.Close() - } - - return &MethodExecutor{method: method, path: objPath, service: i.service, inParam: inParam, err: err} -} diff --git a/pkg/wmiext/invoke.go b/pkg/wmiext/invoke.go deleted file mode 100644 index 6875e2d..0000000 --- a/pkg/wmiext/invoke.go +++ /dev/null @@ -1,132 +0,0 @@ -//go:build windows -// +build windows - -package wmiext - -import ( - "fmt" - "reflect" - - "github.com/go-ole/go-ole" - "github.com/sirupsen/logrus" -) - -type MethodExecutor struct { - err error - path string - method string - service *Service - inParam *Instance - outParam *Instance -} - -// In sets an input parameter for the method of this invocation, converting appropriately -func (e *MethodExecutor) In(name string, value interface{}) *MethodExecutor { - if e.err == nil && e.inParam != nil { - switch t := value.(type) { - case *Instance: - var ref bool - if ref, e.err = e.inParam.IsReferenceProperty(name); e.err != nil { - return e - } - if !ref { - // Embedded Object - break - } - if value, e.err = t.Path(); e.err != nil { - return e - } - } - - e.err = e.inParam.Put(name, value) - } - - return e -} - -// Out sets the specified output parameter, and assigns the value parameter to the result. -// The value parameter must be a reference to the field that should be set. -func (e *MethodExecutor) Out(name string, value interface{}) *MethodExecutor { - if e.err == nil && e.outParam != nil { - var variant *ole.VARIANT - var cimType CIMTYPE_ENUMERATION - var result interface{} - dest := reflect.ValueOf(value) - if dest.Kind() != reflect.Ptr { - e.err = fmt.Errorf("Out() on %q called with %T, out parameters must be a reference", name, value) - return e - } - dest = dest.Elem() - - variant, cimType, _, e.err = e.outParam.GetAsVariant(name) - if e.err != nil || variant == nil { - return e - } - - defer func() { - if err := variant.Clear(); err != nil { - logrus.Error(err) - } - }() - - if _, ok := value.(**Instance); ok && cimType == CIM_REFERENCE { - path := variant.ToString() - result, e.err = e.service.GetObject(path) - if e.err != nil { - return e - } - } else { - target := reflect.ValueOf(value).Elem() - result, e.err = convertToGoType(variant, target, target.Type()) - if e.err != nil { - return e - } - } - - newValue := reflect.ValueOf(result) - if result == nil { - // Nil must be typed to the destination - newValue = reflect.Zero(dest.Type()) - } - - dest.Set(newValue) - } - return e -} - -// Execute executes the method after in parameters have been specified using In() -func (e *MethodExecutor) Execute() *MethodExecutor { - defer e.cleanupInputs() - - if e.err == nil { - e.outParam, e.err = e.service.ExecMethod(e.path, e.method, e.inParam) - } - - return e -} - -func (e *MethodExecutor) cleanupInputs() { - if e.inParam != nil { - e.inParam.Close() - e.inParam = nil - } -} - -// End completes the method invocation and returns an error indicating the return -// code of the underlying method -func (e *MethodExecutor) End() error { - e.cleanupInputs() - - if e.outParam != nil { - e.outParam.Close() - e.outParam = nil - } - - return e.err -} - -// Obtains the last error that occurred while building the invocation. Once -// an error has occurred, all future operations are treated as a no-op. -func (e *MethodExecutor) Error() error { - return e.err -} diff --git a/pkg/wmiext/job.go b/pkg/wmiext/job.go deleted file mode 100644 index 180c1e5..0000000 --- a/pkg/wmiext/job.go +++ /dev/null @@ -1,53 +0,0 @@ -//go:build windows -// +build windows - -package wmiext - -import ( - "fmt" - "time" -) - -type JobError struct { - ErrorCode int -} - -func (err *JobError) Error() string { - return fmt.Sprintf("Job failed with error code: %d", err.ErrorCode) -} - -// WaitJob waits on the specified job instance until it has completed and -// returns a JobError containing the result code in the event of -// a failure. -func WaitJob(service *Service, job *Instance) error { - var jobs []*Instance - defer func() { - for _, job := range jobs { - job.Close() - } - }() - for { - state, _, _, err := job.GetAsAny("JobState") - if err != nil { - return err - } - time.Sleep(100 * time.Millisecond) - job, _ = service.RefetchObject(job) - jobs = append(jobs, job) - // 7+ = completed - if state.(int32) >= 7 { - break - } - } - - result, _, _, err := job.GetAsAny("ErrorCode") - if err != nil { - return err - } - - if result.(int32) != 0 { - return &JobError{ErrorCode: int(result.(int32))} - } - - return nil -} diff --git a/pkg/wmiext/service.go b/pkg/wmiext/service.go deleted file mode 100644 index c58d06c..0000000 --- a/pkg/wmiext/service.go +++ /dev/null @@ -1,416 +0,0 @@ -//go:build windows -// +build windows - -package wmiext - -import ( - "errors" - "fmt" - "syscall" - "unsafe" - - "github.com/go-ole/go-ole" -) - -type IWbemLocatorVtbl struct { - QueryInterface uintptr - AddRef uintptr - Release uintptr - ConnectServer uintptr -} - -type Service struct { - service *ole.IUnknown - vTable *IWbemServicesVtbl -} - -type IWbemServicesVtbl struct { - QueryInterface uintptr - AddRef uintptr - Release uintptr - OpenNamespace uintptr - CancelAsyncCall uintptr - QueryObjectSink uintptr - GetObject uintptr - GetObjectAsync uintptr - PutClass uintptr - PutClassAsync uintptr - DeleteClass uintptr - DeleteClassAsync uintptr - CreateClassEnum uintptr - CreateClassEnumAsync uintptr - PutInstance uintptr - PutInstanceAsync uintptr - DeleteInstance uintptr - DeleteInstanceAsync uintptr - CreateInstanceEnum uintptr - CreateInstanceEnumAsync uintptr - ExecQuery uintptr - ExecQueryAsync uintptr - ExecNotificationQuery uintptr - ExecNotificationQueryAsync uintptr - ExecMethod uintptr - ExecMethodAsync uintptr -} - -func connectService(namespace string) (*Service, error) { - - if wmiWbemLocator == nil { - return nil, errors.New("WMI failed initialization, service calls can not proceed") - } - - var err error - var res uintptr - var strResource *uint16 - var strLocale *uint16 - var service *ole.IUnknown - - loc := fmt.Sprintf(`\\.\%s`, namespace) - - if strResource, err = syscall.UTF16PtrFromString(loc); err != nil { - return nil, err - } - - // Connect with en_US LCID since we do pattern matching against English key values - if strLocale, err = syscall.UTF16PtrFromString("MS_409"); err != nil { - return nil, err - } - - myVTable := (*IWbemLocatorVtbl)(unsafe.Pointer(wmiWbemLocator.RawVTable)) - res, _, _ = syscall.SyscallN( - myVTable.ConnectServer, // IWbemLocator::ConnectServer( - uintptr(unsafe.Pointer(wmiWbemLocator)), // IWbemLocator ptr - uintptr(unsafe.Pointer(strResource)), // [in] const BSTR strNetworkResource, - uintptr(0), // [in] const BSTR strUser, - uintptr(0), // [in] const BSTR strPassword, - uintptr(unsafe.Pointer(strLocale)), // [in] const BSTR strLocale, - uintptr(WBEM_FLAG_CONNECT_USE_MAX_WAIT), // [in] long lSecurityFlags, - uintptr(0), // [in] const BSTR strAuthority, - uintptr(0), // [in] IWbemContext *pCtx, - uintptr(unsafe.Pointer(&service))) // [out] IWbemServices **ppNamespace) - - if res != 0 { - return nil, NewWmiError(res) - } - - if err = CoSetProxyBlanket(service); err != nil { - return nil, err - } - - return newService(service), nil -} - -func newService(service *ole.IUnknown) *Service { - return &Service{ - service: service, - vTable: (*IWbemServicesVtbl)(unsafe.Pointer(service.RawVTable)), - } -} - -const ( - WBEM_FLAG_CONNECT_USE_MAX_WAIT = 0x80 -) - -func CoSetProxyBlanket(service *ole.IUnknown) (err error) { - res, _, _ := procCoSetProxyBlanket.Call( //CoSetProxyBlanket( - uintptr(unsafe.Pointer(service)), // [in] IUnknown *pProxy, - uintptr(RPC_C_AUTHN_WINNT), // [in] DWORD dwAuthnSvc, - uintptr(RPC_C_AUTHZ_NONE), // [in] DWORD dwAuthzSvc, - uintptr(0), // [in, opt] OLECHAR *pServerPrincName, - uintptr(RPC_C_AUTHN_LEVEL_CALL), // [in] DWORD dwAuthnLevel, - uintptr(RPC_C_IMP_LEVEL_IMPERSONATE), // [in] DWORD dwImpLevel, - uintptr(0), // [in, opt] RPC_AUTH_IDENTITY_HANDLE pAuthInfo, - uintptr(EOAC_NONE)) // [in] DWORD dwCapabilities) - - if res != 0 { - return NewWmiError(res) - } - - return nil -} - -// NewLocalService creates a service and connect it to the local system at the specified namespace -func NewLocalService(namespace string) (s *Service, err error) { - return connectService(namespace) -} - -// Close frees all associated memory with this service -func (s *Service) Close() { - if s != nil && s.service != nil { - s.service.Release() - } -} - -// ExecQuery executes a WQL query and returns an enumeration to iterate the result set. -// Queries are executed in a semi-synchronous fashion. -func (s *Service) ExecQuery(wqlQuery string) (*Enum, error) { - var err error - var pEnum *ole.IUnknown - var strQuery *uint16 - var strQL *uint16 - - if strQL, err = syscall.UTF16PtrFromString("WQL"); err != nil { - return nil, err - } - - if strQuery, err = syscall.UTF16PtrFromString(wqlQuery); err != nil { - return nil, err - } - - // Semisynchronous mode = return immed + forward (for perf) - flags := WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY - - hres, _, _ := syscall.SyscallN( - s.vTable.ExecQuery, // IWbemServices::ExecQuery( - uintptr(unsafe.Pointer(s.service)), // IWbemServices ptr - uintptr(unsafe.Pointer(strQL)), // [in] const BSTR strQueryLanguage, - uintptr(unsafe.Pointer(strQuery)), // [in] const BSTR strQuery, - uintptr(flags), // [in] long lFlags, - uintptr(0), // [in] IWbemContext *pCtx, - uintptr(unsafe.Pointer(&pEnum))) // [out] IEnumWbemClassObject **ppEnum) - if hres != 0 { - return nil, NewWmiError(hres) - } - - if err = CoSetProxyBlanket(pEnum); err != nil { - return nil, err - } - - return newEnum(pEnum, s), nil -} - -// GetObject obtains a single WMI class or instance given its path -func (s *Service) GetObject(objectPath string) (instance *Instance, err error) { - var pObject *ole.IUnknown - var strObjectPath *uint16 - - if strObjectPath, err = syscall.UTF16PtrFromString(objectPath); err != nil { - return - } - - // Synchronous call - flags := WBEM_FLAG_RETURN_WBEM_COMPLETE - - res, _, _ := syscall.SyscallN( - s.vTable.GetObject, // IWbemServices::GetObject( - uintptr(unsafe.Pointer(s.service)), // IWbemServices ptr - uintptr(unsafe.Pointer(strObjectPath)), // [in] const BSTR strObjectPath, - uintptr(flags), // [in] long lFlags, - uintptr(0), // [in] IWbemContext *pCtx, - uintptr(unsafe.Pointer(&pObject)), // [out] IWbemClassObject **ppObject, - uintptr(0)) // [out] IWbemCallResult **ppCallResult) - if int(res) < 0 { - // returns WBEM_E_PROVIDER_NOT_FOUND when no entry found - return nil, NewWmiError(res) - } - - return newInstance(pObject, s), nil -} - -// GetObjectAsObject gets an object by its path and set all fields of the passed in target to match the instance's -// properties. Conversion is performed as appropriate. -func (s *Service) GetObjectAsObject(objPath string, target interface{}) error { - instance, err := s.GetObject(objPath) - if err != nil { - return err - } - defer instance.Close() - - return instance.GetAll(target) -} - -// CreateInstanceEnum creates an enumerator that iterates all registered object instances for a given className. -func (s *Service) CreateInstanceEnum(className string) (*Enum, error) { - var err error - var pEnum *ole.IUnknown - var strFilter *uint16 - - if strFilter, err = syscall.UTF16PtrFromString(className); err != nil { - return nil, err - } - - // No subclasses in result set - flags := WBEM_FLAG_SHALLOW - - res, _, _ := syscall.SyscallN( - s.vTable.CreateInstanceEnum, // IWbemServices::CreateInstanceEnum( - uintptr(unsafe.Pointer(s.service)), // IWbemServices ptr - uintptr(unsafe.Pointer(strFilter)), // [in] const BSTR strFilter, - uintptr(flags), // [in] long lFlags, - uintptr(0), // [in] IWbemContext *pCtx, - uintptr(unsafe.Pointer(&pEnum))) // [out] IEnumWbemClassObject **ppEnum) - if int(res) < 0 { - return nil, NewWmiError(res) - } - - if err = CoSetProxyBlanket(pEnum); err != nil { - return nil, err - } - - return newEnum(pEnum, s), nil -} - -// ExecMethod executes a method using the specified class and parameter payload instance. The parameter payload -// instance can be constructed using Instance.GetMethodParameters(). This is an advanced method, it is -// recommended to use BeginInvoke() instead, where possible. -func (s *Service) ExecMethod(className string, methodName string, inParams *Instance) (*Instance, error) { - var err error - var outParams *ole.IUnknown - var strObjectPath *uint16 - var strMethodName *uint16 - - if strObjectPath, err = syscall.UTF16PtrFromString(className); err != nil { - return nil, err - } - - if strMethodName, err = syscall.UTF16PtrFromString(methodName); err != nil { - return nil, err - } - - res, _, _ := syscall.SyscallN( - s.vTable.ExecMethod, // IWbemServices::ExecMethod( - uintptr(unsafe.Pointer(s.service)), // IWbemServices ptr - uintptr(unsafe.Pointer(strObjectPath)), // [in] const BSTR strObjectPath, - uintptr(unsafe.Pointer(strMethodName)), // [in] const BSTR strMethodName, - uintptr(0), // [in] long lFlags, - uintptr(0), // [in] IWbemContext *pCtx, - uintptr(unsafe.Pointer(inParams.object)), // [in] IWbemClassObject *pInParams, - uintptr(unsafe.Pointer(&outParams)), // [out] IWbemClassObject **ppOutParams, - uintptr(0)) // [out] IWbemCallResult **ppCallResult) - if int(res) < 0 { - return nil, NewWmiError(res) - } - - return newInstance(outParams, s), nil -} - -// FindFirstInstance find and returns the first WMI Instance in the result set for a WSL query. -func (s *Service) FindFirstInstance(wql string) (*Instance, error) { - var enum *Enum - var err error - if enum, err = s.ExecQuery(wql); err != nil { - return nil, err - } - defer enum.Close() - - instance, err := enum.Next() - if err != nil { - return nil, err - } - - if instance == nil { - return nil, ErrNoResults - } - - return instance, nil -} - -// FindFirstRelatedInstance finds and returns a related associator of the specified WMI object path of the -// expected className type. -func (s *Service) FindFirstRelatedInstance(objPath string, className string) (*Instance, error) { - wql := fmt.Sprintf("ASSOCIATORS OF {%s} WHERE ResultClass = %s", objPath, className) - return s.FindFirstInstance(wql) -} - -// FindFirstRelatedInstanceThrough finds and returns a related associator of the specified WMI object path of the -// expected className type, and only through the expected association type. -func (s *Service) FindFirstRelatedInstanceThrough(objPath string, resultClass string, assocClass string) (*Instance, error) { - wql := fmt.Sprintf("ASSOCIATORS OF {%s} WHERE AssocClass = %s ResultClass = %s ", objPath, assocClass, resultClass) - return s.FindFirstInstance(wql) -} - -// FindFirstRelatedObject finds and returns a related associator of the specified WMI object path of the -// expected className type, and populates the passed in struct with its fields -func (s *Service) FindFirstRelatedObject(objPath string, className string, target interface{}) error { - wql := fmt.Sprintf("ASSOCIATORS OF {%s} WHERE ResultClass = %s", objPath, className) - return s.FindFirstObject(wql, target) -} - -// FindFirstObject finds and returns the first WMI Instance in the result set for a WSL query, and -// populates the specified struct pointer passed in through the target parameter. -func (s *Service) FindFirstObject(wql string, target interface{}) error { - var enum *Enum - var err error - if enum, err = s.ExecQuery(wql); err != nil { - return err - } - defer enum.Close() - - done, err := NextObject(enum, target) - if err != nil { - return err - } - - if done { - return errors.New("no results found") - } - - return nil -} - -// GetSingletonInstance gets the first WMI instance of the specified object class type. This is a -// shortcut method for uses where only one instance is expected. -func (s *Service) GetSingletonInstance(className string) (*Instance, error) { - var ( - enum *Enum - instance *Instance - err error - ) - - if enum, err = s.CreateInstanceEnum(className); err != nil { - return nil, err - } - defer enum.Close() - - if instance, err = enum.Next(); err != nil { - return nil, err - } - - return instance, nil -} - -// CreateInstance creates a new WMI object class instance of the specified className, and sets -// all properties according to the passed in struct pointer through the src -// parameter, converting appropriately. -func (s *Service) CreateInstance(className string, src interface{}) (*Instance, error) { - instance, err := s.SpawnInstance(className) - if err != nil { - return nil, err - } - - return instance, instance.PutAll(src) -} - -// SpawnInstance creates a new zeroed WMI instance. This instance will not contain expected values. -// Those must be retrieved and set separately, or CreateInstance() can be used instead. -func (s *Service) SpawnInstance(className string) (*Instance, error) { - var class *Instance - var err error - if class, err = s.GetObject(className); err != nil { - return nil, err - } - defer class.Close() - - return class.SpawnInstance() -} - -// RefetchObject re-fetches the object and returns a new instance. The original instance will not -// automatically Close(). Callers of this method will need to manually close the -// original. -func (s *Service) RefetchObject(instance *Instance) (*Instance, error) { - path, err := instance.Path() - if err != nil { - return instance, err - } - return s.GetObject(path) -} - -// GetClassInstance gets the WMI class instance associated with the specified object instance. -// This method is used to perform schema queries. -func (s *Service) GetClassInstance(obj *Instance) (*Instance, error) { - name, err := obj.GetClassName() - if err != nil { - return nil, err - } - return s.GetObject(name) -} diff --git a/test/e2e/basic_test.go b/test/e2e/basic_test.go index efdd2bc..99642c1 100644 --- a/test/e2e/basic_test.go +++ b/test/e2e/basic_test.go @@ -41,7 +41,7 @@ var _ = Describe("basic operation test", func() { defer removeOnError(tvm) Expect(tvm.vm.IsStarting()).To(BeFalse()) - Expect(tvm.vm.State()).To(Equal(hypervctl.Disabled)) + Expect(tvm.vm.State).To(Equal(hypervctl.Disabled)) // start the vm err = tvm.vm.Start() @@ -49,7 +49,7 @@ var _ = Describe("basic operation test", func() { err = tvm.refresh() Expect(err).To(BeNil()) - Expect(tvm.vm.State()).To(Equal(hypervctl.Enabled)) + Expect(tvm.vm.State).To(Equal(hypervctl.Enabled)) // TODO I get an error when trying to immediately stop a VM so this is a placeholder // for "wait" @@ -60,7 +60,7 @@ var _ = Describe("basic operation test", func() { Expect(err).To(BeNil()) err = tvm.refresh() Expect(err).To(BeNil()) - Expect(tvm.vm.State()).To(Equal(hypervctl.Disabled)) + Expect(tvm.vm.State).To(Equal(hypervctl.Disabled)) // remove the vm err = tvm.vm.Remove(tvm.config.DiskPath) diff --git a/winmake.ps1 b/winmake.ps1 index 483fd00..02b1510 100644 --- a/winmake.ps1 +++ b/winmake.ps1 @@ -9,6 +9,7 @@ function Binaries{ Run-Command "go build -o bin ./cmd/dumpvms" Run-Command "go build -o bin ./cmd/createvm" Run-Command "go build -o bin ./cmd/updatevm" + Run-Command "go build -o bin ./cmd/managevm" } function Make-Clean{ From 51535f7335f737031b24920762894cf5f1048cc8 Mon Sep 17 00:00:00 2001 From: Yevhen Vydolob Date: Tue, 6 Jan 2026 14:37:14 +0200 Subject: [PATCH 4/4] use fedora 42 for test vm image Signed-off-by: Yevhen Vydolob --- test/e2e/fedora_test.go | 434 +++++++++++++++++++-------------------- test/e2e/libhvee_test.go | 278 ++++++++++++------------- 2 files changed, 356 insertions(+), 356 deletions(-) diff --git a/test/e2e/fedora_test.go b/test/e2e/fedora_test.go index 1320701..090857d 100644 --- a/test/e2e/fedora_test.go +++ b/test/e2e/fedora_test.go @@ -1,217 +1,217 @@ -package e2e - -import ( - "crypto/sha256" - "encoding/hex" - "encoding/json" - "errors" - "fmt" - "io" - "log" - "os" - "path/filepath" - "runtime" - - . "github.com/onsi/ginkgo/v2" - "github.com/ulikunitz/xz" -) - -const ( - fedoraBaseDirEndpoint = "https://kojipkgs.fedoraproject.org/compose/cloud/latest-Fedora-Cloud-41/compose" -) - -// Fedora metadata for cloud downloads -/* -{ - "header": { - "type": "productmd.images", - "version": "1.2" - }, - "payload": { - "compose": { - "date": "20231130", - "id": "Fedora-Cloud-39-20231130.0", - "respin": 0, - "type": "production" - }, - "images": { - "Cloud": { - "aarch64": [ - { - "arch": "aarch64", - "bootable": false, - "checksums": { - "sha256": "09860169a88d39b865d6b5cc982134d68202d4f9b0ad36fdd14222c99749a5d3" - }, - "disc_count": 1, - "disc_number": 1, - "format": "qcow2", - "implant_md5": null, - "mtime": 1701326607, - "path": "Cloud/aarch64/images/Fedora-Cloud-Base-39-20231130.0.aarch64.qcow2", - "size": 594280448, - "subvariant": "Cloud_Base", - "type": "qcow2", - "volume_id": null - }, -... -*/ - -type fedoraCloudHeader struct { - Type string `json:"type"` - Version string `json:"version"` -} - -type fedoraCloudImage struct { - Arch string - Bootable bool - Checksums map[string]string `json:"checksums"` - DiscCount int `json:"disc_count"` - DiskNumber int `json:"disk_number"` - Format string - ImplantMd5 string `json:"implant_md5"` - Mtime int64 `json:"mtime"` - Path string - Size int64 - Subvariant string - Type string - VolumeID *int64 `json:"volume_id"` -} - -type FedoraCloudCompose struct { - Date string - ID string `json:"id"` - Respin int - Kind string `json:"type"` -} - -type FedoraCloudPayload struct { - Compose FedoraCloudCompose - Images map[string]map[string][]fedoraCloudImage -} - -type fedoraCloudMetadata struct { - Header fedoraCloudHeader `json:"header"` - Payload FedoraCloudPayload `json:"payload"` -} - -func (m fedoraCloudMetadata) downloadPathForVHD() (string, string, error) { - arch := archFromGOOS() - val, ok := m.Payload.Images["Cloud"][arch] - if !ok { - return "", "", errors.New("unable to parse metadata for cloud image") - } - for _, ci := range val { - if ci.Format == "vhd.xz" { // fedora does not make a vhdx - return fmt.Sprintf("%s/%s", fedoraBaseDirEndpoint, ci.Path), ci.Checksums["sha256"], nil - } - } - return "", "", errors.New("unable to find proper image in meatadata") -} - -func checkIfCacheExistsAndLatest(dst string, latestSha string) (bool, error) { - // does a cache image exist - if _, err := os.Stat(dst); err == nil { - // is it the latest - f, err := os.Open(dst) - if err != nil { - log.Fatal(err) - } - defer func() { - _ = f.Close() - }() - h := sha256.New() - _, err = io.Copy(h, f) - if err != nil { - return false, err - } - if hex.EncodeToString(h.Sum(nil)) == latestSha { - // shas are same, return - return true, nil - } - return false, fmt.Errorf("an old cache file exists at %q: remove it and rerun", dst) - } - return false, nil -} - -func getTestImage() (string, error) { - fedoraMetaData, err := get(fmt.Sprintf("%s/metadata/images.json", fedoraBaseDirEndpoint)) - if err != nil { - Fail(fmt.Sprintf("unable to determine fedora cloud version: %q", err)) - } - var fcmd fedoraCloudMetadata - if err := json.Unmarshal(fedoraMetaData, &fcmd); err != nil { - Fail(err.Error()) - } - - downloadPath, upstreamSha, err := fcmd.downloadPathForVHD() - if err != nil { - Fail(err.Error()) - } - fileName := filepath.Base(downloadPath) - dstPath := filepath.Join(defaultCacheDirPath, fileName) - - // check for cached image && if cached image is latest - exists, err := checkIfCacheExistsAndLatest(dstPath, upstreamSha) - if err != nil { - Fail(err.Error()) - } - if err != nil { - Fail(err.Error()) - } - if !exists { - dst, err := os.OpenFile(dstPath, os.O_CREATE|os.O_WRONLY, 0644) - if err != nil { - return "", err - } - defer func() { - _ = dst.Close() - }() - if err := pullWithProgress(downloadPath, dst); err != nil { - return "", err - } - } - // Decompress the downloaded vhdfixed.xz file - vhdPath := filepath.Join(defaultCacheDirPath, filepath.Base(downloadPath[:len(downloadPath)-len(".vhdfixed.xz")])) + ".vhd" - err = decompressVhdXZ(dstPath, vhdPath) - if err != nil { - return "", err - } - return vhdPath, nil -} - -func decompressVhdXZ(src string, dst string) error { - f, err := os.Open(src) - if err != nil { - return err - } - defer func() { - _ = f.Close() - }() - r, err := xz.NewReader(f) - if err != nil { - return fmt.Errorf("xz decompression failed: %v", err) - } - - // Directly copy the decompressed data to the destination file - outFile, err := os.Create(dst) - if err != nil { - return err - } - defer func() { - _ = outFile.Close() - }() - - _, err = io.Copy(outFile, r) - if err != nil { - return err - } - return nil -} - -func archFromGOOS() string { - if runtime.GOOS == "arm64" { - return "aarch64" - } - return "x86_64" -} +package e2e + +import ( + "crypto/sha256" + "encoding/hex" + "encoding/json" + "errors" + "fmt" + "io" + "log" + "os" + "path/filepath" + "runtime" + + . "github.com/onsi/ginkgo/v2" + "github.com/ulikunitz/xz" +) + +const ( + fedoraBaseDirEndpoint = "https://kojipkgs.fedoraproject.org/compose/cloud/latest-Fedora-Cloud-42/compose" +) + +// Fedora metadata for cloud downloads +/* +{ + "header": { + "type": "productmd.images", + "version": "1.2" + }, + "payload": { + "compose": { + "date": "20231130", + "id": "Fedora-Cloud-39-20231130.0", + "respin": 0, + "type": "production" + }, + "images": { + "Cloud": { + "aarch64": [ + { + "arch": "aarch64", + "bootable": false, + "checksums": { + "sha256": "09860169a88d39b865d6b5cc982134d68202d4f9b0ad36fdd14222c99749a5d3" + }, + "disc_count": 1, + "disc_number": 1, + "format": "qcow2", + "implant_md5": null, + "mtime": 1701326607, + "path": "Cloud/aarch64/images/Fedora-Cloud-Base-39-20231130.0.aarch64.qcow2", + "size": 594280448, + "subvariant": "Cloud_Base", + "type": "qcow2", + "volume_id": null + }, +... +*/ + +type fedoraCloudHeader struct { + Type string `json:"type"` + Version string `json:"version"` +} + +type fedoraCloudImage struct { + Arch string + Bootable bool + Checksums map[string]string `json:"checksums"` + DiscCount int `json:"disc_count"` + DiskNumber int `json:"disk_number"` + Format string + ImplantMd5 string `json:"implant_md5"` + Mtime int64 `json:"mtime"` + Path string + Size int64 + Subvariant string + Type string + VolumeID *int64 `json:"volume_id"` +} + +type FedoraCloudCompose struct { + Date string + ID string `json:"id"` + Respin int + Kind string `json:"type"` +} + +type FedoraCloudPayload struct { + Compose FedoraCloudCompose + Images map[string]map[string][]fedoraCloudImage +} + +type fedoraCloudMetadata struct { + Header fedoraCloudHeader `json:"header"` + Payload FedoraCloudPayload `json:"payload"` +} + +func (m fedoraCloudMetadata) downloadPathForVHD() (string, string, error) { + arch := archFromGOOS() + val, ok := m.Payload.Images["Cloud"][arch] + if !ok { + return "", "", errors.New("unable to parse metadata for cloud image") + } + for _, ci := range val { + if ci.Format == "vhd.xz" { // fedora does not make a vhdx + return fmt.Sprintf("%s/%s", fedoraBaseDirEndpoint, ci.Path), ci.Checksums["sha256"], nil + } + } + return "", "", errors.New("unable to find proper image in meatadata") +} + +func checkIfCacheExistsAndLatest(dst string, latestSha string) (bool, error) { + // does a cache image exist + if _, err := os.Stat(dst); err == nil { + // is it the latest + f, err := os.Open(dst) + if err != nil { + log.Fatal(err) + } + defer func() { + _ = f.Close() + }() + h := sha256.New() + _, err = io.Copy(h, f) + if err != nil { + return false, err + } + if hex.EncodeToString(h.Sum(nil)) == latestSha { + // shas are same, return + return true, nil + } + return false, fmt.Errorf("an old cache file exists at %q: remove it and rerun", dst) + } + return false, nil +} + +func getTestImage() (string, error) { + fedoraMetaData, err := get(fmt.Sprintf("%s/metadata/images.json", fedoraBaseDirEndpoint)) + if err != nil { + Fail(fmt.Sprintf("unable to determine fedora cloud version: %q", err)) + } + var fcmd fedoraCloudMetadata + if err := json.Unmarshal(fedoraMetaData, &fcmd); err != nil { + Fail(err.Error()) + } + + downloadPath, upstreamSha, err := fcmd.downloadPathForVHD() + if err != nil { + Fail(err.Error()) + } + fileName := filepath.Base(downloadPath) + dstPath := filepath.Join(defaultCacheDirPath, fileName) + + // check for cached image && if cached image is latest + exists, err := checkIfCacheExistsAndLatest(dstPath, upstreamSha) + if err != nil { + Fail(err.Error()) + } + if err != nil { + Fail(err.Error()) + } + if !exists { + dst, err := os.OpenFile(dstPath, os.O_CREATE|os.O_WRONLY, 0644) + if err != nil { + return "", err + } + defer func() { + _ = dst.Close() + }() + if err := pullWithProgress(downloadPath, dst); err != nil { + return "", err + } + } + // Decompress the downloaded vhdfixed.xz file + vhdPath := filepath.Join(defaultCacheDirPath, filepath.Base(downloadPath[:len(downloadPath)-len(".vhdfixed.xz")])) + ".vhd" + err = decompressVhdXZ(dstPath, vhdPath) + if err != nil { + return "", err + } + return vhdPath, nil +} + +func decompressVhdXZ(src string, dst string) error { + f, err := os.Open(src) + if err != nil { + return err + } + defer func() { + _ = f.Close() + }() + r, err := xz.NewReader(f) + if err != nil { + return fmt.Errorf("xz decompression failed: %v", err) + } + + // Directly copy the decompressed data to the destination file + outFile, err := os.Create(dst) + if err != nil { + return err + } + defer func() { + _ = outFile.Close() + }() + + _, err = io.Copy(outFile, r) + if err != nil { + return err + } + return nil +} + +func archFromGOOS() string { + if runtime.GOOS == "arm64" { + return "aarch64" + } + return "x86_64" +} diff --git a/test/e2e/libhvee_test.go b/test/e2e/libhvee_test.go index 7c1c699..8b807fa 100644 --- a/test/e2e/libhvee_test.go +++ b/test/e2e/libhvee_test.go @@ -1,139 +1,139 @@ -package e2e - -import ( - "fmt" - - "io" - "net/http" - "os" - "testing" - - . "github.com/onsi/ginkgo/v2" - . "github.com/onsi/gomega" - "github.com/schollz/progressbar/v3" -) - -var ( - cachedImagePath string -) - -func TestMain(m *testing.M) { - os.Exit(m.Run()) -} - -func TestLibhvee(t *testing.T) { - RegisterFailHandler(Fail) - RunSpecs(t, "Libhvee Suite") -} - -func get(endpoint string) ([]byte, error) { - - getReq, err := http.NewRequest("GET", endpoint, nil) - if err != nil { - return nil, err - } - addHeaders(getReq) - - resp, err := http.DefaultClient.Do(getReq) - if err != nil { - return nil, err - } - - if resp.StatusCode != http.StatusOK { - Fail(fmt.Sprintf("get %s: status code: %d", endpoint, resp.StatusCode)) - } - defer func() { - _ = resp.Body.Close() - }() - body, err := io.ReadAll(resp.Body) - return body, err -} - -// these headers are required to bypass anubis bot protection -// maybe subject to change if the bot protection is updated -func addHeaders(getReq *http.Request) { - getReq.Header.Set("Accept", "application/json") - getReq.Header.Set("Accept-Encoding", "gzip, deflate, br") - getReq.Header.Set("Accept-Language", "en-US,en;q=0.9") - getReq.Header.Set("Connection", "keep-alive") - getReq.Header.Set("Referer", "https://kojipkgs.fedoraproject.org/compose/cloud/latest-Fedora-Cloud-41/compose") - getReq.Header.Set("Sec-Ch-Ua-Mobile", "?0") - getReq.Header.Set("Sec-Ch-Ua-Platform", "macOS") - getReq.Header.Set("Sec-Fetch-Dest", "document") - getReq.Header.Set("Sec-Fetch-Mode", "navigate") - getReq.Header.Set("Sec-Fetch-Site", "same-origin") - getReq.Header.Set("Sec-User", "?1") - getReq.Header.Set("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/142.0.0.0 Safari/537.36") -} - -func pullWithProgress(endpoint string, dst *os.File) error { - fmt.Println("trying to pull: ", endpoint) - req, err := http.NewRequest("GET", endpoint, nil) - if err != nil { - Fail(err.Error()) - } - addHeaders(req) - resp, err := http.DefaultClient.Do(req) - if err != nil { - Fail(err.Error()) - } - if resp.StatusCode != http.StatusOK { - Fail(fmt.Sprintf("get %s: status code: %d", endpoint, resp.StatusCode)) - } - defer func() { - _ = resp.Body.Close() - }() - return doWithProgress("downloading", resp.Body, dst, resp.ContentLength) -} - -func copyWithProgress(srcPath string, dst *os.File) error { - s, err := os.Stat(srcPath) - if err != nil { - return err - } - f, err := os.Open(srcPath) - if err != nil { - return err - } - defer func() { - _ = f.Close() - }() - return doWithProgress("copying", f, dst, s.Size()) -} - -func doWithProgress(status string, src io.Reader, dst io.Writer, maxBytes int64) error { - bar := progressbar.DefaultBytes( - maxBytes, - status, - ) - _, err := io.Copy(io.MultiWriter(dst, bar), src) - return err - -} - -var _ = BeforeSuite(func() { - // placeholder for stuff - fmt.Println("Before") - var err error - cachedImagePath, err = getTestImage() - if err != nil { - Fail(err.Error()) - } - fmt.Println(cachedImagePath) - -}) - -var _ = AfterSuite(func() { - // placeholder for stuff - fmt.Println("After") - -}) - -var ( - _ = BeforeEach(func() { - }) - - _ = AfterEach(func() { - // we can do stuff after - }) -) +package e2e + +import ( + "fmt" + + "io" + "net/http" + "os" + "testing" + + . "github.com/onsi/ginkgo/v2" + . "github.com/onsi/gomega" + "github.com/schollz/progressbar/v3" +) + +var ( + cachedImagePath string +) + +func TestMain(m *testing.M) { + os.Exit(m.Run()) +} + +func TestLibhvee(t *testing.T) { + RegisterFailHandler(Fail) + RunSpecs(t, "Libhvee Suite") +} + +func get(endpoint string) ([]byte, error) { + + getReq, err := http.NewRequest("GET", endpoint, nil) + if err != nil { + return nil, err + } + addHeaders(getReq) + + resp, err := http.DefaultClient.Do(getReq) + if err != nil { + return nil, err + } + + if resp.StatusCode != http.StatusOK { + Fail(fmt.Sprintf("get %s: status code: %d", endpoint, resp.StatusCode)) + } + defer func() { + _ = resp.Body.Close() + }() + body, err := io.ReadAll(resp.Body) + return body, err +} + +// these headers are required to bypass anubis bot protection +// maybe subject to change if the bot protection is updated +func addHeaders(getReq *http.Request) { + getReq.Header.Set("Accept", "application/json") + getReq.Header.Set("Accept-Encoding", "gzip, deflate, br") + getReq.Header.Set("Accept-Language", "en-US,en;q=0.9") + getReq.Header.Set("Connection", "keep-alive") + getReq.Header.Set("Referer", "https://kojipkgs.fedoraproject.org/compose/cloud/latest-Fedora-Cloud-42/compose") + getReq.Header.Set("Sec-Ch-Ua-Mobile", "?0") + getReq.Header.Set("Sec-Ch-Ua-Platform", "macOS") + getReq.Header.Set("Sec-Fetch-Dest", "document") + getReq.Header.Set("Sec-Fetch-Mode", "navigate") + getReq.Header.Set("Sec-Fetch-Site", "same-origin") + getReq.Header.Set("Sec-User", "?1") + getReq.Header.Set("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/142.0.0.0 Safari/537.36") +} + +func pullWithProgress(endpoint string, dst *os.File) error { + fmt.Println("trying to pull: ", endpoint) + req, err := http.NewRequest("GET", endpoint, nil) + if err != nil { + Fail(err.Error()) + } + addHeaders(req) + resp, err := http.DefaultClient.Do(req) + if err != nil { + Fail(err.Error()) + } + if resp.StatusCode != http.StatusOK { + Fail(fmt.Sprintf("get %s: status code: %d", endpoint, resp.StatusCode)) + } + defer func() { + _ = resp.Body.Close() + }() + return doWithProgress("downloading", resp.Body, dst, resp.ContentLength) +} + +func copyWithProgress(srcPath string, dst *os.File) error { + s, err := os.Stat(srcPath) + if err != nil { + return err + } + f, err := os.Open(srcPath) + if err != nil { + return err + } + defer func() { + _ = f.Close() + }() + return doWithProgress("copying", f, dst, s.Size()) +} + +func doWithProgress(status string, src io.Reader, dst io.Writer, maxBytes int64) error { + bar := progressbar.DefaultBytes( + maxBytes, + status, + ) + _, err := io.Copy(io.MultiWriter(dst, bar), src) + return err + +} + +var _ = BeforeSuite(func() { + // placeholder for stuff + fmt.Println("Before") + var err error + cachedImagePath, err = getTestImage() + if err != nil { + Fail(err.Error()) + } + fmt.Println(cachedImagePath) + +}) + +var _ = AfterSuite(func() { + // placeholder for stuff + fmt.Println("After") + +}) + +var ( + _ = BeforeEach(func() { + }) + + _ = AfterEach(func() { + // we can do stuff after + }) +)