提交 1aa0ba3c 编写于 作者: O Osier Yang

nodedev_udev: Refactor udevGetDeviceType

Checking if the "devtype" is NULL along with each "if" statements
is bad. It wastes the performance, and also not good for reading.
And also when the "devtype" is NULL, the logic is also not clear.

This reorgnizes the logic of with "if...else" and a bunch of "else if".

Other changes:
   * Change the function style.
   * Remove the useless debug statement.
   * Get rid of the goto
   * New helper udevDeviceHasProperty to simplify the logic for checking
     if a property is existing for the device.
   * Add comment to clarify "PCI devices don't set the DEVTYPE property"
   * s/sysfs path/sysfs name/, as udev_device_get_sysname returns the
     name instead of the full path. E.g. "sg0"
   * Refactor the comment for setting VIR_NODE_DEV_CAP_NET cap type
     a bit.
上级 c4a4603d
...@@ -106,7 +106,6 @@ static int udevStrToLong_i(char const *s, ...@@ -106,7 +106,6 @@ static int udevStrToLong_i(char const *s,
return ret; return ret;
} }
/* This function allocates memory from the heap for the property /* This function allocates memory from the heap for the property
* value. That memory must be later freed by some other code. */ * value. That memory must be later freed by some other code. */
static int udevGetDeviceProperty(struct udev_device *udev_device, static int udevGetDeviceProperty(struct udev_device *udev_device,
...@@ -1122,78 +1121,64 @@ out: ...@@ -1122,78 +1121,64 @@ out:
return ret; return ret;
} }
static bool
udevDeviceHasProperty(struct udev_device *dev,
const char *key)
{
const char *value = NULL;
bool ret = false;
if ((value = udev_device_get_property_value(dev, key)))
ret = true;
return ret;
}
static int udevGetDeviceType(struct udev_device *device, static int
udevGetDeviceType(struct udev_device *device,
enum virNodeDevCapType *type) enum virNodeDevCapType *type)
{ {
const char *devtype = NULL; const char *devtype = NULL;
char *tmp_string = NULL; int ret = -1;
unsigned int tmp = 0;
int ret = 0;
devtype = udev_device_get_devtype(device); devtype = udev_device_get_devtype(device);
VIR_DEBUG("Found device type '%s' for device '%s'", *type = 0;
NULLSTR(devtype), udev_device_get_sysname(device));
if (devtype != NULL && STREQ(devtype, "usb_device")) { if (devtype) {
if (STREQ(devtype, "usb_device"))
*type = VIR_NODE_DEV_CAP_USB_DEV; *type = VIR_NODE_DEV_CAP_USB_DEV;
goto out; else if (STREQ(devtype, "usb_interface"))
}
if (devtype != NULL && STREQ(devtype, "usb_interface")) {
*type = VIR_NODE_DEV_CAP_USB_INTERFACE; *type = VIR_NODE_DEV_CAP_USB_INTERFACE;
goto out; else if (STREQ(devtype, "scsi_host"))
}
if (devtype != NULL && STREQ(devtype, "scsi_host")) {
*type = VIR_NODE_DEV_CAP_SCSI_HOST; *type = VIR_NODE_DEV_CAP_SCSI_HOST;
goto out; else if (STREQ(devtype, "scsi_target"))
}
if (devtype != NULL && STREQ(devtype, "scsi_target")) {
*type = VIR_NODE_DEV_CAP_SCSI_TARGET; *type = VIR_NODE_DEV_CAP_SCSI_TARGET;
goto out; else if (STREQ(devtype, "scsi_device"))
}
if (devtype != NULL && STREQ(devtype, "scsi_device")) {
*type = VIR_NODE_DEV_CAP_SCSI; *type = VIR_NODE_DEV_CAP_SCSI;
goto out; else if (STREQ(devtype, "disk"))
}
if (devtype != NULL && STREQ(devtype, "disk")) {
*type = VIR_NODE_DEV_CAP_STORAGE; *type = VIR_NODE_DEV_CAP_STORAGE;
goto out; else if (STREQ(devtype, "wlan"))
}
if (devtype != NULL && STREQ(devtype, "wlan")) {
*type = VIR_NODE_DEV_CAP_NET; *type = VIR_NODE_DEV_CAP_NET;
goto out; } else {
} /* PCI devices don't set the DEVTYPE property. */
if (udevDeviceHasProperty(device, "PCI_CLASS"))
if (udevGetUintProperty(device, "PCI_CLASS", &tmp, 16) == PROPERTY_FOUND) {
*type = VIR_NODE_DEV_CAP_PCI_DEV; *type = VIR_NODE_DEV_CAP_PCI_DEV;
goto out;
}
/* It does not appear that wired network interfaces set the /* Wired network interfaces don't set the DEVTYPE property,
* DEVTYPE property. USB devices also have an INTERFACE property, * USB devices also have an INTERFACE property, but they do
* but they do set DEVTYPE, so if devtype is NULL and the * set DEVTYPE, so if devtype is NULL and the INTERFACE
* INTERFACE property exists, we have a network device. */ * property exists, we have a network device. */
if (devtype == NULL && if (udevDeviceHasProperty(device, "INTERFACE"))
udevGetStringProperty(device,
"INTERFACE",
&tmp_string) == PROPERTY_FOUND) {
VIR_FREE(tmp_string);
*type = VIR_NODE_DEV_CAP_NET; *type = VIR_NODE_DEV_CAP_NET;
goto out;
} }
if (!*type)
VIR_DEBUG("Could not determine device type for device " VIR_DEBUG("Could not determine device type for device "
"with sysfs path '%s'", "with sysfs name '%s'",
udev_device_get_sysname(device)); udev_device_get_sysname(device));
ret = -1; else
ret = 0;
out:
return ret; return ret;
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册