There are many, many places which call the buffer functions without checking the return value. I have manually verified / fixed up the calls which do bother to check return values, but not added any more checks where they don't.
Rich. -- Emerging Technologies, Red Hat - http://et.redhat.com/~rjones/ Registered Address: Red Hat UK Ltd, Amberley Place, 107-111 Peascod Street, Windsor, Berkshire, SL4 1TE, United Kingdom. Registered in England and Wales under Company Registration No. 03798903
Index: lib/README =================================================================== RCS file: lib/README diff -N lib/README --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ lib/README 26 Jun 2007 14:26:56 -0000 @@ -0,0 +1,4 @@ +This subdirectory contains some useful shared code which is used +in other parts of the library and in standalone programs. + + - Richard W.M. Jones, 2007-06-26 Index: lib/buf.c =================================================================== RCS file: lib/buf.c diff -N lib/buf.c --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ lib/buf.c 26 Jun 2007 14:26:56 -0000 @@ -0,0 +1,210 @@ +/* + * buf.c: buffers for qemud + * + * Copyright (C) 2005 Red Hat, Inc. + * + * See COPYING.LIB for the License of this software + * + * Daniel Veillard <veillard@xxxxxxxxxx> + */ + +#include "libvirt/libvirt.h" + +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <stdarg.h> +#include "buf.h" + +/** + * bufferGrow: + * @buf: the buffer + * @len: the minimum free size to allocate on top of existing used space + * + * Grow the available space of a buffer to at least @len bytes. + * + * Returns the new available space or -1 in case of error + */ +static int +bufferGrow(bufferPtr buf, unsigned int len) +{ + int size; + char *newbuf; + + if (buf == NULL) + return (-1); + if (len + buf->use < buf->size) + return (0); + + size = buf->use + len + 1000; + + newbuf = (char *) realloc(buf->content, size); + if (newbuf == NULL) return -1; + buf->content = newbuf; + buf->size = size; + return (buf->size - buf->use); +} + +/** + * bufferAdd: + * @buf: the buffer to dump + * @str: the string + * @len: the number of bytes to add + * + * Add a string range to an XML buffer. if len == -1, the length of + * str is recomputed to the full string. + * + * Returns 0 successful, -1 in case of internal or API error. + */ +int +bufferAdd(bufferPtr buf, const char *str, int len) +{ + unsigned int needSize; + + if ((str == NULL) || (buf == NULL)) { + return -1; + } + if (len == 0) + return 0; + + if (len < 0) + len = strlen(str); + + needSize = buf->use + len + 2; + if (needSize > buf->size) { + if (!bufferGrow(buf, needSize - buf->use)) { + return (-1); + } + } + /* XXX: memmove() is 2x slower than memcpy(), do we really need it? */ + memmove(&buf->content[buf->use], str, len); + buf->use += len; + buf->content[buf->use] = 0; + return (0); +} + +bufferPtr +bufferNew(unsigned int size) +{ + bufferPtr buf; + + if (!(buf = malloc(sizeof(*buf)))) return NULL; + if (size && (buf->content = malloc(size))==NULL) { + free(buf); + return NULL; + } + buf->size = size; + buf->use = 0; + + return buf; +} + +void +bufferFree(bufferPtr buf) +{ + if (buf) { + if (buf->content) + free(buf->content); + free(buf); + } +} + +/** + * bufferContentAndFree: + * @buf: Buffer + * + * Return the content from the buffer and free (only) the buffer structure. + */ +char * +bufferContentAndFree (bufferPtr buf) +{ + char *content = buf->content; + + free (buf); + return content; +} + +/** + * bufferVSprintf: + * @buf: the buffer to dump + * @format: the format + * @argptr: the variable list of arguments + * + * Do a formatted print to an XML buffer. + * + * Returns 0 successful, -1 in case of internal or API error. + */ +int +bufferVSprintf(bufferPtr buf, const char *format, ...) +{ + int size, count; + va_list locarg, argptr; + + if ((format == NULL) || (buf == NULL)) { + return (-1); + } + size = buf->size - buf->use - 1; + va_start(argptr, format); + va_copy(locarg, argptr); + while (((count = vsnprintf(&buf->content[buf->use], size, format, + locarg)) < 0) || (count >= size - 1)) { + buf->content[buf->use] = 0; + va_end(locarg); + if (bufferGrow(buf, 1000) < 0) { + return (-1); + } + size = buf->size - buf->use - 1; + va_copy(locarg, argptr); + } + va_end(locarg); + buf->use += count; + buf->content[buf->use] = 0; + return (0); +} + +/** + * bufferStrcat: + * @buf: the buffer to dump + * @argptr: the variable list of strings, the last argument must be NULL + * + * Concatenate strings to an XML buffer. + * + * Returns 0 successful, -1 in case of internal or API error. + */ +int +bufferStrcat(bufferPtr buf, ...) +{ + va_list ap; + char *str; + + va_start(ap, buf); + + while ((str = va_arg(ap, char *)) != NULL) { + unsigned int len = strlen(str); + unsigned int needSize = buf->use + len + 2; + + if (needSize > buf->size) { + if (!bufferGrow(buf, needSize - buf->use)) + return -1; + } + memcpy(&buf->content[buf->use], str, len); + buf->use += len; + buf->content[buf->use] = 0; + } + va_end(ap); + return 0; +} + +/* + * vim: set tabstop=4: + * vim: set shiftwidth=4: + * vim: set expandtab: + */ +/* + * Local variables: + * indent-tabs-mode: nil + * c-indent-level: 4 + * c-basic-offset: 4 + * tab-width: 4 + * End: + */ Index: lib/buf.h =================================================================== RCS file: lib/buf.h diff -N lib/buf.h --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ lib/buf.h 26 Jun 2007 14:26:56 -0000 @@ -0,0 +1,37 @@ +/* + * buf.h: buffers for qemud + * + * Copyright (C) 2005 Red Hat, Inc. + * + * See COPYING.LIB for the License of this software + * + * Daniel Veillard <veillard@xxxxxxxxxx> + */ + +#ifndef __QEMUD_BUF_H__ +#define __QEMUD_BUF_H__ + +#include "internal.h" + +/** + * buffer: + * + * A buffer structure. + */ +typedef struct _buffer buffer; +typedef buffer *bufferPtr; +struct _buffer { + char *content; /* The buffer content UTF8 */ + unsigned int use; /* The buffer size used */ + unsigned int size; /* The buffer size */ +}; + +bufferPtr bufferNew(unsigned int size); +void bufferFree(bufferPtr buf); +char *bufferContentAndFree(bufferPtr buf); +int bufferAdd(bufferPtr buf, const char *str, int len); +int bufferVSprintf(bufferPtr buf, const char *format, ...) + ATTRIBUTE_FORMAT(printf, 2, 3); +int bufferStrcat(bufferPtr buf, ...); + +#endif /* __QEMUD_BUF_H__ */ Index: proxy/Makefile.am =================================================================== RCS file: /data/cvs/libvirt/proxy/Makefile.am,v retrieving revision 1.10 diff -u -p -r1.10 Makefile.am --- proxy/Makefile.am 29 May 2007 14:44:15 -0000 1.10 +++ proxy/Makefile.am 26 Jun 2007 14:26:56 -0000 @@ -1,7 +1,11 @@ ## Process this file with automake to produce Makefile.in -INCLUDES = -I$(top_builddir)/include -I@top_srcdir@/include \ - -I@top_srcdir@/proxy -I@top_srcdir@/src @LIBXML_CFLAGS@ \ +INCLUDES = -I$(top_builddir)/include \ + -I@top_srcdir@/include \ + -I@top_srcdir@/lib \ + -I@top_srcdir@/proxy \ + -I@top_srcdir@/src \ + @LIBXML_CFLAGS@ \ -DPROXY -DLOCALEBASEDIR=\""$(datadir)/locale"\" \ -DGETTEXT_PACKAGE=\"$(PACKAGE)\" $(WARN_CFLAGS) $(LIBVIRT_FEATURES) @@ -10,7 +14,8 @@ libexec_PROGRAMS = libvirt_proxy libvirt_proxy_SOURCES = libvirt_proxy.c @top_srcdir@/src/xend_internal.c \ @top_srcdir@/src/xen_internal.c @top_srcdir@/src/virterror.c \ @top_srcdir@/src/sexpr.c @top_srcdir@/src/xml.c \ - @top_srcdir@/src/xs_internal.c + @top_srcdir@/src/xs_internal.c \ + @top_srcdir@/lib/buf.c @top_srcdir@/lib/buf.h libvirt_proxy_LDFLAGS = $(WARN_CFLAGS) libvirt_proxy_DEPENDENCIES = libvirt_proxy_LDADD = Index: qemud/Makefile.am =================================================================== RCS file: /data/cvs/libvirt/qemud/Makefile.am,v retrieving revision 1.21 diff -u -p -r1.21 Makefile.am --- qemud/Makefile.am 11 Jun 2007 13:24:45 -0000 1.21 +++ qemud/Makefile.am 26 Jun 2007 14:26:56 -0000 @@ -13,13 +13,16 @@ libvirt_qemud_SOURCES = \ bridge.c bridge.h \ iptables.c iptables.h \ uuid.c uuid.h \ - buf.c buf.h \ + ../lib/buf.c ../lib/buf.h \ protocol.h protocol.c \ remote_protocol.h remote_protocol.c \ remote.c #-D_XOPEN_SOURCE=600 -D_XOPEN_SOURCE_EXTENDED=1 -D_POSIX_C_SOURCE=199506L libvirt_qemud_CFLAGS = \ - -I$(top_srcdir)/include -I$(top_builddir)/include $(LIBXML_CFLAGS) \ + -I$(top_srcdir)/include \ + -I$(top_builddir)/include \ + -I$(top_srcdir)/lib \ + $(LIBXML_CFLAGS) \ $(WARN_CFLAGS) -DLOCAL_STATE_DIR="\"$(localstatedir)\"" \ -DSYSCONF_DIR="\"$(sysconfdir)\"" \ -DQEMUD_PID_FILE="\"$(QEMUD_PID_FILE)\"" \ Index: qemud/buf.c =================================================================== RCS file: qemud/buf.c diff -N qemud/buf.c --- qemud/buf.c 21 Mar 2007 15:32:32 -0000 1.3 +++ /dev/null 1 Jan 1970 00:00:00 -0000 @@ -1,210 +0,0 @@ -/* - * buf.c: buffers for qemud - * - * Copyright (C) 2005 Red Hat, Inc. - * - * See COPYING.LIB for the License of this software - * - * Daniel Veillard <veillard@xxxxxxxxxx> - */ - -#include "libvirt/libvirt.h" - -#include <stdio.h> -#include <stdlib.h> -#include <string.h> -#include <stdarg.h> -#include "buf.h" - -/** - * bufferGrow: - * @buf: the buffer - * @len: the minimum free size to allocate on top of existing used space - * - * Grow the available space of a buffer to at least @len bytes. - * - * Returns the new available space or -1 in case of error - */ -static int -bufferGrow(bufferPtr buf, unsigned int len) -{ - int size; - char *newbuf; - - if (buf == NULL) - return (-1); - if (len + buf->use < buf->size) - return (0); - - size = buf->use + len + 1000; - - newbuf = (char *) realloc(buf->content, size); - if (newbuf == NULL) return -1; - buf->content = newbuf; - buf->size = size; - return (buf->size - buf->use); -} - -/** - * bufferAdd: - * @buf: the buffer to dump - * @str: the string - * @len: the number of bytes to add - * - * Add a string range to an XML buffer. if len == -1, the length of - * str is recomputed to the full string. - * - * Returns 0 successful, -1 in case of internal or API error. - */ -int -bufferAdd(bufferPtr buf, const char *str, int len) -{ - unsigned int needSize; - - if ((str == NULL) || (buf == NULL)) { - return -1; - } - if (len == 0) - return 0; - - if (len < 0) - len = strlen(str); - - needSize = buf->use + len + 2; - if (needSize > buf->size) { - if (!bufferGrow(buf, needSize - buf->use)) { - return (-1); - } - } - /* XXX: memmove() is 2x slower than memcpy(), do we really need it? */ - memmove(&buf->content[buf->use], str, len); - buf->use += len; - buf->content[buf->use] = 0; - return (0); -} - -bufferPtr -bufferNew(unsigned int size) -{ - bufferPtr buf; - - if (!(buf = malloc(sizeof(*buf)))) return NULL; - if (size && (buf->content = malloc(size))==NULL) { - free(buf); - return NULL; - } - buf->size = size; - buf->use = 0; - - return buf; -} - -void -bufferFree(bufferPtr buf) -{ - if (buf) { - if (buf->content) - free(buf->content); - free(buf); - } -} - -/** - * bufferContentAndFree: - * @buf: Buffer - * - * Return the content from the buffer and free (only) the buffer structure. - */ -char * -bufferContentAndFree (bufferPtr buf) -{ - char *content = buf->content; - - free (buf); - return content; -} - -/** - * bufferVSprintf: - * @buf: the buffer to dump - * @format: the format - * @argptr: the variable list of arguments - * - * Do a formatted print to an XML buffer. - * - * Returns 0 successful, -1 in case of internal or API error. - */ -int -bufferVSprintf(bufferPtr buf, const char *format, ...) -{ - int size, count; - va_list locarg, argptr; - - if ((format == NULL) || (buf == NULL)) { - return (-1); - } - size = buf->size - buf->use - 1; - va_start(argptr, format); - va_copy(locarg, argptr); - while (((count = vsnprintf(&buf->content[buf->use], size, format, - locarg)) < 0) || (count >= size - 1)) { - buf->content[buf->use] = 0; - va_end(locarg); - if (bufferGrow(buf, 1000) < 0) { - return (-1); - } - size = buf->size - buf->use - 1; - va_copy(locarg, argptr); - } - va_end(locarg); - buf->use += count; - buf->content[buf->use] = 0; - return (0); -} - -/** - * bufferStrcat: - * @buf: the buffer to dump - * @argptr: the variable list of strings, the last argument must be NULL - * - * Concatenate strings to an XML buffer. - * - * Returns 0 successful, -1 in case of internal or API error. - */ -int -bufferStrcat(bufferPtr buf, ...) -{ - va_list ap; - char *str; - - va_start(ap, buf); - - while ((str = va_arg(ap, char *)) != NULL) { - unsigned int len = strlen(str); - unsigned int needSize = buf->use + len + 2; - - if (needSize > buf->size) { - if (!bufferGrow(buf, needSize - buf->use)) - return -1; - } - memcpy(&buf->content[buf->use], str, len); - buf->use += len; - buf->content[buf->use] = 0; - } - va_end(ap); - return 0; -} - -/* - * vim: set tabstop=4: - * vim: set shiftwidth=4: - * vim: set expandtab: - */ -/* - * Local variables: - * indent-tabs-mode: nil - * c-indent-level: 4 - * c-basic-offset: 4 - * tab-width: 4 - * End: - */ Index: qemud/buf.h =================================================================== RCS file: qemud/buf.h diff -N qemud/buf.h --- qemud/buf.h 15 Mar 2007 17:30:04 -0000 1.1 +++ /dev/null 1 Jan 1970 00:00:00 -0000 @@ -1,37 +0,0 @@ -/* - * buf.h: buffers for qemud - * - * Copyright (C) 2005 Red Hat, Inc. - * - * See COPYING.LIB for the License of this software - * - * Daniel Veillard <veillard@xxxxxxxxxx> - */ - -#ifndef __QEMUD_BUF_H__ -#define __QEMUD_BUF_H__ - -#include "internal.h" - -/** - * buffer: - * - * A buffer structure. - */ -typedef struct _buffer buffer; -typedef buffer *bufferPtr; -struct _buffer { - char *content; /* The buffer content UTF8 */ - unsigned int use; /* The buffer size used */ - unsigned int size; /* The buffer size */ -}; - -bufferPtr bufferNew(unsigned int size); -void bufferFree(bufferPtr buf); -char *bufferContentAndFree(bufferPtr buf); -int bufferAdd(bufferPtr buf, const char *str, int len); -int bufferVSprintf(bufferPtr buf, const char *format, ...) - ATTRIBUTE_FORMAT(printf, 2, 3); -int bufferStrcat(bufferPtr buf, ...); - -#endif /* __QEMUD_BUF_H__ */ Index: src/Makefile.am =================================================================== RCS file: /data/cvs/libvirt/src/Makefile.am,v retrieving revision 1.40 diff -u -p -r1.40 Makefile.am --- src/Makefile.am 15 Jun 2007 01:21:18 -0000 1.40 +++ src/Makefile.am 26 Jun 2007 14:26:56 -0000 @@ -2,6 +2,7 @@ INCLUDES = -I$(top_builddir)/include \ -I@top_srcdir@/include \ + -I@top_srcdir@/lib \ -I@top_srcdir@/qemud \ @LIBXML_CFLAGS@ \ -DBINDIR=\""$(libexecdir)"\" \ @@ -27,6 +28,7 @@ libvirt_la_CFLAGS = $(COVERAGE_CFLAGS) CLIENT_SOURCES = \ libvirt.c internal.h \ + ../lib/buf.c ../lib/buf.h \ hash.c hash.h \ test.c test.h \ xml.c xml.h \ Index: src/conf.c =================================================================== RCS file: /data/cvs/libvirt/src/conf.c,v retrieving revision 1.9 diff -u -p -r1.9 conf.c --- src/conf.c 22 Mar 2007 18:30:57 -0000 1.9 +++ src/conf.c 26 Jun 2007 14:26:57 -0000 @@ -18,6 +18,7 @@ #include <fcntl.h> #include "internal.h" +#include "buf.h" #include "xml.h" #include "conf.h" @@ -237,7 +238,7 @@ virConfAddEntry(virConfPtr conf, char *n * Returns 0 in case of success, -1 in case of error. */ static int -virConfSaveValue(virBufferPtr buf, virConfValuePtr val) +virConfSaveValue(bufferPtr buf, virConfValuePtr val) { if (val == NULL) return(-1); @@ -245,34 +246,34 @@ virConfSaveValue(virBufferPtr buf, virCo case VIR_CONF_NONE: return(-1); case VIR_CONF_LONG: - virBufferVSprintf(buf, "%ld", val->l); + bufferVSprintf(buf, "%ld", val->l); break; case VIR_CONF_STRING: if (strchr(val->str, '\n') != NULL) { - virBufferVSprintf(buf, "\"\"\"%s\"\"\"", val->str); + bufferVSprintf(buf, "\"\"\"%s\"\"\"", val->str); } else if (strchr(val->str, '"') == NULL) { - virBufferVSprintf(buf, "\"%s\"", val->str); + bufferVSprintf(buf, "\"%s\"", val->str); } else if (strchr(val->str, '\'') == NULL) { - virBufferVSprintf(buf, "'%s'", val->str); + bufferVSprintf(buf, "'%s'", val->str); } else { - virBufferVSprintf(buf, "\"\"\"%s\"\"\"", val->str); + bufferVSprintf(buf, "\"\"\"%s\"\"\"", val->str); } break; case VIR_CONF_LIST: { virConfValuePtr cur; cur = val->list; - virBufferAdd(buf, "[ ", 2); + bufferAdd(buf, "[ ", 2); if (cur != NULL) { virConfSaveValue(buf, cur); cur = cur->next; while (cur != NULL) { - virBufferAdd(buf, ", ", 2); + bufferAdd(buf, ", ", 2); virConfSaveValue(buf, cur); cur = cur->next; } } - virBufferAdd(buf, " ]", 2); + bufferAdd(buf, " ]", 2); break; } default: @@ -291,21 +292,21 @@ virConfSaveValue(virBufferPtr buf, virCo * Returns 0 in case of success, -1 in case of error. */ static int -virConfSaveEntry(virBufferPtr buf, virConfEntryPtr cur) +virConfSaveEntry(bufferPtr buf, virConfEntryPtr cur) { if (cur->name != NULL) { - virBufferAdd(buf, cur->name, -1); - virBufferAdd(buf, " = ", 3); + bufferAdd(buf, cur->name, -1); + bufferAdd(buf, " = ", 3); virConfSaveValue(buf, cur->value); if (cur->comment != NULL) { - virBufferAdd(buf, " #", 2); - virBufferAdd(buf, cur->comment, -1); + bufferAdd(buf, " #", 2); + bufferAdd(buf, cur->comment, -1); } } else if (cur->comment != NULL) { - virBufferAdd(buf, "#", 1); - virBufferAdd(buf, cur->comment, -1); + bufferAdd(buf, "#", 1); + bufferAdd(buf, cur->comment, -1); } - virBufferAdd(buf, "\n", 1); + bufferAdd(buf, "\n", 1); return(0); } @@ -874,7 +875,7 @@ __virConfSetValue (virConfPtr conf, int __virConfWriteFile(const char *filename, virConfPtr conf) { - virBufferPtr buf; + bufferPtr buf; virConfEntryPtr cur; int ret; int fd; @@ -882,9 +883,11 @@ __virConfWriteFile(const char *filename, if (conf == NULL) return(-1); - buf = virBufferNew(500); - if (buf == NULL) + buf = bufferNew(500); + if (buf == NULL) { + virConfError(NULL, VIR_ERR_NO_MEMORY, _("failed to allocate buffer"), 0); return(-1); + } cur = conf->entries; while (cur != NULL) { @@ -907,7 +910,7 @@ __virConfWriteFile(const char *filename, goto error; } error: - virBufferFree(buf); + bufferFree(buf); return(ret); } @@ -927,16 +930,18 @@ error: int __virConfWriteMem(char *memory, int *len, virConfPtr conf) { - virBufferPtr buf; + bufferPtr buf; virConfEntryPtr cur; int ret; if ((memory == NULL) || (len == NULL) || (*len <= 0) || (conf == NULL)) return(-1); - buf = virBufferNew(500); - if (buf == NULL) + buf = bufferNew(500); + if (buf == NULL) { + virConfError(NULL, VIR_ERR_NO_MEMORY, _("failed to allocate buffer"), 0); return(-1); + } cur = conf->entries; while (cur != NULL) { @@ -953,7 +958,7 @@ __virConfWriteMem(char *memory, int *len ret = buf->use; *len = buf->use; error: - virBufferFree(buf); + bufferFree(buf); return(ret); } Index: src/sexpr.c =================================================================== RCS file: /data/cvs/libvirt/src/sexpr.c,v retrieving revision 1.6 diff -u -p -r1.6 sexpr.c --- src/sexpr.c 14 Feb 2007 15:40:54 -0000 1.6 +++ src/sexpr.c 26 Jun 2007 14:26:57 -0000 @@ -10,7 +10,7 @@ * archive for more details. */ -#define _GNU_SOURCE +#define _GNU_SOURCE /* for strndup */ #include "sexpr.h" #include "internal.h" Index: src/test.c =================================================================== RCS file: /data/cvs/libvirt/src/test.c,v retrieving revision 1.36 diff -u -p -r1.36 test.c --- src/test.c 26 Jun 2007 11:42:46 -0000 1.36 +++ src/test.c 26 Jun 2007 14:26:58 -0000 @@ -39,6 +39,7 @@ #include "internal.h" #include "test.h" #include "xml.h" +#include "buf.h" int testOpen(virConnectPtr conn, const char *name, @@ -1365,7 +1366,7 @@ int testSetVcpus(virDomainPtr domain, char * testDomainDumpXML(virDomainPtr domain, int flags ATTRIBUTE_UNUSED) { - virBufferPtr buf; + bufferPtr buf; char *xml; unsigned char *uuid; testCon *con; @@ -1382,27 +1383,28 @@ char * testDomainDumpXML(virDomainPtr do priv = (testPrivatePtr) domain->conn->privateData; con = &node->connections[priv->handle]; - if (!(buf = virBufferNew(4000))) { + if (!(buf = bufferNew(4000))) { + testError(domain->conn, domain, VIR_ERR_NO_MEMORY, __FUNCTION__); return (NULL); } - virBufferVSprintf(buf, "<domain type='test' id='%d'>\n", domain->id); - virBufferVSprintf(buf, " <name>%s</name>\n", domain->name); + bufferVSprintf(buf, "<domain type='test' id='%d'>\n", domain->id); + bufferVSprintf(buf, " <name>%s</name>\n", domain->name); uuid = domain->uuid; - virBufferVSprintf(buf, + bufferVSprintf(buf, " <uuid>%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x</uuid>\n", uuid[0], uuid[1], uuid[2], uuid[3], uuid[4], uuid[5], uuid[6], uuid[7], uuid[8], uuid[9], uuid[10], uuid[11], uuid[12], uuid[13], uuid[14], uuid[15]); - virBufferVSprintf(buf, " <memory>%lu</memory>\n", con->domains[domidx].info.maxMem); - virBufferVSprintf(buf, " <vcpu>%d</vcpu>\n", con->domains[domidx].info.nrVirtCpu); - virBufferVSprintf(buf, " <on_reboot>%s</on_reboot>\n", testRestartFlagToString(con->domains[domidx].onReboot)); - virBufferVSprintf(buf, " <on_poweroff>%s</on_poweroff>\n", testRestartFlagToString(con->domains[domidx].onPoweroff)); - virBufferVSprintf(buf, " <on_crash>%s</on_crash>\n", testRestartFlagToString(con->domains[domidx].onCrash)); + bufferVSprintf(buf, " <memory>%lu</memory>\n", con->domains[domidx].info.maxMem); + bufferVSprintf(buf, " <vcpu>%d</vcpu>\n", con->domains[domidx].info.nrVirtCpu); + bufferVSprintf(buf, " <on_reboot>%s</on_reboot>\n", testRestartFlagToString(con->domains[domidx].onReboot)); + bufferVSprintf(buf, " <on_poweroff>%s</on_poweroff>\n", testRestartFlagToString(con->domains[domidx].onPoweroff)); + bufferVSprintf(buf, " <on_crash>%s</on_crash>\n", testRestartFlagToString(con->domains[domidx].onCrash)); - virBufferAdd(buf, "</domain>\n", -1); + bufferAdd(buf, "</domain>\n", -1); xml = buf->content; free(buf); Index: src/virsh.c =================================================================== RCS file: /data/cvs/libvirt/src/virsh.c,v retrieving revision 1.89 diff -u -p -r1.89 virsh.c --- src/virsh.c 26 Jun 2007 11:42:46 -0000 1.89 +++ src/virsh.c 26 Jun 2007 14:27:01 -0000 @@ -13,8 +13,6 @@ * $Id: virsh.c,v 1.89 2007/06/26 11:42:46 rjones Exp $ */ -#define _GNU_SOURCE /* isblank() */ - #include "libvirt/libvirt.h" #include "libvirt/virterror.h" #include <stdio.h> @@ -3789,7 +3787,7 @@ vshCommandGetToken(vshControl * ctl, cha *end = NULL; - while (p && *p && isblank((unsigned char) *p)) + while (p && *p && (*p == ' ' || *p == '\t')) p++; if (p == NULL || *p == '\0') @@ -3800,7 +3798,7 @@ vshCommandGetToken(vshControl * ctl, cha } while (*p) { /* end of token is blank space or ';' */ - if ((quote == FALSE && isblank((unsigned char) *p)) || *p == ';') + if ((quote == FALSE && (*p == ' ' || *p == '\t')) || *p == ';') break; /* end of option name could be '=' */ Index: src/xen_internal.c =================================================================== RCS file: /data/cvs/libvirt/src/xen_internal.c,v retrieving revision 1.81 diff -u -p -r1.81 xen_internal.c --- src/xen_internal.c 26 Jun 2007 11:42:46 -0000 1.81 +++ src/xen_internal.c 26 Jun 2007 14:27:02 -0000 @@ -35,6 +35,7 @@ /* required for shutdown flags */ #include <xen/sched.h> +#include "buf.h" #include "xml.h" /* #define DEBUG */ @@ -2017,7 +2018,7 @@ xenHypervisorMakeCapabilitiesXML(virConn } guest_archs[32]; int nr_guest_archs = 0; - virBufferPtr xml; + bufferPtr xml; char *xml_str; memset(guest_archs, 0, sizeof(guest_archs)); @@ -2137,9 +2138,12 @@ xenHypervisorMakeCapabilitiesXML(virConn } /* Construct the final XML. */ - xml = virBufferNew (1024); - if (!xml) return NULL; - r = virBufferVSprintf (xml, + xml = bufferNew (1024); + if (!xml) { + virXenError(VIR_ERR_NO_MEMORY, __FUNCTION__, 0); + return NULL; + } + r = bufferVSprintf (xml, "\ <capabilities>\n\ <host>\n\ @@ -2150,18 +2154,18 @@ xenHypervisorMakeCapabilitiesXML(virConn if (r == -1) goto vir_buffer_failed; if (strcmp (hvm_type, "") != 0) { - r = virBufferVSprintf (xml, + r = bufferVSprintf (xml, "\ <%s/>\n", hvm_type); if (r == -1) goto vir_buffer_failed; } if (host_pae) { - r = virBufferAdd (xml, "\ + r = bufferAdd (xml, "\ <pae/>\n", -1); if (r == -1) goto vir_buffer_failed; } - r = virBufferAdd (xml, + r = bufferAdd (xml, "\ </features>\n\ </cpu>\n\ @@ -2169,7 +2173,7 @@ xenHypervisorMakeCapabilitiesXML(virConn if (r == -1) goto vir_buffer_failed; for (i = 0; i < nr_guest_archs; ++i) { - r = virBufferVSprintf (xml, + r = bufferVSprintf (xml, "\ \n\ <guest>\n\ @@ -2182,7 +2186,7 @@ xenHypervisorMakeCapabilitiesXML(virConn guest_archs[i].bits); if (r == -1) goto vir_buffer_failed; if (guest_archs[i].hvm) { - r = virBufferVSprintf (xml, + r = bufferVSprintf (xml, "\ <emulator>/usr/lib%s/xen/bin/qemu-dm</emulator>\n\ <machine>pc</machine>\n\ @@ -2191,50 +2195,48 @@ xenHypervisorMakeCapabilitiesXML(virConn guest_archs[i].bits == 64 ? "64" : ""); if (r == -1) goto vir_buffer_failed; } - r = virBufferAdd (xml, + r = bufferAdd (xml, "\ </arch>\n\ <features>\n", -1); if (r == -1) goto vir_buffer_failed; if (guest_archs[i].pae) { - r = virBufferAdd (xml, + r = bufferAdd (xml, "\ <pae/>\n", -1); if (r == -1) goto vir_buffer_failed; } if (guest_archs[i].nonpae) { - r = virBufferAdd (xml, + r = bufferAdd (xml, "\ <nonpae/>\n", -1); if (r == -1) goto vir_buffer_failed; } if (guest_archs[i].ia64_be) { - r = virBufferAdd (xml, + r = bufferAdd (xml, "\ <ia64_be/>\n", -1); if (r == -1) goto vir_buffer_failed; } - r = virBufferAdd (xml, + r = bufferAdd (xml, "\ </features>\n\ </guest>\n", -1); if (r == -1) goto vir_buffer_failed; } - r = virBufferAdd (xml, + r = bufferAdd (xml, "\ </capabilities>\n", -1); if (r == -1) goto vir_buffer_failed; xml_str = strdup (xml->content); - if (!xml_str) { - virXenError(VIR_ERR_NO_MEMORY, "strdup", 0); - goto vir_buffer_failed; - } - virBufferFree (xml); + if (!xml_str) goto vir_buffer_failed; + bufferFree (xml); return xml_str; vir_buffer_failed: - virBufferFree (xml); + virXenError(VIR_ERR_NO_MEMORY, __FUNCTION__, 0); + bufferFree (xml); return NULL; } Index: src/xend_internal.c =================================================================== RCS file: /data/cvs/libvirt/src/xend_internal.c,v retrieving revision 1.124 diff -u -p -r1.124 xend_internal.c --- src/xend_internal.c 26 Jun 2007 11:42:46 -0000 1.124 +++ src/xend_internal.c 26 Jun 2007 14:27:05 -0000 @@ -35,6 +35,7 @@ #include "internal.h" #include "sexpr.h" #include "xml.h" +#include "buf.h" #include "xen_unified.h" #include "xend_internal.h" #include "xen_internal.h" /* for DOM0_INTERFACE_VERSION */ @@ -249,7 +250,7 @@ do_connect(virConnectPtr xend) * wr_sync: * @xend: the xend connection object * @fd: the file descriptor - * @buffer: the I/O buffer + * @buf: the I/O buffer * @size: the size of the I/O * @do_read: write operation if 0, read operation otherwise * @@ -258,7 +259,7 @@ do_connect(virConnectPtr xend) * Returns the number of bytes exchanged, or -1 in case of error */ static size_t -wr_sync(virConnectPtr xend, int fd, void *buffer, size_t size, int do_read) +wr_sync(virConnectPtr xend, int fd, void *buf, size_t size, int do_read) { size_t offset = 0; @@ -266,9 +267,9 @@ wr_sync(virConnectPtr xend, int fd, void ssize_t len; if (do_read) { - len = read(fd, ((char *) buffer) + offset, size - offset); + len = read(fd, ((char *) buf) + offset, size - offset); } else { - len = write(fd, ((char *) buffer) + offset, size - offset); + len = write(fd, ((char *) buf) + offset, size - offset); } /* recoverable error, retry */ @@ -303,7 +304,7 @@ wr_sync(virConnectPtr xend, int fd, void * sread: * @xend: the xend connection object * @fd: the file descriptor - * @buffer: the I/O buffer + * @buf: the I/O buffer * @size: the size of the I/O * * Internal routine to do a synchronous read @@ -311,16 +312,16 @@ wr_sync(virConnectPtr xend, int fd, void * Returns the number of bytes read, or -1 in case of error */ static ssize_t -sread(virConnectPtr xend, int fd, void *buffer, size_t size) +sread(virConnectPtr xend, int fd, void *buf, size_t size) { - return wr_sync(xend, fd, buffer, size, 1); + return wr_sync(xend, fd, buf, size, 1); } /** * swrite: * @xend: the xend connection object * @fd: the file descriptor - * @buffer: the I/O buffer + * @buf: the I/O buffer * @size: the size of the I/O * * Internal routine to do a synchronous write @@ -328,9 +329,9 @@ sread(virConnectPtr xend, int fd, void * * Returns the number of bytes written, or -1 in case of error */ static ssize_t -swrite(virConnectPtr xend, int fd, const void *buffer, size_t size) +swrite(virConnectPtr xend, int fd, const void *buf, size_t size) { - return wr_sync(xend, fd, (void *) buffer, size, 0); + return wr_sync(xend, fd, (void *) buf, size, 0); } /** @@ -353,7 +354,7 @@ swrites(virConnectPtr xend, int fd, cons * sreads: * @xend: the xend connection object * @fd: the file descriptor - * @buffer: the I/O buffer + * @buf: the I/O buffer * @n_buffer: the size of the I/O buffer * * Internal routine to do a synchronous read of a line @@ -361,7 +362,7 @@ swrites(virConnectPtr xend, int fd, cons * Returns the number of bytes read, or -1 in case of error */ static ssize_t -sreads(virConnectPtr xend, int fd, char *buffer, size_t n_buffer) +sreads(virConnectPtr xend, int fd, char *buf, size_t n_buffer) { size_t offset; @@ -371,18 +372,18 @@ sreads(virConnectPtr xend, int fd, char for (offset = 0; offset < (n_buffer - 1); offset++) { ssize_t ret; - ret = sread(xend, fd, buffer + offset, 1); + ret = sread(xend, fd, buf + offset, 1); if (ret == 0) break; else if (ret == -1) return ret; - if (buffer[offset] == '\n') { + if (buf[offset] == '\n') { offset++; break; } } - buffer[offset] = 0; + buf[offset] = 0; return offset; } @@ -408,18 +409,18 @@ istartswith(const char *haystack, const static int xend_req(virConnectPtr xend, int fd, char *content, size_t n_content) { - char buffer[4096]; + char buf[4096]; int content_length = -1; int retcode = 0; - while (sreads(xend, fd, buffer, sizeof(buffer)) > 0) { - if (strcmp(buffer, "\r\n") == 0) + while (sreads(xend, fd, buf, sizeof(buf)) > 0) { + if (strcmp(buf, "\r\n") == 0) break; - if (istartswith(buffer, "Content-Length: ")) - content_length = atoi(buffer + 16); - else if (istartswith(buffer, "HTTP/1.1 ")) - retcode = atoi(buffer + 9); + if (istartswith(buf, "Content-Length: ")) + content_length = atoi(buf + 16); + else if (istartswith(buf, "HTTP/1.1 ")) + retcode = atoi(buf + 9); } if (content_length > -1) { @@ -499,7 +500,7 @@ static int xend_post(virConnectPtr xend, const char *path, const char *ops, char *content, size_t n_content) { - char buffer[100]; + char buf[100]; int ret; int s = do_connect(xend); @@ -515,8 +516,8 @@ xend_post(virConnectPtr xend, const char "Accept-Encoding: identity\r\n" "Content-Type: application/x-www-form-urlencoded\r\n" "Content-Length: "); - snprintf(buffer, sizeof(buffer), "%d", (int) strlen(ops)); - swrites(xend ,s, buffer); + snprintf(buf, sizeof(buf), "%d", (int) strlen(ops)); + swrites(xend ,s, buf); swrites(xend, s, "\r\n\r\n"); swrites(xend, s, ops); @@ -594,7 +595,7 @@ xend_op_ext2(virConnectPtr xend, const c size_t n_error, const char *key, va_list ap) { const char *k = key, *v; - virBuffer buf; + buffer buf; int ret; buf.content = malloc(1000); @@ -608,13 +609,13 @@ xend_op_ext2(virConnectPtr xend, const c while (k) { v = va_arg(ap, const char *); - virBufferVSprintf(&buf, "%s", k); - virBufferVSprintf(&buf, "%s", "="); - virBufferVSprintf(&buf, "%s", v); + bufferVSprintf(&buf, "%s", k); + bufferVSprintf(&buf, "%s", "="); + bufferVSprintf(&buf, "%s", v); k = va_arg(ap, const char *); if (k) - virBufferVSprintf(&buf, "%s", "&"); + bufferVSprintf(&buf, "%s", "&"); } ret = http2unix(xend, xend_post(xend, path, buf.content, error, n_error)); @@ -670,14 +671,14 @@ static int xend_op_ext(virConnectPtr xend, const char *name, char *error, size_t n_error, const char *key, ...) { - char buffer[1024]; + char buf[1024]; va_list ap; int ret; - snprintf(buffer, sizeof(buffer), "/xend/domain/%s", name); + snprintf(buf, sizeof(buf), "/xend/domain/%s", name); va_start(ap, key); - ret = xend_op_ext2(xend, buffer, error, n_error, key, ap); + ret = xend_op_ext2(xend, buf, error, n_error, key, ap); va_end(ap); return ret; @@ -699,7 +700,7 @@ xend_op_ext(virConnectPtr xend, const ch static struct sexpr * sexpr_get(virConnectPtr xend, const char *fmt, ...) { - char buffer[4096]; + char buf[4096]; char path[1024]; va_list ap; int ret; @@ -708,12 +709,12 @@ sexpr_get(virConnectPtr xend, const char vsnprintf(path, sizeof(path), fmt, ap); va_end(ap); - ret = xend_get(xend, path, buffer, sizeof(buffer)); + ret = xend_get(xend, path, buf, sizeof(buf)); ret = http2unix(xend ,ret); if (ret == -1) return NULL; - return string2sexpr(buffer); + return string2sexpr(buf); } /** @@ -810,11 +811,11 @@ static char * urlencode(const char *string) { size_t len = strlen(string); - char *buffer = malloc(len * 3 + 1); - char *ptr = buffer; + char *buf = malloc(len * 3 + 1); + char *ptr = buf; size_t i; - if (buffer == NULL) { + if (buf == NULL) { virXendError(NULL, VIR_ERR_NO_MEMORY, _("allocate new buffer")); return (NULL); } @@ -833,7 +834,7 @@ urlencode(const char *string) *ptr = 0; - return buffer; + return buf; } #endif /* ! PROXY */ @@ -1215,9 +1216,9 @@ xend_node_restart(virConnectPtr xend) * Returns 0 on success; -1 (with errno) on error */ int -xend_dmesg(virConnectPtr xend, char *buffer, size_t n_buffer) +xend_dmesg(virConnectPtr xend, char *buf, size_t n_buffer) { - return http2unix(xend, xend_get(xend, "/xend/node/dmesg", buffer, n_buffer)); + return http2unix(xend, xend_get(xend, "/xend/node/dmesg", buf, n_buffer)); } /** @@ -1238,7 +1239,7 @@ xend_dmesg_clear(virConnectPtr xend) /** * xend_log: * @xend: A xend instance - * @buffer: The buffer to hold the messages + * @buf: The buffer to hold the messages * @n_buffer: Size of buffer (including null terminator) * * This function will place the Xend debugging messages into @@ -1247,9 +1248,9 @@ xend_dmesg_clear(virConnectPtr xend) * Returns 0 on success; -1 (with errno) on error */ int -xend_log(virConnectPtr xend, char *buffer, size_t n_buffer) +xend_log(virConnectPtr xend, char *buf, size_t n_buffer) { - return http2unix(xend, xend_get(xend, "/xend/node/log", buffer, n_buffer)); + return http2unix(xend, xend_get(xend, "/xend/node/log", buf, n_buffer)); } #endif /* PROXY */ @@ -1278,7 +1279,7 @@ xend_log(virConnectPtr xend, char *buffe * Returns 0 in case of success and -1 in case of error */ static int -xend_parse_sexp_desc_os(virConnectPtr xend, struct sexpr *node, virBufferPtr buf, int hvm, int bootloader) +xend_parse_sexp_desc_os(virConnectPtr xend, struct sexpr *node, bufferPtr buf, int hvm, int bootloader) { const char *tmp; @@ -1286,9 +1287,9 @@ xend_parse_sexp_desc_os(virConnectPtr xe return(-1); } - virBufferAdd(buf, " <os>\n", 7); + bufferAdd(buf, " <os>\n", 7); if (hvm) { - virBufferVSprintf(buf, " <type>hvm</type>\n"); + bufferVSprintf(buf, " <type>hvm</type>\n"); tmp = sexpr_node(node, "domain/image/hvm/kernel"); if (tmp == NULL && !bootloader) { virXendError(xend, VIR_ERR_INTERNAL_ERROR, @@ -1296,13 +1297,13 @@ xend_parse_sexp_desc_os(virConnectPtr xe return(-1); } if (tmp) - virBufferVSprintf(buf, " <loader>%s</loader>\n", tmp); + bufferVSprintf(buf, " <loader>%s</loader>\n", tmp); tmp = sexpr_node(node, "domain/image/hvm/boot"); if ((tmp != NULL) && (tmp[0] != 0)) { while (*tmp) { if (*tmp == 'a') /* XXX no way to deal with boot from 2nd floppy */ - virBufferAdd(buf, " <boot dev='fd'/>\n", 21 ); + bufferAdd(buf, " <boot dev='fd'/>\n", 21 ); else if (*tmp == 'c') /* * Don't know what to put here. Say the vm has been given 3 @@ -1310,16 +1311,16 @@ xend_parse_sexp_desc_os(virConnectPtr xe * We're going to assume that first disk is the boot disk since * this is most common practice */ - virBufferAdd(buf, " <boot dev='hd'/>\n", 21 ); + bufferAdd(buf, " <boot dev='hd'/>\n", 21 ); else if (*tmp == 'd') - virBufferAdd(buf, " <boot dev='cdrom'/>\n", 24 ); + bufferAdd(buf, " <boot dev='cdrom'/>\n", 24 ); else if (*tmp == 'n') - virBufferAdd(buf, " <boot dev='network'/>\n", 26 ); + bufferAdd(buf, " <boot dev='network'/>\n", 26 ); tmp++; } } } else { - virBufferVSprintf(buf, " <type>linux</type>\n"); + bufferVSprintf(buf, " <type>linux</type>\n"); tmp = sexpr_node(node, "domain/image/linux/kernel"); if (tmp == NULL && !bootloader) { virXendError(xend, VIR_ERR_INTERNAL_ERROR, @@ -1327,19 +1328,19 @@ xend_parse_sexp_desc_os(virConnectPtr xe return(-1); } if (tmp) - virBufferVSprintf(buf, " <kernel>%s</kernel>\n", tmp); + bufferVSprintf(buf, " <kernel>%s</kernel>\n", tmp); tmp = sexpr_node(node, "domain/image/linux/ramdisk"); if ((tmp != NULL) && (tmp[0] != 0)) - virBufferVSprintf(buf, " <initrd>%s</initrd>\n", tmp); + bufferVSprintf(buf, " <initrd>%s</initrd>\n", tmp); tmp = sexpr_node(node, "domain/image/linux/root"); if ((tmp != NULL) && (tmp[0] != 0)) - virBufferVSprintf(buf, " <root>%s</root>\n", tmp); + bufferVSprintf(buf, " <root>%s</root>\n", tmp); tmp = sexpr_node(node, "domain/image/linux/args"); if ((tmp != NULL) && (tmp[0] != 0)) - virBufferVSprintf(buf, " <cmdline>%s</cmdline>\n", tmp); + bufferVSprintf(buf, " <cmdline>%s</cmdline>\n", tmp); } - virBufferAdd(buf, " </os>\n", 8); + bufferAdd(buf, " </os>\n", 8); return(0); } @@ -1361,7 +1362,7 @@ xend_parse_sexp_desc(virConnectPtr conn, struct sexpr *cur, *node; const char *tmp; char *tty; - virBuffer buf; + buffer buf; int hvm = 0, bootloader = 0; int domid = -1; int max_mem, cur_mem; @@ -1387,7 +1388,7 @@ xend_parse_sexp_desc(virConnectPtr conn, domid = sexpr_int(root, "domain/domid"); else domid = -1; - virBufferVSprintf(&buf, "<domain type='xen' id='%d'>\n", domid); + bufferVSprintf(&buf, "<domain type='xen' id='%d'>\n", domid); tmp = sexpr_node(root, "domain/name"); if (tmp == NULL) { @@ -1395,7 +1396,7 @@ xend_parse_sexp_desc(virConnectPtr conn, _("domain information incomplete, missing name")); goto error; } - virBufferVSprintf(&buf, " <name>%s</name>\n", tmp); + bufferVSprintf(&buf, " <name>%s</name>\n", tmp); tmp = sexpr_node(root, "domain/uuid"); if (tmp != NULL) { char compact[33]; @@ -1410,19 +1411,19 @@ xend_parse_sexp_desc(virConnectPtr conn, } compact[i] = 0; if (i > 0) - virBufferVSprintf(&buf, " <uuid>%s</uuid>\n", compact); + bufferVSprintf(&buf, " <uuid>%s</uuid>\n", compact); } tmp = sexpr_node(root, "domain/bootloader"); if (tmp != NULL) { bootloader = 1; - virBufferVSprintf(&buf, " <bootloader>%s</bootloader>\n", tmp); + bufferVSprintf(&buf, " <bootloader>%s</bootloader>\n", tmp); } tmp = sexpr_node(root, "domain/bootloader_args"); if (tmp != NULL && bootloader) { /* * Only insert bootloader_args if there is also a bootloader param */ - virBufferVSprintf(&buf, " <bootloader_args>%s</bootloader_args>\n", tmp); + bufferVSprintf(&buf, " <bootloader_args>%s</bootloader_args>\n", tmp); } if (domid != 0) { @@ -1436,39 +1437,39 @@ xend_parse_sexp_desc(virConnectPtr conn, cur_mem = (int) (sexpr_u64(root, "domain/memory") << 10); if (cur_mem > max_mem) max_mem = cur_mem; - virBufferVSprintf(&buf, " <memory>%d</memory>\n", max_mem); + bufferVSprintf(&buf, " <memory>%d</memory>\n", max_mem); if ((cur_mem >= MIN_XEN_GUEST_SIZE) && (cur_mem != max_mem)) - virBufferVSprintf(&buf, " <currentMemory>%d</currentMemory>\n", + bufferVSprintf(&buf, " <currentMemory>%d</currentMemory>\n", cur_mem); - virBufferVSprintf(&buf, " <vcpu>%d</vcpu>\n", + bufferVSprintf(&buf, " <vcpu>%d</vcpu>\n", sexpr_int(root, "domain/vcpus")); tmp = sexpr_node(root, "domain/on_poweroff"); if (tmp != NULL) - virBufferVSprintf(&buf, " <on_poweroff>%s</on_poweroff>\n", tmp); + bufferVSprintf(&buf, " <on_poweroff>%s</on_poweroff>\n", tmp); tmp = sexpr_node(root, "domain/on_reboot"); if (tmp != NULL) - virBufferVSprintf(&buf, " <on_reboot>%s</on_reboot>\n", tmp); + bufferVSprintf(&buf, " <on_reboot>%s</on_reboot>\n", tmp); tmp = sexpr_node(root, "domain/on_crash"); if (tmp != NULL) - virBufferVSprintf(&buf, " <on_crash>%s</on_crash>\n", tmp); + bufferVSprintf(&buf, " <on_crash>%s</on_crash>\n", tmp); if (hvm) { - virBufferAdd(&buf, " <features>\n", 13); + bufferAdd(&buf, " <features>\n", 13); if (sexpr_int(root, "domain/image/hvm/acpi")) - virBufferAdd(&buf, " <acpi/>\n", 12); + bufferAdd(&buf, " <acpi/>\n", 12); if (sexpr_int(root, "domain/image/hvm/apic")) - virBufferAdd(&buf, " <apic/>\n", 12); + bufferAdd(&buf, " <apic/>\n", 12); if (sexpr_int(root, "domain/image/hvm/pae")) - virBufferAdd(&buf, " <pae/>\n", 11); - virBufferAdd(&buf, " </features>\n", 14); + bufferAdd(&buf, " <pae/>\n", 11); + bufferAdd(&buf, " </features>\n", 14); } - virBufferAdd(&buf, " <devices>\n", 12); + bufferAdd(&buf, " <devices>\n", 12); /* in case of HVM we have devices emulation */ tmp = sexpr_node(root, "domain/image/hvm/device_model"); if ((tmp != NULL) && (tmp[0] != 0)) - virBufferVSprintf(&buf, " <emulator>%s</emulator>\n", tmp); + bufferVSprintf(&buf, " <emulator>%s</emulator>\n", tmp); for (cur = root; cur->kind == SEXPR_CONS; cur = cur->cdr) { node = cur->car; @@ -1588,33 +1589,33 @@ xend_parse_sexp_desc(virConnectPtr conn, } if (!isNoSrcCdrom) { - virBufferVSprintf(&buf, " <disk type='%s' device='%s'>\n", + bufferVSprintf(&buf, " <disk type='%s' device='%s'>\n", isBlock ? "block" : "file", cdrom ? "cdrom" : "disk"); if (drvType) { - virBufferVSprintf(&buf, " <driver name='%s' type='%s'/>\n", drvName, drvType); + bufferVSprintf(&buf, " <driver name='%s' type='%s'/>\n", drvName, drvType); } else { - virBufferVSprintf(&buf, " <driver name='%s'/>\n", drvName); + bufferVSprintf(&buf, " <driver name='%s'/>\n", drvName); } if (isBlock) { - virBufferVSprintf(&buf, " <source dev='%s'/>\n", src); + bufferVSprintf(&buf, " <source dev='%s'/>\n", src); } else { - virBufferVSprintf(&buf, " <source file='%s'/>\n", src); + bufferVSprintf(&buf, " <source file='%s'/>\n", src); } } else { /* This case is the cdrom device only */ - virBufferVSprintf(&buf, " <disk device='cdrom'>\n"); + bufferVSprintf(&buf, " <disk device='cdrom'>\n"); } - virBufferVSprintf(&buf, " <target dev='%s'/>\n", dst); + bufferVSprintf(&buf, " <target dev='%s'/>\n", dst); /* XXX should we force mode == r, if cdrom==1, or assume xend has already done this ? */ if ((mode != NULL) && (!strcmp(mode, "r"))) - virBufferVSprintf(&buf, " <readonly/>\n"); + bufferVSprintf(&buf, " <readonly/>\n"); else if ((mode != NULL) && (!strcmp(mode, "w!"))) - virBufferVSprintf(&buf, " <shareable/>\n"); - virBufferAdd(&buf, " </disk>\n", 12); + bufferVSprintf(&buf, " <shareable/>\n"); + bufferAdd(&buf, " </disk>\n", 12); bad_parse: if (drvName) @@ -1626,48 +1627,48 @@ xend_parse_sexp_desc(virConnectPtr conn, tmp2 = sexpr_node(node, "device/vif/script"); tmp = sexpr_node(node, "device/vif/bridge"); if ((tmp2 && strstr(tmp2, "bridge")) || tmp) { - virBufferVSprintf(&buf, " <interface type='bridge'>\n"); + bufferVSprintf(&buf, " <interface type='bridge'>\n"); if (tmp != NULL) - virBufferVSprintf(&buf, " <source bridge='%s'/>\n", + bufferVSprintf(&buf, " <source bridge='%s'/>\n", tmp); } else { - virBufferVSprintf(&buf, " <interface type='ethernet'>\n"); + bufferVSprintf(&buf, " <interface type='ethernet'>\n"); } tmp = sexpr_node(node, "device/vif/vifname"); if (tmp) - virBufferVSprintf(&buf, " <target dev='%s'/>\n", + bufferVSprintf(&buf, " <target dev='%s'/>\n", tmp); tmp = sexpr_node(node, "device/vif/mac"); if (tmp) - virBufferVSprintf(&buf, " <mac address='%s'/>\n", + bufferVSprintf(&buf, " <mac address='%s'/>\n", tmp); tmp = sexpr_node(node, "device/vif/ip"); if (tmp) - virBufferVSprintf(&buf, " <ip address='%s'/>\n", + bufferVSprintf(&buf, " <ip address='%s'/>\n", tmp); if (tmp2) - virBufferVSprintf(&buf, " <script path='%s'/>\n", + bufferVSprintf(&buf, " <script path='%s'/>\n", tmp2); - virBufferAdd(&buf, " </interface>\n", 17); + bufferAdd(&buf, " </interface>\n", 17); } else if (sexpr_lookup(node, "device/vfb")) { /* New style graphics config for PV guests in >= 3.0.4, * or for HVM guests in >= 3.0.5 */ tmp = sexpr_node(node, "device/vfb/type"); if (tmp && !strcmp(tmp, "sdl")) { - virBufferAdd(&buf, " <graphics type='sdl'/>\n", 27); + bufferAdd(&buf, " <graphics type='sdl'/>\n", 27); } else if (tmp && !strcmp(tmp, "vnc")) { int port = xenStoreDomainGetVNCPort(conn, domid); const char *listenAddr = sexpr_node(node, "device/vfb/vnclisten"); const char *keymap = sexpr_node(node, "device/vfb/keymap"); - virBufferVSprintf(&buf, " <graphics type='vnc' port='%d'", port); + bufferVSprintf(&buf, " <graphics type='vnc' port='%d'", port); if (listenAddr) - virBufferVSprintf(&buf, " listen='%s'", listenAddr); + bufferVSprintf(&buf, " listen='%s'", listenAddr); if (keymap) - virBufferVSprintf(&buf, " keymap='%s'", keymap); - virBufferAdd(&buf, "/>\n", 3); + bufferVSprintf(&buf, " keymap='%s'", keymap); + bufferAdd(&buf, "/>\n", 3); } } } @@ -1675,29 +1676,29 @@ xend_parse_sexp_desc(virConnectPtr conn, if (hvm) { tmp = sexpr_node(root, "domain/image/hvm/fda"); if ((tmp != NULL) && (tmp[0] != 0)) { - virBufferAdd(&buf, " <disk type='file' device='floppy'>\n", 39); - virBufferVSprintf(&buf, " <source file='%s'/>\n", tmp); - virBufferAdd(&buf, " <target dev='fda'/>\n", 26); - virBufferAdd(&buf, " </disk>\n", 12); + bufferAdd(&buf, " <disk type='file' device='floppy'>\n", 39); + bufferVSprintf(&buf, " <source file='%s'/>\n", tmp); + bufferAdd(&buf, " <target dev='fda'/>\n", 26); + bufferAdd(&buf, " </disk>\n", 12); } tmp = sexpr_node(root, "domain/image/hvm/fdb"); if ((tmp != NULL) && (tmp[0] != 0)) { - virBufferAdd(&buf, " <disk type='file' device='floppy'>\n", 39); - virBufferVSprintf(&buf, " <source file='%s'/>\n", tmp); - virBufferAdd(&buf, " <target dev='fdb'/>\n", 26); - virBufferAdd(&buf, " </disk>\n", 12); + bufferAdd(&buf, " <disk type='file' device='floppy'>\n", 39); + bufferVSprintf(&buf, " <source file='%s'/>\n", tmp); + bufferAdd(&buf, " <target dev='fdb'/>\n", 26); + bufferAdd(&buf, " </disk>\n", 12); } /* Old style cdrom config from Xen <= 3.0.2 */ if (xendConfigVersion == 1) { tmp = sexpr_node(root, "domain/image/hvm/cdrom"); if ((tmp != NULL) && (tmp[0] != 0)) { - virBufferAdd(&buf, " <disk type='file' device='cdrom'>\n", 38); - virBufferAdd(&buf, " <driver name='file'/>\n", 28); - virBufferVSprintf(&buf, " <source file='%s'/>\n", tmp); - virBufferAdd(&buf, " <target dev='hdc'/>\n", 26); - virBufferAdd(&buf, " <readonly/>\n", 18); - virBufferAdd(&buf, " </disk>\n", 12); + bufferAdd(&buf, " <disk type='file' device='cdrom'>\n", 38); + bufferAdd(&buf, " <driver name='file'/>\n", 28); + bufferVSprintf(&buf, " <source file='%s'/>\n", tmp); + bufferAdd(&buf, " <target dev='hdc'/>\n", 26); + bufferAdd(&buf, " <readonly/>\n", 18); + bufferAdd(&buf, " </disk>\n", 12); } } } @@ -1719,12 +1720,12 @@ xend_parse_sexp_desc(virConnectPtr conn, */ if (port == -1 && xendConfigVersion < 2) port = 5900 + domid; - virBufferVSprintf(&buf, " <graphics type='vnc' port='%d'", port); + bufferVSprintf(&buf, " <graphics type='vnc' port='%d'", port); if (listenAddr) - virBufferVSprintf(&buf, " listen='%s'", listenAddr); + bufferVSprintf(&buf, " listen='%s'", listenAddr); if (keymap) - virBufferVSprintf(&buf, " keymap='%s'", keymap); - virBufferAdd(&buf, "/>\n", 3); + bufferVSprintf(&buf, " keymap='%s'", keymap); + bufferAdd(&buf, "/>\n", 3); } } @@ -1732,18 +1733,18 @@ xend_parse_sexp_desc(virConnectPtr conn, tmp = sexpr_fmt_node(root, "domain/image/%s/sdl", hvm ? "hvm" : "linux"); if (tmp != NULL) { if (tmp[0] == '1') - virBufferAdd(&buf, " <graphics type='sdl'/>\n", 27 ); + bufferAdd(&buf, " <graphics type='sdl'/>\n", 27 ); } } tty = xenStoreDomainGetConsolePath(conn, domid); if (tty) { - virBufferVSprintf(&buf, " <console tty='%s'/>\n", tty); + bufferVSprintf(&buf, " <console tty='%s'/>\n", tty); free(tty); } - virBufferAdd(&buf, " </devices>\n", 13); - virBufferAdd(&buf, "</domain>\n", 10); + bufferAdd(&buf, " </devices>\n", 13); + bufferAdd(&buf, "</domain>\n", 10); buf.content[buf.use] = 0; return (ret); Index: src/xm_internal.c =================================================================== RCS file: /data/cvs/libvirt/src/xm_internal.c,v retrieving revision 1.32 diff -u -p -r1.32 xm_internal.c --- src/xm_internal.c 26 Jun 2007 11:42:46 -0000 1.32 +++ src/xm_internal.c 26 Jun 2007 14:27:06 -0000 @@ -46,6 +46,7 @@ #include "conf.h" #include "hash.h" #include "internal.h" +#include "buf.h" #include "xml.h" typedef struct xenXMConfCache *xenXMConfCachePtr; @@ -579,7 +580,7 @@ int xenXMDomainGetInfo(virDomainPtr doma * domain, suitable for later feeding for virDomainCreateLinux */ char *xenXMDomainFormatXML(virConnectPtr conn, virConfPtr conf) { - virBufferPtr buf; + bufferPtr buf; char *xml; const char *name; unsigned char uuid[VIR_UUID_BUFLEN]; @@ -601,11 +602,11 @@ char *xenXMDomainFormatXML(virConnectPtr if (xenXMConfigGetUUID(conf, "uuid", uuid) < 0) return (NULL); - buf = virBufferNew(4096); + buf = bufferNew(4096); - virBufferAdd(buf, "<domain type='xen'>\n", -1); - virBufferVSprintf(buf, " <name>%s</name>\n", name); - virBufferVSprintf(buf, + bufferAdd(buf, "<domain type='xen'>\n", -1); + bufferVSprintf(buf, " <name>%s</name>\n", name); + bufferVSprintf(buf, " <uuid>%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x</uuid>\n", uuid[0], uuid[1], uuid[2], uuid[3], uuid[4], uuid[5], uuid[6], uuid[7], @@ -618,10 +619,10 @@ char *xenXMDomainFormatXML(virConnectPtr if (hvm) { const char *boot; - virBufferAdd(buf, " <os>\n", -1); - virBufferAdd(buf, " <type>hvm</type>\n", -1); + bufferAdd(buf, " <os>\n", -1); + bufferAdd(buf, " <type>hvm</type>\n", -1); if (xenXMConfigGetString(conf, "kernel", &str) == 0) - virBufferVSprintf(buf, " <loader>%s</loader>\n", str); + bufferVSprintf(buf, " <loader>%s</loader>\n", str); if (xenXMConfigGetString(conf, "boot", &boot) < 0) boot = "c"; @@ -640,77 +641,77 @@ char *xenXMDomainFormatXML(virConnectPtr dev = "hd"; break; } - virBufferVSprintf(buf, " <boot dev='%s'/>\n", dev); + bufferVSprintf(buf, " <boot dev='%s'/>\n", dev); boot++; } - virBufferAdd(buf, " </os>\n", -1); + bufferAdd(buf, " </os>\n", -1); } else { if (xenXMConfigGetString(conf, "bootloader", &str) == 0) - virBufferVSprintf(buf, " <bootloader>%s</bootloader>\n", str); + bufferVSprintf(buf, " <bootloader>%s</bootloader>\n", str); if (xenXMConfigGetString(conf, "bootargs", &str) == 0) - virBufferVSprintf(buf, " <bootloader_args>%s</bootloader_args>\n", str); + bufferVSprintf(buf, " <bootloader_args>%s</bootloader_args>\n", str); if (xenXMConfigGetString(conf, "kernel", &str) == 0) { - virBufferAdd(buf, " <os>\n", -1); - virBufferAdd(buf, " <type>linux</type>\n", -1); - virBufferVSprintf(buf, " <kernel>%s</kernel>\n", str); + bufferAdd(buf, " <os>\n", -1); + bufferAdd(buf, " <type>linux</type>\n", -1); + bufferVSprintf(buf, " <kernel>%s</kernel>\n", str); if (xenXMConfigGetString(conf, "ramdisk", &str) == 0) - virBufferVSprintf(buf, " <initrd>%s</initrd>\n", str); + bufferVSprintf(buf, " <initrd>%s</initrd>\n", str); if (xenXMConfigGetString(conf, "extra", &str) == 0) - virBufferVSprintf(buf, " <cmdline>%s</cmdline>\n", str); - virBufferAdd(buf, " </os>\n", -1); + bufferVSprintf(buf, " <cmdline>%s</cmdline>\n", str); + bufferAdd(buf, " </os>\n", -1); } } if (xenXMConfigGetInt(conf, "memory", &val) < 0) val = MIN_XEN_GUEST_SIZE * 2; - virBufferVSprintf(buf, " <currentMemory>%ld</currentMemory>\n", + bufferVSprintf(buf, " <currentMemory>%ld</currentMemory>\n", val * 1024); if (xenXMConfigGetInt(conf, "maxmem", &val) < 0) if (xenXMConfigGetInt(conf, "memory", &val) < 0) val = MIN_XEN_GUEST_SIZE * 2; - virBufferVSprintf(buf, " <memory>%ld</memory>\n", val * 1024); + bufferVSprintf(buf, " <memory>%ld</memory>\n", val * 1024); if (xenXMConfigGetInt(conf, "vcpus", &val) < 0) val = 1; - virBufferVSprintf(buf, " <vcpu>%ld</vcpu>\n", val); + bufferVSprintf(buf, " <vcpu>%ld</vcpu>\n", val); if (xenXMConfigGetString(conf, "on_poweroff", &str) < 0) str = "destroy"; - virBufferVSprintf(buf, " <on_poweroff>%s</on_poweroff>\n", str); + bufferVSprintf(buf, " <on_poweroff>%s</on_poweroff>\n", str); if (xenXMConfigGetString(conf, "on_reboot", &str) < 0) str = "restart"; - virBufferVSprintf(buf, " <on_reboot>%s</on_reboot>\n", str); + bufferVSprintf(buf, " <on_reboot>%s</on_reboot>\n", str); if (xenXMConfigGetString(conf, "on_crash", &str) < 0) str = "restart"; - virBufferVSprintf(buf, " <on_crash>%s</on_crash>\n", str); + bufferVSprintf(buf, " <on_crash>%s</on_crash>\n", str); if (hvm) { - virBufferAdd(buf, " <features>\n", -1); + bufferAdd(buf, " <features>\n", -1); if (xenXMConfigGetInt(conf, "pae", &val) == 0 && val) - virBufferAdd(buf, " <pae/>\n", -1); + bufferAdd(buf, " <pae/>\n", -1); if (xenXMConfigGetInt(conf, "acpi", &val) == 0 && val) - virBufferAdd(buf, " <acpi/>\n", -1); + bufferAdd(buf, " <acpi/>\n", -1); if (xenXMConfigGetInt(conf, "apic", &val) == 0 && val) - virBufferAdd(buf, " <apic/>\n", -1); - virBufferAdd(buf, " </features>\n", -1); + bufferAdd(buf, " <apic/>\n", -1); + bufferAdd(buf, " </features>\n", -1); } - virBufferAdd(buf, " <devices>\n", -1); + bufferAdd(buf, " <devices>\n", -1); if (hvm) { if (xenXMConfigGetString(conf, "device_model", &str) == 0) - virBufferVSprintf(buf, " <emulator>%s</emulator>\n", str); + bufferVSprintf(buf, " <emulator>%s</emulator>\n", str); } list = virConfGetValue(conf, "disk"); @@ -792,19 +793,19 @@ char *xenXMDomainFormatXML(virConnectPtr tmp[0] = '\0'; } - virBufferVSprintf(buf, " <disk type='%s' device='%s'>\n", + bufferVSprintf(buf, " <disk type='%s' device='%s'>\n", block ? "block" : "file", cdrom ? "cdrom" : "disk"); if (drvType[0]) - virBufferVSprintf(buf, " <driver name='%s' type='%s'/>\n", drvName, drvType); + bufferVSprintf(buf, " <driver name='%s' type='%s'/>\n", drvName, drvType); else - virBufferVSprintf(buf, " <driver name='%s'/>\n", drvName); - virBufferVSprintf(buf, " <source %s='%s'/>\n", block ? "dev" : "file", src); - virBufferVSprintf(buf, " <target dev='%s'/>\n", dev); + bufferVSprintf(buf, " <driver name='%s'/>\n", drvName); + bufferVSprintf(buf, " <source %s='%s'/>\n", block ? "dev" : "file", src); + bufferVSprintf(buf, " <target dev='%s'/>\n", dev); if (!strcmp(head, "r") || !strcmp(head, "ro")) - virBufferAdd(buf, " <readonly/>\n", -1); - virBufferAdd(buf, " </disk>\n", -1); + bufferAdd(buf, " <readonly/>\n", -1); + bufferAdd(buf, " </disk>\n", -1); skipdisk: list = list->next; @@ -813,12 +814,12 @@ char *xenXMDomainFormatXML(virConnectPtr if (hvm && priv->xendConfigVersion == 1) { if (xenXMConfigGetString(conf, "cdrom", &str) == 0) { - virBufferAdd(buf, " <disk type='file' device='cdrom'>\n", -1); - virBufferAdd(buf, " <driver name='file'/>\n", -1); - virBufferVSprintf(buf, " <source file='%s'/>\n", str); - virBufferAdd(buf, " <target dev='hdc'/>\n", -1); - virBufferAdd(buf, " <readonly/>\n", -1); - virBufferAdd(buf, " </disk>\n", -1); + bufferAdd(buf, " <disk type='file' device='cdrom'>\n", -1); + bufferAdd(buf, " <driver name='file'/>\n", -1); + bufferVSprintf(buf, " <source file='%s'/>\n", str); + bufferAdd(buf, " <target dev='hdc'/>\n", -1); + bufferAdd(buf, " <readonly/>\n", -1); + bufferAdd(buf, " </disk>\n", -1); } } @@ -889,16 +890,16 @@ char *xenXMDomainFormatXML(virConnectPtr type = 1; } - virBufferAdd(buf, " <interface type='bridge'>\n", -1); + bufferAdd(buf, " <interface type='bridge'>\n", -1); if (mac[0]) - virBufferVSprintf(buf, " <mac address='%s'/>\n", mac); + bufferVSprintf(buf, " <mac address='%s'/>\n", mac); if (type == 1 && bridge[0]) - virBufferVSprintf(buf, " <source bridge='%s'/>\n", bridge); + bufferVSprintf(buf, " <source bridge='%s'/>\n", bridge); if (script[0]) - virBufferVSprintf(buf, " <script path='%s'/>\n", script); + bufferVSprintf(buf, " <script path='%s'/>\n", script); if (ip[0]) - virBufferVSprintf(buf, " <ip address='%s'/>\n", ip); - virBufferAdd(buf, " </interface>\n", -1); + bufferVSprintf(buf, " <ip address='%s'/>\n", ip); + bufferAdd(buf, " </interface>\n", -1); skipnic: list = list->next; @@ -973,39 +974,39 @@ char *xenXMDomainFormatXML(virConnectPtr } if (vnc) { - virBufferVSprintf(buf, + bufferVSprintf(buf, " <graphics type='vnc' port='%ld'", (vncunused ? -1 : 5900+vncdisplay)); if (vnclisten) { - virBufferVSprintf(buf, " listen='%s'", vnclisten); + bufferVSprintf(buf, " listen='%s'", vnclisten); } if (vncpasswd) { - virBufferVSprintf(buf, " passwd='%s'", vncpasswd); + bufferVSprintf(buf, " passwd='%s'", vncpasswd); } if (keymap) { - virBufferVSprintf(buf, " keymap='%s'", keymap); + bufferVSprintf(buf, " keymap='%s'", keymap); } - virBufferAdd(buf, "/>\n", 3); + bufferAdd(buf, "/>\n", 3); } if (sdl) { - virBufferAdd(buf, " <graphics type='sdl'/>\n", -1); + bufferAdd(buf, " <graphics type='sdl'/>\n", -1); } if (hvm) { if (xenXMConfigGetString(conf, "serial", &str) == 0 && !strcmp(str, "pty")) { - virBufferAdd(buf, " <console/>\n", -1); + bufferAdd(buf, " <console/>\n", -1); } } else { /* Paravirt implicitly always has a console */ - virBufferAdd(buf, " <console/>\n", -1); + bufferAdd(buf, " <console/>\n", -1); } - virBufferAdd(buf, " </devices>\n", -1); + bufferAdd(buf, " </devices>\n", -1); - virBufferAdd(buf, "</domain>\n", -1); + bufferAdd(buf, "</domain>\n", -1); xml = buf->content; buf->content = NULL; - virBufferFree(buf); + bufferFree(buf); return (xml); } Index: src/xml.c =================================================================== RCS file: /data/cvs/libvirt/src/xml.c,v retrieving revision 1.77 diff -u -p -r1.77 xml.c --- src/xml.c 7 Jun 2007 13:50:18 -0000 1.77 +++ src/xml.c 26 Jun 2007 14:27:07 -0000 @@ -21,9 +21,11 @@ #include "internal.h" #include "hash.h" #include "sexpr.h" +#include "buf.h" #include "xml.h" #include "xs_internal.h" /* for xenStoreDomainGetNetworkID */ +#ifndef PROXY /** * virXMLError: * @conn: a connection if any @@ -46,7 +48,6 @@ virXMLError(virConnectPtr conn, virError errmsg, info, NULL, value, 0, errmsg, info, value); } -#ifndef PROXY /** * virXPathString: * @xpath: the XPath string to evaluate @@ -266,181 +267,7 @@ virXPathNodeSet(const char *xpath, xmlXP xmlXPathFreeObject(obj); return(ret); } -#endif /* !PROXY */ - -/** - * virBufferGrow: - * @buf: the buffer - * @len: the minimum free size to allocate on top of existing used space - * - * Grow the available space of an XML buffer to at least @len bytes. - * - * Returns the new available space or -1 in case of error - */ -static int -virBufferGrow(virBufferPtr buf, unsigned int len) -{ - int size; - char *newbuf; - - if (buf == NULL) - return (-1); - if (len + buf->use < buf->size) - return (0); - - size = buf->use + len + 1000; - - newbuf = (char *) realloc(buf->content, size); - if (newbuf == NULL) { - virXMLError(NULL, VIR_ERR_NO_MEMORY, _("growing buffer"), size); - return (-1); - } - buf->content = newbuf; - buf->size = size; - return (buf->size - buf->use); -} - -/** - * virBufferAdd: - * @buf: the buffer to dump - * @str: the string - * @len: the number of bytes to add - * - * Add a string range to an XML buffer. if len == -1, the length of - * str is recomputed to the full string. - * - * Returns 0 successful, -1 in case of internal or API error. - */ -int -virBufferAdd(virBufferPtr buf, const char *str, int len) -{ - unsigned int needSize; - - if ((str == NULL) || (buf == NULL)) { - return -1; - } - if (len == 0) - return 0; - - if (len < 0) - len = strlen(str); - - needSize = buf->use + len + 2; - if (needSize > buf->size) { - if (!virBufferGrow(buf, needSize - buf->use)) { - return (-1); - } - } - /* XXX: memmove() is 2x slower than memcpy(), do we really need it? */ - memmove(&buf->content[buf->use], str, len); - buf->use += len; - buf->content[buf->use] = 0; - return (0); -} - -virBufferPtr -virBufferNew(unsigned int size) -{ - virBufferPtr buf; - - if (!(buf = malloc(sizeof(*buf)))) { - virXMLError(NULL, VIR_ERR_NO_MEMORY, _("allocate new buffer"), sizeof(*buf)); - return NULL; - } - if (size && (buf->content = malloc(size))==NULL) { - virXMLError(NULL, VIR_ERR_NO_MEMORY, _("allocate buffer content"), size); - free(buf); - return NULL; - } - buf->size = size; - buf->use = 0; - - return buf; -} - -void -virBufferFree(virBufferPtr buf) -{ - if (buf) { - if (buf->content) - free(buf->content); - free(buf); - } -} - -/** - * virBufferVSprintf: - * @buf: the buffer to dump - * @format: the format - * @argptr: the variable list of arguments - * - * Do a formatted print to an XML buffer. - * - * Returns 0 successful, -1 in case of internal or API error. - */ -int -virBufferVSprintf(virBufferPtr buf, const char *format, ...) -{ - int size, count; - va_list locarg, argptr; - - if ((format == NULL) || (buf == NULL)) { - return (-1); - } - size = buf->size - buf->use - 1; - va_start(argptr, format); - va_copy(locarg, argptr); - while (((count = vsnprintf(&buf->content[buf->use], size, format, - locarg)) < 0) || (count >= size - 1)) { - buf->content[buf->use] = 0; - va_end(locarg); - if (virBufferGrow(buf, 1000) < 0) { - return (-1); - } - size = buf->size - buf->use - 1; - va_copy(locarg, argptr); - } - va_end(locarg); - buf->use += count; - buf->content[buf->use] = 0; - return (0); -} - -/** - * virBufferStrcat: - * @buf: the buffer to dump - * @argptr: the variable list of strings, the last argument must be NULL - * - * Concatenate strings to an XML buffer. - * - * Returns 0 successful, -1 in case of internal or API error. - */ -int -virBufferStrcat(virBufferPtr buf, ...) -{ - va_list ap; - char *str; - va_start(ap, buf); - - while ((str = va_arg(ap, char *)) != NULL) { - unsigned int len = strlen(str); - unsigned int needSize = buf->use + len + 2; - - if (needSize > buf->size) { - if (!virBufferGrow(buf, needSize - buf->use)) - return -1; - } - memcpy(&buf->content[buf->use], str, len); - buf->use += len; - buf->content[buf->use] = 0; - } - va_end(ap); - return 0; -} - - -#ifndef PROXY /** * virtDomainParseXMLGraphicsDescImage: * @conn: pointer to the hypervisor connection @@ -455,23 +282,23 @@ virBufferStrcat(virBufferPtr buf, ...) * * Returns 0 in case of success, -1 in case of error */ -static int virDomainParseXMLGraphicsDescImage(virConnectPtr conn ATTRIBUTE_UNUSED, xmlNodePtr node, virBufferPtr buf, int xendConfigVersion) +static int virDomainParseXMLGraphicsDescImage(virConnectPtr conn ATTRIBUTE_UNUSED, xmlNodePtr node, bufferPtr buf, int xendConfigVersion) { xmlChar *graphics_type = NULL; graphics_type = xmlGetProp(node, BAD_CAST "type"); if (graphics_type != NULL) { if (xmlStrEqual(graphics_type, BAD_CAST "sdl")) { - virBufferAdd(buf, "(sdl 1)", 7); + bufferAdd(buf, "(sdl 1)", 7); /* TODO: * Need to understand sdl options * - *virBufferAdd(buf, "(display localhost:10.0)", 24); - *virBufferAdd(buf, "(xauthority /root/.Xauthority)", 30); + *bufferAdd(buf, "(display localhost:10.0)", 24); + *bufferAdd(buf, "(xauthority /root/.Xauthority)", 30); */ } else if (xmlStrEqual(graphics_type, BAD_CAST "vnc")) { - virBufferAdd(buf, "(vnc 1)", 7); + bufferAdd(buf, "(vnc 1)", 7); if (xendConfigVersion >= 2) { xmlChar *vncport = xmlGetProp(node, BAD_CAST "port"); xmlChar *vnclisten = xmlGetProp(node, BAD_CAST "listen"); @@ -480,21 +307,21 @@ static int virDomainParseXMLGraphicsDesc if (vncport != NULL) { long port = strtol((const char *)vncport, NULL, 10); if (port == -1) - virBufferAdd(buf, "(vncunused 1)", 13); + bufferAdd(buf, "(vncunused 1)", 13); else if (port >= 5900) - virBufferVSprintf(buf, "(vncdisplay %ld)", port - 5900); + bufferVSprintf(buf, "(vncdisplay %ld)", port - 5900); xmlFree(vncport); } if (vnclisten != NULL) { - virBufferVSprintf(buf, "(vnclisten %s)", vnclisten); + bufferVSprintf(buf, "(vnclisten %s)", vnclisten); xmlFree(vnclisten); } if (vncpasswd != NULL) { - virBufferVSprintf(buf, "(vncpasswd %s)", vncpasswd); + bufferVSprintf(buf, "(vncpasswd %s)", vncpasswd); xmlFree(vncpasswd); } if (keymap != NULL) { - virBufferVSprintf(buf, "(keymap %s)", keymap); + bufferVSprintf(buf, "(keymap %s)", keymap); xmlFree(keymap); } } @@ -518,25 +345,25 @@ static int virDomainParseXMLGraphicsDesc * * Returns 0 in case of success, -1 in case of error */ -static int virDomainParseXMLGraphicsDescVFB(virConnectPtr conn ATTRIBUTE_UNUSED, xmlNodePtr node, virBufferPtr buf) +static int virDomainParseXMLGraphicsDescVFB(virConnectPtr conn ATTRIBUTE_UNUSED, xmlNodePtr node, bufferPtr buf) { xmlChar *graphics_type = NULL; graphics_type = xmlGetProp(node, BAD_CAST "type"); if (graphics_type != NULL) { - virBufferAdd(buf, "(device (vkbd))", 15); - virBufferAdd(buf, "(device (vfb ", 13); + bufferAdd(buf, "(device (vkbd))", 15); + bufferAdd(buf, "(device (vfb ", 13); if (xmlStrEqual(graphics_type, BAD_CAST "sdl")) { - virBufferAdd(buf, "(type sdl)", 10); + bufferAdd(buf, "(type sdl)", 10); /* TODO: * Need to understand sdl options * - *virBufferAdd(buf, "(display localhost:10.0)", 24); - *virBufferAdd(buf, "(xauthority /root/.Xauthority)", 30); + *bufferAdd(buf, "(display localhost:10.0)", 24); + *bufferAdd(buf, "(xauthority /root/.Xauthority)", 30); */ } else if (xmlStrEqual(graphics_type, BAD_CAST "vnc")) { - virBufferAdd(buf, "(type vnc)", 10); + bufferAdd(buf, "(type vnc)", 10); xmlChar *vncport = xmlGetProp(node, BAD_CAST "port"); xmlChar *vnclisten = xmlGetProp(node, BAD_CAST "listen"); xmlChar *vncpasswd = xmlGetProp(node, BAD_CAST "passwd"); @@ -544,25 +371,25 @@ static int virDomainParseXMLGraphicsDesc if (vncport != NULL) { long port = strtol((const char *)vncport, NULL, 10); if (port == -1) - virBufferAdd(buf, "(vncunused 1)", 13); + bufferAdd(buf, "(vncunused 1)", 13); else if (port >= 5900) - virBufferVSprintf(buf, "(vncdisplay %ld)", port - 5900); + bufferVSprintf(buf, "(vncdisplay %ld)", port - 5900); xmlFree(vncport); } if (vnclisten != NULL) { - virBufferVSprintf(buf, "(vnclisten %s)", vnclisten); + bufferVSprintf(buf, "(vnclisten %s)", vnclisten); xmlFree(vnclisten); } if (vncpasswd != NULL) { - virBufferVSprintf(buf, "(vncpasswd %s)", vncpasswd); + bufferVSprintf(buf, "(vncpasswd %s)", vncpasswd); xmlFree(vncpasswd); } if (keymap != NULL) { - virBufferVSprintf(buf, "(keymap %s)", keymap); + bufferVSprintf(buf, "(keymap %s)", keymap); xmlFree(keymap); } } - virBufferAdd(buf, "))", 2); + bufferAdd(buf, "))", 2); xmlFree(graphics_type); } return 0; @@ -586,7 +413,7 @@ static int virDomainParseXMLGraphicsDesc * Returns 0 in case of success, -1 in case of error. */ static int -virDomainParseXMLOSDescHVM(virConnectPtr conn, xmlNodePtr node, virBufferPtr buf, xmlXPathContextPtr ctxt, int vcpus, int xendConfigVersion) +virDomainParseXMLOSDescHVM(virConnectPtr conn, xmlNodePtr node, bufferPtr buf, xmlXPathContextPtr ctxt, int vcpus, int xendConfigVersion) { xmlNodePtr cur, txt; xmlChar *type = NULL; @@ -642,12 +469,12 @@ virDomainParseXMLOSDescHVM(virConnectPtr virXMLError(conn, VIR_ERR_OS_TYPE, (const char *) type, 0); return (-1); } - virBufferAdd(buf, "(image (hvm ", 12); + bufferAdd(buf, "(image (hvm ", 12); if (loader == NULL) { virXMLError(conn, VIR_ERR_NO_KERNEL, NULL, 0); goto error; } else { - virBufferVSprintf(buf, "(kernel '%s')", (const char *) loader); + bufferVSprintf(buf, "(kernel '%s')", (const char *) loader); } /* get the device emulation model */ @@ -656,13 +483,13 @@ virDomainParseXMLOSDescHVM(virConnectPtr virXMLError(conn, VIR_ERR_NO_KERNEL, NULL, 0); /* TODO: error */ goto error; } - virBufferVSprintf(buf, "(device_model '%s')", str); + bufferVSprintf(buf, "(device_model '%s')", str); xmlFree(str); - virBufferVSprintf(buf, "(vcpus %d)", vcpus); + bufferVSprintf(buf, "(vcpus %d)", vcpus); if (nbootorder) - virBufferVSprintf(buf, "(boot %s)", bootorder); + bufferVSprintf(buf, "(boot %s)", bootorder); /* get the 1st floppy device file */ cur = virXPathNode("/domain/devices/disk[@device='floppy' and target/@dev='fda']/source", @@ -671,7 +498,7 @@ virDomainParseXMLOSDescHVM(virConnectPtr xmlChar *fdfile; fdfile = xmlGetProp(cur, BAD_CAST "file"); if (fdfile != NULL) { - virBufferVSprintf(buf, "(fda '%s')", fdfile); + bufferVSprintf(buf, "(fda '%s')", fdfile); free(fdfile); } } @@ -683,7 +510,7 @@ virDomainParseXMLOSDescHVM(virConnectPtr xmlChar *fdfile; fdfile = xmlGetProp(cur, BAD_CAST "file"); if (fdfile != NULL) { - virBufferVSprintf(buf, "(fdb '%s')", fdfile); + bufferVSprintf(buf, "(fdb '%s')", fdfile); free(fdfile); } } @@ -699,7 +526,7 @@ virDomainParseXMLOSDescHVM(virConnectPtr cdfile = xmlGetProp(cur, BAD_CAST "file"); if (cdfile != NULL) { - virBufferVSprintf(buf, "(cdrom '%s')", + bufferVSprintf(buf, "(cdrom '%s')", (const char *)cdfile); xmlFree(cdfile); } @@ -707,11 +534,11 @@ virDomainParseXMLOSDescHVM(virConnectPtr } if (virXPathNode("/domain/features/acpi", ctxt) != NULL) - virBufferAdd(buf, "(acpi 1)", 8); + bufferAdd(buf, "(acpi 1)", 8); if (virXPathNode("/domain/features/apic", ctxt) != NULL) - virBufferAdd(buf, "(apic 1)", 8); + bufferAdd(buf, "(apic 1)", 8); if (virXPathNode("/domain/features/pae", ctxt) != NULL) - virBufferAdd(buf, "(pae 1)", 7); + bufferAdd(buf, "(pae 1)", 7); res = virXPathBoolean("count(domain/devices/console) > 0", ctxt); if (res < 0) { @@ -719,7 +546,7 @@ virDomainParseXMLOSDescHVM(virConnectPtr goto error; } if (res) { - virBufferAdd(buf, "(serial pty)", 12); + bufferAdd(buf, "(serial pty)", 12); } /* HVM graphics for xen <= 3.0.5 */ @@ -735,7 +562,7 @@ virDomainParseXMLOSDescHVM(virConnectPtr } } - virBufferAdd(buf, "))", 2); + bufferAdd(buf, "))", 2); return (0); @@ -759,7 +586,7 @@ virDomainParseXMLOSDescHVM(virConnectPtr * Returns 0 in case of success, -1 in case of error. */ static int -virDomainParseXMLOSDescPV(virConnectPtr conn, xmlNodePtr node, virBufferPtr buf, xmlXPathContextPtr ctxt, int xendConfigVersion) +virDomainParseXMLOSDescPV(virConnectPtr conn, xmlNodePtr node, bufferPtr buf, xmlXPathContextPtr ctxt, int xendConfigVersion) { xmlNodePtr cur, txt; const xmlChar *type = NULL; @@ -811,19 +638,19 @@ virDomainParseXMLOSDescPV(virConnectPtr virXMLError(conn, VIR_ERR_OS_TYPE, (const char *) type, 0); return (-1); } - virBufferAdd(buf, "(image (linux ", 14); + bufferAdd(buf, "(image (linux ", 14); if (kernel == NULL) { virXMLError(conn, VIR_ERR_NO_KERNEL, NULL, 0); return (-1); } else { - virBufferVSprintf(buf, "(kernel '%s')", (const char *) kernel); + bufferVSprintf(buf, "(kernel '%s')", (const char *) kernel); } if (initrd != NULL) - virBufferVSprintf(buf, "(ramdisk '%s')", (const char *) initrd); + bufferVSprintf(buf, "(ramdisk '%s')", (const char *) initrd); if (root != NULL) - virBufferVSprintf(buf, "(root '%s')", (const char *) root); + bufferVSprintf(buf, "(root '%s')", (const char *) root); if (cmdline != NULL) - virBufferVSprintf(buf, "(args '%s')", (const char *) cmdline); + bufferVSprintf(buf, "(args '%s')", (const char *) cmdline); /* PV graphics for xen <= 3.0.4 */ if (xendConfigVersion < 3) { @@ -838,7 +665,7 @@ virDomainParseXMLOSDescPV(virConnectPtr } error: - virBufferAdd(buf, "))", 2); + bufferAdd(buf, "))", 2); return (0); } @@ -878,7 +705,7 @@ virCatchXMLParseError(void *ctx, const c * Returns 0 in case of success, -1 in case of error. */ static int -virDomainParseXMLDiskDesc(virConnectPtr conn, xmlNodePtr node, virBufferPtr buf, int hvm, int xendConfigVersion) +virDomainParseXMLDiskDesc(virConnectPtr conn, xmlNodePtr node, bufferPtr buf, int hvm, int xendConfigVersion) { xmlNodePtr cur; xmlChar *type = NULL; @@ -978,14 +805,14 @@ virDomainParseXMLDiskDesc(virConnectPtr } - virBufferAdd(buf, "(device ", 8); + bufferAdd(buf, "(device ", 8); /* Normally disks are in a (device (vbd ...)) block but blktap disks ended up in a differently named (device (tap ....)) block.... */ if (drvName && !strcmp((const char *)drvName, "tap")) { - virBufferAdd(buf, "(tap ", 5); + bufferAdd(buf, "(tap ", 5); } else { - virBufferAdd(buf, "(vbd ", 5); + bufferAdd(buf, "(vbd ", 5); } if (hvm) { @@ -996,42 +823,42 @@ virDomainParseXMLDiskDesc(virConnectPtr /* Xend <= 3.0.2 wants a ioemu: prefix on devices for HVM */ if (xendConfigVersion == 1) - virBufferVSprintf(buf, "(dev 'ioemu:%s')", (const char *)tmp); + bufferVSprintf(buf, "(dev 'ioemu:%s')", (const char *)tmp); else /* But newer does not */ - virBufferVSprintf(buf, "(dev '%s%s')", (const char *)tmp, cdrom ? ":cdrom" : ":disk"); + bufferVSprintf(buf, "(dev '%s%s')", (const char *)tmp, cdrom ? ":cdrom" : ":disk"); } else - virBufferVSprintf(buf, "(dev '%s')", (const char *)target); + bufferVSprintf(buf, "(dev '%s')", (const char *)target); if (drvName && !isNoSrcCdrom) { if (!strcmp((const char *)drvName, "tap")) { - virBufferVSprintf(buf, "(uname '%s:%s:%s')", + bufferVSprintf(buf, "(uname '%s:%s:%s')", (const char *)drvName, (drvType ? (const char *)drvType : "aio"), (const char *)source); } else { - virBufferVSprintf(buf, "(uname '%s:%s')", + bufferVSprintf(buf, "(uname '%s:%s')", (const char *)drvName, (const char *)source); } } else if (!isNoSrcCdrom) { if (typ == 0) - virBufferVSprintf(buf, "(uname 'file:%s')", source); + bufferVSprintf(buf, "(uname 'file:%s')", source); else if (typ == 1) { if (source[0] == '/') - virBufferVSprintf(buf, "(uname 'phy:%s')", source); + bufferVSprintf(buf, "(uname 'phy:%s')", source); else - virBufferVSprintf(buf, "(uname 'phy:/dev/%s')", source); + bufferVSprintf(buf, "(uname 'phy:/dev/%s')", source); } } if (ro == 1) - virBufferVSprintf(buf, "(mode 'r')"); + bufferVSprintf(buf, "(mode 'r')"); else if (shareable == 1) - virBufferVSprintf(buf, "(mode 'w!')"); + bufferVSprintf(buf, "(mode 'w!')"); else - virBufferVSprintf(buf, "(mode 'w')"); + bufferVSprintf(buf, "(mode 'w')"); - virBufferAdd(buf, ")", 1); - virBufferAdd(buf, ")", 1); + bufferAdd(buf, ")", 1); + bufferAdd(buf, ")", 1); cleanup: xmlFree(drvType); @@ -1057,7 +884,7 @@ virDomainParseXMLDiskDesc(virConnectPtr * Returns 0 in case of success, -1 in case of error. */ static int -virDomainParseXMLIfDesc(virConnectPtr conn ATTRIBUTE_UNUSED, xmlNodePtr node, virBufferPtr buf, int hvm, int xendConfigVersion) +virDomainParseXMLIfDesc(virConnectPtr conn ATTRIBUTE_UNUSED, xmlNodePtr node, bufferPtr buf, int hvm, int xendConfigVersion) { xmlNodePtr cur; xmlChar *type = NULL; @@ -1107,14 +934,14 @@ virDomainParseXMLIfDesc(virConnectPtr co cur = cur->next; } - virBufferAdd(buf, "(vif ", 5); + bufferAdd(buf, "(vif ", 5); if (mac != NULL) - virBufferVSprintf(buf, "(mac '%s')", (const char *) mac); + bufferVSprintf(buf, "(mac '%s')", (const char *) mac); if (source != NULL) { if (typ == 0) - virBufferVSprintf(buf, "(bridge '%s')", (const char *) source); + bufferVSprintf(buf, "(bridge '%s')", (const char *) source); else if (typ == 1) /* TODO does that work like that ? */ - virBufferVSprintf(buf, "(dev '%s')", (const char *) source); + bufferVSprintf(buf, "(dev '%s')", (const char *) source); else { virNetworkPtr network = virNetworkLookupByName(conn, (const char *) source); char *bridge; @@ -1122,22 +949,22 @@ virDomainParseXMLIfDesc(virConnectPtr co virXMLError(conn, VIR_ERR_NO_SOURCE, (const char *) source, 0); goto error; } - virBufferVSprintf(buf, "(bridge '%s')", bridge); + bufferVSprintf(buf, "(bridge '%s')", bridge); free(bridge); } } if (script != NULL) - virBufferVSprintf(buf, "(script '%s')", script); + bufferVSprintf(buf, "(script '%s')", script); if (ip != NULL) - virBufferVSprintf(buf, "(ip '%s')", ip); + bufferVSprintf(buf, "(ip '%s')", ip); /* * apparently (type ioemu) breaks paravirt drivers on HVM so skip this * from Xen 3.1.0 */ if ((hvm) && (xendConfigVersion < 4)) - virBufferAdd(buf, "(type ioemu)", 12); + bufferAdd(buf, "(type ioemu)", 12); - virBufferAdd(buf, ")", 1); + bufferAdd(buf, ")", 1); ret = 0; error: if (mac != NULL) @@ -1171,7 +998,7 @@ virDomainParseXMLDesc(virConnectPtr conn xmlDocPtr xml = NULL; xmlNodePtr node; char *nam = NULL; - virBuffer buf; + buffer buf; xmlChar *prop; xmlParserCtxtPtr pctxt; xmlXPathContextPtr ctxt = NULL; @@ -1221,7 +1048,7 @@ virDomainParseXMLDesc(virConnectPtr conn } xmlFree(prop); } - virBufferAdd(&buf, "(vm ", 4); + bufferAdd(&buf, "(vm ", 4); ctxt = xmlXPathNewContext(xml); if (ctxt == NULL) { goto error; @@ -1234,7 +1061,7 @@ virDomainParseXMLDesc(virConnectPtr conn virXMLError(conn, VIR_ERR_NO_NAME, xmldesc, 0); goto error; } - virBufferVSprintf(&buf, "(name '%s')", nam); + bufferVSprintf(&buf, "(name '%s')", nam); if ((virXPathNumber("number(/domain/memory[1])", ctxt, &f) < 0) || (f < MIN_XEN_GUEST_SIZE * 1024)) { @@ -1252,23 +1079,23 @@ virDomainParseXMLDesc(virConnectPtr conn max_mem = mem; } } - virBufferVSprintf(&buf, "(memory %lu)(maxmem %lu)", mem, max_mem); + bufferVSprintf(&buf, "(memory %lu)(maxmem %lu)", mem, max_mem); if ((virXPathNumber("number(/domain/vcpu[1])", ctxt, &f) == 0) && (f > 0)) { vcpus = (unsigned int) f; } - virBufferVSprintf(&buf, "(vcpus %u)", vcpus); + bufferVSprintf(&buf, "(vcpus %u)", vcpus); str = virXPathString("string(/domain/uuid[1])", ctxt); if (str != NULL) { - virBufferVSprintf(&buf, "(uuid '%s')", str); + bufferVSprintf(&buf, "(uuid '%s')", str); free(str); } str = virXPathString("string(/domain/bootloader[1])", ctxt); if (str != NULL) { - virBufferVSprintf(&buf, "(bootloader '%s')", str); + bufferVSprintf(&buf, "(bootloader '%s')", str); /* * if using a bootloader, the kernel and initrd strings are not * significant and should be discarded @@ -1282,25 +1109,25 @@ virDomainParseXMLDesc(virConnectPtr conn /* * ignore the bootloader_args value unless a bootloader was specified */ - virBufferVSprintf(&buf, "(bootloader_args '%s')", str); + bufferVSprintf(&buf, "(bootloader_args '%s')", str); free(str); } str = virXPathString("string(/domain/on_poweroff[1])", ctxt); if (str != NULL) { - virBufferVSprintf(&buf, "(on_poweroff '%s')", str); + bufferVSprintf(&buf, "(on_poweroff '%s')", str); free(str); } str = virXPathString("string(/domain/on_reboot[1])", ctxt); if (str != NULL) { - virBufferVSprintf(&buf, "(on_reboot '%s')", str); + bufferVSprintf(&buf, "(on_reboot '%s')", str); free(str); } str = virXPathString("string(/domain/on_crash[1])", ctxt); if (str != NULL) { - virBufferVSprintf(&buf, "(on_crash '%s')", str); + bufferVSprintf(&buf, "(on_crash '%s')", str); free(str); } @@ -1345,13 +1172,13 @@ virDomainParseXMLDesc(virConnectPtr conn nb_nodes = virXPathNodeSet("/domain/devices/interface", ctxt, &nodes); if (nb_nodes > 0) { for (i = 0; i < nb_nodes; i++) { - virBufferAdd(&buf, "(device ", 8); + bufferAdd(&buf, "(device ", 8); res = virDomainParseXMLIfDesc(conn, nodes[i], &buf, hvm, xendConfigVersion); if (res != 0) { free(nodes); goto error; } - virBufferAdd(&buf, ")", 1); + bufferAdd(&buf, ")", 1); } free(nodes); } @@ -1374,7 +1201,7 @@ virDomainParseXMLDesc(virConnectPtr conn } - virBufferAdd(&buf, ")", 1); /* closes (vm */ + bufferAdd(&buf, ")", 1); /* closes (vm */ buf.content[buf.use] = 0; xmlXPathFreeContext(ctxt); @@ -1485,7 +1312,7 @@ virParseXMLDevice(virConnectPtr conn, ch { xmlDocPtr xml = NULL; xmlNodePtr node; - virBuffer buf; + buffer buf; buf.content = malloc(1000); if (buf.content == NULL) Index: src/xml.h =================================================================== RCS file: /data/cvs/libvirt/src/xml.h,v retrieving revision 1.14 diff -u -p -r1.14 xml.h --- src/xml.h 6 Apr 2007 12:28:24 -0000 1.14 +++ src/xml.h 26 Jun 2007 14:27:07 -0000 @@ -15,26 +15,6 @@ extern "C" { #endif -/** - * virBuffer: - * - * A buffer structure. - */ -typedef struct _virBuffer virBuffer; -typedef virBuffer *virBufferPtr; -struct _virBuffer { - char *content; /* The buffer content UTF8 */ - unsigned int use; /* The buffer size used */ - unsigned int size; /* The buffer size */ -}; - -virBufferPtr virBufferNew(unsigned int size); -void virBufferFree(virBufferPtr buf); -int virBufferAdd(virBufferPtr buf, const char *str, int len); -int virBufferVSprintf(virBufferPtr buf, const char *format, ...) - ATTRIBUTE_FORMAT(printf, 2, 3); -int virBufferStrcat(virBufferPtr buf, ...); - int virXPathBoolean (const char *xpath, xmlXPathContextPtr ctxt); char * virXPathString (const char *xpath, Index: src/xmlrpc.c =================================================================== RCS file: /data/cvs/libvirt/src/xmlrpc.c,v retrieving revision 1.6 diff -u -p -r1.6 xmlrpc.c --- src/xmlrpc.c 14 Feb 2007 15:40:54 -0000 1.6 +++ src/xmlrpc.c 26 Jun 2007 14:27:08 -0000 @@ -305,44 +305,44 @@ void xmlRpcValueFree(xmlRpcValuePtr valu free(value); } -void xmlRpcValueMarshal(xmlRpcValuePtr value, virBufferPtr buf, int indent) +void xmlRpcValueMarshal(xmlRpcValuePtr value, bufferPtr buf, int indent) { int i; xmlRpcValueDictElement *elem; - virBufferVSprintf(buf, "%*s<value>", indent, ""); + bufferVSprintf(buf, "%*s<value>", indent, ""); switch (value->kind) { case XML_RPC_ARRAY: - virBufferStrcat(buf, "<array><data>\n", NULL); + bufferStrcat(buf, "<array><data>\n", NULL); for (i = 0; i < value->value.array.n_elements; i++) xmlRpcValueMarshal(value->value.array.elements[i], buf, indent+2); - virBufferVSprintf(buf, "%*s</data></array>", indent, ""); + bufferVSprintf(buf, "%*s</data></array>", indent, ""); break; case XML_RPC_STRUCT: - virBufferStrcat(buf, "<struct>\n", NULL); + bufferStrcat(buf, "<struct>\n", NULL); indent += 2; for (elem = value->value.dict.root; elem; elem = elem->next) { - virBufferVSprintf(buf, "%*s<member>\n", indent, ""); - virBufferVSprintf(buf, "%*s<name>%s</name>\n", + bufferVSprintf(buf, "%*s<member>\n", indent, ""); + bufferVSprintf(buf, "%*s<name>%s</name>\n", indent + 2, "", elem->name); xmlRpcValueMarshal(elem->value, buf, indent + 2); - virBufferVSprintf(buf, "%*s</member>\n", indent, ""); + bufferVSprintf(buf, "%*s</member>\n", indent, ""); } indent -= 2; - virBufferVSprintf(buf, "%*s</struct>", indent, ""); + bufferVSprintf(buf, "%*s</struct>", indent, ""); break; case XML_RPC_INTEGER: - virBufferVSprintf(buf, "<int>%d</int>", value->value.integer); + bufferVSprintf(buf, "<int>%d</int>", value->value.integer); break; case XML_RPC_DOUBLE: - virBufferVSprintf(buf, "<double>%f</double>", value->value.real); + bufferVSprintf(buf, "<double>%f</double>", value->value.real); break; case XML_RPC_BOOLEAN: if (value->value.boolean) i = 1; else i = 0; - virBufferVSprintf(buf, "<boolean>%d</boolean>", i); + bufferVSprintf(buf, "<boolean>%d</boolean>", i); break; case XML_RPC_DATE_TIME: /* FIXME */ @@ -353,37 +353,37 @@ void xmlRpcValueMarshal(xmlRpcValuePtr v TODO break; case XML_RPC_STRING: - virBufferStrcat(buf, + bufferStrcat(buf, "<string>", value->value.string, "</string>", NULL); break; case XML_RPC_NIL: - virBufferStrcat(buf, "<nil> </nil>", NULL); + bufferStrcat(buf, "<nil> </nil>", NULL); break; } - virBufferStrcat(buf, "</value>\n", NULL); + bufferStrcat(buf, "</value>\n", NULL); } -virBufferPtr xmlRpcMarshalRequest(const char *request, +bufferPtr xmlRpcMarshalRequest(const char *request, int argc, xmlRpcValuePtr *argv) { - virBufferPtr buf; + bufferPtr buf; int i; - buf = virBufferNew(1024); + buf = bufferNew(1024); - virBufferStrcat(buf, + bufferStrcat(buf, "<?xml version=\"1.0\"?>\n" "<methodCall>\n" " <methodName>", request, "</methodName>\n" " <params>\n", NULL); for (i = 0; i < argc; i++) { - virBufferStrcat(buf, + bufferStrcat(buf, " <param>\n", NULL); xmlRpcValueMarshal(argv[i], buf, 6); - virBufferStrcat(buf, + bufferStrcat(buf, " </param>\n", NULL); } - virBufferStrcat(buf, + bufferStrcat(buf, " </params>\n" "</methodCall>\n", NULL); return buf; @@ -564,7 +564,7 @@ int xmlRpcCall(xmlRpcContextPtr context, va_list ap; int argc; xmlRpcValuePtr *argv; - virBufferPtr buf; + bufferPtr buf; char *ret; xmlDocPtr xml; xmlNodePtr node; @@ -591,7 +591,7 @@ int xmlRpcCall(xmlRpcContextPtr context, ret = xmlRpcCallRaw(context->uri, buf->content); - virBufferFree(buf); + bufferFree(buf); if (!ret) return -1; Index: src/xmlrpc.h =================================================================== RCS file: /data/cvs/libvirt/src/xmlrpc.h,v retrieving revision 1.2 diff -u -p -r1.2 xmlrpc.h --- src/xmlrpc.h 9 May 2006 15:35:46 -0000 1.2 +++ src/xmlrpc.h 26 Jun 2007 14:27:08 -0000 @@ -19,6 +19,7 @@ #include <time.h> #include <stdarg.h> +#include "buf.h" #include "xml.h" typedef enum _xmlRpcValueType xmlRpcValueType; @@ -89,12 +90,12 @@ struct _xmlRpcContext; xmlRpcValuePtr *xmlRpcArgvNew(const char *fmt, va_list ap, int *argc); void xmlRpcArgvFree(int argc, xmlRpcValuePtr *argv); -virBufferPtr xmlRpcMarshalRequest(const char *request, +bufferPtr xmlRpcMarshalRequest(const char *request, int argc, xmlRpcValuePtr *argv); xmlRpcValuePtr xmlRpcUnmarshalResponse(xmlNodePtr node, bool *is_fault); -void xmlRpcValueMarshal(xmlRpcValuePtr value, virBufferPtr buf, int indent); +void xmlRpcValueMarshal(xmlRpcValuePtr value, bufferPtr buf, int indent); xmlRpcValuePtr xmlRpcValueUnmarshal(xmlNodePtr node); Index: tests/Makefile.am =================================================================== RCS file: /data/cvs/libvirt/tests/Makefile.am,v retrieving revision 1.19 diff -u -p -r1.19 Makefile.am --- tests/Makefile.am 29 May 2007 14:44:15 -0000 1.19 +++ tests/Makefile.am 26 Jun 2007 14:27:08 -0000 @@ -15,6 +15,7 @@ INCLUDES = \ -I$(top_builddir)/src \ -I$(top_srcdir)/include \ -I$(top_srcdir)/src \ + -I$(top_srcdir)/lib \ @LIBXML_CFLAGS@ \ -D_XOPEN_SOURCE=600 -D_POSIX_C_SOURCE=199506L \ -DGETTEXT_PACKAGE=\"$(PACKAGE)\" \ @@ -46,8 +47,8 @@ valgrind: xmlrpctest_SOURCES = \ xmlrpctest.c \ testutils.c testutils.h \ - $(top_builddir)/src/xmlrpc.c \ - $(top_builddir)/src/xmlrpc.h + @top_srcdir@/src/xmlrpc.c \ + @top_srcdir@/src/xmlrpc.h xmlrpctest_LDFLAGS = xmlrpctest_LDADD = $(LDADDS) Index: tests/xmlrpctest.c =================================================================== RCS file: /data/cvs/libvirt/tests/xmlrpctest.c,v retrieving revision 1.3 diff -u -p -r1.3 xmlrpctest.c --- tests/xmlrpctest.c 26 Jun 2006 15:02:19 -0000 1.3 +++ tests/xmlrpctest.c 26 Jun 2007 14:27:08 -0000 @@ -20,6 +20,7 @@ #include <libxml/xpath.h> #include "libvirt/libvirt.h" +#include "buf.h" #include "xml.h" #include "xmlrpc.h" @@ -57,12 +58,12 @@ testMethodPlusDOUBLE(void *data) return retval==(10.1234+10.1234) ? 0 : -1; } -static virBufferPtr +static bufferPtr marshalRequest(const char *fmt, ...) { int argc; xmlRpcValuePtr *argv; - virBufferPtr buf; + bufferPtr buf; va_list ap; va_start(ap, fmt); @@ -132,14 +133,14 @@ testMarshalRequestINT(void *data) int num = INT_MAX; int ret = 0; int check = data ? *((int *)data) : 0; - virBufferPtr buf = marshalRequest("i", num); + bufferPtr buf = marshalRequest("i", num); if (check) ret = checkRequestValue(buf->content, "number(/methodCall/params/param[1]/value/int)", XML_RPC_INTEGER, (void *) &num); - virBufferFree(buf); + bufferFree(buf); return ret; } @@ -149,13 +150,13 @@ testMarshalRequestSTRING(void *data ATTR const char *str = "This library will be really sexy."; int ret = 0; int check = data ? *((int *)data) : 0; - virBufferPtr buf = marshalRequest("s", str); + bufferPtr buf = marshalRequest("s", str); if (check) ret = checkRequestValue(buf->content, "string(/methodCall/params/param[1]/value/string)", XML_RPC_STRING, (void *) str); - virBufferFree(buf); + bufferFree(buf); return ret; } @@ -165,40 +166,40 @@ testMarshalRequestDOUBLE(void *data) double num = 123456789.123; int ret = 0; int check = data ? *((int *)data) : 0; - virBufferPtr buf = marshalRequest("f", num); + bufferPtr buf = marshalRequest("f", num); if (check) ret = checkRequestValue(buf->content, "number(/methodCall/params/param[1]/value/double)", XML_RPC_DOUBLE, (void *) &num); - virBufferFree(buf); + bufferFree(buf); return ret; } static int testBufferStrcat(void *data ATTRIBUTE_UNUSED) { - virBufferPtr buf = virBufferNew(1000*32); /* don't waste time with realloc */ + bufferPtr buf = bufferNew(1000*32); /* don't waste time with realloc */ int i; for (i=0; i < 1000; i++) - virBufferStrcat(buf, "My name is ", "libvirt", ".\n", NULL); + bufferStrcat(buf, "My name is ", "libvirt", ".\n", NULL); - virBufferFree(buf); + bufferFree(buf); return 0; } static int testBufferVSprintf(void *data ATTRIBUTE_UNUSED) { - virBufferPtr buf = virBufferNew(1000*32); /* don't waste time with realloc */ + bufferPtr buf = bufferNew(1000*32); /* don't waste time with realloc */ int i; for (i=0; i < 1000; i++) - virBufferVSprintf(buf, "My name is %s.\n", "libvirt"); + bufferVSprintf(buf, "My name is %s.\n", "libvirt"); - virBufferFree(buf); + bufferFree(buf); return 0; }
Attachment:
smime.p7s
Description: S/MIME Cryptographic Signature