changeset 478:b1e80180818a

Improve th_stat_path() some more and fix Windows issues.
author Matti Hamalainen <ccr@tnsp.org>
date Mon, 09 Jul 2018 09:25:51 +0300
parents 96d137a6b392
children 77ad030e82c9
files th_file.c th_file.h
diffstat 2 files changed, 29 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/th_file.c	Mon Jul 09 08:06:45 2018 +0300
+++ b/th_file.c	Mon Jul 09 09:25:51 2018 +0300
@@ -8,7 +8,6 @@
 #include "th_file.h"
 #include "th_string.h"
 #include <unistd.h>
-//#include <fcntl.h>
 #ifdef TH_PLAT_WINDOWS
 #  include <shlwapi.h>
 #  include <shfolder.h>
@@ -69,20 +68,47 @@
 }
 
 
+#ifdef TH_PLAT_WINDOWS
+static uint64_t th_convert_windows_time(FILETIME ftime)
+{
+    uint64_t value = (((uint64_t) ftime.dwHighDateTime) << 32ULL) | ((uint64_t) ftime.dwLowDateTime);
+
+    // Naive conversion (1000 ns / 100) * ns - difference_between_win_to_unix_epoch
+    return (value / ((1000 / 100) * 1000 * 1000)) - 11644473600ULL;;
+}
+#endif
+
+
 BOOL th_stat_path(const char *path, th_stat_data *data)
 {
+#ifdef TH_PLAT_WINDOWS
+    WIN32_FILE_ATTRIBUTE_DATA fdata;
+    if (!GetFileAttributesExA(path, GetFileExInfoStandard, &fdata))
+        return FALSE;
+
+    data->size   = (((uint64_t) fdata.nFileSizeHigh) << 32ULL) | ((uint64_t) fdata.nFileSizeLow);
+    data->ctime  = th_convert_windows_time(fdata.ftCreationTime);
+    data->atime  = th_convert_windows_time(fdata.ftLastAccessTime);
+    data->mtime  = th_convert_windows_time(fdata.ftLastWriteTime);
+
+    data->flags  = (fdata.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ? TH_IS_DIR : 0;
+    data->flags |= (fdata.dwFileAttributes & FILE_ATTRIBUTE_READONLY) ? 0 : TH_IS_WRITABLE;
+    data->flags |= TH_IS_READABLE;
+#else
     uid_t id = geteuid();
     struct stat sb;
     if (stat(path, &sb) < 0)
         return FALSE;
 
     data->size   = sb.st_size;
+    data->ctime  = sb.st_ctime;
     data->atime  = sb.st_atime;
     data->mtime  = sb.st_mtime;
 
     data->flags  = S_ISDIR(sb.st_mode) ? TH_IS_DIR : 0;
     data->flags |= (id == sb.st_uid && (sb.st_mode & S_IWUSR)) ? TH_IS_WRITABLE : 0;
     data->flags |= (id == sb.st_uid && (sb.st_mode & S_IRUSR)) ? TH_IS_READABLE : 0;
+#endif
 
     return TRUE;
 }
--- a/th_file.h	Mon Jul 09 08:06:45 2018 +0300
+++ b/th_file.h	Mon Jul 09 09:25:51 2018 +0300
@@ -38,8 +38,8 @@
 typedef struct
 {
     int flags;
-    off_t size;
-    uint64_t atime, mtime;
+    uint64_t size;
+    uint64_t atime, mtime, ctime;
 } th_stat_data;