qemuDomainSaveImageOpen: Refactor handling of errors

Return error codes directly and fix weird reporting of errors via
temporary variable.

Signed-off-by: Peter Krempa <pkrempa@redhat.com>
Reviewed-by: Ján Tomko <jtomko@redhat.com>
This commit is contained in:
Peter Krempa 2020-04-22 16:52:24 +02:00
parent 9219424f56
commit d9792233ec

View File

@ -6705,17 +6705,18 @@ qemuDomainSaveImageOpen(virQEMUDriverPtr driver,
if (directFlag < 0) { if (directFlag < 0) {
virReportError(VIR_ERR_OPERATION_FAILED, "%s", virReportError(VIR_ERR_OPERATION_FAILED, "%s",
_("bypass cache unsupported by this system")); _("bypass cache unsupported by this system"));
goto error; return -1;
} }
oflags |= directFlag; oflags |= directFlag;
} }
if ((fd = qemuOpenFile(driver, NULL, path, oflags, NULL)) < 0) if ((fd = qemuOpenFile(driver, NULL, path, oflags, NULL)) < 0)
goto error; return -1;
if (bypass_cache && if (bypass_cache &&
!(*wrapperFd = virFileWrapperFdNew(&fd, path, !(*wrapperFd = virFileWrapperFdNew(&fd, path,
VIR_FILE_WRAPPER_BYPASS_CACHE))) VIR_FILE_WRAPPER_BYPASS_CACHE)))
goto error; return -1;
data = g_new0(virQEMUSaveData, 1); data = g_new0(virQEMUSaveData, 1);
@ -6726,35 +6727,38 @@ qemuDomainSaveImageOpen(virQEMUDriverPtr driver,
virReportSystemError(errno, virReportSystemError(errno,
_("cannot remove corrupt file: %s"), _("cannot remove corrupt file: %s"),
path); path);
return -1;
} else { } else {
ret = -3; return -3;
} }
} else {
virReportError(VIR_ERR_OPERATION_FAILED,
"%s", _("failed to read qemu header"));
} }
goto error;
virReportError(VIR_ERR_OPERATION_FAILED,
"%s", _("failed to read qemu header"));
return -1;
} }
if (memcmp(header->magic, QEMU_SAVE_MAGIC, sizeof(header->magic)) != 0) { if (memcmp(header->magic, QEMU_SAVE_MAGIC, sizeof(header->magic)) != 0) {
const char *msg = _("image magic is incorrect"); if (memcmp(header->magic, QEMU_SAVE_PARTIAL, sizeof(header->magic)) == 0) {
if (memcmp(header->magic, QEMU_SAVE_PARTIAL,
sizeof(header->magic)) == 0) {
msg = _("save image is incomplete");
if (unlink_corrupt) { if (unlink_corrupt) {
if (unlink(path) < 0) { if (unlink(path) < 0) {
virReportSystemError(errno, virReportSystemError(errno,
_("cannot remove corrupt file: %s"), _("cannot remove corrupt file: %s"),
path); path);
return -1;
} else { } else {
ret = -3; return -3;
} }
goto error;
} }
virReportError(VIR_ERR_OPERATION_FAILED, "%s",
_("save image is incomplete"));
return -1;
} }
virReportError(VIR_ERR_OPERATION_FAILED, "%s", msg);
goto error; virReportError(VIR_ERR_OPERATION_FAILED, "%s",
_("image magic is incorrect"));
return -1;
} }
if (header->version > QEMU_SAVE_VERSION) { if (header->version > QEMU_SAVE_VERSION) {
@ -6766,13 +6770,13 @@ qemuDomainSaveImageOpen(virQEMUDriverPtr driver,
virReportError(VIR_ERR_OPERATION_FAILED, virReportError(VIR_ERR_OPERATION_FAILED,
_("image version is not supported (%d > %d)"), _("image version is not supported (%d > %d)"),
header->version, QEMU_SAVE_VERSION); header->version, QEMU_SAVE_VERSION);
goto error; return -1;
} }
if (header->data_len <= 0) { if (header->data_len <= 0) {
virReportError(VIR_ERR_OPERATION_FAILED, virReportError(VIR_ERR_OPERATION_FAILED,
_("invalid header data length: %d"), header->data_len); _("invalid header data length: %d"), header->data_len);
goto error; return -1;
} }
if (header->cookieOffset) if (header->cookieOffset)
@ -6787,7 +6791,7 @@ qemuDomainSaveImageOpen(virQEMUDriverPtr driver,
if (saferead(fd, data->xml, xml_len) != xml_len) { if (saferead(fd, data->xml, xml_len) != xml_len) {
virReportError(VIR_ERR_OPERATION_FAILED, virReportError(VIR_ERR_OPERATION_FAILED,
"%s", _("failed to read domain XML")); "%s", _("failed to read domain XML"));
goto error; return -1;
} }
if (cookie_len > 0) { if (cookie_len > 0) {
@ -6796,7 +6800,7 @@ qemuDomainSaveImageOpen(virQEMUDriverPtr driver,
if (saferead(fd, data->cookie, cookie_len) != cookie_len) { if (saferead(fd, data->cookie, cookie_len) != cookie_len) {
virReportError(VIR_ERR_OPERATION_FAILED, "%s", virReportError(VIR_ERR_OPERATION_FAILED, "%s",
_("failed to read cookie")); _("failed to read cookie"));
goto error; return -1;
} }
} }
@ -6804,7 +6808,7 @@ qemuDomainSaveImageOpen(virQEMUDriverPtr driver,
if (!(def = virDomainDefParseString(data->xml, driver->xmlopt, qemuCaps, if (!(def = virDomainDefParseString(data->xml, driver->xmlopt, qemuCaps,
VIR_DOMAIN_DEF_PARSE_INACTIVE | VIR_DOMAIN_DEF_PARSE_INACTIVE |
VIR_DOMAIN_DEF_PARSE_SKIP_VALIDATE))) VIR_DOMAIN_DEF_PARSE_SKIP_VALIDATE)))
goto error; return -1;
*ret_def = g_steal_pointer(&def); *ret_def = g_steal_pointer(&def);
*ret_data = g_steal_pointer(&data); *ret_data = g_steal_pointer(&data);
@ -6813,9 +6817,6 @@ qemuDomainSaveImageOpen(virQEMUDriverPtr driver,
fd = -1; fd = -1;
return ret; return ret;
error:
return ret;
} }
static int ATTRIBUTE_NONNULL(4) ATTRIBUTE_NONNULL(5) ATTRIBUTE_NONNULL(6) static int ATTRIBUTE_NONNULL(4) ATTRIBUTE_NONNULL(5) ATTRIBUTE_NONNULL(6)