From 3c8b0ef35999d81483a1dc42c51ef87ddc76963d Mon Sep 17 00:00:00 2001 From: zhengyin Date: Tue, 17 Dec 2019 11:03:45 +0800 Subject: [PATCH] librbd: add snap_exists method API Signed-off-by: Zheng Yin --- src/include/rbd/librbd.h | 1 + src/librbd/librbd.cc | 10 ++++++++++ src/pybind/rbd/rbd.pyx | 19 +++++++++++++++++++ 3 files changed, 30 insertions(+) diff --git a/src/include/rbd/librbd.h b/src/include/rbd/librbd.h index a53e2c448fb..197e8d55d87 100644 --- a/src/include/rbd/librbd.h +++ b/src/include/rbd/librbd.h @@ -763,6 +763,7 @@ CEPH_RBD_API int rbd_deep_copy_with_progress(rbd_image_t image, CEPH_RBD_API int rbd_snap_list(rbd_image_t image, rbd_snap_info_t *snaps, int *max_snaps); CEPH_RBD_API void rbd_snap_list_end(rbd_snap_info_t *snaps); +CEPH_RBD_API int rbd_snap_exists(rbd_image_t image, const char *snapname, bool *exists); CEPH_RBD_API int rbd_snap_create(rbd_image_t image, const char *snapname); CEPH_RBD_API int rbd_snap_remove(rbd_image_t image, const char *snapname); CEPH_RBD_API int rbd_snap_remove2(rbd_image_t image, const char *snap_name, diff --git a/src/librbd/librbd.cc b/src/librbd/librbd.cc index 517fa4132dc..c0dbd82be2e 100644 --- a/src/librbd/librbd.cc +++ b/src/librbd/librbd.cc @@ -5292,6 +5292,16 @@ extern "C" int rbd_snap_get_limit(rbd_image_t image, uint64_t *limit) return r; } +extern "C" int rbd_snap_exists(rbd_image_t image, const char *snapname, bool *exists) +{ + librbd::ImageCtx *ictx = (librbd::ImageCtx *)image; + tracepoint(librbd, snap_exists_enter, ictx, ictx->name.c_str(), + ictx->snap_name.c_str(), ictx->read_only, snapname); + int r = librbd::api::Snapshot<>::exists(ictx, cls::rbd::UserSnapshotNamespace(), snapname, exists); + tracepoint(librbd, snap_exists_exit, r, *exists); + return r; +} + extern "C" int rbd_snap_get_timestamp(rbd_image_t image, uint64_t snap_id, struct timespec *timestamp) { librbd::ImageCtx *ictx = (librbd::ImageCtx *)image; diff --git a/src/pybind/rbd/rbd.pyx b/src/pybind/rbd/rbd.pyx index 6544bfbdf15..41534ac440a 100644 --- a/src/pybind/rbd/rbd.pyx +++ b/src/pybind/rbd/rbd.pyx @@ -502,6 +502,7 @@ cdef extern from "rbd/librbd.h" nogil: int rbd_snap_unprotect(rbd_image_t image, const char *snap_name) int rbd_snap_is_protected(rbd_image_t image, const char *snap_name, int *is_protected) + int rbd_snap_exists(rbd_image_t image, const char *snapname, bint *exists) int rbd_snap_get_limit(rbd_image_t image, uint64_t *limit) int rbd_snap_set_limit(rbd_image_t image, uint64_t limit) int rbd_snap_get_timestamp(rbd_image_t image, uint64_t snap_id, timespec *timestamp) @@ -3905,6 +3906,24 @@ cdef class Image(object): raise make_ex(ret, 'error checking if snapshot %s@%s is protected' % (self.name, name)) return is_protected == 1 + def snap_exists(self, name): + """ + Find out whether a snapshot is exists. + + :param name: the snapshot to check + :type name: str + :returns: bool - whether the snapshot is exists + """ + name = cstr(name, 'name') + cdef: + char *_name = name + bint _exists = False + with nogil: + ret = rbd_snap_exists(self.image, _name, &_exists) + if ret != 0: + raise make_ex(ret, 'error getting snapshot exists for %s' % self.name) + return bool(_exists != 0) + def get_snap_limit(self): """ Get the snapshot limit for an image. -- 2.39.5