forked from hooklift/govix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
snapshot.go
169 lines (138 loc) · 3.95 KB
/
snapshot.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
package vix
/*
#cgo CFLAGS: -I./vendor/libvix/include -Werror
#cgo LDFLAGS: -L./vendor/libvix -lvixAllProducts -ldl -lpthread
#include "vix.h"
#include "helper.h"
*/
import "C"
import (
"runtime"
"unsafe"
)
// Snapshot represents a VM snapshot.
type Snapshot struct {
// Internal VIX handle
handle C.VixHandle
}
// Name returns user defined name for the snapshot.
func (s *Snapshot) Name() (string, error) {
var err C.VixError = C.VIX_OK
var name *C.char
err = C.get_property(s.handle,
C.VIX_PROPERTY_SNAPSHOT_DISPLAYNAME,
unsafe.Pointer(&name))
defer C.Vix_FreeBuffer(unsafe.Pointer(name))
if C.VIX_OK != err {
return "", &Error{
Operation: "snapshot.Name",
Code: int(err & 0xFFFF),
Text: C.GoString(C.Vix_GetErrorText(err, nil)),
}
}
return C.GoString(name), nil
}
// Description returns user defined description for the snapshot.
func (s *Snapshot) Description() (string, error) {
var err C.VixError = C.VIX_OK
var desc *C.char
err = C.get_property(s.handle,
C.VIX_PROPERTY_SNAPSHOT_DESCRIPTION,
unsafe.Pointer(&desc))
defer C.Vix_FreeBuffer(unsafe.Pointer(desc))
if C.VIX_OK != err {
return "", &Error{
Operation: "snapshot.Description",
Code: int(err & 0xFFFF),
Text: C.GoString(C.Vix_GetErrorText(err, nil)),
}
}
return C.GoString(desc), nil
}
// Child returns the child snapshot corresponding to the index parameter.
//
// Parameters:
//
// index: Index into the list of snapshots.
//
// Remarks:
//
// * Snapshots are indexed from 0 to n-1, where n is the number of child
// snapshots. Use the function Snapshot.NumChildren() to get the value of n.
// * This function is not supported when using the VMWARE_PLAYER provider.
//
// Since VMware Workstation 6.0
func (s *Snapshot) Child(index int) (*Snapshot, error) {
var snapshotHandle C.VixHandle = C.VIX_INVALID_HANDLE
var err C.VixError = C.VIX_OK
err = C.VixSnapshot_GetChild(s.handle,
C.int(index), //index
&snapshotHandle) //(output) A handle to the child snapshot.
if C.VIX_OK != err {
return nil, &Error{
Operation: "snapshot.Child",
Code: int(err & 0xFFFF),
Text: C.GoString(C.Vix_GetErrorText(err, nil)),
}
}
snapshot := &Snapshot{
handle: snapshotHandle,
}
runtime.SetFinalizer(snapshot, cleanupSnapshot)
return snapshot, nil
}
// NumChildren returns the number of child snapshots of a specified snapshot.
//
// Remarks:
//
// * This function is not supported when using the VMWARE_PLAYER provider.
//
// Since VMware Workstation 6.0.
func (s *Snapshot) NumChildren() (int, error) {
var err C.VixError = C.VIX_OK
var numChildren *C.int
err = C.VixSnapshot_GetNumChildren(s.handle, numChildren)
if C.VIX_OK != err {
return 0, &Error{
Operation: "snapshot.NumChildren",
Code: int(err & 0xFFFF),
Text: C.GoString(C.Vix_GetErrorText(err, nil)),
}
}
return int(*numChildren), nil
}
// Parent returns the parent of a snapshot.
//
// Remarks:
//
// * This function is not supported when using the VMWARE_PLAYER provider
//
// Since VMware Workstation 6.0
func (s *Snapshot) Parent() (*Snapshot, error) {
var snapshotHandle C.VixHandle = C.VIX_INVALID_HANDLE
var err C.VixError = C.VIX_OK
err = C.VixSnapshot_GetParent(s.handle,
&snapshotHandle) //(output) A handle to the child snapshot.
if C.VIX_OK != err {
return nil, &Error{
Operation: "snapshot.Parent",
Code: int(err & 0xFFFF),
Text: C.GoString(C.Vix_GetErrorText(err, nil)),
}
}
snapshot := &Snapshot{
handle: snapshotHandle,
}
runtime.SetFinalizer(snapshot, cleanupSnapshot)
return snapshot, nil
}
// cleanupSnapshot cleans up snapshot internal C handle.
func cleanupSnapshot(s *Snapshot) {
if s.handle != C.VIX_INVALID_HANDLE {
C.Vix_ReleaseHandle(s.handle)
s.handle = C.VIX_INVALID_HANDLE
}
}