virpcimock: Use VIR_AUTOFREE()

It saves us couple of lines.

Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
Tested-by: Daniel Henrique Barboza <danielhb413@gmail.com>
Reviewed-by: Daniel Henrique Barboza <danielhb413@gmail.com>
This commit is contained in:
Michal Privoznik 2019-08-13 13:50:48 +02:00
parent caf6cc4fd2
commit ea893afaf8
1 changed files with 36 additions and 73 deletions

View File

@ -173,7 +173,7 @@ make_file(const char *path,
ssize_t len) ssize_t len)
{ {
int fd = -1; int fd = -1;
char *filepath = NULL; VIR_AUTOFREE(char *) filepath = NULL;
if (value && len == -1) if (value && len == -1)
len = strlen(value); len = strlen(value);
@ -187,7 +187,6 @@ make_file(const char *path,
ABORT("Unable to write: %s", filepath); ABORT("Unable to write: %s", filepath);
VIR_FORCE_CLOSE(fd); VIR_FORCE_CLOSE(fd);
VIR_FREE(filepath);
} }
static void static void
@ -195,15 +194,13 @@ make_symlink(const char *path,
const char *name, const char *name,
const char *target) const char *target)
{ {
char *filepath = NULL; VIR_AUTOFREE(char *) filepath = NULL;
if (virAsprintfQuiet(&filepath, "%s/%s", path, name) < 0) if (virAsprintfQuiet(&filepath, "%s/%s", path, name) < 0)
ABORT_OOM(); ABORT_OOM();
if (symlink(target, filepath) < 0) if (symlink(target, filepath) < 0)
ABORT("Unable to create symlink filepath -> target"); ABORT("Unable to create symlink filepath -> target");
VIR_FREE(filepath);
} }
static int static int
@ -214,7 +211,7 @@ pci_read_file(const char *path,
{ {
int ret = -1; int ret = -1;
int fd = -1; int fd = -1;
char *newpath; VIR_AUTOFREE(char *) newpath = NULL;
if (virAsprintfQuiet(&newpath, "%s/%s", if (virAsprintfQuiet(&newpath, "%s/%s",
fakesysfspcidir, fakesysfspcidir,
@ -238,7 +235,6 @@ pci_read_file(const char *path,
ret = 0; ret = 0;
cleanup: cleanup:
VIR_FREE(newpath);
real_close(fd); real_close(fd);
return ret; return ret;
} }
@ -336,10 +332,10 @@ static void
pci_device_new_from_stub(const struct pciDevice *data) pci_device_new_from_stub(const struct pciDevice *data)
{ {
struct pciDevice *dev; struct pciDevice *dev;
char *devpath; VIR_AUTOFREE(char *) devpath = NULL;
char *id; VIR_AUTOFREE(char *) id = NULL;
char *c; char *c;
char *configSrc; VIR_AUTOFREE(char *) configSrc = NULL;
char tmp[256]; char tmp[256];
struct stat sb; struct stat sb;
bool configSrcExists = false; bool configSrcExists = false;
@ -364,7 +360,6 @@ pci_device_new_from_stub(const struct pciDevice *data)
virAsprintfQuiet(&devpath, "%s/devices/%s", fakesysfspcidir, data->id) < 0) virAsprintfQuiet(&devpath, "%s/devices/%s", fakesysfspcidir, data->id) < 0)
ABORT_OOM(); ABORT_OOM();
VIR_FREE(id);
memcpy(dev, data, sizeof(*dev)); memcpy(dev, data, sizeof(*dev));
if (virFileMakePath(devpath) < 0) if (virFileMakePath(devpath) < 0)
@ -381,14 +376,13 @@ pci_device_new_from_stub(const struct pciDevice *data)
* file, and parallel VPATH builds must not stomp on the * file, and parallel VPATH builds must not stomp on the
* original; besides, 'make distcheck' requires the original * original; besides, 'make distcheck' requires the original
* to be read-only */ * to be read-only */
char *buf; VIR_AUTOFREE(char *) buf = NULL;
ssize_t len; ssize_t len;
if ((len = virFileReadAll(configSrc, 4096, &buf)) < 0) if ((len = virFileReadAll(configSrc, 4096, &buf)) < 0)
ABORT("Unable to read config file '%s'", configSrc); ABORT("Unable to read config file '%s'", configSrc);
make_file(devpath, "config", buf, len); make_file(devpath, "config", buf, len);
VIR_FREE(buf);
} else { } else {
/* If there's no config data in the virpcitestdata dir, create a dummy /* If there's no config data in the virpcitestdata dir, create a dummy
* config file */ * config file */
@ -428,9 +422,6 @@ pci_device_new_from_stub(const struct pciDevice *data)
if (VIR_APPEND_ELEMENT_QUIET(pciDevices, nPCIDevices, dev) < 0) if (VIR_APPEND_ELEMENT_QUIET(pciDevices, nPCIDevices, dev) < 0)
ABORT_OOM(); ABORT_OOM();
VIR_FREE(devpath);
VIR_FREE(configSrc);
} }
static struct pciDevice * static struct pciDevice *
@ -484,7 +475,7 @@ pci_driver_new(const char *name, int fail, ...)
struct pciDriver *driver; struct pciDriver *driver;
va_list args; va_list args;
int vendor, device; int vendor, device;
char *driverpath; VIR_AUTOFREE(char *) driverpath = NULL;
if (VIR_ALLOC_QUIET(driver) < 0 || if (VIR_ALLOC_QUIET(driver) < 0 ||
VIR_STRDUP_QUIET(driver->name, name) < 0 || VIR_STRDUP_QUIET(driver->name, name) < 0 ||
@ -520,8 +511,6 @@ pci_driver_new(const char *name, int fail, ...)
if (VIR_APPEND_ELEMENT_QUIET(pciDrivers, nPCIDrivers, driver) < 0) if (VIR_APPEND_ELEMENT_QUIET(pciDrivers, nPCIDrivers, driver) < 0)
ABORT_OOM(); ABORT_OOM();
VIR_FREE(driverpath);
} }
static struct pciDriver * static struct pciDriver *
@ -587,13 +576,13 @@ static int
pci_driver_bind(struct pciDriver *driver, pci_driver_bind(struct pciDriver *driver,
struct pciDevice *dev) struct pciDevice *dev)
{ {
int ret = -1; VIR_AUTOFREE(char *) devpath = NULL;
char *devpath = NULL, *driverpath = NULL; VIR_AUTOFREE(char *) driverpath = NULL;
if (dev->driver) { if (dev->driver) {
/* Device already bound */ /* Device already bound */
errno = ENODEV; errno = ENODEV;
return ret; return -1;
} }
/* Make symlink under device tree */ /* Make symlink under device tree */
@ -602,11 +591,11 @@ pci_driver_bind(struct pciDriver *driver,
virAsprintfQuiet(&driverpath, "%s/drivers/%s", virAsprintfQuiet(&driverpath, "%s/drivers/%s",
fakesysfspcidir, driver->name) < 0) { fakesysfspcidir, driver->name) < 0) {
errno = ENOMEM; errno = ENOMEM;
goto cleanup; return -1;
} }
if (symlink(driverpath, devpath) < 0) if (symlink(driverpath, devpath) < 0)
goto cleanup; return -1;
/* Make symlink under driver tree */ /* Make symlink under driver tree */
VIR_FREE(devpath); VIR_FREE(devpath);
@ -616,31 +605,27 @@ pci_driver_bind(struct pciDriver *driver,
virAsprintfQuiet(&driverpath, "%s/drivers/%s/%s", virAsprintfQuiet(&driverpath, "%s/drivers/%s/%s",
fakesysfspcidir, driver->name, dev->id) < 0) { fakesysfspcidir, driver->name, dev->id) < 0) {
errno = ENOMEM; errno = ENOMEM;
goto cleanup; return -1;
} }
if (symlink(devpath, driverpath) < 0) if (symlink(devpath, driverpath) < 0)
goto cleanup; return -1;
dev->driver = driver; dev->driver = driver;
ret = 0; return 0;
cleanup:
VIR_FREE(devpath);
VIR_FREE(driverpath);
return ret;
} }
static int static int
pci_driver_unbind(struct pciDriver *driver, pci_driver_unbind(struct pciDriver *driver,
struct pciDevice *dev) struct pciDevice *dev)
{ {
int ret = -1; VIR_AUTOFREE(char *) devpath = NULL;
char *devpath = NULL, *driverpath = NULL; VIR_AUTOFREE(char *) driverpath = NULL;
if (dev->driver != driver) { if (dev->driver != driver) {
/* Device not bound to the @driver */ /* Device not bound to the @driver */
errno = ENODEV; errno = ENODEV;
return ret; return -1;
} }
/* Make symlink under device tree */ /* Make symlink under device tree */
@ -649,19 +634,15 @@ pci_driver_unbind(struct pciDriver *driver,
virAsprintfQuiet(&driverpath, "%s/drivers/%s/%s", virAsprintfQuiet(&driverpath, "%s/drivers/%s/%s",
fakesysfspcidir, driver->name, dev->id) < 0) { fakesysfspcidir, driver->name, dev->id) < 0) {
errno = ENOMEM; errno = ENOMEM;
goto cleanup; return -1;
} }
if (unlink(devpath) < 0 || if (unlink(devpath) < 0 ||
unlink(driverpath) < 0) unlink(driverpath) < 0)
goto cleanup; return -1;
dev->driver = NULL; dev->driver = NULL;
ret = 0; return 0;
cleanup:
VIR_FREE(devpath);
VIR_FREE(driverpath);
return ret;
} }
static int static int
@ -911,20 +892,15 @@ init_env(void)
int int
access(const char *path, int mode) access(const char *path, int mode)
{ {
int ret; VIR_AUTOFREE(char *) newpath = NULL;
init_syms(); init_syms();
if (STRPREFIX(path, SYSFS_PCI_PREFIX)) { if (STRPREFIX(path, SYSFS_PCI_PREFIX) &&
char *newpath; getrealpath(&newpath, path) < 0)
if (getrealpath(&newpath, path) < 0) return -1;
return -1;
ret = real_access(newpath, mode); return real_access(newpath ? newpath : path, mode);
VIR_FREE(newpath);
} else {
ret = real_access(path, mode);
}
return ret;
} }
@ -943,7 +919,7 @@ int
open(const char *path, int flags, ...) open(const char *path, int flags, ...)
{ {
int ret; int ret;
char *newpath = NULL; VIR_AUTOFREE(char *) newpath = NULL;
init_syms(); init_syms();
@ -970,7 +946,6 @@ open(const char *path, int flags, ...)
ret = -1; ret = -1;
} }
VIR_FREE(newpath);
return ret; return ret;
} }
@ -1009,8 +984,7 @@ __open_2(const char *path, int flags)
DIR * DIR *
opendir(const char *path) opendir(const char *path)
{ {
DIR *ret; VIR_AUTOFREE(char *) newpath = NULL;
char *newpath = NULL;
init_syms(); init_syms();
@ -1018,10 +992,7 @@ opendir(const char *path)
getrealpath(&newpath, path) < 0) getrealpath(&newpath, path) < 0)
return NULL; return NULL;
ret = real_opendir(newpath ? newpath : path); return real_opendir(newpath ? newpath : path);
VIR_FREE(newpath);
return ret;
} }
int int
@ -1035,23 +1006,15 @@ close(int fd)
char * char *
virFileCanonicalizePath(const char *path) virFileCanonicalizePath(const char *path)
{ {
char *ret; VIR_AUTOFREE(char *) newpath = NULL;
init_syms(); init_syms();
if (STRPREFIX(path, SYSFS_PCI_PREFIX)) { if (STRPREFIX(path, SYSFS_PCI_PREFIX) &&
char *newpath; getrealpath(&newpath, path) < 0)
return NULL;
if (getrealpath(&newpath, path) < 0) return real_virFileCanonicalizePath(newpath ? newpath : path);
return NULL;
ret = real_virFileCanonicalizePath(newpath);
VIR_FREE(newpath);
} else {
ret = real_virFileCanonicalizePath(path);
}
return ret;
} }
# include "virmockstathelpers.c" # include "virmockstathelpers.c"