Break some long lines, and use more efficient functions when possible, such as relying on virBufferEscapeString to skip output on a NULL arg. Ensure that output does not embed newlines, since auto-indent won't work in those situations. * src/conf/domain_conf.c (virDomainTimerDefFormat): Break output lines. (virDomainDefFormatInternal, virDomainDiskDefFormat) (virDomainActualNetDefFormat, virDomainNetDefFormat) (virDomainHostdevDefFormat): Minor cleanups. --- src/conf/domain_conf.c | 179 ++++++++++++++++++++++-------------------------- 1 files changed, 81 insertions(+), 98 deletions(-) diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c index 6ab73ec..1871974 100644 --- a/src/conf/domain_conf.c +++ b/src/conf/domain_conf.c @@ -9146,7 +9146,7 @@ virDomainDiskDefFormat(virBufferPtr buf, if (def->driverName || def->driverType || def->cachemode || def->ioeventfd || def->event_idx) { - virBufferAsprintf(buf, " <driver"); + virBufferAddLit(buf, " <driver"); if (def->driverName) virBufferAsprintf(buf, " name='%s'", def->driverName); if (def->driverType) @@ -9161,7 +9161,7 @@ virDomainDiskDefFormat(virBufferPtr buf, virBufferAsprintf(buf, " ioeventfd='%s'", ioeventfd); if (def->event_idx) virBufferAsprintf(buf, " event_idx='%s'", event_idx); - virBufferAsprintf(buf, "/>\n"); + virBufferAddLit(buf, "/>\n"); } if (def->src || def->nhosts > 0) { @@ -9185,18 +9185,18 @@ virDomainDiskDefFormat(virBufferPtr buf, virBufferEscapeString(buf, " name='%s'", def->src); } if (def->nhosts == 0) { - virBufferAsprintf(buf, "/>\n"); + virBufferAddLit(buf, "/>\n"); } else { int i; - virBufferAsprintf(buf, ">\n"); + virBufferAddLit(buf, ">\n"); for (i = 0; i < def->nhosts; i++) { virBufferEscapeString(buf, " <host name='%s'", def->hosts[i].name); virBufferEscapeString(buf, " port='%s'/>\n", def->hosts[i].port); } - virBufferAsprintf(buf, " </source>\n"); + virBufferAddLit(buf, " </source>\n"); } break; default: @@ -9218,9 +9218,7 @@ virDomainDiskDefFormat(virBufferPtr buf, virBufferAddLit(buf, " <shareable/>\n"); if (def->transient) virBufferAddLit(buf, " <transient/>\n"); - if (def->serial) - virBufferEscapeString(buf, " <serial>%s</serial>\n", - def->serial); + virBufferEscapeString(buf, " <serial>%s</serial>\n", def->serial); if (def->encryption) { virBufferAdjustIndent(buf, 6); if (virStorageEncryptionFormat(buf, def->encryption) < 0) @@ -9398,10 +9396,8 @@ virDomainActualNetDefFormat(virBufferPtr buf, switch (def->type) { case VIR_DOMAIN_NET_TYPE_BRIDGE: - if (def->data.bridge.brname) { - virBufferEscapeString(buf, " <source bridge='%s'/>\n", - def->data.bridge.brname); - } + virBufferEscapeString(buf, " <source bridge='%s'/>\n", + def->data.bridge.brname); break; case VIR_DOMAIN_NET_TYPE_DIRECT: @@ -9462,10 +9458,8 @@ virDomainNetDefFormat(virBufferPtr buf, case VIR_DOMAIN_NET_TYPE_NETWORK: virBufferEscapeString(buf, " <source network='%s'", def->data.network.name); - if (def->data.network.portgroup) { - virBufferEscapeString(buf, " portgroup='%s'", - def->data.network.portgroup); - } + virBufferEscapeString(buf, " portgroup='%s'", + def->data.network.portgroup); virBufferAddLit(buf, "/>\n"); virBufferAdjustIndent(buf, 6); virVirtualPortProfileFormat(buf, def->data.network.virtPortProfile); @@ -9476,15 +9470,13 @@ virDomainNetDefFormat(virBufferPtr buf, break; case VIR_DOMAIN_NET_TYPE_ETHERNET: - if (def->data.ethernet.dev) - virBufferEscapeString(buf, " <source dev='%s'/>\n", - def->data.ethernet.dev); + virBufferEscapeString(buf, " <source dev='%s'/>\n", + def->data.ethernet.dev); if (def->data.ethernet.ipaddr) virBufferAsprintf(buf, " <ip address='%s'/>\n", def->data.ethernet.ipaddr); - if (def->data.ethernet.script) - virBufferEscapeString(buf, " <script path='%s'/>\n", - def->data.ethernet.script); + virBufferEscapeString(buf, " <script path='%s'/>\n", + def->data.ethernet.script); break; case VIR_DOMAIN_NET_TYPE_BRIDGE: @@ -9493,9 +9485,8 @@ virDomainNetDefFormat(virBufferPtr buf, if (def->data.bridge.ipaddr) virBufferAsprintf(buf, " <ip address='%s'/>\n", def->data.bridge.ipaddr); - if (def->data.bridge.script) - virBufferEscapeString(buf, " <script path='%s'/>\n", - def->data.bridge.script); + virBufferEscapeString(buf, " <script path='%s'/>\n", + def->data.bridge.script); break; case VIR_DOMAIN_NET_TYPE_SERVER: @@ -9518,7 +9509,7 @@ virDomainNetDefFormat(virBufferPtr buf, virBufferEscapeString(buf, " <source dev='%s'", def->data.direct.linkdev); virBufferAsprintf(buf, " mode='%s'", - virMacvtapModeTypeToString(def->data.direct.mode)); + virMacvtapModeTypeToString(def->data.direct.mode)); virBufferAddLit(buf, "/>\n"); virBufferAdjustIndent(buf, 6); virVirtualPortProfileFormat(buf, def->data.direct.virtPortProfile); @@ -9575,7 +9566,8 @@ virDomainNetDefFormat(virBufferPtr buf, if (def->tune.sndbuf_specified) { virBufferAddLit(buf, " <tune>\n"); - virBufferAsprintf(buf, " <sndbuf>%lu</sndbuf>\n", def->tune.sndbuf); + virBufferAsprintf(buf, " <sndbuf>%lu</sndbuf>\n", + def->tune.sndbuf); virBufferAddLit(buf, " </tune>\n"); } @@ -9613,7 +9605,7 @@ virDomainChrSourceDefFormat(virBufferPtr buf, return -1; } - /* Compat with legacy <console tty='/dev/pts/5'/> syntax */ + /* Compat with legacy <console tty='/dev/pts/5'/> syntax */ virBufferAsprintf(buf, " type='%s'", type); if (tty_compat) { virBufferEscapeString(buf, " tty='%s'", @@ -9784,8 +9776,7 @@ virDomainChrDefFormat(virBufferPtr buf, return -1; } - virBufferAsprintf(buf, " </%s>\n", - elementName); + virBufferAsprintf(buf, " </%s>\n", elementName); return ret; } @@ -9819,9 +9810,8 @@ virDomainSmartcardDefFormat(virBufferPtr buf, for (i = 0; i < VIR_DOMAIN_SMARTCARD_NUM_CERTIFICATES; i++) virBufferEscapeString(buf, " <certificate>%s</certificate>\n", def->data.cert.file[i]); - if (def->data.cert.database) - virBufferEscapeString(buf, " <database>%s</database>\n", - def->data.cert.database); + virBufferEscapeString(buf, " <database>%s</database>\n", + def->data.cert.database); break; case VIR_DOMAIN_SMARTCARD_TYPE_PASSTHROUGH: @@ -9854,8 +9844,7 @@ virDomainSoundDefFormat(virBufferPtr buf, return -1; } - virBufferAsprintf(buf, " <sound model='%s'", - model); + virBufferAsprintf(buf, " <sound model='%s'", model); if (virDomainDeviceInfoIsSet(&def->info, flags)) { virBufferAddLit(buf, ">\n"); @@ -9883,8 +9872,7 @@ virDomainMemballoonDefFormat(virBufferPtr buf, return -1; } - virBufferAsprintf(buf, " <memballoon model='%s'", - model); + virBufferAsprintf(buf, " <memballoon model='%s'", model); if (virDomainDeviceInfoIsSet(&def->info, flags)) { virBufferAddLit(buf, ">\n"); @@ -10098,7 +10086,8 @@ virDomainTimerDefFormat(virBufferPtr buf, && (def->catchup.limit == 0)) { virBufferAddLit(buf, "/>\n"); } else { - virBufferAddLit(buf, ">\n <catchup "); + virBufferAddLit(buf, ">\n"); + virBufferAddLit(buf, " <catchup "); if (def->catchup.threshold > 0) { virBufferAsprintf(buf, " threshold='%lu'", def->catchup.threshold); } @@ -10108,7 +10097,8 @@ virDomainTimerDefFormat(virBufferPtr buf, if (def->catchup.limit > 0) { virBufferAsprintf(buf, " limit='%lu'", def->catchup.limit); } - virBufferAddLit(buf, "/>\n </timer>\n"); + virBufferAddLit(buf, "/>\n"); + virBufferAddLit(buf, " </timer>\n"); } return 0; @@ -10396,7 +10386,8 @@ virDomainHostdevDefFormat(virBufferPtr buf, def->source.subsys.u.usb.bus, def->source.subsys.u.usb.device); } else if (def->source.subsys.type == VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_PCI) { - virBufferAsprintf(buf, " <address domain='0x%.4x' bus='0x%.2x' slot='0x%.2x' function='0x%.1x'/>\n", + virBufferAsprintf(buf, " <address domain='0x%.4x' bus='0x%.2x' " + "slot='0x%.2x' function='0x%.1x'/>\n", def->source.subsys.u.pci.domain, def->source.subsys.u.pci.bus, def->source.subsys.u.pci.slot, @@ -10449,8 +10440,8 @@ virDomainRedirdevDefFormat(virBufferPtr buf, static int virDomainHubDefFormat(virBufferPtr buf, - virDomainHubDefPtr def, - unsigned int flags) + virDomainHubDefPtr def, + unsigned int flags) { const char *type = virDomainHubTypeToString(def->type); @@ -10525,9 +10516,8 @@ virDomainDefFormatInternal(virDomainDefPtr def, virUUIDFormat(uuid, uuidstr); virBufferAsprintf(buf, " <uuid>%s</uuid>\n", uuidstr); - if (def->description) - virBufferEscapeString(buf, " <description>%s</description>\n", - def->description); + virBufferEscapeString(buf, " <description>%s</description>\n", + def->description); virBufferAsprintf(buf, " <memory>%lu</memory>\n", def->mem.max_balloon); virBufferAsprintf(buf, " <currentMemory>%lu</currentMemory>\n", @@ -10535,16 +10525,16 @@ virDomainDefFormatInternal(virDomainDefPtr def, /* add blkiotune only if there are any */ if (def->blkio.weight) { - virBufferAsprintf(buf, " <blkiotune>\n"); + virBufferAddLit(buf, " <blkiotune>\n"); virBufferAsprintf(buf, " <weight>%u</weight>\n", def->blkio.weight); - virBufferAsprintf(buf, " </blkiotune>\n"); + virBufferAddLit(buf, " </blkiotune>\n"); } /* add memtune only if there are any */ if (def->mem.hard_limit || def->mem.soft_limit || def->mem.min_guarantee || def->mem.swap_hard_limit) - virBufferAsprintf(buf, " <memtune>\n"); + virBufferAddLit(buf, " <memtune>\n"); if (def->mem.hard_limit) { virBufferAsprintf(buf, " <hard_limit>%lu</hard_limit>\n", def->mem.hard_limit); @@ -10563,12 +10553,12 @@ virDomainDefFormatInternal(virDomainDefPtr def, } if (def->mem.hard_limit || def->mem.soft_limit || def->mem.min_guarantee || def->mem.swap_hard_limit) - virBufferAsprintf(buf, " </memtune>\n"); + virBufferAddLit(buf, " </memtune>\n"); if (def->mem.hugepage_backed) { - virBufferAddLit(buf, " <memoryBacking>\n"); - virBufferAddLit(buf, " <hugepages/>\n"); - virBufferAddLit(buf, " </memoryBacking>\n"); + virBufferStrcat(buf, " <memoryBacking>\n", + " <hugepages/>\n", + " </memoryBacking>\n", NULL); } for (n = 0 ; n < def->cpumasklen ; n++) @@ -10626,27 +10616,25 @@ virDomainDefFormatInternal(virDomainDefPtr def, def->cputune.period || def->cputune.quota) virBufferAddLit(buf, " </cputune>\n"); - if (def->numatune.memory.nodemask) - virBufferAddLit(buf, " <numatune>\n"); - if (def->numatune.memory.nodemask) { char *nodemask = NULL; + + virBufferAddLit(buf, " <numatune>\n"); nodemask = virDomainCpuSetFormat(def->numatune.memory.nodemask, VIR_DOMAIN_CPUMASK_LEN); if (nodemask == NULL) { - virDomainReportError(VIR_ERR_INTERNAL_ERROR, - "%s", _("failed to format nodeset for NUMA memory tuning")); + virDomainReportError(VIR_ERR_INTERNAL_ERROR, "%s", + _("failed to format nodeset for NUMA memory tuning")); goto cleanup; } virBufferAsprintf(buf, " <memory mode='%s' nodeset='%s'/>\n", - virDomainNumatuneMemModeTypeToString(def->numatune.memory.mode), + virDomainNumatuneMemModeTypeToString(def->numatune. + memory.mode), nodemask); VIR_FREE(nodemask); - } - - if (def->numatune.memory.nodemask) virBufferAddLit(buf, " </numatune>\n"); + } if (def->sysinfo) virDomainSysinfoDefFormat(buf, def->sysinfo); @@ -10654,12 +10642,12 @@ virDomainDefFormatInternal(virDomainDefPtr def, if (def->os.bootloader) { virBufferEscapeString(buf, " <bootloader>%s</bootloader>\n", def->os.bootloader); - if (def->os.bootloaderArgs) - virBufferEscapeString(buf, " <bootloader_args>%s</bootloader_args>\n", - def->os.bootloaderArgs); + virBufferEscapeString(buf, + " <bootloader_args>%s</bootloader_args>\n", + def->os.bootloaderArgs); } - virBufferAddLit(buf, " <os>\n"); + virBufferAddLit(buf, " <os>\n"); virBufferAddLit(buf, " <type"); if (def->os.arch) virBufferAsprintf(buf, " arch='%s'", def->os.arch); @@ -10676,24 +10664,18 @@ virDomainDefFormatInternal(virDomainDefPtr def, else virBufferAsprintf(buf, ">%s</type>\n", def->os.type); - if (def->os.init) - virBufferEscapeString(buf, " <init>%s</init>\n", - def->os.init); - if (def->os.loader) - virBufferEscapeString(buf, " <loader>%s</loader>\n", - def->os.loader); - if (def->os.kernel) - virBufferEscapeString(buf, " <kernel>%s</kernel>\n", - def->os.kernel); - if (def->os.initrd) - virBufferEscapeString(buf, " <initrd>%s</initrd>\n", - def->os.initrd); - if (def->os.cmdline) - virBufferEscapeString(buf, " <cmdline>%s</cmdline>\n", - def->os.cmdline); - if (def->os.root) - virBufferEscapeString(buf, " <root>%s</root>\n", - def->os.root); + virBufferEscapeString(buf, " <init>%s</init>\n", + def->os.init); + virBufferEscapeString(buf, " <loader>%s</loader>\n", + def->os.loader); + virBufferEscapeString(buf, " <kernel>%s</kernel>\n", + def->os.kernel); + virBufferEscapeString(buf, " <initrd>%s</initrd>\n", + def->os.initrd); + virBufferEscapeString(buf, " <cmdline>%s</cmdline>\n", + def->os.cmdline); + virBufferEscapeString(buf, " <root>%s</root>\n", + def->os.root); if (!def->os.bootloader) { for (n = 0 ; n < def->os.nBootDevs ; n++) { @@ -10763,7 +10745,8 @@ virDomainDefFormatInternal(virDomainDefPtr def, virDomainClockOffsetTypeToString(def->clock.offset)); switch (def->clock.offset) { case VIR_DOMAIN_CLOCK_OFFSET_VARIABLE: - virBufferAsprintf(buf, " adjustment='%lld'", def->clock.data.adjustment); + virBufferAsprintf(buf, " adjustment='%lld'", + def->clock.data.adjustment); break; case VIR_DOMAIN_CLOCK_OFFSET_TIMEZONE: virBufferEscapeString(buf, " timezone='%s'", def->clock.data.timezone); @@ -10795,9 +10778,8 @@ virDomainDefFormatInternal(virDomainDefPtr def, virBufferAddLit(buf, " <devices>\n"); - if (def->emulator) - virBufferEscapeString(buf, " <emulator>%s</emulator>\n", - def->emulator); + virBufferEscapeString(buf, " <emulator>%s</emulator>\n", + def->emulator); for (n = 0 ; n < def->ndisks ; n++) if (virDomainDiskDefFormat(buf, def->disks[n], flags) < 0) @@ -10893,10 +10875,10 @@ virDomainDefFormatInternal(virDomainDefPtr def, goto cleanup; if (def->watchdog) - virDomainWatchdogDefFormat (buf, def->watchdog, flags); + virDomainWatchdogDefFormat(buf, def->watchdog, flags); if (def->memballoon) - virDomainMemballoonDefFormat (buf, def->memballoon, flags); + virDomainMemballoonDefFormat(buf, def->memballoon, flags); virBufferAddLit(buf, " </devices>\n"); @@ -10910,18 +10892,19 @@ virDomainDefFormatInternal(virDomainDefPtr def, (flags & VIR_DOMAIN_XML_INACTIVE)) { /* This is the default for inactive xml, so nothing to output. */ } else { - virBufferAsprintf(buf, " <seclabel type='%s' model='%s' relabel='%s'>\n", + virBufferAsprintf(buf, " <seclabel type='%s' model='%s' " + "relabel='%s'>\n", sectype, def->seclabel.model, def->seclabel.norelabel ? "no" : "yes"); - if (def->seclabel.label) - virBufferEscapeString(buf, " <label>%s</label>\n", - def->seclabel.label); - if (!def->seclabel.norelabel && def->seclabel.imagelabel) - virBufferEscapeString(buf, " <imagelabel>%s</imagelabel>\n", + virBufferEscapeString(buf, " <label>%s</label>\n", + def->seclabel.label); + if (!def->seclabel.norelabel) + virBufferEscapeString(buf, + " <imagelabel>%s</imagelabel>\n", def->seclabel.imagelabel); - if (def->seclabel.baselabel && - (def->seclabel.type == VIR_DOMAIN_SECLABEL_DYNAMIC)) - virBufferEscapeString(buf, " <baselabel>%s</baselabel>\n", + if (def->seclabel.type == VIR_DOMAIN_SECLABEL_DYNAMIC) + virBufferEscapeString(buf, + " <baselabel>%s</baselabel>\n", def->seclabel.baselabel); virBufferAddLit(buf, " </seclabel>\n"); } -- 1.7.4.4 -- libvir-list mailing list libvir-list@xxxxxxxxxx https://www.redhat.com/mailman/listinfo/libvir-list