Пример #1
0
// int rbd_open(rados_ioctx_t io, const char *name, rbd_image_t *image, const char *snap_name);
// int rbd_open_read_only(rados_ioctx_t io, const char *name, rbd_image_t *image,
//                const char *snap_name);
func (image *Image) Open(args ...interface{}) error {
	var c_image C.rbd_image_t
	var c_name *C.char = C.CString(image.name)
	var c_snap_name *C.char
	var ret C.int
	var read_only bool = false

	defer C.free(unsafe.Pointer(c_name))
	for _, arg := range args {
		switch t := arg.(type) {
		case string:
			if t != "" {
				c_snap_name = C.CString(t)
				defer C.free(unsafe.Pointer(c_snap_name))
			}
		case bool:
			read_only = t
		default:
			return errors.New("Unexpected argument")
		}
	}

	if read_only {
		ret = C.rbd_open_read_only(C.rados_ioctx_t(image.ioctx.Pointer()), c_name,
			&c_image, c_snap_name)
	} else {
		ret = C.rbd_open(C.rados_ioctx_t(image.ioctx.Pointer()), c_name,
			&c_image, c_snap_name)
	}

	image.image = c_image

	return GetError(ret)
}
Пример #2
0
func (p *Pool) wrapOpen(imageName string, action func(*C.rbd_image_t) error) error {
	image := C.make_image()
	imageStr := C.CString(imageName)
	defer func() {
		C.free(unsafe.Pointer(image))
		C.free(unsafe.Pointer(imageStr))
	}()

	if i, err := C.rbd_open(p.ioctx, imageStr, image, nil); err != nil || i < 0 {
		if i < 0 {
			err = strerror(i)
		}

		return fmt.Errorf("Error creating snapshot: %v", err)
	}

	defer func() error {
		if i, err := C.rbd_close(*image); err != nil || i < 0 {
			if i < 0 {
				err = strerror(i)
			}

			return fmt.Errorf("Error creating snapshot: %v", err)
		}

		return nil
	}()

	if err := action(image); err != nil {
		return err
	}

	return nil
}