# HG changeset patch # User Matti Hamalainen # Date 1531117551 -10800 # Node ID b1e80180818a6dfedc35d4a3abb7433cf3d27155 # Parent 96d137a6b3928e419b226e712b822859e11ba98b Improve th_stat_path() some more and fix Windows issues. diff -r 96d137a6b392 -r b1e80180818a th_file.c --- 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 -//#include #ifdef TH_PLAT_WINDOWS # include # include @@ -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; } diff -r 96d137a6b392 -r b1e80180818a th_file.h --- 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;