conf: nwfilter: Refactor XML formatting in virNWFilterRuleDefFormat

Use virXMLFormatElement to simplify the formatter. Drop return value of
virNWFilterRuleDefFormat as there are no errors to report.

Signed-off-by: Peter Krempa <pkrempa@redhat.com>
Reviewed-by: Ján Tomko <jtomko@redhat.com>
This commit is contained in:
Peter Krempa 2023-02-16 10:18:46 +01:00
parent d50eb74b9d
commit 43f280cc65

View File

@ -2877,43 +2877,31 @@ virNWFilterRuleDefDetailsFormat(virBuffer *buf,
} }
static int static void
virNWFilterRuleDefFormat(virBuffer *buf, virNWFilterRuleDefFormat(virBuffer *buf,
virNWFilterRuleDef *def) virNWFilterRuleDef *def)
{ {
g_auto(virBuffer) attrBuf = VIR_BUFFER_INITIALIZER;
g_auto(virBuffer) childBuf = VIR_BUFFER_INIT_CHILD(buf);
size_t i; size_t i;
bool subelement = false;
virBufferAsprintf(buf, "<rule action='%s' direction='%s' priority='%d'", virBufferAsprintf(&attrBuf, " action='%s' direction='%s' priority='%d'",
virNWFilterRuleActionTypeToString(def->action), virNWFilterRuleActionTypeToString(def->action),
virNWFilterRuleDirectionTypeToString(def->tt), virNWFilterRuleDirectionTypeToString(def->tt),
def->priority); def->priority);
if ((def->flags & RULE_FLAG_NO_STATEMATCH)) if ((def->flags & RULE_FLAG_NO_STATEMATCH))
virBufferAddLit(buf, " statematch='false'"); virBufferAddLit(&attrBuf, " statematch='false'");
virBufferAdjustIndent(buf, 2); for (i = 0; virAttr[i].id; i++) {
i = 0; if (virAttr[i].prtclType != def->prtclType)
while (virAttr[i].id) { continue;
if (virAttr[i].prtclType == def->prtclType) {
if (!subelement) virNWFilterRuleDefDetailsFormat(&childBuf, virAttr[i].id, virAttr[i].att, def);
virBufferAddLit(buf, ">\n"); break;
virNWFilterRuleDefDetailsFormat(buf,
virAttr[i].id,
virAttr[i].att,
def);
subelement = true;
break;
}
i++;
} }
virBufferAdjustIndent(buf, -2); virXMLFormatElement(buf, "rule", &attrBuf, &childBuf);
if (subelement)
virBufferAddLit(buf, "</rule>\n");
else
virBufferAddLit(buf, "/>\n");
return 0;
} }
@ -2921,8 +2909,11 @@ static int
virNWFilterEntryFormat(virBuffer *buf, virNWFilterEntryFormat(virBuffer *buf,
virNWFilterEntry *entry) virNWFilterEntry *entry)
{ {
if (entry->rule) if (entry->rule) {
return virNWFilterRuleDefFormat(buf, entry->rule); virNWFilterRuleDefFormat(buf, entry->rule);
return 0;
}
return virNWFilterFormatParamAttributes(buf, entry->include->params, return virNWFilterFormatParamAttributes(buf, entry->include->params,
entry->include->filterref); entry->include->filterref);
} }