From c828e2bb39f711c82c3b816f57ddde9ab29bdb20 Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Thu, 18 Jun 2015 17:27:25 +0200 Subject: Move path_mtime to filesystem, rename file_mtime, follow symlinks --- butl/filesystem.cxx | 44 +++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 43 insertions(+), 1 deletion(-) (limited to 'butl/filesystem.cxx') diff --git a/butl/filesystem.cxx b/butl/filesystem.cxx index 230bc8e..7c8893e 100644 --- a/butl/filesystem.cxx +++ b/butl/filesystem.cxx @@ -4,7 +4,7 @@ #include -#include // rmdir(), unlink() +#include // stat, rmdir(), unlink() #include // stat #include // stat, lstat(), S_IS*, mkdir() @@ -14,6 +14,48 @@ using namespace std; namespace butl { + // Figuring out whether we have the nanoseconds in some form. + // + template + constexpr auto nsec (const S* s) -> decltype(s->st_mtim.tv_nsec) + { + return s->st_mtim.tv_nsec; // POSIX (GNU/Linux, Solaris). + } + + template + constexpr auto nsec (const S* s) -> decltype(s->st_mtimespec.tv_nsec) + { + return s->st_mtimespec.tv_nsec; // MacOS X. + } + + template + constexpr auto nsec (const S* s) -> decltype(s->st_mtime_n) + { + return s->st_mtime_n; // AIX 5.2 and later. + } + + template + constexpr int nsec (...) {return 0;} + + timestamp + file_mtime (const path& p) + { + struct stat s; + if (::lstat (p.string ().c_str (), &s) != 0) + { + if (errno == ENOENT || errno == ENOTDIR) + return timestamp_nonexistent; + else + throw system_error (errno, system_category ()); + } + + return S_ISREG (s.st_mode) + ? system_clock::from_time_t (s.st_mtime) + + chrono::duration_cast ( + chrono::nanoseconds (nsec (&s))) + : timestamp_nonexistent; + } + bool dir_exists (const path& p) { -- cgit v1.1