qemu_driver.c: use g_auto* in some functions

This patch changes qemuDomainSnapshotLoad, qemuDomainCheckpointLoad and
qemuStateInitialize to use g_autoptr() and g_autofree, cleaning up
some virObjectUnref() and VIR_FREE() calls on each.

The reason this is being sent separately is because these are not
trivial search/replace cases. In all these functions some strings
declarations are moved inside local loops, where they are in fact
used, allowing us to erase VIR_FREE() calls that were made inside
the loop and in 'cleanup' labels.

Following patches with tackle more trivial cases of g_auto* usage
in all qemu_driver.c file.

Suggested-by: Ján Tomko <jtomko@redhat.com>
Signed-off-by: Daniel Henrique Barboza <danielhb413@gmail.com>
Reviewed-by: Michal Privoznik <mprivozn@redhat.com>
This commit is contained in:
Daniel Henrique Barboza 2019-10-16 17:54:29 -03:00 committed by Michal Privoznik
parent 813510f95c
commit af6e383e4b

View File

@ -386,11 +386,9 @@ qemuDomainSnapshotLoad(virDomainObjPtr vm,
void *data) void *data)
{ {
char *baseDir = (char *)data; char *baseDir = (char *)data;
char *snapDir = NULL; g_autofree char *snapDir = NULL;
DIR *dir = NULL; DIR *dir = NULL;
struct dirent *entry; struct dirent *entry;
char *xmlStr;
char *fullpath;
virDomainSnapshotDefPtr def = NULL; virDomainSnapshotDefPtr def = NULL;
virDomainMomentObjPtr snap = NULL; virDomainMomentObjPtr snap = NULL;
virDomainMomentObjPtr current = NULL; virDomainMomentObjPtr current = NULL;
@ -399,7 +397,7 @@ qemuDomainSnapshotLoad(virDomainObjPtr vm,
VIR_DOMAIN_SNAPSHOT_PARSE_DISKS | VIR_DOMAIN_SNAPSHOT_PARSE_DISKS |
VIR_DOMAIN_SNAPSHOT_PARSE_INTERNAL); VIR_DOMAIN_SNAPSHOT_PARSE_INTERNAL);
int ret = -1; int ret = -1;
virCapsPtr caps = NULL; g_autoptr(virCaps) caps = NULL;
int direrr; int direrr;
qemuDomainObjPrivatePtr priv; qemuDomainObjPrivatePtr priv;
@ -425,6 +423,9 @@ qemuDomainSnapshotLoad(virDomainObjPtr vm,
goto cleanup; goto cleanup;
while ((direrr = virDirRead(dir, &entry, NULL)) > 0) { while ((direrr = virDirRead(dir, &entry, NULL)) > 0) {
g_autofree char *xmlStr = NULL;
g_autofree char *fullpath = NULL;
/* NB: ignoring errors, so one malformed config doesn't /* NB: ignoring errors, so one malformed config doesn't
kill the whole process */ kill the whole process */
VIR_INFO("Loading snapshot file '%s'", entry->d_name); VIR_INFO("Loading snapshot file '%s'", entry->d_name);
@ -440,7 +441,6 @@ qemuDomainSnapshotLoad(virDomainObjPtr vm,
virReportSystemError(errno, virReportSystemError(errno,
_("Failed to read snapshot file %s"), _("Failed to read snapshot file %s"),
fullpath); fullpath);
VIR_FREE(fullpath);
continue; continue;
} }
@ -453,8 +453,6 @@ qemuDomainSnapshotLoad(virDomainObjPtr vm,
virReportError(VIR_ERR_INTERNAL_ERROR, virReportError(VIR_ERR_INTERNAL_ERROR,
_("Failed to parse snapshot XML from file '%s'"), _("Failed to parse snapshot XML from file '%s'"),
fullpath); fullpath);
VIR_FREE(fullpath);
VIR_FREE(xmlStr);
continue; continue;
} }
@ -468,9 +466,6 @@ qemuDomainSnapshotLoad(virDomainObjPtr vm,
vm->def->name); vm->def->name);
current = snap; current = snap;
} }
VIR_FREE(fullpath);
VIR_FREE(xmlStr);
} }
if (direrr < 0) if (direrr < 0)
virReportError(VIR_ERR_INTERNAL_ERROR, virReportError(VIR_ERR_INTERNAL_ERROR,
@ -497,8 +492,6 @@ qemuDomainSnapshotLoad(virDomainObjPtr vm,
ret = 0; ret = 0;
cleanup: cleanup:
VIR_DIR_CLOSE(dir); VIR_DIR_CLOSE(dir);
VIR_FREE(snapDir);
virObjectUnref(caps);
virObjectUnlock(vm); virObjectUnlock(vm);
return ret; return ret;
} }
@ -509,17 +502,15 @@ qemuDomainCheckpointLoad(virDomainObjPtr vm,
void *data) void *data)
{ {
char *baseDir = (char *)data; char *baseDir = (char *)data;
char *chkDir = NULL; g_autofree char *chkDir = NULL;
DIR *dir = NULL; DIR *dir = NULL;
struct dirent *entry; struct dirent *entry;
char *xmlStr;
char *fullpath;
virDomainCheckpointDefPtr def = NULL; virDomainCheckpointDefPtr def = NULL;
virDomainMomentObjPtr chk = NULL; virDomainMomentObjPtr chk = NULL;
virDomainMomentObjPtr current = NULL; virDomainMomentObjPtr current = NULL;
unsigned int flags = VIR_DOMAIN_CHECKPOINT_PARSE_REDEFINE; unsigned int flags = VIR_DOMAIN_CHECKPOINT_PARSE_REDEFINE;
int ret = -1; int ret = -1;
virCapsPtr caps = NULL; g_autoptr(virCaps) caps = NULL;
int direrr; int direrr;
qemuDomainObjPrivatePtr priv; qemuDomainObjPrivatePtr priv;
@ -544,6 +535,9 @@ qemuDomainCheckpointLoad(virDomainObjPtr vm,
goto cleanup; goto cleanup;
while ((direrr = virDirRead(dir, &entry, NULL)) > 0) { while ((direrr = virDirRead(dir, &entry, NULL)) > 0) {
g_autofree char *xmlStr = NULL;
g_autofree char *fullpath = NULL;
/* NB: ignoring errors, so one malformed config doesn't /* NB: ignoring errors, so one malformed config doesn't
kill the whole process */ kill the whole process */
VIR_INFO("Loading checkpoint file '%s'", entry->d_name); VIR_INFO("Loading checkpoint file '%s'", entry->d_name);
@ -559,7 +553,6 @@ qemuDomainCheckpointLoad(virDomainObjPtr vm,
virReportSystemError(errno, virReportSystemError(errno,
_("Failed to read checkpoint file %s"), _("Failed to read checkpoint file %s"),
fullpath); fullpath);
VIR_FREE(fullpath);
continue; continue;
} }
@ -572,8 +565,6 @@ qemuDomainCheckpointLoad(virDomainObjPtr vm,
virReportError(VIR_ERR_INTERNAL_ERROR, virReportError(VIR_ERR_INTERNAL_ERROR,
_("Failed to parse checkpoint XML from file '%s'"), _("Failed to parse checkpoint XML from file '%s'"),
fullpath); fullpath);
VIR_FREE(fullpath);
VIR_FREE(xmlStr);
virObjectUnref(def); virObjectUnref(def);
continue; continue;
} }
@ -581,9 +572,6 @@ qemuDomainCheckpointLoad(virDomainObjPtr vm,
chk = virDomainCheckpointAssignDef(vm->checkpoints, def); chk = virDomainCheckpointAssignDef(vm->checkpoints, def);
if (chk == NULL) if (chk == NULL)
virObjectUnref(def); virObjectUnref(def);
VIR_FREE(fullpath);
VIR_FREE(xmlStr);
} }
if (direrr < 0) if (direrr < 0)
virReportError(VIR_ERR_INTERNAL_ERROR, virReportError(VIR_ERR_INTERNAL_ERROR,
@ -608,8 +596,6 @@ qemuDomainCheckpointLoad(virDomainObjPtr vm,
ret = 0; ret = 0;
cleanup: cleanup:
VIR_DIR_CLOSE(dir); VIR_DIR_CLOSE(dir);
VIR_FREE(chkDir);
virObjectUnref(caps);
virObjectUnlock(vm); virObjectUnlock(vm);
return ret; return ret;
} }
@ -667,12 +653,11 @@ qemuStateInitialize(bool privileged,
virStateInhibitCallback callback, virStateInhibitCallback callback,
void *opaque) void *opaque)
{ {
char *driverConf = NULL; g_autofree char *driverConf = NULL;
virQEMUDriverConfigPtr cfg; virQEMUDriverConfigPtr cfg;
uid_t run_uid = -1; uid_t run_uid = -1;
gid_t run_gid = -1; gid_t run_gid = -1;
char *hugepagePath = NULL; g_autofree char *memoryBackingPath = NULL;
char *memoryBackingPath = NULL;
bool autostart = true; bool autostart = true;
size_t i; size_t i;
@ -713,7 +698,6 @@ qemuStateInitialize(bool privileged,
if (virQEMUDriverConfigLoadFile(cfg, driverConf, privileged) < 0) if (virQEMUDriverConfigLoadFile(cfg, driverConf, privileged) < 0)
goto error; goto error;
VIR_FREE(driverConf);
if (virQEMUDriverConfigValidate(cfg) < 0) if (virQEMUDriverConfigValidate(cfg) < 0)
goto error; goto error;
@ -837,7 +821,7 @@ qemuStateInitialize(bool privileged,
goto error; goto error;
if (privileged) { if (privileged) {
char *channeldir; g_autofree char *channeldir = NULL;
if (chown(cfg->libDir, cfg->user, cfg->group) < 0) { if (chown(cfg->libDir, cfg->user, cfg->group) < 0) {
virReportSystemError(errno, virReportSystemError(errno,
@ -890,10 +874,8 @@ qemuStateInitialize(bool privileged,
_("unable to set ownership of '%s' to %d:%d"), _("unable to set ownership of '%s' to %d:%d"),
channeldir, (int)cfg->user, channeldir, (int)cfg->user,
(int)cfg->group); (int)cfg->group);
VIR_FREE(channeldir);
goto error; goto error;
} }
VIR_FREE(channeldir);
if (chown(cfg->channelTargetDir, cfg->user, cfg->group) < 0) { if (chown(cfg->channelTargetDir, cfg->user, cfg->group) < 0) {
virReportSystemError(errno, virReportSystemError(errno,
_("unable to set ownership of '%s' to %d:%d"), _("unable to set ownership of '%s' to %d:%d"),
@ -944,6 +926,8 @@ qemuStateInitialize(bool privileged,
* it, since we can't assume the root mount point has permissions that * it, since we can't assume the root mount point has permissions that
* will let our spawned QEMU instances use it. */ * will let our spawned QEMU instances use it. */
for (i = 0; i < cfg->nhugetlbfs; i++) { for (i = 0; i < cfg->nhugetlbfs; i++) {
g_autofree char *hugepagePath = NULL;
hugepagePath = qemuGetBaseHugepagePath(&cfg->hugetlbfs[i]); hugepagePath = qemuGetBaseHugepagePath(&cfg->hugetlbfs[i]);
if (!hugepagePath) if (!hugepagePath)
@ -959,7 +943,6 @@ qemuStateInitialize(bool privileged,
virFileUpdatePerm(cfg->hugetlbfs[i].mnt_dir, virFileUpdatePerm(cfg->hugetlbfs[i].mnt_dir,
0, S_IXGRP | S_IXOTH) < 0) 0, S_IXGRP | S_IXOTH) < 0)
goto error; goto error;
VIR_FREE(hugepagePath);
} }
if (qemuGetMemoryBackingBasePath(cfg, &memoryBackingPath) < 0) if (qemuGetMemoryBackingBasePath(cfg, &memoryBackingPath) < 0)
@ -976,7 +959,6 @@ qemuStateInitialize(bool privileged,
virFileUpdatePerm(memoryBackingPath, virFileUpdatePerm(memoryBackingPath,
0, S_IXGRP | S_IXOTH) < 0) 0, S_IXGRP | S_IXOTH) < 0)
goto error; goto error;
VIR_FREE(memoryBackingPath);
if (!(qemu_driver->closeCallbacks = virCloseCallbacksNew())) if (!(qemu_driver->closeCallbacks = virCloseCallbacksNew()))
goto error; goto error;
@ -1045,9 +1027,6 @@ qemuStateInitialize(bool privileged,
return VIR_DRV_STATE_INIT_COMPLETE; return VIR_DRV_STATE_INIT_COMPLETE;
error: error:
VIR_FREE(driverConf);
VIR_FREE(hugepagePath);
VIR_FREE(memoryBackingPath);
qemuStateCleanup(); qemuStateCleanup();
return VIR_DRV_STATE_INIT_ERROR; return VIR_DRV_STATE_INIT_ERROR;
} }