Mercurial > hg > th-libs
comparison th_file.c @ 475:77d1af382e08
Improve th_stat_path() by changing flags into a struct and adding more information.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Mon, 09 Jul 2018 07:28:13 +0300 |
parents | 85fa3d333556 |
children | b1e80180818a |
comparison
equal
deleted
inserted
replaced
474:b3b8f90bbbe6 | 475:77d1af382e08 |
---|---|
67 return th_get_data_dir(); | 67 return th_get_data_dir(); |
68 #endif | 68 #endif |
69 } | 69 } |
70 | 70 |
71 | 71 |
72 BOOL th_stat_path(const char *path, int *flags) | 72 BOOL th_stat_path(const char *path, th_stat_data *data) |
73 { | 73 { |
74 *flags = 0; | |
75 | |
76 #ifdef TH_PLAT_WINDOWS | |
77 DWORD attr = GetFileAttributes(path); | |
78 | |
79 *flags |= (attr & FILE_ATTRIBUTE_DIRECTORY) ? TH_IS_DIR : 0; | |
80 *flags |= (attr & FILE_ATTRIBUTE_READONLY) ? 0 : TH_IS_WRITABLE; | |
81 *flags |= TH_IS_READABLE; | |
82 #else | |
83 uid_t id = geteuid(); | 74 uid_t id = geteuid(); |
84 struct stat sb; | 75 struct stat sb; |
85 if (stat(path, &sb) < 0) | 76 if (stat(path, &sb) < 0) |
86 return FALSE; | 77 return FALSE; |
87 | 78 |
88 *flags |= S_ISDIR(sb.st_mode) ? TH_IS_DIR : 0; | 79 data->size = sb.st_size; |
89 *flags |= (id == sb.st_uid && (sb.st_mode & S_IWUSR)) ? TH_IS_WRITABLE : 0; | 80 data->atime = sb.st_atime; |
90 *flags |= (id == sb.st_uid && (sb.st_mode & S_IRUSR)) ? TH_IS_READABLE : 0; | 81 data->mtime = sb.st_mtime; |
91 #endif | 82 |
83 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; | |
85 data->flags |= (id == sb.st_uid && (sb.st_mode & S_IRUSR)) ? TH_IS_READABLE : 0; | |
92 | 86 |
93 return TRUE; | 87 return TRUE; |
94 } | 88 } |
95 | 89 |
96 | 90 |
117 } | 111 } |
118 | 112 |
119 // If the element is there, create it | 113 // If the element is there, create it |
120 if (path[start] != 0) | 114 if (path[start] != 0) |
121 { | 115 { |
122 int flags; | 116 th_stat_data sdata; |
123 BOOL exists = th_stat_path(path, &flags); | 117 BOOL exists = th_stat_path(path, &sdata); |
124 if (exists && (flags & TH_IS_DIR) == 0) | 118 if (exists && (sdata.flags & TH_IS_DIR) == 0) |
125 goto error; | 119 goto error; |
126 | 120 |
127 if (!exists) | 121 if (!exists) |
128 { | 122 { |
129 #ifdef TH_PLAT_WINDOWS | 123 #ifdef TH_PLAT_WINDOWS |