[PATCH 1/7] Add virFileTouch for creating empty files

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

 



From: "Daniel P. Berrange" <berrange@xxxxxxxxxx>

Add a virFileTouch API which ensures that a file will always
exist, even if zero length

* src/lxc/lxc_container.c, src/util/virfile.h: virFileTouch
---
 src/util/virfile.c |   21 +++++++++++++++++++++
 src/util/virfile.h |    2 ++
 2 files changed, 23 insertions(+), 0 deletions(-)

diff --git a/src/util/virfile.c b/src/util/virfile.c
index cbc3fcc..e6b469c 100644
--- a/src/util/virfile.c
+++ b/src/util/virfile.c
@@ -390,3 +390,24 @@ cleanup:
     }
     return ret;
 }
+
+
+int virFileTouch(const char *path, mode_t mode)
+{
+    int fd = -1;
+
+    if ((fd = open(path, O_WRONLY | O_CREAT, mode)) < 0) {
+        virReportSystemError(errno, _("cannot create file '%s'"),
+                             path);
+        return -1;
+    }
+
+    if (VIR_CLOSE(fd) < 0) {
+        virReportSystemError(errno, _("cannot save file '%s'"),
+                             path);
+        VIR_FORCE_CLOSE(fd);
+        return -1;
+    }
+
+    return 0;
+}
diff --git a/src/util/virfile.h b/src/util/virfile.h
index a6e6597..7be15b5 100644
--- a/src/util/virfile.h
+++ b/src/util/virfile.h
@@ -74,4 +74,6 @@ int virFileRewrite(const char *path,
                    virFileRewriteFunc rewrite,
                    void *opaque);
 
+int virFileTouch(const char *path, mode_t mode);
+
 #endif /* __VIR_FILES_H */
-- 
1.7.7.5

--
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]