all: Replace virGetLastError with virGetLastErrorCode where we can

Replace instances where we previously called virGetLastError just to
either get the code or to check if an error exists with
virGetLastErrorCode to avoid a validity pre-check.

Signed-off-by: Ramy Elkest <ramyelkest@gmail.com>
Reviewed-by: Erik Skultety <eskultet@redhat.com>
This commit is contained in:
ramyelkest 2018-05-05 13:04:21 +01:00 committed by Erik Skultety
parent 50e96bb2a1
commit 2b6667abbf
24 changed files with 39 additions and 55 deletions

View File

@ -302,8 +302,7 @@ static int virLockManagerLockDaemonSetupLockspace(const char *path)
0, NULL, NULL, NULL, 0, NULL, NULL, NULL,
(xdrproc_t)xdr_virLockSpaceProtocolCreateLockSpaceArgs, (char*)&args, (xdrproc_t)xdr_virLockSpaceProtocolCreateLockSpaceArgs, (char*)&args,
(xdrproc_t)xdr_void, NULL) < 0) { (xdrproc_t)xdr_void, NULL) < 0) {
virErrorPtr err = virGetLastError(); if (virGetLastErrorCode() == VIR_ERR_OPERATION_INVALID) {
if (err && err->code == VIR_ERR_OPERATION_INVALID) {
/* The lockspace already exists */ /* The lockspace already exists */
virResetLastError(); virResetLastError();
rv = 0; rv = 0;

View File

@ -1295,7 +1295,6 @@ static void virLXCControllerConsoleIO(int watch, int fd, int events, void *opaqu
*/ */
static int virLXCControllerMain(virLXCControllerPtr ctrl) static int virLXCControllerMain(virLXCControllerPtr ctrl)
{ {
virErrorPtr err;
int rc = -1; int rc = -1;
size_t i; size_t i;
@ -1347,8 +1346,7 @@ static int virLXCControllerMain(virLXCControllerPtr ctrl)
virNetDaemonRun(ctrl->daemon); virNetDaemonRun(ctrl->daemon);
err = virGetLastError(); if (virGetLastErrorCode() == VIR_ERR_OK)
if (!err || err->code == VIR_ERR_OK)
rc = wantReboot ? 1 : 0; rc = wantReboot ? 1 : 0;
cleanup: cleanup:

View File

@ -620,8 +620,7 @@ qemuAgentIO(int watch, int fd, int events, void *opaque)
/* Already have an error, so clear any new error */ /* Already have an error, so clear any new error */
virResetLastError(); virResetLastError();
} else { } else {
virErrorPtr err = virGetLastError(); if (virGetLastErrorCode() == VIR_ERR_OK)
if (!err)
virReportError(VIR_ERR_INTERNAL_ERROR, "%s", virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
_("Error while processing monitor IO")); _("Error while processing monitor IO"));
virCopyLastError(&mon->lastError); virCopyLastError(&mon->lastError);

View File

@ -298,8 +298,7 @@ virQEMUDriverConfigPtr virQEMUDriverConfigNew(bool privileged)
if (privileged && if (privileged &&
virFileFindHugeTLBFS(&cfg->hugetlbfs, &cfg->nhugetlbfs) < 0) { virFileFindHugeTLBFS(&cfg->hugetlbfs, &cfg->nhugetlbfs) < 0) {
/* This however is not implemented on all platforms. */ /* This however is not implemented on all platforms. */
virErrorPtr err = virGetLastError(); if (virGetLastErrorCode() != VIR_ERR_NO_SUPPORT)
if (err && err->code != VIR_ERR_NO_SUPPORT)
goto error; goto error;
} }

View File

@ -6662,7 +6662,7 @@ int qemuDomainObjExitMonitor(virQEMUDriverPtr driver,
{ {
qemuDomainObjExitMonitorInternal(driver, obj); qemuDomainObjExitMonitorInternal(driver, obj);
if (!virDomainObjIsActive(obj)) { if (!virDomainObjIsActive(obj)) {
if (!virGetLastError()) if (virGetLastErrorCode() == VIR_ERR_OK)
virReportError(VIR_ERR_OPERATION_FAILED, "%s", virReportError(VIR_ERR_OPERATION_FAILED, "%s",
_("domain is no longer running")); _("domain is no longer running"));
return -1; return -1;

View File

@ -1923,7 +1923,7 @@ static int qemuDomainResume(virDomainPtr dom)
if (qemuProcessStartCPUs(driver, vm, if (qemuProcessStartCPUs(driver, vm,
VIR_DOMAIN_RUNNING_UNPAUSED, VIR_DOMAIN_RUNNING_UNPAUSED,
QEMU_ASYNC_JOB_NONE) < 0) { QEMU_ASYNC_JOB_NONE) < 0) {
if (virGetLastError() == NULL) if (virGetLastErrorCode() == VIR_ERR_OK)
virReportError(VIR_ERR_OPERATION_FAILED, virReportError(VIR_ERR_OPERATION_FAILED,
"%s", _("resume operation failed")); "%s", _("resume operation failed"));
goto endjob; goto endjob;
@ -3185,7 +3185,7 @@ qemuFileWrapperFDClose(virDomainObjPtr vm,
ret = virFileWrapperFdClose(fd); ret = virFileWrapperFdClose(fd);
virObjectLock(vm); virObjectLock(vm);
if (!virDomainObjIsActive(vm)) { if (!virDomainObjIsActive(vm)) {
if (!virGetLastError()) if (virGetLastErrorCode() == VIR_ERR_OK)
virReportError(VIR_ERR_OPERATION_FAILED, "%s", virReportError(VIR_ERR_OPERATION_FAILED, "%s",
_("domain is no longer running")); _("domain is no longer running"));
ret = -1; ret = -1;
@ -3959,7 +3959,7 @@ qemuDomainCoreDumpWithFormat(virDomainPtr dom,
event = virDomainEventLifecycleNewFromObj(vm, event = virDomainEventLifecycleNewFromObj(vm,
VIR_DOMAIN_EVENT_SUSPENDED, VIR_DOMAIN_EVENT_SUSPENDED,
VIR_DOMAIN_EVENT_SUSPENDED_API_ERROR); VIR_DOMAIN_EVENT_SUSPENDED_API_ERROR);
if (virGetLastError() == NULL) if (virGetLastErrorCode() == VIR_ERR_OK)
virReportError(VIR_ERR_OPERATION_FAILED, virReportError(VIR_ERR_OPERATION_FAILED,
"%s", _("resuming after dump failed")); "%s", _("resuming after dump failed"));
} }
@ -6629,7 +6629,7 @@ qemuDomainSaveImageStartVM(virConnectPtr conn,
if (qemuProcessStartCPUs(driver, vm, if (qemuProcessStartCPUs(driver, vm,
VIR_DOMAIN_RUNNING_RESTORED, VIR_DOMAIN_RUNNING_RESTORED,
asyncJob) < 0) { asyncJob) < 0) {
if (virGetLastError() == NULL) if (virGetLastErrorCode() == VIR_ERR_OK)
virReportError(VIR_ERR_OPERATION_FAILED, virReportError(VIR_ERR_OPERATION_FAILED,
"%s", _("failed to resume domain")); "%s", _("failed to resume domain"));
goto cleanup; goto cleanup;
@ -14326,7 +14326,7 @@ qemuDomainSnapshotCreateActiveInternal(virQEMUDriverPtr driver,
event = virDomainEventLifecycleNewFromObj(vm, event = virDomainEventLifecycleNewFromObj(vm,
VIR_DOMAIN_EVENT_SUSPENDED, VIR_DOMAIN_EVENT_SUSPENDED,
VIR_DOMAIN_EVENT_SUSPENDED_API_ERROR); VIR_DOMAIN_EVENT_SUSPENDED_API_ERROR);
if (virGetLastError() == NULL) { if (virGetLastErrorCode() == VIR_ERR_OK) {
virReportError(VIR_ERR_OPERATION_FAILED, "%s", virReportError(VIR_ERR_OPERATION_FAILED, "%s",
_("resuming after snapshot failed")); _("resuming after snapshot failed"));
} }
@ -15290,7 +15290,7 @@ qemuDomainSnapshotCreateActiveExternal(virQEMUDriverPtr driver,
VIR_DOMAIN_EVENT_SUSPENDED, VIR_DOMAIN_EVENT_SUSPENDED,
VIR_DOMAIN_EVENT_SUSPENDED_API_ERROR); VIR_DOMAIN_EVENT_SUSPENDED_API_ERROR);
qemuDomainEventQueue(driver, event); qemuDomainEventQueue(driver, event);
if (virGetLastError() == NULL) { if (virGetLastErrorCode() == VIR_ERR_OK) {
virReportError(VIR_ERR_OPERATION_FAILED, "%s", virReportError(VIR_ERR_OPERATION_FAILED, "%s",
_("resuming after snapshot failed")); _("resuming after snapshot failed"));
} }

View File

@ -173,7 +173,7 @@ qemuHotplugWaitForTrayEject(virQEMUDriverPtr driver,
if (rc > 0) { if (rc > 0) {
/* the caller called qemuMonitorEjectMedia which usually reports an /* the caller called qemuMonitorEjectMedia which usually reports an
* error. Report the failure in an off-chance that it didn't. */ * error. Report the failure in an off-chance that it didn't. */
if (!virGetLastError()) { if (virGetLastErrorCode() == VIR_ERR_OK) {
virReportError(VIR_ERR_INTERNAL_ERROR, "%s", virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
_("timed out waiting for disk tray status update")); _("timed out waiting for disk tray status update"));
} }

View File

@ -5057,7 +5057,7 @@ qemuMigrationDstFinish(virQEMUDriverPtr driver,
inPostCopy ? VIR_DOMAIN_RUNNING_POSTCOPY inPostCopy ? VIR_DOMAIN_RUNNING_POSTCOPY
: VIR_DOMAIN_RUNNING_MIGRATED, : VIR_DOMAIN_RUNNING_MIGRATED,
QEMU_ASYNC_JOB_MIGRATION_IN) < 0) { QEMU_ASYNC_JOB_MIGRATION_IN) < 0) {
if (virGetLastError() == NULL) if (virGetLastErrorCode() == VIR_ERR_OK)
virReportError(VIR_ERR_INTERNAL_ERROR, virReportError(VIR_ERR_INTERNAL_ERROR,
"%s", _("resume operation failed")); "%s", _("resume operation failed"));
/* Need to save the current error, in case shutting /* Need to save the current error, in case shutting
@ -5196,7 +5196,7 @@ qemuMigrationDstFinish(virQEMUDriverPtr driver,
/* Set a special error if Finish is expected to return NULL as a result of /* Set a special error if Finish is expected to return NULL as a result of
* successful call with retcode != 0 * successful call with retcode != 0
*/ */
if (retcode != 0 && !dom && !virGetLastError()) if (retcode != 0 && !dom && virGetLastErrorCode() == VIR_ERR_OK)
virReportError(VIR_ERR_MIGRATE_FINISH_OK, NULL); virReportError(VIR_ERR_MIGRATE_FINISH_OK, NULL);
return dom; return dom;
} }

View File

@ -748,8 +748,7 @@ qemuMonitorIO(int watch, int fd, int events, void *opaque)
/* Already have an error, so clear any new error */ /* Already have an error, so clear any new error */
virResetLastError(); virResetLastError();
} else { } else {
virErrorPtr err = virGetLastError(); if (virGetLastErrorCode() == VIR_ERR_OK)
if (!err)
virReportError(VIR_ERR_INTERNAL_ERROR, "%s", virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
_("Error while processing monitor IO")); _("Error while processing monitor IO"));
virCopyLastError(&mon->lastError); virCopyLastError(&mon->lastError);
@ -1029,7 +1028,7 @@ qemuMonitorClose(qemuMonitorPtr mon)
/* Propagate existing monitor error in case the current thread has no /* Propagate existing monitor error in case the current thread has no
* error set. * error set.
*/ */
if (mon->lastError.code != VIR_ERR_OK && !virGetLastError()) if (mon->lastError.code != VIR_ERR_OK && virGetLastErrorCode() == VIR_ERR_OK)
virSetError(&mon->lastError); virSetError(&mon->lastError);
virObjectUnlock(mon); virObjectUnlock(mon);

View File

@ -4352,7 +4352,7 @@ qemuMonitorJSONDiskNameLookup(qemuMonitorPtr mon,
} }
/* Guarantee an error when returning NULL, but don't override a /* Guarantee an error when returning NULL, but don't override a
* more specific error if one was already generated. */ * more specific error if one was already generated. */
if (!ret && !virGetLastError()) if (!ret && virGetLastErrorCode() == VIR_ERR_OK)
virReportError(VIR_ERR_INTERNAL_ERROR, virReportError(VIR_ERR_INTERNAL_ERROR,
_("unable to find backing name for device %s"), _("unable to find backing name for device %s"),
device); device);

View File

@ -466,7 +466,7 @@ qemuProcessFakeReboot(void *opaque)
if (qemuProcessStartCPUs(driver, vm, if (qemuProcessStartCPUs(driver, vm,
reason, reason,
QEMU_ASYNC_JOB_NONE) < 0) { QEMU_ASYNC_JOB_NONE) < 0) {
if (virGetLastError() == NULL) if (virGetLastErrorCode() == VIR_ERR_OK)
virReportError(VIR_ERR_INTERNAL_ERROR, virReportError(VIR_ERR_INTERNAL_ERROR,
"%s", _("resume operation failed")); "%s", _("resume operation failed"));
goto endjob; goto endjob;
@ -6411,7 +6411,7 @@ qemuProcessFinishStartup(virQEMUDriverPtr driver,
if (qemuProcessStartCPUs(driver, vm, if (qemuProcessStartCPUs(driver, vm,
VIR_DOMAIN_RUNNING_BOOTED, VIR_DOMAIN_RUNNING_BOOTED,
asyncJob) < 0) { asyncJob) < 0) {
if (!virGetLastError()) if (virGetLastErrorCode() == VIR_ERR_OK)
virReportError(VIR_ERR_OPERATION_FAILED, "%s", virReportError(VIR_ERR_OPERATION_FAILED, "%s",
_("resume operation failed")); _("resume operation failed"));
goto cleanup; goto cleanup;

View File

@ -3672,8 +3672,7 @@ remoteAuthenticate(virConnectPtr conn, struct private_data *priv,
(xdrproc_t) xdr_void, (char *) NULL, (xdrproc_t) xdr_void, (char *) NULL,
(xdrproc_t) xdr_remote_auth_list_ret, (char *) &ret); (xdrproc_t) xdr_remote_auth_list_ret, (char *) &ret);
if (err < 0) { if (err < 0) {
virErrorPtr verr = virGetLastError(); if (virGetLastErrorCode() == VIR_ERR_NO_SUPPORT) {
if (verr && verr->code == VIR_ERR_NO_SUPPORT) {
/* Missing RPC - old server - ignore */ /* Missing RPC - old server - ignore */
virResetLastError(); virResetLastError();
return 0; return 0;

View File

@ -1958,7 +1958,7 @@ static int virNetClientIO(virNetClientPtr client,
virNetClientIOUpdateCallback(client, true); virNetClientIOUpdateCallback(client, true);
if (rv == 0 && if (rv == 0 &&
virGetLastError()) virGetLastErrorCode())
rv = -1; rv = -1;
cleanup: cleanup:

View File

@ -499,9 +499,7 @@ virNetLibsshImportPrivkey(virNetLibsshSessionPtr sess,
err = SSH_AUTH_ERROR; err = SSH_AUTH_ERROR;
goto error; goto error;
} else if (ret == SSH_ERROR) { } else if (ret == SSH_ERROR) {
virErrorPtr vir_err = virGetLastError(); if (virGetLastErrorCode() == VIR_ERR_OK) {
if (!vir_err || !vir_err->code) {
virReportError(VIR_ERR_AUTH_FAILED, virReportError(VIR_ERR_AUTH_FAILED,
_("error while opening private key '%s', wrong " _("error while opening private key '%s', wrong "
"passphrase?"), "passphrase?"),

View File

@ -123,8 +123,7 @@ virModuleLoad(const char *path,
if ((*regsym)() < 0) { if ((*regsym)() < 0) {
/* regsym() should report an error itself, but lets /* regsym() should report an error itself, but lets
* just make sure */ * just make sure */
virErrorPtr err = virGetLastError(); if (virGetLastErrorCode() == VIR_ERR_OK) {
if (err == NULL) {
virReportError(VIR_ERR_INTERNAL_ERROR, virReportError(VIR_ERR_INTERNAL_ERROR,
_("Failed to execute symbol '%s' in module '%s'"), _("Failed to execute symbol '%s' in module '%s'"),
regfunc, path); regfunc, path);

View File

@ -708,7 +708,7 @@ catchXMLError(void *ctx, const char *msg ATTRIBUTE_UNUSED, ...)
/* conditions for error printing */ /* conditions for error printing */
if (!ctxt || if (!ctxt ||
(virGetLastError() != NULL) || (virGetLastErrorCode()) ||
ctxt->input == NULL || ctxt->input == NULL ||
ctxt->lastError.level != XML_ERR_FATAL || ctxt->lastError.level != XML_ERR_FATAL ||
ctxt->lastError.message == NULL) ctxt->lastError.message == NULL)
@ -845,7 +845,7 @@ virXMLParseHelper(int domcode,
xmlFreeDoc(xml); xmlFreeDoc(xml);
xml = NULL; xml = NULL;
if (virGetLastError() == NULL) { if (virGetLastErrorCode() == VIR_ERR_OK) {
virGenericReportError(domcode, VIR_ERR_XML_ERROR, virGenericReportError(domcode, VIR_ERR_XML_ERROR,
"%s", _("failed to parse xml document")); "%s", _("failed to parse xml document"));
} }

View File

@ -127,7 +127,7 @@ static int test0(const void *unused ATTRIBUTE_UNUSED)
if (virCommandRun(cmd, NULL) == 0) if (virCommandRun(cmd, NULL) == 0)
goto cleanup; goto cleanup;
if (virGetLastError() == NULL) if (virGetLastErrorCode() == VIR_ERR_OK)
goto cleanup; goto cleanup;
virResetLastError(); virResetLastError();

View File

@ -178,8 +178,7 @@ virTestRun(const char *title,
virResetLastError(); virResetLastError();
ret = body(data); ret = body(data);
virErrorPtr err = virGetLastError(); if (virGetLastErrorCode()) {
if (err) {
if (virTestGetVerbose() || virTestGetDebug()) if (virTestGetVerbose() || virTestGetDebug())
virDispatchError(NULL); virDispatchError(NULL);
} }
@ -258,8 +257,7 @@ virTestRun(const char *title,
fprintf(stderr, " alloc %zu failed but no err status\n", i + 1); fprintf(stderr, " alloc %zu failed but no err status\n", i + 1);
# endif # endif
} else { } else {
virErrorPtr lerr = virGetLastError(); if (virGetLastErrorCode() == VIR_ERR_OK) {
if (!lerr) {
# if 0 # if 0
fprintf(stderr, " alloc %zu failed but no error report\n", i + 1); fprintf(stderr, " alloc %zu failed but no error report\n", i + 1);
# endif # endif

View File

@ -49,7 +49,7 @@ linuxTestCompareFiles(const char *cpuinfofile,
&nodeinfo.nodes, &nodeinfo.sockets, &nodeinfo.nodes, &nodeinfo.sockets,
&nodeinfo.cores, &nodeinfo.threads) < 0) { &nodeinfo.cores, &nodeinfo.threads) < 0) {
if (virTestGetDebug()) { if (virTestGetDebug()) {
if (virGetLastError()) if (virGetLastErrorCode())
VIR_TEST_DEBUG("\n%s\n", virGetLastErrorMessage()); VIR_TEST_DEBUG("\n%s\n", virGetLastErrorMessage());
} }
VIR_FORCE_FCLOSE(cpuinfo); VIR_FORCE_FCLOSE(cpuinfo);

View File

@ -333,7 +333,7 @@ testStorageChain(const void *args)
goto cleanup; goto cleanup;
} }
if (data->flags & EXP_WARN) { if (data->flags & EXP_WARN) {
if (!virGetLastError()) { if (virGetLastErrorCode() == VIR_ERR_OK) {
fprintf(stderr, "call should have warned\n"); fprintf(stderr, "call should have warned\n");
goto cleanup; goto cleanup;
} }
@ -343,7 +343,7 @@ testStorageChain(const void *args)
goto cleanup; goto cleanup;
} }
} else { } else {
if (virGetLastError()) { if (virGetLastErrorCode()) {
fprintf(stderr, "call should not have warned\n"); fprintf(stderr, "call should not have warned\n");
goto cleanup; goto cleanup;
} }
@ -449,13 +449,13 @@ testStorageLookup(const void *args)
idx, NULL); idx, NULL);
if (!data->expResult) { if (!data->expResult) {
if (!virGetLastError()) { if (virGetLastErrorCode() == VIR_ERR_OK) {
fprintf(stderr, "call should have failed\n"); fprintf(stderr, "call should have failed\n");
ret = -1; ret = -1;
} }
virResetLastError(); virResetLastError();
} else { } else {
if (virGetLastError()) { if (virGetLastErrorCode()) {
fprintf(stderr, "call should not have warned\n"); fprintf(stderr, "call should not have warned\n");
ret = -1; ret = -1;
} }

View File

@ -60,7 +60,6 @@ virshGetDomainDescription(vshControl *ctl, virDomainPtr dom, bool title,
unsigned int flags) unsigned int flags)
{ {
char *desc = NULL; char *desc = NULL;
virErrorPtr err = NULL;
xmlDocPtr doc = NULL; xmlDocPtr doc = NULL;
xmlXPathContextPtr ctxt = NULL; xmlXPathContextPtr ctxt = NULL;
int type; int type;
@ -73,15 +72,15 @@ virshGetDomainDescription(vshControl *ctl, virDomainPtr dom, bool title,
if ((desc = virDomainGetMetadata(dom, type, NULL, flags))) { if ((desc = virDomainGetMetadata(dom, type, NULL, flags))) {
return desc; return desc;
} else { } else {
err = virGetLastError(); int errCode = virGetLastErrorCode();
if (err && err->code == VIR_ERR_NO_DOMAIN_METADATA) { if (errCode == VIR_ERR_NO_DOMAIN_METADATA) {
desc = vshStrdup(ctl, ""); desc = vshStrdup(ctl, "");
vshResetLibvirtError(); vshResetLibvirtError();
return desc; return desc;
} }
if (err && err->code != VIR_ERR_NO_SUPPORT) if (errCode != VIR_ERR_NO_SUPPORT)
return desc; return desc;
} }

View File

@ -91,9 +91,7 @@ virshDomainDefine(virConnectPtr conn, const char *xml, unsigned int flags)
* try again. * try again.
*/ */
if (!dom) { if (!dom) {
virErrorPtr err = virGetLastError(); if ((virGetLastErrorCode() == VIR_ERR_NO_SUPPORT) &&
if (err &&
(err->code == VIR_ERR_NO_SUPPORT) &&
(flags == VIR_DOMAIN_DEFINE_VALIDATE)) (flags == VIR_DOMAIN_DEFINE_VALIDATE))
dom = virDomainDefineXML(conn, xml); dom = virDomainDefineXML(conn, xml);
} }

View File

@ -123,8 +123,7 @@ virshDomainState(vshControl *ctl,
if (!priv->useGetInfo) { if (!priv->useGetInfo) {
int state; int state;
if (virDomainGetState(dom, &state, reason, 0) < 0) { if (virDomainGetState(dom, &state, reason, 0) < 0) {
virErrorPtr err = virGetLastError(); if (virGetLastErrorCode() == VIR_ERR_NO_SUPPORT)
if (err && err->code == VIR_ERR_NO_SUPPORT)
priv->useGetInfo = true; priv->useGetInfo = true;
else else
return -1; return -1;

View File

@ -266,7 +266,7 @@ vshSaveLibvirtHelperError(void)
if (last_error) if (last_error)
return; return;
if (!virGetLastError()) if (virGetLastErrorCode() == VIR_ERR_OK)
return; return;
vshSaveLibvirtError(); vshSaveLibvirtError();