1 /* Core of implementation of fstat and stat for native Windows.
2 Copyright (C) 2017-2021 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 Bruno Haible. */
21 #if defined _WIN32 && ! defined __CYGWIN__
23 /* Attempt to make <windows.h> define FILE_ID_INFO.
24 But ensure that the redefinition of _WIN32_WINNT does not make us assume
25 Windows Vista or newer when building for an older version of Windows. */
27 # include <sdkddkver.h>
28 # if _WIN32_WINNT >= _WIN32_WINNT_VISTA
29 # define WIN32_ASSUME_VISTA 1
31 # define WIN32_ASSUME_VISTA 0
33 # if !defined _WIN32_WINNT || (_WIN32_WINNT < _WIN32_WINNT_WIN8)
35 # define _WIN32_WINNT _WIN32_WINNT_WIN8
38 # define WIN32_ASSUME_VISTA (_WIN32_WINNT >= _WIN32_WINNT_VISTA)
41 #include <sys/types.h>
55 /* Don't assume that UNICODE is not defined. */
57 #define LoadLibrary LoadLibraryA
58 #undef GetFinalPathNameByHandle
59 #define GetFinalPathNameByHandle GetFinalPathNameByHandleA
61 /* Older mingw headers do not define VOLUME_NAME_NONE. */
62 #ifndef VOLUME_NAME_NONE
63 # define VOLUME_NAME_NONE 4
66 #if !WIN32_ASSUME_VISTA
68 /* Avoid warnings from gcc -Wcast-function-type. */
69 # define GetProcAddress \
70 (void *) GetProcAddress
72 # if _GL_WINDOWS_STAT_INODES == 2
73 /* GetFileInformationByHandleEx was introduced only in Windows Vista. */
74 typedef DWORD (WINAPI * GetFileInformationByHandleExFuncType) (HANDLE hFile,
75 FILE_INFO_BY_HANDLE_CLASS fiClass,
78 static GetFileInformationByHandleExFuncType GetFileInformationByHandleExFunc = NULL;
80 /* GetFinalPathNameByHandle was introduced only in Windows Vista. */
81 typedef DWORD (WINAPI * GetFinalPathNameByHandleFuncType) (HANDLE hFile,
85 static GetFinalPathNameByHandleFuncType GetFinalPathNameByHandleFunc = NULL;
86 static BOOL initialized = FALSE;
91 HMODULE kernel32 = LoadLibrary ("kernel32.dll");
94 # if _GL_WINDOWS_STAT_INODES == 2
95 GetFileInformationByHandleExFunc =
96 (GetFileInformationByHandleExFuncType) GetProcAddress (kernel32, "GetFileInformationByHandleEx");
98 GetFinalPathNameByHandleFunc =
99 (GetFinalPathNameByHandleFuncType) GetProcAddress (kernel32, "GetFinalPathNameByHandleA");
106 # define GetFileInformationByHandleExFunc GetFileInformationByHandleEx
107 # define GetFinalPathNameByHandleFunc GetFinalPathNameByHandle
111 /* Converts a FILETIME to GMT time since 1970-01-01 00:00:00. */
112 #if _GL_WINDOWS_STAT_TIMESPEC
114 _gl_convert_FILETIME_to_timespec (const FILETIME *ft)
116 struct timespec result;
117 /* FILETIME: <https://docs.microsoft.com/en-us/windows/desktop/api/minwinbase/ns-minwinbase-filetime> */
118 unsigned long long since_1601 =
119 ((unsigned long long) ft->dwHighDateTime << 32)
120 | (unsigned long long) ft->dwLowDateTime;
128 /* Between 1601-01-01 and 1970-01-01 there were 280 normal years and 89
129 leap years, in total 134774 days. */
130 unsigned long long since_1970 =
131 since_1601 - (unsigned long long) 134774 * (unsigned long long) 86400 * (unsigned long long) 10000000;
132 result.tv_sec = since_1970 / (unsigned long long) 10000000;
133 result.tv_nsec = (unsigned long) (since_1970 % (unsigned long long) 10000000) * 100;
139 _gl_convert_FILETIME_to_POSIX (const FILETIME *ft)
141 /* FILETIME: <https://docs.microsoft.com/en-us/windows/desktop/api/minwinbase/ns-minwinbase-filetime> */
142 unsigned long long since_1601 =
143 ((unsigned long long) ft->dwHighDateTime << 32)
144 | (unsigned long long) ft->dwLowDateTime;
149 /* Between 1601-01-01 and 1970-01-01 there were 280 normal years and 89
150 leap years, in total 134774 days. */
151 unsigned long long since_1970 =
152 since_1601 - (unsigned long long) 134774 * (unsigned long long) 86400 * (unsigned long long) 10000000;
153 return since_1970 / (unsigned long long) 10000000;
158 /* Fill *BUF with information about the file designated by H.
159 PATH is the file name, if known, otherwise NULL.
160 Return 0 if successful, or -1 with errno set upon failure. */
162 _gl_fstat_by_handle (HANDLE h, const char *path, struct stat *buf)
165 <https://docs.microsoft.com/en-us/windows/desktop/api/fileapi/nf-fileapi-getfiletype> */
166 DWORD type = GetFileType (h);
167 if (type == FILE_TYPE_DISK)
169 #if !WIN32_ASSUME_VISTA
174 /* st_mode can be determined through
176 <https://docs.microsoft.com/en-us/windows/desktop/api/fileapi/nf-fileapi-getfileattributesexa>
177 <https://docs.microsoft.com/en-us/windows/desktop/api/fileapi/ns-fileapi-_win32_file_attribute_data>
179 GetFileInformationByHandle
180 <https://docs.microsoft.com/en-us/windows/desktop/api/fileapi/nf-fileapi-getfileinformationbyhandle>
181 <https://docs.microsoft.com/en-us/windows/desktop/api/fileapi/ns-fileapi-_by_handle_file_information>
183 GetFileInformationByHandleEx with argument FileBasicInfo
184 <https://docs.microsoft.com/en-us/windows/desktop/api/winbase/nf-winbase-getfileinformationbyhandleex>
185 <https://docs.microsoft.com/en-us/windows/desktop/api/winbase/ns-winbase-_file_basic_info>
186 The latter requires -D_WIN32_WINNT=_WIN32_WINNT_VISTA or higher. */
187 BY_HANDLE_FILE_INFORMATION info;
188 if (! GetFileInformationByHandle (h, &info))
191 /* Test for error conditions before starting to fill *buf. */
192 if (sizeof (buf->st_size) <= 4 && info.nFileSizeHigh > 0)
198 #if _GL_WINDOWS_STAT_INODES
199 /* st_ino can be determined through
200 GetFileInformationByHandle
201 <https://docs.microsoft.com/en-us/windows/desktop/api/fileapi/nf-fileapi-getfileinformationbyhandle>
202 <https://docs.microsoft.com/en-us/windows/desktop/api/fileapi/ns-fileapi-_by_handle_file_information>
203 as 64 bits, or through
204 GetFileInformationByHandleEx with argument FileIdInfo
205 <https://docs.microsoft.com/en-us/windows/desktop/api/winbase/nf-winbase-getfileinformationbyhandleex>
206 <https://docs.microsoft.com/en-us/windows/desktop/api/winbase/ns-winbase-_file_id_info>
208 The latter requires -D_WIN32_WINNT=_WIN32_WINNT_WIN8 or higher. */
209 /* Experiments show that GetFileInformationByHandleEx does not provide
210 much more information than GetFileInformationByHandle:
211 * The dwVolumeSerialNumber from GetFileInformationByHandle is equal
212 to the low 32 bits of the 64-bit VolumeSerialNumber from
213 GetFileInformationByHandleEx, and is apparently sufficient for
214 identifying the device.
215 * The nFileIndex from GetFileInformationByHandle is equal to the low
216 64 bits of the 128-bit FileId from GetFileInformationByHandleEx,
217 and the high 64 bits of this 128-bit FileId are zero.
218 * On a FAT file system, GetFileInformationByHandleEx fails with error
219 ERROR_INVALID_PARAMETER, whereas GetFileInformationByHandle
221 * On a CIFS/SMB file system, GetFileInformationByHandleEx fails with
222 error ERROR_INVALID_LEVEL, whereas GetFileInformationByHandle
224 # if _GL_WINDOWS_STAT_INODES == 2
225 if (GetFileInformationByHandleExFunc != NULL)
228 if (GetFileInformationByHandleExFunc (h, FileIdInfo, &id, sizeof (id)))
230 buf->st_dev = id.VolumeSerialNumber;
231 verify (sizeof (ino_t) == sizeof (id.FileId));
232 memcpy (&buf->st_ino, &id.FileId, sizeof (ino_t));
237 switch (GetLastError ())
239 case ERROR_INVALID_PARAMETER: /* older Windows version, or FAT */
240 case ERROR_INVALID_LEVEL: /* CIFS/SMB file system */
248 /* Fallback for older Windows versions. */
249 buf->st_dev = info.dwVolumeSerialNumber;
250 buf->st_ino._gl_ino[0] = ((ULONGLONG) info.nFileIndexHigh << 32) | (ULONGLONG) info.nFileIndexLow;
251 buf->st_ino._gl_ino[1] = 0;
253 # else /* _GL_WINDOWS_STAT_INODES == 1 */
254 buf->st_dev = info.dwVolumeSerialNumber;
255 buf->st_ino = ((ULONGLONG) info.nFileIndexHigh << 32) | (ULONGLONG) info.nFileIndexLow;
258 /* st_ino is not wide enough for identifying a file on a device.
259 Without st_ino, st_dev is pointless. */
266 /* XXX How to handle FILE_ATTRIBUTE_REPARSE_POINT ? */
267 ((info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ? _S_IFDIR | S_IEXEC_UGO : _S_IFREG)
269 | ((info.dwFileAttributes & FILE_ATTRIBUTE_READONLY) ? 0 : S_IWRITE_UGO);
270 if (!(info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
272 /* Determine whether the file is executable by looking at the file
274 If the file name is already known, use it. Otherwise, for
275 non-empty files, it can be determined through
276 GetFinalPathNameByHandle
277 <https://docs.microsoft.com/en-us/windows/desktop/api/fileapi/nf-fileapi-getfinalpathnamebyhandlea>
279 GetFileInformationByHandleEx with argument FileNameInfo
280 <https://docs.microsoft.com/en-us/windows/desktop/api/winbase/nf-winbase-getfileinformationbyhandleex>
281 <https://docs.microsoft.com/en-us/windows/desktop/api/winbase/ns-winbase-_file_name_info>
282 Both require -D_WIN32_WINNT=_WIN32_WINNT_VISTA or higher. */
283 if (info.nFileSizeHigh > 0 || info.nFileSizeLow > 0)
285 char fpath[PATH_MAX];
287 || (GetFinalPathNameByHandleFunc != NULL
288 && GetFinalPathNameByHandleFunc (h, fpath, sizeof (fpath), VOLUME_NAME_NONE)
290 && (path = fpath, 1)))
292 const char *last_dot = NULL;
294 for (p = path; *p != '\0'; p++)
297 if (last_dot != NULL)
299 const char *suffix = last_dot + 1;
300 if (_stricmp (suffix, "exe") == 0
301 || _stricmp (suffix, "bat") == 0
302 || _stricmp (suffix, "cmd") == 0
303 || _stricmp (suffix, "com") == 0)
308 /* Cannot determine file name. Pretend that it is executable. */
314 /* st_nlink can be determined through
315 GetFileInformationByHandle
316 <https://docs.microsoft.com/en-us/windows/desktop/api/fileapi/nf-fileapi-getfileinformationbyhandle>
317 <https://docs.microsoft.com/en-us/windows/desktop/api/fileapi/ns-fileapi-_by_handle_file_information>
319 GetFileInformationByHandleEx with argument FileStandardInfo
320 <https://docs.microsoft.com/en-us/windows/desktop/api/winbase/nf-winbase-getfileinformationbyhandleex>
321 <https://docs.microsoft.com/en-us/windows/desktop/api/winbase/ns-winbase-_file_standard_info>
322 The latter requires -D_WIN32_WINNT=_WIN32_WINNT_VISTA or higher. */
323 buf->st_nlink = (info.nNumberOfLinks > SHRT_MAX ? SHRT_MAX : info.nNumberOfLinks);
325 /* There's no easy way to map the Windows SID concept to an integer. */
329 /* st_rdev is irrelevant for normal files and directories. */
332 /* st_size can be determined through
334 <https://docs.microsoft.com/en-us/windows/desktop/api/fileapi/nf-fileapi-getfilesizeex>
337 <https://docs.microsoft.com/en-us/windows/desktop/api/fileapi/nf-fileapi-getfileattributesexa>
338 <https://docs.microsoft.com/en-us/windows/desktop/api/fileapi/ns-fileapi-_win32_file_attribute_data>
340 GetFileInformationByHandle
341 <https://docs.microsoft.com/en-us/windows/desktop/api/fileapi/nf-fileapi-getfileinformationbyhandle>
342 <https://docs.microsoft.com/en-us/windows/desktop/api/fileapi/ns-fileapi-_by_handle_file_information>
344 GetFileInformationByHandleEx with argument FileStandardInfo
345 <https://docs.microsoft.com/en-us/windows/desktop/api/winbase/nf-winbase-getfileinformationbyhandleex>
346 <https://docs.microsoft.com/en-us/windows/desktop/api/winbase/ns-winbase-_file_standard_info>
347 The latter requires -D_WIN32_WINNT=_WIN32_WINNT_VISTA or higher. */
348 if (sizeof (buf->st_size) <= 4)
349 /* Range check already done above. */
350 buf->st_size = info.nFileSizeLow;
352 buf->st_size = ((long long) info.nFileSizeHigh << 32) | (long long) info.nFileSizeLow;
354 /* st_atime, st_mtime, st_ctime can be determined through
356 <https://docs.microsoft.com/en-us/windows/desktop/api/fileapi/nf-fileapi-getfiletime>
359 <https://docs.microsoft.com/en-us/windows/desktop/api/fileapi/nf-fileapi-getfileattributesexa>
360 <https://docs.microsoft.com/en-us/windows/desktop/api/fileapi/ns-fileapi-_win32_file_attribute_data>
362 GetFileInformationByHandle
363 <https://docs.microsoft.com/en-us/windows/desktop/api/fileapi/nf-fileapi-getfileinformationbyhandle>
364 <https://docs.microsoft.com/en-us/windows/desktop/api/fileapi/ns-fileapi-_by_handle_file_information>
366 GetFileInformationByHandleEx with argument FileBasicInfo
367 <https://docs.microsoft.com/en-us/windows/desktop/api/winbase/nf-winbase-getfileinformationbyhandleex>
368 <https://docs.microsoft.com/en-us/windows/desktop/api/winbase/ns-winbase-_file_basic_info>
369 The latter requires -D_WIN32_WINNT=_WIN32_WINNT_VISTA or higher. */
370 #if _GL_WINDOWS_STAT_TIMESPEC
371 buf->st_atim = _gl_convert_FILETIME_to_timespec (&info.ftLastAccessTime);
372 buf->st_mtim = _gl_convert_FILETIME_to_timespec (&info.ftLastWriteTime);
373 buf->st_ctim = _gl_convert_FILETIME_to_timespec (&info.ftCreationTime);
375 buf->st_atime = _gl_convert_FILETIME_to_POSIX (&info.ftLastAccessTime);
376 buf->st_mtime = _gl_convert_FILETIME_to_POSIX (&info.ftLastWriteTime);
377 buf->st_ctime = _gl_convert_FILETIME_to_POSIX (&info.ftCreationTime);
382 else if (type == FILE_TYPE_CHAR || type == FILE_TYPE_PIPE)
385 #if _GL_WINDOWS_STAT_INODES == 2
386 buf->st_ino._gl_ino[0] = buf->st_ino._gl_ino[1] = 0;
390 buf->st_mode = (type == FILE_TYPE_PIPE ? _S_IFIFO : _S_IFCHR);
395 if (type == FILE_TYPE_PIPE)
398 <https://msdn.microsoft.com/en-us/library/aa365779.aspx> */
399 DWORD bytes_available;
400 if (PeekNamedPipe (h, NULL, 0, NULL, &bytes_available, NULL))
401 buf->st_size = bytes_available;
407 #if _GL_WINDOWS_STAT_TIMESPEC
408 buf->st_atim.tv_sec = 0; buf->st_atim.tv_nsec = 0;
409 buf->st_mtim.tv_sec = 0; buf->st_mtim.tv_nsec = 0;
410 buf->st_ctim.tv_sec = 0; buf->st_ctim.tv_nsec = 0;
426 DWORD error = GetLastError ();
428 fprintf (stderr, "_gl_fstat_by_handle error 0x%x\n", (unsigned int) error);
432 case ERROR_ACCESS_DENIED:
433 case ERROR_SHARING_VIOLATION:
437 case ERROR_OUTOFMEMORY:
441 case ERROR_WRITE_FAULT:
442 case ERROR_READ_FAULT:
443 case ERROR_GEN_FAILURE:
457 /* This declaration is solely to ensure that after preprocessing
458 this file is never empty. */