-
Notifications
You must be signed in to change notification settings - Fork 260
/
Copy pathoptions.go
241 lines (209 loc) · 7.75 KB
/
options.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
package rbd
// #cgo LDFLAGS: -lrbd
// #include <stdlib.h>
// #include <rbd/librbd.h>
import "C"
import (
"fmt"
"unsafe"
)
const (
// RBD image options.
// ImageOptionFormat is the representation of RBD_IMAGE_OPTION_FORMAT from
// librbd
ImageOptionFormat = C.RBD_IMAGE_OPTION_FORMAT
// ImageOptionFeatures is the representation of RBD_IMAGE_OPTION_FEATURES
// from librbd
ImageOptionFeatures = C.RBD_IMAGE_OPTION_FEATURES
// ImageOptionOrder is the representation of RBD_IMAGE_OPTION_ORDER from
// librbd
ImageOptionOrder = C.RBD_IMAGE_OPTION_ORDER
// ImageOptionStripeUnit is the representation of
// RBD_IMAGE_OPTION_STRIPE_UNIT from librbd
ImageOptionStripeUnit = C.RBD_IMAGE_OPTION_STRIPE_UNIT
// ImageOptionStripeCount is the representation of
// RBD_IMAGE_OPTION_STRIPE_COUNT from librbd
ImageOptionStripeCount = C.RBD_IMAGE_OPTION_STRIPE_COUNT
// ImageOptionJournalOrder is the representation of
// RBD_IMAGE_OPTION_JOURNAL_ORDER from librbd
ImageOptionJournalOrder = C.RBD_IMAGE_OPTION_JOURNAL_ORDER
// ImageOptionJournalSplayWidth is the representation of
// RBD_IMAGE_OPTION_JOURNAL_SPLAY_WIDTH from librbd
ImageOptionJournalSplayWidth = C.RBD_IMAGE_OPTION_JOURNAL_SPLAY_WIDTH
// ImageOptionJournalPool is the representation of
// RBD_IMAGE_OPTION_JOURNAL_POOL from librbd
ImageOptionJournalPool = C.RBD_IMAGE_OPTION_JOURNAL_POOL
// ImageOptionFeaturesSet is the representation of
// RBD_IMAGE_OPTION_FEATURES_SET from librbd
ImageOptionFeaturesSet = C.RBD_IMAGE_OPTION_FEATURES_SET
// ImageOptionFeaturesClear is the representation of
// RBD_IMAGE_OPTION_FEATURES_CLEAR from librbd
ImageOptionFeaturesClear = C.RBD_IMAGE_OPTION_FEATURES_CLEAR
// ImageOptionDataPool is the representation of RBD_IMAGE_OPTION_DATA_POOL
// from librbd
ImageOptionDataPool = C.RBD_IMAGE_OPTION_DATA_POOL
// ImageOptionFlatten is the representation of RBD_IMAGE_OPTION_FLATTEN
// from librbd
ImageOptionFlatten = C.RBD_IMAGE_OPTION_FLATTEN
// ImageOptionCloneFormat is the representation of
// RBD_IMAGE_OPTION_CLONE_FORMAT from librbd
ImageOptionCloneFormat = C.RBD_IMAGE_OPTION_CLONE_FORMAT
// RbdImageOptionFormat deprecated alias for ImageOptionFormat
RbdImageOptionFormat = ImageOptionFormat
// RbdImageOptionFeatures deprecated alias for ImageOptionFeatures
RbdImageOptionFeatures = ImageOptionFeatures
// RbdImageOptionOrder deprecated alias for ImageOptionOrder
RbdImageOptionOrder = ImageOptionOrder
// RbdImageOptionStripeUnit deprecated alias for ImageOptionStripeUnit
RbdImageOptionStripeUnit = ImageOptionStripeUnit
// RbdImageOptionStripeCount deprecated alias for ImageOptionStripeCount
RbdImageOptionStripeCount = ImageOptionStripeCount
// RbdImageOptionJournalOrder deprecated alias for ImageOptionJournalOrder
RbdImageOptionJournalOrder = ImageOptionJournalOrder
// RbdImageOptionJournalSplayWidth deprecated alias for
RbdImageOptionJournalSplayWidth = ImageOptionJournalSplayWidth
// RbdImageOptionJournalPool deprecated alias for ImageOptionJournalPool
RbdImageOptionJournalPool = ImageOptionJournalPool
// RbdImageOptionFeaturesSet deprecated alias for ImageOptionFeaturesSet
RbdImageOptionFeaturesSet = ImageOptionFeaturesSet
// RbdImageOptionFeaturesClear deprecated alias for ImageOptionFeaturesClear
RbdImageOptionFeaturesClear = ImageOptionFeaturesClear
// RbdImageOptionDataPool deprecated alias for ImageOptionDataPool
RbdImageOptionDataPool = ImageOptionDataPool
)
// ImageOptions represents a group of configurable image options.
type ImageOptions struct {
options C.rbd_image_options_t
}
// ImageOption values are unique keys for configurable options.
type ImageOption C.int
// revive:disable:exported Deprecated aliases
// RbdImageOptions deprecated alias for ImageOptions
type RbdImageOptions = ImageOptions
// RbdImageOption is a deprecated alias for ImageOption
type RbdImageOption = ImageOption
//revive:enable:exported
// NewRbdImageOptions creates a new RbdImageOptions struct. Call
// RbdImageOptions.Destroy() to free the resources.
//
// Implements:
//
// void rbd_image_options_create(rbd_image_options_t* opts)
func NewRbdImageOptions() *ImageOptions {
rio := &ImageOptions{}
C.rbd_image_options_create(&rio.options)
return rio
}
// Destroy a RbdImageOptions struct and free the associated resources.
//
// Implements:
//
// void rbd_image_options_destroy(rbd_image_options_t opts);
func (rio *ImageOptions) Destroy() {
C.rbd_image_options_destroy(rio.options)
}
// SetString sets the value of the RbdImageOption to the given string.
//
// Implements:
//
// int rbd_image_options_set_string(rbd_image_options_t opts, int optname,
// const char* optval);
func (rio *ImageOptions) SetString(option ImageOption, value string) error {
cValue := C.CString(value)
defer C.free(unsafe.Pointer(cValue))
ret := C.rbd_image_options_set_string(rio.options, C.int(option), cValue)
if ret != 0 {
return fmt.Errorf("%v, could not set option %v to \"%v\"",
getError(ret), option, value)
}
return nil
}
// GetString returns the string value of the RbdImageOption.
//
// Implements:
//
// int rbd_image_options_get_string(rbd_image_options_t opts, int optname,
// char* optval, size_t maxlen);
func (rio *ImageOptions) GetString(option ImageOption) (string, error) {
value := make([]byte, 4096)
ret := C.rbd_image_options_get_string(rio.options, C.int(option),
(*C.char)(unsafe.Pointer(&value[0])),
C.size_t(len(value)))
if ret != 0 {
return "", fmt.Errorf("%v, could not get option %v", getError(ret), option)
}
return C.GoString((*C.char)(unsafe.Pointer(&value[0]))), nil
}
// SetUint64 sets the value of the RbdImageOption to the given uint64.
//
// Implements:
//
// int rbd_image_options_set_uint64(rbd_image_options_t opts, int optname,
// const uint64_t optval);
func (rio *ImageOptions) SetUint64(option ImageOption, value uint64) error {
cValue := C.uint64_t(value)
ret := C.rbd_image_options_set_uint64(rio.options, C.int(option), cValue)
if ret != 0 {
return fmt.Errorf("%v, could not set option %v to \"%v\"",
getError(ret), option, value)
}
return nil
}
// GetUint64 returns the uint64 value of the RbdImageOption.
//
// Implements:
//
// int rbd_image_options_get_uint64(rbd_image_options_t opts, int optname,
// uint64_t* optval);
func (rio *ImageOptions) GetUint64(option ImageOption) (uint64, error) {
var cValue C.uint64_t
ret := C.rbd_image_options_get_uint64(rio.options, C.int(option), &cValue)
if ret != 0 {
return 0, fmt.Errorf("%v, could not get option %v", getError(ret), option)
}
return uint64(cValue), nil
}
// IsSet returns a true if the RbdImageOption is set, false otherwise.
//
// Implements:
//
// int rbd_image_options_is_set(rbd_image_options_t opts, int optname,
// bool* is_set);
func (rio *ImageOptions) IsSet(option ImageOption) (bool, error) {
var cSet C.bool
ret := C.rbd_image_options_is_set(rio.options, C.int(option), &cSet)
if ret != 0 {
return false, fmt.Errorf("%v, could not check option %v", getError(ret), option)
}
return bool(cSet), nil
}
// Unset a given RbdImageOption.
//
// Implements:
//
// int rbd_image_options_unset(rbd_image_options_t opts, int optname)
func (rio *ImageOptions) Unset(option ImageOption) error {
ret := C.rbd_image_options_unset(rio.options, C.int(option))
if ret != 0 {
return fmt.Errorf("%v, could not unset option %v", getError(ret), option)
}
return nil
}
// Clear all options in the RbdImageOptions.
//
// Implements:
//
// void rbd_image_options_clear(rbd_image_options_t opts)
func (rio *ImageOptions) Clear() {
C.rbd_image_options_clear(rio.options)
}
// IsEmpty returns true if there are no options set in the RbdImageOptions,
// false otherwise.
//
// Implements:
//
// int rbd_image_options_is_empty(rbd_image_options_t opts)
func (rio *ImageOptions) IsEmpty() bool {
ret := C.rbd_image_options_is_empty(rio.options)
return ret != 0
}