1 /* Work around platform bugs in stat.
2 Copyright (C) 2009-2020 Free Software Foundation, Inc.
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 3 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <https://www.gnu.org/licenses/>. */
17 /* Written by Eric Blake and Bruno Haible. */
19 /* If the user's config.h happens to include <sys/stat.h>, let it include only
20 the system's <sys/stat.h> here, so that orig_stat doesn't recurse to
22 #define __need_system_sys_stat_h
25 /* Get the original definition of stat. It might be defined as a macro. */
26 #include <sys/types.h>
28 #undef __need_system_sys_stat_h
30 #if defined _WIN32 && ! defined __CYGWIN__
31 # define WINDOWS_NATIVE
34 #if !defined WINDOWS_NATIVE
37 orig_stat (const char *filename
, struct stat
*buf
)
39 return stat (filename
, buf
);
46 /* Write "sys/stat.h" here, not <sys/stat.h>, otherwise OSF/1 5.1 DTK cc
47 eliminates this include because of the preliminary #include <sys/stat.h>
49 # include "sys/stat.h"
51 # include <sys/stat.h>
54 #include "stat-time.h"
65 # define WIN32_LEAN_AND_MEAN
67 # include "stat-w32.h"
71 /* Return TRUE if the given file name denotes an UNC root. */
73 is_unc_root (const char *rname
)
75 /* Test whether it has the syntax '\\server\share'. */
76 if (ISSLASH (rname
[0]) && ISSLASH (rname
[1]))
78 /* It starts with two slashes. Find the next slash. */
79 const char *p
= rname
+ 2;
81 while (*q
!= '\0' && !ISSLASH (*q
))
83 if (q
> p
&& *q
!= '\0')
85 /* Found the next slash at q. */
88 while (*r
!= '\0' && !ISSLASH (*r
))
90 if (r
> q
&& *r
== '\0')
98 /* Store information about NAME into ST. Work around bugs with
99 trailing slashes. Mingw has other bugs (such as st_ino always
100 being 0 on success) which this wrapper does not work around. But
101 at least this implementation provides the ability to emulate fchdir
105 rpl_stat (char const *name
, struct stat
*buf
)
107 #ifdef WINDOWS_NATIVE
108 /* Fill the fields ourselves, because the original stat function returns
109 values for st_atime, st_mtime, st_ctime that depend on the current time
111 <https://lists.gnu.org/r/bug-gnulib/2017-04/msg00134.html> */
112 /* XXX Should we convert to wchar_t* and prepend '\\?\', in order to work
113 around length limitations
114 <https://docs.microsoft.com/en-us/windows/desktop/FileIO/naming-a-file> ? */
116 /* POSIX <https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap04.html#tag_04_13>
117 specifies: "More than two leading <slash> characters shall be treated as
118 a single <slash> character." */
119 if (ISSLASH (name
[0]) && ISSLASH (name
[1]) && ISSLASH (name
[2]))
122 while (ISSLASH (name
[1]))
126 size_t len
= strlen (name
);
127 size_t drive_prefix_len
= (HAS_DEVICE (name
) ? 2 : 0);
129 /* Remove trailing slashes (except the very first one, at position
130 drive_prefix_len), but remember their presence. */
132 bool check_dir
= false;
135 while (rlen
> drive_prefix_len
&& ISSLASH (name
[rlen
-1]))
138 if (rlen
== drive_prefix_len
+ 1)
143 /* Handle '' and 'C:'. */
144 if (!check_dir
&& rlen
== drive_prefix_len
)
151 if (rlen
== 1 && ISSLASH (name
[0]) && len
>= 2)
162 malloca_rname
= NULL
;
166 malloca_rname
= malloca (rlen
+ 1);
167 if (malloca_rname
== NULL
)
172 memcpy (malloca_rname
, name
, rlen
);
173 malloca_rname
[rlen
] = '\0';
174 rname
= malloca_rname
;
177 /* There are two ways to get at the requested information:
178 - by scanning the parent directory and examining the relevant
180 - by opening the file directly.
181 The first approach fails for root directories (e.g. 'C:\') and
182 UNC root directories (e.g. '\\server\share').
183 The second approach fails for some system files (e.g. 'C:\pagefile.sys'
184 and 'C:\hiberfil.sys'): ERROR_SHARING_VIOLATION.
185 The second approach gives more information (in particular, correct
186 st_dev, st_ino, st_nlink fields).
187 So we use the second approach and, as a fallback except for root and
188 UNC root directories, also the first approach. */
193 /* Approach based on the file. */
195 /* Open a handle to the file.
197 <https://docs.microsoft.com/en-us/windows/desktop/api/fileapi/nf-fileapi-createfilea>
198 <https://docs.microsoft.com/en-us/windows/desktop/FileIO/creating-and-opening-files> */
201 FILE_READ_ATTRIBUTES
,
202 FILE_SHARE_READ
| FILE_SHARE_WRITE
| FILE_SHARE_DELETE
,
205 /* FILE_FLAG_POSIX_SEMANTICS (treat file names that differ only
206 in case as different) makes sense only when applied to *all*
207 filesystem operations. */
208 FILE_FLAG_BACKUP_SEMANTICS
/* | FILE_FLAG_POSIX_SEMANTICS */,
210 if (h
!= INVALID_HANDLE_VALUE
)
212 ret
= _gl_fstat_by_handle (h
, rname
, buf
);
218 /* Test for root and UNC root directories. */
219 if ((rlen
== drive_prefix_len
+ 1 && ISSLASH (rname
[drive_prefix_len
]))
220 || is_unc_root (rname
))
225 /* Approach based on the directory entry. */
227 if (strchr (rname
, '?') != NULL
|| strchr (rname
, '*') != NULL
)
229 /* Other Windows API functions would fail with error
230 ERROR_INVALID_NAME. */
231 if (malloca_rname
!= NULL
)
232 freea (malloca_rname
);
237 /* Get the details about the directory entry. This can be done through
239 <https://docs.microsoft.com/en-us/windows/desktop/api/fileapi/nf-fileapi-findfirstfilea>
240 <https://docs.microsoft.com/en-us/windows/desktop/api/minwinbase/ns-minwinbase-_win32_find_dataa>
242 FindFirstFileEx with argument FindExInfoBasic
243 <https://docs.microsoft.com/en-us/windows/desktop/api/fileapi/nf-fileapi-findfirstfileexa>
244 <https://docs.microsoft.com/en-us/windows/desktop/api/minwinbase/ne-minwinbase-findex_info_levels>
245 <https://docs.microsoft.com/en-us/windows/desktop/api/minwinbase/ns-minwinbase-_win32_find_dataa> */
246 WIN32_FIND_DATA info
;
247 HANDLE h
= FindFirstFile (rname
, &info
);
248 if (h
== INVALID_HANDLE_VALUE
)
251 /* Test for error conditions before starting to fill *buf. */
252 if (sizeof (buf
->st_size
) <= 4 && info
.nFileSizeHigh
> 0)
255 if (malloca_rname
!= NULL
)
256 freea (malloca_rname
);
261 # if _GL_WINDOWS_STAT_INODES
263 # if _GL_WINDOWS_STAT_INODES == 2
264 buf
->st_ino
._gl_ino
[0] = buf
->st_ino
._gl_ino
[1] = 0;
265 # else /* _GL_WINDOWS_STAT_INODES == 1 */
269 /* st_ino is not wide enough for identifying a file on a device.
270 Without st_ino, st_dev is pointless. */
277 /* XXX How to handle FILE_ATTRIBUTE_REPARSE_POINT ? */
278 ((info
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
) ? _S_IFDIR
| S_IEXEC_UGO
: _S_IFREG
)
280 | ((info
.dwFileAttributes
& FILE_ATTRIBUTE_READONLY
) ? 0 : S_IWRITE_UGO
);
281 if (!(info
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
))
283 /* Determine whether the file is executable by looking at the file
285 if (info
.nFileSizeHigh
> 0 || info
.nFileSizeLow
> 0)
287 const char *last_dot
= NULL
;
289 for (p
= info
.cFileName
; *p
!= '\0'; p
++)
292 if (last_dot
!= NULL
)
294 const char *suffix
= last_dot
+ 1;
295 if (_stricmp (suffix
, "exe") == 0
296 || _stricmp (suffix
, "bat") == 0
297 || _stricmp (suffix
, "cmd") == 0
298 || _stricmp (suffix
, "com") == 0)
305 /* st_nlink. Ignore hard links here. */
308 /* There's no easy way to map the Windows SID concept to an integer. */
312 /* st_rdev is irrelevant for normal files and directories. */
316 if (sizeof (buf
->st_size
) <= 4)
317 /* Range check already done above. */
318 buf
->st_size
= info
.nFileSizeLow
;
320 buf
->st_size
= ((long long) info
.nFileSizeHigh
<< 32) | (long long) info
.nFileSizeLow
;
322 /* st_atime, st_mtime, st_ctime. */
323 # if _GL_WINDOWS_STAT_TIMESPEC
324 buf
->st_atim
= _gl_convert_FILETIME_to_timespec (&info
.ftLastAccessTime
);
325 buf
->st_mtim
= _gl_convert_FILETIME_to_timespec (&info
.ftLastWriteTime
);
326 buf
->st_ctim
= _gl_convert_FILETIME_to_timespec (&info
.ftCreationTime
);
328 buf
->st_atime
= _gl_convert_FILETIME_to_POSIX (&info
.ftLastAccessTime
);
329 buf
->st_mtime
= _gl_convert_FILETIME_to_POSIX (&info
.ftLastWriteTime
);
330 buf
->st_ctime
= _gl_convert_FILETIME_to_POSIX (&info
.ftCreationTime
);
339 if (ret
>= 0 && check_dir
&& !S_ISDIR (buf
->st_mode
))
344 if (malloca_rname
!= NULL
)
346 int saved_errno
= errno
;
347 freea (malloca_rname
);
355 DWORD error
= GetLastError ();
357 fprintf (stderr
, "rpl_stat error 0x%x\n", (unsigned int) error
);
360 if (malloca_rname
!= NULL
)
361 freea (malloca_rname
);
365 /* Some of these errors probably cannot happen with the specific flags
366 that we pass to CreateFile. But who knows... */
367 case ERROR_FILE_NOT_FOUND
: /* The last component of rname does not exist. */
368 case ERROR_PATH_NOT_FOUND
: /* Some directory component in rname does not exist. */
369 case ERROR_BAD_PATHNAME
: /* rname is such as '\\server'. */
370 case ERROR_BAD_NET_NAME
: /* rname is such as '\\server\nonexistentshare'. */
371 case ERROR_INVALID_NAME
: /* rname contains wildcards, misplaced colon, etc. */
372 case ERROR_DIRECTORY
:
376 case ERROR_ACCESS_DENIED
: /* rname is such as 'C:\System Volume Information\foo'. */
377 case ERROR_SHARING_VIOLATION
: /* rname is such as 'C:\pagefile.sys' (second approach only). */
378 /* XXX map to EACCES or EPERM? */
382 case ERROR_OUTOFMEMORY
:
386 case ERROR_WRITE_PROTECT
:
390 case ERROR_WRITE_FAULT
:
391 case ERROR_READ_FAULT
:
392 case ERROR_GEN_FAILURE
:
396 case ERROR_BUFFER_OVERFLOW
:
397 case ERROR_FILENAME_EXCED_RANGE
:
398 errno
= ENAMETOOLONG
;
401 case ERROR_DELETE_PENDING
: /* XXX map to EACCES or EPERM? */
413 int result
= orig_stat (name
, buf
);
416 # if REPLACE_FUNC_STAT_FILE
417 /* Solaris 9 mistakenly succeeds when given a non-directory with a
419 if (!S_ISDIR (buf
->st_mode
))
421 size_t len
= strlen (name
);
422 if (ISSLASH (name
[len
- 1]))
428 # endif /* REPLACE_FUNC_STAT_FILE */
429 result
= stat_time_normalize (result
, buf
);