]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
librbd: change RBD::open to take a reference to an Image instead of a pointer
authorJosh Durgin <josh.durgin@dreamhost.com>
Wed, 2 Mar 2011 18:11:55 +0000 (10:11 -0800)
committerJosh Durgin <josh.durgin@dreamhost.com>
Wed, 2 Mar 2011 18:20:43 +0000 (10:20 -0800)
This makes the API more consistent with the librados API.

Signed-off-by: Josh Durgin <josh.durgin@dreamhost.com>
src/include/rbd/librbd.hpp
src/librbd.cc
src/rbd.cc
src/testlibrbdpp.cc

index ce73a4dc9e2ae8764c8a0ffb8f85ddbab811c783..66a46a66d59f96c2d37d9ba0a382e973107d36d6 100644 (file)
@@ -57,8 +57,8 @@ public:
 
   void version(int *major, int *minor, int *extra);
 
-  int open(IoCtx& io_ctx, Image *image, const char *name);
-  int open(IoCtx& io_ctx, Image *image, const char *name, const char *snapname);
+  int open(IoCtx& io_ctx, Imageimage, const char *name);
+  int open(IoCtx& io_ctx, Imageimage, const char *name, const char *snapname);
   int list(IoCtx& io_ctx, std::vector<std::string>& names);
   int create(IoCtx& io_ctx, const char *name, size_t size, int *order);
   int remove(IoCtx& io_ctx, const char *name);
@@ -74,6 +74,7 @@ private:
 class Image
 {
 public:
+  Image();
   ~Image();
 
   int resize(size_t size);
@@ -96,8 +97,6 @@ public:
   int aio_read(off_t off, size_t len, ceph::bufferlist& bl, RBD::AioCompletion *c);
 
 private:
-  /* Image instances only come from RBD::open */
-  Image(image_ctx_t ctx_);
   friend class RBD;
 
   Image(const Image& rhs);
index 58be33081d05d379e9fa24adaaaac94d7c06e836..74f551284df97ef37e686725c686e3fcd86c8d89 100644 (file)
@@ -1308,12 +1308,12 @@ void RBD::version(int *major, int *minor, int *extra)
   rbd_version(major, minor, extra);
 }
 
-int RBD::open(IoCtx& io_ctx, Image *image, const char *name)
+int RBD::open(IoCtx& io_ctx, Imageimage, const char *name)
 {
   return open(io_ctx, image, name, NULL);
 }
 
-int RBD::open(IoCtx& io_ctx, Image *image, const char *name, const char *snapname)
+int RBD::open(IoCtx& io_ctx, Imageimage, const char *name, const char *snapname)
 {
   ImageCtx *ictx = new ImageCtx(name, io_ctx);
   if (!ictx)
@@ -1323,7 +1323,7 @@ int RBD::open(IoCtx& io_ctx, Image *image, const char *name, const char *snapnam
   if (r < 0)
     return r;
 
-  image = new Image((image_ctx_t)ictx);
+  image.ctx = (image_ctx_t) ictx;
   return 0;
 }
 
@@ -1388,7 +1388,7 @@ void RBD::AioCompletion::release()
   Image
 */
 
-Image::Image(image_ctx_t ctx_) : ctx(ctx_)
+Image::Image() : ctx(NULL)
 {
 }
 
index 1afa9c394e77abc02dc5ebfc3f950b922140ebb4..1e5531bf1d98ca2b5b5903c4d04054f31b4a5679 100644 (file)
@@ -129,10 +129,10 @@ static int do_rename(librbd::RBD &rbd, librados::IoCtx& io_ctx,
   return 0;
 }
 
-static int do_show_info(const char *imgname, librbd::Image *image)
+static int do_show_info(const char *imgname, librbd::Imageimage)
 {
   librbd::image_info_t info;
-  int r = image->stat(info, sizeof(info));
+  int r = image.stat(info, sizeof(info));
   if (r < 0)
     return r;
 
@@ -149,19 +149,19 @@ static int do_show_info(const char *imgname, librbd::Image *image)
   return 0;
 }
 
-static int do_resize(librbd::Image *image, size_t size)
+static int do_resize(librbd::Imageimage, size_t size)
 {
-  int r = image->resize(size);
+  int r = image.resize(size);
   if (r < 0)
     return r;
 
   return 0;
 }
 
-static int do_list_snaps(librbd::Image *image)
+static int do_list_snaps(librbd::Imageimage)
 {
   std::vector<librbd::snap_info_t> snaps;
-  int r = image->snap_list(snaps);
+  int r = image.snap_list(snaps);
   if (r < 0)
     return r;
 
@@ -171,27 +171,27 @@ static int do_list_snaps(librbd::Image *image)
   return 0;
 }
 
-static int do_add_snap(librbd::Image *image, const char *snapname)
+static int do_add_snap(librbd::Imageimage, const char *snapname)
 {
-  int r = image->snap_create(snapname);
+  int r = image.snap_create(snapname);
   if (r < 0)
     return r;
 
   return 0;
 }
 
-static int do_remove_snap(librbd::Image *image, const char *snapname)
+static int do_remove_snap(librbd::Imageimage, const char *snapname)
 {
-  int r = image->snap_remove(snapname);
+  int r = image.snap_remove(snapname);
   if (r < 0)
     return r;
 
   return 0;
 }
 
-static int do_rollback_snap(librbd::Image *image, const char *snapname)
+static int do_rollback_snap(librbd::Imageimage, const char *snapname)
 {
-  int r = image->snap_rollback(snapname);
+  int r = image.snap_rollback(snapname);
   if (r < 0)
     return r;
 
@@ -217,7 +217,7 @@ static int export_read_cb(off_t ofs, size_t len, const char *buf, void *arg)
   return 0;
 }
 
-static int do_export(librbd::Image *image, const char *path)
+static int do_export(librbd::Imageimage, const char *path)
 {
   int r;
   librbd::image_info_t info;
@@ -226,11 +226,11 @@ static int do_export(librbd::Image *image, const char *path)
   if (fd < 0)
     return -errno;
 
-  r = image->stat(info, sizeof(info));
+  r = image.stat(info, sizeof(info));
   if (r < 0)
     return r;
 
-  r = image->read_iterate(0, info.size, export_read_cb, (void *)&fd);
+  r = image.read_iterate(0, info.size, export_read_cb, (void *)&fd);
   if (r < 0)
     return r;
 
@@ -343,7 +343,7 @@ static int do_import(librbd::RBD &rbd, librados::IoCtx& io_ctx,
     cerr << "image creation failed" << std::endl;
     return r;
   }
-  librbd::Image *image = NULL;
+  librbd::Image image;
   r = rbd.open(io_ctx, image, imgname);
   if (r < 0) {
     cerr << "failed to open image" << std::endl;
@@ -422,7 +422,7 @@ static int do_import(librbd::RBD &rbd, librados::IoCtx& io_ctx,
           r = -ENOMEM;
           goto done;
         }
-        r = image->aio_write(file_pos, len, bl, completion);
+        r = image.aio_write(file_pos, len, bl, completion);
         if (r < 0)
           goto done;
        completion->wait_for_complete();
@@ -573,7 +573,7 @@ int main(int argc, const char **argv)
   librados::Rados rados;
   librbd::RBD rbd;
   librados::IoCtx io_ctx, dest_io_ctx;
-  std::auto_ptr < librbd::Image > image;
+  librbd::Image image;
 
   vector<const char*> args;
   DEFINE_CONF_VARS(usage_exit);
@@ -708,17 +708,15 @@ int main(int argc, const char **argv)
       (opt_cmd == OPT_RESIZE || opt_cmd == OPT_INFO || opt_cmd == OPT_SNAP_LIST ||
        opt_cmd == OPT_SNAP_CREATE || opt_cmd == OPT_SNAP_ROLLBACK ||
        opt_cmd == OPT_SNAP_REMOVE || opt_cmd == OPT_EXPORT || opt_cmd == OPT_WATCH)) {
-    librbd::Image *image_ptr = NULL;
-    r = rbd.open(io_ctx, image_ptr, imgname);
+    r = rbd.open(io_ctx, image, imgname);
     if (r < 0) {
       cerr << "error opening image " << imgname << ": " << strerror(r) << std::endl;
       exit(1);
     }
-    image.reset(image_ptr);
   }
 
   if (snapname) {
-    r = image->snap_set(snapname);
+    r = image.snap_set(snapname);
     if (r < 0 && !(r == -ENOENT && opt_cmd == OPT_SNAP_CREATE)) {
       cerr << "error setting snapshot context: " << strerror(-r) << std::endl;
       exit(1);
@@ -775,7 +773,7 @@ int main(int argc, const char **argv)
     break;
 
   case OPT_INFO:
-    r = do_show_info(imgname, image.get());
+    r = do_show_info(imgname, image);
     if (r < 0) {
       cerr << "error: " << strerror(-r) << std::endl;
       exit(1);
@@ -791,7 +789,7 @@ int main(int argc, const char **argv)
     break;
 
   case OPT_RESIZE:
-    r = do_resize(image.get(), size);
+    r = do_resize(image, size);
     if (r < 0) {
       cerr << "resize error: " << strerror(-r) << std::endl;
       exit(1);
@@ -803,7 +801,7 @@ int main(int argc, const char **argv)
       usage();
       exit(1);
     }
-    r = do_list_snaps(image.get());
+    r = do_list_snaps(image);
     if (r < 0) {
       cerr << "failed to list snapshots: " << strerror(-r) << std::endl;
       exit(1);
@@ -815,7 +813,7 @@ int main(int argc, const char **argv)
       usage();
       exit(1);
     }
-    r = do_add_snap(image.get(), snapname);
+    r = do_add_snap(image, snapname);
     if (r < 0) {
       cerr << "failed to create snapshot: " << strerror(-r) << std::endl;
       exit(1);
@@ -827,7 +825,7 @@ int main(int argc, const char **argv)
       usage();
       exit(1);
     }
-    r = do_rollback_snap(image.get(), snapname);
+    r = do_rollback_snap(image, snapname);
     if (r < 0) {
       cerr << "rollback failed: " << strerror(-r) << std::endl;
       exit(1);
@@ -839,7 +837,7 @@ int main(int argc, const char **argv)
       usage();
       exit(1);
     }
-    r = do_remove_snap(image.get(), snapname);
+    r = do_remove_snap(image, snapname);
     if (r < 0) {
       cerr << "rollback failed: " << strerror(-r) << std::endl;
       exit(1);
@@ -851,7 +849,7 @@ int main(int argc, const char **argv)
       cerr << "pathname should be specified" << std::endl;
       exit(1);
     }
-    r = do_export(image.get(), path);
+    r = do_export(image, path);
     if (r < 0) {
       cerr << "export error: " << strerror(-r) << std::endl;
       exit(1);
index 21b8111bd6c6930cb1a5de2b24e58c13e0dd521a..6016bcc6255617af5363b73dd5c0f2cc688bc3dc 100644 (file)
@@ -41,22 +41,21 @@ librbd::RBD *rbd;
 void test_create_and_stat(librados::IoCtx& io_ctx, const char *name, size_t size)
 {
   librbd::image_info_t info;
-  librbd::Image *image = NULL;
+  librbd::Image image;
   int order = 0;
   assert(rbd->create(io_ctx, name, size, &order) == 0);
   assert(rbd->open(io_ctx, image, name, NULL) == 0);
-  assert(image->stat(info, sizeof(info)) == 0);
+  assert(image.stat(info, sizeof(info)) == 0);
   cout << "image has size " << info.size << " and order " << info.order << endl;
   assert(info.size == size);
   assert(info.order == order);
-  delete image;
 }
 
-void test_resize_and_stat(librbd::Image *image, size_t size)
+void test_resize_and_stat(librbd::Imageimage, size_t size)
 {
   librbd::image_info_t info;
-  assert(image->resize(size) == 0);
-  assert(image->stat(info, sizeof(info)) == 0);
+  assert(image.resize(size) == 0);
+  assert(image.stat(info, sizeof(info)) == 0);
   cout << "image has size " << info.size << " and order " << info.order << endl;
   assert(info.size == size);
 }
@@ -96,19 +95,19 @@ void test_delete(librados::IoCtx& io_ctx, const char *name)
   assert(rbd->remove(io_ctx, name) == 0);
 }
 
-void test_create_snap(librbd::Image *image, const char *name)
+void test_create_snap(librbd::Imageimage, const char *name)
 {
-  assert(image->snap_create(name) == 0);
+  assert(image.snap_create(name) == 0);
 }
 
-void test_ls_snaps(librbd::Image *image, size_t num_expected, ...)
+void test_ls_snaps(librbd::Imageimage, size_t num_expected, ...)
 {
   int r;
   size_t i, j, expected_size;
   char *expected;
   va_list ap;
   vector<librbd::snap_info_t> snaps;
-  r = image->snap_list(snaps);
+  r = image.snap_list(snaps);
   assert(r >= 0);
   cout << "num snaps is: " << snaps.size() << endl
        << "expected: " << num_expected << endl;
@@ -142,9 +141,9 @@ void test_ls_snaps(librbd::Image *image, size_t num_expected, ...)
   }
 }
 
-void test_delete_snap(librbd::Image *image, const char *name)
+void test_delete_snap(librbd::Imageimage, const char *name)
 {
-  assert(image->snap_remove(name) == 0);
+  assert(image.snap_remove(name) == 0);
 }
 
 void simple_write_cb(librbd::completion_t cb, void *arg)
@@ -157,13 +156,13 @@ void simple_read_cb(librbd::completion_t cb, void *arg)
   cout << "read completion cb called!" << endl;
 }
 
-void aio_write_test_data(librbd::Image *image, const char *test_data, off_t off)
+void aio_write_test_data(librbd::Imageimage, const char *test_data, off_t off)
 {
   ceph::bufferlist bl;
   bl.append(test_data, strlen(test_data));
   librbd::RBD::AioCompletion *comp = new librbd::RBD::AioCompletion(NULL, (librbd::callback_t) simple_write_cb);
   printf("created completion\n");
-  image->aio_write(off, strlen(test_data), bl, comp);
+  image.aio_write(off, strlen(test_data), bl, comp);
   printf("started write\n");
   comp->wait_for_complete();
   int r = comp->get_return_value();
@@ -174,23 +173,23 @@ void aio_write_test_data(librbd::Image *image, const char *test_data, off_t off)
   delete comp;
 }
 
-void write_test_data(librbd::Image *image, const char *test_data, off_t off)
+void write_test_data(librbd::Imageimage, const char *test_data, off_t off)
 {
   size_t written;
   size_t len = strlen(test_data);
   ceph::bufferlist bl;
   bl.append(test_data, len);
-  written = image->write(off, len, bl);
+  written = image.write(off, len, bl);
   assert(written >= 0);
   printf("wrote: %u\n", (unsigned int) written);
 }
 
-void aio_read_test_data(librbd::Image *image, const char *expected, off_t off)
+void aio_read_test_data(librbd::Imageimage, const char *expected, off_t off)
 {
   librbd::RBD::AioCompletion *comp = new librbd::RBD::AioCompletion(NULL, (librbd::callback_t) simple_read_cb);
   ceph::bufferlist bl;
   printf("created completion\n");
-  image->aio_read(off, strlen(expected), bl, comp);
+  image.aio_read(off, strlen(expected), bl, comp);
   printf("started read\n");
   comp->wait_for_complete();
   int r = comp->get_return_value();
@@ -202,20 +201,20 @@ void aio_read_test_data(librbd::Image *image, const char *expected, off_t off)
   delete comp;
 }
 
-void read_test_data(librbd::Image *image, const char *expected, off_t off)
+void read_test_data(librbd::Imageimage, const char *expected, off_t off)
 {
   size_t read, total_read = 0;
   size_t expected_len = strlen(expected);
   size_t len = expected_len;
   ceph::bufferlist bl;
-  read = image->read(off + total_read, len, bl);
+  read = image.read(off + total_read, len, bl);
   assert(read >= 0);
   printf("read: %u\n", (unsigned int) read);
   printf("read: %s\nexpected: %s\n", bl.c_str(), expected);
   assert(strncmp(bl.c_str(), expected, expected_len) == 0);
 }
 
-void test_io(librados::IoCtx& io_ctx, librbd::Image *image)
+void test_io(librados::IoCtx& io_ctx, librbd::Imageimage)
 {
   char test_data[TEST_IO_SIZE];
   int i;
@@ -244,7 +243,7 @@ int main(int argc, const char **argv)
 {
   librados::Rados rados;
   librados::IoCtx io_ctx;
-  std::auto_ptr< librbd::Image > image;
+  librbd::Image image;
   rbd = new librbd::RBD();
   assert(rados.init(NULL) == 0);
   assert(rados.connect() == 0);
@@ -257,19 +256,19 @@ int main(int argc, const char **argv)
   assert(rados.ioctx_create(TEST_POOL, io_ctx) == 0);
   test_ls(io_ctx, 0);
   test_create_and_stat(io_ctx, TEST_IMAGE, MB_BYTES(1));
-  assert(rbd->open(io_ctx, image.get(), TEST_IMAGE, NULL) == 0);
+  assert(rbd->open(io_ctx, image, TEST_IMAGE, NULL) == 0);
   test_ls(io_ctx, 1, TEST_IMAGE);
-  test_ls_snaps(image.get(), 0);
-  test_create_snap(image.get(), TEST_SNAP);
-  test_ls_snaps(image.get(), 1, TEST_SNAP, MB_BYTES(1));
-  test_resize_and_stat(image.get(), MB_BYTES(2));
-  test_io(io_ctx, image.get());
-  test_create_snap(image.get(), TEST_SNAP "1");
-  test_ls_snaps(image.get(), 2, TEST_SNAP, MB_BYTES(1), TEST_SNAP "1", MB_BYTES(2));
-  test_delete_snap(image.get(), TEST_SNAP);
-  test_ls_snaps(image.get(), 1, TEST_SNAP "1", MB_BYTES(2));
-  test_delete_snap(image.get(), TEST_SNAP "1");
-  test_ls_snaps(image.get(), 0);
+  test_ls_snaps(image, 0);
+  test_create_snap(image, TEST_SNAP);
+  test_ls_snaps(image, 1, TEST_SNAP, MB_BYTES(1));
+  test_resize_and_stat(image, MB_BYTES(2));
+  test_io(io_ctx, image);
+  test_create_snap(image, TEST_SNAP "1");
+  test_ls_snaps(image, 2, TEST_SNAP, MB_BYTES(1), TEST_SNAP "1", MB_BYTES(2));
+  test_delete_snap(image, TEST_SNAP);
+  test_ls_snaps(image, 1, TEST_SNAP "1", MB_BYTES(2));
+  test_delete_snap(image, TEST_SNAP "1");
+  test_ls_snaps(image, 0);
   test_create_and_stat(io_ctx, TEST_IMAGE "1", MB_BYTES(2));
   test_ls(io_ctx, 2, TEST_IMAGE, TEST_IMAGE "1");
   test_delete(io_ctx, TEST_IMAGE);