提交 c0d9dfe2 编写于 作者: J Jiri Denemark

Use virXMLSaveFile when writing XML config

上级 fef8127c
......@@ -11105,8 +11105,7 @@ int virDomainSaveXML(const char *configDir,
const char *xml)
{
char *configFile = NULL;
int fd = -1, ret = -1;
size_t towrite;
int ret = -1;
if ((configFile = virDomainConfigFile(configDir, def->name)) == NULL)
goto cleanup;
......@@ -11118,36 +11117,9 @@ int virDomainSaveXML(const char *configDir,
goto cleanup;
}
if ((fd = open(configFile,
O_WRONLY | O_CREAT | O_TRUNC,
S_IRUSR | S_IWUSR )) < 0) {
virReportSystemError(errno,
_("cannot create config file '%s'"),
configFile);
goto cleanup;
}
virEmitXMLWarning(fd, def->name, "edit");
ret = virXMLSaveFile(configFile, def->name, "edit", xml);
towrite = strlen(xml);
if (safewrite(fd, xml, towrite) < 0) {
virReportSystemError(errno,
_("cannot write config file '%s'"),
configFile);
goto cleanup;
}
if (VIR_CLOSE(fd) < 0) {
virReportSystemError(errno,
_("cannot save config file '%s'"),
configFile);
goto cleanup;
}
ret = 0;
cleanup:
VIR_FORCE_CLOSE(fd);
VIR_FREE(configFile);
return ret;
}
......
......@@ -1395,8 +1395,7 @@ int virNetworkSaveXML(const char *configDir,
const char *xml)
{
char *configFile = NULL;
int fd = -1, ret = -1;
size_t towrite;
int ret = -1;
if ((configFile = virNetworkConfigFile(configDir, def->name)) == NULL)
goto cleanup;
......@@ -1408,39 +1407,10 @@ int virNetworkSaveXML(const char *configDir,
goto cleanup;
}
if ((fd = open(configFile,
O_WRONLY | O_CREAT | O_TRUNC,
S_IRUSR | S_IWUSR )) < 0) {
virReportSystemError(errno,
_("cannot create config file '%s'"),
configFile);
goto cleanup;
}
virEmitXMLWarning(fd, def->name, "net-edit");
towrite = strlen(xml);
if (safewrite(fd, xml, towrite) < 0) {
virReportSystemError(errno,
_("cannot write config file '%s'"),
configFile);
goto cleanup;
}
if (VIR_CLOSE(fd) < 0) {
virReportSystemError(errno,
_("cannot save config file '%s'"),
configFile);
goto cleanup;
}
ret = 0;
ret = virXMLSaveFile(configFile, def->name, "net-edit", xml);
cleanup:
VIR_FORCE_CLOSE(fd);
VIR_FREE(configFile);
return ret;
}
......
......@@ -2186,8 +2186,7 @@ int virNWFilterSaveXML(const char *configDir,
const char *xml)
{
char *configFile = NULL;
int fd = -1, ret = -1;
size_t towrite;
int ret = -1;
if ((configFile = virNWFilterConfigFile(configDir, def->name)) == NULL)
goto cleanup;
......@@ -2199,38 +2198,10 @@ int virNWFilterSaveXML(const char *configDir,
goto cleanup;
}
if ((fd = open(configFile,
O_WRONLY | O_CREAT | O_TRUNC,
S_IRUSR | S_IWUSR )) < 0) {
virReportSystemError(errno,
_("cannot create config file '%s'"),
configFile);
goto cleanup;
}
virEmitXMLWarning(fd, def->name, "nwfilter-edit");
towrite = strlen(xml);
if (safewrite(fd, xml, towrite) < 0) {
virReportSystemError(errno,
_("cannot write config file '%s'"),
configFile);
goto cleanup;
}
if (VIR_CLOSE(fd) < 0) {
virReportSystemError(errno,
_("cannot save config file '%s'"),
configFile);
goto cleanup;
}
ret = 0;
ret = virXMLSaveFile(configFile, def->name, "nwfilter-edit", xml);
cleanup:
VIR_FORCE_CLOSE(fd);
VIR_FREE(configFile);
return ret;
}
......@@ -2573,8 +2544,7 @@ virNWFilterObjSaveDef(virNWFilterDriverStatePtr driver,
virNWFilterDefPtr def)
{
char *xml;
int fd = -1, ret = -1;
ssize_t towrite;
int ret;
if (!nwfilter->configFile) {
if (virFileMakePath(driver->configDir) < 0) {
......@@ -2596,37 +2566,7 @@ virNWFilterObjSaveDef(virNWFilterDriverStatePtr driver,
return -1;
}
if ((fd = open(nwfilter->configFile,
O_WRONLY | O_CREAT | O_TRUNC,
S_IRUSR | S_IWUSR )) < 0) {
virReportSystemError(errno,
_("cannot create config file %s"),
nwfilter->configFile);
goto cleanup;
}
virEmitXMLWarning(fd, def->name, "nwfilter-edit");
towrite = strlen(xml);
if (safewrite(fd, xml, towrite) != towrite) {
virReportSystemError(errno,
_("cannot write config file %s"),
nwfilter->configFile);
goto cleanup;
}
if (VIR_CLOSE(fd) < 0) {
virReportSystemError(errno,
_("cannot save config file %s"),
nwfilter->configFile);
goto cleanup;
}
ret = 0;
cleanup:
VIR_FORCE_CLOSE(fd);
ret = virXMLSaveFile(nwfilter->configFile, def->name, "nwfilter-edit", xml);
VIR_FREE(xml);
return ret;
......
......@@ -1525,10 +1525,10 @@ virStoragePoolLoadAllConfigs(virStoragePoolObjListPtr pools,
int
virStoragePoolObjSaveDef(virStorageDriverStatePtr driver,
virStoragePoolObjPtr pool,
virStoragePoolDefPtr def) {
virStoragePoolDefPtr def)
{
char *xml;
int fd = -1, ret = -1;
ssize_t towrite;
int ret = -1;
if (!pool->configFile) {
if (virFileMakePath(driver->configDir) < 0) {
......@@ -1556,36 +1556,7 @@ virStoragePoolObjSaveDef(virStorageDriverStatePtr driver,
return -1;
}
if ((fd = open(pool->configFile,
O_WRONLY | O_CREAT | O_TRUNC,
S_IRUSR | S_IWUSR )) < 0) {
virReportSystemError(errno,
_("cannot create config file %s"),
pool->configFile);
goto cleanup;
}
virEmitXMLWarning(fd, def->name, "pool-edit");
towrite = strlen(xml);
if (safewrite(fd, xml, towrite) != towrite) {
virReportSystemError(errno,
_("cannot write config file %s"),
pool->configFile);
goto cleanup;
}
if (VIR_CLOSE(fd) < 0) {
virReportSystemError(errno,
_("cannot save config file %s"),
pool->configFile);
goto cleanup;
}
ret = 0;
cleanup:
VIR_FORCE_CLOSE(fd);
ret = virXMLSaveFile(pool->configFile, def->name, "pool-edit", xml);
VIR_FREE(xml);
return ret;
......
......@@ -1337,7 +1337,6 @@ qemuDomainSnapshotWriteMetadata(virDomainObjPtr vm,
virDomainSnapshotObjPtr snapshot,
char *snapshotDir)
{
int fd = -1;
char *newxml = NULL;
int ret = -1;
char *snapDir = NULL;
......@@ -1367,33 +1366,19 @@ qemuDomainSnapshotWriteMetadata(virDomainObjPtr vm,
virReportOOMError();
goto cleanup;
}
fd = open(snapFile, O_CREAT|O_TRUNC|O_WRONLY, S_IRUSR|S_IWUSR);
if (fd < 0) {
qemuReportError(VIR_ERR_OPERATION_FAILED,
_("failed to create snapshot file '%s'"), snapFile);
goto cleanup;
}
if (virAsprintf(&tmp, "snapshot-edit %s", vm->def->name) < 0) {
virReportOOMError();
goto cleanup;
}
virEmitXMLWarning(fd, snapshot->def->name, tmp);
VIR_FREE(tmp);
if (safewrite(fd, newxml, strlen(newxml)) != strlen(newxml)) {
virReportSystemError(errno, _("Failed to write snapshot data to %s"),
snapFile);
goto cleanup;
}
ret = 0;
ret = virXMLSaveFile(snapFile, snapshot->def->name, tmp, newxml);
VIR_FREE(tmp);
cleanup:
VIR_FREE(snapFile);
VIR_FREE(snapDir);
VIR_FREE(newxml);
VIR_FORCE_CLOSE(fd);
return ret;
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册