[PATCH v2 05/10] util: Introduce virFileRewriteStr

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

 



There are couple of places where we have a string and want to
save it to a file. Atomically. In all those places we use
virFileRewrite() but also implement the very same callback which
takes the string and write it into temp file. This makes no
sense. Unify the callbacks and move them to one place.

Signed-off-by: Michal Privoznik <mprivozn@xxxxxxxxxx>
---
 src/conf/virsecretobj.c    | 20 ++------------------
 src/libvirt_private.syms   |  1 +
 src/network/leaseshelper.c | 14 +-------------
 src/util/virfile.c         | 24 +++++++++++++++++++++++-
 src/util/virfile.h         |  7 +++++--
 src/util/virxml.c          |  4 ++--
 6 files changed, 34 insertions(+), 36 deletions(-)

diff --git a/src/conf/virsecretobj.c b/src/conf/virsecretobj.c
index 2bdfe08ea..1351f18b2 100644
--- a/src/conf/virsecretobj.c
+++ b/src/conf/virsecretobj.c
@@ -692,20 +692,6 @@ virSecretObjDeleteData(virSecretObjPtr secret)
    has virSecretDef stored as XML in "$basename.xml".  If a value of the
    secret is defined, it is stored as base64 (with no formatting) in
    "$basename.base64".  "$basename" is in both cases the base64-encoded UUID. */
-
-static int
-virSecretRewriteFile(int fd,
-                     void *opaque)
-{
-    char *data = opaque;
-
-    if (safewrite(fd, data, strlen(data)) < 0)
-        return -1;
-
-    return 0;
-}
-
-
 int
 virSecretObjSaveConfig(virSecretObjPtr secret)
 {
@@ -715,8 +701,7 @@ virSecretObjSaveConfig(virSecretObjPtr secret)
     if (!(xml = virSecretDefFormat(secret->def)))
         goto cleanup;
 
-    if (virFileRewrite(secret->configFile, S_IRUSR | S_IWUSR,
-                       virSecretRewriteFile, xml) < 0)
+    if (virFileRewriteStr(secret->configFile, S_IRUSR | S_IWUSR, xml) < 0)
         goto cleanup;
 
     ret = 0;
@@ -739,8 +724,7 @@ virSecretObjSaveData(virSecretObjPtr secret)
     if (!(base64 = virStringEncodeBase64(secret->value, secret->value_size)))
         goto cleanup;
 
-    if (virFileRewrite(secret->base64File, S_IRUSR | S_IWUSR,
-                       virSecretRewriteFile, base64) < 0)
+    if (virFileRewriteStr(secret->base64File, S_IRUSR | S_IWUSR, base64) < 0)
         goto cleanup;
 
     ret = 0;
diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
index 3d4da7356..9189f56fe 100644
--- a/src/libvirt_private.syms
+++ b/src/libvirt_private.syms
@@ -1601,6 +1601,7 @@ virFileRemoveLastComponent;
 virFileResolveAllLinks;
 virFileResolveLink;
 virFileRewrite;
+virFileRewriteStr;
 virFileSanitizePath;
 virFileSkipRoot;
 virFileStripSuffix;
diff --git a/src/network/leaseshelper.c b/src/network/leaseshelper.c
index 16f6eb87b..a0262dd07 100644
--- a/src/network/leaseshelper.c
+++ b/src/network/leaseshelper.c
@@ -64,17 +64,6 @@ usage(int status)
     exit(status);
 }
 
-static int
-customLeaseRewriteFile(int fd, void *opaque)
-{
-    char **data = opaque;
-
-    if (safewrite(fd, *data, strlen(*data)) < 0)
-        return -1;
-
-    return 0;
-}
-
 /* Flags denoting actions for a lease */
 enum virLeaseActionFlags {
     VIR_LEASE_ACTION_ADD,       /* Create new lease */
@@ -252,8 +241,7 @@ main(int argc, char **argv)
         }
 
         /* Write to file */
-        if (virFileRewrite(custom_lease_file, 0644,
-                           customLeaseRewriteFile, &leases_str) < 0)
+        if (virFileRewriteStr(custom_lease_file, 0644, leases_str) < 0)
             goto cleanup;
         break;
 
diff --git a/src/util/virfile.c b/src/util/virfile.c
index fcd0d9288..1f0bfa906 100644
--- a/src/util/virfile.c
+++ b/src/util/virfile.c
@@ -443,7 +443,7 @@ int
 virFileRewrite(const char *path,
                mode_t mode,
                virFileRewriteFunc rewrite,
-               void *opaque)
+               const void *opaque)
 {
     char *newfile = NULL;
     int fd = -1;
@@ -494,6 +494,28 @@ virFileRewrite(const char *path,
 }
 
 
+static int
+virFileRewriteStrHelper(int fd, const void *opaque)
+{
+    const char *data = opaque;
+
+    if (safewrite(fd, data, strlen(data)) < 0)
+        return -1;
+
+    return 0;
+}
+
+
+int
+virFileRewriteStr(const char *path,
+                  mode_t mode,
+                  const char *str)
+{
+    return virFileRewrite(path, mode,
+                          virFileRewriteStrHelper, str);
+}
+
+
 int virFileTouch(const char *path, mode_t mode)
 {
     int fd = -1;
diff --git a/src/util/virfile.h b/src/util/virfile.h
index 836cc602c..5b810956e 100644
--- a/src/util/virfile.h
+++ b/src/util/virfile.h
@@ -101,11 +101,14 @@ void virFileWrapperFdFree(virFileWrapperFdPtr dfd);
 int virFileLock(int fd, bool shared, off_t start, off_t len, bool waitForLock);
 int virFileUnlock(int fd, off_t start, off_t len);
 
-typedef int (*virFileRewriteFunc)(int fd, void *opaque);
+typedef int (*virFileRewriteFunc)(int fd, const void *opaque);
 int virFileRewrite(const char *path,
                    mode_t mode,
                    virFileRewriteFunc rewrite,
-                   void *opaque);
+                   const void *opaque);
+int virFileRewriteStr(const char *path,
+                      mode_t mode,
+                      const char *str);
 
 int virFileTouch(const char *path, mode_t mode);
 
diff --git a/src/util/virxml.c b/src/util/virxml.c
index 96c17fa9c..666024809 100644
--- a/src/util/virxml.c
+++ b/src/util/virxml.c
@@ -871,9 +871,9 @@ struct virXMLRewriteFileData {
 };
 
 static int
-virXMLRewriteFile(int fd, void *opaque)
+virXMLRewriteFile(int fd, const void *opaque)
 {
-    struct virXMLRewriteFileData *data = opaque;
+    const struct virXMLRewriteFileData *data = opaque;
 
     if (data->warnCommand) {
         if (virXMLEmitWarning(fd, data->warnName, data->warnCommand) < 0)
-- 
2.11.0

--
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]
  Powered by Linux