qemuBlockStorageSourceGetBackendProps: Convert boolean arguments to flags

Upcoming commit will need to add another flag for the function so
convert it to a bitwise-or'd array of flags to prevent having 4
booleans.

Signed-off-by: Peter Krempa <pkrempa@redhat.com>
Reviewed-by: Ján Tomko <jtomko@redhat.com>
This commit is contained in:
Peter Krempa 2020-06-19 16:28:12 +02:00
parent 8b0cb0e666
commit d73c5eda63
4 changed files with 42 additions and 21 deletions

View File

@ -1052,26 +1052,32 @@ qemuBlockStorageSourceGetBlockdevGetCacheProps(virStorageSourcePtr src,
/**
* qemuBlockStorageSourceGetBackendProps:
* @src: disk source
* @legacy: use legacy formatting of attributes (for -drive / old qemus)
* @onlytarget: omit any data which does not identify the image itself
* @autoreadonly: use the auto-read-only feature of qemu
* @flags: bitwise-or of qemuBlockStorageSourceBackendPropsFlags
*
* Flags:
* QEMU_BLOCK_STORAGE_SOURCE_BACKEND_PROPS_LEGACY:
* use legacy formatting of attributes (for -drive / old qemus)
* QEMU_BLOCK_STORAGE_SOURCE_BACKEND_PROPS_TARGET_ONLY:
* omit any data which does not identify the image itself
* QEMU_BLOCK_STORAGE_SOURCE_BACKEND_PROPS_AUTO_READONLY:
* use the auto-read-only feature of qemu
*
* Creates a JSON object describing the underlying storage or protocol of a
* storage source. Returns NULL on error and reports an appropriate error message.
*/
virJSONValuePtr
qemuBlockStorageSourceGetBackendProps(virStorageSourcePtr src,
bool legacy,
bool onlytarget,
bool autoreadonly)
unsigned int flags)
{
int actualType = virStorageSourceGetActualType(src);
g_autoptr(virJSONValue) fileprops = NULL;
const char *driver = NULL;
virTristateBool aro = VIR_TRISTATE_BOOL_ABSENT;
virTristateBool ro = VIR_TRISTATE_BOOL_ABSENT;
bool onlytarget = flags & QEMU_BLOCK_STORAGE_SOURCE_BACKEND_PROPS_TARGET_ONLY;
bool legacy = flags & QEMU_BLOCK_STORAGE_SOURCE_BACKEND_PROPS_LEGACY;
if (autoreadonly) {
if (flags & QEMU_BLOCK_STORAGE_SOURCE_BACKEND_PROPS_AUTO_READONLY) {
aro = VIR_TRISTATE_BOOL_YES;
} else {
if (src->readonly)
@ -1576,15 +1582,18 @@ qemuBlockStorageSourceAttachPrepareBlockdev(virStorageSourcePtr src,
bool autoreadonly)
{
g_autoptr(qemuBlockStorageSourceAttachData) data = NULL;
unsigned int backendpropsflags = 0;
if (autoreadonly)
backendpropsflags |= QEMU_BLOCK_STORAGE_SOURCE_BACKEND_PROPS_AUTO_READONLY;
if (VIR_ALLOC(data) < 0)
return NULL;
if (!(data->formatProps = qemuBlockStorageSourceGetBlockdevProps(src,
backingStore)) ||
!(data->storageProps = qemuBlockStorageSourceGetBackendProps(src, false,
false,
autoreadonly)))
!(data->storageProps = qemuBlockStorageSourceGetBackendProps(src,
backendpropsflags)))
return NULL;
data->storageNodeName = src->nodestorage;
@ -2108,7 +2117,8 @@ qemuBlockGetBackingStoreString(virStorageSourcePtr src,
}
/* use json: pseudo protocol otherwise */
if (!(backingProps = qemuBlockStorageSourceGetBackendProps(src, false, true, false)))
if (!(backingProps = qemuBlockStorageSourceGetBackendProps(src,
QEMU_BLOCK_STORAGE_SOURCE_BACKEND_PROPS_TARGET_ONLY)))
return NULL;
props = backingProps;

View File

@ -56,11 +56,15 @@ qemuBlockGetNodeData(virJSONValuePtr data);
bool
qemuBlockStorageSourceSupportsConcurrentAccess(virStorageSourcePtr src);
typedef enum {
QEMU_BLOCK_STORAGE_SOURCE_BACKEND_PROPS_LEGACY = 1 << 0,
QEMU_BLOCK_STORAGE_SOURCE_BACKEND_PROPS_TARGET_ONLY = 1 << 1,
QEMU_BLOCK_STORAGE_SOURCE_BACKEND_PROPS_AUTO_READONLY = 1 << 2,
} qemuBlockStorageSourceBackendPropsFlags;
virJSONValuePtr
qemuBlockStorageSourceGetBackendProps(virStorageSourcePtr src,
bool legacy,
bool onlytarget,
bool autoreadonly);
unsigned int flags);
virURIPtr
qemuBlockStorageSourceGetURI(virStorageSourcePtr src);

View File

@ -1193,7 +1193,8 @@ qemuDiskSourceGetProps(virStorageSourcePtr src)
g_autoptr(virJSONValue) props = NULL;
virJSONValuePtr ret;
if (!(props = qemuBlockStorageSourceGetBackendProps(src, true, false, false)))
if (!(props = qemuBlockStorageSourceGetBackendProps(src,
QEMU_BLOCK_STORAGE_SOURCE_BACKEND_PROPS_LEGACY)))
return NULL;
if (virJSONValueObjectCreate(&ret, "a:file", &props, NULL) < 0)

View File

@ -62,6 +62,10 @@ testBackingXMLjsonXML(const void *args)
g_autoptr(virStorageSource) xmlsrc = NULL;
g_autoptr(virStorageSource) jsonsrc = NULL;
g_auto(virBuffer) debug = VIR_BUFFER_INITIALIZER;
unsigned int backendpropsflags = 0;
if (data->legacy)
backendpropsflags |= QEMU_BLOCK_STORAGE_SOURCE_BACKEND_PROPS_LEGACY;
if (!(xmlsrc = virStorageSourceNew()))
return -1;
@ -77,9 +81,7 @@ testBackingXMLjsonXML(const void *args)
}
if (!(backendprops = qemuBlockStorageSourceGetBackendProps(xmlsrc,
data->legacy,
false,
false))) {
backendpropsflags))) {
fprintf(stderr, "failed to format disk source json\n");
return -1;
}
@ -159,7 +161,8 @@ testJSONtoJSON(const void *args)
return -1;
}
if (!(jsonsrcout = qemuBlockStorageSourceGetBackendProps(src, false, false, true))) {
if (!(jsonsrcout = qemuBlockStorageSourceGetBackendProps(src,
QEMU_BLOCK_STORAGE_SOURCE_BACKEND_PROPS_AUTO_READONLY))) {
fprintf(stderr, "failed to format disk source json\n");
return -1;
}
@ -290,6 +293,9 @@ testQemuDiskXMLToProps(const void *opaque)
for (n = disk->src; virStorageSourceIsBacking(n); n = n->backingStore) {
g_autofree char *backingstore = NULL;
unsigned int backendpropsflagsnormal = QEMU_BLOCK_STORAGE_SOURCE_BACKEND_PROPS_AUTO_READONLY;
unsigned int backendpropsflagstarget = QEMU_BLOCK_STORAGE_SOURCE_BACKEND_PROPS_AUTO_READONLY |
QEMU_BLOCK_STORAGE_SOURCE_BACKEND_PROPS_TARGET_ONLY;
if (testQemuDiskXMLToJSONFakeSecrets(n) < 0)
return -1;
@ -300,8 +306,8 @@ testQemuDiskXMLToProps(const void *opaque)
qemuDomainPrepareDiskSourceData(disk, n);
if (!(formatProps = qemuBlockStorageSourceGetBlockdevProps(n, n->backingStore)) ||
!(storageSrcOnlyProps = qemuBlockStorageSourceGetBackendProps(n, false, true, true)) ||
!(storageProps = qemuBlockStorageSourceGetBackendProps(n, false, false, true)) ||
!(storageSrcOnlyProps = qemuBlockStorageSourceGetBackendProps(n, backendpropsflagstarget)) ||
!(storageProps = qemuBlockStorageSourceGetBackendProps(n, backendpropsflagsnormal)) ||
!(backingstore = qemuBlockGetBackingStoreString(n, true))) {
if (!data->fail) {
VIR_TEST_VERBOSE("failed to generate qemu blockdev props");