Add SGPath::touch() helper

This commit is contained in:
James Turner 2018-01-31 11:04:10 +00:00
parent 37bc43c7aa
commit 5c30ca5dc6
2 changed files with 53 additions and 3 deletions

View File

@ -35,9 +35,15 @@
#include <fstream>
#include <cstdlib>
#ifdef _WIN32
#if !defined(SG_WINDOWS)
# include <sys/types.h>
# include <utime.h>
#endif
#if defined(SG_WINDOWS)
# include <direct.h>
#endif
#include "sg_path.hxx"
#include <boost/algorithm/string/case_conv.hpp>
@ -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 <ShlObj.h> // for CSIDL
// TODO: replace this include file with the official <versionhelpers.h> 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;
}

View File

@ -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,