提交 7f7bd601 编写于 作者: M Michal Privoznik

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: NMichal Privoznik <mprivozn@redhat.com>
Tested-by: NDaniel Henrique Barboza <danielhb413@gmail.com>
Reviewed-by: NDaniel Henrique Barboza <danielhb413@gmail.com>
上级 ec60538c
...@@ -338,6 +338,29 @@ remove_fd(int fd) ...@@ -338,6 +338,29 @@ remove_fd(int fd)
/* /*
* PCI Device functions * 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 static void
pci_device_new_from_stub(const struct pciDevice *data) pci_device_new_from_stub(const struct pciDevice *data)
{ {
...@@ -366,12 +389,14 @@ 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 || if (VIR_ALLOC_QUIET(dev) < 0 ||
virAsprintfQuiet(&configSrc, "%s/virpcitestdata/%s.config", virAsprintfQuiet(&configSrc, "%s/virpcitestdata/%s.config",
abs_srcdir, id) < 0 || abs_srcdir, id) < 0)
virAsprintfQuiet(&devpath, "%s/sys/bus/pci/devices/%s", fakerootdir, data->id) < 0)
ABORT_OOM(); ABORT_OOM();
memcpy(dev, data, sizeof(*dev)); memcpy(dev, data, sizeof(*dev));
if (!(devpath = pci_device_get_path(dev, NULL, true)))
ABORT_OOM();
if (virFileMakePath(devpath) < 0) if (virFileMakePath(devpath) < 0)
ABORT("Unable to create: %s", devpath); ABORT("Unable to create: %s", devpath);
...@@ -564,9 +589,7 @@ pci_driver_find_by_driver_override(struct pciDevice *dev) ...@@ -564,9 +589,7 @@ pci_driver_find_by_driver_override(struct pciDevice *dev)
char tmp[32]; char tmp[32];
size_t i; size_t i;
if (virAsprintfQuiet(&path, if (!(path = pci_device_get_path(dev, "driver_override", false)))
SYSFS_PCI_PREFIX "devices/%s/driver_override",
dev->id) < 0)
return NULL; return NULL;
if (pci_read_file(path, tmp, sizeof(tmp), false) < 0) if (pci_read_file(path, tmp, sizeof(tmp), false) < 0)
...@@ -596,8 +619,7 @@ pci_driver_bind(struct pciDriver *driver, ...@@ -596,8 +619,7 @@ pci_driver_bind(struct pciDriver *driver,
} }
/* Make symlink under device tree */ /* Make symlink under device tree */
if (virAsprintfQuiet(&devpath, "%s/sys/bus/pci/devices/%s/driver", if (!(devpath = pci_device_get_path(dev, "driver", true)) ||
fakerootdir, dev->id) < 0 ||
virAsprintfQuiet(&driverpath, "%s/sys/bus/pci/drivers/%s", virAsprintfQuiet(&driverpath, "%s/sys/bus/pci/drivers/%s",
fakerootdir, driver->name) < 0) { fakerootdir, driver->name) < 0) {
errno = ENOMEM; errno = ENOMEM;
...@@ -610,8 +632,7 @@ pci_driver_bind(struct pciDriver *driver, ...@@ -610,8 +632,7 @@ pci_driver_bind(struct pciDriver *driver,
/* Make symlink under driver tree */ /* Make symlink under driver tree */
VIR_FREE(devpath); VIR_FREE(devpath);
VIR_FREE(driverpath); VIR_FREE(driverpath);
if (virAsprintfQuiet(&devpath, "%s/sys/bus/pci/devices/%s", if (!(devpath = pci_device_get_path(dev, NULL, true)) ||
fakerootdir, dev->id) < 0 ||
virAsprintfQuiet(&driverpath, "%s/sys/bus/pci/drivers/%s/%s", virAsprintfQuiet(&driverpath, "%s/sys/bus/pci/drivers/%s/%s",
fakerootdir, driver->name, dev->id) < 0) { fakerootdir, driver->name, dev->id) < 0) {
errno = ENOMEM; errno = ENOMEM;
...@@ -639,8 +660,7 @@ pci_driver_unbind(struct pciDriver *driver, ...@@ -639,8 +660,7 @@ pci_driver_unbind(struct pciDriver *driver,
} }
/* Make symlink under device tree */ /* Make symlink under device tree */
if (virAsprintfQuiet(&devpath, "%s/sys/bus/pci/devices/%s/driver", if (!(devpath = pci_device_get_path(dev, "driver", true)) ||
fakerootdir, dev->id) < 0 ||
virAsprintfQuiet(&driverpath, "%s/sys/bus/pci/drivers/%s/%s", virAsprintfQuiet(&driverpath, "%s/sys/bus/pci/drivers/%s/%s",
fakerootdir, driver->name, dev->id) < 0) { fakerootdir, driver->name, dev->id) < 0) {
errno = ENOMEM; errno = ENOMEM;
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册