rbd: make RBDError type unexported

In order to avoid external dependencies on implementation details,
this change replaces RBDError with the unexported rbdError. In case
some application really needs access to the integer value, it can use
the pattern
  var errno interface{ Errno() int }
  if errors.As(err, errno) { ... errno.Errno() ... }

Signed-off-by: Sven Anderson <sven@redhat.com>
This commit is contained in:
Sven Anderson 2020-07-14 01:23:13 +02:00 committed by John Mulligan
parent 5ee762c586
commit 6c944e8b65
9 changed files with 49 additions and 49 deletions

View File

@ -99,7 +99,7 @@ func (image *Image) DiffIterate(config DiffIterateConfig) error {
return err
}
if config.Callback == nil {
return RBDError(C.EINVAL)
return rbdError(C.EINVAL)
}
var cSnapName *C.char

View File

@ -335,8 +335,8 @@ func testDiffIterateEarlyExit(t *testing.T, ioctx *rados.IOContext) {
},
})
assert.Error(t, err)
if rbderr, ok := err.(RBDError); assert.True(t, ok) {
assert.EqualValues(t, -5, int(rbderr))
if errno, ok := err.(interface{ Errno() int }); assert.True(t, ok) {
assert.EqualValues(t, -5, errno.Errno())
}
if assert.Len(t, calls, 1) {
assert.EqualValues(t, 0, calls[0].offset)

View File

@ -12,14 +12,10 @@ import (
"github.com/ceph/go-ceph/internal/errutil"
)
// revive:disable:exported Temporarily live with stuttering
// rbdError represents an error condition returned from the librbd APIs.
type rbdError int
// RBDError represents an error condition returned from the librbd APIs.
type RBDError int
// revive:enable:exported
func (e RBDError) Error() string {
func (e rbdError) Error() string {
errno, s := errutil.FormatErrno(int(e))
if s == "" {
return fmt.Sprintf("rbd: ret=%d", errno)
@ -27,12 +23,16 @@ func (e RBDError) Error() string {
return fmt.Sprintf("rbd: ret=%d, %s", errno, s)
}
func (e rbdError) Errno() int {
return int(e)
}
func getError(err C.int) error {
if err != 0 {
if err == -C.ENOENT {
return ErrNotFound
}
return RBDError(err)
return rbdError(err)
}
return nil
}
@ -79,5 +79,5 @@ var (
// Private errors:
const (
errRange = RBDError(-C.ERANGE)
errRange = rbdError(-C.ERANGE)
)

View File

@ -152,7 +152,7 @@ func (image *Image) GetFeatures() (features uint64, err error) {
}
if ret := C.rbd_get_features(image.image, (*C.uint64_t)(&features)); ret < 0 {
return 0, RBDError(ret)
return 0, rbdError(ret)
}
return features, nil

View File

@ -182,7 +182,7 @@ func Create(ioctx *rados.IOContext, name string, size uint64, order int,
}
if ret < 0 {
return nil, RBDError(ret)
return nil, rbdError(ret)
}
return &Image{
@ -208,7 +208,7 @@ func Create2(ioctx *rados.IOContext, name string, size uint64, features uint64,
ret = C.rbd_create2(cephIoctx(ioctx), c_name,
C.uint64_t(size), C.uint64_t(features), &c_order)
if ret < 0 {
return nil, RBDError(ret)
return nil, rbdError(ret)
}
return &Image{
@ -237,7 +237,7 @@ func Create3(ioctx *rados.IOContext, name string, size uint64, features uint64,
C.uint64_t(size), C.uint64_t(features), &c_order,
C.uint64_t(stripe_unit), C.uint64_t(stripe_count))
if ret < 0 {
return nil, RBDError(ret)
return nil, rbdError(ret)
}
return &Image{
@ -271,7 +271,7 @@ func (image *Image) Clone(snapname string, c_ioctx *rados.IOContext, c_name stri
cephIoctx(c_ioctx),
c_c_name, C.uint64_t(features), &c_order)
if ret < 0 {
return nil, RBDError(ret)
return nil, rbdError(ret)
}
return &Image{
@ -320,7 +320,7 @@ func (image *Image) Rename(destname string) error {
defer C.free(unsafe.Pointer(c_srcname))
defer C.free(unsafe.Pointer(c_destname))
err := RBDError(C.rbd_rename(cephIoctx(image.ioctx),
err := rbdError(C.rbd_rename(cephIoctx(image.ioctx),
c_srcname, c_destname))
if err == 0 {
image.name = destname
@ -381,7 +381,7 @@ func (image *Image) Close() error {
}
if ret := C.rbd_close(image.image); ret != 0 {
return RBDError(ret)
return rbdError(ret)
}
image.image = nil
@ -412,7 +412,7 @@ func (image *Image) Stat() (info *ImageInfo, err error) {
var c_stat C.rbd_image_info_t
if ret := C.rbd_stat(image.image, &c_stat, C.size_t(unsafe.Sizeof(info))); ret < 0 {
return info, RBDError(ret)
return info, rbdError(ret)
}
return &ImageInfo{
@ -438,7 +438,7 @@ func (image *Image) IsOldFormat() (old_format bool, err error) {
ret := C.rbd_get_old_format(image.image,
&c_old_format)
if ret < 0 {
return false, RBDError(ret)
return false, rbdError(ret)
}
return c_old_format != 0, nil
@ -454,7 +454,7 @@ func (image *Image) GetSize() (size uint64, err error) {
}
if ret := C.rbd_get_size(image.image, (*C.uint64_t)(&size)); ret < 0 {
return 0, RBDError(ret)
return 0, rbdError(ret)
}
return size, nil
@ -470,7 +470,7 @@ func (image *Image) GetStripeUnit() (stripe_unit uint64, err error) {
}
if ret := C.rbd_get_stripe_unit(image.image, (*C.uint64_t)(&stripe_unit)); ret < 0 {
return 0, RBDError(ret)
return 0, rbdError(ret)
}
return stripe_unit, nil
@ -486,7 +486,7 @@ func (image *Image) GetStripeCount() (stripe_count uint64, err error) {
}
if ret := C.rbd_get_stripe_count(image.image, (*C.uint64_t)(&stripe_count)); ret < 0 {
return 0, RBDError(ret)
return 0, rbdError(ret)
}
return stripe_count, nil
@ -503,7 +503,7 @@ func (image *Image) GetOverlap() (overlap uint64, err error) {
}
if ret := C.rbd_get_overlap(image.image, (*C.uint64_t)(&overlap)); ret < 0 {
return overlap, RBDError(ret)
return overlap, rbdError(ret)
}
return overlap, nil
@ -602,7 +602,7 @@ func (image *Image) ListLockers() (tag string, lockers []Locker, err error) {
// but *0* is unexpected here because first rbd_list_lockers already
// dealt with no locker case
if int(c_locker_cnt) <= 0 {
return "", nil, RBDError(c_locker_cnt)
return "", nil, rbdError(c_locker_cnt)
}
clients := split(clients_buf)
@ -708,7 +708,7 @@ func (image *Image) Read(data []byte) (int, error) {
(*C.char)(unsafe.Pointer(&data[0]))))
if ret < 0 {
return 0, RBDError(ret)
return 0, rbdError(ret)
}
image.offset += int64(ret)
@ -733,7 +733,7 @@ func (image *Image) Write(data []byte) (n int, err error) {
}
if ret != len(data) {
err = RBDError(-C.EPERM)
err = rbdError(-C.EPERM)
}
return ret, err
@ -771,7 +771,7 @@ func (image *Image) Discard(ofs uint64, length uint64) (int, error) {
ret := C.rbd_discard(image.image, C.uint64_t(ofs), C.uint64_t(length))
if ret < 0 {
return 0, RBDError(ret)
return 0, rbdError(ret)
}
return int(ret), nil
@ -794,7 +794,7 @@ func (image *Image) ReadAt(data []byte, off int64) (int, error) {
(*C.char)(unsafe.Pointer(&data[0]))))
if ret < 0 {
return 0, RBDError(ret)
return 0, rbdError(ret)
}
if ret < len(data) {
@ -818,7 +818,7 @@ func (image *Image) WriteAt(data []byte, off int64) (n int, err error) {
C.size_t(len(data)), (*C.char)(unsafe.Pointer(&data[0]))))
if ret != len(data) {
err = RBDError(-C.EPERM)
err = rbdError(-C.EPERM)
}
return ret, err
@ -856,7 +856,7 @@ func (image *Image) GetSnapshotNames() (snaps []SnapInfo, err error) {
ret = C.rbd_snap_list(image.image,
&c_snaps[0], &c_max_snaps)
if ret < 0 {
return nil, RBDError(ret)
return nil, rbdError(ret)
}
for i, s := range c_snaps {
@ -917,7 +917,7 @@ func (image *Image) SetMetadata(key string, value string) error {
ret := C.rbd_metadata_set(image.image, c_key, c_value)
if ret < 0 {
return RBDError(ret)
return rbdError(ret)
}
return nil
@ -937,7 +937,7 @@ func (image *Image) RemoveMetadata(key string) error {
ret := C.rbd_metadata_remove(image.image, c_key)
if ret < 0 {
return RBDError(ret)
return rbdError(ret)
}
return nil
@ -1209,7 +1209,7 @@ func CreateImage(ioctx *rados.IOContext, name string, size uint64, rio *ImageOpt
return ErrNoName
}
if rio == nil {
return RBDError(C.EINVAL)
return rbdError(C.EINVAL)
}
c_name := C.CString(name)
@ -1248,7 +1248,7 @@ func CloneImage(ioctx *rados.IOContext, parentName, snapName string,
destctx *rados.IOContext, name string, rio *ImageOptions) error {
if rio == nil {
return RBDError(C.EINVAL)
return rbdError(C.EINVAL)
}
cParentName := C.CString(parentName)

View File

@ -66,7 +66,7 @@ func TestClosedImageNautilus(t *testing.T) {
err = image.Close()
assert.NoError(t, err)
// functions should now fail with an RBDError
// functions should now fail with an rbdError
_, err = image.GetCreateTimestamp()
assert.Error(t, err)

View File

@ -30,7 +30,7 @@ func (image *Image) CreateSnapshot(snapname string) (*Snapshot, error) {
ret := C.rbd_snap_create(image.image, c_snapname)
if ret < 0 {
return nil, RBDError(ret)
return nil, rbdError(ret)
}
return &Snapshot{
@ -139,7 +139,7 @@ func (snapshot *Snapshot) IsProtected() (bool, error) {
ret := C.rbd_snap_is_protected(snapshot.image.image, c_snapname,
&c_is_protected)
if ret < 0 {
return false, RBDError(ret)
return false, rbdError(ret)
}
return c_is_protected != 0, nil

View File

@ -40,7 +40,7 @@ func (image *Image) GetParentInfo(p_pool, p_name, p_snapname []byte) error {
if ret == 0 {
return nil
} else {
return RBDError(ret)
return rbdError(ret)
}
}
@ -66,7 +66,7 @@ func (image *Image) ListChildren() (pools []string, images []string, err error)
return nil, nil, nil
}
if ret < 0 && ret != -C.ERANGE {
return nil, nil, RBDError(ret)
return nil, nil, rbdError(ret)
}
pools_buf := make([]byte, c_pools_len)
@ -78,7 +78,7 @@ func (image *Image) ListChildren() (pools []string, images []string, err error)
(*C.char)(unsafe.Pointer(&images_buf[0])),
&c_images_len)
if ret < 0 {
return nil, nil, RBDError(ret)
return nil, nil, rbdError(ret)
}
tmp := bytes.Split(pools_buf[:c_pools_len-1], []byte{0})

View File

@ -32,7 +32,7 @@ func (image *Image) GetParentInfo(pool, name, snapname []byte) error {
parentSnap := C.rbd_snap_spec_t{}
ret := C.rbd_get_parent(image.image, &parentImage, &parentSnap)
if ret != 0 {
return RBDError(ret)
return rbdError(ret)
}
defer C.rbd_linked_image_spec_cleanup(&parentImage)
@ -40,26 +40,26 @@ func (image *Image) GetParentInfo(pool, name, snapname []byte) error {
strlen := int(C.strlen(parentImage.pool_name))
if len(pool) < strlen {
return RBDError(C.ERANGE)
return rbdError(C.ERANGE)
}
if copy(pool, C.GoString(parentImage.pool_name)) != strlen {
return RBDError(C.ERANGE)
return rbdError(C.ERANGE)
}
strlen = int(C.strlen(parentImage.image_name))
if len(name) < strlen {
return RBDError(C.ERANGE)
return rbdError(C.ERANGE)
}
if copy(name, C.GoString(parentImage.image_name)) != strlen {
return RBDError(C.ERANGE)
return rbdError(C.ERANGE)
}
strlen = int(C.strlen(parentSnap.name))
if len(snapname) < strlen {
return RBDError(C.ERANGE)
return rbdError(C.ERANGE)
}
if copy(snapname, C.GoString(parentSnap.name)) != strlen {
return RBDError(C.ERANGE)
return rbdError(C.ERANGE)
}
return nil