qemu: block: Extract formatting of cookie string

Introduce qemuBlockStorageSourceGetCookieString which does the
concatenation so that we can reuse it later.

Signed-off-by: Peter Krempa <pkrempa@redhat.com>
Reviewed-by: Michal Privoznik <mprivozn@redhat.com>
This commit is contained in:
Peter Krempa 2020-03-23 16:31:19 +01:00
parent 7ba2208add
commit bafd2e94fa
3 changed files with 29 additions and 12 deletions

View File

@ -3343,3 +3343,28 @@ qemuBlockStorageSourceNeedsStorageSliceLayer(const virStorageSource *src)
return false;
}
/**
* qemuBlockStorageSourceGetCookieString:
* @src: storage source
*
* Returns a properly formatted string representing cookies of @src in format
* accepted by qemu.
*/
char *
qemuBlockStorageSourceGetCookieString(virStorageSourcePtr src)
{
virBuffer buf = VIR_BUFFER_INITIALIZER;
size_t i;
for (i = 0; i < src->ncookies; i++) {
virStorageNetCookieDefPtr cookie = src->cookies[i];
virBufferAsprintf(&buf, "%s=%s; ", cookie->name, cookie->value);
}
virBufferTrim(&buf, "; ");
return virBufferContentAndReset(&buf);
}

View File

@ -258,3 +258,6 @@ qemuBlockReopenReadOnly(virDomainObjPtr vm,
bool
qemuBlockStorageSourceNeedsStorageSliceLayer(const virStorageSource *src);
char *
qemuBlockStorageSourceGetCookieString(virStorageSourcePtr src);

View File

@ -1747,18 +1747,7 @@ qemuDomainSecretStorageSourcePrepareCookies(qemuDomainObjPrivatePtr priv,
const char *aliasprotocol)
{
g_autofree char *secretalias = qemuAliasForSecret(aliasprotocol, "httpcookie");
g_autofree char *cookies = NULL;
g_auto(virBuffer) buf = VIR_BUFFER_INITIALIZER;
size_t i;
for (i = 0; i < src->ncookies; i++) {
virStorageNetCookieDefPtr cookie = src->cookies[i];
virBufferAsprintf(&buf, "%s=%s; ", cookie->name, cookie->value);
}
virBufferTrim(&buf, "; ");
cookies = virBufferContentAndReset(&buf);
g_autofree char *cookies = qemuBlockStorageSourceGetCookieString(src);
return qemuDomainSecretAESSetup(priv, secretalias, NULL,
(uint8_t *) cookies, strlen(cookies));