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

[libvirt] [PATCH] Create file in virFileWriteStr() if it doesn't exist



This patch adds a virFileWriteStrEx() function with a
third parameter to set the created file permissions.
virFileWriteStr() calls this new function with a
default value for the mode parameter.
---
 src/util/util.c |   11 ++++++++---
 src/util/util.h |    1 +
 2 files changed, 9 insertions(+), 3 deletions(-)

diff --git a/src/util/util.c b/src/util/util.c
index a2582aa..82ca9b3 100644
--- a/src/util/util.c
+++ b/src/util/util.c
@@ -1123,14 +1123,19 @@ int virFileReadAll(const char *path, int maxlen, char **buf)
     return len;
 }
 
-/* Truncate @path and write @str to it.
+int virFileWriteStr(const char *path, const char *str)
+{
+    return virFileWriteStrEx(path, str, S_IRUSR|S_IWUSR);
+}
+
+/* Truncate or create @path and write @str to it.
    Return 0 for success, nonzero for failure.
    Be careful to preserve any errno value upon failure. */
-int virFileWriteStr(const char *path, const char *str)
+int virFileWriteStrEx(const char *path, const char *str, mode_t mode)
 {
     int fd;
 
-    if ((fd = open(path, O_WRONLY|O_TRUNC)) == -1)
+    if ((fd = open(path, O_WRONLY|O_TRUNC|O_CREAT, mode)) == -1)
         return -1;
 
     if (safewrite(fd, str, strlen(str)) < 0) {
diff --git a/src/util/util.h b/src/util/util.h
index a240d87..18ef693 100644
--- a/src/util/util.h
+++ b/src/util/util.h
@@ -96,6 +96,7 @@ int virFileReadLimFD(int fd, int maxlen, char **buf) ATTRIBUTE_RETURN_CHECK;
 
 int virFileReadAll(const char *path, int maxlen, char **buf) ATTRIBUTE_RETURN_CHECK;
 
+int virFileWriteStrEx(const char *path, const char *str, mode_t mode) ATTRIBUTE_RETURN_CHECK;
 int virFileWriteStr(const char *path, const char *str) ATTRIBUTE_RETURN_CHECK;
 
 int virFileMatchesNameSuffix(const char *file,
-- 
1.7.0.4


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