mirror of
https://gitlab.com/libvirt/libvirt.git
synced 2024-12-22 21:55:25 +00:00
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:
parent
c444505c99
commit
1421e7168c
@ -7514,19 +7514,24 @@ qemuDomainDetermineDiskChain(virQEMUDriverPtr driver,
|
||||
|
||||
/* skip to the end of the chain if there is any */
|
||||
while (virStorageSourceHasBacking(src)) {
|
||||
if (report_broken &&
|
||||
virStorageFileSupportsAccess(src)) {
|
||||
if (report_broken) {
|
||||
int rv = virStorageFileSupportsAccess(src);
|
||||
|
||||
if (qemuDomainStorageFileInit(driver, vm, src, disk->src) < 0)
|
||||
if (rv < 0)
|
||||
goto cleanup;
|
||||
|
||||
if (virStorageFileAccess(src, F_OK) < 0) {
|
||||
virStorageFileReportBrokenChain(errno, src, disk->src);
|
||||
if (rv > 0) {
|
||||
if (qemuDomainStorageFileInit(driver, vm, src, disk->src) < 0)
|
||||
goto cleanup;
|
||||
|
||||
if (virStorageFileAccess(src, F_OK) < 0) {
|
||||
virStorageFileReportBrokenChain(errno, src, disk->src);
|
||||
virStorageFileDeinit(src);
|
||||
goto cleanup;
|
||||
}
|
||||
|
||||
virStorageFileDeinit(src);
|
||||
goto cleanup;
|
||||
}
|
||||
|
||||
virStorageFileDeinit(src);
|
||||
}
|
||||
src = src->backingStore;
|
||||
}
|
||||
|
@ -308,9 +308,11 @@ qemuSecurityChownCallback(const virStorageSource *src,
|
||||
struct stat sb;
|
||||
int save_errno = 0;
|
||||
int ret = -1;
|
||||
int rv;
|
||||
|
||||
if (!virStorageFileSupportsSecurityDriver(src))
|
||||
return 0;
|
||||
rv = virStorageFileSupportsSecurityDriver(src);
|
||||
if (rv <= 0)
|
||||
return rv;
|
||||
|
||||
if (virStorageSourceIsLocalStorage(src)) {
|
||||
/* use direct chmod for local files so that the file doesn't
|
||||
|
@ -4098,34 +4098,46 @@ virStorageFileIsInitialized(const virStorageSource *src)
|
||||
}
|
||||
|
||||
|
||||
static virStorageFileBackendPtr
|
||||
virStorageFileGetBackendForSupportCheck(const virStorageSource *src)
|
||||
static int
|
||||
virStorageFileGetBackendForSupportCheck(const virStorageSource *src,
|
||||
virStorageFileBackendPtr *backend)
|
||||
{
|
||||
int actualType;
|
||||
|
||||
if (!src)
|
||||
return NULL;
|
||||
|
||||
if (src->drv)
|
||||
return src->drv->backend;
|
||||
if (!src) {
|
||||
*backend = NULL;
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (src->drv) {
|
||||
*backend = src->drv->backend;
|
||||
return 0;
|
||||
}
|
||||
|
||||
actualType = virStorageSourceGetActualType(src);
|
||||
|
||||
return virStorageFileBackendForTypeInternal(actualType, src->protocol, false);
|
||||
*backend = virStorageFileBackendForTypeInternal(actualType, src->protocol, false);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
static bool
|
||||
static int
|
||||
virStorageFileSupportsBackingChainTraversal(virStorageSourcePtr src)
|
||||
{
|
||||
virStorageFileBackendPtr backend;
|
||||
int rv;
|
||||
|
||||
if (!(backend = virStorageFileGetBackendForSupportCheck(src)))
|
||||
return false;
|
||||
rv = virStorageFileGetBackendForSupportCheck(src, &backend);
|
||||
if (rv < 0)
|
||||
return -1;
|
||||
|
||||
if (!backend)
|
||||
return 0;
|
||||
|
||||
return backend->storageFileGetUniqueIdentifier &&
|
||||
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
|
||||
* driver to perform labelling
|
||||
*/
|
||||
bool
|
||||
int
|
||||
virStorageFileSupportsSecurityDriver(const virStorageSource *src)
|
||||
{
|
||||
virStorageFileBackendPtr backend;
|
||||
int rv;
|
||||
|
||||
if (!(backend = virStorageFileGetBackendForSupportCheck(src)))
|
||||
return false;
|
||||
rv = virStorageFileGetBackendForSupportCheck(src, &backend);
|
||||
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
|
||||
* for the given vm.
|
||||
*/
|
||||
bool
|
||||
int
|
||||
virStorageFileSupportsAccess(const virStorageSource *src)
|
||||
{
|
||||
virStorageFileBackendPtr backend;
|
||||
int ret;
|
||||
|
||||
if (!(backend = virStorageFileGetBackendForSupportCheck(src)))
|
||||
return false;
|
||||
ret = virStorageFileGetBackendForSupportCheck(src, &backend);
|
||||
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;
|
||||
virStorageSourcePtr backingStore = NULL;
|
||||
int backingFormat;
|
||||
int rv;
|
||||
|
||||
VIR_DEBUG("path=%s format=%d uid=%u gid=%u probe=%d",
|
||||
src->path, src->format,
|
||||
(unsigned int)uid, (unsigned int)gid, allow_probe);
|
||||
|
||||
/* exit if we can't load information about the current image */
|
||||
if (!virStorageFileSupportsBackingChainTraversal(src))
|
||||
return 0;
|
||||
rv = virStorageFileSupportsBackingChainTraversal(src);
|
||||
if (rv <= 0)
|
||||
return rv;
|
||||
|
||||
if (virStorageFileInitAs(src, uid, gid) < 0)
|
||||
return -1;
|
||||
|
@ -465,8 +465,8 @@ const char *virStorageFileGetUniqueIdentifier(virStorageSourcePtr src);
|
||||
int virStorageFileAccess(virStorageSourcePtr src, int mode);
|
||||
int virStorageFileChown(const virStorageSource *src, uid_t uid, gid_t gid);
|
||||
|
||||
bool virStorageFileSupportsSecurityDriver(const virStorageSource *src);
|
||||
bool virStorageFileSupportsAccess(const virStorageSource *src);
|
||||
int virStorageFileSupportsSecurityDriver(const virStorageSource *src);
|
||||
int virStorageFileSupportsAccess(const virStorageSource *src);
|
||||
|
||||
int virStorageFileGetMetadata(virStorageSourcePtr src,
|
||||
uid_t uid, gid_t gid,
|
||||
|
Loading…
Reference in New Issue
Block a user