comparison th_file.c @ 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 77d1af382e08
children 2aa2052cb17d
comparison
equal deleted inserted replaced
477:96d137a6b392 478:b1e80180818a
6 * Please read file 'COPYING' for information on license and distribution. 6 * Please read file 'COPYING' for information on license and distribution.
7 */ 7 */
8 #include "th_file.h" 8 #include "th_file.h"
9 #include "th_string.h" 9 #include "th_string.h"
10 #include <unistd.h> 10 #include <unistd.h>
11 //#include <fcntl.h>
12 #ifdef TH_PLAT_WINDOWS 11 #ifdef TH_PLAT_WINDOWS
13 # include <shlwapi.h> 12 # include <shlwapi.h>
14 # include <shfolder.h> 13 # include <shfolder.h>
15 #else 14 #else
16 //# include <sys/wait.h> 15 //# include <sys/wait.h>
67 return th_get_data_dir(); 66 return th_get_data_dir();
68 #endif 67 #endif
69 } 68 }
70 69
71 70
71 #ifdef TH_PLAT_WINDOWS
72 static uint64_t th_convert_windows_time(FILETIME ftime)
73 {
74 uint64_t value = (((uint64_t) ftime.dwHighDateTime) << 32ULL) | ((uint64_t) ftime.dwLowDateTime);
75
76 // Naive conversion (1000 ns / 100) * ns - difference_between_win_to_unix_epoch
77 return (value / ((1000 / 100) * 1000 * 1000)) - 11644473600ULL;;
78 }
79 #endif
80
81
72 BOOL th_stat_path(const char *path, th_stat_data *data) 82 BOOL th_stat_path(const char *path, th_stat_data *data)
73 { 83 {
84 #ifdef TH_PLAT_WINDOWS
85 WIN32_FILE_ATTRIBUTE_DATA fdata;
86 if (!GetFileAttributesExA(path, GetFileExInfoStandard, &fdata))
87 return FALSE;
88
89 data->size = (((uint64_t) fdata.nFileSizeHigh) << 32ULL) | ((uint64_t) fdata.nFileSizeLow);
90 data->ctime = th_convert_windows_time(fdata.ftCreationTime);
91 data->atime = th_convert_windows_time(fdata.ftLastAccessTime);
92 data->mtime = th_convert_windows_time(fdata.ftLastWriteTime);
93
94 data->flags = (fdata.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ? TH_IS_DIR : 0;
95 data->flags |= (fdata.dwFileAttributes & FILE_ATTRIBUTE_READONLY) ? 0 : TH_IS_WRITABLE;
96 data->flags |= TH_IS_READABLE;
97 #else
74 uid_t id = geteuid(); 98 uid_t id = geteuid();
75 struct stat sb; 99 struct stat sb;
76 if (stat(path, &sb) < 0) 100 if (stat(path, &sb) < 0)
77 return FALSE; 101 return FALSE;
78 102
79 data->size = sb.st_size; 103 data->size = sb.st_size;
104 data->ctime = sb.st_ctime;
80 data->atime = sb.st_atime; 105 data->atime = sb.st_atime;
81 data->mtime = sb.st_mtime; 106 data->mtime = sb.st_mtime;
82 107
83 data->flags = S_ISDIR(sb.st_mode) ? TH_IS_DIR : 0; 108 data->flags = S_ISDIR(sb.st_mode) ? TH_IS_DIR : 0;
84 data->flags |= (id == sb.st_uid && (sb.st_mode & S_IWUSR)) ? TH_IS_WRITABLE : 0; 109 data->flags |= (id == sb.st_uid && (sb.st_mode & S_IWUSR)) ? TH_IS_WRITABLE : 0;
85 data->flags |= (id == sb.st_uid && (sb.st_mode & S_IRUSR)) ? TH_IS_READABLE : 0; 110 data->flags |= (id == sb.st_uid && (sb.st_mode & S_IRUSR)) ? TH_IS_READABLE : 0;
111 #endif
86 112
87 return TRUE; 113 return TRUE;
88 } 114 }
89 115
90 116