From d96a414c03e18a7f10b93a02f97113bccf9c3794 Mon Sep 17 00:00:00 2001 From: Michal Privoznik Date: Fri, 9 Feb 2024 10:20:50 +0100 Subject: [PATCH] secret_conf: Modernize XML parsing & formatting MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Our virSecret XML is still parsed and formatted using old way (e.g. virXPathString() + virXXXTypeFromString() combo, or formatting elements using plain virBufferAsprintf() instead of virXMLFormatElement()). Modernize the code as it'll make it easier for future expansion. Signed-off-by: Michal Privoznik Reviewed-by: Ján Tomko --- src/conf/secret_conf.c | 47 ++++++++++++++++++------------------------ 1 file changed, 20 insertions(+), 27 deletions(-) diff --git a/src/conf/secret_conf.c b/src/conf/secret_conf.c index c6c8c30ba9..966536599e 100644 --- a/src/conf/secret_conf.c +++ b/src/conf/secret_conf.c @@ -195,39 +195,34 @@ static int virSecretDefFormatUsage(virBuffer *buf, const virSecretDef *def) { - const char *type; + g_auto(virBuffer) attrBuf = VIR_BUFFER_INITIALIZER; + g_auto(virBuffer) childBuf = VIR_BUFFER_INIT_CHILD(buf); + + virBufferAsprintf(&attrBuf, " type='%s'", + virSecretUsageTypeToString(def->usage_type)); - type = virSecretUsageTypeToString(def->usage_type); - if (type == NULL) { - virReportError(VIR_ERR_INTERNAL_ERROR, - _("unexpected secret usage type %1$d"), - def->usage_type); - return -1; - } - virBufferAsprintf(buf, "\n", type); - virBufferAdjustIndent(buf, 2); switch (def->usage_type) { case VIR_SECRET_USAGE_TYPE_NONE: break; case VIR_SECRET_USAGE_TYPE_VOLUME: - virBufferEscapeString(buf, "%s\n", def->usage_id); + virBufferEscapeString(&childBuf, "%s\n", def->usage_id); break; case VIR_SECRET_USAGE_TYPE_CEPH: - virBufferEscapeString(buf, "%s\n", def->usage_id); + virBufferEscapeString(&childBuf, "%s\n", def->usage_id); break; case VIR_SECRET_USAGE_TYPE_ISCSI: - virBufferEscapeString(buf, "%s\n", def->usage_id); + virBufferEscapeString(&childBuf, "%s\n", def->usage_id); break; case VIR_SECRET_USAGE_TYPE_TLS: - virBufferEscapeString(buf, "%s\n", def->usage_id); + virBufferEscapeString(&childBuf, "%s\n", def->usage_id); break; case VIR_SECRET_USAGE_TYPE_VTPM: - virBufferEscapeString(buf, "%s\n", def->usage_id); + virBufferEscapeString(&childBuf, "%s\n", def->usage_id); break; default: @@ -235,8 +230,8 @@ virSecretDefFormatUsage(virBuffer *buf, virReportEnumRangeError(virSecretUsageType, def->usage_type); return -1; } - virBufferAdjustIndent(buf, -2); - virBufferAddLit(buf, "\n"); + + virXMLFormatElement(buf, "usage", &attrBuf, &childBuf); return 0; } @@ -245,25 +240,23 @@ char * virSecretDefFormat(const virSecretDef *def) { g_auto(virBuffer) buf = VIR_BUFFER_INITIALIZER; - const unsigned char *uuid; + g_auto(virBuffer) attrBuf = VIR_BUFFER_INITIALIZER; + g_auto(virBuffer) childBuf = VIR_BUFFER_INIT_CHILD(&buf); char uuidstr[VIR_UUID_STRING_BUFLEN]; - virBufferAsprintf(&buf, "\n", + virBufferAsprintf(&attrBuf, " ephemeral='%s' private='%s'", def->isephemeral ? "yes" : "no", def->isprivate ? "yes" : "no"); - uuid = def->uuid; - virUUIDFormat(uuid, uuidstr); - virBufferAdjustIndent(&buf, 2); - virBufferEscapeString(&buf, "%s\n", uuidstr); + virUUIDFormat(def->uuid, uuidstr); + virBufferEscapeString(&childBuf, "%s\n", uuidstr); if (def->description != NULL) - virBufferEscapeString(&buf, "%s\n", + virBufferEscapeString(&childBuf, "%s\n", def->description); if (def->usage_type != VIR_SECRET_USAGE_TYPE_NONE && - virSecretDefFormatUsage(&buf, def) < 0) + virSecretDefFormatUsage(&childBuf, def) < 0) return NULL; - virBufferAdjustIndent(&buf, -2); - virBufferAddLit(&buf, "\n"); + virXMLFormatElement(&buf, "secret", &attrBuf, &childBuf); return virBufferContentAndReset(&buf); }