From d3679928b1331874929f9fbe18d5ddb92851c993 Mon Sep 17 00:00:00 2001 From: Laine Stump Date: Wed, 5 Mar 2014 17:04:15 +0200 Subject: [PATCH] conf: eliminate hardcoded indentation in nwfilter xml This file was using multiple virBuffers, inserting the contents of buf3 into buf2, then inserting the contents of buf2 into buf1, rather than the more conventional method of just passing around a single virBufferPtr and streaming everything into that single buffer. This was unnecessary, and also made it more difficult to make indentation relative, because when you insert a string into a buffer, the indentation of the buffer is only applied once at the beginning of the string, *not* each time a newline is encountered in the string. --- src/conf/nwfilter_conf.c | 94 ++++++++++++-------------------------- src/conf/nwfilter_params.c | 6 ++- 2 files changed, 32 insertions(+), 68 deletions(-) diff --git a/src/conf/nwfilter_conf.c b/src/conf/nwfilter_conf.c index 73e668fe0f..2ea4d2ea6d 100644 --- a/src/conf/nwfilter_conf.c +++ b/src/conf/nwfilter_conf.c @@ -2,7 +2,7 @@ * nwfilter_conf.c: network filter XML processing * (derived from storage_conf.c) * - * Copyright (C) 2006-2013 Red Hat, Inc. + * Copyright (C) 2006-2014 Red Hat, Inc. * Copyright (C) 2006-2008 Daniel P. Berrange * * Copyright (C) 2010-2011 IBM Corporation @@ -3216,7 +3216,7 @@ virNWFilterRuleDefDetailsFormat(virBufferPtr buf, enum virNWFilterEntryItemFlags flags = item->flags; if ((flags & NWFILTER_ENTRY_ITEM_FLAG_EXISTS)) { if (!typeShown) { - virBufferAsprintf(buf, " <%s", type); + virBufferAsprintf(buf, "<%s", type); typeShown = true; neverShown = false; } @@ -3327,95 +3327,59 @@ virNWFilterRuleDefDetailsFormat(virBufferPtr buf, if (neverShown) virBufferAsprintf(buf, - " <%s/>\n", type); + "<%s/>\n", type); err_exit: return; } -static char * -virNWFilterRuleDefFormat(virNWFilterRuleDefPtr def) +static int +virNWFilterRuleDefFormat(virBufferPtr buf, virNWFilterRuleDefPtr def) { size_t i; - virBuffer buf = VIR_BUFFER_INITIALIZER; - virBuffer buf2 = VIR_BUFFER_INITIALIZER; - char *data; + bool subelement = false; - virBufferAsprintf(&buf, " action), virNWFilterRuleDirectionTypeToString(def->tt), def->priority); if ((def->flags & RULE_FLAG_NO_STATEMATCH)) - virBufferAddLit(&buf, " statematch='false'"); + virBufferAddLit(buf, " statematch='false'"); + virBufferAdjustIndent(buf, 2); i = 0; while (virAttr[i].id) { if (virAttr[i].prtclType == def->prtclType) { - virNWFilterRuleDefDetailsFormat(&buf2, + if (!subelement) + virBufferAddLit(buf, ">\n"); + virNWFilterRuleDefDetailsFormat(buf, virAttr[i].id, virAttr[i].att, def); + subelement = true; break; } i++; } - if (virBufferError(&buf2)) - goto no_memory; - - data = virBufferContentAndReset(&buf2); - - if (data) { - virBufferAddLit(&buf, ">\n"); - virBufferAsprintf(&buf, "%s \n", data); - VIR_FREE(data); - } else - virBufferAddLit(&buf, "/>\n"); - - if (virBufferError(&buf)) - goto no_memory; - - return virBufferContentAndReset(&buf); - -no_memory: - virReportOOMError(); - virBufferFreeAndReset(&buf); - virBufferFreeAndReset(&buf2); - - return NULL; -} - - -static char * -virNWFilterIncludeDefFormat(virNWFilterIncludeDefPtr inc) -{ - virBuffer buf = VIR_BUFFER_INITIALIZER; - - virBufferAdjustIndent(&buf, 2); - if (virNWFilterFormatParamAttributes(&buf, inc->params, - inc->filterref) < 0) { - virBufferFreeAndReset(&buf); - return NULL; - } - virBufferAdjustIndent(&buf, -2); - if (virBufferError(&buf)) { - virReportOOMError(); - virBufferFreeAndReset(&buf); - return NULL; - } - - return virBufferContentAndReset(&buf); + virBufferAdjustIndent(buf, -2); + if (subelement) + virBufferAddLit(buf, "\n"); + else + virBufferAddLit(buf, "/>\n"); + return 0; } -static char * -virNWFilterEntryFormat(virNWFilterEntryPtr entry) +static int +virNWFilterEntryFormat(virBufferPtr buf, virNWFilterEntryPtr entry) { if (entry->rule) - return virNWFilterRuleDefFormat(entry->rule); - return virNWFilterIncludeDefFormat(entry->include); + return virNWFilterRuleDefFormat(buf, entry->rule); + return virNWFilterFormatParamAttributes(buf, entry->include->params, + entry->include->filterref); } @@ -3425,7 +3389,6 @@ virNWFilterDefFormat(const virNWFilterDef *def) virBuffer buf = VIR_BUFFER_INITIALIZER; char uuid[VIR_UUID_STRING_BUFLEN]; size_t i; - char *xml; virBufferAsprintf(&buf, "name, @@ -3434,18 +3397,17 @@ virNWFilterDefFormat(const virNWFilterDef *def) virBufferAsprintf(&buf, " priority='%d'", def->chainPriority); virBufferAddLit(&buf, ">\n"); + virBufferAdjustIndent(&buf, 2); virUUIDFormat(def->uuid, uuid); - virBufferAsprintf(&buf, " %s\n", uuid); + virBufferAsprintf(&buf, "%s\n", uuid); for (i = 0; i < def->nentries; i++) { - xml = virNWFilterEntryFormat(def->filterEntries[i]); - if (!xml) + if (virNWFilterEntryFormat(&buf, def->filterEntries[i]) < 0) goto err_exit; - virBufferAdd(&buf, xml, -1); - VIR_FREE(xml); } + virBufferAdjustIndent(&buf, -2); virBufferAddLit(&buf, "\n"); if (virBufferError(&buf)) diff --git a/src/conf/nwfilter_params.c b/src/conf/nwfilter_params.c index 2379aa7f0c..a78c407849 100644 --- a/src/conf/nwfilter_params.c +++ b/src/conf/nwfilter_params.c @@ -1,7 +1,7 @@ /* * nwfilter_params.c: parsing and data maintenance of filter parameters * - * Copyright (C) 2011-2013 Red Hat, Inc. + * Copyright (C) 2011-2014 Red Hat, Inc. * Copyright (C) 2010 IBM Corporation * * This library is free software; you can redistribute it and/or @@ -901,6 +901,7 @@ virNWFilterFormatParamAttributes(virBufferPtr buf, virBufferAsprintf(buf, "\n"); + virBufferAdjustIndent(buf, 2); for (i = 0; i < numKeys; i++) { const virNWFilterVarValue *value = items[i].value; @@ -908,11 +909,12 @@ virNWFilterFormatParamAttributes(virBufferPtr buf, for (j = 0; j < card; j++) virBufferAsprintf(buf, - " \n", + "\n", (const char *)items[i].key, virNWFilterVarValueGetNthValue(value, j)); } + virBufferAdjustIndent(buf, -2); virBufferAddLit(buf, "\n"); } else { virBufferAddLit(buf, "/>\n"); -- 2.39.5