1 /* Core of implementation of fstat and stat for native Windows.
2 Copyright (C) 2017 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 <http://www.gnu.org/licenses/>. */
17 /* Written by Bruno Haible. */
21 #if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
23 /* Ensure that <windows.h> defines FILE_ID_INFO. */
25 #define _WIN32_WINNT _WIN32_WINNT_WIN8
27 #include <sys/types.h>
41 #if _GL_WINDOWS_STAT_INODES == 2
42 /* GetFileInformationByHandleEx was introduced only in Windows Vista. */
43 typedef DWORD (WINAPI
* GetFileInformationByHandleExFuncType
) (HANDLE hFile
,
44 FILE_INFO_BY_HANDLE_CLASS fiClass
,
47 static GetFileInformationByHandleExFuncType GetFileInformationByHandleExFunc
= NULL
;
49 /* GetFinalPathNameByHandle was introduced only in Windows Vista. */
50 typedef DWORD (WINAPI
* GetFinalPathNameByHandleFuncType
) (HANDLE hFile
,
54 static GetFinalPathNameByHandleFuncType GetFinalPathNameByHandleFunc
= NULL
;
55 static BOOL initialized
= FALSE
;
60 HMODULE kernel32
= LoadLibrary ("kernel32.dll");
63 #if _GL_WINDOWS_STAT_INODES == 2
64 GetFileInformationByHandleExFunc
=
65 (GetFileInformationByHandleExFuncType
) GetProcAddress (kernel32
, "GetFileInformationByHandleEx");
67 GetFinalPathNameByHandleFunc
=
68 (GetFinalPathNameByHandleFuncType
) GetProcAddress (kernel32
, "GetFinalPathNameByHandleA");
73 /* Converts a FILETIME to GMT time since 1970-01-01 00:00:00. */
74 #if _GL_WINDOWS_STAT_TIMESPEC
76 _gl_convert_FILETIME_to_timespec (const FILETIME
*ft
)
78 struct timespec result
;
79 /* FILETIME: <https://msdn.microsoft.com/en-us/library/ms724284.aspx> */
80 unsigned long long since_1601
=
81 ((unsigned long long) ft
->dwHighDateTime
<< 32)
82 | (unsigned long long) ft
->dwLowDateTime
;
90 /* Between 1601-01-01 and 1970-01-01 there were 280 normal years and 89
91 leap years, in total 134774 days. */
92 unsigned long long since_1970
=
93 since_1601
- (unsigned long long) 134774 * (unsigned long long) 86400 * (unsigned long long) 10000000;
94 result
.tv_sec
= since_1970
/ (unsigned long long) 10000000;
95 result
.tv_nsec
= (unsigned long) (since_1970
% (unsigned long long) 10000000) * 100;
101 _gl_convert_FILETIME_to_POSIX (const FILETIME
*ft
)
103 /* FILETIME: <https://msdn.microsoft.com/en-us/library/ms724284.aspx> */
104 unsigned long long since_1601
=
105 ((unsigned long long) ft
->dwHighDateTime
<< 32)
106 | (unsigned long long) ft
->dwLowDateTime
;
111 /* Between 1601-01-01 and 1970-01-01 there were 280 normal years and 89
112 leap years, in total 134774 days. */
113 unsigned long long since_1970
=
114 since_1601
- (unsigned long long) 134774 * (unsigned long long) 86400 * (unsigned long long) 10000000;
115 return since_1970
/ (unsigned long long) 10000000;
120 /* Fill *BUF with information about the file designated by H.
121 PATH is the file name, if known, otherwise NULL.
122 Return 0 if successful, or -1 with errno set upon failure. */
124 _gl_fstat_by_handle (HANDLE h
, const char *path
, struct stat
*buf
)
127 <https://msdn.microsoft.com/en-us/library/aa364960.aspx> */
128 DWORD type
= GetFileType (h
);
129 if (type
== FILE_TYPE_DISK
)
134 /* st_mode can be determined through
136 <https://msdn.microsoft.com/en-us/library/aa364946.aspx>
137 <https://msdn.microsoft.com/en-us/library/aa365739.aspx>
139 GetFileInformationByHandle
140 <https://msdn.microsoft.com/en-us/library/aa364952.aspx>
141 <https://msdn.microsoft.com/en-us/library/aa363788.aspx>
143 GetFileInformationByHandleEx with argument FileBasicInfo
144 <https://msdn.microsoft.com/en-us/library/aa364953.aspx>
145 <https://msdn.microsoft.com/en-us/library/aa364217.aspx>
146 The latter requires -D_WIN32_WINNT=_WIN32_WINNT_VISTA or higher. */
147 BY_HANDLE_FILE_INFORMATION info
;
148 if (! GetFileInformationByHandle (h
, &info
))
151 /* Test for error conditions before starting to fill *buf. */
152 if (sizeof (buf
->st_size
) <= 4 && info
.nFileSizeHigh
> 0)
158 #if _GL_WINDOWS_STAT_INODES
159 /* st_ino can be determined through
160 GetFileInformationByHandle
161 <https://msdn.microsoft.com/en-us/library/aa364952.aspx>
162 <https://msdn.microsoft.com/en-us/library/aa363788.aspx>
163 as 64 bits, or through
164 GetFileInformationByHandleEx with argument FileIdInfo
165 <https://msdn.microsoft.com/en-us/library/aa364953.aspx>
166 <https://msdn.microsoft.com/en-us/library/hh802691.aspx>
168 The latter requires -D_WIN32_WINNT=_WIN32_WINNT_WIN8 or higher. */
169 /* Experiments show that GetFileInformationByHandleEx does not provide
170 much more information than GetFileInformationByHandle:
171 * The dwVolumeSerialNumber from GetFileInformationByHandle is equal
172 to the low 32 bits of the 64-bit VolumeSerialNumber from
173 GetFileInformationByHandleEx, and is apparently sufficient for
174 identifying the device.
175 * The nFileIndex from GetFileInformationByHandle is equal to the low
176 64 bits of the 128-bit FileId from GetFileInformationByHandleEx,
177 and the high 64 bits of this 128-bit FileId are zero.
178 * On a FAT file system, GetFileInformationByHandleEx fails with error
179 ERROR_INVALID_PARAMETER, whereas GetFileInformationByHandle
181 * On a CIFS/SMB file system, GetFileInformationByHandleEx fails with
182 error ERROR_INVALID_LEVEL, whereas GetFileInformationByHandle
184 # if _GL_WINDOWS_STAT_INODES == 2
185 if (GetFileInformationByHandleExFunc
!= NULL
)
188 if (GetFileInformationByHandleExFunc (h
, FileIdInfo
, &id
, sizeof (id
)))
190 buf
->st_dev
= id
.VolumeSerialNumber
;
191 verify (sizeof (ino_t
) == sizeof (id
.FileId
));
192 memcpy (&buf
->st_ino
, &id
.FileId
, sizeof (ino_t
));
197 switch (GetLastError ())
199 case ERROR_INVALID_PARAMETER
: /* older Windows version, or FAT */
200 case ERROR_INVALID_LEVEL
: /* CIFS/SMB file system */
208 /* Fallback for older Windows versions. */
209 buf
->st_dev
= info
.dwVolumeSerialNumber
;
210 buf
->st_ino
._gl_ino
[0] = ((ULONGLONG
) info
.nFileIndexHigh
<< 32) | (ULONGLONG
) info
.nFileIndexLow
;
211 buf
->st_ino
._gl_ino
[1] = 0;
213 # else /* _GL_WINDOWS_STAT_INODES == 1 */
214 buf
->st_dev
= info
.dwVolumeSerialNumber
;
215 buf
->st_ino
= ((ULONGLONG
) info
.nFileIndexHigh
<< 32) | (ULONGLONG
) info
.nFileIndexLow
;
218 /* st_ino is not wide enough for identifying a file on a device.
219 Without st_ino, st_dev is pointless. */
226 /* XXX How to handle FILE_ATTRIBUTE_REPARSE_POINT ? */
227 ((info
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
) ? _S_IFDIR
| S_IEXEC_UGO
: _S_IFREG
)
229 | ((info
.dwFileAttributes
& FILE_ATTRIBUTE_READONLY
) ? 0 : S_IWRITE_UGO
);
230 if (!(info
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
))
232 /* Determine whether the file is executable by looking at the file
234 If the file name is already known, use it. Otherwise, for
235 non-empty files, it can be determined through
236 GetFinalPathNameByHandle
237 <https://msdn.microsoft.com/en-us/library/aa364962.aspx>
239 GetFileInformationByHandleEx with argument FileNameInfo
240 <https://msdn.microsoft.com/en-us/library/aa364953.aspx>
241 <https://msdn.microsoft.com/en-us/library/aa364388.aspx>
242 Both require -D_WIN32_WINNT=_WIN32_WINNT_VISTA or higher. */
243 if (info
.nFileSizeHigh
> 0 || info
.nFileSizeLow
> 0)
245 char fpath
[PATH_MAX
];
247 || (GetFinalPathNameByHandleFunc
!= NULL
248 && GetFinalPathNameByHandleFunc (h
, fpath
, sizeof (fpath
), VOLUME_NAME_NONE
)
250 && (path
= fpath
, 1)))
252 const char *last_dot
= NULL
;
254 for (p
= path
; *p
!= '\0'; p
++)
257 if (last_dot
!= NULL
)
259 const char *suffix
= last_dot
+ 1;
260 if (_stricmp (suffix
, "exe") == 0
261 || _stricmp (suffix
, "bat") == 0
262 || _stricmp (suffix
, "cmd") == 0
263 || _stricmp (suffix
, "com") == 0)
268 /* Cannot determine file name. Pretend that it is executable. */
274 /* st_nlink can be determined through
275 GetFileInformationByHandle
276 <https://msdn.microsoft.com/en-us/library/aa364952.aspx>
277 <https://msdn.microsoft.com/en-us/library/aa363788.aspx>
279 GetFileInformationByHandleEx with argument FileStandardInfo
280 <https://msdn.microsoft.com/en-us/library/aa364953.aspx>
281 <https://msdn.microsoft.com/en-us/library/aa364401.aspx>
282 The latter requires -D_WIN32_WINNT=_WIN32_WINNT_VISTA or higher. */
283 buf
->st_nlink
= (info
.nNumberOfLinks
> SHRT_MAX
? SHRT_MAX
: info
.nNumberOfLinks
);
285 /* There's no easy way to map the Windows SID concept to an integer. */
289 /* st_rdev is irrelevant for normal files and directories. */
292 /* st_size can be determined through
294 <https://msdn.microsoft.com/en-us/library/aa364957.aspx>
297 <https://msdn.microsoft.com/en-us/library/aa364946.aspx>
298 <https://msdn.microsoft.com/en-us/library/aa365739.aspx>
300 GetFileInformationByHandle
301 <https://msdn.microsoft.com/en-us/library/aa364952.aspx>
302 <https://msdn.microsoft.com/en-us/library/aa363788.aspx>
304 GetFileInformationByHandleEx with argument FileStandardInfo
305 <https://msdn.microsoft.com/en-us/library/aa364953.aspx>
306 <https://msdn.microsoft.com/en-us/library/aa364401.aspx>
307 The latter requires -D_WIN32_WINNT=_WIN32_WINNT_VISTA or higher. */
308 if (sizeof (buf
->st_size
) <= 4)
309 /* Range check already done above. */
310 buf
->st_size
= info
.nFileSizeLow
;
312 buf
->st_size
= ((long long) info
.nFileSizeHigh
<< 32) | (long long) info
.nFileSizeLow
;
314 /* st_atime, st_mtime, st_ctime can be determined through
316 <https://msdn.microsoft.com/en-us/library/ms724320.aspx>
319 <https://msdn.microsoft.com/en-us/library/aa364946.aspx>
320 <https://msdn.microsoft.com/en-us/library/aa365739.aspx>
322 GetFileInformationByHandle
323 <https://msdn.microsoft.com/en-us/library/aa364952.aspx>
324 <https://msdn.microsoft.com/en-us/library/aa363788.aspx>
326 GetFileInformationByHandleEx with argument FileBasicInfo
327 <https://msdn.microsoft.com/en-us/library/aa364953.aspx>
328 <https://msdn.microsoft.com/en-us/library/aa364217.aspx>
329 The latter requires -D_WIN32_WINNT=_WIN32_WINNT_VISTA or higher. */
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
);
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
);
342 else if (type
== FILE_TYPE_CHAR
|| type
== FILE_TYPE_PIPE
)
345 #if _GL_WINDOWS_STAT_INODES == 2
346 buf
->st_ino
._gl_ino
[0] = buf
->st_ino
._gl_ino
[1] = 0;
350 buf
->st_mode
= (type
== FILE_TYPE_PIPE
? _S_IFIFO
: _S_IFCHR
);
355 if (type
== FILE_TYPE_PIPE
)
358 <https://msdn.microsoft.com/en-us/library/aa365779.aspx> */
359 DWORD bytes_available
;
360 if (PeekNamedPipe (h
, NULL
, 0, NULL
, &bytes_available
, NULL
))
361 buf
->st_size
= bytes_available
;
367 #if _GL_WINDOWS_STAT_TIMESPEC
368 buf
->st_atim
.tv_sec
= 0; buf
->st_atim
.tv_nsec
= 0;
369 buf
->st_mtim
.tv_sec
= 0; buf
->st_mtim
.tv_nsec
= 0;
370 buf
->st_ctim
.tv_sec
= 0; buf
->st_ctim
.tv_nsec
= 0;
386 DWORD error
= GetLastError ();
388 fprintf (stderr
, "_gl_fstat_by_handle error 0x%x\n", (unsigned int) error
);
392 case ERROR_ACCESS_DENIED
:
393 case ERROR_SHARING_VIOLATION
:
397 case ERROR_OUTOFMEMORY
:
401 case ERROR_WRITE_FAULT
:
402 case ERROR_READ_FAULT
:
403 case ERROR_GEN_FAILURE
:
417 /* This declaration is solely to ensure that after preprocessing
418 this file is never empty. */