]> xenbits.xensource.com Git - libvirt.git/commitdiff
util: Introduce virFileRewriteStr
authorMichal Privoznik <mprivozn@redhat.com>
Mon, 28 Nov 2016 08:00:55 +0000 (09:00 +0100)
committerMichal Privoznik <mprivozn@redhat.com>
Tue, 6 Dec 2016 12:33:18 +0000 (13:33 +0100)
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@redhat.com>
src/conf/virsecretobj.c
src/libvirt_private.syms
src/network/leaseshelper.c
src/util/virfile.c
src/util/virfile.h
src/util/virxml.c

index 2bdfe08eab13464e2636d26bf7edf8e4ec339530..1351f18b253a0ec981ba935de3df6eec10e6460d 100644 (file)
@@ -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;
index 3d4da7356265eb91d8b072364cc668695c1ae397..9189f56fe4af82ca79306b5b9e78e63bc7b244c8 100644 (file)
@@ -1601,6 +1601,7 @@ virFileRemoveLastComponent;
 virFileResolveAllLinks;
 virFileResolveLink;
 virFileRewrite;
+virFileRewriteStr;
 virFileSanitizePath;
 virFileSkipRoot;
 virFileStripSuffix;
index 16f6eb87b53ca30887eccb52a296103e98b1d1b3..a0262dd07f588ea90cc5cdcd747e705a6b156f7a 100644 (file)
@@ -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;
 
index fcd0d92889c7b24ec9363057efb0a1253ce60c28..1f0bfa906f43d302617c8290a21a023eb3186bd2 100644 (file)
@@ -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;
index 836cc602c8329ae78ce1ac5f5cfe0bfb4c88edd9..5b810956e19c75d9e273fb67888511e07d7598c6 100644 (file)
@@ -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);
 
index 96c17fa9c00d5595248e29e0b9e15ab872b807bd..666024809438f9ea4551944444f2386349da1416 100644 (file)
@@ -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)