[libvirt] [PATCH 10/18] virpcimock: Introduce and use pci_driver_get_path()

Michal Privoznik mprivozn at redhat.com
Wed Aug 14 11:57:39 UTC 2019


Have just one function to generate path to a PCI driver so that
when we change it in near future there's only few of the places
we need to fix.

Signed-off-by: Michal Privoznik <mprivozn at redhat.com>
---
 tests/virpcimock.c | 34 +++++++++++++++++++++++++++-------
 1 file changed, 27 insertions(+), 7 deletions(-)

diff --git a/tests/virpcimock.c b/tests/virpcimock.c
index 52a585d0cc..de365cdb78 100644
--- a/tests/virpcimock.c
+++ b/tests/virpcimock.c
@@ -503,6 +503,29 @@ pci_device_autobind(struct pciDevice *dev)
 /*
  * PCI Driver functions
  */
+static char *
+pci_driver_get_path(const struct pciDriver *driver,
+                    const char *file,
+                    bool faked)
+{
+    char *ret = NULL;
+    const char *prefix = "";
+
+    if (faked)
+        prefix = fakerootdir;
+
+    if (file) {
+        ignore_value(virAsprintfQuiet(&ret, "%s" SYSFS_PCI_PREFIX "drivers/%s/%s",
+                                      prefix, driver->name, file));
+    } else {
+        ignore_value(virAsprintfQuiet(&ret, "%s" SYSFS_PCI_PREFIX "drivers/%s",
+                                      prefix, driver->name));
+    }
+
+    return ret;
+}
+
+
 static void
 pci_driver_new(const char *name, int fail, ...)
 {
@@ -513,7 +536,7 @@ pci_driver_new(const char *name, int fail, ...)
 
     if (VIR_ALLOC_QUIET(driver) < 0 ||
         VIR_STRDUP_QUIET(driver->name, name) < 0 ||
-        virAsprintfQuiet(&driverpath, "%s/sys/bus/pci/drivers/%s", fakerootdir, name) < 0)
+        !(driverpath = pci_driver_get_path(driver, NULL, true)))
         ABORT_OOM();
 
     driver->fail = fail;
@@ -619,8 +642,7 @@ pci_driver_bind(struct pciDriver *driver,
 
     /* Make symlink under device tree */
     if (!(devpath = pci_device_get_path(dev, "driver", true)) ||
-        virAsprintfQuiet(&driverpath, "%s/sys/bus/pci/drivers/%s",
-                         fakerootdir, driver->name) < 0) {
+        !(driverpath = pci_driver_get_path(driver, NULL, true))) {
         errno = ENOMEM;
         return -1;
     }
@@ -632,8 +654,7 @@ pci_driver_bind(struct pciDriver *driver,
     VIR_FREE(devpath);
     VIR_FREE(driverpath);
     if (!(devpath = pci_device_get_path(dev, NULL, true)) ||
-        virAsprintfQuiet(&driverpath, "%s/sys/bus/pci/drivers/%s/%s",
-                         fakerootdir, driver->name, dev->id) < 0) {
+        !(driverpath = pci_driver_get_path(driver, dev->id, true))) {
         errno = ENOMEM;
         return -1;
     }
@@ -660,8 +681,7 @@ pci_driver_unbind(struct pciDriver *driver,
 
     /* Make symlink under device tree */
     if (!(devpath = pci_device_get_path(dev, "driver", true)) ||
-        virAsprintfQuiet(&driverpath, "%s/sys/bus/pci/drivers/%s/%s",
-                         fakerootdir, driver->name, dev->id) < 0) {
+        !(driverpath = pci_driver_get_path(driver, dev->id, true))) {
         errno = ENOMEM;
         return -1;
     }
-- 
2.21.0




More information about the libvir-list mailing list