]> xenbits.xensource.com Git - libvirt.git/commitdiff
Add virFileTouch for creating empty files
authorDaniel P. Berrange <berrange@redhat.com>
Wed, 11 Jan 2012 09:58:59 +0000 (09:58 +0000)
committerDaniel P. Berrange <berrange@redhat.com>
Wed, 25 Jan 2012 14:11:03 +0000 (14:11 +0000)
Add a virFileTouch API which ensures that a file will always
exist, even if zero length

* src/util/virfile.c, src/util/virfile.h,
  src/libvirt_private.syms: Introduce virFileTouch

src/libvirt_private.syms
src/util/virfile.c
src/util/virfile.h

index 04ae35c25c4e43ee8b05a0acb86fab32ecf37142..924ec16c787c7bce46dbb34e38e41bda1b0fd89c 100644 (file)
@@ -1174,6 +1174,7 @@ virFileDirectFdNew;
 virFileFclose;
 virFileFdopen;
 virFileRewrite;
+virFileTouch;
 
 
 # virkeycode.h
index cbc3fccccc413b23e6fa8a7d1fc858e59d07976c..e6b469ccd6e7e03a15e87f3fcb450f0f57f62ed4 100644 (file)
@@ -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;
+}
index a6e659712b102ccc0200ba2a8aef20bf63d6daee..7be15b5c6993b7ae245608aec549ee98101498da 100644 (file)
@@ -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 */