1
0
mirror of https://gitlab.com/libvirt/libvirt.git synced 2025-03-07 17:28:15 +00:00

storage: Cleanup logical volume creation code

This patch plugs two memory leaks, removes some useless and confusing
constructs and renames renames "cleanup" label as "error" since it is
only used for error path rather then being common for both success and
error paths.
This commit is contained in:
Jiri Denemark 2013-03-06 12:57:40 +01:00
parent 80e524de86
commit 4b0cb4a745

View File

@ -700,7 +700,7 @@ virStorageBackendLogicalCreateVol(virConnectPtr conn,
virStoragePoolObjPtr pool, virStoragePoolObjPtr pool,
virStorageVolDefPtr vol) virStorageVolDefPtr vol)
{ {
int fdret, fd = -1; int fd = -1;
virCommandPtr cmd = NULL; virCommandPtr cmd = NULL;
virErrorPtr err; virErrorPtr err;
@ -741,11 +741,13 @@ virStorageBackendLogicalCreateVol(virConnectPtr conn,
virCommandAddArg(cmd, pool->def->source.name); virCommandAddArg(cmd, pool->def->source.name);
if (virCommandRun(cmd, NULL) < 0) if (virCommandRun(cmd, NULL) < 0)
goto cleanup; goto error;
if ((fdret = virStorageBackendVolOpen(vol->target.path)) < 0) virCommandFree(cmd);
goto cleanup; cmd = NULL;
fd = fdret;
if ((fd = virStorageBackendVolOpen(vol->target.path)) < 0)
goto error;
/* We can only chown/grp if root */ /* We can only chown/grp if root */
if (getuid() == 0) { if (getuid() == 0) {
@ -753,40 +755,40 @@ virStorageBackendLogicalCreateVol(virConnectPtr conn,
virReportSystemError(errno, virReportSystemError(errno,
_("cannot set file owner '%s'"), _("cannot set file owner '%s'"),
vol->target.path); vol->target.path);
goto cleanup; goto error;
} }
} }
if (fchmod(fd, vol->target.perms.mode) < 0) { if (fchmod(fd, vol->target.perms.mode) < 0) {
virReportSystemError(errno, virReportSystemError(errno,
_("cannot set file mode '%s'"), _("cannot set file mode '%s'"),
vol->target.path); vol->target.path);
goto cleanup; goto error;
} }
if (VIR_CLOSE(fd) < 0) { if (VIR_CLOSE(fd) < 0) {
virReportSystemError(errno, virReportSystemError(errno,
_("cannot close file '%s'"), _("cannot close file '%s'"),
vol->target.path); vol->target.path);
goto cleanup; goto error;
} }
fd = -1;
/* Fill in data about this new vol */ /* Fill in data about this new vol */
if (virStorageBackendLogicalFindLVs(pool, vol) < 0) { if (virStorageBackendLogicalFindLVs(pool, vol) < 0) {
virReportSystemError(errno, virReportSystemError(errno,
_("cannot find newly created volume '%s'"), _("cannot find newly created volume '%s'"),
vol->target.path); vol->target.path);
goto cleanup; goto error;
} }
return 0; return 0;
cleanup: error:
err = virSaveLastError(); err = virSaveLastError();
VIR_FORCE_CLOSE(fd); VIR_FORCE_CLOSE(fd);
virStorageBackendLogicalDeleteVol(conn, pool, vol, 0); virStorageBackendLogicalDeleteVol(conn, pool, vol, 0);
virCommandFree(cmd); virCommandFree(cmd);
virSetError(err); virSetError(err);
virFreeError(err);
return -1; return -1;
} }