Codebase list golang-gopkg-lxc-go-lxc.v2 / upstream/0.0_git20181101.0aadfc3
Merge pull request #112 from brauner/v2 bindings: add runtime version check Stéphane Graber authored 5 years ago GitHub committed 5 years ago
1 changed file(s) with 78 addition(s) and 4 deletion(s). Raw diff Collapse all Expand all
1818 import (
1919 "fmt"
2020 "runtime"
21 "strconv"
2122 "strings"
2223 "unsafe"
2324 )
243244 return bool(C.go_lxc_config_item_is_supported(configItem))
244245 }
245246
247 // runtimeLiblxcVersionAtLeast checks if the system's liblxc matches the
248 // provided version requirement
249 func runtimeLiblxcVersionAtLeast(major int, minor int, micro int) bool {
250 version := Version()
251 version = strings.Replace(version, " (devel)", "-devel", 1)
252 parts := strings.Split(version, ".")
253 partsLen := len(parts)
254 if partsLen == 0 {
255 return false
256 }
257
258 develParts := strings.Split(parts[partsLen-1], "-")
259 if len(develParts) == 2 && develParts[1] == "devel" {
260 return true
261 }
262
263 maj := -1
264 min := -1
265 mic := -1
266
267 for i, v := range parts {
268 if i > 2 {
269 break
270 }
271
272 num, err := strconv.Atoi(v)
273 if err != nil {
274 return false
275 }
276
277 switch i {
278 case 0:
279 maj = num
280 case 1:
281 min = num
282 case 2:
283 mic = num
284 }
285 }
286
287 /* Major version is greater. */
288 if maj > major {
289 return true
290 }
291
292 if maj < major {
293 return false
294 }
295
296 /* Minor number is greater.*/
297 if min > minor {
298 return true
299 }
300
301 if min < minor {
302 return false
303 }
304
305 /* Patch number is greater. */
306 if mic > micro {
307 return true
308 }
309
310 if mic < micro {
311 return false
312 }
313
314 return true
315 }
316
246317 // HasApiExtension returns true if the extension is supported.
247318 func HasApiExtension(extension string) bool {
248 apiExtension := C.CString(extension)
249 defer C.free(unsafe.Pointer(apiExtension))
250 return bool(C.go_lxc_has_api_extension(apiExtension))
251 }
319 if runtimeLiblxcVersionAtLeast(3, 1, 0) {
320 apiExtension := C.CString(extension)
321 defer C.free(unsafe.Pointer(apiExtension))
322 return bool(C.go_lxc_has_api_extension(apiExtension))
323 }
324 return false
325 }