esx: Handle non-UTF-8 encoded VMX files

ESX(i) uses UTF-8, but a Windows based GSX server writes
Windows-1252 encoded VMX files.

Add a test case to ensure that libxml2 provides Windows-1252
to UTF-8 conversion.
This commit is contained in:
Matthias Bolte
2010-10-19 16:29:12 +02:00
parent f04de501bc
commit 1c61648961
4 changed files with 115 additions and 3 deletions
+41 -3
View File
@@ -612,9 +612,9 @@ esxUtil_ReformatUuid(const char *input, char *output)
unsigned char uuid[VIR_UUID_BUFLEN];
if (virUUIDParse(input, uuid) < 0) {
ESX_VI_ERROR(VIR_ERR_INTERNAL_ERROR,
_("Could not parse UUID from string '%s'"),
input);
ESX_ERROR(VIR_ERR_INTERNAL_ERROR,
_("Could not parse UUID from string '%s'"),
input);
return -1;
}
@@ -819,3 +819,41 @@ esxUtil_EscapeDatastoreItem(const char *string)
return escaped2;
}
char *
esxUtil_ConvertToUTF8(const char *encoding, const char *string)
{
char *result = NULL;
xmlCharEncodingHandlerPtr handler;
xmlBufferPtr input;
xmlBufferPtr utf8;
handler = xmlFindCharEncodingHandler(encoding);
if (handler == NULL) {
ESX_ERROR(VIR_ERR_INTERNAL_ERROR,
_("libxml2 doesn't handle %s encoding"), encoding);
return NULL;
}
input = xmlBufferCreateStatic((char *)string, strlen(string));
utf8 = xmlBufferCreate();
if (xmlCharEncInFunc(handler, utf8, input) < 0) {
ESX_ERROR(VIR_ERR_INTERNAL_ERROR,
_("Could not convert from %s to UTF-8 encoding"), encoding);
goto cleanup;
}
result = (char *)utf8->content;
utf8->content = NULL;
cleanup:
xmlCharEncCloseFunc(handler);
xmlBufferFree(input);
xmlBufferFree(utf8);
return result;
}
+2
View File
@@ -89,4 +89,6 @@ void esxUtil_ReplaceSpecialWindowsPathChars(char *string);
char *esxUtil_EscapeDatastoreItem(const char *string);
char *esxUtil_ConvertToUTF8(const char *encoding, const char *string);
#endif /* __ESX_UTIL_H__ */
+30
View File
@@ -868,6 +868,8 @@ esxVMX_ParseConfig(esxVMX_Context *ctx, virCapsPtr caps, const char *vmx,
{
bool success = false;
virConfPtr conf = NULL;
char *encoding = NULL;
char *utf8;
virDomainDefPtr def = NULL;
long long config_version = 0;
long long virtualHW_version = 0;
@@ -895,6 +897,33 @@ esxVMX_ParseConfig(esxVMX_Context *ctx, virCapsPtr caps, const char *vmx,
return NULL;
}
/* vmx:.encoding */
if (esxUtil_GetConfigString(conf, ".encoding", &encoding, true) < 0) {
goto cleanup;
}
if (encoding == NULL || STRCASEEQ(encoding, "UTF-8")) {
/* nothing */
} else {
virConfFree(conf);
conf = NULL;
utf8 = esxUtil_ConvertToUTF8(encoding, vmx);
if (utf8 == NULL) {
goto cleanup;
}
conf = virConfReadMem(utf8, strlen(utf8), VIR_CONF_FLAG_VMX_FORMAT);
VIR_FREE(utf8);
if (conf == NULL) {
goto cleanup;
}
}
/* Allocate domain def */
if (VIR_ALLOC(def) < 0) {
virReportOOMError();
return NULL;
@@ -1359,6 +1388,7 @@ esxVMX_ParseConfig(esxVMX_Context *ctx, virCapsPtr caps, const char *vmx,
}
virConfFree(conf);
VIR_FREE(encoding);
VIR_FREE(sched_cpu_affinity);
VIR_FREE(guestOS);