]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
rbd nbd: move nbd index parsing to function
authorMike Christie <mchristi@redhat.com>
Wed, 1 May 2019 00:27:09 +0000 (19:27 -0500)
committerMike Christie <mchristi@redhat.com>
Wed, 5 Jun 2019 04:56:53 +0000 (23:56 -0500)
Make the nbd index parsing into a functio so we can use in the ioctl and
netlink paths.

Signed-off-by: Mike Christie <mchristi@redhat.com>
src/tools/rbd_nbd/rbd-nbd.cc

index 5485ce0226bb0a7a0368ab999420dc80d7754319..c51a64b36c775dfc88e1b434c9a16e1131dd64cd 100644 (file)
@@ -644,6 +644,23 @@ static int check_device_size(int nbd_index, unsigned long expected_size)
   return 0;
 }
 
+static int parse_nbd_index(const std::string& devpath)
+{
+  int index, ret;
+
+  ret = sscanf(devpath.c_str(), "/dev/nbd%d", &index);
+  if (ret <= 0) {
+    // mean an early matching failure. But some cases need a negative value.
+    if (ret == 0)
+      ret = -EINVAL;
+    cerr << "rbd-nbd: invalid device path: " <<  devpath
+         << " (expected /dev/nbd{num})" << std::endl;
+    return ret;
+  }
+
+  return index;
+}
+
 static int try_ioctl_setup(Config *cfg, int fd, uint64_t size, int *nbd_index,
                            uint64_t flags)
 {
@@ -689,15 +706,11 @@ static int try_ioctl_setup(Config *cfg, int fd, uint64_t size, int *nbd_index,
       break;
     }
   } else {
-    r = sscanf(cfg->devpath.c_str(), "/dev/nbd%d", &index);
-    if (r <= 0) {
-      // mean an early matching failure. But some cases need a negative value.
-      if (r == 0)
-       r = -EINVAL;
-      cerr << "rbd-nbd: invalid device path: " << cfg->devpath
-           << " (expected /dev/nbd{num})" << std::endl;
+    r = parse_nbd_index(cfg->devpath);
+    if (r < 0)
       goto done;
-    }
+    index = r;
+
     nbd = open_device(cfg->devpath.c_str(), cfg, true);
     if (nbd < 0) {
       r = nbd;