[PATCH 4/4] Use virXMLSaveFile when writing XML config

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



---
 src/conf/domain_conf.c   |   49 +-------------------------------
 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(+), 193 deletions(-)

diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c
index 6656e8b..dda5e1a 100644
--- a/src/conf/domain_conf.c
+++ b/src/conf/domain_conf.c
@@ -11078,9 +11078,7 @@ int virDomainSaveXML(const char *configDir,
                      const char *xml)
 {
     char *configFile = NULL;
-    char *newfile = NULL;
-    int fd = -1, ret = -1;
-    size_t towrite;
+    int ret = -1;
 
     if ((configFile = virDomainConfigFile(configDir, def->name)) == NULL)
         goto cleanup;
@@ -11092,52 +11090,9 @@ int virDomainSaveXML(const char *configDir,
         goto cleanup;
     }
 
-    if (virAsprintf(&newfile, "%s.new", configFile) < 0) {
-        virReportOOMError();
-        goto cleanup;
-    }
-
-    if ((fd = open(newfile,
-                   O_WRONLY | O_CREAT | O_TRUNC,
-                   S_IRUSR | S_IWUSR )) < 0) {
-        virReportSystemError(errno,
-                             _("cannot create config file '%s'"),
-                             newfile);
-        goto cleanup;
-    }
-
-    virEmitXMLWarning(fd, def->name, "edit");
-
-    towrite = strlen(xml);
-    if (safewrite(fd, xml, towrite) < 0) {
-        virReportSystemError(errno,
-                             _("cannot write config file '%s'"),
-                             newfile);
-        goto cleanup;
-    }
-
-    if (VIR_CLOSE(fd) < 0) {
-        virReportSystemError(errno,
-                             _("cannot save config file '%s'"),
-                             newfile);
-        goto cleanup;
-    }
-
-    if (rename(newfile, configFile) < 0) {
-        virReportSystemError(errno,
-                             _("cannot rename config file '%s' as '%s'"),
-                             newfile, configFile);
-        goto cleanup;
-    }
+    ret = virXMLSaveFile(configFile, def->name, "edit", xml);
 
-    ret = 0;
  cleanup:
-    VIR_FORCE_CLOSE(fd);
-
-    if (newfile) {
-        unlink(newfile);
-        VIR_FREE(newfile);
-    }
     VIR_FREE(configFile);
     return ret;
 }
diff --git a/src/conf/network_conf.c b/src/conf/network_conf.c
index b98ffad..f2ea9bf 100644
--- a/src/conf/network_conf.c
+++ b/src/conf/network_conf.c
@@ -1389,8 +1389,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;
@@ -1402,39 +1401,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 08ede48..5527348 100644
--- a/src/conf/nwfilter_conf.c
+++ b/src/conf/nwfilter_conf.c
@@ -2182,8 +2182,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;
@@ -2195,38 +2194,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;
 }
 
@@ -2569,8 +2540,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) {
@@ -2592,37 +2562,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 e893b2d..36b24da 100644
--- a/src/conf/storage_conf.c
+++ b/src/conf/storage_conf.c
@@ -1515,10 +1515,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) {
@@ -1546,36 +1546,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 b202ba7..7fa8523 100644
--- a/src/qemu/qemu_domain.c
+++ b/src/qemu/qemu_domain.c
@@ -1336,7 +1336,6 @@ qemuDomainSnapshotWriteMetadata(virDomainObjPtr vm,
                                 virDomainSnapshotObjPtr snapshot,
                                 char *snapshotDir)
 {
-    int fd = -1;
     char *newxml = NULL;
     int ret = -1;
     char *snapDir = NULL;
@@ -1366,33 +1365,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;
 }
 
-- 
1.7.7

--
libvir-list mailing list
libvir-list@xxxxxxxxxx
https://www.redhat.com/mailman/listinfo/libvir-list


[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Lib OS Info]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]