[libvirt] [PATCH 1/3] virfile: Introduce virFileAppendStr

Michal Privoznik mprivozn at redhat.com
Mon Jan 27 14:03:34 UTC 2014


So far we only have an API that truncates the file prior to
writing it.  However, experience show need for new API that just
appends a string to file.

Signed-off-by: Michal Privoznik <mprivozn at redhat.com>
---
 src/libvirt_private.syms |  1 +
 src/util/virfile.c       | 48 ++++++++++++++++++++++++++++++++++++++++--------
 src/util/virfile.h       |  2 ++
 3 files changed, 43 insertions(+), 8 deletions(-)

diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
index 0c16343..f9bdd8c 100644
--- a/src/libvirt_private.syms
+++ b/src/libvirt_private.syms
@@ -1202,6 +1202,7 @@ virBuildPathInternal;
 virDirCreate;
 virFileAbsPath;
 virFileAccessibleAs;
+virFileAppendStr;
 virFileBuildPath;
 virFileClose;
 virFileDeleteTree;
diff --git a/src/util/virfile.c b/src/util/virfile.c
index 631cd06..1be3367 100644
--- a/src/util/virfile.c
+++ b/src/util/virfile.c
@@ -1291,19 +1291,16 @@ virFileReadAll(const char *path, int maxlen, char **buf)
     return len;
 }
 
-/* Truncate @path and write @str to it.  If @mode is 0, ensure that
-   @path exists; otherwise, use @mode if @path must be created.
-   Return 0 for success, nonzero for failure.
-   Be careful to preserve any errno value upon failure. */
-int
-virFileWriteStr(const char *path, const char *str, mode_t mode)
+static int
+virFileWriteAppendStr(const char *path, const char *str,
+                      int o_flags, mode_t mode)
 {
     int fd;
 
     if (mode)
-        fd = open(path, O_WRONLY|O_TRUNC|O_CREAT, mode);
+        fd = open(path, o_flags | O_CREAT, mode);
     else
-        fd = open(path, O_WRONLY|O_TRUNC);
+        fd = open(path, o_flags);
     if (fd == -1)
         return -1;
 
@@ -1319,6 +1316,41 @@ virFileWriteStr(const char *path, const char *str, mode_t mode)
     return 0;
 }
 
+/**
+ * virFileWriteStr:
+ * @path: file to write to
+ * @str: string to write
+ * @mode: file mode used when creating @path
+ *
+ * Truncate @path and write @str to it. If @mode is 0, ensure
+ * that @path exists; otherwise, use @mode if @path must be
+ * created. Be careful to preserve any errno value upon failure.
+ *
+ * Returns 0 on success, -1 on failure.
+ */
+int
+virFileWriteStr(const char *path, const char *str, mode_t mode)
+{
+    return virFileWriteAppendStr(path, str, O_WRONLY | O_TRUNC, mode);
+}
+
+/**
+ * virFileAppendStr:
+ * @path: file to write to
+ * @str: string to write
+ * @mode: file mode used when creating @path
+ *
+ * Append @str to @path. If the file doesn't exist,
+ * it is created using @mode.
+ *
+ * Returns 0 on success, -1 on failure.
+ */
+int
+virFileAppendStr(const char *path, const char *str, mode_t mode)
+{
+    return virFileWriteAppendStr(path, str, O_WRONLY | O_APPEND, mode);
+}
+
 int
 virFileMatchesNameSuffix(const char *file,
                          const char *name,
diff --git a/src/util/virfile.h b/src/util/virfile.h
index 0d20cdb..4bac829 100644
--- a/src/util/virfile.h
+++ b/src/util/virfile.h
@@ -131,6 +131,8 @@ int virFileReadAll(const char *path, int maxlen, char **buf)
 
 int virFileWriteStr(const char *path, const char *str, mode_t mode)
     ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2) ATTRIBUTE_RETURN_CHECK;
+int virFileAppendStr(const char *path, const char *str, mode_t mode)
+    ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2) ATTRIBUTE_RETURN_CHECK;
 
 int virFileMatchesNameSuffix(const char *file,
                              const char *name,
-- 
1.8.5.2




More information about the libvir-list mailing list