remove unnecessary cleanup labels and unused return variables

After converting all DIR* to g_autoptr(DIR), many cleanup: labels
ended up just having "return ret", and every place that set ret would
just immediately goto cleanup. Remove the cleanup label and its
return, and just return the set value immediately, thus eliminating
the need for the return variable itself.

Signed-off-by: Laine Stump <laine@redhat.com>
Reviewed-by: Daniel Henrique Barboza <danielhb413@gmail.com>
This commit is contained in:
Laine Stump 2020-10-27 17:49:11 -04:00
parent 77401d549c
commit 85c8c29214
15 changed files with 119 additions and 214 deletions

View File

@ -1091,12 +1091,11 @@ virNetworkObjLoadAllState(virNetworkObjListPtr nets,
if (obj && if (obj &&
virNetworkObjLoadAllPorts(obj, stateDir) < 0) { virNetworkObjLoadAllPorts(obj, stateDir) < 0) {
virNetworkObjEndAPI(&obj); virNetworkObjEndAPI(&obj);
goto cleanup; return -1;
} }
virNetworkObjEndAPI(&obj); virNetworkObjEndAPI(&obj);
} }
cleanup:
return ret; return ret;
} }
@ -1708,15 +1707,12 @@ virNetworkObjDeleteAllPorts(virNetworkObjPtr net,
g_autoptr(DIR) dh = NULL; g_autoptr(DIR) dh = NULL;
struct dirent *de; struct dirent *de;
int rc; int rc;
int ret = -1;
if (!(dir = virNetworkObjGetPortStatusDir(net, stateDir))) if (!(dir = virNetworkObjGetPortStatusDir(net, stateDir)))
goto cleanup; return -1;
if ((rc = virDirOpenIfExists(&dh, dir)) <= 0) { if ((rc = virDirOpenIfExists(&dh, dir)) <= 0)
ret = rc; return rc;
goto cleanup;
}
while ((rc = virDirRead(dh, &de, dir)) > 0) { while ((rc = virDirRead(dh, &de, dir)) > 0) {
char *file = NULL; char *file = NULL;
@ -1733,10 +1729,7 @@ virNetworkObjDeleteAllPorts(virNetworkObjPtr net,
} }
virHashRemoveAll(net->ports); virHashRemoveAll(net->ports);
return 0;
ret = 0;
cleanup:
return ret;
} }
@ -1862,18 +1855,15 @@ virNetworkObjLoadAllPorts(virNetworkObjPtr net,
g_autofree char *dir = NULL; g_autofree char *dir = NULL;
g_autoptr(DIR) dh = NULL; g_autoptr(DIR) dh = NULL;
struct dirent *de; struct dirent *de;
int ret = -1;
int rc; int rc;
char uuidstr[VIR_UUID_STRING_BUFLEN]; char uuidstr[VIR_UUID_STRING_BUFLEN];
g_autoptr(virNetworkPortDef) portdef = NULL; g_autoptr(virNetworkPortDef) portdef = NULL;
if (!(dir = virNetworkObjGetPortStatusDir(net, stateDir))) if (!(dir = virNetworkObjGetPortStatusDir(net, stateDir)))
goto cleanup; return -1;
if ((rc = virDirOpenIfExists(&dh, dir)) <= 0) { if ((rc = virDirOpenIfExists(&dh, dir)) <= 0)
ret = rc; return rc;
goto cleanup;
}
while ((rc = virDirRead(dh, &de, dir)) > 0) { while ((rc = virDirRead(dh, &de, dir)) > 0) {
g_autofree char *file = NULL; g_autofree char *file = NULL;
@ -1891,12 +1881,10 @@ virNetworkObjLoadAllPorts(virNetworkObjPtr net,
virUUIDFormat(portdef->uuid, uuidstr); virUUIDFormat(portdef->uuid, uuidstr);
if (virHashAddEntry(net->ports, uuidstr, portdef) < 0) if (virHashAddEntry(net->ports, uuidstr, portdef) < 0)
goto cleanup; return -1;
portdef = NULL; portdef = NULL;
} }
ret = 0; return 0;
cleanup:
return ret;
} }

View File

@ -40,7 +40,6 @@ qemuBuildFileList(virHashTablePtr files, const char *dir)
g_autoptr(DIR) dirp = NULL; g_autoptr(DIR) dirp = NULL;
struct dirent *ent = NULL; struct dirent *ent = NULL;
int rc; int rc;
int ret = -1;
if ((rc = virDirOpenIfExists(&dirp, dir)) < 0) if ((rc = virDirOpenIfExists(&dirp, dir)) < 0)
return -1; return -1;
@ -62,24 +61,22 @@ qemuBuildFileList(virHashTablePtr files, const char *dir)
if (stat(path, &sb) < 0) { if (stat(path, &sb) < 0) {
virReportSystemError(errno, _("Unable to access %s"), path); virReportSystemError(errno, _("Unable to access %s"), path);
goto cleanup; return -1;
} }
if (!S_ISREG(sb.st_mode) && !S_ISLNK(sb.st_mode)) if (!S_ISREG(sb.st_mode) && !S_ISLNK(sb.st_mode))
continue; continue;
if (virHashUpdateEntry(files, filename, path) < 0) if (virHashUpdateEntry(files, filename, path) < 0)
goto cleanup; return -1;
path = NULL; path = NULL;
} }
if (rc < 0) if (rc < 0)
goto cleanup; return -1;
ret = 0; return 0;
cleanup:
return ret;
} }
static int static int

View File

@ -3507,13 +3507,12 @@ virStorageBackendRefreshLocal(virStoragePoolObjPtr pool)
struct statvfs sb; struct statvfs sb;
struct stat statbuf; struct stat statbuf;
int direrr; int direrr;
int ret = -1;
g_autoptr(virStorageVolDef) vol = NULL; g_autoptr(virStorageVolDef) vol = NULL;
VIR_AUTOCLOSE fd = -1; VIR_AUTOCLOSE fd = -1;
g_autoptr(virStorageSource) target = NULL; g_autoptr(virStorageSource) target = NULL;
if (virDirOpen(&dir, def->target.path) < 0) if (virDirOpen(&dir, def->target.path) < 0)
goto cleanup; return -1;
while ((direrr = virDirRead(dir, &ent, def->target.path)) > 0) { while ((direrr = virDirRead(dir, &ent, def->target.path)) > 0) {
int err; int err;
@ -3541,15 +3540,15 @@ virStorageBackendRefreshLocal(virStoragePoolObjPtr pool)
vol = NULL; vol = NULL;
continue; continue;
} }
goto cleanup; return -1;
} }
if (virStoragePoolObjAddVol(pool, vol) < 0) if (virStoragePoolObjAddVol(pool, vol) < 0)
goto cleanup; return -1;
vol = NULL; vol = NULL;
} }
if (direrr < 0) if (direrr < 0)
goto cleanup; return -1;
target = virStorageSourceNew(); target = virStorageSourceNew();
@ -3557,25 +3556,25 @@ virStorageBackendRefreshLocal(virStoragePoolObjPtr pool)
virReportSystemError(errno, virReportSystemError(errno,
_("cannot open path '%s'"), _("cannot open path '%s'"),
def->target.path); def->target.path);
goto cleanup; return -1;
} }
if (fstat(fd, &statbuf) < 0) { if (fstat(fd, &statbuf) < 0) {
virReportSystemError(errno, virReportSystemError(errno,
_("cannot stat path '%s'"), _("cannot stat path '%s'"),
def->target.path); def->target.path);
goto cleanup; return -1;
} }
if (virStorageBackendUpdateVolTargetInfoFD(target, fd, &statbuf) < 0) if (virStorageBackendUpdateVolTargetInfoFD(target, fd, &statbuf) < 0)
goto cleanup; return -1;
/* VolTargetInfoFD doesn't update capacity correctly for the pool case */ /* VolTargetInfoFD doesn't update capacity correctly for the pool case */
if (statvfs(def->target.path, &sb) < 0) { if (statvfs(def->target.path, &sb) < 0) {
virReportSystemError(errno, virReportSystemError(errno,
_("cannot statvfs path '%s'"), _("cannot statvfs path '%s'"),
def->target.path); def->target.path);
goto cleanup; return -1;
} }
def->capacity = ((unsigned long long)sb.f_frsize * def->capacity = ((unsigned long long)sb.f_frsize *
@ -3590,9 +3589,7 @@ virStorageBackendRefreshLocal(virStoragePoolObjPtr pool)
VIR_FREE(def->target.perms.label); VIR_FREE(def->target.perms.label);
def->target.perms.label = g_strdup(target->perms->label); def->target.perms.label = g_strdup(target->perms->label);
ret = 0; return 0;
cleanup:
return ret;
} }
@ -3724,7 +3721,6 @@ getNewStyleBlockDevice(const char *lun_path,
{ {
g_autoptr(DIR) block_dir = NULL; g_autoptr(DIR) block_dir = NULL;
struct dirent *block_dirent = NULL; struct dirent *block_dirent = NULL;
int retval = -1;
int direrr; int direrr;
g_autofree char *block_path = NULL; g_autofree char *block_path = NULL;
@ -3733,7 +3729,7 @@ getNewStyleBlockDevice(const char *lun_path,
VIR_DEBUG("Looking for block device in '%s'", block_path); VIR_DEBUG("Looking for block device in '%s'", block_path);
if (virDirOpen(&block_dir, block_path) < 0) if (virDirOpen(&block_dir, block_path) < 0)
goto cleanup; return -1;
while ((direrr = virDirRead(block_dir, &block_dirent, block_path)) > 0) { while ((direrr = virDirRead(block_dir, &block_dirent, block_path)) > 0) {
*block_device = g_strdup(block_dirent->d_name); *block_device = g_strdup(block_dirent->d_name);
@ -3744,12 +3740,9 @@ getNewStyleBlockDevice(const char *lun_path,
} }
if (direrr < 0) if (direrr < 0)
goto cleanup; return -1;
retval = 0; return 0;
cleanup:
return retval;
} }
@ -3796,7 +3789,6 @@ getBlockDevice(uint32_t host,
{ {
g_autoptr(DIR) lun_dir = NULL; g_autoptr(DIR) lun_dir = NULL;
struct dirent *lun_dirent = NULL; struct dirent *lun_dirent = NULL;
int retval = -1;
int direrr; int direrr;
g_autofree char *lun_path = NULL; g_autofree char *lun_path = NULL;
@ -3806,7 +3798,7 @@ getBlockDevice(uint32_t host,
target, lun); target, lun);
if (virDirOpen(&lun_dir, lun_path) < 0) if (virDirOpen(&lun_dir, lun_path) < 0)
goto cleanup; return -1;
while ((direrr = virDirRead(lun_dir, &lun_dirent, lun_path)) > 0) { while ((direrr = virDirRead(lun_dir, &lun_dirent, lun_path)) > 0) {
if (STRPREFIX(lun_dirent->d_name, "block")) { if (STRPREFIX(lun_dirent->d_name, "block")) {
@ -3814,27 +3806,23 @@ getBlockDevice(uint32_t host,
if (getNewStyleBlockDevice(lun_path, if (getNewStyleBlockDevice(lun_path,
lun_dirent->d_name, lun_dirent->d_name,
block_device) < 0) block_device) < 0)
goto cleanup; return -1;
} else { } else {
if (getOldStyleBlockDevice(lun_path, if (getOldStyleBlockDevice(lun_path,
lun_dirent->d_name, lun_dirent->d_name,
block_device) < 0) block_device) < 0)
goto cleanup; return -1;
} }
break; break;
} }
} }
if (direrr < 0) if (direrr < 0)
goto cleanup; return -1;
if (!*block_device) {
retval = -2;
goto cleanup;
}
retval = 0; if (!*block_device)
return -2;
cleanup: return 0;
return retval;
} }

View File

@ -2465,7 +2465,6 @@ virCgroupKillRecursiveInternal(virCgroupPtr group,
const char *taskFile, const char *taskFile,
bool dormdir) bool dormdir)
{ {
int ret = -1;
int rc; int rc;
bool killedAny = false; bool killedAny = false;
g_autofree char *keypath = NULL; g_autofree char *keypath = NULL;
@ -2480,14 +2479,14 @@ virCgroupKillRecursiveInternal(virCgroupPtr group,
if ((rc = virCgroupKillInternal(group, signum, pids, if ((rc = virCgroupKillInternal(group, signum, pids,
controller, taskFile)) < 0) { controller, taskFile)) < 0) {
goto cleanup; return -1;
} }
if (rc == 1) if (rc == 1)
killedAny = true; killedAny = true;
VIR_DEBUG("Iterate over children of %s (killedAny=%d)", keypath, killedAny); VIR_DEBUG("Iterate over children of %s (killedAny=%d)", keypath, killedAny);
if ((rc = virDirOpenIfExists(&dp, keypath)) < 0) if ((rc = virDirOpenIfExists(&dp, keypath)) < 0)
goto cleanup; return -1;
if (rc == 0) { if (rc == 0) {
VIR_DEBUG("Path %s does not exist, assuming done", keypath); VIR_DEBUG("Path %s does not exist, assuming done", keypath);
@ -2504,11 +2503,11 @@ virCgroupKillRecursiveInternal(virCgroupPtr group,
VIR_DEBUG("Process subdir %s", ent->d_name); VIR_DEBUG("Process subdir %s", ent->d_name);
if (virCgroupNew(-1, ent->d_name, group, -1, &subgroup) < 0) if (virCgroupNew(-1, ent->d_name, group, -1, &subgroup) < 0)
goto cleanup; return -1;
if ((rc = virCgroupKillRecursiveInternal(subgroup, signum, pids, if ((rc = virCgroupKillRecursiveInternal(subgroup, signum, pids,
controller, taskFile, true)) < 0) controller, taskFile, true)) < 0)
goto cleanup; return -1;
if (rc == 1) if (rc == 1)
killedAny = true; killedAny = true;
@ -2516,13 +2515,10 @@ virCgroupKillRecursiveInternal(virCgroupPtr group,
virCgroupRemove(subgroup); virCgroupRemove(subgroup);
} }
if (direrr < 0) if (direrr < 0)
goto cleanup; return -1;
done: done:
ret = killedAny ? 1 : 0; return killedAny ? 1 : 0;
cleanup:
return ret;
} }

View File

@ -456,7 +456,6 @@ virCommandMassCloseGetFDsLinux(virCommandPtr cmd G_GNUC_UNUSED,
struct dirent *entry; struct dirent *entry;
const char *dirName = "/proc/self/fd"; const char *dirName = "/proc/self/fd";
int rc; int rc;
int ret = -1;
if (virDirOpen(&dp, dirName) < 0) if (virDirOpen(&dp, dirName) < 0)
return -1; return -1;
@ -468,18 +467,16 @@ virCommandMassCloseGetFDsLinux(virCommandPtr cmd G_GNUC_UNUSED,
virReportError(VIR_ERR_INTERNAL_ERROR, virReportError(VIR_ERR_INTERNAL_ERROR,
_("unable to parse FD: %s"), _("unable to parse FD: %s"),
entry->d_name); entry->d_name);
goto cleanup; return -1;
} }
ignore_value(virBitmapSetBit(fds, fd)); ignore_value(virBitmapSetBit(fds, fd));
} }
if (rc < 0) if (rc < 0)
goto cleanup; return -1;
ret = 0; return 0;
cleanup:
return ret;
} }
# else /* !__linux__ */ # else /* !__linux__ */

View File

@ -171,7 +171,6 @@ virDMSanitizepath(const char *path)
struct stat sb[2]; struct stat sb[2];
g_autoptr(DIR) dh = NULL; g_autoptr(DIR) dh = NULL;
const char *p; const char *p;
char *ret = NULL;
/* If a path is NOT provided then assume it's DM name */ /* If a path is NOT provided then assume it's DM name */
p = strrchr(path, '/'); p = strrchr(path, '/');
@ -206,12 +205,11 @@ virDMSanitizepath(const char *path)
if (stat(tmp, &sb[1]) == 0 && if (stat(tmp, &sb[1]) == 0 &&
sb[0].st_rdev == sb[0].st_rdev) { sb[0].st_rdev == sb[0].st_rdev) {
ret = g_steal_pointer(&tmp); return g_steal_pointer(&tmp);
break;
} }
} }
return ret; return NULL;
} }

View File

@ -836,7 +836,6 @@ static char *
virFileNBDDeviceFindUnused(void) virFileNBDDeviceFindUnused(void)
{ {
g_autoptr(DIR) dh = NULL; g_autoptr(DIR) dh = NULL;
char *ret = NULL;
struct dirent *de; struct dirent *de;
int direrr; int direrr;
@ -846,21 +845,19 @@ virFileNBDDeviceFindUnused(void)
while ((direrr = virDirRead(dh, &de, SYSFS_BLOCK_DIR)) > 0) { while ((direrr = virDirRead(dh, &de, SYSFS_BLOCK_DIR)) > 0) {
if (STRPREFIX(de->d_name, "nbd")) { if (STRPREFIX(de->d_name, "nbd")) {
int rv = virFileNBDDeviceIsBusy(de->d_name); int rv = virFileNBDDeviceIsBusy(de->d_name);
if (rv < 0) if (rv < 0)
goto cleanup; return NULL;
if (rv == 0) {
ret = g_strdup_printf("/dev/%s", de->d_name); if (rv == 0)
goto cleanup; return g_strdup_printf("/dev/%s", de->d_name);
}
} }
} }
if (direrr < 0) if (direrr < 0)
goto cleanup; return NULL;
virReportSystemError(EBUSY, "%s",
_("No free NBD devices"));
cleanup: virReportSystemError(EBUSY, "%s", _("No free NBD devices"));
return ret; return NULL;
} }
static bool static bool
@ -979,7 +976,6 @@ int virFileDeleteTree(const char *dir)
{ {
g_autoptr(DIR) dh = NULL; g_autoptr(DIR) dh = NULL;
struct dirent *de; struct dirent *de;
int ret = -1;
int direrr; int direrr;
/* Silently return 0 if passed NULL or directory doesn't exist */ /* Silently return 0 if passed NULL or directory doesn't exist */
@ -998,35 +994,32 @@ int virFileDeleteTree(const char *dir)
if (g_lstat(filepath, &sb) < 0) { if (g_lstat(filepath, &sb) < 0) {
virReportSystemError(errno, _("Cannot access '%s'"), virReportSystemError(errno, _("Cannot access '%s'"),
filepath); filepath);
goto cleanup; return -1;
} }
if (S_ISDIR(sb.st_mode)) { if (S_ISDIR(sb.st_mode)) {
if (virFileDeleteTree(filepath) < 0) if (virFileDeleteTree(filepath) < 0)
goto cleanup; return -1;
} else { } else {
if (unlink(filepath) < 0 && errno != ENOENT) { if (unlink(filepath) < 0 && errno != ENOENT) {
virReportSystemError(errno, virReportSystemError(errno,
_("Cannot delete file '%s'"), _("Cannot delete file '%s'"),
filepath); filepath);
goto cleanup; return -1;
} }
} }
} }
if (direrr < 0) if (direrr < 0)
goto cleanup; return -1;
if (rmdir(dir) < 0 && errno != ENOENT) { if (rmdir(dir) < 0 && errno != ENOENT) {
virReportSystemError(errno, virReportSystemError(errno,
_("Cannot delete directory '%s'"), _("Cannot delete directory '%s'"),
dir); dir);
goto cleanup; return -1;
} }
ret = 0; return 0;
cleanup:
return ret;
} }
/* Like read(), but restarts after EINTR. Doesn't play /* Like read(), but restarts after EINTR. Doesn't play
@ -2949,7 +2942,6 @@ int virFileChownFiles(const char *name,
gid_t gid) gid_t gid)
{ {
struct dirent *ent; struct dirent *ent;
int ret = -1;
int direrr; int direrr;
g_autoptr(DIR) dir = NULL; g_autoptr(DIR) dir = NULL;
@ -2969,17 +2961,14 @@ int virFileChownFiles(const char *name,
_("cannot chown '%s' to (%u, %u)"), _("cannot chown '%s' to (%u, %u)"),
ent->d_name, (unsigned int) uid, ent->d_name, (unsigned int) uid,
(unsigned int) gid); (unsigned int) gid);
goto cleanup; return -1;
} }
} }
if (direrr < 0) if (direrr < 0)
goto cleanup; return -1;
ret = 0; return 0;
cleanup:
return ret;
} }
#else /* WIN32 */ #else /* WIN32 */

View File

@ -2900,7 +2900,6 @@ virNetDevRDMAFeature(const char *ifname,
g_autofree char *eth_res_buf = NULL; g_autofree char *eth_res_buf = NULL;
g_autoptr(DIR) dirp = NULL; g_autoptr(DIR) dirp = NULL;
struct dirent *dp; struct dirent *dp;
int ret = -1;
if (!virFileExists(SYSFS_INFINIBAND_DIR)) if (!virFileExists(SYSFS_INFINIBAND_DIR))
return 0; return 0;
@ -2913,12 +2912,11 @@ virNetDevRDMAFeature(const char *ifname,
/* If /sys/class/net/<ifname>/device/resource doesn't exist it is not a PCI /* If /sys/class/net/<ifname>/device/resource doesn't exist it is not a PCI
* device and therefore it will not have RDMA. */ * device and therefore it will not have RDMA. */
if (!virFileExists(eth_devpath)) { if (!virFileExists(eth_devpath)) {
ret = 0; return 0;
goto cleanup;
} }
if (virFileReadAll(eth_devpath, RESOURCE_FILE_LEN, &eth_res_buf) < 0) if (virFileReadAll(eth_devpath, RESOURCE_FILE_LEN, &eth_res_buf) < 0)
goto cleanup; return -1;
while (virDirRead(dirp, &dp, SYSFS_INFINIBAND_DIR) > 0) { while (virDirRead(dirp, &dp, SYSFS_INFINIBAND_DIR) > 0) {
g_autofree char *ib_res_buf = NULL; g_autofree char *ib_res_buf = NULL;
@ -2931,10 +2929,8 @@ virNetDevRDMAFeature(const char *ifname,
break; break;
} }
} }
ret = 0;
cleanup: return 0;
return ret;
} }

View File

@ -739,7 +739,6 @@ virNumaGetPages(int node,
unsigned long long **pages_free, unsigned long long **pages_free,
size_t *npages) size_t *npages)
{ {
int ret = -1;
g_autoptr(DIR) dir = NULL; g_autoptr(DIR) dir = NULL;
int direrr = 0; int direrr = 0;
struct dirent *entry; struct dirent *entry;
@ -763,12 +762,12 @@ virNumaGetPages(int node,
* slightly different information. So we take the total memory on a node * slightly different information. So we take the total memory on a node
* and subtract memory taken by the huge pages. */ * and subtract memory taken by the huge pages. */
if (virNumaGetHugePageInfoDir(&path, node) < 0) if (virNumaGetHugePageInfoDir(&path, node) < 0)
goto cleanup; return -1;
/* It's okay if the @path doesn't exist. Maybe we are running on /* It's okay if the @path doesn't exist. Maybe we are running on
* system without huge pages support where the path may not exist. */ * system without huge pages support where the path may not exist. */
if (virDirOpenIfExists(&dir, path) < 0) if (virDirOpenIfExists(&dir, path) < 0)
goto cleanup; return -1;
while (dir && (direrr = virDirRead(dir, &entry, path)) > 0) { while (dir && (direrr = virDirRead(dir, &entry, path)) > 0) {
const char *page_name = entry->d_name; const char *page_name = entry->d_name;
@ -789,17 +788,17 @@ virNumaGetPages(int node,
virReportError(VIR_ERR_INTERNAL_ERROR, virReportError(VIR_ERR_INTERNAL_ERROR,
_("unable to parse %s"), _("unable to parse %s"),
entry->d_name); entry->d_name);
goto cleanup; return -1;
} }
if (virNumaGetHugePageInfo(node, page_size, if (virNumaGetHugePageInfo(node, page_size,
&page_avail, &page_free) < 0) &page_avail, &page_free) < 0)
goto cleanup; return -1;
if (VIR_REALLOC_N(tmp_size, ntmp + 1) < 0 || if (VIR_REALLOC_N(tmp_size, ntmp + 1) < 0 ||
VIR_REALLOC_N(tmp_avail, ntmp + 1) < 0 || VIR_REALLOC_N(tmp_avail, ntmp + 1) < 0 ||
VIR_REALLOC_N(tmp_free, ntmp + 1) < 0) VIR_REALLOC_N(tmp_free, ntmp + 1) < 0)
goto cleanup; return -1;
tmp_size[ntmp] = page_size; tmp_size[ntmp] = page_size;
tmp_avail[ntmp] = page_avail; tmp_avail[ntmp] = page_avail;
@ -812,17 +811,17 @@ virNumaGetPages(int node,
} }
if (direrr < 0) if (direrr < 0)
goto cleanup; return -1;
/* Now append the ordinary system pages */ /* Now append the ordinary system pages */
if (VIR_REALLOC_N(tmp_size, ntmp + 1) < 0 || if (VIR_REALLOC_N(tmp_size, ntmp + 1) < 0 ||
VIR_REALLOC_N(tmp_avail, ntmp + 1) < 0 || VIR_REALLOC_N(tmp_avail, ntmp + 1) < 0 ||
VIR_REALLOC_N(tmp_free, ntmp + 1) < 0) VIR_REALLOC_N(tmp_free, ntmp + 1) < 0)
goto cleanup; return -1;
if (virNumaGetPageInfo(node, system_page_size, huge_page_sum, if (virNumaGetPageInfo(node, system_page_size, huge_page_sum,
&tmp_avail[ntmp], &tmp_free[ntmp]) < 0) &tmp_avail[ntmp], &tmp_free[ntmp]) < 0)
goto cleanup; return -1;
tmp_size[ntmp] = system_page_size; tmp_size[ntmp] = system_page_size;
ntmp++; ntmp++;
@ -852,9 +851,7 @@ virNumaGetPages(int node,
tmp_free = NULL; tmp_free = NULL;
} }
*npages = ntmp; *npages = ntmp;
ret = 0; return 0;
cleanup:
return ret;
} }

View File

@ -1706,7 +1706,6 @@ int virPCIDeviceFileIterate(virPCIDevicePtr dev,
{ {
g_autofree char *pcidir = NULL; g_autofree char *pcidir = NULL;
g_autoptr(DIR) dir = NULL; g_autoptr(DIR) dir = NULL;
int ret = -1;
struct dirent *ent; struct dirent *ent;
int direrr; int direrr;
@ -1715,7 +1714,7 @@ int virPCIDeviceFileIterate(virPCIDevicePtr dev,
dev->address.function); dev->address.function);
if (virDirOpen(&dir, pcidir) < 0) if (virDirOpen(&dir, pcidir) < 0)
goto cleanup; return -1;
while ((direrr = virDirRead(dir, &ent, pcidir)) > 0) { while ((direrr = virDirRead(dir, &ent, pcidir)) > 0) {
g_autofree char *file = NULL; g_autofree char *file = NULL;
@ -1731,16 +1730,13 @@ int virPCIDeviceFileIterate(virPCIDevicePtr dev,
STREQ(ent->d_name, "reset")) { STREQ(ent->d_name, "reset")) {
file = g_strdup_printf("%s/%s", pcidir, ent->d_name); file = g_strdup_printf("%s/%s", pcidir, ent->d_name);
if ((actor)(dev, file, opaque) < 0) if ((actor)(dev, file, opaque) < 0)
goto cleanup; return -1;
} }
} }
if (direrr < 0) if (direrr < 0)
goto cleanup; return -1;
ret = 0; return 0;
cleanup:
return ret;
} }
@ -1756,7 +1752,6 @@ virPCIDeviceAddressIOMMUGroupIterate(virPCIDeviceAddressPtr orig,
{ {
g_autofree char *groupPath = NULL; g_autofree char *groupPath = NULL;
g_autoptr(DIR) groupDir = NULL; g_autoptr(DIR) groupDir = NULL;
int ret = -1;
struct dirent *ent; struct dirent *ent;
int direrr; int direrr;
@ -1765,8 +1760,7 @@ virPCIDeviceAddressIOMMUGroupIterate(virPCIDeviceAddressPtr orig,
if (virDirOpenQuiet(&groupDir, groupPath) < 0) { if (virDirOpenQuiet(&groupDir, groupPath) < 0) {
/* just process the original device, nothing more */ /* just process the original device, nothing more */
ret = (actor)(orig, opaque); return (actor)(orig, opaque);
goto cleanup;
} }
while ((direrr = virDirRead(groupDir, &ent, groupPath)) > 0) { while ((direrr = virDirRead(groupDir, &ent, groupPath)) > 0) {
@ -1776,19 +1770,16 @@ virPCIDeviceAddressIOMMUGroupIterate(virPCIDeviceAddressPtr orig,
virReportError(VIR_ERR_INTERNAL_ERROR, virReportError(VIR_ERR_INTERNAL_ERROR,
_("Found invalid device link '%s' in '%s'"), _("Found invalid device link '%s' in '%s'"),
ent->d_name, groupPath); ent->d_name, groupPath);
goto cleanup; return -1;
} }
if ((actor)(&newDev, opaque) < 0) if ((actor)(&newDev, opaque) < 0)
goto cleanup; return -1;
} }
if (direrr < 0) if (direrr < 0)
goto cleanup; return -1;
ret = 0; return 0;
cleanup:
return ret;
} }

View File

@ -786,23 +786,18 @@ virResctrlGetInfo(virResctrlInfoPtr resctrl)
ret = virDirOpenIfExists(&dirp, SYSFS_RESCTRL_PATH "/info"); ret = virDirOpenIfExists(&dirp, SYSFS_RESCTRL_PATH "/info");
if (ret <= 0) if (ret <= 0)
goto cleanup; return ret;
ret = virResctrlGetMemoryBandwidthInfo(resctrl); if ((ret = virResctrlGetMemoryBandwidthInfo(resctrl)) < 0)
if (ret < 0) return -1;
goto cleanup;
ret = virResctrlGetCacheInfo(resctrl, dirp); if ((ret = virResctrlGetCacheInfo(resctrl, dirp)) < 0)
if (ret < 0) return -1;
goto cleanup;
ret = virResctrlGetMonitorInfo(resctrl); if ((ret = virResctrlGetMonitorInfo(resctrl)) < 0)
if (ret < 0) return -1;
goto cleanup;
ret = 0; return 0;
cleanup:
return ret;
} }

View File

@ -109,7 +109,6 @@ virSCSIDeviceGetSgName(const char *sysfs_prefix,
g_autoptr(DIR) dir = NULL; g_autoptr(DIR) dir = NULL;
struct dirent *entry; struct dirent *entry;
g_autofree char *path = NULL; g_autofree char *path = NULL;
char *sg = NULL;
unsigned int adapter_id; unsigned int adapter_id;
const char *prefix = sysfs_prefix ? sysfs_prefix : SYSFS_SCSI_DEVICES; const char *prefix = sysfs_prefix ? sysfs_prefix : SYSFS_SCSI_DEVICES;
@ -120,16 +119,13 @@ virSCSIDeviceGetSgName(const char *sysfs_prefix,
bus, target, unit); bus, target, unit);
if (virDirOpen(&dir, path) < 0) if (virDirOpen(&dir, path) < 0)
goto cleanup; return NULL;
while (virDirRead(dir, &entry, path) > 0) { /* Assume a single directory entry */
/* Assume a single directory entry */ if (virDirRead(dir, &entry, path) > 0)
sg = g_strdup(entry->d_name); return g_strdup(entry->d_name);
break;
}
cleanup: return NULL;
return sg;
} }
/* Returns device name (e.g. "sdc") on success, or NULL /* Returns device name (e.g. "sdc") on success, or NULL
@ -145,7 +141,6 @@ virSCSIDeviceGetDevName(const char *sysfs_prefix,
g_autoptr(DIR) dir = NULL; g_autoptr(DIR) dir = NULL;
struct dirent *entry; struct dirent *entry;
g_autofree char *path = NULL; g_autofree char *path = NULL;
char *name = NULL;
unsigned int adapter_id; unsigned int adapter_id;
const char *prefix = sysfs_prefix ? sysfs_prefix : SYSFS_SCSI_DEVICES; const char *prefix = sysfs_prefix ? sysfs_prefix : SYSFS_SCSI_DEVICES;
@ -156,15 +151,12 @@ virSCSIDeviceGetDevName(const char *sysfs_prefix,
target, unit); target, unit);
if (virDirOpen(&dir, path) < 0) if (virDirOpen(&dir, path) < 0)
goto cleanup; return NULL;
while (virDirRead(dir, &entry, path) > 0) { if (virDirRead(dir, &entry, path) > 0)
name = g_strdup(entry->d_name); return g_strdup(entry->d_name);
break;
}
cleanup: return NULL;
return name;
} }
virSCSIDevicePtr virSCSIDevicePtr

View File

@ -1623,22 +1623,18 @@ virHostHasIOMMU(void)
{ {
g_autoptr(DIR) iommuDir = NULL; g_autoptr(DIR) iommuDir = NULL;
struct dirent *iommuGroup = NULL; struct dirent *iommuGroup = NULL;
bool ret = false;
int direrr; int direrr;
if (virDirOpenQuiet(&iommuDir, "/sys/kernel/iommu_groups/") < 0) if (virDirOpenQuiet(&iommuDir, "/sys/kernel/iommu_groups/") < 0)
goto cleanup; return false;
while ((direrr = virDirRead(iommuDir, &iommuGroup, NULL)) > 0) while ((direrr = virDirRead(iommuDir, &iommuGroup, NULL)) > 0)
break; break;
if (direrr < 0 || !iommuGroup) if (direrr < 0 || !iommuGroup)
goto cleanup; return false;
ret = true; return true;
cleanup:
return ret;
} }
@ -1656,7 +1652,6 @@ virHostHasIOMMU(void)
char * char *
virHostGetDRMRenderNode(void) virHostGetDRMRenderNode(void)
{ {
char *ret = NULL;
g_autoptr(DIR) driDir = NULL; g_autoptr(DIR) driDir = NULL;
const char *driPath = "/dev/dri"; const char *driPath = "/dev/dri";
struct dirent *ent = NULL; struct dirent *ent = NULL;
@ -1674,19 +1669,16 @@ virHostGetDRMRenderNode(void)
} }
if (dirErr < 0) if (dirErr < 0)
goto cleanup; return NULL;
/* even if /dev/dri exists, there might be no renderDX nodes available */ /* even if /dev/dri exists, there might be no renderDX nodes available */
if (!have_rendernode) { if (!have_rendernode) {
virReportError(VIR_ERR_INTERNAL_ERROR, "%s", virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
_("No DRM render nodes available")); _("No DRM render nodes available"));
goto cleanup; return NULL;
} }
ret = g_strdup_printf("%s/%s", driPath, ent->d_name); return g_strdup_printf("%s/%s", driPath, ent->d_name);
cleanup:
return ret;
} }

View File

@ -365,7 +365,6 @@ virVHBAGetHostByWWN(const char *sysfs_prefix,
const char *prefix = sysfs_prefix ? sysfs_prefix : SYSFS_FC_HOST_PATH; const char *prefix = sysfs_prefix ? sysfs_prefix : SYSFS_FC_HOST_PATH;
struct dirent *entry = NULL; struct dirent *entry = NULL;
g_autoptr(DIR) dir = NULL; g_autoptr(DIR) dir = NULL;
char *ret = NULL;
if (virDirOpen(&dir, prefix) < 0) if (virDirOpen(&dir, prefix) < 0)
return NULL; return NULL;
@ -375,24 +374,22 @@ virVHBAGetHostByWWN(const char *sysfs_prefix,
if ((rc = vhbaReadCompareWWN(prefix, entry->d_name, if ((rc = vhbaReadCompareWWN(prefix, entry->d_name,
"node_name", wwnn)) < 0) "node_name", wwnn)) < 0)
goto cleanup; return NULL;
if (rc == 0) if (rc == 0)
continue; continue;
if ((rc = vhbaReadCompareWWN(prefix, entry->d_name, if ((rc = vhbaReadCompareWWN(prefix, entry->d_name,
"port_name", wwpn)) < 0) "port_name", wwpn)) < 0)
goto cleanup; return NULL;
if (rc == 0) if (rc == 0)
continue; continue;
ret = g_strdup(entry->d_name); return g_strdup(entry->d_name);
break;
} }
cleanup: return NULL;
return ret;
} }
/* virVHBAGetHostByFabricWWN: /* virVHBAGetHostByFabricWWN:

View File

@ -516,12 +516,11 @@ testQemuGetLatestCapsForArch(const char *arch,
unsigned long maxver = 0; unsigned long maxver = 0;
unsigned long ver; unsigned long ver;
g_autofree char *maxname = NULL; g_autofree char *maxname = NULL;
char *ret = NULL;
fullsuffix = g_strdup_printf("%s.%s", arch, suffix); fullsuffix = g_strdup_printf("%s.%s", arch, suffix);
if (virDirOpen(&dir, TEST_QEMU_CAPS_PATH) < 0) if (virDirOpen(&dir, TEST_QEMU_CAPS_PATH) < 0)
goto cleanup; return NULL;
while ((rc = virDirRead(dir, &ent, TEST_QEMU_CAPS_PATH)) > 0) { while ((rc = virDirRead(dir, &ent, TEST_QEMU_CAPS_PATH)) > 0) {
g_autofree char *tmp = NULL; g_autofree char *tmp = NULL;
@ -547,18 +546,15 @@ testQemuGetLatestCapsForArch(const char *arch,
} }
if (rc < 0) if (rc < 0)
goto cleanup; return NULL;
if (!maxname) { if (!maxname) {
VIR_TEST_VERBOSE("failed to find capabilities for '%s' in '%s'", VIR_TEST_VERBOSE("failed to find capabilities for '%s' in '%s'",
arch, TEST_QEMU_CAPS_PATH); arch, TEST_QEMU_CAPS_PATH);
goto cleanup; return NULL;
} }
ret = g_strdup_printf("%s/%s", TEST_QEMU_CAPS_PATH, maxname); return g_strdup_printf("%s/%s", TEST_QEMU_CAPS_PATH, maxname);
cleanup:
return ret;
} }
@ -607,7 +603,6 @@ testQemuCapsIterate(const char *suffix,
struct dirent *ent; struct dirent *ent;
g_autoptr(DIR) dir = NULL; g_autoptr(DIR) dir = NULL;
int rc; int rc;
int ret = -1;
bool fail = false; bool fail = false;
if (!callback) if (!callback)
@ -616,11 +611,11 @@ testQemuCapsIterate(const char *suffix,
/* Validate suffix */ /* Validate suffix */
if (!STRPREFIX(suffix, ".")) { if (!STRPREFIX(suffix, ".")) {
VIR_TEST_VERBOSE("malformed suffix '%s'", suffix); VIR_TEST_VERBOSE("malformed suffix '%s'", suffix);
goto cleanup; return -1;
} }
if (virDirOpen(&dir, TEST_QEMU_CAPS_PATH) < 0) if (virDirOpen(&dir, TEST_QEMU_CAPS_PATH) < 0)
goto cleanup; return -1;
while ((rc = virDirRead(dir, &ent, TEST_QEMU_CAPS_PATH)) > 0) { while ((rc = virDirRead(dir, &ent, TEST_QEMU_CAPS_PATH)) > 0) {
g_autofree char *tmp = g_strdup(ent->d_name); g_autofree char *tmp = g_strdup(ent->d_name);
@ -634,13 +629,13 @@ testQemuCapsIterate(const char *suffix,
/* Strip the leading prefix */ /* Strip the leading prefix */
if (!(version = STRSKIP(tmp, "caps_"))) { if (!(version = STRSKIP(tmp, "caps_"))) {
VIR_TEST_VERBOSE("malformed file name '%s'", ent->d_name); VIR_TEST_VERBOSE("malformed file name '%s'", ent->d_name);
goto cleanup; return -1;
} }
/* Find the last dot */ /* Find the last dot */
if (!(archName = strrchr(tmp, '.'))) { if (!(archName = strrchr(tmp, '.'))) {
VIR_TEST_VERBOSE("malformed file name '%s'", ent->d_name); VIR_TEST_VERBOSE("malformed file name '%s'", ent->d_name);
goto cleanup; return -1;
} }
/* The version number and the architecture name are separated by /* The version number and the architecture name are separated by
@ -661,12 +656,9 @@ testQemuCapsIterate(const char *suffix,
} }
if (rc < 0 || fail) if (rc < 0 || fail)
goto cleanup; return -1;
ret = 0; return 0;
cleanup:
return ret;
} }