From ef7efbc6efe4f4d35749e34a8798ff3cfcb4f593 Mon Sep 17 00:00:00 2001 From: "Daniel P. Berrange" Date: Wed, 11 Jan 2012 09:58:59 +0000 Subject: [PATCH] Add virFileTouch for creating empty files 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 | 1 + src/util/virfile.c | 21 +++++++++++++++++++++ src/util/virfile.h | 2 ++ 3 files changed, 24 insertions(+) diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index 04ae35c25c..924ec16c78 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -1174,6 +1174,7 @@ virFileDirectFdNew; virFileFclose; virFileFdopen; virFileRewrite; +virFileTouch; # virkeycode.h diff --git a/src/util/virfile.c b/src/util/virfile.c index cbc3fccccc..e6b469ccd6 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 a6e659712b..7be15b5c69 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 */