]> git.lizzy.rs Git - nothing.git/commitdiff
implement file last_modified for windows
authorRIscRIpt <richard@riscript.com>
Sat, 15 Jun 2019 18:37:48 +0000 (20:37 +0200)
committerRIscRIpt <richard@riscript.com>
Sat, 15 Jun 2019 18:37:48 +0000 (20:37 +0200)
src/system/file.c

index 743744d07ed3aa545c7d48280340ff9303190825..edcb10dfdb6135cfa327bc9aef9200f54f6ba65e 100644 (file)
@@ -3,6 +3,10 @@
 #ifdef __linux__
 #include <sys/stat.h>
 #include <sys/types.h>
+#elif defined(_WIN32)
+#include <Windows.h>
+#define WINDOWS_TICK 10000000
+#define SEC_TO_UNIX_EPOCH 11644473600LL
 #endif // __linux__
 
 #include "system/stacktrace.h"
@@ -16,5 +20,30 @@ time_t last_modified(const char *filepath)
     struct stat attr;
     stat(filepath, &attr);
     return attr.st_mtime;
+#elif defined(_WIN32)
+    // CreateFile opens file (see flag OPEN_EXISTING)
+    HANDLE hFile = CreateFile(
+        filepath,
+        GENERIC_READ,
+        FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
+        NULL,
+        OPEN_EXISTING,
+        0,
+        NULL
+    );
+    if (!hFile) {
+        return 0;
+    }
+    FILETIME filetime = { 0 };
+    if (!GetFileTime(hFile, NULL, NULL, &filetime)) {
+        CloseHandle(hFile);
+        return 0;
+    }
+    CloseHandle(hFile);
+    unsigned long long mod_time = filetime.dwHighDateTime;
+    mod_time <<= 32;
+    mod_time |= filetime.dwLowDateTime;
+    // Taken from https://stackoverflow.com/a/6161842/1901561
+    return mod_time / WINDOWS_TICK - SEC_TO_UNIX_EPOCH;
 #endif // __linux__
 }