diff --git a/util-unix.cpp b/util-unix.cpp index 1224e66..ec4ecfb 100644 --- a/util-unix.cpp +++ b/util-unix.cpp @@ -31,7 +31,9 @@ #include #include #include +#include #include +#include #include #include #include @@ -274,6 +276,13 @@ bool successful_exit (int status) return status != -1 && WIFEXITED(status) && WEXITSTATUS(status) == 0; } +void touch_file (const std::string& filename) +{ + if (utimes(filename.c_str(), NULL) == -1) { + throw System_error("utimes", "", errno); + } +} + static void init_std_streams_platform () { } diff --git a/util-win32.cpp b/util-win32.cpp index b0d20d1..6f9d358 100644 --- a/util-win32.cpp +++ b/util-win32.cpp @@ -320,6 +320,25 @@ bool successful_exit (int status) return status == 0; } +void touch_file (const std::string& filename) +{ + HANDLE fh = CreateFileA(filename.c_str(), FILE_WRITE_ATTRIBUTES, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL); + if (fh == INVALID_HANDLE_VALUE) { + throw System_error("CreateFileA", filename, GetLastError()); + } + SYSTEMTIME system_time; + GetSystemTime(&system_time); + FILETIME file_time; + SystemTimeToFileTime(&system_time, &file_time); + + if (!SetFileTime(fh, NULL, NULL, &file_time)) { + DWORD error = GetLastError(); + CloseHandle(fh); + throw System_error("SetFileTime", filename, error); + } + CloseHandle(fh); +} + static void init_std_streams_platform () { _setmode(_fileno(stdin), _O_BINARY); diff --git a/util.hpp b/util.hpp index cf23771..bb79ee2 100644 --- a/util.hpp +++ b/util.hpp @@ -64,6 +64,7 @@ int exec_command (const std::vector&); int exec_command (const std::vector&, std::ostream& output); int exec_command_with_input (const std::vector&, const char* p, size_t len); bool successful_exit (int status); +void touch_file (const std::string&); std::string escape_shell_arg (const std::string&); uint32_t load_be32 (const unsigned char*); void store_be32 (unsigned char*, uint32_t);