aboutsummaryrefslogtreecommitdiff
path: root/libbutl/filesystem.cxx
diff options
context:
space:
mode:
authorBoris Kolpackov <boris@codesynthesis.com>2017-07-22 14:15:35 +0200
committerBoris Kolpackov <boris@codesynthesis.com>2017-07-22 14:15:35 +0200
commit374db6aebec2a008538c7708305b9be01000c013 (patch)
treead62a79260958ca9bcad5e13be964c02363a84c3 /libbutl/filesystem.cxx
parent001d98f94f6d97a79abe78bc909c263f9e74c527 (diff)
Add touch_file() filesystem function
Diffstat (limited to 'libbutl/filesystem.cxx')
-rw-r--r--libbutl/filesystem.cxx29
1 files changed, 29 insertions, 0 deletions
diff --git a/libbutl/filesystem.cxx b/libbutl/filesystem.cxx
index 70597b9..7ba5275 100644
--- a/libbutl/filesystem.cxx
+++ b/libbutl/filesystem.cxx
@@ -6,6 +6,7 @@
#ifndef _WIN32
# include <stdio.h> // rename()
+# include <utime.h> // utime()
# include <dirent.h> // struct dirent, *dir()
# include <unistd.h> // symlink(), link(), stat(), rmdir(), unlink()
# include <sys/time.h> // utimes()
@@ -18,6 +19,7 @@
# include <direct.h> // _mkdir(), _rmdir()
# include <sys/types.h> // _stat
# include <sys/stat.h> // _stat(), S_I*
+# include <sys/utime.h> // _utime()
# ifdef _MSC_VER // Unlikely to be fixed in newer versions.
# define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
@@ -153,6 +155,33 @@ namespace butl
}
#endif
+ bool
+ touch_file (const path& p, bool create)
+ {
+ if (file_exists (p))
+ {
+#ifndef _WIN32
+ if (utime (p.string ().c_str (), nullptr) == -1)
+#else
+ if (_utime (p.string ().c_str (), nullptr) == -1)
+#endif
+ throw_generic_error (errno);
+
+ return false;
+ }
+
+ if (create && !entry_exists (p))
+ {
+ // Assuming the file access and modification times are set to the
+ // current time automatically.
+ //
+ fdopen (p, fdopen_mode::out | fdopen_mode::create);
+ return true;
+ }
+
+ throw_generic_error (ENOENT); // Does not exist or not a file.
+ }
+
mkdir_status
#ifndef _WIN32
try_mkdir (const dir_path& p, mode_t m)