| 1 | |
| 2 | |
| 3 | package guestos |
| 4 | |
| 5 | import ( |
| 6 | "context" |
| 7 | "errors" |
| 8 | "os" |
| 9 | "path/filepath" |
| 10 | |
| 11 | "github.com/google/uuid" |
| 12 | "golang.org/x/sys/windows" |
| 13 | ) |
| 14 | |
| 15 | func windowsHostStorage(ctx context.Context, id uuid.UUID, input windowsHostStorageRequest) (windowsHostStorageState, error) { |
| 16 | var state windowsHostStorageState |
| 17 | err := windowsHostManagerRequestCall(ctx, windowsHostManagerRequest{Version: windowsHostManagerVersion, VMID: id, Operation: "storage", Storage: &input}, &state) |
| 18 | return state, err |
| 19 | } |
| 20 | |
| 21 | func WindowsPrivateStoragePaths() []string { |
| 22 | return []string{filepath.Join(managerKnownFolder(windows.FOLDERID_ProgramData), "Cage", "HyperVManager")} |
| 23 | } |
| 24 | |
| 25 | func replaceWindowsStorageProfile(path string, data []byte) error { |
| 26 | file, err := os.CreateTemp(filepath.Dir(path), ".windows-storage-*") |
| 27 | if err != nil { |
| 28 | return err |
| 29 | } |
| 30 | defer os.Remove(file.Name()) |
| 31 | if err = protectWindowsProfile(file.Name()); err != nil { |
| 32 | file.Close() |
| 33 | return err |
| 34 | } |
| 35 | _, err = file.Write(data) |
| 36 | if err == nil { |
| 37 | err = file.Sync() |
| 38 | } |
| 39 | if err = errors.Join(err, file.Close()); err != nil { |
| 40 | return err |
| 41 | } |
| 42 | from, err := windows.UTF16PtrFromString(file.Name()) |
| 43 | if err != nil { |
| 44 | return err |
| 45 | } |
| 46 | to, err := windows.UTF16PtrFromString(path) |
| 47 | if err != nil { |
| 48 | return err |
| 49 | } |
| 50 | return windows.MoveFileEx(from, to, windows.MOVEFILE_REPLACE_EXISTING|windows.MOVEFILE_WRITE_THROUGH) |
| 51 | } |