]> git.ipfire.org Git - thirdparty/libvirt.git/commitdiff
virpcimock: Introduce and use pci_driver_get_path()
authorMichal Privoznik <mprivozn@redhat.com>
Tue, 13 Aug 2019 14:11:17 +0000 (16:11 +0200)
committerMichal Privoznik <mprivozn@redhat.com>
Sat, 17 Aug 2019 10:08:20 +0000 (12:08 +0200)
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@redhat.com>
Tested-by: Daniel Henrique Barboza <danielhb413@gmail.com>
Reviewed-by: Daniel Henrique Barboza <danielhb413@gmail.com>
tests/virpcimock.c

index adeb7469c59b68ee1bb2ffe75abdce2b8e95cde8..76a06d82555da99e0ec0eff98d99e8215bb46586 100644 (file)
@@ -504,6 +504,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, ...)
 {
@@ -514,7 +537,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;
@@ -620,8 +643,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;
     }
@@ -633,8 +655,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;
     }
@@ -661,8 +682,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;
     }