stat-w32.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  1. /* Core of implementation of fstat and stat for native Windows.
  2. Copyright (C) 2017-2023 Free Software Foundation, Inc.
  3. This file is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU Lesser General Public License as
  5. published by the Free Software Foundation; either version 2.1 of the
  6. License, or (at your option) any later version.
  7. This file is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public License
  12. along with this program. If not, see <https://www.gnu.org/licenses/>. */
  13. /* Written by Bruno Haible. */
  14. #include <config.h>
  15. #if defined _WIN32 && ! defined __CYGWIN__
  16. /* Attempt to make <windows.h> define FILE_ID_INFO.
  17. But ensure that the redefinition of _WIN32_WINNT does not make us assume
  18. Windows Vista or newer when building for an older version of Windows. */
  19. #if HAVE_SDKDDKVER_H
  20. # include <sdkddkver.h>
  21. # if _WIN32_WINNT >= _WIN32_WINNT_VISTA
  22. # define WIN32_ASSUME_VISTA 1
  23. # else
  24. # define WIN32_ASSUME_VISTA 0
  25. # endif
  26. # if !defined _WIN32_WINNT || (_WIN32_WINNT < _WIN32_WINNT_WIN8)
  27. # undef _WIN32_WINNT
  28. # define _WIN32_WINNT _WIN32_WINNT_WIN8
  29. # endif
  30. #else
  31. # define WIN32_ASSUME_VISTA (_WIN32_WINNT >= _WIN32_WINNT_VISTA)
  32. #endif
  33. #include <sys/types.h>
  34. #include <sys/stat.h>
  35. #include <errno.h>
  36. #include <limits.h>
  37. #include <string.h>
  38. #include <unistd.h>
  39. #include <windows.h>
  40. /* Specification. */
  41. #include "stat-w32.h"
  42. #include "pathmax.h"
  43. /* Don't assume that UNICODE is not defined. */
  44. #undef LoadLibrary
  45. #define LoadLibrary LoadLibraryA
  46. #undef GetFinalPathNameByHandle
  47. #define GetFinalPathNameByHandle GetFinalPathNameByHandleA
  48. /* Older mingw headers do not define VOLUME_NAME_NONE. */
  49. #ifndef VOLUME_NAME_NONE
  50. # define VOLUME_NAME_NONE 4
  51. #endif
  52. #if !WIN32_ASSUME_VISTA
  53. /* Avoid warnings from gcc -Wcast-function-type. */
  54. # define GetProcAddress \
  55. (void *) GetProcAddress
  56. # if _GL_WINDOWS_STAT_INODES == 2
  57. /* GetFileInformationByHandleEx was introduced only in Windows Vista. */
  58. typedef DWORD (WINAPI * GetFileInformationByHandleExFuncType) (HANDLE hFile,
  59. FILE_INFO_BY_HANDLE_CLASS fiClass,
  60. LPVOID lpBuffer,
  61. DWORD dwBufferSize);
  62. static GetFileInformationByHandleExFuncType GetFileInformationByHandleExFunc = NULL;
  63. # endif
  64. /* GetFinalPathNameByHandle was introduced only in Windows Vista. */
  65. typedef DWORD (WINAPI * GetFinalPathNameByHandleFuncType) (HANDLE hFile,
  66. LPSTR lpFilePath,
  67. DWORD lenFilePath,
  68. DWORD dwFlags);
  69. static GetFinalPathNameByHandleFuncType GetFinalPathNameByHandleFunc = NULL;
  70. static BOOL initialized = FALSE;
  71. static void
  72. initialize (void)
  73. {
  74. HMODULE kernel32 = LoadLibrary ("kernel32.dll");
  75. if (kernel32 != NULL)
  76. {
  77. # if _GL_WINDOWS_STAT_INODES == 2
  78. GetFileInformationByHandleExFunc =
  79. (GetFileInformationByHandleExFuncType) GetProcAddress (kernel32, "GetFileInformationByHandleEx");
  80. # endif
  81. GetFinalPathNameByHandleFunc =
  82. (GetFinalPathNameByHandleFuncType) GetProcAddress (kernel32, "GetFinalPathNameByHandleA");
  83. }
  84. initialized = TRUE;
  85. }
  86. #else
  87. # define GetFileInformationByHandleExFunc GetFileInformationByHandleEx
  88. # define GetFinalPathNameByHandleFunc GetFinalPathNameByHandle
  89. #endif
  90. /* Converts a FILETIME to GMT time since 1970-01-01 00:00:00. */
  91. #if _GL_WINDOWS_STAT_TIMESPEC
  92. struct timespec
  93. _gl_convert_FILETIME_to_timespec (const FILETIME *ft)
  94. {
  95. struct timespec result;
  96. /* FILETIME: <https://docs.microsoft.com/en-us/windows/desktop/api/minwinbase/ns-minwinbase-filetime> */
  97. unsigned long long since_1601 =
  98. ((unsigned long long) ft->dwHighDateTime << 32)
  99. | (unsigned long long) ft->dwLowDateTime;
  100. if (since_1601 == 0)
  101. {
  102. result.tv_sec = 0;
  103. result.tv_nsec = 0;
  104. }
  105. else
  106. {
  107. /* Between 1601-01-01 and 1970-01-01 there were 280 normal years and 89
  108. leap years, in total 134774 days. */
  109. unsigned long long since_1970 =
  110. since_1601 - (unsigned long long) 134774 * (unsigned long long) 86400 * (unsigned long long) 10000000;
  111. result.tv_sec = since_1970 / (unsigned long long) 10000000;
  112. result.tv_nsec = (unsigned long) (since_1970 % (unsigned long long) 10000000) * 100;
  113. }
  114. return result;
  115. }
  116. #else
  117. time_t
  118. _gl_convert_FILETIME_to_POSIX (const FILETIME *ft)
  119. {
  120. /* FILETIME: <https://docs.microsoft.com/en-us/windows/desktop/api/minwinbase/ns-minwinbase-filetime> */
  121. unsigned long long since_1601 =
  122. ((unsigned long long) ft->dwHighDateTime << 32)
  123. | (unsigned long long) ft->dwLowDateTime;
  124. if (since_1601 == 0)
  125. return 0;
  126. else
  127. {
  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. return since_1970 / (unsigned long long) 10000000;
  133. }
  134. }
  135. #endif
  136. /* Fill *BUF with information about the file designated by H.
  137. PATH is the file name, if known, otherwise NULL.
  138. Return 0 if successful, or -1 with errno set upon failure. */
  139. int
  140. _gl_fstat_by_handle (HANDLE h, const char *path, struct stat *buf)
  141. {
  142. /* GetFileType
  143. <https://docs.microsoft.com/en-us/windows/desktop/api/fileapi/nf-fileapi-getfiletype> */
  144. DWORD type = GetFileType (h);
  145. if (type == FILE_TYPE_DISK)
  146. {
  147. #if !WIN32_ASSUME_VISTA
  148. if (!initialized)
  149. initialize ();
  150. #endif
  151. /* st_mode can be determined through
  152. GetFileAttributesEx
  153. <https://docs.microsoft.com/en-us/windows/desktop/api/fileapi/nf-fileapi-getfileattributesexa>
  154. <https://docs.microsoft.com/en-us/windows/desktop/api/fileapi/ns-fileapi-_win32_file_attribute_data>
  155. or through
  156. GetFileInformationByHandle
  157. <https://docs.microsoft.com/en-us/windows/desktop/api/fileapi/nf-fileapi-getfileinformationbyhandle>
  158. <https://docs.microsoft.com/en-us/windows/desktop/api/fileapi/ns-fileapi-_by_handle_file_information>
  159. or through
  160. GetFileInformationByHandleEx with argument FileBasicInfo
  161. <https://docs.microsoft.com/en-us/windows/desktop/api/winbase/nf-winbase-getfileinformationbyhandleex>
  162. <https://docs.microsoft.com/en-us/windows/desktop/api/winbase/ns-winbase-_file_basic_info>
  163. The latter requires -D_WIN32_WINNT=_WIN32_WINNT_VISTA or higher. */
  164. BY_HANDLE_FILE_INFORMATION info;
  165. if (! GetFileInformationByHandle (h, &info))
  166. goto failed;
  167. /* Test for error conditions before starting to fill *buf. */
  168. if (sizeof (buf->st_size) <= 4 && info.nFileSizeHigh > 0)
  169. {
  170. errno = EOVERFLOW;
  171. return -1;
  172. }
  173. #if _GL_WINDOWS_STAT_INODES
  174. /* st_ino can be determined through
  175. GetFileInformationByHandle
  176. <https://docs.microsoft.com/en-us/windows/desktop/api/fileapi/nf-fileapi-getfileinformationbyhandle>
  177. <https://docs.microsoft.com/en-us/windows/desktop/api/fileapi/ns-fileapi-_by_handle_file_information>
  178. as 64 bits, or through
  179. GetFileInformationByHandleEx with argument FileIdInfo
  180. <https://docs.microsoft.com/en-us/windows/desktop/api/winbase/nf-winbase-getfileinformationbyhandleex>
  181. <https://docs.microsoft.com/en-us/windows/desktop/api/winbase/ns-winbase-_file_id_info>
  182. as 128 bits.
  183. The latter requires -D_WIN32_WINNT=_WIN32_WINNT_WIN8 or higher. */
  184. /* Experiments show that GetFileInformationByHandleEx does not provide
  185. much more information than GetFileInformationByHandle:
  186. * The dwVolumeSerialNumber from GetFileInformationByHandle is equal
  187. to the low 32 bits of the 64-bit VolumeSerialNumber from
  188. GetFileInformationByHandleEx, and is apparently sufficient for
  189. identifying the device.
  190. * The nFileIndex from GetFileInformationByHandle is equal to the low
  191. 64 bits of the 128-bit FileId from GetFileInformationByHandleEx,
  192. and the high 64 bits of this 128-bit FileId are zero.
  193. * On a FAT file system, GetFileInformationByHandleEx fails with error
  194. ERROR_INVALID_PARAMETER, whereas GetFileInformationByHandle
  195. succeeds.
  196. * On a CIFS/SMB file system, GetFileInformationByHandleEx fails with
  197. error ERROR_INVALID_LEVEL, whereas GetFileInformationByHandle
  198. succeeds. */
  199. # if _GL_WINDOWS_STAT_INODES == 2
  200. if (GetFileInformationByHandleExFunc != NULL)
  201. {
  202. FILE_ID_INFO id;
  203. if (GetFileInformationByHandleExFunc (h, FileIdInfo, &id, sizeof (id)))
  204. {
  205. buf->st_dev = id.VolumeSerialNumber;
  206. static_assert (sizeof (ino_t) == sizeof (id.FileId));
  207. memcpy (&buf->st_ino, &id.FileId, sizeof (ino_t));
  208. goto ino_done;
  209. }
  210. else
  211. {
  212. switch (GetLastError ())
  213. {
  214. case ERROR_INVALID_PARAMETER: /* older Windows version, or FAT */
  215. case ERROR_INVALID_LEVEL: /* CIFS/SMB file system */
  216. goto fallback;
  217. default:
  218. goto failed;
  219. }
  220. }
  221. }
  222. fallback: ;
  223. /* Fallback for older Windows versions. */
  224. buf->st_dev = info.dwVolumeSerialNumber;
  225. buf->st_ino._gl_ino[0] = ((ULONGLONG) info.nFileIndexHigh << 32) | (ULONGLONG) info.nFileIndexLow;
  226. buf->st_ino._gl_ino[1] = 0;
  227. ino_done: ;
  228. # else /* _GL_WINDOWS_STAT_INODES == 1 */
  229. buf->st_dev = info.dwVolumeSerialNumber;
  230. buf->st_ino = ((ULONGLONG) info.nFileIndexHigh << 32) | (ULONGLONG) info.nFileIndexLow;
  231. # endif
  232. #else
  233. /* st_ino is not wide enough for identifying a file on a device.
  234. Without st_ino, st_dev is pointless. */
  235. buf->st_dev = 0;
  236. buf->st_ino = 0;
  237. #endif
  238. /* st_mode. */
  239. unsigned int mode =
  240. /* XXX How to handle FILE_ATTRIBUTE_REPARSE_POINT ? */
  241. ((info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ? _S_IFDIR | S_IEXEC_UGO : _S_IFREG)
  242. | S_IREAD_UGO
  243. | ((info.dwFileAttributes & FILE_ATTRIBUTE_READONLY) ? 0 : S_IWRITE_UGO);
  244. if (!(info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
  245. {
  246. /* Determine whether the file is executable by looking at the file
  247. name suffix.
  248. If the file name is already known, use it. Otherwise, for
  249. non-empty files, it can be determined through
  250. GetFinalPathNameByHandle
  251. <https://docs.microsoft.com/en-us/windows/desktop/api/fileapi/nf-fileapi-getfinalpathnamebyhandlea>
  252. or through
  253. GetFileInformationByHandleEx with argument FileNameInfo
  254. <https://docs.microsoft.com/en-us/windows/desktop/api/winbase/nf-winbase-getfileinformationbyhandleex>
  255. <https://docs.microsoft.com/en-us/windows/desktop/api/winbase/ns-winbase-_file_name_info>
  256. Both require -D_WIN32_WINNT=_WIN32_WINNT_VISTA or higher. */
  257. if (info.nFileSizeHigh > 0 || info.nFileSizeLow > 0)
  258. {
  259. char fpath[PATH_MAX];
  260. if (path != NULL
  261. || (GetFinalPathNameByHandleFunc != NULL
  262. && GetFinalPathNameByHandleFunc (h, fpath, sizeof (fpath), VOLUME_NAME_NONE)
  263. < sizeof (fpath)
  264. && (path = fpath, 1)))
  265. {
  266. const char *last_dot = NULL;
  267. const char *p;
  268. for (p = path; *p != '\0'; p++)
  269. if (*p == '.')
  270. last_dot = p;
  271. if (last_dot != NULL)
  272. {
  273. const char *suffix = last_dot + 1;
  274. if (_stricmp (suffix, "exe") == 0
  275. || _stricmp (suffix, "bat") == 0
  276. || _stricmp (suffix, "cmd") == 0
  277. || _stricmp (suffix, "com") == 0)
  278. mode |= S_IEXEC_UGO;
  279. }
  280. }
  281. else
  282. /* Cannot determine file name. Pretend that it is executable. */
  283. mode |= S_IEXEC_UGO;
  284. }
  285. }
  286. buf->st_mode = mode;
  287. /* st_nlink can be determined through
  288. GetFileInformationByHandle
  289. <https://docs.microsoft.com/en-us/windows/desktop/api/fileapi/nf-fileapi-getfileinformationbyhandle>
  290. <https://docs.microsoft.com/en-us/windows/desktop/api/fileapi/ns-fileapi-_by_handle_file_information>
  291. or through
  292. GetFileInformationByHandleEx with argument FileStandardInfo
  293. <https://docs.microsoft.com/en-us/windows/desktop/api/winbase/nf-winbase-getfileinformationbyhandleex>
  294. <https://docs.microsoft.com/en-us/windows/desktop/api/winbase/ns-winbase-_file_standard_info>
  295. The latter requires -D_WIN32_WINNT=_WIN32_WINNT_VISTA or higher. */
  296. buf->st_nlink = (info.nNumberOfLinks > SHRT_MAX ? SHRT_MAX : info.nNumberOfLinks);
  297. /* There's no easy way to map the Windows SID concept to an integer. */
  298. buf->st_uid = 0;
  299. buf->st_gid = 0;
  300. /* st_rdev is irrelevant for normal files and directories. */
  301. buf->st_rdev = 0;
  302. /* st_size can be determined through
  303. GetFileSizeEx
  304. <https://docs.microsoft.com/en-us/windows/desktop/api/fileapi/nf-fileapi-getfilesizeex>
  305. or through
  306. GetFileAttributesEx
  307. <https://docs.microsoft.com/en-us/windows/desktop/api/fileapi/nf-fileapi-getfileattributesexa>
  308. <https://docs.microsoft.com/en-us/windows/desktop/api/fileapi/ns-fileapi-_win32_file_attribute_data>
  309. or through
  310. GetFileInformationByHandle
  311. <https://docs.microsoft.com/en-us/windows/desktop/api/fileapi/nf-fileapi-getfileinformationbyhandle>
  312. <https://docs.microsoft.com/en-us/windows/desktop/api/fileapi/ns-fileapi-_by_handle_file_information>
  313. or through
  314. GetFileInformationByHandleEx with argument FileStandardInfo
  315. <https://docs.microsoft.com/en-us/windows/desktop/api/winbase/nf-winbase-getfileinformationbyhandleex>
  316. <https://docs.microsoft.com/en-us/windows/desktop/api/winbase/ns-winbase-_file_standard_info>
  317. The latter requires -D_WIN32_WINNT=_WIN32_WINNT_VISTA or higher. */
  318. if (sizeof (buf->st_size) <= 4)
  319. /* Range check already done above. */
  320. buf->st_size = info.nFileSizeLow;
  321. else
  322. buf->st_size = ((long long) info.nFileSizeHigh << 32) | (long long) info.nFileSizeLow;
  323. /* st_atime, st_mtime, st_ctime can be determined through
  324. GetFileTime
  325. <https://docs.microsoft.com/en-us/windows/desktop/api/fileapi/nf-fileapi-getfiletime>
  326. or through
  327. GetFileAttributesEx
  328. <https://docs.microsoft.com/en-us/windows/desktop/api/fileapi/nf-fileapi-getfileattributesexa>
  329. <https://docs.microsoft.com/en-us/windows/desktop/api/fileapi/ns-fileapi-_win32_file_attribute_data>
  330. or through
  331. GetFileInformationByHandle
  332. <https://docs.microsoft.com/en-us/windows/desktop/api/fileapi/nf-fileapi-getfileinformationbyhandle>
  333. <https://docs.microsoft.com/en-us/windows/desktop/api/fileapi/ns-fileapi-_by_handle_file_information>
  334. or through
  335. GetFileInformationByHandleEx with argument FileBasicInfo
  336. <https://docs.microsoft.com/en-us/windows/desktop/api/winbase/nf-winbase-getfileinformationbyhandleex>
  337. <https://docs.microsoft.com/en-us/windows/desktop/api/winbase/ns-winbase-_file_basic_info>
  338. The latter requires -D_WIN32_WINNT=_WIN32_WINNT_VISTA or higher. */
  339. #if _GL_WINDOWS_STAT_TIMESPEC
  340. buf->st_atim = _gl_convert_FILETIME_to_timespec (&info.ftLastAccessTime);
  341. buf->st_mtim = _gl_convert_FILETIME_to_timespec (&info.ftLastWriteTime);
  342. buf->st_ctim = _gl_convert_FILETIME_to_timespec (&info.ftCreationTime);
  343. #else
  344. buf->st_atime = _gl_convert_FILETIME_to_POSIX (&info.ftLastAccessTime);
  345. buf->st_mtime = _gl_convert_FILETIME_to_POSIX (&info.ftLastWriteTime);
  346. buf->st_ctime = _gl_convert_FILETIME_to_POSIX (&info.ftCreationTime);
  347. #endif
  348. return 0;
  349. }
  350. else if (type == FILE_TYPE_CHAR || type == FILE_TYPE_PIPE)
  351. {
  352. buf->st_dev = 0;
  353. #if _GL_WINDOWS_STAT_INODES == 2
  354. buf->st_ino._gl_ino[0] = buf->st_ino._gl_ino[1] = 0;
  355. #else
  356. buf->st_ino = 0;
  357. #endif
  358. buf->st_mode = (type == FILE_TYPE_PIPE ? _S_IFIFO : _S_IFCHR);
  359. buf->st_nlink = 1;
  360. buf->st_uid = 0;
  361. buf->st_gid = 0;
  362. buf->st_rdev = 0;
  363. if (type == FILE_TYPE_PIPE)
  364. {
  365. /* PeekNamedPipe
  366. <https://msdn.microsoft.com/en-us/library/aa365779.aspx> */
  367. DWORD bytes_available;
  368. if (PeekNamedPipe (h, NULL, 0, NULL, &bytes_available, NULL))
  369. buf->st_size = bytes_available;
  370. else
  371. buf->st_size = 0;
  372. }
  373. else
  374. buf->st_size = 0;
  375. #if _GL_WINDOWS_STAT_TIMESPEC
  376. buf->st_atim.tv_sec = 0; buf->st_atim.tv_nsec = 0;
  377. buf->st_mtim.tv_sec = 0; buf->st_mtim.tv_nsec = 0;
  378. buf->st_ctim.tv_sec = 0; buf->st_ctim.tv_nsec = 0;
  379. #else
  380. buf->st_atime = 0;
  381. buf->st_mtime = 0;
  382. buf->st_ctime = 0;
  383. #endif
  384. return 0;
  385. }
  386. else
  387. {
  388. errno = ENOENT;
  389. return -1;
  390. }
  391. failed:
  392. {
  393. DWORD error = GetLastError ();
  394. #if 0
  395. fprintf (stderr, "_gl_fstat_by_handle error 0x%x\n", (unsigned int) error);
  396. #endif
  397. switch (error)
  398. {
  399. case ERROR_ACCESS_DENIED:
  400. case ERROR_SHARING_VIOLATION:
  401. errno = EACCES;
  402. break;
  403. case ERROR_OUTOFMEMORY:
  404. errno = ENOMEM;
  405. break;
  406. case ERROR_WRITE_FAULT:
  407. case ERROR_READ_FAULT:
  408. case ERROR_GEN_FAILURE:
  409. errno = EIO;
  410. break;
  411. default:
  412. errno = EINVAL;
  413. break;
  414. }
  415. return -1;
  416. }
  417. }
  418. #else
  419. /* This declaration is solely to ensure that after preprocessing
  420. this file is never empty. */
  421. typedef int dummy;
  422. #endif