1
0
mirror of https://gitlab.com/libvirt/libvirt.git synced 2025-03-20 07:59:00 +00:00

Change virBuffer API to prevent common usage errors. Update all users of APIs

This commit is contained in:
Daniel P. Berrange 2008-04-28 15:14:59 +00:00
parent 7301131e42
commit 642b26fab2
18 changed files with 913 additions and 1166 deletions

View File

@ -1,3 +1,14 @@
Mon Apr 28 10:24:22 EST 2008 Daniel P. Berrange <berrange@redhat.com>
* src/buf.c, src/buf.h: Change API to move errors checks to
a single place
* src/capabilities.c, src/conf.c, src/lxc_conf.c, src/qemu_conf.c,
src/qparams.c, src/storage_conf.c, src/test.c, src/virsh.c,
src/xend_internal.c, src/xm_internal.c, src/xml.c, src/xmlrpc.c,
src/xmlrpc.h, tests/xmlrpctest.c: Update to conform with new
virBuffer API
* src/libvirt_sym.version: Add new virBuffer APIs for virsh
Mon Apr 28 16:11:22 CEST 2008 Jim Meyering <meyering@redhat.com> Mon Apr 28 16:11:22 CEST 2008 Jim Meyering <meyering@redhat.com>
fix typo in bootstrap script fix typo in bootstrap script

263
src/buf.c
View File

@ -18,8 +18,37 @@
#include <stdarg.h> #include <stdarg.h>
#include <ctype.h> #include <ctype.h>
#define __VIR_BUFFER_C__
#include "buf.h" #include "buf.h"
/* If adding more fields, ensure to edit buf.h to match
the number of fields */
struct _virBuffer {
unsigned int size;
unsigned int use;
unsigned int error;
char *content;
};
/**
* virBufferFail
* @buf: the buffer
*
* Mark the buffer has having failed a memory allocation,
* freeing the content and setting the error flag.
*/
static void
virBufferNoMemory(const virBufferPtr buf)
{
free(buf->content);
buf->content = NULL;
buf->size = 0;
buf->use = 0;
buf->error = 1;
}
/** /**
* virBufferGrow: * virBufferGrow:
* @buf: the buffer * @buf: the buffer
@ -27,7 +56,7 @@
* *
* Grow the available space of a buffer to at least @len bytes. * Grow the available space of a buffer to at least @len bytes.
* *
* Returns the new available space or -1 in case of error * Returns zero on success or -1 on error
*/ */
static int static int
virBufferGrow(virBufferPtr buf, unsigned int len) virBufferGrow(virBufferPtr buf, unsigned int len)
@ -35,18 +64,22 @@ virBufferGrow(virBufferPtr buf, unsigned int len)
int size; int size;
char *newbuf; char *newbuf;
if (buf == NULL) if (buf->error)
return (-1); return -1;
if (len + buf->use < buf->size)
return (0); if ((len + buf->use) < buf->size)
return 0;
size = buf->use + len + 1000; size = buf->use + len + 1000;
newbuf = realloc(buf->content, size); newbuf = realloc(buf->content, size);
if (newbuf == NULL) return -1; if (newbuf == NULL) {
virBufferNoMemory(buf);
return -1;
}
buf->content = newbuf; buf->content = newbuf;
buf->size = size; buf->size = size;
return (buf->size - buf->use); return 0;
} }
/** /**
@ -58,33 +91,29 @@ virBufferGrow(virBufferPtr buf, unsigned int len)
* Add a string range to an XML buffer. if len == -1, the length of * Add a string range to an XML buffer. if len == -1, the length of
* str is recomputed to the full string. * str is recomputed to the full string.
* *
* Returns 0 successful, -1 in case of internal or API error.
*/ */
int void
__virBufferAdd(virBufferPtr buf, const char *str, int len) __virBufferAdd(const virBufferPtr buf, const char *str, int len)
{ {
unsigned int needSize; unsigned int needSize;
if ((str == NULL) || (buf == NULL)) { if ((str == NULL) || (buf == NULL) || (len == 0))
return -1; return;
}
if (len == 0) if (buf->error)
return 0; return;
if (len < 0) if (len < 0)
len = strlen(str); len = strlen(str);
needSize = buf->use + len + 2; needSize = buf->use + len + 2;
if (needSize > buf->size) { if (needSize > buf->size &&
if (!virBufferGrow(buf, needSize - buf->use)) { virBufferGrow(buf, needSize - buf->use) < 0)
return (-1); return;
}
}
memcpy (&buf->content[buf->use], str, len); memcpy (&buf->content[buf->use], str, len);
buf->use += len; buf->use += len;
buf->content[buf->use] = 0; buf->content[buf->use] = '\0';
return (0);
} }
/** /**
@ -94,89 +123,85 @@ __virBufferAdd(virBufferPtr buf, const char *str, int len)
* *
* Add a single character 'c' to a buffer. * Add a single character 'c' to a buffer.
* *
* Returns 0 if successful, -1 in the case of error.
*/ */
int void
__virBufferAddChar (virBufferPtr buf, char c) __virBufferAddChar (virBufferPtr buf, char c)
{ {
unsigned int needSize; unsigned int needSize;
if (buf == NULL) if (buf == NULL)
return -1; return;
if (buf->error)
return;
needSize = buf->use + 2; needSize = buf->use + 2;
if (needSize > buf->size) if (needSize > buf->size &&
if (!virBufferGrow (buf, needSize - buf->use)) virBufferGrow (buf, needSize - buf->use) < 0)
return -1; return;
buf->content[buf->use++] = c; buf->content[buf->use++] = c;
buf->content[buf->use] = 0; buf->content[buf->use] = '\0';
return 0;
} }
/** /**
* virBufferNew: * virBufferContentAndReset:
* @size: creation size in bytes
*
* Creates a new buffer
*
* Returns a pointer to the buffer or NULL in case of error
*/
virBufferPtr
virBufferNew(unsigned int size)
{
virBufferPtr 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;
}
/**
* virBufferFree:
* @buf: the buffer to deallocate
*
* Free the set of resources used by a buffer.
*/
void
virBufferFree(virBufferPtr buf)
{
if (buf) {
free(buf->content);
free(buf);
}
}
/**
* virBufferContentAndFree:
* @buf: Buffer * @buf: Buffer
* *
* Get the content from the buffer and free (only) the buffer structure. * Get the content from the buffer and free (only) the buffer structure.
* The caller owns the returned string & should free it when no longer
* required. The buffer object is reset to its initial state.
* *
* Returns the buffer content or NULL in case of error. * Returns the buffer content or NULL in case of error.
*/ */
char * char *
virBufferContentAndFree (virBufferPtr buf) __virBufferContentAndReset(const virBufferPtr buf)
{ {
char *content; char *str;
if (buf == NULL) if (buf == NULL)
return(NULL); return NULL;
content = buf->content; if (buf->error) {
if (content != NULL) memset(buf, 0, sizeof(*buf));
content[buf->use] = 0; return NULL;
}
free (buf); str = buf->content;
return(content); memset(buf, 0, sizeof(*buf));
return str;
}
/**
* virBufferError:
* @buf: the buffer
*
* Check to see if the buffer is in an error state due
* to failed memory allocation
*
* Return true if in error, 0 if normal
*/
int
__virBufferError(const virBufferPtr buf)
{
if (buf == NULL)
return 1;
return buf->error;
}
/**
* virBufferUse:
* @buf: the usage of the string in the buffer
*
* Return the string usage in bytes
*/
unsigned int
virBufferUse(const virBufferPtr buf)
{
if (buf == NULL)
return 0;
return buf->use;
} }
/** /**
@ -186,22 +211,22 @@ virBufferContentAndFree (virBufferPtr buf)
* @...: the variable list of arguments * @...: the variable list of arguments
* *
* Do a formatted print to an XML buffer. * Do a formatted print to an XML buffer.
*
* Returns 0 successful, -1 in case of internal or API error.
*/ */
int void
__virBufferVSprintf(virBufferPtr buf, const char *format, ...) __virBufferVSprintf(const virBufferPtr buf, const char *format, ...)
{ {
int size, count, grow_size; int size, count, grow_size;
va_list locarg, argptr; va_list locarg, argptr;
if ((format == NULL) || (buf == NULL)) { if ((format == NULL) || (buf == NULL))
return (-1); return;
}
if (buf->error)
return;
if (buf->size == 0 && if (buf->size == 0 &&
virBufferGrow(buf, 100) < 0) virBufferGrow(buf, 100) < 0)
return -1; return;
size = buf->size - buf->use - 1; size = buf->size - buf->use - 1;
va_start(argptr, format); va_start(argptr, format);
@ -210,17 +235,17 @@ __virBufferVSprintf(virBufferPtr buf, const char *format, ...)
locarg)) < 0) || (count >= size - 1)) { locarg)) < 0) || (count >= size - 1)) {
buf->content[buf->use] = 0; buf->content[buf->use] = 0;
va_end(locarg); va_end(locarg);
grow_size = (count > 1000) ? count : 1000; grow_size = (count > 1000) ? count : 1000;
if (virBufferGrow(buf, grow_size) < 0) { if (virBufferGrow(buf, grow_size) < 0)
return (-1); return;
}
size = buf->size - buf->use - 1; size = buf->size - buf->use - 1;
va_copy(locarg, argptr); va_copy(locarg, argptr);
} }
va_end(locarg); va_end(locarg);
buf->use += count; buf->use += count;
buf->content[buf->use] = 0; buf->content[buf->use] = '\0';
return (0);
} }
/** /**
@ -231,25 +256,27 @@ __virBufferVSprintf(virBufferPtr buf, const char *format, ...)
* *
* Do a formatted print with a single string to an XML buffer. The string * Do a formatted print with a single string to an XML buffer. The string
* is escaped to avoid generating a not well-formed XML instance. * is escaped to avoid generating a not well-formed XML instance.
*
* Returns 0 successful, -1 in case of internal or API error.
*/ */
int void
virBufferEscapeString(virBufferPtr buf, const char *format, const char *str) virBufferEscapeString(const virBufferPtr buf, const char *format, const char *str)
{ {
int size, count, len, grow_size; int size, count, len, grow_size;
char *escaped, *out; char *escaped, *out;
const char *cur; const char *cur;
if ((format == NULL) || (buf == NULL) || (str == NULL)) { if ((format == NULL) || (buf == NULL) || (str == NULL))
return (-1); return;
}
if (buf->error)
return;
len = strlen(str); len = strlen(str);
escaped = malloc(5 * len + 1); escaped = malloc(5 * len + 1);
if (escaped == NULL) { if (escaped == NULL) {
return (-1); virBufferNoMemory(buf);
return;
} }
cur = str; cur = str;
out = escaped; out = escaped;
while (*cur != 0) { while (*cur != 0) {
@ -290,14 +317,13 @@ virBufferEscapeString(virBufferPtr buf, const char *format, const char *str)
grow_size = (count > 1000) ? count : 1000; grow_size = (count > 1000) ? count : 1000;
if (virBufferGrow(buf, grow_size) < 0) { if (virBufferGrow(buf, grow_size) < 0) {
free(escaped); free(escaped);
return (-1); return;
} }
size = buf->size - buf->use - 1; size = buf->size - buf->use - 1;
} }
buf->use += count; buf->use += count;
buf->content[buf->use] = 0; buf->content[buf->use] = '\0';
free(escaped); free(escaped);
return (0);
} }
/** /**
@ -308,10 +334,8 @@ virBufferEscapeString(virBufferPtr buf, const char *format, const char *str)
* Append the string to the buffer. The string will be URI-encoded * Append the string to the buffer. The string will be URI-encoded
* during the append (ie any non alpha-numeric characters are replaced * during the append (ie any non alpha-numeric characters are replaced
* with '%xx' hex sequences). * with '%xx' hex sequences).
*
* Returns 0 successful, -1 in case of internal or API error.
*/ */
int void
virBufferURIEncodeString (virBufferPtr buf, const char *str) virBufferURIEncodeString (virBufferPtr buf, const char *str)
{ {
int grow_size = 0; int grow_size = 0;
@ -319,6 +343,12 @@ virBufferURIEncodeString (virBufferPtr buf, const char *str)
unsigned char uc; unsigned char uc;
const char *hex = "0123456789abcdef"; const char *hex = "0123456789abcdef";
if ((buf == NULL) || (str == NULL))
return;
if (buf->error)
return;
for (p = str; *p; ++p) { for (p = str; *p; ++p) {
/* This may not work on EBCDIC. */ /* This may not work on EBCDIC. */
if ((*p >= 'a' && *p <= 'z') || if ((*p >= 'a' && *p <= 'z') ||
@ -329,8 +359,8 @@ virBufferURIEncodeString (virBufferPtr buf, const char *str)
grow_size += 3; /* %ab */ grow_size += 3; /* %ab */
} }
if (virBufferGrow (buf, grow_size) == -1) if (virBufferGrow (buf, grow_size) < 0)
return -1; return;
for (p = str; *p; ++p) { for (p = str; *p; ++p) {
/* This may not work on EBCDIC. */ /* This may not work on EBCDIC. */
@ -347,7 +377,6 @@ virBufferURIEncodeString (virBufferPtr buf, const char *str)
} }
buf->content[buf->use] = '\0'; buf->content[buf->use] = '\0';
return 0;
} }
/** /**
@ -356,15 +385,16 @@ virBufferURIEncodeString (virBufferPtr buf, const char *str)
* @...: the variable list of strings, the last argument must be NULL * @...: the variable list of strings, the last argument must be NULL
* *
* Concatenate strings to an XML buffer. * Concatenate strings to an XML buffer.
*
* Returns 0 successful, -1 in case of internal or API error.
*/ */
int void
virBufferStrcat(virBufferPtr buf, ...) virBufferStrcat(virBufferPtr buf, ...)
{ {
va_list ap; va_list ap;
char *str; char *str;
if (buf->error)
return;
va_start(ap, buf); va_start(ap, buf);
while ((str = va_arg(ap, char *)) != NULL) { while ((str = va_arg(ap, char *)) != NULL) {
@ -372,13 +402,12 @@ virBufferStrcat(virBufferPtr buf, ...)
unsigned int needSize = buf->use + len + 2; unsigned int needSize = buf->use + len + 2;
if (needSize > buf->size) { if (needSize > buf->size) {
if (!virBufferGrow(buf, needSize - buf->use)) if (virBufferGrow(buf, needSize - buf->use) < 0)
return -1; return;
} }
memcpy(&buf->content[buf->use], str, len); memcpy(&buf->content[buf->use], str, len);
buf->use += len; buf->use += len;
buf->content[buf->use] = 0; buf->content[buf->use] = 0;
} }
va_end(ap); va_end(ap);
return 0;
} }

View File

@ -20,22 +20,30 @@
*/ */
typedef struct _virBuffer virBuffer; typedef struct _virBuffer virBuffer;
typedef virBuffer *virBufferPtr; 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); #ifndef __VIR_BUFFER_C__
void virBufferFree(virBufferPtr buf); #define VIR_BUFFER_INITIALIZER { 0, 0, 0, NULL }
char *virBufferContentAndFree(virBufferPtr buf);
int __virBufferAdd(virBufferPtr buf, const char *str, int len); /* This struct must be kept in syn with the real struct
int __virBufferAddChar(virBufferPtr buf, char c); in the buf.c impl file */
int __virBufferVSprintf(virBufferPtr buf, const char *format, ...) struct _virBuffer {
unsigned int a;
unsigned int b;
unsigned int c;
char *d;
};
#endif
char *__virBufferContentAndReset(const virBufferPtr buf);
int __virBufferError(const virBufferPtr buf);
unsigned int virBufferUse(const virBufferPtr buf);
void __virBufferAdd(const virBufferPtr buf, const char *str, int len);
void __virBufferAddChar(const virBufferPtr buf, char c);
void __virBufferVSprintf(const virBufferPtr buf, const char *format, ...)
ATTRIBUTE_FORMAT(printf, 2, 3); ATTRIBUTE_FORMAT(printf, 2, 3);
int virBufferStrcat(virBufferPtr buf, ...); void virBufferStrcat(const virBufferPtr buf, ...);
int virBufferEscapeString(virBufferPtr buf, const char *format, const char *str); void virBufferEscapeString(const virBufferPtr buf, const char *format, const char *str);
int virBufferURIEncodeString (virBufferPtr buf, const char *str); void virBufferURIEncodeString (const virBufferPtr buf, const char *str);
#define virBufferAddLit(buf_, literal_string_) \ #define virBufferAddLit(buf_, literal_string_) \
__virBufferAdd (buf_, "" literal_string_ "", sizeof literal_string_ - 1) __virBufferAdd (buf_, "" literal_string_ "", sizeof literal_string_ - 1)
@ -44,4 +52,7 @@ int virBufferURIEncodeString (virBufferPtr buf, const char *str);
#define virBufferAddChar(b,c) __virBufferAddChar((b),(c)) #define virBufferAddChar(b,c) __virBufferAddChar((b),(c))
#define virBufferVSprintf(b,f,...) __virBufferVSprintf((b),(f), __VA_ARGS__) #define virBufferVSprintf(b,f,...) __virBufferVSprintf((b),(f), __VA_ARGS__)
#define virBufferContentAndReset(b) __virBufferContentAndReset((b))
#define virBufferError(b) __virBufferError((b))
#endif /* __VIR_BUFFER_H__ */ #endif /* __VIR_BUFFER_H__ */

View File

@ -521,172 +521,127 @@ virCapabilitiesDefaultGuestEmulator(virCapsPtr caps,
char * char *
virCapabilitiesFormatXML(virCapsPtr caps) virCapabilitiesFormatXML(virCapsPtr caps)
{ {
virBuffer xml = { NULL, 0, 0 }; virBuffer xml = VIR_BUFFER_INITIALIZER;
int i, j, k; int i, j, k;
if (virBufferAddLit(&xml, "<capabilities>\n\n") < 0) virBufferAddLit(&xml, "<capabilities>\n\n");
goto no_memory; virBufferAddLit(&xml, " <host>\n");
if (virBufferAddLit(&xml, " <host>\n") < 0) virBufferAddLit(&xml, " <cpu>\n");
goto no_memory; virBufferVSprintf(&xml, " <arch>%s</arch>\n",
if (virBufferAddLit(&xml, " <cpu>\n") < 0) caps->host.arch);
goto no_memory;
if (virBufferVSprintf(&xml, " <arch>%s</arch>\n",
caps->host.arch) < 0)
goto no_memory;
if (caps->host.nfeatures) { if (caps->host.nfeatures) {
if (virBufferAddLit(&xml, " <features>\n") < 0) virBufferAddLit(&xml, " <features>\n");
goto no_memory;
for (i = 0 ; i < caps->host.nfeatures ; i++) { for (i = 0 ; i < caps->host.nfeatures ; i++) {
if (virBufferVSprintf(&xml, " <%s/>\n", virBufferVSprintf(&xml, " <%s/>\n",
caps->host.features[i]) <0) caps->host.features[i]);
goto no_memory;
} }
if (virBufferAddLit(&xml, " </features>\n") < 0) virBufferAddLit(&xml, " </features>\n");
goto no_memory;
} }
if (virBufferAddLit(&xml, " </cpu>\n") < 0) virBufferAddLit(&xml, " </cpu>\n");
goto no_memory;
if (caps->host.offlineMigrate) { if (caps->host.offlineMigrate) {
if (virBufferAddLit(&xml, " <migration_features>\n") < 0) virBufferAddLit(&xml, " <migration_features>\n");
goto no_memory; if (caps->host.liveMigrate)
if (caps->host.liveMigrate && virBufferAddLit(&xml, " <live/>\n");
virBufferAddLit(&xml, " <live/>\n") < 0)
goto no_memory;
if (caps->host.nmigrateTrans) { if (caps->host.nmigrateTrans) {
if (virBufferAddLit(&xml, " <uri_transports>\n") < 0) virBufferAddLit(&xml, " <uri_transports>\n");
goto no_memory;
for (i = 0 ; i < caps->host.nmigrateTrans ; i++) { for (i = 0 ; i < caps->host.nmigrateTrans ; i++) {
if (virBufferVSprintf(&xml, " <uri_transport>%s</uri_transport>\n", virBufferVSprintf(&xml, " <uri_transport>%s</uri_transport>\n",
caps->host.migrateTrans[i]) < 0) caps->host.migrateTrans[i]);
goto no_memory;
} }
if (virBufferAddLit(&xml, " </uri_transports>\n") < 0) virBufferAddLit(&xml, " </uri_transports>\n");
goto no_memory;
} }
if (virBufferAddLit(&xml, " </migration_features>\n") < 0) virBufferAddLit(&xml, " </migration_features>\n");
goto no_memory;
} }
if (caps->host.nnumaCell) { if (caps->host.nnumaCell) {
if (virBufferAddLit(&xml, " <topology>\n") < 0) virBufferAddLit(&xml, " <topology>\n");
goto no_memory; virBufferVSprintf(&xml, " <cells num='%d'>\n",
if (virBufferVSprintf(&xml, " <cells num='%d'>\n", caps->host.nnumaCell);
caps->host.nnumaCell) < 0)
goto no_memory;
for (i = 0 ; i < caps->host.nnumaCell ; i++) { for (i = 0 ; i < caps->host.nnumaCell ; i++) {
if (virBufferVSprintf(&xml, " <cell id='%d'>\n", virBufferVSprintf(&xml, " <cell id='%d'>\n",
caps->host.numaCell[i]->num) < 0) caps->host.numaCell[i]->num);
goto no_memory; virBufferVSprintf(&xml, " <cpus num='%d'>\n",
if (virBufferVSprintf(&xml, " <cpus num='%d'>\n", caps->host.numaCell[i]->ncpus);
caps->host.numaCell[i]->ncpus) < 0)
goto no_memory;
for (j = 0 ; j < caps->host.numaCell[i]->ncpus ; j++) for (j = 0 ; j < caps->host.numaCell[i]->ncpus ; j++)
if (virBufferVSprintf(&xml, " <cpu id='%d'/>\n", virBufferVSprintf(&xml, " <cpu id='%d'/>\n",
caps->host.numaCell[i]->cpus[j]) < 0) caps->host.numaCell[i]->cpus[j]);
goto no_memory; virBufferAddLit(&xml, " </cpus>\n");
if (virBufferAddLit(&xml, " </cpus>\n") < 0) virBufferAddLit(&xml, " </cell>\n");
goto no_memory;
if (virBufferAddLit(&xml, " </cell>\n") < 0)
goto no_memory;
} }
if (virBufferAddLit(&xml, " </cells>\n") < 0) virBufferAddLit(&xml, " </cells>\n");
goto no_memory; virBufferAddLit(&xml, " </topology>\n");
if (virBufferAddLit(&xml, " </topology>\n") < 0)
goto no_memory;
} }
if (virBufferAddLit(&xml, " </host>\n\n") < 0) virBufferAddLit(&xml, " </host>\n\n");
goto no_memory;
for (i = 0 ; i < caps->nguests ; i++) { for (i = 0 ; i < caps->nguests ; i++) {
if (virBufferAddLit(&xml, " <guest>\n") < 0) virBufferAddLit(&xml, " <guest>\n");
goto no_memory; virBufferVSprintf(&xml, " <os_type>%s</os_type>\n",
if (virBufferVSprintf(&xml, " <os_type>%s</os_type>\n", caps->guests[i]->ostype);
caps->guests[i]->ostype) < 0) virBufferVSprintf(&xml, " <arch name='%s'>\n",
goto no_memory; caps->guests[i]->arch.name);
if (virBufferVSprintf(&xml, " <arch name='%s'>\n", virBufferVSprintf(&xml, " <wordsize>%d</wordsize>\n",
caps->guests[i]->arch.name) < 0) caps->guests[i]->arch.wordsize);
goto no_memory; if (caps->guests[i]->arch.defaultInfo.emulator)
if (virBufferVSprintf(&xml, " <wordsize>%d</wordsize>\n",
caps->guests[i]->arch.wordsize) < 0)
goto no_memory;
if (caps->guests[i]->arch.defaultInfo.emulator &&
virBufferVSprintf(&xml, " <emulator>%s</emulator>\n", virBufferVSprintf(&xml, " <emulator>%s</emulator>\n",
caps->guests[i]->arch.defaultInfo.emulator) < 0) caps->guests[i]->arch.defaultInfo.emulator);
goto no_memory; if (caps->guests[i]->arch.defaultInfo.loader)
if (caps->guests[i]->arch.defaultInfo.loader && virBufferVSprintf(&xml, " <loader>%s</loader>\n",
virBufferVSprintf(&xml, " <loader>%s</loader>\n", caps->guests[i]->arch.defaultInfo.loader);
caps->guests[i]->arch.defaultInfo.loader) < 0)
goto no_memory;
for (j = 0 ; j < caps->guests[i]->arch.defaultInfo.nmachines ; j++) { for (j = 0 ; j < caps->guests[i]->arch.defaultInfo.nmachines ; j++) {
if (virBufferVSprintf(&xml, " <machine>%s</machine>\n", virBufferVSprintf(&xml, " <machine>%s</machine>\n",
caps->guests[i]->arch.defaultInfo.machines[j]) < 0) caps->guests[i]->arch.defaultInfo.machines[j]);
goto no_memory;
} }
for (j = 0 ; j < caps->guests[i]->arch.ndomains ; j++) { for (j = 0 ; j < caps->guests[i]->arch.ndomains ; j++) {
if (virBufferVSprintf(&xml, " <domain type='%s'>\n", virBufferVSprintf(&xml, " <domain type='%s'>\n",
caps->guests[i]->arch.domains[j]->type) < 0) caps->guests[i]->arch.domains[j]->type);
goto no_memory; if (caps->guests[i]->arch.domains[j]->info.emulator)
if (caps->guests[i]->arch.domains[j]->info.emulator &&
virBufferVSprintf(&xml, " <emulator>%s</emulator>\n", virBufferVSprintf(&xml, " <emulator>%s</emulator>\n",
caps->guests[i]->arch.domains[j]->info.emulator) < 0) caps->guests[i]->arch.domains[j]->info.emulator);
goto no_memory; if (caps->guests[i]->arch.domains[j]->info.loader)
if (caps->guests[i]->arch.domains[j]->info.loader &&
virBufferVSprintf(&xml, " <loader>%s</loader>\n", virBufferVSprintf(&xml, " <loader>%s</loader>\n",
caps->guests[i]->arch.domains[j]->info.loader) < 0) caps->guests[i]->arch.domains[j]->info.loader);
goto no_memory;
for (k = 0 ; k < caps->guests[i]->arch.domains[j]->info.nmachines ; k++) { for (k = 0 ; k < caps->guests[i]->arch.domains[j]->info.nmachines ; k++) {
if (virBufferVSprintf(&xml, " <machine>%s</machine>\n", virBufferVSprintf(&xml, " <machine>%s</machine>\n",
caps->guests[i]->arch.domains[j]->info.machines[k]) < 0) caps->guests[i]->arch.domains[j]->info.machines[k]);
goto no_memory;
} }
if (virBufferAddLit(&xml, " </domain>\n") < 0) virBufferAddLit(&xml, " </domain>\n");
goto no_memory;
} }
if (virBufferAddLit(&xml, " </arch>\n") < 0) virBufferAddLit(&xml, " </arch>\n");
goto no_memory;
if (caps->guests[i]->nfeatures) { if (caps->guests[i]->nfeatures) {
if (virBufferAddLit(&xml, " <features>\n") < 0) virBufferAddLit(&xml, " <features>\n");
goto no_memory;
for (j = 0 ; j < caps->guests[i]->nfeatures ; j++) { for (j = 0 ; j < caps->guests[i]->nfeatures ; j++) {
if (STREQ(caps->guests[i]->features[j]->name, "pae") || if (STREQ(caps->guests[i]->features[j]->name, "pae") ||
STREQ(caps->guests[i]->features[j]->name, "nonpae") || STREQ(caps->guests[i]->features[j]->name, "nonpae") ||
STREQ(caps->guests[i]->features[j]->name, "ia64_be")) { STREQ(caps->guests[i]->features[j]->name, "ia64_be")) {
if (virBufferVSprintf(&xml, " <%s/>\n", virBufferVSprintf(&xml, " <%s/>\n",
caps->guests[i]->features[j]->name) < 0) caps->guests[i]->features[j]->name);
goto no_memory;
} else { } else {
if (virBufferVSprintf(&xml, " <%s default='%s' toggle='%s'/>\n", virBufferVSprintf(&xml, " <%s default='%s' toggle='%s'/>\n",
caps->guests[i]->features[j]->name, caps->guests[i]->features[j]->name,
caps->guests[i]->features[j]->defaultOn ? "on" : "off", caps->guests[i]->features[j]->defaultOn ? "on" : "off",
caps->guests[i]->features[j]->toggle ? "yes" : "no") < 0) caps->guests[i]->features[j]->toggle ? "yes" : "no");
goto no_memory;
} }
} }
if (virBufferAddLit(&xml, " </features>\n") < 0) virBufferAddLit(&xml, " </features>\n");
goto no_memory;
} }
virBufferAddLit(&xml, " </guest>\n\n");
if (virBufferAddLit(&xml, " </guest>\n\n") < 0)
goto no_memory;
} }
if (virBufferAddLit(&xml, "</capabilities>\n") < 0) virBufferAddLit(&xml, "</capabilities>\n");
goto no_memory;
return xml.content; if (virBufferError(&xml))
return NULL;
no_memory: return virBufferContentAndReset(&xml);
free(xml.content);
return NULL;
} }

View File

@ -877,43 +877,45 @@ __virConfSetValue (virConfPtr conf,
int int
__virConfWriteFile(const char *filename, virConfPtr conf) __virConfWriteFile(const char *filename, virConfPtr conf)
{ {
virBufferPtr buf; virBuffer buf = VIR_BUFFER_INITIALIZER;
virConfEntryPtr cur; virConfEntryPtr cur;
int ret; int ret;
int fd; int fd;
char *content;
unsigned int use;
if (conf == NULL) if (conf == NULL)
return(-1); return(-1);
buf = virBufferNew(500);
if (buf == NULL) {
virConfError(NULL, VIR_ERR_NO_MEMORY, _("failed to allocate buffer"), 0);
return(-1);
}
cur = conf->entries; cur = conf->entries;
while (cur != NULL) { while (cur != NULL) {
virConfSaveEntry(buf, cur); virConfSaveEntry(&buf, cur);
cur = cur->next; cur = cur->next;
} }
if (virBufferError(&buf)) {
virConfError(NULL, VIR_ERR_NO_MEMORY, _("allocate buffer"), 0);
return -1;
}
fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR ); fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR );
if (fd < 0) { if (fd < 0) {
virConfError(NULL, VIR_ERR_WRITE_FAILED, _("failed to open file"), 0); virConfError(NULL, VIR_ERR_WRITE_FAILED, _("failed to open file"), 0);
ret = -1; free(virBufferContentAndReset(&buf));
goto error; return -1;
} }
ret = safewrite(fd, buf->content, buf->use); use = virBufferUse(&buf);
content = virBufferContentAndReset(&buf);
ret = safewrite(fd, content, use);
free(content);
close(fd); close(fd);
if (ret != (int) buf->use) { if (ret != (int)use) {
virConfError(NULL, VIR_ERR_WRITE_FAILED, _("failed to save content"), 0); virConfError(NULL, VIR_ERR_WRITE_FAILED, _("failed to save content"), 0);
ret = -1; return -1;
goto error;
} }
error:
virBufferFree(buf); return ret;
return(ret);
} }
/** /**
@ -932,34 +934,35 @@ error:
int int
__virConfWriteMem(char *memory, int *len, virConfPtr conf) __virConfWriteMem(char *memory, int *len, virConfPtr conf)
{ {
virBufferPtr buf; virBuffer buf = VIR_BUFFER_INITIALIZER;
virConfEntryPtr cur; virConfEntryPtr cur;
int ret; char *content;
unsigned int use;
if ((memory == NULL) || (len == NULL) || (*len <= 0) || (conf == NULL)) if ((memory == NULL) || (len == NULL) || (*len <= 0) || (conf == NULL))
return(-1); return(-1);
buf = virBufferNew(500);
if (buf == NULL) {
virConfError(NULL, VIR_ERR_NO_MEMORY, _("failed to allocate buffer"), 0);
return(-1);
}
cur = conf->entries; cur = conf->entries;
while (cur != NULL) { while (cur != NULL) {
virConfSaveEntry(buf, cur); virConfSaveEntry(&buf, cur);
cur = cur->next; cur = cur->next;
} }
if ((int) buf->use >= *len) { if (virBufferError(&buf)) {
*len = buf->use; virConfError(NULL, VIR_ERR_NO_MEMORY, _("allocate buffer"), 0);
ret = -1; return -1;
goto error;
} }
memcpy(memory, buf->content, buf->use);
ret = buf->use; use = virBufferUse(&buf);
*len = buf->use; content = virBufferContentAndReset(&buf);
error:
virBufferFree(buf); if ((int)use >= *len) {
return(ret); *len = (int)use;
free(content);
return -1;
}
memcpy(memory, content, use);
free(content);
*len = use;
return use;
} }

View File

@ -184,6 +184,8 @@
__virBufferVSprintf; __virBufferVSprintf;
__virBufferAdd; __virBufferAdd;
__virBufferAddChar; __virBufferAddChar;
__virBufferContentAndReset;
__virBufferError;
__virMacAddrCompare; __virMacAddrCompare;

View File

@ -688,99 +688,49 @@ char *lxcGenerateXML(virConnectPtr conn,
lxc_vm_t *vm, lxc_vm_t *vm,
lxc_vm_def_t *def) lxc_vm_def_t *def)
{ {
virBufferPtr buf = 0; virBuffer buf = VIR_BUFFER_INITIALIZER;
unsigned char *uuid; unsigned char *uuid;
char uuidstr[VIR_UUID_STRING_BUFLEN]; char uuidstr[VIR_UUID_STRING_BUFLEN];
lxc_mount_t *mount; lxc_mount_t *mount;
buf = virBufferNew(LXC_MAX_XML_LENGTH); if (lxcIsActiveVM(vm))
if (!buf) { virBufferVSprintf(&buf, "<domain type='%s' id='%d'>\n",
goto no_memory; LXC_DOMAIN_TYPE, vm->def->id);
} else
virBufferVSprintf(&buf, "<domain type='%s'>\n",
LXC_DOMAIN_TYPE);
if (lxcIsActiveVM(vm)) { virBufferVSprintf(&buf, " <name>%s</name>\n", def->name);
if (virBufferVSprintf(buf, "<domain type='%s' id='%d'>\n",
LXC_DOMAIN_TYPE, vm->def->id) < 0) {
goto no_memory;
}
} else {
if (virBufferVSprintf(buf, "<domain type='%s'>\n",
LXC_DOMAIN_TYPE) < 0) {
goto no_memory;
}
}
if (virBufferVSprintf(buf, " <name>%s</name>\n", def->name) < 0) {
goto no_memory;
}
uuid = def->uuid; uuid = def->uuid;
virUUIDFormat(uuid, uuidstr); virUUIDFormat(uuid, uuidstr);
if (virBufferVSprintf(buf, " <uuid>%s</uuid>\n", uuidstr) < 0) { virBufferVSprintf(&buf, " <uuid>%s</uuid>\n", uuidstr);
goto no_memory; virBufferAddLit(&buf, " <os>\n");
} virBufferVSprintf(&buf, " <init>%s</init>\n", def->init);
virBufferAddLit(&buf, " </os>\n");
if (virBufferAddLit(buf, " <os>\n") < 0) { virBufferVSprintf(&buf, " <memory>%d</memory>\n", def->maxMemory);
goto no_memory; virBufferAddLit(&buf, " <devices>\n");
}
if (virBufferVSprintf(buf, " <init>%s</init>\n", def->init) < 0) {
goto no_memory;
}
if (virBufferAddLit(buf, " </os>\n") < 0) {
goto no_memory;
}
if (virBufferVSprintf(buf, " <memory>%d</memory>\n", def->maxMemory) < 0) {
goto no_memory;
}
if (virBufferAddLit(buf, " <devices>\n") < 0) {
goto no_memory;
}
/* loop adding mounts */ /* loop adding mounts */
for (mount = def->mounts; mount; mount = mount->next) { for (mount = def->mounts; mount; mount = mount->next) {
if (virBufferAddLit(buf, " <filesystem type='mount'>\n") < 0) { virBufferAddLit(&buf, " <filesystem type='mount'>\n");
goto no_memory; virBufferVSprintf(&buf, " <source dir='%s'/>\n",
} mount->source);
virBufferVSprintf(&buf, " <target dir='%s'/>\n",
if (virBufferVSprintf(buf, " <source dir='%s'/>\n", mount->target);
mount->source) < 0) { virBufferAddLit(&buf, " </filesystem>\n");
goto no_memory;
}
if (virBufferVSprintf(buf, " <target dir='%s'/>\n",
mount->target) < 0) {
goto no_memory;
}
if (virBufferAddLit(buf, " </filesystem>\n") < 0) {
goto no_memory;
}
} }
if (virBufferVSprintf(buf, " <console tty='%s'/>\n", def->tty) < 0) { virBufferVSprintf(&buf, " <console tty='%s'/>\n", def->tty);
goto no_memory; virBufferAddLit(&buf, " </devices>\n");
virBufferAddLit(&buf, "</domain>\n");
if (virBufferError(&buf)) {
lxcError(conn, NULL, VIR_ERR_NO_MEMORY,_("allocate buffer"));
return NULL;
} }
if (virBufferAddLit(buf, " </devices>\n") < 0) { return virBufferContentAndReset(&buf);
goto no_memory;
}
if (virBufferAddLit(buf, "</domain>\n") < 0) {
goto no_memory;
}
return virBufferContentAndFree(buf);
no_memory:
lxcError(conn, NULL, VIR_ERR_NO_MEMORY, "generateXml");
virBufferFree(buf);
return NULL;
} }
void lxcFreeVMDef(lxc_vm_def_t *vmdef) void lxcFreeVMDef(lxc_vm_def_t *vmdef)

View File

@ -3359,14 +3359,12 @@ static int qemudGenerateXMLChar(virBufferPtr buf,
if (STREQ(type, "console") && if (STREQ(type, "console") &&
dev->srcType == QEMUD_CHR_SRC_TYPE_PTY && dev->srcType == QEMUD_CHR_SRC_TYPE_PTY &&
dev->srcData.file.path[0] != '\0') { dev->srcData.file.path[0] != '\0') {
if (virBufferVSprintf(buf, " <%s type='%s' tty='%s'>\n", virBufferVSprintf(buf, " <%s type='%s' tty='%s'>\n",
type, types[dev->srcType], type, types[dev->srcType],
dev->srcData.file.path) < 0) dev->srcData.file.path);
return -1;
} else { } else {
if (virBufferVSprintf(buf, " <%s type='%s'>\n", virBufferVSprintf(buf, " <%s type='%s'>\n",
type, types[dev->srcType]) < 0) type, types[dev->srcType]);
return -1;
} }
switch (dev->srcType) { switch (dev->srcType) {
@ -3382,74 +3380,62 @@ static int qemudGenerateXMLChar(virBufferPtr buf,
case QEMUD_CHR_SRC_TYPE_PIPE: case QEMUD_CHR_SRC_TYPE_PIPE:
if (dev->srcType != QEMUD_CHR_SRC_TYPE_PTY || if (dev->srcType != QEMUD_CHR_SRC_TYPE_PTY ||
dev->srcData.file.path[0]) { dev->srcData.file.path[0]) {
if (virBufferVSprintf(buf, " <source path='%s'/>\n", virBufferVSprintf(buf, " <source path='%s'/>\n",
dev->srcData.file.path) < 0) dev->srcData.file.path);
return -1;
} }
break; break;
case QEMUD_CHR_SRC_TYPE_UDP: case QEMUD_CHR_SRC_TYPE_UDP:
if (dev->srcData.udp.bindService[0] != '\0' && if (dev->srcData.udp.bindService[0] != '\0' &&
dev->srcData.udp.bindHost[0] != '\0') { dev->srcData.udp.bindHost[0] != '\0') {
if (virBufferVSprintf(buf, " <source mode='bind' host='%s' service='%s'/>\n", virBufferVSprintf(buf, " <source mode='bind' host='%s' service='%s'/>\n",
dev->srcData.udp.bindHost, dev->srcData.udp.bindHost,
dev->srcData.udp.bindService) < 0) dev->srcData.udp.bindService);
return -1;
} else if (dev->srcData.udp.bindHost[0] !='\0') { } else if (dev->srcData.udp.bindHost[0] !='\0') {
if (virBufferVSprintf(buf, " <source mode='bind' host='%s'/>\n", virBufferVSprintf(buf, " <source mode='bind' host='%s'/>\n",
dev->srcData.udp.bindHost) < 0) dev->srcData.udp.bindHost);
return -1;
} else if (dev->srcData.udp.bindService[0] != '\0') { } else if (dev->srcData.udp.bindService[0] != '\0') {
if (virBufferVSprintf(buf, " <source mode='bind' service='%s'/>\n", virBufferVSprintf(buf, " <source mode='bind' service='%s'/>\n",
dev->srcData.udp.bindService) < 0) dev->srcData.udp.bindService);
return -1;
} }
if (dev->srcData.udp.connectService[0] != '\0' && if (dev->srcData.udp.connectService[0] != '\0' &&
dev->srcData.udp.connectHost[0] != '\0') { dev->srcData.udp.connectHost[0] != '\0') {
if (virBufferVSprintf(buf, " <source mode='connect' host='%s' service='%s'/>\n", virBufferVSprintf(buf, " <source mode='connect' host='%s' service='%s'/>\n",
dev->srcData.udp.connectHost, dev->srcData.udp.connectHost,
dev->srcData.udp.connectService) < 0) dev->srcData.udp.connectService);
return -1;
} else if (dev->srcData.udp.connectHost[0] != '\0') { } else if (dev->srcData.udp.connectHost[0] != '\0') {
if (virBufferVSprintf(buf, " <source mode='connect' host='%s'/>\n", virBufferVSprintf(buf, " <source mode='connect' host='%s'/>\n",
dev->srcData.udp.connectHost) < 0) dev->srcData.udp.connectHost);
return -1;
} else if (dev->srcData.udp.connectService[0] != '\0') { } else if (dev->srcData.udp.connectService[0] != '\0') {
if (virBufferVSprintf(buf, " <source mode='connect' service='%s'/>\n", virBufferVSprintf(buf, " <source mode='connect' service='%s'/>\n",
dev->srcData.udp.connectService) < 0) dev->srcData.udp.connectService);
return -1;
} }
break; break;
case QEMUD_CHR_SRC_TYPE_TCP: case QEMUD_CHR_SRC_TYPE_TCP:
if (virBufferVSprintf(buf, " <source mode='%s' host='%s' service='%s'/>\n", virBufferVSprintf(buf, " <source mode='%s' host='%s' service='%s'/>\n",
dev->srcData.tcp.listen ? "bind" : "connect", dev->srcData.tcp.listen ? "bind" : "connect",
dev->srcData.tcp.host, dev->srcData.tcp.host,
dev->srcData.tcp.service) < 0) dev->srcData.tcp.service);
return -1; virBufferVSprintf(buf, " <protocol type='%s'/>\n",
if (virBufferVSprintf(buf, " <protocol type='%s'/>\n", dev->srcData.tcp.protocol == QEMUD_CHR_SRC_TCP_PROTOCOL_TELNET
dev->srcData.tcp.protocol == QEMUD_CHR_SRC_TCP_PROTOCOL_TELNET ? "telnet" : "raw");
? "telnet" : "raw") < 0)
return -1;
break; break;
case QEMUD_CHR_SRC_TYPE_UNIX: case QEMUD_CHR_SRC_TYPE_UNIX:
if (virBufferVSprintf(buf, " <source mode='%s' path='%s'/>\n", virBufferVSprintf(buf, " <source mode='%s' path='%s'/>\n",
dev->srcData.nix.listen ? "bind" : "connect", dev->srcData.nix.listen ? "bind" : "connect",
dev->srcData.nix.path) < 0) dev->srcData.nix.path);
return -1;
break; break;
} }
if (virBufferVSprintf(buf, " <target port='%d'/>\n", virBufferVSprintf(buf, " <target port='%d'/>\n",
dev->dstPort) < 0) dev->dstPort);
return -1;
if (virBufferVSprintf(buf, " </%s>\n", virBufferVSprintf(buf, " </%s>\n",
type) < 0) type);
return -1;
return 0; return 0;
} }
@ -3461,7 +3447,7 @@ char *qemudGenerateXML(virConnectPtr conn,
struct qemud_vm *vm, struct qemud_vm *vm,
struct qemud_vm_def *def, struct qemud_vm_def *def,
int live) { int live) {
virBufferPtr buf = 0; virBuffer buf = VIR_BUFFER_INITIALIZER;
unsigned char *uuid; unsigned char *uuid;
char uuidstr[VIR_UUID_STRING_BUFLEN]; char uuidstr[VIR_UUID_STRING_BUFLEN];
const struct qemud_vm_disk_def *disk; const struct qemud_vm_disk_def *disk;
@ -3471,10 +3457,6 @@ char *qemudGenerateXML(virConnectPtr conn,
const char *type = NULL; const char *type = NULL;
int n; int n;
buf = virBufferNew (QEMUD_MAX_XML_LEN);
if (!buf)
goto no_memory;
switch (def->virtType) { switch (def->virtType) {
case QEMUD_VIRT_QEMU: case QEMUD_VIRT_QEMU:
type = "qemu"; type = "qemu";
@ -3492,49 +3474,34 @@ char *qemudGenerateXML(virConnectPtr conn,
goto cleanup; goto cleanup;
} }
if (qemudIsActiveVM(vm) && live) { if (qemudIsActiveVM(vm) && live)
if (virBufferVSprintf(buf, "<domain type='%s' id='%d'>\n", type, vm->id) < 0) virBufferVSprintf(&buf, "<domain type='%s' id='%d'>\n", type, vm->id);
goto no_memory; else
} else { virBufferVSprintf(&buf, "<domain type='%s'>\n", type);
if (virBufferVSprintf(buf, "<domain type='%s'>\n", type) < 0)
goto no_memory;
}
if (virBufferVSprintf(buf, " <name>%s</name>\n", def->name) < 0) virBufferVSprintf(&buf, " <name>%s</name>\n", def->name);
goto no_memory;
uuid = def->uuid; uuid = def->uuid;
virUUIDFormat(uuid, uuidstr); virUUIDFormat(uuid, uuidstr);
if (virBufferVSprintf(buf, " <uuid>%s</uuid>\n", uuidstr) < 0) virBufferVSprintf(&buf, " <uuid>%s</uuid>\n", uuidstr);
goto no_memory;
if (virBufferVSprintf(buf, " <memory>%lu</memory>\n", def->maxmem) < 0)
goto no_memory;
if (virBufferVSprintf(buf, " <currentMemory>%lu</currentMemory>\n", def->memory) < 0)
goto no_memory;
if (virBufferVSprintf(buf, " <vcpu>%d</vcpu>\n", def->vcpus) < 0)
goto no_memory;
if (virBufferAddLit(buf, " <os>\n") < 0) virBufferVSprintf(&buf, " <memory>%lu</memory>\n", def->maxmem);
goto no_memory; virBufferVSprintf(&buf, " <currentMemory>%lu</currentMemory>\n", def->memory);
virBufferVSprintf(&buf, " <vcpu>%d</vcpu>\n", def->vcpus);
virBufferAddLit(&buf, " <os>\n");
if (def->virtType == QEMUD_VIRT_QEMU) { if (def->virtType == QEMUD_VIRT_QEMU)
if (virBufferVSprintf(buf, " <type arch='%s' machine='%s'>%s</type>\n", virBufferVSprintf(&buf, " <type arch='%s' machine='%s'>%s</type>\n",
def->os.arch, def->os.machine, def->os.type) < 0) def->os.arch, def->os.machine, def->os.type);
goto no_memory; else
} else { virBufferVSprintf(&buf, " <type>%s</type>\n", def->os.type);
if (virBufferVSprintf(buf, " <type>%s</type>\n", def->os.type) < 0)
goto no_memory;
}
if (def->os.kernel[0]) if (def->os.kernel[0])
if (virBufferVSprintf(buf, " <kernel>%s</kernel>\n", def->os.kernel) < 0) virBufferVSprintf(&buf, " <kernel>%s</kernel>\n", def->os.kernel);
goto no_memory;
if (def->os.initrd[0]) if (def->os.initrd[0])
if (virBufferVSprintf(buf, " <initrd>%s</initrd>\n", def->os.initrd) < 0) virBufferVSprintf(&buf, " <initrd>%s</initrd>\n", def->os.initrd);
goto no_memory;
if (def->os.cmdline[0]) if (def->os.cmdline[0])
if (virBufferVSprintf(buf, " <cmdline>%s</cmdline>\n", def->os.cmdline) < 0) virBufferVSprintf(&buf, " <cmdline>%s</cmdline>\n", def->os.cmdline);
goto no_memory;
for (n = 0 ; n < def->os.nBootDevs ; n++) { for (n = 0 ; n < def->os.nBootDevs ; n++) {
const char *boottype = "hd"; const char *boottype = "hd";
@ -3552,41 +3519,29 @@ char *qemudGenerateXML(virConnectPtr conn,
boottype = "network"; boottype = "network";
break; break;
} }
if (virBufferVSprintf(buf, " <boot dev='%s'/>\n", boottype) < 0) virBufferVSprintf(&buf, " <boot dev='%s'/>\n", boottype);
goto no_memory;
} }
if (virBufferAddLit(buf, " </os>\n") < 0) virBufferAddLit(&buf, " </os>\n");
goto no_memory;
if (def->features & QEMUD_FEATURE_ACPI) { if (def->features & QEMUD_FEATURE_ACPI) {
if (virBufferAddLit(buf, " <features>\n") < 0) virBufferAddLit(&buf, " <features>\n");
goto no_memory; virBufferAddLit(&buf, " <acpi/>\n");
if (virBufferAddLit(buf, " <acpi/>\n") < 0) virBufferAddLit(&buf, " </features>\n");
goto no_memory;
if (virBufferAddLit(buf, " </features>\n") < 0)
goto no_memory;
} }
virBufferVSprintf(buf, " <clock offset='%s'/>\n", def->localtime ? "localtime" : "utc"); virBufferVSprintf(&buf, " <clock offset='%s'/>\n", def->localtime ? "localtime" : "utc");
if (virBufferAddLit(buf, " <on_poweroff>destroy</on_poweroff>\n") < 0) virBufferAddLit(&buf, " <on_poweroff>destroy</on_poweroff>\n");
goto no_memory; if (def->noReboot)
if (def->noReboot) { virBufferAddLit(&buf, " <on_reboot>destroy</on_reboot>\n");
if (virBufferAddLit(buf, " <on_reboot>destroy</on_reboot>\n") < 0) else
goto no_memory; virBufferAddLit(&buf, " <on_reboot>restart</on_reboot>\n");
} else {
if (virBufferAddLit(buf, " <on_reboot>restart</on_reboot>\n") < 0)
goto no_memory;
}
if (virBufferAddLit(buf, " <on_crash>destroy</on_crash>\n") < 0)
goto no_memory;
if (virBufferAddLit(buf, " <devices>\n") < 0) virBufferAddLit(&buf, " <on_crash>destroy</on_crash>\n");
goto no_memory; virBufferAddLit(&buf, " <devices>\n");
if (virBufferVSprintf(buf, " <emulator>%s</emulator>\n", def->os.binary) < 0) virBufferVSprintf(&buf, " <emulator>%s</emulator>\n", def->os.binary);
goto no_memory;
disk = def->disks; disk = def->disks;
while (disk) { while (disk) {
@ -3603,24 +3558,19 @@ char *qemudGenerateXML(virConnectPtr conn,
"cdrom", "cdrom",
"floppy", "floppy",
}; };
if (virBufferVSprintf(buf, " <disk type='%s' device='%s'>\n", virBufferVSprintf(&buf, " <disk type='%s' device='%s'>\n",
types[disk->type], devices[disk->device]) < 0) types[disk->type], devices[disk->device]);
goto no_memory;
if (disk->src[0]) if (disk->src[0])
if (virBufferVSprintf(buf, " <source %s='%s'/>\n", virBufferVSprintf(&buf, " <source %s='%s'/>\n",
typeAttrs[disk->type], disk->src) < 0) typeAttrs[disk->type], disk->src);
goto no_memory;
if (virBufferVSprintf(buf, " <target dev='%s'/>\n", disk->dst) < 0) virBufferVSprintf(&buf, " <target dev='%s'/>\n", disk->dst);
goto no_memory;
if (disk->readonly) if (disk->readonly)
if (virBufferAddLit(buf, " <readonly/>\n") < 0) virBufferAddLit(&buf, " <readonly/>\n");
goto no_memory;
if (virBufferAddLit(buf, " </disk>\n") < 0) virBufferAddLit(&buf, " </disk>\n");
goto no_memory;
disk = disk->next; disk = disk->next;
} }
@ -3636,69 +3586,53 @@ char *qemudGenerateXML(virConnectPtr conn,
"network", "network",
"bridge", "bridge",
}; };
if (virBufferVSprintf(buf, " <interface type='%s'>\n", virBufferVSprintf(&buf, " <interface type='%s'>\n",
types[net->type]) < 0) types[net->type]);
goto no_memory;
if (virBufferVSprintf(buf, " <mac address='%02x:%02x:%02x:%02x:%02x:%02x'/>\n", virBufferVSprintf(&buf, " <mac address='%02x:%02x:%02x:%02x:%02x:%02x'/>\n",
net->mac[0], net->mac[1], net->mac[2], net->mac[0], net->mac[1], net->mac[2],
net->mac[3], net->mac[4], net->mac[5]) < 0) net->mac[3], net->mac[4], net->mac[5]);
goto no_memory;
switch (net->type) { switch (net->type) {
case QEMUD_NET_NETWORK: case QEMUD_NET_NETWORK:
if (virBufferVSprintf(buf, " <source network='%s'/>\n", net->dst.network.name) < 0) virBufferVSprintf(&buf, " <source network='%s'/>\n", net->dst.network.name);
goto no_memory;
if (net->dst.network.ifname[0] != '\0') { if (net->dst.network.ifname[0] != '\0')
if (virBufferVSprintf(buf, " <target dev='%s'/>\n", net->dst.network.ifname) < 0) virBufferVSprintf(&buf, " <target dev='%s'/>\n", net->dst.network.ifname);
goto no_memory;
}
break; break;
case QEMUD_NET_ETHERNET: case QEMUD_NET_ETHERNET:
if (net->dst.ethernet.ifname[0] != '\0') { if (net->dst.ethernet.ifname[0] != '\0')
if (virBufferVSprintf(buf, " <target dev='%s'/>\n", net->dst.ethernet.ifname) < 0) virBufferVSprintf(&buf, " <target dev='%s'/>\n", net->dst.ethernet.ifname);
goto no_memory; if (net->dst.ethernet.script[0] != '\0')
} virBufferVSprintf(&buf, " <script path='%s'/>\n", net->dst.ethernet.script);
if (net->dst.ethernet.script[0] != '\0') {
if (virBufferVSprintf(buf, " <script path='%s'/>\n", net->dst.ethernet.script) < 0)
goto no_memory;
}
break; break;
case QEMUD_NET_BRIDGE: case QEMUD_NET_BRIDGE:
if (virBufferVSprintf(buf, " <source bridge='%s'/>\n", net->dst.bridge.brname) < 0) virBufferVSprintf(&buf, " <source bridge='%s'/>\n", net->dst.bridge.brname);
goto no_memory; if (net->dst.bridge.ifname[0] != '\0')
if (net->dst.bridge.ifname[0] != '\0') { virBufferVSprintf(&buf, " <target dev='%s'/>\n", net->dst.bridge.ifname);
if (virBufferVSprintf(buf, " <target dev='%s'/>\n", net->dst.bridge.ifname) < 0)
goto no_memory;
}
break; break;
case QEMUD_NET_SERVER: case QEMUD_NET_SERVER:
case QEMUD_NET_CLIENT: case QEMUD_NET_CLIENT:
case QEMUD_NET_MCAST: case QEMUD_NET_MCAST:
if (net->dst.socket.address[0] != '\0') { if (net->dst.socket.address[0] != '\0')
if (virBufferVSprintf(buf, " <source address='%s' port='%d'/>\n", virBufferVSprintf(&buf, " <source address='%s' port='%d'/>\n",
net->dst.socket.address, net->dst.socket.port) < 0) net->dst.socket.address, net->dst.socket.port);
goto no_memory; else
} else { virBufferVSprintf(&buf, " <source port='%d'/>\n",
if (virBufferVSprintf(buf, " <source port='%d'/>\n", net->dst.socket.port);
net->dst.socket.port) < 0)
goto no_memory;
}
} }
if (virBufferAddLit(buf, " </interface>\n") < 0) virBufferAddLit(&buf, " </interface>\n");
goto no_memory;
net = net->next; net = net->next;
} }
chr = def->serials; chr = def->serials;
while (chr) { while (chr) {
if (qemudGenerateXMLChar(buf, chr, "serial") < 0) if (qemudGenerateXMLChar(&buf, chr, "serial") < 0)
goto no_memory; goto no_memory;
chr = chr->next; chr = chr->next;
@ -3706,7 +3640,7 @@ char *qemudGenerateXML(virConnectPtr conn,
chr = def->parallels; chr = def->parallels;
while (chr) { while (chr) {
if (qemudGenerateXMLChar(buf, chr, "parallel") < 0) if (qemudGenerateXMLChar(&buf, chr, "parallel") < 0)
goto no_memory; goto no_memory;
chr = chr->next; chr = chr->next;
@ -3714,49 +3648,41 @@ char *qemudGenerateXML(virConnectPtr conn,
/* First serial device is the primary console */ /* First serial device is the primary console */
if (def->nserials > 0 && if (def->nserials > 0 &&
qemudGenerateXMLChar(buf, def->serials, "console") < 0) qemudGenerateXMLChar(&buf, def->serials, "console") < 0)
goto no_memory; goto no_memory;
input = def->inputs; input = def->inputs;
while (input) { while (input) {
if (input->bus != QEMU_INPUT_BUS_PS2 && if (input->bus != QEMU_INPUT_BUS_PS2)
virBufferVSprintf(buf, " <input type='%s' bus='usb'/>\n", virBufferVSprintf(&buf, " <input type='%s' bus='usb'/>\n",
input->type == QEMU_INPUT_TYPE_MOUSE ? "mouse" : "tablet") < 0) input->type == QEMU_INPUT_TYPE_MOUSE ? "mouse" : "tablet");
goto no_memory;
input = input->next; input = input->next;
} }
/* If graphics is enable, add implicit mouse */ /* If graphics is enable, add implicit mouse */
if (def->graphicsType != QEMUD_GRAPHICS_NONE) if (def->graphicsType != QEMUD_GRAPHICS_NONE)
if (virBufferAddLit(buf, " <input type='mouse' bus='ps2'/>\n") < 0) virBufferAddLit(&buf, " <input type='mouse' bus='ps2'/>\n");
goto no_memory;
switch (def->graphicsType) { switch (def->graphicsType) {
case QEMUD_GRAPHICS_VNC: case QEMUD_GRAPHICS_VNC:
if (virBufferAddLit(buf, " <graphics type='vnc'") < 0) virBufferAddLit(&buf, " <graphics type='vnc'");
goto no_memory;
if (def->vncPort && if (def->vncPort)
virBufferVSprintf(buf, " port='%d'", virBufferVSprintf(&buf, " port='%d'",
qemudIsActiveVM(vm) && live ? def->vncActivePort : def->vncPort) < 0) qemudIsActiveVM(vm) && live ? def->vncActivePort : def->vncPort);
goto no_memory;
if (def->vncListen[0] && if (def->vncListen[0])
virBufferVSprintf(buf, " listen='%s'", virBufferVSprintf(&buf, " listen='%s'",
def->vncListen) < 0) def->vncListen);
goto no_memory;
if (def->keymap && if (def->keymap)
virBufferVSprintf(buf, " keymap='%s'", virBufferVSprintf(&buf, " keymap='%s'",
def->keymap) < 0) def->keymap);
goto no_memory;
if (virBufferAddLit(buf, "/>\n") < 0) virBufferAddLit(&buf, "/>\n");
goto no_memory;
break; break;
case QEMUD_GRAPHICS_SDL: case QEMUD_GRAPHICS_SDL:
if (virBufferAddLit(buf, " <graphics type='sdl'/>\n") < 0) virBufferAddLit(&buf, " <graphics type='sdl'/>\n");
goto no_memory;
break; break;
case QEMUD_GRAPHICS_NONE: case QEMUD_GRAPHICS_NONE:
@ -3764,20 +3690,19 @@ char *qemudGenerateXML(virConnectPtr conn,
break; break;
} }
if (virBufferAddLit(buf, " </devices>\n") < 0) virBufferAddLit(&buf, " </devices>\n");
virBufferAddLit(&buf, "</domain>\n");
if (virBufferError(&buf))
goto no_memory; goto no_memory;
return virBufferContentAndReset(&buf);
if (virBufferAddLit(buf, "</domain>\n") < 0)
goto no_memory;
return virBufferContentAndFree (buf);
no_memory: no_memory:
qemudReportError(conn, NULL, NULL, VIR_ERR_NO_MEMORY, qemudReportError(conn, NULL, NULL, VIR_ERR_NO_MEMORY,
"%s", _("failed to generate XML: out of memory")); "%s", _("failed to generate XML: out of memory"));
cleanup: cleanup:
if (buf) virBufferFree (buf); free(virBufferContentAndReset(&buf));
return NULL; return NULL;
} }
@ -3786,89 +3711,73 @@ char *qemudGenerateNetworkXML(virConnectPtr conn,
struct qemud_driver *driver ATTRIBUTE_UNUSED, struct qemud_driver *driver ATTRIBUTE_UNUSED,
struct qemud_network *network, struct qemud_network *network,
struct qemud_network_def *def) { struct qemud_network_def *def) {
virBufferPtr buf = 0; virBuffer buf = VIR_BUFFER_INITIALIZER;
unsigned char *uuid; unsigned char *uuid;
char uuidstr[VIR_UUID_STRING_BUFLEN]; char uuidstr[VIR_UUID_STRING_BUFLEN];
buf = virBufferNew (QEMUD_MAX_XML_LEN); virBufferAddLit(&buf, "<network>\n");
if (!buf)
goto no_memory;
if (virBufferAddLit(buf, "<network>\n") < 0) virBufferVSprintf(&buf, " <name>%s</name>\n", def->name);
goto no_memory;
if (virBufferVSprintf(buf, " <name>%s</name>\n", def->name) < 0)
goto no_memory;
uuid = def->uuid; uuid = def->uuid;
virUUIDFormat(uuid, uuidstr); virUUIDFormat(uuid, uuidstr);
if (virBufferVSprintf(buf, " <uuid>%s</uuid>\n", uuidstr) < 0) virBufferVSprintf(&buf, " <uuid>%s</uuid>\n", uuidstr);
goto no_memory;
if (def->forward) { if (def->forward) {
if (def->forwardDev[0]) { if (def->forwardDev[0]) {
virBufferVSprintf(buf, " <forward dev='%s' mode='%s'/>\n", virBufferVSprintf(&buf, " <forward dev='%s' mode='%s'/>\n",
def->forwardDev, (def->forwardMode == QEMUD_NET_FORWARD_ROUTE ? "route" : "nat")); def->forwardDev, (def->forwardMode == QEMUD_NET_FORWARD_ROUTE ? "route" : "nat"));
} else { } else {
virBufferVSprintf(buf, " <forward mode='%s'/>\n", (def->forwardMode == QEMUD_NET_FORWARD_ROUTE ? "route" : "nat")); virBufferVSprintf(&buf, " <forward mode='%s'/>\n", (def->forwardMode == QEMUD_NET_FORWARD_ROUTE ? "route" : "nat"));
} }
} }
virBufferAddLit(buf, " <bridge"); virBufferAddLit(&buf, " <bridge");
if (qemudIsActiveNetwork(network)) { if (qemudIsActiveNetwork(network)) {
if (virBufferVSprintf(buf, " name='%s'", network->bridge) < 0) virBufferVSprintf(&buf, " name='%s'", network->bridge);
goto no_memory;
} else if (def->bridge[0]) { } else if (def->bridge[0]) {
if (virBufferVSprintf(buf, " name='%s'", def->bridge) < 0) virBufferVSprintf(&buf, " name='%s'", def->bridge);
goto no_memory;
} }
if (virBufferVSprintf(buf, " stp='%s' forwardDelay='%d' />\n", virBufferVSprintf(&buf, " stp='%s' forwardDelay='%d' />\n",
def->disableSTP ? "off" : "on", def->disableSTP ? "off" : "on",
def->forwardDelay) < 0) def->forwardDelay);
goto no_memory;
if (def->ipAddress[0] || def->netmask[0]) { if (def->ipAddress[0] || def->netmask[0]) {
if (virBufferAddLit(buf, " <ip") < 0) virBufferAddLit(&buf, " <ip");
goto no_memory;
if (def->ipAddress[0] && if (def->ipAddress[0])
virBufferVSprintf(buf, " address='%s'", def->ipAddress) < 0) virBufferVSprintf(&buf, " address='%s'", def->ipAddress);
goto no_memory;
if (def->netmask[0] && if (def->netmask[0])
virBufferVSprintf(buf, " netmask='%s'", def->netmask) < 0) virBufferVSprintf(&buf, " netmask='%s'", def->netmask);
goto no_memory;
if (virBufferAddLit(buf, ">\n") < 0) virBufferAddLit(&buf, ">\n");
goto no_memory;
if (def->ranges) { if (def->ranges) {
struct qemud_dhcp_range_def *range = def->ranges; struct qemud_dhcp_range_def *range = def->ranges;
if (virBufferAddLit(buf, " <dhcp>\n") < 0) virBufferAddLit(&buf, " <dhcp>\n");
goto no_memory;
while (range) { while (range) {
if (virBufferVSprintf(buf, " <range start='%s' end='%s' />\n", virBufferVSprintf(&buf, " <range start='%s' end='%s' />\n",
range->start, range->end) < 0) range->start, range->end);
goto no_memory;
range = range->next; range = range->next;
} }
if (virBufferAddLit(buf, " </dhcp>\n") < 0) virBufferAddLit(&buf, " </dhcp>\n");
goto no_memory;
} }
if (virBufferAddLit(buf, " </ip>\n") < 0) virBufferAddLit(&buf, " </ip>\n");
goto no_memory;
} }
if (virBufferAddLit(buf, "</network>\n") < 0) virBufferAddLit(&buf, "</network>\n");
if (virBufferError(&buf))
goto no_memory; goto no_memory;
return virBufferContentAndFree (buf); return virBufferContentAndReset(&buf);
no_memory: no_memory:
qemudReportError(conn, NULL, NULL, VIR_ERR_NO_MEMORY, qemudReportError(conn, NULL, NULL, VIR_ERR_NO_MEMORY,
"%s", _("failed to generate XML: out of memory")); "%s", _("failed to generate XML: out of memory"));
if (buf) virBufferFree (buf); free(virBufferContentAndReset(&buf));
return NULL; return NULL;
} }

View File

@ -136,20 +136,23 @@ append_qparam (struct qparam_set *ps,
char * char *
qparam_get_query (const struct qparam_set *ps) qparam_get_query (const struct qparam_set *ps)
{ {
virBufferPtr buf; virBuffer buf = VIR_BUFFER_INITIALIZER;
int i, amp = 0; int i, amp = 0;
buf = virBufferNew (100);
for (i = 0; i < ps->n; ++i) { for (i = 0; i < ps->n; ++i) {
if (!ps->p[i].ignore) { if (!ps->p[i].ignore) {
if (amp) virBufferAddChar (buf, '&'); if (amp) virBufferAddChar (&buf, '&');
virBufferStrcat (buf, ps->p[i].name, "=", NULL); virBufferStrcat (&buf, ps->p[i].name, "=", NULL);
virBufferURIEncodeString (buf, ps->p[i].value); virBufferURIEncodeString (&buf, ps->p[i].value);
amp = 1; amp = 1;
} }
} }
return virBufferContentAndFree (buf); if (virBufferError(&buf)) {
return NULL;
}
return virBufferContentAndReset(&buf);
} }
void void

View File

@ -414,7 +414,7 @@ char *
virStoragePoolDefFormat(virConnectPtr conn, virStoragePoolDefFormat(virConnectPtr conn,
virStoragePoolDefPtr def) { virStoragePoolDefPtr def) {
virStorageBackendPoolOptionsPtr options; virStorageBackendPoolOptionsPtr options;
virBufferPtr buf; virBuffer buf = VIR_BUFFER_INITIALIZER;
const char *type; const char *type;
char uuid[VIR_UUID_STRING_BUFLEN]; char uuid[VIR_UUID_STRING_BUFLEN];
int i; int i;
@ -423,126 +423,96 @@ virStoragePoolDefFormat(virConnectPtr conn,
if (options == NULL) if (options == NULL)
return NULL; return NULL;
if ((buf = virBufferNew(8192)) == NULL)
goto no_memory;
type = virStorageBackendToString(def->type); type = virStorageBackendToString(def->type);
if (!type) { if (!type) {
virStorageReportError(conn, VIR_ERR_INTERNAL_ERROR, virStorageReportError(conn, VIR_ERR_INTERNAL_ERROR,
"%s", _("unexpected pool type")); "%s", _("unexpected pool type"));
goto cleanup; goto cleanup;
} }
if (virBufferVSprintf(buf, "<pool type='%s'>\n", type) < 0) virBufferVSprintf(&buf, "<pool type='%s'>\n", type);
goto no_memory; virBufferVSprintf(&buf," <name>%s</name>\n", def->name);
if (virBufferVSprintf(buf," <name>%s</name>\n", def->name) < 0)
goto no_memory;
virUUIDFormat(def->uuid, uuid); virUUIDFormat(def->uuid, uuid);
if (virBufferVSprintf(buf," <uuid>%s</uuid>\n", uuid) < 0) virBufferVSprintf(&buf," <uuid>%s</uuid>\n", uuid);
goto no_memory;
if (virBufferVSprintf(buf," <capacity>%llu</capacity>\n", virBufferVSprintf(&buf," <capacity>%llu</capacity>\n",
def->capacity) < 0) def->capacity);
goto no_memory; virBufferVSprintf(&buf," <allocation>%llu</allocation>\n",
if (virBufferVSprintf(buf," <allocation>%llu</allocation>\n", def->allocation);
def->allocation) < 0) virBufferVSprintf(&buf," <available>%llu</available>\n",
goto no_memory; def->available);
if (virBufferVSprintf(buf," <available>%llu</available>\n",
def->available) < 0)
goto no_memory;
virBufferAddLit(&buf," <source>\n");
if (virBufferAddLit(buf," <source>\n") < 0)
goto no_memory;
if ((options->flags & VIR_STORAGE_BACKEND_POOL_SOURCE_HOST) && if ((options->flags & VIR_STORAGE_BACKEND_POOL_SOURCE_HOST) &&
def->source.host.name && def->source.host.name)
virBufferVSprintf(buf," <host name='%s'/>\n", def->source.host.name) < 0) virBufferVSprintf(&buf," <host name='%s'/>\n", def->source.host.name);
goto no_memory;
if ((options->flags & VIR_STORAGE_BACKEND_POOL_SOURCE_DEVICE) && if ((options->flags & VIR_STORAGE_BACKEND_POOL_SOURCE_DEVICE) &&
def->source.ndevice) { def->source.ndevice) {
for (i = 0 ; i < def->source.ndevice ; i++) { for (i = 0 ; i < def->source.ndevice ; i++) {
if (virBufferVSprintf(buf," <device path='%s'>\n", def->source.devices[i].path) < 0) virBufferVSprintf(&buf," <device path='%s'>\n", def->source.devices[i].path);
goto no_memory;
if (def->source.devices[i].nfreeExtent) { if (def->source.devices[i].nfreeExtent) {
int j; int j;
for (j = 0 ; j < def->source.devices[i].nfreeExtent ; j++) { for (j = 0 ; j < def->source.devices[i].nfreeExtent ; j++) {
if (virBufferVSprintf(buf, " <freeExtent start='%llu' end='%llu'/>\n", virBufferVSprintf(&buf, " <freeExtent start='%llu' end='%llu'/>\n",
def->source.devices[i].freeExtents[j].start, def->source.devices[i].freeExtents[j].start,
def->source.devices[i].freeExtents[j].end) < 0) def->source.devices[i].freeExtents[j].end);
goto no_memory;
} }
} }
if (virBufferAddLit(buf," </device>\n") < 0) virBufferAddLit(&buf," </device>\n");
goto no_memory;
} }
} }
if ((options->flags & VIR_STORAGE_BACKEND_POOL_SOURCE_DIR) && if ((options->flags & VIR_STORAGE_BACKEND_POOL_SOURCE_DIR) &&
def->source.dir && def->source.dir)
virBufferVSprintf(buf," <dir path='%s'/>\n", def->source.dir) < 0) virBufferVSprintf(&buf," <dir path='%s'/>\n", def->source.dir);
goto no_memory;
if ((options->flags & VIR_STORAGE_BACKEND_POOL_SOURCE_ADAPTER) && if ((options->flags & VIR_STORAGE_BACKEND_POOL_SOURCE_ADAPTER) &&
def->source.adapter && def->source.adapter)
virBufferVSprintf(buf," <adapter name='%s'/>\n", def->source.adapter) < 0) virBufferVSprintf(&buf," <adapter name='%s'/>\n", def->source.adapter);
goto no_memory;
if (options->formatToString) { if (options->formatToString) {
const char *format = (options->formatToString)(conn, def->source.format); const char *format = (options->formatToString)(conn, def->source.format);
if (!format) if (!format)
goto cleanup; goto cleanup;
if (virBufferVSprintf(buf," <format type='%s'/>\n", format) < 0) virBufferVSprintf(&buf," <format type='%s'/>\n", format);
goto no_memory;
} }
if (def->source.authType == VIR_STORAGE_POOL_AUTH_CHAP && if (def->source.authType == VIR_STORAGE_POOL_AUTH_CHAP)
virBufferVSprintf(buf," <auth type='chap' login='%s' passwd='%s'>\n", virBufferVSprintf(&buf," <auth type='chap' login='%s' passwd='%s'>\n",
def->source.auth.chap.login, def->source.auth.chap.login,
def->source.auth.chap.passwd) < 0) def->source.auth.chap.passwd);
goto no_memory; virBufferAddLit(&buf," </source>\n");
if (virBufferAddLit(buf," </source>\n") < 0)
virBufferAddLit(&buf," <target>\n");
if (def->target.path)
virBufferVSprintf(&buf," <path>%s</path>\n", def->target.path);
virBufferAddLit(&buf," <permissions>\n");
virBufferVSprintf(&buf," <mode>0%o</mode>\n",
def->target.perms.mode);
virBufferVSprintf(&buf," <owner>%d</owner>\n",
def->target.perms.uid);
virBufferVSprintf(&buf," <group>%d</group>\n",
def->target.perms.gid);
if (def->target.perms.label)
virBufferVSprintf(&buf," <label>%s</label>\n",
def->target.perms.label);
virBufferAddLit(&buf," </permissions>\n");
virBufferAddLit(&buf," </target>\n");
virBufferAddLit(&buf,"</pool>\n");
if (virBufferError(&buf))
goto no_memory; goto no_memory;
return virBufferContentAndReset(&buf);
if (virBufferAddLit(buf," <target>\n") < 0)
goto no_memory;
if (def->target.path &&
virBufferVSprintf(buf," <path>%s</path>\n", def->target.path) < 0)
goto no_memory;
if (virBufferAddLit(buf," <permissions>\n") < 0)
goto no_memory;
if (virBufferVSprintf(buf," <mode>0%o</mode>\n",
def->target.perms.mode) < 0)
goto no_memory;
if (virBufferVSprintf(buf," <owner>%d</owner>\n",
def->target.perms.uid) < 0)
goto no_memory;
if (virBufferVSprintf(buf," <group>%d</group>\n",
def->target.perms.gid) < 0)
goto no_memory;
if (def->target.perms.label) {
if (virBufferVSprintf(buf," <label>%s</label>\n",
def->target.perms.label) < 0)
goto no_memory;
}
if (virBufferAddLit(buf," </permissions>\n") < 0)
goto no_memory;
if (virBufferAddLit(buf," </target>\n") < 0)
goto no_memory;
if (virBufferAddLit(buf,"</pool>\n") < 0)
goto no_memory;
return virBufferContentAndFree(buf);
no_memory: no_memory:
virStorageReportError(conn, VIR_ERR_NO_MEMORY, "%s", _("xml")); virStorageReportError(conn, VIR_ERR_NO_MEMORY, "%s", _("xml"));
cleanup: cleanup:
virBufferFree(buf); free(virBufferContentAndReset(&buf));
return NULL; return NULL;
} }
@ -804,26 +774,17 @@ virStorageVolDefFormat(virConnectPtr conn,
virStoragePoolDefPtr pool, virStoragePoolDefPtr pool,
virStorageVolDefPtr def) { virStorageVolDefPtr def) {
virStorageBackendVolOptionsPtr options; virStorageBackendVolOptionsPtr options;
virBufferPtr buf = virBufferNew(8192); virBuffer buf = VIR_BUFFER_INITIALIZER;
options = virStorageBackendVolOptionsForType(pool->type); options = virStorageBackendVolOptionsForType(pool->type);
if (options == NULL) if (options == NULL)
return NULL; return NULL;
if (!buf) virBufferAddLit(&buf, "<volume>\n");
goto no_memory; virBufferVSprintf(&buf," <name>%s</name>\n", def->name);
virBufferVSprintf(&buf," <key>%s</key>\n", def->key);
virBufferAddLit(&buf, " <source>\n");
if (virBufferAddLit(buf, "<volume>\n") < 0)
goto no_memory;
if (virBufferVSprintf(buf," <name>%s</name>\n", def->name) < 0)
goto no_memory;
if (virBufferVSprintf(buf," <key>%s</key>\n", def->key) < 0)
goto no_memory;
if (virBufferAddLit(buf, " <source>\n") < 0)
goto no_memory;
if (def->source.nextent) { if (def->source.nextent) {
int i; int i;
const char *thispath = NULL; const char *thispath = NULL;
@ -831,81 +792,67 @@ virStorageVolDefFormat(virConnectPtr conn,
if (thispath == NULL || if (thispath == NULL ||
STRNEQ(thispath, def->source.extents[i].path)) { STRNEQ(thispath, def->source.extents[i].path)) {
if (thispath != NULL) if (thispath != NULL)
if (virBufferAddLit(buf, " </device>\n") < 0) virBufferAddLit(&buf, " </device>\n");
goto no_memory;
if (virBufferVSprintf(buf, " <device path='%s'>\n", virBufferVSprintf(&buf, " <device path='%s'>\n",
def->source.extents[i].path) < 0) def->source.extents[i].path);
goto no_memory;
} }
if (virBufferVSprintf(buf, virBufferVSprintf(&buf,
" <extent start='%llu' end='%llu'/>\n", " <extent start='%llu' end='%llu'/>\n",
def->source.extents[i].start, def->source.extents[i].start,
def->source.extents[i].end) < 0) def->source.extents[i].end);
goto no_memory;
thispath = def->source.extents[i].path; thispath = def->source.extents[i].path;
} }
if (thispath != NULL) if (thispath != NULL)
if (virBufferAddLit(buf, " </device>\n") < 0) virBufferAddLit(&buf, " </device>\n");
goto no_memory;
} }
if (virBufferAddLit(buf, " </source>\n") < 0) virBufferAddLit(&buf, " </source>\n");
goto no_memory;
if (virBufferVSprintf(buf," <capacity>%llu</capacity>\n", virBufferVSprintf(&buf," <capacity>%llu</capacity>\n",
def->capacity) < 0) def->capacity);
goto no_memory; virBufferVSprintf(&buf," <allocation>%llu</allocation>\n",
if (virBufferVSprintf(buf," <allocation>%llu</allocation>\n", def->allocation);
def->allocation) < 0)
goto no_memory;
if (virBufferAddLit(buf, " <target>\n") < 0) virBufferAddLit(&buf, " <target>\n");
goto no_memory;
if (def->target.path && if (def->target.path)
virBufferVSprintf(buf," <path>%s</path>\n", def->target.path) < 0) virBufferVSprintf(&buf," <path>%s</path>\n", def->target.path);
goto no_memory;
if (options->formatToString) { if (options->formatToString) {
const char *format = (options->formatToString)(conn, const char *format = (options->formatToString)(conn,
def->target.format); def->target.format);
if (!format) if (!format)
goto cleanup; goto cleanup;
if (virBufferVSprintf(buf," <format type='%s'/>\n", format) < 0) virBufferVSprintf(&buf," <format type='%s'/>\n", format);
goto no_memory;
} }
if (virBufferAddLit(buf," <permissions>\n") < 0) virBufferAddLit(&buf," <permissions>\n");
goto no_memory; virBufferVSprintf(&buf," <mode>0%o</mode>\n",
if (virBufferVSprintf(buf," <mode>0%o</mode>\n", def->target.perms.mode);
def->target.perms.mode) < 0) virBufferVSprintf(&buf," <owner>%d</owner>\n",
goto no_memory; def->target.perms.uid);
if (virBufferVSprintf(buf," <owner>%d</owner>\n", virBufferVSprintf(&buf," <group>%d</group>\n",
def->target.perms.uid) < 0) def->target.perms.gid);
goto no_memory;
if (virBufferVSprintf(buf," <group>%d</group>\n",
def->target.perms.gid) < 0) if (def->target.perms.label)
virBufferVSprintf(&buf," <label>%s</label>\n",
def->target.perms.label);
virBufferAddLit(&buf," </permissions>\n");
virBufferAddLit(&buf, " </target>\n");
virBufferAddLit(&buf,"</volume>\n");
if (virBufferError(&buf))
goto no_memory; goto no_memory;
if (def->target.perms.label && return virBufferContentAndReset(&buf);
virBufferVSprintf(buf," <label>%s</label>\n",
def->target.perms.label) < 0)
goto no_memory;
if (virBufferAddLit(buf," </permissions>\n") < 0)
goto no_memory;
if (virBufferAddLit(buf, " </target>\n") < 0)
goto no_memory;
if (virBufferAddLit(buf,"</volume>\n") < 0)
goto no_memory;
return virBufferContentAndFree(buf);
no_memory: no_memory:
virStorageReportError(conn, VIR_ERR_NO_MEMORY, "%s", _("xml")); virStorageReportError(conn, VIR_ERR_NO_MEMORY, "%s", _("xml"));
cleanup: cleanup:
virBufferFree(buf); free(virBufferContentAndReset(&buf));
return NULL; return NULL;
} }

View File

@ -1523,34 +1523,30 @@ static int testSetVcpus(virDomainPtr domain,
static char *testDomainDumpXML(virDomainPtr domain, int flags ATTRIBUTE_UNUSED) static char *testDomainDumpXML(virDomainPtr domain, int flags ATTRIBUTE_UNUSED)
{ {
virBufferPtr buf; virBuffer buf = VIR_BUFFER_INITIALIZER;
char *xml;
unsigned char *uuid; unsigned char *uuid;
char uuidstr[VIR_UUID_STRING_BUFLEN]; char uuidstr[VIR_UUID_STRING_BUFLEN];
GET_DOMAIN(domain, NULL); GET_DOMAIN(domain, NULL);
if (!(buf = virBufferNew(4000))) { virBufferVSprintf(&buf, "<domain type='test' id='%d'>\n", domain->id);
testError(domain->conn, domain, NULL, VIR_ERR_NO_MEMORY, __FUNCTION__); virBufferVSprintf(&buf, " <name>%s</name>\n", domain->name);
return (NULL);
}
virBufferVSprintf(buf, "<domain type='test' id='%d'>\n", domain->id);
virBufferVSprintf(buf, " <name>%s</name>\n", domain->name);
uuid = domain->uuid; uuid = domain->uuid;
virUUIDFormat(uuid, uuidstr); virUUIDFormat(uuid, uuidstr);
virBufferVSprintf(buf, " <uuid>%s</uuid>\n", uuidstr); virBufferVSprintf(&buf, " <uuid>%s</uuid>\n", uuidstr);
virBufferVSprintf(buf, " <memory>%lu</memory>\n", privdom->info.maxMem); virBufferVSprintf(&buf, " <memory>%lu</memory>\n", privdom->info.maxMem);
virBufferVSprintf(buf, " <vcpu>%d</vcpu>\n", privdom->info.nrVirtCpu); virBufferVSprintf(&buf, " <vcpu>%d</vcpu>\n", privdom->info.nrVirtCpu);
virBufferVSprintf(buf, " <on_reboot>%s</on_reboot>\n", testRestartFlagToString(privdom->onReboot)); virBufferVSprintf(&buf, " <on_reboot>%s</on_reboot>\n", testRestartFlagToString(privdom->onReboot));
virBufferVSprintf(buf, " <on_poweroff>%s</on_poweroff>\n", testRestartFlagToString(privdom->onPoweroff)); virBufferVSprintf(&buf, " <on_poweroff>%s</on_poweroff>\n", testRestartFlagToString(privdom->onPoweroff));
virBufferVSprintf(buf, " <on_crash>%s</on_crash>\n", testRestartFlagToString(privdom->onCrash)); virBufferVSprintf(&buf, " <on_crash>%s</on_crash>\n", testRestartFlagToString(privdom->onCrash));
virBufferAddLit(buf, "</domain>\n"); virBufferAddLit(&buf, "</domain>\n");
xml = buf->content; if (virBufferError(&buf)) {
free(buf); testError(domain->conn, domain, NULL, VIR_ERR_NO_MEMORY, __FUNCTION__);
return NULL;
}
return (xml); return virBufferContentAndReset(&buf);
} }
static int testNumOfDefinedDomains(virConnectPtr conn) { static int testNumOfDefinedDomains(virConnectPtr conn) {
@ -1928,44 +1924,40 @@ static int testNetworkDestroy(virNetworkPtr network) {
} }
static char *testNetworkDumpXML(virNetworkPtr network, int flags ATTRIBUTE_UNUSED) { static char *testNetworkDumpXML(virNetworkPtr network, int flags ATTRIBUTE_UNUSED) {
virBufferPtr buf; virBuffer buf = VIR_BUFFER_INITIALIZER;
char *xml;
unsigned char *uuid; unsigned char *uuid;
char uuidstr[VIR_UUID_STRING_BUFLEN]; char uuidstr[VIR_UUID_STRING_BUFLEN];
GET_NETWORK(network, NULL); GET_NETWORK(network, NULL);
if (!(buf = virBufferNew(4000))) { virBufferAddLit(&buf, "<network>\n");
testError(network->conn, NULL, network, VIR_ERR_NO_MEMORY, __FUNCTION__); virBufferVSprintf(&buf, " <name>%s</name>\n", network->name);
return (NULL);
}
virBufferAddLit(buf, "<network>\n");
virBufferVSprintf(buf, " <name>%s</name>\n", network->name);
uuid = network->uuid; uuid = network->uuid;
virUUIDFormat(uuid, uuidstr); virUUIDFormat(uuid, uuidstr);
virBufferVSprintf(buf, " <uuid>%s</uuid>\n", uuidstr); virBufferVSprintf(&buf, " <uuid>%s</uuid>\n", uuidstr);
virBufferVSprintf(buf, " <bridge name='%s'/>\n", privnet->bridge); virBufferVSprintf(&buf, " <bridge name='%s'/>\n", privnet->bridge);
if (privnet->forward) { if (privnet->forward) {
if (privnet->forwardDev[0]) if (privnet->forwardDev[0])
virBufferVSprintf(buf, " <forward dev='%s'/>\n", privnet->forwardDev); virBufferVSprintf(&buf, " <forward dev='%s'/>\n", privnet->forwardDev);
else else
virBufferAddLit(buf, " <forward/>\n"); virBufferAddLit(&buf, " <forward/>\n");
} }
virBufferVSprintf(buf, " <ip address='%s' netmask='%s'>\n", virBufferVSprintf(&buf, " <ip address='%s' netmask='%s'>\n",
privnet->ipAddress, privnet->ipNetmask); privnet->ipAddress, privnet->ipNetmask);
virBufferAddLit(buf, " <dhcp>\n"); virBufferAddLit(&buf, " <dhcp>\n");
virBufferVSprintf(buf, " <range start='%s' end='%s'/>\n", virBufferVSprintf(&buf, " <range start='%s' end='%s'/>\n",
privnet->dhcpStart, privnet->dhcpEnd); privnet->dhcpStart, privnet->dhcpEnd);
virBufferAddLit(buf, " </dhcp>\n"); virBufferAddLit(&buf, " </dhcp>\n");
virBufferAddLit(buf, " </ip>\n"); virBufferAddLit(&buf, " </ip>\n");
virBufferAddLit(buf, "</network>\n"); virBufferAddLit(&buf, "</network>\n");
xml = buf->content; if (virBufferError(&buf)) {
free(buf); testError(network->conn, NULL, network, VIR_ERR_NO_MEMORY, __FUNCTION__);
return NULL;
}
return (xml); return virBufferContentAndReset(&buf);
} }
static char *testNetworkGetBridgeName(virNetworkPtr network) { static char *testNetworkGetBridgeName(virNetworkPtr network) {

View File

@ -2885,10 +2885,9 @@ cmdPoolCreateAs(vshControl * ctl, vshCmd * cmd)
{ {
virStoragePoolPtr pool; virStoragePoolPtr pool;
int found; int found;
char *xml;
char *name, *type, *srcHost, *srcPath, *srcDev, *target; char *name, *type, *srcHost, *srcPath, *srcDev, *target;
virBuffer buf; virBuffer buf = VIR_BUFFER_INITIALIZER;
memset(&buf, 0, sizeof(buf));
if (!vshConnectionUsability(ctl, ctl->conn, TRUE)) if (!vshConnectionUsability(ctl, ctl->conn, TRUE))
return FALSE; return FALSE;
@ -2905,39 +2904,36 @@ cmdPoolCreateAs(vshControl * ctl, vshCmd * cmd)
srcDev = vshCommandOptString(cmd, "source-dev", &found); srcDev = vshCommandOptString(cmd, "source-dev", &found);
target = vshCommandOptString(cmd, "target", &found); target = vshCommandOptString(cmd, "target", &found);
if (virBufferVSprintf(&buf, "<pool type='%s'>\n", type) < 0) virBufferVSprintf(&buf, "<pool type='%s'>\n", type);
goto cleanup; virBufferVSprintf(&buf, " <name>%s</name>\n", name);
if (virBufferVSprintf(&buf, " <name>%s</name>\n", name) < 0)
goto cleanup;
if (srcHost || srcPath || srcDev) { if (srcHost || srcPath || srcDev) {
if (virBufferAddLit(&buf, " <source>\n") < 0) virBufferAddLit(&buf, " <source>\n");
goto cleanup; if (srcHost)
if (srcHost && virBufferVSprintf(&buf, " <host name='%s'>\n", srcHost);
virBufferVSprintf(&buf, " <host name='%s'>\n", srcHost) < 0)
goto cleanup;
if (srcPath &&
virBufferVSprintf(&buf, " <dir path='%s'/>\n", srcPath) < 0)
goto cleanup;
if (srcDev &&
virBufferVSprintf(&buf, " <device path='%s'/>\n", srcDev) < 0)
goto cleanup;
if (virBufferAddLit(&buf, " </source>\n") < 0) if (srcPath)
goto cleanup; virBufferVSprintf(&buf, " <dir path='%s'/>\n", srcPath);
if (srcDev)
virBufferVSprintf(&buf, " <device path='%s'/>\n", srcDev);
virBufferAddLit(&buf, " </source>\n");
} }
if (target) { if (target) {
if (virBufferAddLit(&buf, " <target>\n") < 0) virBufferAddLit(&buf, " <target>\n");
goto cleanup; virBufferVSprintf(&buf, " <path>%s</path>\n", target);
if (virBufferVSprintf(&buf, " <path>%s</path>\n", target) < 0) virBufferAddLit(&buf, " </target>\n");
goto cleanup;
if (virBufferAddLit(&buf, " </target>\n") < 0)
goto cleanup;
} }
if (virBufferAddLit(&buf, "</pool>\n") < 0) virBufferAddLit(&buf, "</pool>\n");
goto cleanup;
pool = virStoragePoolCreateXML(ctl->conn, buf.content, 0); if (virBufferError(&buf)) {
free (buf.content); vshPrint(ctl, "%s", _("Failed to allocate XML buffer"));
return FALSE;
}
xml = virBufferContentAndReset(&buf);
pool = virStoragePoolCreateXML(ctl->conn, xml, 0);
free (xml);
if (pool != NULL) { if (pool != NULL) {
vshPrint(ctl, _("Pool %s created\n"), name); vshPrint(ctl, _("Pool %s created\n"), name);
@ -2949,7 +2945,7 @@ cmdPoolCreateAs(vshControl * ctl, vshCmd * cmd)
} }
cleanup: cleanup:
free(buf.content); free(virBufferContentAndReset(&buf));
return FALSE; return FALSE;
} }
@ -3028,10 +3024,9 @@ cmdPoolDefineAs(vshControl * ctl, vshCmd * cmd)
{ {
virStoragePoolPtr pool; virStoragePoolPtr pool;
int found; int found;
char *xml;
char *name, *type, *srcHost, *srcPath, *srcDev, *target; char *name, *type, *srcHost, *srcPath, *srcDev, *target;
virBuffer buf; virBuffer buf = VIR_BUFFER_INITIALIZER;
memset(&buf, 0, sizeof(buf));
if (!vshConnectionUsability(ctl, ctl->conn, TRUE)) if (!vshConnectionUsability(ctl, ctl->conn, TRUE))
return FALSE; return FALSE;
@ -3048,39 +3043,35 @@ cmdPoolDefineAs(vshControl * ctl, vshCmd * cmd)
srcDev = vshCommandOptString(cmd, "source-dev", &found); srcDev = vshCommandOptString(cmd, "source-dev", &found);
target = vshCommandOptString(cmd, "target", &found); target = vshCommandOptString(cmd, "target", &found);
if (virBufferVSprintf(&buf, "<pool type='%s'>\n", type) < 0) virBufferVSprintf(&buf, "<pool type='%s'>\n", type);
goto cleanup; virBufferVSprintf(&buf, " <name>%s</name>\n", name);
if (virBufferVSprintf(&buf, " <name>%s</name>\n", name) < 0)
goto cleanup;
if (srcHost || srcPath || srcDev) { if (srcHost || srcPath || srcDev) {
if (virBufferAddLit(&buf, " <source>\n") < 0) virBufferAddLit(&buf, " <source>\n");
goto cleanup; if (srcHost)
if (srcHost && virBufferVSprintf(&buf, " <host>%s</host>\n", srcHost);
virBufferVSprintf(&buf, " <host>%s</host>\n", srcHost) < 0) if (srcPath)
goto cleanup; virBufferVSprintf(&buf, " <path>%s</path>\n", srcPath);
if (srcPath && if (srcDev)
virBufferVSprintf(&buf, " <path>%s</path>\n", srcPath) < 0) virBufferVSprintf(&buf, " <device>%s</device>\n", srcDev);
goto cleanup;
if (srcDev &&
virBufferVSprintf(&buf, " <device>%s</device>\n", srcDev) < 0)
goto cleanup;
if (virBufferAddLit(&buf, " </source>\n") < 0) virBufferAddLit(&buf, " </source>\n");
goto cleanup;
} }
if (target) { if (target) {
if (virBufferAddLit(&buf, " <target>\n") < 0) virBufferAddLit(&buf, " <target>\n");
goto cleanup; virBufferVSprintf(&buf, " <path>%s</path>\n", target);
if (virBufferVSprintf(&buf, " <path>%s</path>\n", target) < 0) virBufferAddLit(&buf, " </target>\n");
goto cleanup;
if (virBufferAddLit(&buf, " </target>\n") < 0)
goto cleanup;
} }
if (virBufferAddLit(&buf, "</pool>\n") < 0) virBufferAddLit(&buf, "</pool>\n");
goto cleanup;
pool = virStoragePoolDefineXML(ctl->conn, buf.content, 0);
free (buf.content); if (virBufferError(&buf)) {
vshPrint(ctl, "%s", _("Failed to allocate XML buffer"));
return FALSE;
}
xml = virBufferContentAndReset(&buf);
pool = virStoragePoolDefineXML(ctl->conn, xml, 0);
free (xml);
if (pool != NULL) { if (pool != NULL) {
vshPrint(ctl, _("Pool %s defined\n"), name); vshPrint(ctl, _("Pool %s defined\n"), name);
@ -3092,7 +3083,7 @@ cmdPoolDefineAs(vshControl * ctl, vshCmd * cmd)
} }
cleanup: cleanup:
free(buf.content); free(virBufferContentAndReset(&buf));
return FALSE; return FALSE;
} }
@ -3641,11 +3632,10 @@ cmdVolCreateAs(vshControl * ctl, vshCmd * cmd)
virStoragePoolPtr pool; virStoragePoolPtr pool;
virStorageVolPtr vol; virStorageVolPtr vol;
int found; int found;
char *xml;
char *name, *capacityStr, *allocationStr, *format; char *name, *capacityStr, *allocationStr, *format;
unsigned long long capacity, allocation = 0; unsigned long long capacity, allocation = 0;
virBuffer buf; virBuffer buf = VIR_BUFFER_INITIALIZER;
memset(&buf, 0, sizeof(buf));
if (!vshConnectionUsability(ctl, ctl->conn, TRUE)) if (!vshConnectionUsability(ctl, ctl->conn, TRUE))
return FALSE; return FALSE;
@ -3671,30 +3661,28 @@ cmdVolCreateAs(vshControl * ctl, vshCmd * cmd)
format = vshCommandOptString(cmd, "format", &found); format = vshCommandOptString(cmd, "format", &found);
if (virBufferAddLit(&buf, "<volume>\n") < 0) virBufferAddLit(&buf, "<volume>\n");
goto cleanup; virBufferVSprintf(&buf, " <name>%s</name>\n", name);
if (virBufferVSprintf(&buf, " <name>%s</name>\n", name) < 0) virBufferVSprintf(&buf, " <capacity>%llu</capacity>\n", capacity);
goto cleanup; if (allocationStr)
if (virBufferVSprintf(&buf, " <capacity>%llu</capacity>\n", capacity) < 0) virBufferVSprintf(&buf, " <allocation>%llu</allocation>\n", allocation);
goto cleanup;
if (allocationStr &&
virBufferVSprintf(&buf, " <allocation>%llu</allocation>\n", allocation) < 0)
goto cleanup;
if (format) { if (format) {
if (virBufferAddLit(&buf, " <target>\n") < 0) virBufferAddLit(&buf, " <target>\n");
goto cleanup;
if (format) if (format)
if (virBufferVSprintf(&buf, " <format type='%s'/>\n",format) < 0) virBufferVSprintf(&buf, " <format type='%s'/>\n",format);
goto cleanup; virBufferAddLit(&buf, " </target>\n");
if (virBufferAddLit(&buf, " </target>\n") < 0)
goto cleanup;
} }
if (virBufferAddLit(&buf, "</volume>\n") < 0) virBufferAddLit(&buf, "</volume>\n");
goto cleanup;
vol = virStorageVolCreateXML(pool, buf.content, 0);
free (buf.content); if (virBufferError(&buf)) {
vshPrint(ctl, "%s", _("Failed to allocate XML buffer"));
return FALSE;
}
xml = virBufferContentAndReset(&buf);
vol = virStorageVolCreateXML(pool, xml, 0);
free (xml);
virStoragePoolFree(pool); virStoragePoolFree(pool);
if (vol != NULL) { if (vol != NULL) {
@ -3707,7 +3695,7 @@ cmdVolCreateAs(vshControl * ctl, vshCmd * cmd)
} }
cleanup: cleanup:
free(buf.content); free(virBufferContentAndReset(&buf));
virStoragePoolFree(pool); virStoragePoolFree(pool);
return FALSE; return FALSE;
} }

View File

@ -608,16 +608,9 @@ xend_op_ext2(virConnectPtr xend, const char *path, char *error,
size_t n_error, const char *key, va_list ap) size_t n_error, const char *key, va_list ap)
{ {
const char *k = key, *v; const char *k = key, *v;
virBuffer buf; virBuffer buf = VIR_BUFFER_INITIALIZER;
int ret; int ret;
char *content;
buf.content = malloc(1000);
if (buf.content == NULL) {
virXendError(xend, VIR_ERR_NO_MEMORY, _("allocate new buffer"));
return -1;
}
buf.size = 1000;
buf.use = 0;
while (k) { while (k) {
v = va_arg(ap, const char *); v = va_arg(ap, const char *);
@ -631,8 +624,14 @@ xend_op_ext2(virConnectPtr xend, const char *path, char *error,
virBufferVSprintf(&buf, "%s", "&"); virBufferVSprintf(&buf, "%s", "&");
} }
ret = http2unix(xend, xend_post(xend, path, buf.content, error, n_error)); if (virBufferError(&buf)) {
free(buf.content); virXendError(NULL, VIR_ERR_NO_MEMORY, _("allocate buffer"));
return -1;
}
content = virBufferContentAndReset(&buf);
ret = http2unix(xend, xend_post(xend, path, content, error, n_error));
free(content);
return ret; return ret;
} }
@ -1437,13 +1436,11 @@ xend_parse_sexp_desc_char(virConnectPtr conn,
if (STREQ(devtype, "console") && if (STREQ(devtype, "console") &&
STREQ(type, "pty") && STREQ(type, "pty") &&
tty != NULL) { tty != NULL) {
if (virBufferVSprintf(buf, " <%s type='%s' tty='%s'>\n", virBufferVSprintf(buf, " <%s type='%s' tty='%s'>\n",
devtype, type, tty) < 0) devtype, type, tty);
goto no_memory;
} else { } else {
if (virBufferVSprintf(buf, " <%s type='%s'>\n", virBufferVSprintf(buf, " <%s type='%s'>\n",
devtype, type) < 0) devtype, type);
goto no_memory;
} }
if (STREQ(type, "null") || if (STREQ(type, "null") ||
@ -1451,15 +1448,13 @@ xend_parse_sexp_desc_char(virConnectPtr conn,
STREQ(type, "stdio")) { STREQ(type, "stdio")) {
/* no source needed */ /* no source needed */
} else if (STREQ(type, "pty")) { } else if (STREQ(type, "pty")) {
if (tty && if (tty)
virBufferVSprintf(buf, " <source path='%s'/>\n", virBufferVSprintf(buf, " <source path='%s'/>\n",
tty) < 0) tty);
goto no_memory;
} else if (STREQ(type, "file") || } else if (STREQ(type, "file") ||
STREQ(type, "pipe")) { STREQ(type, "pipe")) {
if (virBufferVSprintf(buf, " <source path='%s'/>\n", virBufferVSprintf(buf, " <source path='%s'/>\n",
value) < 0) value);
goto no_memory;
} else if (STREQ(type, "tcp")) { } else if (STREQ(type, "tcp")) {
const char *offset = strchr(value, ':'); const char *offset = strchr(value, ':');
const char *offset2; const char *offset2;
@ -1490,20 +1485,17 @@ xend_parse_sexp_desc_char(virConnectPtr conn,
protocol = telnet ? "telnet":"raw"; protocol = telnet ? "telnet":"raw";
if (bindHost) { if (bindHost) {
if (virBufferVSprintf(buf, virBufferVSprintf(buf,
" <source mode='%s' host='%s' service='%s'/>\n", " <source mode='%s' host='%s' service='%s'/>\n",
mode, bindHost, bindPort) < 0) mode, bindHost, bindPort);
goto no_memory;
} else { } else {
if (virBufferVSprintf(buf, virBufferVSprintf(buf,
" <source mode='%s' service='%s'/>\n", " <source mode='%s' service='%s'/>\n",
mode, bindPort) < 0) mode, bindPort);
goto no_memory;
} }
if (virBufferVSprintf(buf, virBufferVSprintf(buf,
" <protocol type='%s'/>\n", " <protocol type='%s'/>\n",
protocol) < 0) protocol);
goto no_memory;
} else if (STREQ(type, "udp")) { } else if (STREQ(type, "udp")) {
const char *offset = strchr(value, ':'); const char *offset = strchr(value, ':');
const char *offset2, *offset3; const char *offset2, *offset3;
@ -1543,28 +1535,24 @@ xend_parse_sexp_desc_char(virConnectPtr conn,
if (connectPort) { if (connectPort) {
if (connectHost) { if (connectHost) {
if (virBufferVSprintf(buf, virBufferVSprintf(buf,
" <source mode='connect' host='%s' service='%s'/>\n", " <source mode='connect' host='%s' service='%s'/>\n",
connectHost, connectPort) < 0) connectHost, connectPort);
goto no_memory;
} else { } else {
if (virBufferVSprintf(buf, virBufferVSprintf(buf,
" <source mode='connect' service='%s'/>\n", " <source mode='connect' service='%s'/>\n",
connectPort) < 0) connectPort);
goto no_memory;
} }
} }
if (bindPort) { if (bindPort) {
if (bindHost) { if (bindHost) {
if (virBufferVSprintf(buf, virBufferVSprintf(buf,
" <source mode='bind' host='%s' service='%s'/>\n", " <source mode='bind' host='%s' service='%s'/>\n",
bindHost, bindPort) < 0) bindHost, bindPort);
goto no_memory;
} else { } else {
if (virBufferVSprintf(buf, virBufferVSprintf(buf,
" <source mode='bind' service='%s'/>\n", " <source mode='bind' service='%s'/>\n",
bindPort) < 0) bindPort);
goto no_memory;
} }
} }
@ -1582,18 +1570,15 @@ xend_parse_sexp_desc_char(virConnectPtr conn,
strstr(offset, ",listen") != NULL) strstr(offset, ",listen") != NULL)
dolisten = 1; dolisten = 1;
if (virBufferVSprintf(buf, " <source mode='%s' path='%s'/>\n", virBufferVSprintf(buf, " <source mode='%s' path='%s'/>\n",
dolisten ? "bind" : "connect", path) < 0) dolisten ? "bind" : "connect", path);
goto no_memory;
} }
if (virBufferVSprintf(buf, " <target port='%d'/>\n", virBufferVSprintf(buf, " <target port='%d'/>\n",
portNum) < 0) portNum);
goto no_memory;
if (virBufferVSprintf(buf, " </%s>\n", virBufferVSprintf(buf, " </%s>\n",
devtype) < 0) devtype);
goto no_memory;
ret = 0; ret = 0;
@ -1635,7 +1620,7 @@ xend_parse_sexp_desc(virConnectPtr conn, struct sexpr *root,
struct sexpr *cur, *node; struct sexpr *cur, *node;
const char *tmp; const char *tmp;
char *tty; char *tty;
virBuffer buf; virBuffer buf = VIR_BUFFER_INITIALIZER;
int hvm = 0, bootloader = 0, vfb = 0; int hvm = 0, bootloader = 0, vfb = 0;
int domid = -1; int domid = -1;
int max_mem, cur_mem; int max_mem, cur_mem;
@ -1647,11 +1632,6 @@ xend_parse_sexp_desc(virConnectPtr conn, struct sexpr *root,
/* ERROR */ /* ERROR */
return (NULL); return (NULL);
} }
buf.content = malloc(4000);
if (buf.content == NULL)
return (NULL);
buf.size = 4000;
buf.use = 0;
tmp = sexpr_node(root, "domain/domid"); tmp = sexpr_node(root, "domain/domid");
if (tmp == NULL && xendConfigVersion < 3) { /* Old XenD, domid was mandatory */ if (tmp == NULL && xendConfigVersion < 3) { /* Old XenD, domid was mandatory */
@ -2097,11 +2077,15 @@ xend_parse_sexp_desc(virConnectPtr conn, struct sexpr *root,
virBufferAddLit(&buf, " </devices>\n"); virBufferAddLit(&buf, " </devices>\n");
virBufferAddLit(&buf, "</domain>\n"); virBufferAddLit(&buf, "</domain>\n");
buf.content[buf.use] = 0; if (virBufferError(&buf)) {
return (buf.content); virXendError(conn, VIR_ERR_NO_MEMORY, _("allocate buffer"));
return NULL;
}
return virBufferContentAndReset(&buf);
error: error:
free(buf.content); free(virBufferContentAndReset(&buf));
return (NULL); return (NULL);
} }

View File

@ -579,8 +579,7 @@ int xenXMDomainGetInfo(virDomainPtr domain, virDomainInfoPtr info) {
* domain, suitable for later feeding for virDomainCreateLinux * domain, suitable for later feeding for virDomainCreateLinux
*/ */
char *xenXMDomainFormatXML(virConnectPtr conn, virConfPtr conf) { char *xenXMDomainFormatXML(virConnectPtr conn, virConfPtr conf) {
virBufferPtr buf; virBuffer buf = VIR_BUFFER_INITIALIZER;
char *xml;
const char *name; const char *name;
unsigned char uuid[VIR_UUID_BUFLEN]; unsigned char uuid[VIR_UUID_BUFLEN];
char uuidstr[VIR_UUID_STRING_BUFLEN]; char uuidstr[VIR_UUID_STRING_BUFLEN];
@ -602,12 +601,10 @@ char *xenXMDomainFormatXML(virConnectPtr conn, virConfPtr conf) {
if (xenXMConfigGetUUID(conf, "uuid", uuid) < 0) if (xenXMConfigGetUUID(conf, "uuid", uuid) < 0)
return (NULL); return (NULL);
buf = virBufferNew(4096); virBufferAddLit(&buf, "<domain type='xen'>\n");
virBufferVSprintf(&buf, " <name>%s</name>\n", name);
virBufferAddLit(buf, "<domain type='xen'>\n");
virBufferVSprintf(buf, " <name>%s</name>\n", name);
virUUIDFormat(uuid, uuidstr); virUUIDFormat(uuid, uuidstr);
virBufferVSprintf(buf, " <uuid>%s</uuid>\n", uuidstr); virBufferVSprintf(&buf, " <uuid>%s</uuid>\n", uuidstr);
if ((xenXMConfigGetString(conf, "builder", &str) == 0) && if ((xenXMConfigGetString(conf, "builder", &str) == 0) &&
!strcmp(str, "hvm")) !strcmp(str, "hvm"))
@ -615,10 +612,10 @@ char *xenXMDomainFormatXML(virConnectPtr conn, virConfPtr conf) {
if (hvm) { if (hvm) {
const char *boot; const char *boot;
virBufferAddLit(buf, " <os>\n"); virBufferAddLit(&buf, " <os>\n");
virBufferAddLit(buf, " <type>hvm</type>\n"); virBufferAddLit(&buf, " <type>hvm</type>\n");
if (xenXMConfigGetString(conf, "kernel", &str) == 0) if (xenXMConfigGetString(conf, "kernel", &str) == 0)
virBufferVSprintf(buf, " <loader>%s</loader>\n", str); virBufferVSprintf(&buf, " <loader>%s</loader>\n", str);
if (xenXMConfigGetString(conf, "boot", &boot) < 0) if (xenXMConfigGetString(conf, "boot", &boot) < 0)
boot = "c"; boot = "c";
@ -637,90 +634,90 @@ char *xenXMDomainFormatXML(virConnectPtr conn, virConfPtr conf) {
dev = "hd"; dev = "hd";
break; break;
} }
virBufferVSprintf(buf, " <boot dev='%s'/>\n", dev); virBufferVSprintf(&buf, " <boot dev='%s'/>\n", dev);
boot++; boot++;
} }
virBufferAddLit(buf, " </os>\n"); virBufferAddLit(&buf, " </os>\n");
} else { } else {
if (xenXMConfigGetString(conf, "bootloader", &str) == 0) if (xenXMConfigGetString(conf, "bootloader", &str) == 0)
virBufferVSprintf(buf, " <bootloader>%s</bootloader>\n", str); virBufferVSprintf(&buf, " <bootloader>%s</bootloader>\n", str);
if (xenXMConfigGetString(conf, "bootargs", &str) == 0) if (xenXMConfigGetString(conf, "bootargs", &str) == 0)
virBufferEscapeString(buf, " <bootloader_args>%s</bootloader_args>\n", str); virBufferEscapeString(&buf, " <bootloader_args>%s</bootloader_args>\n", str);
if (xenXMConfigGetString(conf, "kernel", &str) == 0) { if (xenXMConfigGetString(conf, "kernel", &str) == 0) {
virBufferAddLit(buf, " <os>\n"); virBufferAddLit(&buf, " <os>\n");
virBufferAddLit(buf, " <type>linux</type>\n"); virBufferAddLit(&buf, " <type>linux</type>\n");
virBufferVSprintf(buf, " <kernel>%s</kernel>\n", str); virBufferVSprintf(&buf, " <kernel>%s</kernel>\n", str);
if (xenXMConfigGetString(conf, "ramdisk", &str) == 0) if (xenXMConfigGetString(conf, "ramdisk", &str) == 0)
virBufferVSprintf(buf, " <initrd>%s</initrd>\n", str); virBufferVSprintf(&buf, " <initrd>%s</initrd>\n", str);
if (xenXMConfigGetString(conf, "extra", &str) == 0) if (xenXMConfigGetString(conf, "extra", &str) == 0)
virBufferEscapeString(buf, " <cmdline>%s</cmdline>\n", str); virBufferEscapeString(&buf, " <cmdline>%s</cmdline>\n", str);
virBufferAddLit(buf, " </os>\n"); virBufferAddLit(&buf, " </os>\n");
} }
} }
if (xenXMConfigGetInt(conf, "memory", &val) < 0) if (xenXMConfigGetInt(conf, "memory", &val) < 0)
val = MIN_XEN_GUEST_SIZE * 2; val = MIN_XEN_GUEST_SIZE * 2;
virBufferVSprintf(buf, " <currentMemory>%ld</currentMemory>\n", virBufferVSprintf(&buf, " <currentMemory>%ld</currentMemory>\n",
val * 1024); val * 1024);
if (xenXMConfigGetInt(conf, "maxmem", &val) < 0) if (xenXMConfigGetInt(conf, "maxmem", &val) < 0)
if (xenXMConfigGetInt(conf, "memory", &val) < 0) if (xenXMConfigGetInt(conf, "memory", &val) < 0)
val = MIN_XEN_GUEST_SIZE * 2; val = MIN_XEN_GUEST_SIZE * 2;
virBufferVSprintf(buf, " <memory>%ld</memory>\n", val * 1024); virBufferVSprintf(&buf, " <memory>%ld</memory>\n", val * 1024);
virBufferAddLit(buf, " <vcpu"); virBufferAddLit(&buf, " <vcpu");
if (xenXMConfigGetString(conf, "cpus", &str) == 0) { if (xenXMConfigGetString(conf, "cpus", &str) == 0) {
char *ranges; char *ranges;
ranges = virConvertCpuSet(conn, str, 0); ranges = virConvertCpuSet(conn, str, 0);
if (ranges != NULL) { if (ranges != NULL) {
virBufferVSprintf(buf, " cpuset='%s'", ranges); virBufferVSprintf(&buf, " cpuset='%s'", ranges);
free(ranges); free(ranges);
} else } else
virBufferVSprintf(buf, " cpuset='%s'", str); virBufferVSprintf(&buf, " cpuset='%s'", str);
} }
if (xenXMConfigGetInt(conf, "vcpus", &val) < 0) if (xenXMConfigGetInt(conf, "vcpus", &val) < 0)
val = 1; val = 1;
virBufferVSprintf(buf, ">%ld</vcpu>\n", val); virBufferVSprintf(&buf, ">%ld</vcpu>\n", val);
if (xenXMConfigGetString(conf, "on_poweroff", &str) < 0) if (xenXMConfigGetString(conf, "on_poweroff", &str) < 0)
str = "destroy"; str = "destroy";
virBufferVSprintf(buf, " <on_poweroff>%s</on_poweroff>\n", str); virBufferVSprintf(&buf, " <on_poweroff>%s</on_poweroff>\n", str);
if (xenXMConfigGetString(conf, "on_reboot", &str) < 0) if (xenXMConfigGetString(conf, "on_reboot", &str) < 0)
str = "restart"; str = "restart";
virBufferVSprintf(buf, " <on_reboot>%s</on_reboot>\n", str); virBufferVSprintf(&buf, " <on_reboot>%s</on_reboot>\n", str);
if (xenXMConfigGetString(conf, "on_crash", &str) < 0) if (xenXMConfigGetString(conf, "on_crash", &str) < 0)
str = "restart"; str = "restart";
virBufferVSprintf(buf, " <on_crash>%s</on_crash>\n", str); virBufferVSprintf(&buf, " <on_crash>%s</on_crash>\n", str);
if (hvm) { if (hvm) {
virBufferAddLit(buf, " <features>\n"); virBufferAddLit(&buf, " <features>\n");
if (xenXMConfigGetInt(conf, "pae", &val) == 0 && if (xenXMConfigGetInt(conf, "pae", &val) == 0 &&
val) val)
virBufferAddLit(buf, " <pae/>\n"); virBufferAddLit(&buf, " <pae/>\n");
if (xenXMConfigGetInt(conf, "acpi", &val) == 0 && if (xenXMConfigGetInt(conf, "acpi", &val) == 0 &&
val) val)
virBufferAddLit(buf, " <acpi/>\n"); virBufferAddLit(&buf, " <acpi/>\n");
if (xenXMConfigGetInt(conf, "apic", &val) == 0 && if (xenXMConfigGetInt(conf, "apic", &val) == 0 &&
val) val)
virBufferAddLit(buf, " <apic/>\n"); virBufferAddLit(&buf, " <apic/>\n");
virBufferAddLit(buf, " </features>\n"); virBufferAddLit(&buf, " </features>\n");
if (xenXMConfigGetInt(conf, "localtime", &val) < 0) if (xenXMConfigGetInt(conf, "localtime", &val) < 0)
val = 0; val = 0;
virBufferVSprintf(buf, " <clock offset='%s'/>\n", val ? "localtime" : "utc"); virBufferVSprintf(&buf, " <clock offset='%s'/>\n", val ? "localtime" : "utc");
} }
virBufferAddLit(buf, " <devices>\n"); virBufferAddLit(&buf, " <devices>\n");
if (hvm) { if (hvm) {
if (xenXMConfigGetString(conf, "device_model", &str) == 0) if (xenXMConfigGetString(conf, "device_model", &str) == 0)
virBufferVSprintf(buf, " <emulator>%s</emulator>\n", str); virBufferVSprintf(&buf, " <emulator>%s</emulator>\n", str);
} }
list = virConfGetValue(conf, "disk"); list = virConfGetValue(conf, "disk");
@ -808,23 +805,23 @@ char *xenXMDomainFormatXML(virConnectPtr conn, virConfPtr conf) {
tmp[0] = '\0'; tmp[0] = '\0';
} }
virBufferVSprintf(buf, " <disk type='%s' device='%s'>\n", virBufferVSprintf(&buf, " <disk type='%s' device='%s'>\n",
block ? "block" : "file", block ? "block" : "file",
cdrom ? "cdrom" : "disk"); cdrom ? "cdrom" : "disk");
if (drvType[0]) if (drvType[0])
virBufferVSprintf(buf, " <driver name='%s' type='%s'/>\n", drvName, drvType); virBufferVSprintf(&buf, " <driver name='%s' type='%s'/>\n", drvName, drvType);
else else
virBufferVSprintf(buf, " <driver name='%s'/>\n", drvName); virBufferVSprintf(&buf, " <driver name='%s'/>\n", drvName);
if (src[0]) if (src[0])
virBufferVSprintf(buf, " <source %s='%s'/>\n", block ? "dev" : "file", src); virBufferVSprintf(&buf, " <source %s='%s'/>\n", block ? "dev" : "file", src);
virBufferVSprintf(buf, " <target dev='%s'/>\n", dev); virBufferVSprintf(&buf, " <target dev='%s'/>\n", dev);
if (!strcmp(head, "r") || if (!strcmp(head, "r") ||
!strcmp(head, "ro")) !strcmp(head, "ro"))
virBufferAddLit(buf, " <readonly/>\n"); virBufferAddLit(&buf, " <readonly/>\n");
else if ((!strcmp(head, "w!")) || else if ((!strcmp(head, "w!")) ||
(!strcmp(head, "!"))) (!strcmp(head, "!")))
virBufferAddLit(buf, " <shareable/>\n"); virBufferAddLit(&buf, " <shareable/>\n");
virBufferAddLit(buf, " </disk>\n"); virBufferAddLit(&buf, " </disk>\n");
skipdisk: skipdisk:
list = list->next; list = list->next;
@ -833,12 +830,12 @@ char *xenXMDomainFormatXML(virConnectPtr conn, virConfPtr conf) {
if (hvm && priv->xendConfigVersion == 1) { if (hvm && priv->xendConfigVersion == 1) {
if (xenXMConfigGetString(conf, "cdrom", &str) == 0) { if (xenXMConfigGetString(conf, "cdrom", &str) == 0) {
virBufferAddLit(buf, " <disk type='file' device='cdrom'>\n"); virBufferAddLit(&buf, " <disk type='file' device='cdrom'>\n");
virBufferAddLit(buf, " <driver name='file'/>\n"); virBufferAddLit(&buf, " <driver name='file'/>\n");
virBufferVSprintf(buf, " <source file='%s'/>\n", str); virBufferVSprintf(&buf, " <source file='%s'/>\n", str);
virBufferAddLit(buf, " <target dev='hdc'/>\n"); virBufferAddLit(&buf, " <target dev='hdc'/>\n");
virBufferAddLit(buf, " <readonly/>\n"); virBufferAddLit(&buf, " <readonly/>\n");
virBufferAddLit(buf, " </disk>\n"); virBufferAddLit(&buf, " </disk>\n");
} }
} }
@ -909,16 +906,16 @@ char *xenXMDomainFormatXML(virConnectPtr conn, virConfPtr conf) {
type = 1; type = 1;
} }
virBufferAddLit(buf, " <interface type='bridge'>\n"); virBufferAddLit(&buf, " <interface type='bridge'>\n");
if (mac[0]) if (mac[0])
virBufferVSprintf(buf, " <mac address='%s'/>\n", mac); virBufferVSprintf(&buf, " <mac address='%s'/>\n", mac);
if (type == 1 && bridge[0]) if (type == 1 && bridge[0])
virBufferVSprintf(buf, " <source bridge='%s'/>\n", bridge); virBufferVSprintf(&buf, " <source bridge='%s'/>\n", bridge);
if (script[0]) if (script[0])
virBufferVSprintf(buf, " <script path='%s'/>\n", script); virBufferVSprintf(&buf, " <script path='%s'/>\n", script);
if (ip[0]) if (ip[0])
virBufferVSprintf(buf, " <ip address='%s'/>\n", ip); virBufferVSprintf(&buf, " <ip address='%s'/>\n", ip);
virBufferAddLit(buf, " </interface>\n"); virBufferAddLit(&buf, " </interface>\n");
skipnic: skipnic:
list = list->next; list = list->next;
@ -928,9 +925,9 @@ char *xenXMDomainFormatXML(virConnectPtr conn, virConfPtr conf) {
if (hvm) { if (hvm) {
if (xenXMConfigGetString(conf, "usbdevice", &str) == 0 && str) { if (xenXMConfigGetString(conf, "usbdevice", &str) == 0 && str) {
if (!strcmp(str, "tablet")) if (!strcmp(str, "tablet"))
virBufferAddLit(buf, " <input type='tablet' bus='usb'/>\n"); virBufferAddLit(&buf, " <input type='tablet' bus='usb'/>\n");
else if (!strcmp(str, "mouse")) else if (!strcmp(str, "mouse"))
virBufferAddLit(buf, " <input type='mouse' bus='usb'/>\n"); virBufferAddLit(&buf, " <input type='mouse' bus='usb'/>\n");
/* Ignore else branch - probably some other non-input device we don't /* Ignore else branch - probably some other non-input device we don't
support in libvirt yet */ support in libvirt yet */
} }
@ -1003,54 +1000,56 @@ char *xenXMDomainFormatXML(virConnectPtr conn, virConfPtr conf) {
} }
if (vnc || sdl) { if (vnc || sdl) {
virBufferVSprintf(buf, " <input type='mouse' bus='%s'/>\n", hvm ? "ps2":"xen"); virBufferVSprintf(&buf, " <input type='mouse' bus='%s'/>\n", hvm ? "ps2":"xen");
} }
if (vnc) { if (vnc) {
virBufferVSprintf(buf, virBufferVSprintf(&buf,
" <graphics type='vnc' port='%ld'", " <graphics type='vnc' port='%ld'",
(vncunused ? -1 : 5900+vncdisplay)); (vncunused ? -1 : 5900+vncdisplay));
if (vnclisten) { if (vnclisten) {
virBufferVSprintf(buf, " listen='%s'", vnclisten); virBufferVSprintf(&buf, " listen='%s'", vnclisten);
} }
if (vncpasswd) { if (vncpasswd) {
virBufferVSprintf(buf, " passwd='%s'", vncpasswd); virBufferVSprintf(&buf, " passwd='%s'", vncpasswd);
} }
if (keymap) { if (keymap) {
virBufferVSprintf(buf, " keymap='%s'", keymap); virBufferVSprintf(&buf, " keymap='%s'", keymap);
} }
virBufferAddLit(buf, "/>\n"); virBufferAddLit(&buf, "/>\n");
} }
if (sdl) { if (sdl) {
virBufferAddLit(buf, " <graphics type='sdl'/>\n"); virBufferAddLit(&buf, " <graphics type='sdl'/>\n");
} }
if (hvm) { if (hvm) {
if (xenXMConfigGetString(conf, "parallel", &str) == 0) { if (xenXMConfigGetString(conf, "parallel", &str) == 0) {
if (STRNEQ(str, "none")) if (STRNEQ(str, "none"))
xend_parse_sexp_desc_char(conn, buf, "parallel", 0, str, NULL); xend_parse_sexp_desc_char(conn, &buf, "parallel", 0, str, NULL);
} }
if (xenXMConfigGetString(conf, "serial", &str) == 0) { if (xenXMConfigGetString(conf, "serial", &str) == 0) {
if (STRNEQ(str, "none")) { if (STRNEQ(str, "none")) {
xend_parse_sexp_desc_char(conn, buf, "serial", 0, str, NULL); xend_parse_sexp_desc_char(conn, &buf, "serial", 0, str, NULL);
/* Add back-compat console tag for primary console */ /* Add back-compat console tag for primary console */
xend_parse_sexp_desc_char(conn, buf, "console", 0, str, NULL); xend_parse_sexp_desc_char(conn, &buf, "console", 0, str, NULL);
} }
} }
} else { } else {
/* Paravirt implicitly always has a single console */ /* Paravirt implicitly always has a single console */
virBufferAddLit(buf, " <console type='pty'>\n"); virBufferAddLit(&buf, " <console type='pty'>\n");
virBufferAddLit(buf, " <target port='0'/>\n"); virBufferAddLit(&buf, " <target port='0'/>\n");
virBufferAddLit(buf, " </console>\n"); virBufferAddLit(&buf, " </console>\n");
} }
virBufferAddLit(buf, " </devices>\n"); virBufferAddLit(&buf, " </devices>\n");
virBufferAddLit(buf, "</domain>\n"); virBufferAddLit(&buf, "</domain>\n");
xml = buf->content; if (virBufferError(&buf)) {
buf->content = NULL; xenXMError(conn, VIR_ERR_NO_MEMORY, _("allocate buffer"));
virBufferFree(buf); return NULL;
return (xml); }
return virBufferContentAndReset(&buf);
} }
@ -1254,7 +1253,7 @@ int xenXMDomainPinVcpu(virDomainPtr domain,
{ {
const char *filename; const char *filename;
xenXMConfCachePtr entry; xenXMConfCachePtr entry;
virBufferPtr mapbuf; virBuffer mapbuf = VIR_BUFFER_INITIALIZER;
char *mapstr = NULL; char *mapstr = NULL;
char *ranges = NULL; char *ranges = NULL;
int i, j, n, comma = 0; int i, j, n, comma = 0;
@ -1288,33 +1287,24 @@ int xenXMDomainPinVcpu(virDomainPtr domain,
} }
/* from bit map, build character string of mapped CPU numbers */ /* from bit map, build character string of mapped CPU numbers */
mapbuf = virBufferNew (16);
if (mapbuf == NULL) {
xenXMError (domain->conn, VIR_ERR_NO_MEMORY, __FUNCTION__);
return -1;
}
for (i = 0; i < maplen; i++) for (i = 0; i < maplen; i++)
for (j = 0; j < 8; j++) for (j = 0; j < 8; j++)
if ((cpumap[i] & (1 << j))) { if ((cpumap[i] & (1 << j))) {
n = i*8 + j; n = i*8 + j;
if (comma) { if (comma)
if (virBufferAddLit (mapbuf, ",") == -1) { virBufferAddLit (&mapbuf, ",");
xenXMError (domain->conn, VIR_ERR_NO_MEMORY, __FUNCTION__);
virBufferFree (mapbuf);
return -1;
}
}
comma = 1; comma = 1;
if (virBufferVSprintf (mapbuf, "%d", n) == -1) { virBufferVSprintf (&mapbuf, "%d", n);
xenXMError (domain->conn, VIR_ERR_NO_MEMORY, __FUNCTION__);
virBufferFree (mapbuf);
return -1;
}
} }
mapstr = virBufferContentAndFree (mapbuf); if (virBufferError(&mapbuf)) {
xenXMError(domain->conn, VIR_ERR_NO_MEMORY, _("allocate buffer"));
return -1;
}
mapstr = virBufferContentAndReset(&mapbuf);
/* convert the mapstr to a range based string */ /* convert the mapstr to a range based string */
ranges = virConvertCpuSet(domain->conn, mapstr, 0); ranges = virConvertCpuSet(domain->conn, mapstr, 0);

101
src/xml.c
View File

@ -102,19 +102,13 @@ parseCpuNumber(const char **str, int maxcpu)
char * char *
virSaveCpuSet(virConnectPtr conn, char *cpuset, int maxcpu) virSaveCpuSet(virConnectPtr conn, char *cpuset, int maxcpu)
{ {
virBufferPtr buf; virBuffer buf = VIR_BUFFER_INITIALIZER;
char *ret;
int start, cur; int start, cur;
int first = 1; int first = 1;
if ((cpuset == NULL) || (maxcpu <= 0) || (maxcpu > 100000)) if ((cpuset == NULL) || (maxcpu <= 0) || (maxcpu > 100000))
return (NULL); return (NULL);
buf = virBufferNew(1000);
if (buf == NULL) {
virXMLError(conn, VIR_ERR_NO_MEMORY, _("allocate buffer"), 1000);
return (NULL);
}
cur = 0; cur = 0;
start = -1; start = -1;
while (cur < maxcpu) { while (cur < maxcpu) {
@ -123,27 +117,32 @@ virSaveCpuSet(virConnectPtr conn, char *cpuset, int maxcpu)
start = cur; start = cur;
} else if (start != -1) { } else if (start != -1) {
if (!first) if (!first)
virBufferAddLit(buf, ","); virBufferAddLit(&buf, ",");
else else
first = 0; first = 0;
if (cur == start + 1) if (cur == start + 1)
virBufferVSprintf(buf, "%d", start); virBufferVSprintf(&buf, "%d", start);
else else
virBufferVSprintf(buf, "%d-%d", start, cur - 1); virBufferVSprintf(&buf, "%d-%d", start, cur - 1);
start = -1; start = -1;
} }
cur++; cur++;
} }
if (start != -1) { if (start != -1) {
if (!first) if (!first)
virBufferAddLit(buf, ","); virBufferAddLit(&buf, ",");
if (maxcpu == start + 1) if (maxcpu == start + 1)
virBufferVSprintf(buf, "%d", start); virBufferVSprintf(&buf, "%d", start);
else else
virBufferVSprintf(buf, "%d-%d", start, maxcpu - 1); virBufferVSprintf(&buf, "%d-%d", start, maxcpu - 1);
} }
ret = virBufferContentAndFree(buf);
return (ret); if (virBufferError(&buf)) {
virXMLError(conn, VIR_ERR_NO_MEMORY, _("allocate buffer"), 1000);
return NULL;
}
return virBufferContentAndReset(&buf);
} }
/** /**
@ -1054,11 +1053,9 @@ virDomainParseXMLOSDescHVM(virConnectPtr conn, xmlNodePtr node,
char scratch[PATH_MAX]; char scratch[PATH_MAX];
if (virDomainParseXMLOSDescHVMChar(conn, scratch, sizeof(scratch), cur) < 0) if (virDomainParseXMLOSDescHVMChar(conn, scratch, sizeof(scratch), cur) < 0)
goto error; goto error;
if (virBufferVSprintf(buf, "(parallel %s)", scratch) < 0) virBufferVSprintf(buf, "(parallel %s)", scratch);
goto no_memory;
} else { } else {
if (virBufferAddLit(buf, "(parallel none)") < 0) virBufferAddLit(buf, "(parallel none)");
goto no_memory;
} }
cur = virXPathNode("/domain/devices/serial[1]", ctxt); cur = virXPathNode("/domain/devices/serial[1]", ctxt);
@ -1066,8 +1063,7 @@ virDomainParseXMLOSDescHVM(virConnectPtr conn, xmlNodePtr node,
char scratch[PATH_MAX]; char scratch[PATH_MAX];
if (virDomainParseXMLOSDescHVMChar(conn, scratch, sizeof(scratch), cur) < 0) if (virDomainParseXMLOSDescHVMChar(conn, scratch, sizeof(scratch), cur) < 0)
goto error; goto error;
if (virBufferVSprintf(buf, "(serial %s)", scratch) < 0) virBufferVSprintf(buf, "(serial %s)", scratch);
goto no_memory;
} else { } else {
res = virXPathBoolean("count(domain/devices/console) > 0", ctxt); res = virXPathBoolean("count(domain/devices/console) > 0", ctxt);
if (res < 0) { if (res < 0) {
@ -1075,27 +1071,20 @@ virDomainParseXMLOSDescHVM(virConnectPtr conn, xmlNodePtr node,
goto error; goto error;
} }
if (res) { if (res) {
if (virBufferAddLit(buf, "(serial pty)") < 0) virBufferAddLit(buf, "(serial pty)");
goto no_memory;
} else { } else {
if (virBufferAddLit(buf, "(serial none)") < 0) virBufferAddLit(buf, "(serial none)");
goto no_memory;
} }
} }
str = virXPathString("string(/domain/clock/@offset)", ctxt); str = virXPathString("string(/domain/clock/@offset)", ctxt);
if (str != NULL && !strcmp(str, "localtime")) { if (str != NULL && STREQ(str, "localtime")) {
if (virBufferAddLit(buf, "(localtime 1)") < 0) virBufferAddLit(buf, "(localtime 1)");
goto no_memory;
} }
free(str); free(str);
return (0); return (0);
no_memory:
virXMLError(conn, VIR_ERR_XML_ERROR,
_("cannot allocate memory for buffer"), 0);
error: error:
free(nodes); free(nodes);
return (-1); return (-1);
@ -1509,7 +1498,7 @@ virDomainParseXMLDesc(virConnectPtr conn, const char *xmldesc, char **name,
xmlDocPtr xml = NULL; xmlDocPtr xml = NULL;
xmlNodePtr node; xmlNodePtr node;
char *nam = NULL; char *nam = NULL;
virBuffer buf; virBuffer buf = VIR_BUFFER_INITIALIZER;
xmlChar *prop; xmlChar *prop;
xmlParserCtxtPtr pctxt; xmlParserCtxtPtr pctxt;
xmlXPathContextPtr ctxt = NULL; xmlXPathContextPtr ctxt = NULL;
@ -1525,11 +1514,6 @@ virDomainParseXMLDesc(virConnectPtr conn, const char *xmldesc, char **name,
if (name != NULL) if (name != NULL)
*name = NULL; *name = NULL;
buf.content = malloc(1000);
if (buf.content == NULL)
return (NULL);
buf.size = 1000;
buf.use = 0;
pctxt = xmlNewParserCtxt(); pctxt = xmlNewParserCtxt();
if ((pctxt == NULL) || (pctxt->sax == NULL)) { if ((pctxt == NULL) || (pctxt->sax == NULL)) {
@ -1787,7 +1771,6 @@ virDomainParseXMLDesc(virConnectPtr conn, const char *xmldesc, char **name,
virBufferAddLit(&buf, ")"); /* closes (vm */ virBufferAddLit(&buf, ")"); /* closes (vm */
buf.content[buf.use] = 0;
xmlXPathFreeContext(ctxt); xmlXPathFreeContext(ctxt);
xmlFreeDoc(xml); xmlFreeDoc(xml);
@ -1798,7 +1781,12 @@ virDomainParseXMLDesc(virConnectPtr conn, const char *xmldesc, char **name,
else else
free(nam); free(nam);
return (buf.content); if (virBufferError(&buf)) {
virXMLError(conn, VIR_ERR_NO_MEMORY, _("allocate buffer"), 0);
return NULL;
}
return virBufferContentAndReset(&buf);
error: error:
free(nam); free(nam);
@ -1809,7 +1797,7 @@ virDomainParseXMLDesc(virConnectPtr conn, const char *xmldesc, char **name,
xmlFreeDoc(xml); xmlFreeDoc(xml);
if (pctxt != NULL) if (pctxt != NULL)
xmlFreeParserCtxt(pctxt); xmlFreeParserCtxt(pctxt);
free(buf.content); free(virBufferContentAndReset(&buf));
return (NULL); return (NULL);
} }
@ -1834,14 +1822,8 @@ virParseXMLDevice(virConnectPtr conn, const char *xmldesc, int hvm,
{ {
xmlDocPtr xml = NULL; xmlDocPtr xml = NULL;
xmlNodePtr node; xmlNodePtr node;
virBuffer buf; virBuffer buf = VIR_BUFFER_INITIALIZER;
buf.content = malloc(1000);
if (buf.content == NULL)
return (NULL);
buf.size = 1000;
buf.use = 0;
buf.content[0] = 0;
xml = xmlReadDoc((const xmlChar *) xmldesc, "device.xml", NULL, xml = xmlReadDoc((const xmlChar *) xmldesc, "device.xml", NULL,
XML_PARSE_NOENT | XML_PARSE_NONET | XML_PARSE_NOENT | XML_PARSE_NONET |
XML_PARSE_NOERROR | XML_PARSE_NOWARNING); XML_PARSE_NOERROR | XML_PARSE_NOWARNING);
@ -1856,9 +1838,6 @@ virParseXMLDevice(virConnectPtr conn, const char *xmldesc, int hvm,
if (virDomainParseXMLDiskDesc(conn, node, &buf, hvm, if (virDomainParseXMLDiskDesc(conn, node, &buf, hvm,
xendConfigVersion) != 0) xendConfigVersion) != 0)
goto error; goto error;
/* SXP is not created when device is "floppy". */
else if (buf.use == 0)
goto error;
} else if (xmlStrEqual(node->name, BAD_CAST "interface")) { } else if (xmlStrEqual(node->name, BAD_CAST "interface")) {
if (virDomainParseXMLIfDesc(conn, node, &buf, hvm, if (virDomainParseXMLIfDesc(conn, node, &buf, hvm,
xendConfigVersion) != 0) xendConfigVersion) != 0)
@ -1867,14 +1846,20 @@ virParseXMLDevice(virConnectPtr conn, const char *xmldesc, int hvm,
virXMLError(conn, VIR_ERR_XML_ERROR, (const char *) node->name, 0); virXMLError(conn, VIR_ERR_XML_ERROR, (const char *) node->name, 0);
goto error; goto error;
} }
cleanup:
if (xml != NULL) xmlFreeDoc(xml);
xmlFreeDoc(xml);
return buf.content; if (virBufferError(&buf)) {
virXMLError(conn, VIR_ERR_NO_MEMORY, _("allocate buffer"), 0);
return NULL;
}
return virBufferContentAndReset(&buf);
error: error:
free(buf.content); free(virBufferContentAndReset(&buf));
buf.content = NULL; xmlFreeDoc(xml);
goto cleanup; return NULL;
} }

View File

@ -363,14 +363,12 @@ void xmlRpcValueMarshal(xmlRpcValuePtr value, virBufferPtr buf, int indent)
virBufferStrcat(buf, "</value>\n", NULL); virBufferStrcat(buf, "</value>\n", NULL);
} }
virBufferPtr xmlRpcMarshalRequest(const char *request, void xmlRpcMarshalRequest(const char *request,
int argc, xmlRpcValuePtr *argv) virBufferPtr buf,
int argc, xmlRpcValuePtr *argv)
{ {
virBufferPtr buf;
int i; int i;
buf = virBufferNew(1024);
virBufferStrcat(buf, virBufferStrcat(buf,
"<?xml version=\"1.0\"?>\n" "<?xml version=\"1.0\"?>\n"
"<methodCall>\n" "<methodCall>\n"
@ -386,7 +384,6 @@ virBufferPtr xmlRpcMarshalRequest(const char *request,
virBufferStrcat(buf, virBufferStrcat(buf,
" </params>\n" " </params>\n"
"</methodCall>\n", NULL); "</methodCall>\n", NULL);
return buf;
} }
xmlRpcValuePtr xmlRpcUnmarshalResponse(xmlNodePtr node, bool *is_fault) xmlRpcValuePtr xmlRpcUnmarshalResponse(xmlNodePtr node, bool *is_fault)
@ -564,13 +561,14 @@ int xmlRpcCall(xmlRpcContextPtr context, const char *method,
va_list ap; va_list ap;
int argc; int argc;
xmlRpcValuePtr *argv; xmlRpcValuePtr *argv;
virBufferPtr buf; virBuffer buf = VIR_BUFFER_INITIALIZER;
char *ret; char *ret;
xmlDocPtr xml; xmlDocPtr xml;
xmlNodePtr node; xmlNodePtr node;
bool fault; bool fault;
xmlRpcValuePtr value; xmlRpcValuePtr value;
void *retval = NULL; void *retval = NULL;
char *content;
va_start(ap, fmt); va_start(ap, fmt);
@ -582,16 +580,16 @@ int xmlRpcCall(xmlRpcContextPtr context, const char *method,
va_end(ap); va_end(ap);
buf = xmlRpcMarshalRequest(method, argc, argv); xmlRpcMarshalRequest(method, &buf, argc, argv);
xmlRpcArgvFree(argc, argv); xmlRpcArgvFree(argc, argv);
if (!buf) if (virBufferError(&buf))
return -1; return -1;
ret = xmlRpcCallRaw(context->uri, buf->content); content = virBufferContentAndReset(&buf);
ret = xmlRpcCallRaw(context->uri, content);
virBufferFree(buf); free(content);
if (!ret) if (!ret)
return -1; return -1;

View File

@ -89,8 +89,9 @@ struct _xmlRpcContext;
xmlRpcValuePtr *xmlRpcArgvNew(const char *fmt, va_list ap, int *argc); xmlRpcValuePtr *xmlRpcArgvNew(const char *fmt, va_list ap, int *argc);
void xmlRpcArgvFree(int argc, xmlRpcValuePtr *argv); void xmlRpcArgvFree(int argc, xmlRpcValuePtr *argv);
virBufferPtr xmlRpcMarshalRequest(const char *request, void xmlRpcMarshalRequest(const char *request,
int argc, xmlRpcValuePtr *argv); virBufferPtr buf,
int argc, xmlRpcValuePtr *argv);
xmlRpcValuePtr xmlRpcUnmarshalResponse(xmlNodePtr node, bool *is_fault); xmlRpcValuePtr xmlRpcUnmarshalResponse(xmlNodePtr node, bool *is_fault);

View File

@ -59,22 +59,20 @@ testMethodPlusDOUBLE(const void *data)
return retval==(10.1234+10.1234) ? 0 : -1; return retval==(10.1234+10.1234) ? 0 : -1;
} }
static virBufferPtr static void
marshalRequest(const char *fmt, ...) marshalRequest(virBufferPtr buf, const char *fmt, ...)
{ {
int argc; int argc;
xmlRpcValuePtr *argv; xmlRpcValuePtr *argv;
virBufferPtr buf;
va_list ap; va_list ap;
va_start(ap, fmt); va_start(ap, fmt);
argv = xmlRpcArgvNew(fmt, ap, &argc); argv = xmlRpcArgvNew(fmt, ap, &argc);
va_end(ap); va_end(ap);
buf = xmlRpcMarshalRequest("test", argc, argv); xmlRpcMarshalRequest("test", buf, argc, argv);
xmlRpcArgvFree(argc, argv); xmlRpcArgvFree(argc, argv);
return buf;
} }
static int static int
@ -132,14 +130,21 @@ testMarshalRequestINT(const void *data)
int num = INT_MAX; int num = INT_MAX;
int ret = 0; int ret = 0;
int check = data ? *((int *)data) : 0; int check = data ? *((int *)data) : 0;
virBufferPtr buf = marshalRequest("i", num); virBuffer buf = VIR_BUFFER_INITIALIZER;
marshalRequest(&buf, "i", num);
char *content;
if (virBufferError(&buf))
return -1;
content = virBufferContentAndReset(&buf);
if (check) if (check)
ret = checkRequestValue(buf->content, ret = checkRequestValue(content,
"number(/methodCall/params/param[1]/value/int)", "number(/methodCall/params/param[1]/value/int)",
XML_RPC_INTEGER, (void *) &num); XML_RPC_INTEGER, (void *) &num);
virBufferFree(buf); free(content);
return ret; return ret;
} }
@ -149,13 +154,21 @@ testMarshalRequestSTRING(const void *data ATTRIBUTE_UNUSED)
const char *str = "This library will be really sexy."; const char *str = "This library will be really sexy.";
int ret = 0; int ret = 0;
int check = data ? *((int *)data) : 0; int check = data ? *((int *)data) : 0;
virBufferPtr buf = marshalRequest("s", str); virBuffer buf = VIR_BUFFER_INITIALIZER;
char *content;
marshalRequest(&buf, "s", str);
if (virBufferError(&buf))
return -1;
content = virBufferContentAndReset(&buf);
if (check) if (check)
ret = checkRequestValue(buf->content, ret = checkRequestValue(content,
"string(/methodCall/params/param[1]/value/string)", "string(/methodCall/params/param[1]/value/string)",
XML_RPC_STRING, (void *) str); XML_RPC_STRING, (void *) str);
virBufferFree(buf);
free(content);
return ret; return ret;
} }
@ -165,42 +178,24 @@ testMarshalRequestDOUBLE(const void *data)
double num = 123456789.123; double num = 123456789.123;
int ret = 0; int ret = 0;
int check = data ? *((int *)data) : 0; int check = data ? *((int *)data) : 0;
virBufferPtr buf = marshalRequest("f", num); virBuffer buf = VIR_BUFFER_INITIALIZER;
char *content;
marshalRequest(&buf, "f", num);
if (virBufferError(&buf))
return -1;
content = virBufferContentAndReset(&buf);
if (check) if (check)
ret = checkRequestValue(buf->content, ret = checkRequestValue(content,
"number(/methodCall/params/param[1]/value/double)", "number(/methodCall/params/param[1]/value/double)",
XML_RPC_DOUBLE, (void *) &num); XML_RPC_DOUBLE, (void *) &num);
virBufferFree(buf); free(content);
return ret; return ret;
} }
static int
testBufferStrcat(const void *data ATTRIBUTE_UNUSED)
{
virBufferPtr buf = virBufferNew(1000*32); /* don't waste time with realloc */
int i;
for (i=0; i < 1000; i++)
virBufferStrcat(buf, "My name is ", "libvirt", ".\n", NULL);
virBufferFree(buf);
return 0;
}
static int
testBufferVSprintf(const void *data ATTRIBUTE_UNUSED)
{
virBufferPtr buf = virBufferNew(1000*32); /* don't waste time with realloc */
int i;
for (i=0; i < 1000; i++)
virBufferVSprintf(buf, "My name is %s.\n", "libvirt");
virBufferFree(buf);
return 0;
}
int int
main(int argc, char **argv) main(int argc, char **argv)
@ -263,13 +258,7 @@ main(int argc, char **argv)
NLOOPS, testMarshalRequestSTRING, NULL) != 0) NLOOPS, testMarshalRequestSTRING, NULL) != 0)
ret = -1; ret = -1;
if (virtTestRun("Buffer: strcat", NLOOPS, testBufferStrcat, NULL) != 0) exit(ret==0 ? EXIT_SUCCESS : EXIT_FAILURE);
ret = -1;
if (virtTestRun("Buffer: sprintf", NLOOPS, testBufferVSprintf, NULL) != 0)
ret = -1;
exit(ret==0 ? EXIT_SUCCESS : EXIT_FAILURE);
} }