rbd: replace deprecated rbd_list() with rbd_list2()

Signed-off-by: Niels de Vos <ndevos@redhat.com>
This commit is contained in:
Niels de Vos 2020-01-28 10:42:33 +01:00 committed by John Mulligan
parent 58b4237aa9
commit efe2f6b97c
3 changed files with 88 additions and 24 deletions

View File

@ -203,30 +203,6 @@ func Version() (int, int, int) {
return int(c_major), int(c_minor), int(c_patch)
}
// GetImageNames returns the list of current RBD images.
func GetImageNames(ioctx *rados.IOContext) (names []string, err error) {
buf := make([]byte, 4096)
for {
size := C.size_t(len(buf))
ret := C.rbd_list(C.rados_ioctx_t(ioctx.Pointer()),
(*C.char)(unsafe.Pointer(&buf[0])), &size)
if ret == -C.ERANGE {
buf = make([]byte, size)
continue
} else if ret < 0 {
return nil, RBDError(ret)
}
tmp := bytes.Split(buf[:size-1], []byte{0})
for _, s := range tmp {
if len(s) > 0 {
name := C.GoString((*C.char)(unsafe.Pointer(&s[0])))
names = append(names, name)
}
}
return names, nil
}
}
// GetImage gets a reference to a previously created rbd image.
func GetImage(ioctx *rados.IOContext, name string) *Image {
return &Image{

43
rbd/rbd_mimic.go Normal file
View File

@ -0,0 +1,43 @@
// +build luminous mimic
// +build !nautilus
//
// Ceph Nautilus includes rbd_list2() and marked rbd_list() deprecated.
package rbd
// #cgo LDFLAGS: -lrbd
// #include <rados/librados.h>
// #include <rbd/librbd.h>
// #include <errno.h>
import "C"
import (
"bytes"
"unsafe"
"github.com/ceph/go-ceph/rados"
)
// GetImageNames returns the list of current RBD images.
func GetImageNames(ioctx *rados.IOContext) (names []string, err error) {
buf := make([]byte, 4096)
for {
size := C.size_t(len(buf))
ret := C.rbd_list(C.rados_ioctx_t(ioctx.Pointer()),
(*C.char)(unsafe.Pointer(&buf[0])), &size)
if ret == -C.ERANGE {
buf = make([]byte, size)
continue
} else if ret < 0 {
return nil, RBDError(ret)
}
tmp := bytes.Split(buf[:size-1], []byte{0})
for _, s := range tmp {
if len(s) > 0 {
name := C.GoString((*C.char)(unsafe.Pointer(&s[0])))
names = append(names, name)
}
}
return names, nil
}
}

45
rbd/rbd_nautilus.go Normal file
View File

@ -0,0 +1,45 @@
// +build !luminous,!mimic
//
// Ceph Nautilus is the first release that includes rbd_list2().
package rbd
// #cgo LDFLAGS: -lrbd
// #include <rados/librados.h>
// #include <rbd/librbd.h>
// #include <errno.h>
import "C"
import (
"fmt"
"unsafe"
"github.com/ceph/go-ceph/rados"
)
// GetImageNames returns the list of current RBD images.
func GetImageNames(ioctx *rados.IOContext) ([]string, error) {
size := C.size_t(0)
ret := C.rbd_list2(C.rados_ioctx_t(ioctx.Pointer()), nil, &size)
if ret < 0 && ret != -C.ERANGE {
return nil, RBDError(ret)
} else if ret > 0 {
return nil, fmt.Errorf("rbd_list2() returned %d names, expected 0", ret)
} else if ret == 0 && size == 0 {
return nil, nil
}
// expected: ret == -ERANGE, size contains number of image names
images := make([]C.rbd_image_spec_t, size)
ret = C.rbd_list2(C.rados_ioctx_t(ioctx.Pointer()), (*C.rbd_image_spec_t)(unsafe.Pointer(&images[0])), &size)
if ret < 0 {
return nil, RBDError(ret)
}
defer C.rbd_image_spec_list_cleanup((*C.rbd_image_spec_t)(unsafe.Pointer(&images[0])), size)
names := make([]string, size)
for i, image := range images {
names[i] = C.GoString(image.name)
}
return names, nil
}