rbd: refactor GetImageNames to use a for loop instead of recursion

This is done because using a for loop is more idiomatic in Go code.

Signed-off-by: Sanford Miller <smiller@digitalocean.com>
This commit is contained in:
Sanford Miller 2022-06-14 11:09:17 -04:00 committed by mergify[bot]
parent 36d5c4498e
commit 133e675c8d
1 changed files with 15 additions and 19 deletions

View File

@ -19,10 +19,22 @@ import (
// GetImageNames returns the list of current RBD images.
func GetImageNames(ioctx *rados.IOContext) ([]string, error) {
var images []C.rbd_image_spec_t
size := C.size_t(4096)
images, err := getImageNames(ioctx, &size)
if err != nil {
return nil, err
for {
images = make([]C.rbd_image_spec_t, size)
ret := C.rbd_list2(
cephIoctx(ioctx),
(*C.rbd_image_spec_t)(unsafe.Pointer(&images[0])),
&size)
err := getErrorIfNegative(ret)
if err != nil {
if err == errRange {
continue
}
return nil, err
}
break
}
defer C.rbd_image_spec_list_cleanup((*C.rbd_image_spec_t)(unsafe.Pointer(&images[0])), size)
@ -33,22 +45,6 @@ func GetImageNames(ioctx *rados.IOContext) ([]string, error) {
return names, nil
}
func getImageNames(ioctx *rados.IOContext, size *C.size_t) ([]C.rbd_image_spec_t, error) {
images := make([]C.rbd_image_spec_t, *size)
ret := C.rbd_list2(
cephIoctx(ioctx),
(*C.rbd_image_spec_t)(unsafe.Pointer(&images[0])),
size)
err := getErrorIfNegative(ret)
if err != nil {
if err == errRange {
return getImageNames(ioctx, size)
}
return nil, err
}
return images, nil
}
// GetCreateTimestamp returns the time the rbd image was created.
//
// Implements: