xml: print uuids in the warning

In the XML warning, we print a virsh command line that can be used to
edit that XML. This patch prints UUIDs if the entity name contains
special characters (like shell metacharacters, or "--" that would break
parsing of the XML comment). If the entity doesn't have a UUID, just
print the virsh command that can be used to edit it.
This commit is contained in:
Ján Tomko 2012-10-29 13:15:55 +01:00 committed by Peter Krempa
parent 23f5e74ed3
commit 0b121614a2
9 changed files with 42 additions and 20 deletions

View File

@ -14288,6 +14288,7 @@ int virDomainSaveXML(const char *configDir,
virDomainDefPtr def,
const char *xml)
{
char uuidstr[VIR_UUID_STRING_BUFLEN];
char *configFile = NULL;
int ret = -1;
@ -14301,7 +14302,10 @@ int virDomainSaveXML(const char *configDir,
goto cleanup;
}
ret = virXMLSaveFile(configFile, def->name, "edit", xml);
virUUIDFormat(def->uuid, uuidstr);
ret = virXMLSaveFile(configFile,
virXMLPickShellSafeComment(def->name, uuidstr), "edit",
xml);
cleanup:
VIR_FREE(configFile);

View File

@ -1978,6 +1978,7 @@ int virNetworkSaveXML(const char *configDir,
virNetworkDefPtr def,
const char *xml)
{
char uuidstr[VIR_UUID_STRING_BUFLEN];
char *configFile = NULL;
int ret = -1;
@ -1991,7 +1992,10 @@ int virNetworkSaveXML(const char *configDir,
goto cleanup;
}
ret = virXMLSaveFile(configFile, def->name, "net-edit", xml);
virUUIDFormat(def->uuid, uuidstr);
ret = virXMLSaveFile(configFile,
virXMLPickShellSafeComment(def->name, uuidstr),
"net-edit", xml);
cleanup:
VIR_FREE(configFile);

View File

@ -2717,6 +2717,7 @@ int virNWFilterSaveXML(const char *configDir,
virNWFilterDefPtr def,
const char *xml)
{
char uuidstr[VIR_UUID_STRING_BUFLEN];
char *configFile = NULL;
int ret = -1;
@ -2730,7 +2731,10 @@ int virNWFilterSaveXML(const char *configDir,
goto cleanup;
}
ret = virXMLSaveFile(configFile, def->name, "nwfilter-edit", xml);
virUUIDFormat(def->uuid, uuidstr);
ret = virXMLSaveFile(configFile,
virXMLPickShellSafeComment(def->name, uuidstr),
"nwfilter-edit", xml);
cleanup:
VIR_FREE(configFile);
@ -3151,6 +3155,7 @@ virNWFilterObjSaveDef(virNWFilterDriverStatePtr driver,
virNWFilterObjPtr nwfilter,
virNWFilterDefPtr def)
{
char uuidstr[VIR_UUID_STRING_BUFLEN];
char *xml;
int ret;
@ -3174,7 +3179,10 @@ virNWFilterObjSaveDef(virNWFilterDriverStatePtr driver,
return -1;
}
ret = virXMLSaveFile(nwfilter->configFile, def->name, "nwfilter-edit", xml);
virUUIDFormat(def->uuid, uuidstr);
ret = virXMLSaveFile(nwfilter->configFile,
virXMLPickShellSafeComment(def->name, uuidstr),
"nwfilter-edit", xml);
VIR_FREE(xml);
return ret;

View File

@ -1637,6 +1637,7 @@ virStoragePoolObjSaveDef(virStorageDriverStatePtr driver,
virStoragePoolObjPtr pool,
virStoragePoolDefPtr def)
{
char uuidstr[VIR_UUID_STRING_BUFLEN];
char *xml;
int ret = -1;
@ -1666,7 +1667,10 @@ virStoragePoolObjSaveDef(virStorageDriverStatePtr driver,
return -1;
}
ret = virXMLSaveFile(pool->configFile, def->name, "pool-edit", xml);
virUUIDFormat(def->uuid, uuidstr);
ret = virXMLSaveFile(pool->configFile,
virXMLPickShellSafeComment(def->name, uuidstr),
"pool-edit", xml);
VIR_FREE(xml);
return ret;

View File

@ -1819,6 +1819,7 @@ virURIParse;
# xml.h
virXMLChildElementCount;
virXMLParseHelper;
virXMLPickShellSafeComment;
virXMLPropString;
virXMLSaveFile;
virXPathBoolean;

View File

@ -973,8 +973,7 @@ parallelsStorageVolumeDefine(virStoragePoolObjPtr pool,
if (!xml_path)
goto cleanup;
if (virXMLSaveFile(xml_path, privvol->name,
"volume-create", xmldesc)) {
if (virXMLSaveFile(xml_path, NULL, "volume-create", xmldesc)) {
virReportError(VIR_ERR_OPERATION_FAILED, "%s",
_("Can't create file with volume description"));
goto cleanup;

View File

@ -1599,7 +1599,6 @@ qemuDomainSnapshotWriteMetadata(virDomainObjPtr vm,
char *snapDir = NULL;
char *snapFile = NULL;
char uuidstr[VIR_UUID_STRING_BUFLEN];
char *tmp;
virUUIDFormat(vm->def->uuid, uuidstr);
newxml = virDomainSnapshotDefFormat(uuidstr, snapshot->def,
@ -1622,13 +1621,7 @@ qemuDomainSnapshotWriteMetadata(virDomainObjPtr vm,
goto cleanup;
}
if (virAsprintf(&tmp, "snapshot-edit %s", vm->def->name) < 0) {
virReportOOMError();
goto cleanup;
}
ret = virXMLSaveFile(snapFile, snapshot->def->name, tmp, newxml);
VIR_FREE(tmp);
ret = virXMLSaveFile(snapFile, NULL, "snapshot-edit", newxml);
cleanup:
VIR_FREE(snapFile);

View File

@ -780,6 +780,16 @@ error:
goto cleanup;
}
const char *virXMLPickShellSafeComment(const char *str1, const char *str2)
{
if(str1 && !strpbrk(str1, "\r\t\n !\"#$&'()*;<>?[\\]^`{|}~") &&
!strstr(str1, "--"))
return str1;
if(str2 && !strpbrk(str2, "\r\t\n !\"#$&'()*;<>?[\\]^`{|}~") &&
!strstr(str2, "--"))
return str2;
return NULL;
}
static int virXMLEmitWarning(int fd,
const char *name,
@ -794,7 +804,7 @@ OVERWRITTEN AND LOST. Changes to this xml configuration should be made using:\n\
or other application using the libvirt API.\n\
-->\n\n";
if (fd < 0 || !name || !cmd) {
if (fd < 0 || !cmd) {
errno = EINVAL;
return -1;
}
@ -807,9 +817,7 @@ or other application using the libvirt API.\n\
if (safewrite(fd, cmd, len) != len)
return -1;
/* Omit the domain name if it contains a double hyphen
* because they aren't allowed in XML comments */
if (!strstr(name, "--")) {
if (name) {
if (safewrite(fd, " ", 1) != 1)
return -1;
@ -837,7 +845,7 @@ virXMLRewriteFile(int fd, void *opaque)
{
struct virXMLRewriteFileData *data = opaque;
if (data->warnName && data->warnCommand) {
if (data->warnCommand) {
if (virXMLEmitWarning(fd, data->warnName, data->warnCommand) < 0)
return -1;
}

View File

@ -61,6 +61,7 @@ xmlDocPtr virXMLParseHelper(int domcode,
const char *url,
xmlXPathContextPtr *pctxt);
const char *virXMLPickShellSafeComment(const char *str1, const char *str2);
/**
* virXMLParse:
* @filename: file to parse, or NULL for string parsing