This source file includes following definitions.
- orig_stat
- is_unc_root
- rpl_stat
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 #define __need_system_sys_stat_h
23 #include <config.h>
24
25
26 #include <sys/types.h>
27 #include <sys/stat.h>
28 #undef __need_system_sys_stat_h
29
30 #if defined _WIN32 && ! defined __CYGWIN__
31 # define WINDOWS_NATIVE
32 #endif
33
34 #if !defined WINDOWS_NATIVE
35
36 static int
37 orig_stat (const char *filename, struct stat *buf)
38 {
39 return stat (filename, buf);
40 }
41
42 #endif
43
44
45 #ifdef __osf__
46
47
48
49 # include "sys/stat.h"
50 #else
51 # include <sys/stat.h>
52 #endif
53
54 #include "stat-time.h"
55
56 #include <errno.h>
57 #include <limits.h>
58 #include <stdbool.h>
59 #include <string.h>
60 #include "filename.h"
61 #include "malloca.h"
62 #include "verify.h"
63
64 #ifdef WINDOWS_NATIVE
65 # define WIN32_LEAN_AND_MEAN
66 # include <windows.h>
67 # include "stat-w32.h"
68
69 # undef WIN32_FIND_DATA
70 # define WIN32_FIND_DATA WIN32_FIND_DATAA
71 # undef CreateFile
72 # define CreateFile CreateFileA
73 # undef FindFirstFile
74 # define FindFirstFile FindFirstFileA
75 #endif
76
77 #ifdef WINDOWS_NATIVE
78
79 static BOOL
80 is_unc_root (const char *rname)
81 {
82
83 if (ISSLASH (rname[0]) && ISSLASH (rname[1]))
84 {
85
86 const char *p = rname + 2;
87 const char *q = p;
88 while (*q != '\0' && !ISSLASH (*q))
89 q++;
90 if (q > p && *q != '\0')
91 {
92
93 q++;
94 const char *r = q;
95 while (*r != '\0' && !ISSLASH (*r))
96 r++;
97 if (r > q && *r == '\0')
98 return TRUE;
99 }
100 }
101 return FALSE;
102 }
103 #endif
104
105
106
107
108
109
110
111 int
112 rpl_stat (char const *name, struct stat *buf)
113 {
114 #ifdef WINDOWS_NATIVE
115
116
117
118
119
120
121
122
123
124
125
126 if (ISSLASH (name[0]) && ISSLASH (name[1]) && ISSLASH (name[2]))
127 {
128 name += 2;
129 while (ISSLASH (name[1]))
130 name++;
131 }
132
133 size_t len = strlen (name);
134 size_t drive_prefix_len = (HAS_DEVICE (name) ? 2 : 0);
135
136
137
138 size_t rlen;
139 bool check_dir = false;
140
141 rlen = len;
142 while (rlen > drive_prefix_len && ISSLASH (name[rlen-1]))
143 {
144 check_dir = true;
145 if (rlen == drive_prefix_len + 1)
146 break;
147 rlen--;
148 }
149
150
151 if (!check_dir && rlen == drive_prefix_len)
152 {
153 errno = ENOENT;
154 return -1;
155 }
156
157
158 if (rlen == 1 && ISSLASH (name[0]) && len >= 2)
159 {
160 errno = ENOENT;
161 return -1;
162 }
163
164 const char *rname;
165 char *malloca_rname;
166 if (rlen == len)
167 {
168 rname = name;
169 malloca_rname = NULL;
170 }
171 else
172 {
173 malloca_rname = malloca (rlen + 1);
174 if (malloca_rname == NULL)
175 {
176 errno = ENOMEM;
177 return -1;
178 }
179 memcpy (malloca_rname, name, rlen);
180 malloca_rname[rlen] = '\0';
181 rname = malloca_rname;
182 }
183
184
185
186
187
188
189
190
191
192
193
194
195
196 {
197 int ret;
198
199 {
200
201
202
203
204
205
206 HANDLE h =
207 CreateFile (rname,
208 FILE_READ_ATTRIBUTES,
209 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
210 NULL,
211 OPEN_EXISTING,
212
213
214
215 FILE_FLAG_BACKUP_SEMANTICS ,
216 NULL);
217 if (h != INVALID_HANDLE_VALUE)
218 {
219 ret = _gl_fstat_by_handle (h, rname, buf);
220 CloseHandle (h);
221 goto done;
222 }
223 }
224
225
226 if ((rlen == drive_prefix_len + 1 && ISSLASH (rname[drive_prefix_len]))
227 || is_unc_root (rname))
228 goto failed;
229
230
231 {
232
233
234 if (strchr (rname, '?') != NULL || strchr (rname, '*') != NULL)
235 {
236
237
238 if (malloca_rname != NULL)
239 freea (malloca_rname);
240 errno = ENOENT;
241 return -1;
242 }
243
244
245
246
247
248
249
250
251
252
253 WIN32_FIND_DATA info;
254 HANDLE h = FindFirstFile (rname, &info);
255 if (h == INVALID_HANDLE_VALUE)
256 goto failed;
257
258
259 if (sizeof (buf->st_size) <= 4 && info.nFileSizeHigh > 0)
260 {
261 FindClose (h);
262 if (malloca_rname != NULL)
263 freea (malloca_rname);
264 errno = EOVERFLOW;
265 return -1;
266 }
267
268 # if _GL_WINDOWS_STAT_INODES
269 buf->st_dev = 0;
270 # if _GL_WINDOWS_STAT_INODES == 2
271 buf->st_ino._gl_ino[0] = buf->st_ino._gl_ino[1] = 0;
272 # else
273 buf->st_ino = 0;
274 # endif
275 # else
276
277
278 buf->st_dev = 0;
279 buf->st_ino = 0;
280 # endif
281
282
283 unsigned int mode =
284
285 ((info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ? _S_IFDIR | S_IEXEC_UGO : _S_IFREG)
286 | S_IREAD_UGO
287 | ((info.dwFileAttributes & FILE_ATTRIBUTE_READONLY) ? 0 : S_IWRITE_UGO);
288 if (!(info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
289 {
290
291
292 if (info.nFileSizeHigh > 0 || info.nFileSizeLow > 0)
293 {
294 const char *last_dot = NULL;
295 const char *p;
296 for (p = info.cFileName; *p != '\0'; p++)
297 if (*p == '.')
298 last_dot = p;
299 if (last_dot != NULL)
300 {
301 const char *suffix = last_dot + 1;
302 if (_stricmp (suffix, "exe") == 0
303 || _stricmp (suffix, "bat") == 0
304 || _stricmp (suffix, "cmd") == 0
305 || _stricmp (suffix, "com") == 0)
306 mode |= S_IEXEC_UGO;
307 }
308 }
309 }
310 buf->st_mode = mode;
311
312
313 buf->st_nlink = 1;
314
315
316 buf->st_uid = 0;
317 buf->st_gid = 0;
318
319
320 buf->st_rdev = 0;
321
322
323 if (sizeof (buf->st_size) <= 4)
324
325 buf->st_size = info.nFileSizeLow;
326 else
327 buf->st_size = ((long long) info.nFileSizeHigh << 32) | (long long) info.nFileSizeLow;
328
329
330 # if _GL_WINDOWS_STAT_TIMESPEC
331 buf->st_atim = _gl_convert_FILETIME_to_timespec (&info.ftLastAccessTime);
332 buf->st_mtim = _gl_convert_FILETIME_to_timespec (&info.ftLastWriteTime);
333 buf->st_ctim = _gl_convert_FILETIME_to_timespec (&info.ftCreationTime);
334 # else
335 buf->st_atime = _gl_convert_FILETIME_to_POSIX (&info.ftLastAccessTime);
336 buf->st_mtime = _gl_convert_FILETIME_to_POSIX (&info.ftLastWriteTime);
337 buf->st_ctime = _gl_convert_FILETIME_to_POSIX (&info.ftCreationTime);
338 # endif
339
340 FindClose (h);
341
342 ret = 0;
343 }
344
345 done:
346 if (ret >= 0 && check_dir && !S_ISDIR (buf->st_mode))
347 {
348 errno = ENOTDIR;
349 ret = -1;
350 }
351 if (malloca_rname != NULL)
352 {
353 int saved_errno = errno;
354 freea (malloca_rname);
355 errno = saved_errno;
356 }
357 return ret;
358 }
359
360 failed:
361 {
362 DWORD error = GetLastError ();
363 #if 0
364 fprintf (stderr, "rpl_stat error 0x%x\n", (unsigned int) error);
365 #endif
366
367 if (malloca_rname != NULL)
368 freea (malloca_rname);
369
370 switch (error)
371 {
372
373
374 case ERROR_FILE_NOT_FOUND:
375 case ERROR_PATH_NOT_FOUND:
376 case ERROR_BAD_PATHNAME:
377 case ERROR_BAD_NET_NAME:
378 case ERROR_INVALID_NAME:
379 case ERROR_DIRECTORY:
380 errno = ENOENT;
381 break;
382
383 case ERROR_ACCESS_DENIED:
384 case ERROR_SHARING_VIOLATION:
385
386 errno = EACCES;
387 break;
388
389 case ERROR_OUTOFMEMORY:
390 errno = ENOMEM;
391 break;
392
393 case ERROR_WRITE_PROTECT:
394 errno = EROFS;
395 break;
396
397 case ERROR_WRITE_FAULT:
398 case ERROR_READ_FAULT:
399 case ERROR_GEN_FAILURE:
400 errno = EIO;
401 break;
402
403 case ERROR_BUFFER_OVERFLOW:
404 case ERROR_FILENAME_EXCED_RANGE:
405 errno = ENAMETOOLONG;
406 break;
407
408 case ERROR_DELETE_PENDING:
409 errno = EPERM;
410 break;
411
412 default:
413 errno = EINVAL;
414 break;
415 }
416
417 return -1;
418 }
419 #else
420 int result = orig_stat (name, buf);
421 if (result == 0)
422 {
423 # if REPLACE_FUNC_STAT_FILE
424
425
426 if (!S_ISDIR (buf->st_mode))
427 {
428 size_t len = strlen (name);
429 if (ISSLASH (name[len - 1]))
430 {
431 errno = ENOTDIR;
432 return -1;
433 }
434 }
435 # endif
436 result = stat_time_normalize (result, buf);
437 }
438 return result;
439 #endif
440 }