diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c index 557d0b9b78..ff737ecda9 100644 --- a/src/conf/domain_conf.c +++ b/src/conf/domain_conf.c @@ -9554,7 +9554,6 @@ virDomainNetDefFormat(virBufferPtr buf, unsigned int flags) { const char *type = virDomainNetTypeToString(def->type); - char *attrs; if (!type) { virDomainReportError(VIR_ERR_INTERNAL_ERROR, @@ -9675,15 +9674,11 @@ virDomainNetDefFormat(virBufferPtr buf, } } if (def->filter) { - virBufferEscapeString(buf, " filter); - attrs = virNWFilterFormatParamAttributes(def->filterparams, - " "); - if (!attrs || strlen(attrs) <= 1) - virBufferAddLit(buf, "/>\n"); - else - virBufferAsprintf(buf, ">\n%s \n", attrs); - VIR_FREE(attrs); + virBufferAdjustIndent(buf, 6); + if (virNWFilterFormatParamAttributes(buf, def->filterparams, + def->filter) < 0) + return -1; + virBufferAdjustIndent(buf, -6); } if (def->bootIndex) virBufferAsprintf(buf, " \n", def->bootIndex); diff --git a/src/conf/nwfilter_conf.c b/src/conf/nwfilter_conf.c index e0c2fb630f..289d2bb55d 100644 --- a/src/conf/nwfilter_conf.c +++ b/src/conf/nwfilter_conf.c @@ -2853,19 +2853,15 @@ no_memory: static char * virNWFilterIncludeDefFormat(virNWFilterIncludeDefPtr inc) { - char *attrs; virBuffer buf = VIR_BUFFER_INITIALIZER; - virBufferAsprintf(&buf," filterref); - - attrs = virNWFilterFormatParamAttributes(inc->params, " "); - - if (!attrs || strlen(attrs) <= 1) - virBufferAddLit(&buf, "/>\n"); - else - virBufferAsprintf(&buf, ">\n%s \n", attrs); - + virBufferAdjustIndent(&buf, 2); + if (virNWFilterFormatParamAttributes(&buf, inc->params, + inc->filterref) < 0) { + virBufferFreeAndReset(&buf); + return NULL; + } + virBufferAdjustIndent(&buf, -2); if (virBufferError(&buf)) { virReportOOMError(); virBufferFreeAndReset(&buf); diff --git a/src/conf/nwfilter_params.c b/src/conf/nwfilter_params.c index ee10b21584..871aca9b97 100644 --- a/src/conf/nwfilter_params.c +++ b/src/conf/nwfilter_params.c @@ -258,41 +258,36 @@ skip_entry: } -struct formatterParam { - virBufferPtr buf; - const char *indent; -}; - - static void _formatParameterAttrs(void *payload, const void *name, void *data) { - struct formatterParam *fp = (struct formatterParam *)data; + virBufferPtr buf = data; - virBufferAsprintf(fp->buf, "%s\n", - fp->indent, + virBufferAsprintf(buf, " \n", (const char *)name, (char *)payload); } -char * -virNWFilterFormatParamAttributes(virNWFilterHashTablePtr table, - const char *indent) +int +virNWFilterFormatParamAttributes(virBufferPtr buf, + virNWFilterHashTablePtr table, + const char *filterref) { - virBuffer buf = VIR_BUFFER_INITIALIZER; - struct formatterParam fp = { - .buf = &buf, - .indent = indent, - }; + int count = virHashSize(table->hashTable); - virHashForEach(table->hashTable, _formatParameterAttrs, &fp); - - if (virBufferError(&buf)) { - virReportOOMError(); - virBufferFreeAndReset(&buf); - return NULL; + if (count < 0) { + virNWFilterReportError(VIR_ERR_INTERNAL_ERROR, "%s", + _("missing filter parameter table")); + return -1; } - - return virBufferContentAndReset(&buf); + virBufferAsprintf(buf, "\n"); + virHashForEach(table->hashTable, _formatParameterAttrs, buf); + virBufferAddLit(buf, "\n"); + } else { + virBufferAddLit(buf, "/>\n"); + } + return 0; } diff --git a/src/conf/nwfilter_params.h b/src/conf/nwfilter_params.h index 012d6a175a..43452297bc 100644 --- a/src/conf/nwfilter_params.h +++ b/src/conf/nwfilter_params.h @@ -1,6 +1,7 @@ /* * nwfilter_params.h: parsing and data maintenance of filter parameters * + * Copyright (C) 2011 Red Hat, Inc. * Copyright (C) 2010 IBM Corporation * * This library is free software; you can redistribute it and/or @@ -23,6 +24,7 @@ # define NWFILTER_PARAMS_H # include "hash.h" +# include "buf.h" typedef struct _virNWFilterHashTable virNWFilterHashTable; typedef virNWFilterHashTable *virNWFilterHashTablePtr; @@ -35,8 +37,9 @@ struct _virNWFilterHashTable { virNWFilterHashTablePtr virNWFilterParseParamAttributes(xmlNodePtr cur); -char * virNWFilterFormatParamAttributes(virNWFilterHashTablePtr table, - const char *indent); +int virNWFilterFormatParamAttributes(virBufferPtr buf, + virNWFilterHashTablePtr table, + const char *filterref); virNWFilterHashTablePtr virNWFilterHashTableCreate(int n); void virNWFilterHashTableFree(virNWFilterHashTablePtr table);