diff --git a/rbd/rbd.go b/rbd/rbd.go index 9d54e86..cab39d8 100644 --- a/rbd/rbd.go +++ b/rbd/rbd.go @@ -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{ diff --git a/rbd/rbd_mimic.go b/rbd/rbd_mimic.go new file mode 100644 index 0000000..5e602ec --- /dev/null +++ b/rbd/rbd_mimic.go @@ -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 +// #include +// #include +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 + } +} diff --git a/rbd/rbd_nautilus.go b/rbd/rbd_nautilus.go new file mode 100644 index 0000000..f722c0a --- /dev/null +++ b/rbd/rbd_nautilus.go @@ -0,0 +1,45 @@ +// +build !luminous,!mimic +// +// Ceph Nautilus is the first release that includes rbd_list2(). + +package rbd + +// #cgo LDFLAGS: -lrbd +// #include +// #include +// #include +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 +}