From c0d9dfe2a1a567299b6fd12acd6913f6a0dc86c7 Mon Sep 17 00:00:00 2001 From: Jiri Denemark Date: Thu, 27 Oct 2011 18:55:47 +0200 Subject: [PATCH] Use virXMLSaveFile when writing XML config --- src/conf/domain_conf.c | 32 ++----------------- src/conf/network_conf.c | 34 ++------------------ src/conf/nwfilter_conf.c | 68 +++------------------------------------- src/conf/storage_conf.c | 37 +++------------------- src/qemu/qemu_domain.c | 19 ++--------- 5 files changed, 14 insertions(+), 176 deletions(-) diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c index 7f9c542f2c..f5c59df6e9 100644 --- a/src/conf/domain_conf.c +++ b/src/conf/domain_conf.c @@ -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"); - - 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 = virXMLSaveFile(configFile, def->name, "edit", xml); - ret = 0; cleanup: - VIR_FORCE_CLOSE(fd); - VIR_FREE(configFile); return ret; } diff --git a/src/conf/network_conf.c b/src/conf/network_conf.c index 7bc2e2cbe8..f7369e8c13 100644 --- a/src/conf/network_conf.c +++ b/src/conf/network_conf.c @@ -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; } diff --git a/src/conf/nwfilter_conf.c b/src/conf/nwfilter_conf.c index 289d2bb55d..6c3e1cf67e 100644 --- a/src/conf/nwfilter_conf.c +++ b/src/conf/nwfilter_conf.c @@ -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; diff --git a/src/conf/storage_conf.c b/src/conf/storage_conf.c index 55205d40a2..18cbfdb579 100644 --- a/src/conf/storage_conf.c +++ b/src/conf/storage_conf.c @@ -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; diff --git a/src/qemu/qemu_domain.c b/src/qemu/qemu_domain.c index 198ebcc9e4..e7b20d55e1 100644 --- a/src/qemu/qemu_domain.c +++ b/src/qemu/qemu_domain.c @@ -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; } -- GitLab