]> git.ipfire.org Git - thirdparty/libvirt.git/commitdiff
virsh: Factor out function to find node device
authorJonathon Jongsma <jjongsma@redhat.com>
Mon, 17 Aug 2020 21:21:48 +0000 (16:21 -0500)
committerJonathon Jongsma <jjongsma@redhat.com>
Wed, 7 Apr 2021 20:14:01 +0000 (15:14 -0500)
Several functions accept providing a node device by name or by wwnn,wwpn
pair. Extract the logic to do this into a function that can be used by
both callers.

Signed-off-by: Jonathon Jongsma <jjongsma@redhat.com>
Reviewed-by: Erik Skultety <eskultet@redhat.com>
tools/virsh-nodedev.c

index 1cbf2e082ba91a81338fbdd6a12f9ccf9cd9ba12..5589a5862da751df0ce5d172977737b6df1763d4 100644 (file)
@@ -111,23 +111,18 @@ static const vshCmdOptDef opts_node_device_destroy[] = {
     {.name = NULL}
 };
 
-static bool
-cmdNodeDeviceDestroy(vshControl *ctl, const vshCmd *cmd)
+static virNodeDevice*
+vshFindNodeDevice(vshControl *ctl, const char *value)
 {
     virNodeDevicePtr dev = NULL;
-    bool ret = false;
-    const char *device_value = NULL;
     char **arr = NULL;
     int narr;
     virshControlPtr priv = ctl->privData;
 
-    if (vshCommandOptStringReq(ctl, cmd, "device", &device_value) < 0)
-        return false;
-
-    if (strchr(device_value, ',')) {
-        narr = vshStringToArray(device_value, &arr);
+    if (strchr(value, ',')) {
+        narr = vshStringToArray(value, &arr);
         if (narr != 2) {
-            vshError(ctl, _("Malformed device value '%s'"), device_value);
+            vshError(ctl, _("Malformed device value '%s'"), value);
             goto cleanup;
         }
 
@@ -136,14 +131,33 @@ cmdNodeDeviceDestroy(vshControl *ctl, const vshCmd *cmd)
 
         dev = virNodeDeviceLookupSCSIHostByWWN(priv->conn, arr[0], arr[1], 0);
     } else {
-        dev = virNodeDeviceLookupByName(priv->conn, device_value);
+        dev = virNodeDeviceLookupByName(priv->conn, value);
     }
 
     if (!dev) {
-        vshError(ctl, "%s '%s'", _("Could not find matching device"), device_value);
+        vshError(ctl, "%s '%s'", _("Could not find matching device"), value);
         goto cleanup;
     }
 
+ cleanup:
+    g_strfreev(arr);
+    return dev;
+}
+
+static bool
+cmdNodeDeviceDestroy(vshControl *ctl, const vshCmd *cmd)
+{
+    virNodeDevice *dev = NULL;
+    bool ret = false;
+    const char *device_value = NULL;
+
+    if (vshCommandOptStringReq(ctl, cmd, "device", &device_value) < 0)
+        return false;
+
+    dev = vshFindNodeDevice(ctl, device_value);
+    if (!dev)
+        goto cleanup;
+
     if (virNodeDeviceDestroy(dev) == 0) {
         vshPrintExtra(ctl, _("Destroyed node device '%s'\n"), device_value);
     } else {
@@ -153,7 +167,6 @@ cmdNodeDeviceDestroy(vshControl *ctl, const vshCmd *cmd)
 
     ret = true;
  cleanup:
-    g_strfreev(arr);
     if (dev)
         virNodeDeviceFree(dev);
     return ret;
@@ -578,33 +591,15 @@ cmdNodeDeviceDumpXML(vshControl *ctl, const vshCmd *cmd)
     virNodeDevicePtr device = NULL;
     char *xml = NULL;
     const char *device_value = NULL;
-    char **arr = NULL;
-    int narr;
     bool ret = false;
-    virshControlPtr priv = ctl->privData;
 
     if (vshCommandOptStringReq(ctl, cmd, "device", &device_value) < 0)
          return false;
 
-    if (strchr(device_value, ',')) {
-        narr = vshStringToArray(device_value, &arr);
-        if (narr != 2) {
-            vshError(ctl, _("Malformed device value '%s'"), device_value);
-            goto cleanup;
-        }
+    device = vshFindNodeDevice(ctl, device_value);
 
-        if (!virValidateWWN(arr[0]) || !virValidateWWN(arr[1]))
-            goto cleanup;
-
-        device = virNodeDeviceLookupSCSIHostByWWN(priv->conn, arr[0], arr[1], 0);
-    } else {
-        device = virNodeDeviceLookupByName(priv->conn, device_value);
-    }
-
-    if (!device) {
-        vshError(ctl, "%s '%s'", _("Could not find matching device"), device_value);
+    if (!device)
         goto cleanup;
-    }
 
     if (!(xml = virNodeDeviceGetXMLDesc(device, 0)))
         goto cleanup;
@@ -613,7 +608,6 @@ cmdNodeDeviceDumpXML(vshControl *ctl, const vshCmd *cmd)
 
     ret = true;
  cleanup:
-    g_strfreev(arr);
     VIR_FREE(xml);
     if (device)
         virNodeDeviceFree(device);