util: refactor storage file checks to allow error reporting

The virStorageFileSupportsSecurityDriver and
virStorageFileSupportsAccess currently just return a boolean
value. This is ok because they don't have any failure scenarios
but a subsequent patch is going to introduce potential failure
scenario. This changes their return type from a boolean to an
int with values -1, 0, 1.

Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>
This commit is contained in:
Daniel P. Berrangé 2018-04-25 14:09:24 +01:00
parent c444505c99
commit 1421e7168c
4 changed files with 62 additions and 33 deletions

View File

@ -7514,9 +7514,13 @@ qemuDomainDetermineDiskChain(virQEMUDriverPtr driver,
/* skip to the end of the chain if there is any */ /* skip to the end of the chain if there is any */
while (virStorageSourceHasBacking(src)) { while (virStorageSourceHasBacking(src)) {
if (report_broken && if (report_broken) {
virStorageFileSupportsAccess(src)) { int rv = virStorageFileSupportsAccess(src);
if (rv < 0)
goto cleanup;
if (rv > 0) {
if (qemuDomainStorageFileInit(driver, vm, src, disk->src) < 0) if (qemuDomainStorageFileInit(driver, vm, src, disk->src) < 0)
goto cleanup; goto cleanup;
@ -7528,6 +7532,7 @@ qemuDomainDetermineDiskChain(virQEMUDriverPtr driver,
virStorageFileDeinit(src); virStorageFileDeinit(src);
} }
}
src = src->backingStore; src = src->backingStore;
} }

View File

@ -308,9 +308,11 @@ qemuSecurityChownCallback(const virStorageSource *src,
struct stat sb; struct stat sb;
int save_errno = 0; int save_errno = 0;
int ret = -1; int ret = -1;
int rv;
if (!virStorageFileSupportsSecurityDriver(src)) rv = virStorageFileSupportsSecurityDriver(src);
return 0; if (rv <= 0)
return rv;
if (virStorageSourceIsLocalStorage(src)) { if (virStorageSourceIsLocalStorage(src)) {
/* use direct chmod for local files so that the file doesn't /* use direct chmod for local files so that the file doesn't

View File

@ -4098,34 +4098,46 @@ virStorageFileIsInitialized(const virStorageSource *src)
} }
static virStorageFileBackendPtr static int
virStorageFileGetBackendForSupportCheck(const virStorageSource *src) virStorageFileGetBackendForSupportCheck(const virStorageSource *src,
virStorageFileBackendPtr *backend)
{ {
int actualType; int actualType;
if (!src)
return NULL;
if (src->drv) if (!src) {
return src->drv->backend; *backend = NULL;
return 0;
}
if (src->drv) {
*backend = src->drv->backend;
return 0;
}
actualType = virStorageSourceGetActualType(src); actualType = virStorageSourceGetActualType(src);
return virStorageFileBackendForTypeInternal(actualType, src->protocol, false); *backend = virStorageFileBackendForTypeInternal(actualType, src->protocol, false);
return 0;
} }
static bool static int
virStorageFileSupportsBackingChainTraversal(virStorageSourcePtr src) virStorageFileSupportsBackingChainTraversal(virStorageSourcePtr src)
{ {
virStorageFileBackendPtr backend; virStorageFileBackendPtr backend;
int rv;
if (!(backend = virStorageFileGetBackendForSupportCheck(src))) rv = virStorageFileGetBackendForSupportCheck(src, &backend);
return false; if (rv < 0)
return -1;
if (!backend)
return 0;
return backend->storageFileGetUniqueIdentifier && return backend->storageFileGetUniqueIdentifier &&
backend->storageFileRead && backend->storageFileRead &&
backend->storageFileAccess; backend->storageFileAccess ? 1 : 0;
} }
@ -4137,15 +4149,19 @@ virStorageFileSupportsBackingChainTraversal(virStorageSourcePtr src)
* Check if a storage file supports operations needed by the security * Check if a storage file supports operations needed by the security
* driver to perform labelling * driver to perform labelling
*/ */
bool int
virStorageFileSupportsSecurityDriver(const virStorageSource *src) virStorageFileSupportsSecurityDriver(const virStorageSource *src)
{ {
virStorageFileBackendPtr backend; virStorageFileBackendPtr backend;
int rv;
if (!(backend = virStorageFileGetBackendForSupportCheck(src))) rv = virStorageFileGetBackendForSupportCheck(src, &backend);
return false; if (rv < 0)
return -1;
if (backend == NULL)
return 0;
return !!backend->storageFileChown; return backend->storageFileChown ? 1 : 0;
} }
@ -4157,15 +4173,19 @@ virStorageFileSupportsSecurityDriver(const virStorageSource *src)
* Check if a storage file supports checking if the storage source is accessible * Check if a storage file supports checking if the storage source is accessible
* for the given vm. * for the given vm.
*/ */
bool int
virStorageFileSupportsAccess(const virStorageSource *src) virStorageFileSupportsAccess(const virStorageSource *src)
{ {
virStorageFileBackendPtr backend; virStorageFileBackendPtr backend;
int ret;
if (!(backend = virStorageFileGetBackendForSupportCheck(src))) ret = virStorageFileGetBackendForSupportCheck(src, &backend);
return false; if (ret < 0)
return -1;
if (backend == NULL)
return 0;
return !!backend->storageFileAccess; return backend->storageFileAccess ? 1 : 0;
} }
@ -4514,14 +4534,16 @@ virStorageFileGetMetadataRecurse(virStorageSourcePtr src,
ssize_t headerLen; ssize_t headerLen;
virStorageSourcePtr backingStore = NULL; virStorageSourcePtr backingStore = NULL;
int backingFormat; int backingFormat;
int rv;
VIR_DEBUG("path=%s format=%d uid=%u gid=%u probe=%d", VIR_DEBUG("path=%s format=%d uid=%u gid=%u probe=%d",
src->path, src->format, src->path, src->format,
(unsigned int)uid, (unsigned int)gid, allow_probe); (unsigned int)uid, (unsigned int)gid, allow_probe);
/* exit if we can't load information about the current image */ /* exit if we can't load information about the current image */
if (!virStorageFileSupportsBackingChainTraversal(src)) rv = virStorageFileSupportsBackingChainTraversal(src);
return 0; if (rv <= 0)
return rv;
if (virStorageFileInitAs(src, uid, gid) < 0) if (virStorageFileInitAs(src, uid, gid) < 0)
return -1; return -1;

View File

@ -465,8 +465,8 @@ const char *virStorageFileGetUniqueIdentifier(virStorageSourcePtr src);
int virStorageFileAccess(virStorageSourcePtr src, int mode); int virStorageFileAccess(virStorageSourcePtr src, int mode);
int virStorageFileChown(const virStorageSource *src, uid_t uid, gid_t gid); int virStorageFileChown(const virStorageSource *src, uid_t uid, gid_t gid);
bool virStorageFileSupportsSecurityDriver(const virStorageSource *src); int virStorageFileSupportsSecurityDriver(const virStorageSource *src);
bool virStorageFileSupportsAccess(const virStorageSource *src); int virStorageFileSupportsAccess(const virStorageSource *src);
int virStorageFileGetMetadata(virStorageSourcePtr src, int virStorageFileGetMetadata(virStorageSourcePtr src,
uid_t uid, gid_t gid, uid_t uid, gid_t gid,