From 7f7bd6016e6566fa958c499ec0834d966ddef5e6 Mon Sep 17 00:00:00 2001 From: Michal Privoznik Date: Tue, 13 Aug 2019 15:31:09 +0200 Subject: [PATCH] virpcimock: Introduce and use pci_device_get_path() Have just one function to generate path to a PCI device 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 Tested-by: Daniel Henrique Barboza Reviewed-by: Daniel Henrique Barboza --- tests/virpcimock.c | 42 +++++++++++++++++++++++++++++++----------- 1 file changed, 31 insertions(+), 11 deletions(-) diff --git a/tests/virpcimock.c b/tests/virpcimock.c index bc34c0f181..adeb7469c5 100644 --- a/tests/virpcimock.c +++ b/tests/virpcimock.c @@ -338,6 +338,29 @@ remove_fd(int fd) /* * PCI Device functions */ +static char * +pci_device_get_path(const struct pciDevice *dev, + 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 "devices/%s/%s", + prefix, dev->id, file)); + } else { + ignore_value(virAsprintfQuiet(&ret, "%s" SYSFS_PCI_PREFIX "devices/%s", + prefix, dev->id)); + } + + return ret; +} + + static void pci_device_new_from_stub(const struct pciDevice *data) { @@ -366,12 +389,14 @@ pci_device_new_from_stub(const struct pciDevice *data) if (VIR_ALLOC_QUIET(dev) < 0 || virAsprintfQuiet(&configSrc, "%s/virpcitestdata/%s.config", - abs_srcdir, id) < 0 || - virAsprintfQuiet(&devpath, "%s/sys/bus/pci/devices/%s", fakerootdir, data->id) < 0) + abs_srcdir, id) < 0) ABORT_OOM(); memcpy(dev, data, sizeof(*dev)); + if (!(devpath = pci_device_get_path(dev, NULL, true))) + ABORT_OOM(); + if (virFileMakePath(devpath) < 0) ABORT("Unable to create: %s", devpath); @@ -564,9 +589,7 @@ pci_driver_find_by_driver_override(struct pciDevice *dev) char tmp[32]; size_t i; - if (virAsprintfQuiet(&path, - SYSFS_PCI_PREFIX "devices/%s/driver_override", - dev->id) < 0) + if (!(path = pci_device_get_path(dev, "driver_override", false))) return NULL; if (pci_read_file(path, tmp, sizeof(tmp), false) < 0) @@ -596,8 +619,7 @@ pci_driver_bind(struct pciDriver *driver, } /* Make symlink under device tree */ - if (virAsprintfQuiet(&devpath, "%s/sys/bus/pci/devices/%s/driver", - fakerootdir, dev->id) < 0 || + if (!(devpath = pci_device_get_path(dev, "driver", true)) || virAsprintfQuiet(&driverpath, "%s/sys/bus/pci/drivers/%s", fakerootdir, driver->name) < 0) { errno = ENOMEM; @@ -610,8 +632,7 @@ pci_driver_bind(struct pciDriver *driver, /* Make symlink under driver tree */ VIR_FREE(devpath); VIR_FREE(driverpath); - if (virAsprintfQuiet(&devpath, "%s/sys/bus/pci/devices/%s", - fakerootdir, dev->id) < 0 || + if (!(devpath = pci_device_get_path(dev, NULL, true)) || virAsprintfQuiet(&driverpath, "%s/sys/bus/pci/drivers/%s/%s", fakerootdir, driver->name, dev->id) < 0) { errno = ENOMEM; @@ -639,8 +660,7 @@ pci_driver_unbind(struct pciDriver *driver, } /* Make symlink under device tree */ - if (virAsprintfQuiet(&devpath, "%s/sys/bus/pci/devices/%s/driver", - fakerootdir, dev->id) < 0 || + if (!(devpath = pci_device_get_path(dev, "driver", true)) || virAsprintfQuiet(&driverpath, "%s/sys/bus/pci/drivers/%s/%s", fakerootdir, driver->name, dev->id) < 0) { errno = ENOMEM; -- GitLab