From 5c30ca5dc610f97f0987379debf9643689545dae Mon Sep 17 00:00:00 2001 From: James Turner Date: Wed, 31 Jan 2018 11:04:10 +0000 Subject: [PATCH] Add SGPath::touch() helper --- simgear/misc/sg_path.cxx | 49 +++++++++++++++++++++++++++++++++++++--- simgear/misc/sg_path.hxx | 7 ++++++ 2 files changed, 53 insertions(+), 3 deletions(-) diff --git a/simgear/misc/sg_path.cxx b/simgear/misc/sg_path.cxx index 299a004c..5260be83 100644 --- a/simgear/misc/sg_path.cxx +++ b/simgear/misc/sg_path.cxx @@ -35,9 +35,15 @@ #include #include -#ifdef _WIN32 +#if !defined(SG_WINDOWS) +# include +# include +#endif + +#if defined(SG_WINDOWS) # include #endif + #include "sg_path.hxx" #include @@ -52,13 +58,13 @@ using simgear::strutils::starts_with; static const char sgDirPathSep = '/'; static const char sgDirPathSepBad = '\\'; -#ifdef _WIN32 +#if defined(SG_WINDOWS) const char SGPath::pathListSep[] = ";"; // this is null-terminated #else const char SGPath::pathListSep[] = ":"; // ditto #endif -#ifdef _WIN32 +#if defined(SG_WINDOWS) #include // for CSIDL // TODO: replace this include file with the official header // included in the Windows 8.1 SDK @@ -1048,3 +1054,40 @@ std::string SGPath::fileUrl() const return {}; } } + +//------------------------------------------------------------------------------ +bool SGPath::touch() +{ + if (!permissionsAllowsWrite()) + { + SG_LOG(SG_IO, SG_WARN, "file touch failed: (" << *this << ")" + " reason: access denied" ); + return false; + } + + if (!exists()) { + SG_LOG(SG_IO, SG_WARN, "file touch failed: (" << *this << ")" + " reason: missing file"); + return false; + } +#if defined(SG_WINDOWS) + auto ws = wstr(); + // set this link for docs on behaviour here, about passing nullptr + // https://msdn.microsoft.com/en-us/library/aa273399(v=vs.60).aspx + if (_wutime(ws.c_str(), nullptr) != 0) { + SG_LOG(SG_IO, SG_WARN, "file touch failed: (" << *this << ")" + " reason: _wutime failed with error:" << simgear::strutils::error_string(errno)); + return false; + } +#else + if (::utime(path.c_str(), nullptr) != 0) { + SG_LOG(SG_IO, SG_WARN, "file touch failed: (" << *this << ")" + " reason: utime failed with error:" << simgear::strutils::error_string(errno)); + return false; + } +#endif + + // reset the cache flag so we re-stat() on next request + _cached = false; + return true; +} diff --git a/simgear/misc/sg_path.hxx b/simgear/misc/sg_path.hxx index a9ef7c03..af37d6ad 100644 --- a/simgear/misc/sg_path.hxx +++ b/simgear/misc/sg_path.hxx @@ -282,6 +282,13 @@ public: */ std::string fileUrl() const; + /** + * Update the file modification timestamp to be 'now'. The contents will + * not be changed. (Same as POSIX 'touch' command). Will fail if the file + * does not exist or permissions do not allow writing. + */ + bool touch(); + enum StandardLocation { HOME,