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(a)redhat.com>
Reviewed-by: Erik Skultety <eskultet(a)redhat.com>
---
tools/virsh-nodedev.c | 62 +++++++++++++++++++------------------------
1 file changed, 28 insertions(+), 34 deletions(-)
diff --git a/tools/virsh-nodedev.c b/tools/virsh-nodedev.c
index 2ad676fc91..ed45ceea5e 100644
--- a/tools/virsh-nodedev.c
+++ b/tools/virsh-nodedev.c
@@ -111,23 +111,18 @@ static const vshCmdOptDef opts_node_device_destroy[] = {
{.name = NULL}
};
-static bool
-cmdNodeDeviceDestroy(vshControl *ctl, const vshCmd *cmd)
+static virNodeDevicePtr
+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)
+{
+ virNodeDevicePtr 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);
--
2.26.2