diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c index c299c0319a759ddd5030fcdc514ecab90344d8b0..7c1fb4767da1ead618ff264c43cfe77236f7d022 100644 --- a/src/conf/domain_conf.c +++ b/src/conf/domain_conf.c @@ -7148,75 +7148,12 @@ static int virDomainSysinfoDefFormat(virBufferPtr buf, virSysinfoDefPtr def) { - const char *type = virDomainSysinfoTypeToString(def->type); + char *format = virSysinfoFormat(def, " "); - if (!type) { - virDomainReportError(VIR_ERR_INTERNAL_ERROR, - _("unexpected sysinfo type model %d"), - def->type); + if (!format) return -1; - } - - virBufferVSprintf(buf, " \n", type); - if ((def->bios_vendor != NULL) || (def->bios_version != NULL) || - (def->bios_date != NULL) || (def->bios_release != NULL)) { - virBufferAddLit(buf, " \n"); - if (def->bios_vendor != NULL) - virBufferEscapeString(buf, - " %s\n", - def->bios_vendor); - if (def->bios_version != NULL) - virBufferEscapeString(buf, - " %s\n", - def->bios_version); - if (def->bios_date != NULL) - virBufferEscapeString(buf, - " %s\n", - def->bios_date); - if (def->bios_release != NULL) - virBufferEscapeString(buf, - " %s\n", - def->bios_release); - virBufferAddLit(buf, " \n"); - } - if ((def->system_manufacturer != NULL) || (def->system_product != NULL) || - (def->system_version != NULL) || (def->system_serial != NULL) || - (def->system_uuid != NULL) || (def->system_sku != NULL) || - (def->system_family != NULL)) { - virBufferAddLit(buf, " \n"); - if (def->system_manufacturer != NULL) - virBufferEscapeString(buf, - " %s\n", - def->system_manufacturer); - if (def->system_product != NULL) - virBufferEscapeString(buf, - " %s\n", - def->system_product); - if (def->system_version != NULL) - virBufferEscapeString(buf, - " %s\n", - def->system_version); - if (def->system_serial != NULL) - virBufferEscapeString(buf, - " %s\n", - def->system_serial); - if (def->system_uuid != NULL) - virBufferEscapeString(buf, - " %s\n", - def->system_uuid); - if (def->system_sku != NULL) - virBufferEscapeString(buf, - " %s\n", - def->system_sku); - if (def->system_family != NULL) - virBufferEscapeString(buf, - " %s\n", - def->system_family); - virBufferAddLit(buf, " \n"); - } - - virBufferAddLit(buf, " \n"); - + virBufferAdd(buf, format, strlen(format)); + VIR_FREE(format); return 0; } diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index 1bbd44e8dd91d5bdc1486ddd3707b306feaf12ec..b9e3efe50fffc20536a4b938e7accee8d8d8a072 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -800,6 +800,7 @@ virStorageFileProbeFormatFromFD; # sysinfo.h virSysinfoDefFree; +virSysinfoFormat; virSysinfoRead; diff --git a/src/util/sysinfo.c b/src/util/sysinfo.c index 48ab26702bc36adb0c11bc0c26398f81cd797ab8..2b764ae21c4942765f835ca114de373031deb9d2 100644 --- a/src/util/sysinfo.c +++ b/src/util/sysinfo.c @@ -1,7 +1,7 @@ /* * sysinfo.c: get SMBIOS/sysinfo information from the host * - * Copyright (C) 2010 Red Hat, Inc. + * Copyright (C) 2010-2011 Red Hat, Inc. * Copyright (C) 2010 Daniel Veillard * * This library is free software; you can redistribute it and/or @@ -91,7 +91,18 @@ virSysinfoRead(void) { _("Host sysinfo extraction not supported on this platform")); return NULL; } -#else + +char * +virSysinfoFormat(virSysinfoDefPtr def ATTRIBUTE_UNUSED, + const char *prefix ATTRIBUTE_UNUSED) +{ + virReportSystemError(ENOSYS, "%s", + _("Host sysinfo extraction not supported on this platform")); + return NULL; +} + +#else /* !WIN32 */ + virSysinfoDefPtr virSysinfoRead(void) { char *path, *cur, *eol, *base; @@ -207,4 +218,112 @@ no_memory: ret = NULL; goto cleanup; } -#endif + +/** + * virSysinfoFormat: + * @def: structure to convert to xml string + * @prefix: string to prefix before each line of xml + * + * This returns the XML description of the sysinfo, or NULL after + * generating an error message. + */ +char * +virSysinfoFormat(virSysinfoDefPtr def, const char *prefix) +{ + const char *type = virDomainSysinfoTypeToString(def->type); + virBuffer buf = VIR_BUFFER_INITIALIZER; + size_t len = strlen(prefix); + + if (!type) { + virSmbiosReportError(VIR_ERR_INTERNAL_ERROR, + _("unexpected sysinfo type model %d"), + def->type); + return NULL; + } + + virBufferVSprintf(&buf, "%s\n", prefix, type); + if ((def->bios_vendor != NULL) || (def->bios_version != NULL) || + (def->bios_date != NULL) || (def->bios_release != NULL)) { + virBufferVSprintf(&buf, "%s \n", prefix); + if (def->bios_vendor != NULL) { + virBufferAdd(&buf, prefix, len); + virBufferEscapeString(&buf, + " %s\n", + def->bios_vendor); + } + if (def->bios_version != NULL) { + virBufferAdd(&buf, prefix, len); + virBufferEscapeString(&buf, + " %s\n", + def->bios_version); + } + if (def->bios_date != NULL) { + virBufferAdd(&buf, prefix, len); + virBufferEscapeString(&buf, + " %s\n", + def->bios_date); + } + if (def->bios_release != NULL) { + virBufferAdd(&buf, prefix, len); + virBufferEscapeString(&buf, + " %s\n", + def->bios_release); + } + virBufferVSprintf(&buf, "%s \n", prefix); + } + if ((def->system_manufacturer != NULL) || (def->system_product != NULL) || + (def->system_version != NULL) || (def->system_serial != NULL) || + (def->system_uuid != NULL) || (def->system_sku != NULL) || + (def->system_family != NULL)) { + virBufferVSprintf(&buf, "%s \n", prefix); + if (def->system_manufacturer != NULL) { + virBufferAdd(&buf, prefix, len); + virBufferEscapeString(&buf, + " %s\n", + def->system_manufacturer); + } + if (def->system_product != NULL) { + virBufferAdd(&buf, prefix, len); + virBufferEscapeString(&buf, + " %s\n", + def->system_product); + } + if (def->system_version != NULL) { + virBufferAdd(&buf, prefix, len); + virBufferEscapeString(&buf, + " %s\n", + def->system_version); + } + if (def->system_serial != NULL) { + virBufferAdd(&buf, prefix, len); + virBufferEscapeString(&buf, + " %s\n", + def->system_serial); + } + if (def->system_uuid != NULL) { + virBufferAdd(&buf, prefix, len); + virBufferEscapeString(&buf, + " %s\n", + def->system_uuid); + } + if (def->system_sku != NULL) { + virBufferAdd(&buf, prefix, len); + virBufferEscapeString(&buf, + " %s\n", + def->system_sku); + } + if (def->system_family != NULL) { + virBufferAdd(&buf, prefix, len); + virBufferEscapeString(&buf, + " %s\n", + def->system_family); + } + virBufferVSprintf(&buf, "%s \n", prefix); + } + + virBufferVSprintf(&buf, "%s\n", prefix); + + return virBufferContentAndReset(&buf); +} + +#endif /* !WIN32 */ diff --git a/src/util/sysinfo.h b/src/util/sysinfo.h index 1af7ef6f73671d3d7f4008f3ae915ad746c1dad3..66a59db70bbefdbdf4091f31c08b82fe9c4415e9 100644 --- a/src/util/sysinfo.h +++ b/src/util/sysinfo.h @@ -1,7 +1,7 @@ /* * sysinfo.h: structure and entry points for sysinfo support * - * Copyright (C) 2010 Red Hat, Inc. + * Copyright (C) 2010-2011 Red Hat, Inc. * Copyright (C) 2010 Daniel Veillard * * This library is free software; you can redistribute it and/or @@ -56,4 +56,7 @@ virSysinfoDefPtr virSysinfoRead(void); void virSysinfoDefFree(virSysinfoDefPtr def); +char *virSysinfoFormat(virSysinfoDefPtr def, const char *prefix) + ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2); + #endif /* __VIR_SYSINFOS_H__ */