1 /* Work around platform bugs in stat.
2 Copyright (C) 2009-2019 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
);
45 /* Write "sys/stat.h" here, not <sys/stat.h>, otherwise OSF/1 5.1 DTK cc
46 eliminates this include because of the preliminary #include <sys/stat.h>
50 #include "stat-time.h"
61 # define WIN32_LEAN_AND_MEAN
63 # include "stat-w32.h"
67 /* Return TRUE if the given file name denotes an UNC root. */
69 is_unc_root (const char *rname
)
71 /* Test whether it has the syntax '\\server\share'. */
72 if (ISSLASH (rname
[0]) && ISSLASH (rname
[1]))
74 /* It starts with two slashes. Find the next slash. */
75 const char *p
= rname
+ 2;
77 while (*q
!= '\0' && !ISSLASH (*q
))
79 if (q
> p
&& *q
!= '\0')
81 /* Found the next slash at q. */
84 while (*r
!= '\0' && !ISSLASH (*r
))
86 if (r
> q
&& *r
== '\0')
94 /* Store information about NAME into ST. Work around bugs with
95 trailing slashes. Mingw has other bugs (such as st_ino always
96 being 0 on success) which this wrapper does not work around. But
97 at least this implementation provides the ability to emulate fchdir
101 rpl_stat (char const *name
, struct stat
*buf
)
103 #ifdef WINDOWS_NATIVE
104 /* Fill the fields ourselves, because the original stat function returns
105 values for st_atime, st_mtime, st_ctime that depend on the current time
107 <https://lists.gnu.org/r/bug-gnulib/2017-04/msg00134.html> */
108 /* XXX Should we convert to wchar_t* and prepend '\\?\', in order to work
109 around length limitations
110 <https://msdn.microsoft.com/en-us/library/aa365247.aspx> ? */
112 /* POSIX <http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap04.html#tag_04_13>
113 specifies: "More than two leading <slash> characters shall be treated as
114 a single <slash> character." */
115 if (ISSLASH (name
[0]) && ISSLASH (name
[1]) && ISSLASH (name
[2]))
118 while (ISSLASH (name
[1]))
122 size_t len
= strlen (name
);
123 size_t drive_prefix_len
= (HAS_DEVICE (name
) ? 2 : 0);
125 /* Remove trailing slashes (except the very first one, at position
126 drive_prefix_len), but remember their presence. */
128 bool check_dir
= false;
131 while (rlen
> drive_prefix_len
&& ISSLASH (name
[rlen
-1]))
134 if (rlen
== drive_prefix_len
+ 1)
139 /* Handle '' and 'C:'. */
140 if (!check_dir
&& rlen
== drive_prefix_len
)
147 if (rlen
== 1 && ISSLASH (name
[0]) && len
>= 2)
158 malloca_rname
= NULL
;
162 malloca_rname
= malloca (rlen
+ 1);
163 if (malloca_rname
== NULL
)
168 memcpy (malloca_rname
, name
, rlen
);
169 malloca_rname
[rlen
] = '\0';
170 rname
= malloca_rname
;
173 /* There are two ways to get at the requested information:
174 - by scanning the parent directory and examining the relevant
176 - by opening the file directly.
177 The first approach fails for root directories (e.g. 'C:\') and
178 UNC root directories (e.g. '\\server\share').
179 The second approach fails for some system files (e.g. 'C:\pagefile.sys'
180 and 'C:\hiberfil.sys'): ERROR_SHARING_VIOLATION.
181 The second approach gives more information (in particular, correct
182 st_dev, st_ino, st_nlink fields).
183 So we use the second approach and, as a fallback except for root and
184 UNC root directories, also the first approach. */
189 /* Approach based on the file. */
191 /* Open a handle to the file.
193 <https://msdn.microsoft.com/en-us/library/aa363858.aspx>
194 <https://msdn.microsoft.com/en-us/library/aa363874.aspx> */
197 FILE_READ_ATTRIBUTES
,
198 FILE_SHARE_READ
| FILE_SHARE_WRITE
| FILE_SHARE_DELETE
,
201 /* FILE_FLAG_POSIX_SEMANTICS (treat file names that differ only
202 in case as different) makes sense only when applied to *all*
203 filesystem operations. */
204 FILE_FLAG_BACKUP_SEMANTICS
/* | FILE_FLAG_POSIX_SEMANTICS */,
206 if (h
!= INVALID_HANDLE_VALUE
)
208 ret
= _gl_fstat_by_handle (h
, rname
, buf
);
214 /* Test for root and UNC root directories. */
215 if ((rlen
== drive_prefix_len
+ 1 && ISSLASH (rname
[drive_prefix_len
]))
216 || is_unc_root (rname
))
221 /* Approach based on the directory entry. */
223 if (strchr (rname
, '?') != NULL
|| strchr (rname
, '*') != NULL
)
225 /* Other Windows API functions would fail with error
226 ERROR_INVALID_NAME. */
227 if (malloca_rname
!= NULL
)
228 freea (malloca_rname
);
233 /* Get the details about the directory entry. This can be done through
235 <https://msdn.microsoft.com/en-us/library/aa364418.aspx>
236 <https://msdn.microsoft.com/en-us/library/aa365740.aspx>
238 FindFirstFileEx with argument FindExInfoBasic
239 <https://msdn.microsoft.com/en-us/library/aa364419.aspx>
240 <https://msdn.microsoft.com/en-us/library/aa364415.aspx>
241 <https://msdn.microsoft.com/en-us/library/aa365740.aspx> */
242 WIN32_FIND_DATA info
;
243 HANDLE h
= FindFirstFile (rname
, &info
);
244 if (h
== INVALID_HANDLE_VALUE
)
247 /* Test for error conditions before starting to fill *buf. */
248 if (sizeof (buf
->st_size
) <= 4 && info
.nFileSizeHigh
> 0)
251 if (malloca_rname
!= NULL
)
252 freea (malloca_rname
);
257 # if _GL_WINDOWS_STAT_INODES
259 # if _GL_WINDOWS_STAT_INODES == 2
260 buf
->st_ino
._gl_ino
[0] = buf
->st_ino
._gl_ino
[1] = 0;
261 # else /* _GL_WINDOWS_STAT_INODES == 1 */
265 /* st_ino is not wide enough for identifying a file on a device.
266 Without st_ino, st_dev is pointless. */
273 /* XXX How to handle FILE_ATTRIBUTE_REPARSE_POINT ? */
274 ((info
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
) ? _S_IFDIR
| S_IEXEC_UGO
: _S_IFREG
)
276 | ((info
.dwFileAttributes
& FILE_ATTRIBUTE_READONLY
) ? 0 : S_IWRITE_UGO
);
277 if (!(info
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
))
279 /* Determine whether the file is executable by looking at the file
281 if (info
.nFileSizeHigh
> 0 || info
.nFileSizeLow
> 0)
283 const char *last_dot
= NULL
;
285 for (p
= info
.cFileName
; *p
!= '\0'; p
++)
288 if (last_dot
!= NULL
)
290 const char *suffix
= last_dot
+ 1;
291 if (_stricmp (suffix
, "exe") == 0
292 || _stricmp (suffix
, "bat") == 0
293 || _stricmp (suffix
, "cmd") == 0
294 || _stricmp (suffix
, "com") == 0)
301 /* st_nlink. Ignore hard links here. */
304 /* There's no easy way to map the Windows SID concept to an integer. */
308 /* st_rdev is irrelevant for normal files and directories. */
312 if (sizeof (buf
->st_size
) <= 4)
313 /* Range check already done above. */
314 buf
->st_size
= info
.nFileSizeLow
;
316 buf
->st_size
= ((long long) info
.nFileSizeHigh
<< 32) | (long long) info
.nFileSizeLow
;
318 /* st_atime, st_mtime, st_ctime. */
319 # if _GL_WINDOWS_STAT_TIMESPEC
320 buf
->st_atim
= _gl_convert_FILETIME_to_timespec (&info
.ftLastAccessTime
);
321 buf
->st_mtim
= _gl_convert_FILETIME_to_timespec (&info
.ftLastWriteTime
);
322 buf
->st_ctim
= _gl_convert_FILETIME_to_timespec (&info
.ftCreationTime
);
324 buf
->st_atime
= _gl_convert_FILETIME_to_POSIX (&info
.ftLastAccessTime
);
325 buf
->st_mtime
= _gl_convert_FILETIME_to_POSIX (&info
.ftLastWriteTime
);
326 buf
->st_ctime
= _gl_convert_FILETIME_to_POSIX (&info
.ftCreationTime
);
335 if (ret
>= 0 && check_dir
&& !S_ISDIR (buf
->st_mode
))
340 if (malloca_rname
!= NULL
)
342 int saved_errno
= errno
;
343 freea (malloca_rname
);
351 DWORD error
= GetLastError ();
353 fprintf (stderr
, "rpl_stat error 0x%x\n", (unsigned int) error
);
356 if (malloca_rname
!= NULL
)
357 freea (malloca_rname
);
361 /* Some of these errors probably cannot happen with the specific flags
362 that we pass to CreateFile. But who knows... */
363 case ERROR_FILE_NOT_FOUND
: /* The last component of rname does not exist. */
364 case ERROR_PATH_NOT_FOUND
: /* Some directory component in rname does not exist. */
365 case ERROR_BAD_PATHNAME
: /* rname is such as '\\server'. */
366 case ERROR_BAD_NET_NAME
: /* rname is such as '\\server\nonexistentshare'. */
367 case ERROR_INVALID_NAME
: /* rname contains wildcards, misplaced colon, etc. */
368 case ERROR_DIRECTORY
:
372 case ERROR_ACCESS_DENIED
: /* rname is such as 'C:\System Volume Information\foo'. */
373 case ERROR_SHARING_VIOLATION
: /* rname is such as 'C:\pagefile.sys' (second approach only). */
374 /* XXX map to EACCESS or EPERM? */
378 case ERROR_OUTOFMEMORY
:
382 case ERROR_WRITE_PROTECT
:
386 case ERROR_WRITE_FAULT
:
387 case ERROR_READ_FAULT
:
388 case ERROR_GEN_FAILURE
:
392 case ERROR_BUFFER_OVERFLOW
:
393 case ERROR_FILENAME_EXCED_RANGE
:
394 errno
= ENAMETOOLONG
;
397 case ERROR_DELETE_PENDING
: /* XXX map to EACCESS or EPERM? */
409 int result
= orig_stat (name
, buf
);
412 # if REPLACE_FUNC_STAT_FILE
413 /* Solaris 9 mistakenly succeeds when given a non-directory with a
415 if (!S_ISDIR (buf
->st_mode
))
417 size_t len
= strlen (name
);
418 if (ISSLASH (name
[len
- 1]))
424 # endif /* REPLACE_FUNC_STAT_FILE */
425 result
= stat_time_normalize (result
, buf
);