1 1.1 christos /* Work around platform bugs in stat. 2 1.1.1.2 christos Copyright (C) 2009-2022 Free Software Foundation, Inc. 3 1.1 christos 4 1.1.1.2 christos This file is free software: you can redistribute it and/or modify 5 1.1.1.2 christos it under the terms of the GNU Lesser General Public License as 6 1.1.1.2 christos published by the Free Software Foundation; either version 2.1 of the 7 1.1.1.2 christos License, or (at your option) any later version. 8 1.1 christos 9 1.1.1.2 christos This file is distributed in the hope that it will be useful, 10 1.1 christos but WITHOUT ANY WARRANTY; without even the implied warranty of 11 1.1 christos MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 1.1.1.2 christos GNU Lesser General Public License for more details. 13 1.1 christos 14 1.1.1.2 christos You should have received a copy of the GNU Lesser General Public License 15 1.1 christos along with this program. If not, see <https://www.gnu.org/licenses/>. */ 16 1.1 christos 17 1.1 christos /* Written by Eric Blake and Bruno Haible. */ 18 1.1 christos 19 1.1 christos /* If the user's config.h happens to include <sys/stat.h>, let it include only 20 1.1 christos the system's <sys/stat.h> here, so that orig_stat doesn't recurse to 21 1.1 christos rpl_stat. */ 22 1.1 christos #define __need_system_sys_stat_h 23 1.1 christos #include <config.h> 24 1.1 christos 25 1.1 christos /* Get the original definition of stat. It might be defined as a macro. */ 26 1.1 christos #include <sys/types.h> 27 1.1 christos #include <sys/stat.h> 28 1.1 christos #undef __need_system_sys_stat_h 29 1.1 christos 30 1.1 christos #if defined _WIN32 && ! defined __CYGWIN__ 31 1.1 christos # define WINDOWS_NATIVE 32 1.1 christos #endif 33 1.1 christos 34 1.1 christos #if !defined WINDOWS_NATIVE 35 1.1 christos 36 1.1 christos static int 37 1.1 christos orig_stat (const char *filename, struct stat *buf) 38 1.1 christos { 39 1.1 christos return stat (filename, buf); 40 1.1 christos } 41 1.1 christos 42 1.1 christos #endif 43 1.1 christos 44 1.1 christos /* Specification. */ 45 1.1 christos #ifdef __osf__ 46 1.1 christos /* Write "sys/stat.h" here, not <sys/stat.h>, otherwise OSF/1 5.1 DTK cc 47 1.1 christos eliminates this include because of the preliminary #include <sys/stat.h> 48 1.1 christos above. */ 49 1.1 christos # include "sys/stat.h" 50 1.1 christos #else 51 1.1 christos # include <sys/stat.h> 52 1.1 christos #endif 53 1.1 christos 54 1.1 christos #include "stat-time.h" 55 1.1 christos 56 1.1 christos #include <errno.h> 57 1.1 christos #include <limits.h> 58 1.1 christos #include <stdbool.h> 59 1.1 christos #include <string.h> 60 1.1 christos #include "filename.h" 61 1.1 christos #include "malloca.h" 62 1.1 christos #include "verify.h" 63 1.1 christos 64 1.1 christos #ifdef WINDOWS_NATIVE 65 1.1 christos # define WIN32_LEAN_AND_MEAN 66 1.1 christos # include <windows.h> 67 1.1 christos # include "stat-w32.h" 68 1.1 christos /* Don't assume that UNICODE is not defined. */ 69 1.1 christos # undef WIN32_FIND_DATA 70 1.1 christos # define WIN32_FIND_DATA WIN32_FIND_DATAA 71 1.1 christos # undef CreateFile 72 1.1 christos # define CreateFile CreateFileA 73 1.1 christos # undef FindFirstFile 74 1.1 christos # define FindFirstFile FindFirstFileA 75 1.1 christos #endif 76 1.1 christos 77 1.1 christos #ifdef WINDOWS_NATIVE 78 1.1 christos /* Return TRUE if the given file name denotes an UNC root. */ 79 1.1 christos static BOOL 80 1.1 christos is_unc_root (const char *rname) 81 1.1 christos { 82 1.1 christos /* Test whether it has the syntax '\\server\share'. */ 83 1.1 christos if (ISSLASH (rname[0]) && ISSLASH (rname[1])) 84 1.1 christos { 85 1.1 christos /* It starts with two slashes. Find the next slash. */ 86 1.1 christos const char *p = rname + 2; 87 1.1 christos const char *q = p; 88 1.1 christos while (*q != '\0' && !ISSLASH (*q)) 89 1.1 christos q++; 90 1.1 christos if (q > p && *q != '\0') 91 1.1 christos { 92 1.1 christos /* Found the next slash at q. */ 93 1.1 christos q++; 94 1.1 christos const char *r = q; 95 1.1 christos while (*r != '\0' && !ISSLASH (*r)) 96 1.1 christos r++; 97 1.1 christos if (r > q && *r == '\0') 98 1.1 christos return TRUE; 99 1.1 christos } 100 1.1 christos } 101 1.1 christos return FALSE; 102 1.1 christos } 103 1.1 christos #endif 104 1.1 christos 105 1.1 christos /* Store information about NAME into ST. Work around bugs with 106 1.1 christos trailing slashes. Mingw has other bugs (such as st_ino always 107 1.1 christos being 0 on success) which this wrapper does not work around. But 108 1.1 christos at least this implementation provides the ability to emulate fchdir 109 1.1 christos correctly. */ 110 1.1 christos 111 1.1 christos int 112 1.1 christos rpl_stat (char const *name, struct stat *buf) 113 1.1 christos { 114 1.1 christos #ifdef WINDOWS_NATIVE 115 1.1 christos /* Fill the fields ourselves, because the original stat function returns 116 1.1 christos values for st_atime, st_mtime, st_ctime that depend on the current time 117 1.1 christos zone. See 118 1.1 christos <https://lists.gnu.org/r/bug-gnulib/2017-04/msg00134.html> */ 119 1.1 christos /* XXX Should we convert to wchar_t* and prepend '\\?\', in order to work 120 1.1 christos around length limitations 121 1.1 christos <https://docs.microsoft.com/en-us/windows/desktop/FileIO/naming-a-file> ? */ 122 1.1 christos 123 1.1 christos /* POSIX <https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap04.html#tag_04_13> 124 1.1 christos specifies: "More than two leading <slash> characters shall be treated as 125 1.1 christos a single <slash> character." */ 126 1.1 christos if (ISSLASH (name[0]) && ISSLASH (name[1]) && ISSLASH (name[2])) 127 1.1 christos { 128 1.1 christos name += 2; 129 1.1 christos while (ISSLASH (name[1])) 130 1.1 christos name++; 131 1.1 christos } 132 1.1 christos 133 1.1 christos size_t len = strlen (name); 134 1.1 christos size_t drive_prefix_len = (HAS_DEVICE (name) ? 2 : 0); 135 1.1 christos 136 1.1 christos /* Remove trailing slashes (except the very first one, at position 137 1.1 christos drive_prefix_len), but remember their presence. */ 138 1.1 christos size_t rlen; 139 1.1 christos bool check_dir = false; 140 1.1 christos 141 1.1 christos rlen = len; 142 1.1 christos while (rlen > drive_prefix_len && ISSLASH (name[rlen-1])) 143 1.1 christos { 144 1.1 christos check_dir = true; 145 1.1 christos if (rlen == drive_prefix_len + 1) 146 1.1 christos break; 147 1.1 christos rlen--; 148 1.1 christos } 149 1.1 christos 150 1.1 christos /* Handle '' and 'C:'. */ 151 1.1 christos if (!check_dir && rlen == drive_prefix_len) 152 1.1 christos { 153 1.1 christos errno = ENOENT; 154 1.1 christos return -1; 155 1.1 christos } 156 1.1 christos 157 1.1 christos /* Handle '\\'. */ 158 1.1 christos if (rlen == 1 && ISSLASH (name[0]) && len >= 2) 159 1.1 christos { 160 1.1 christos errno = ENOENT; 161 1.1 christos return -1; 162 1.1 christos } 163 1.1 christos 164 1.1 christos const char *rname; 165 1.1 christos char *malloca_rname; 166 1.1 christos if (rlen == len) 167 1.1 christos { 168 1.1 christos rname = name; 169 1.1 christos malloca_rname = NULL; 170 1.1 christos } 171 1.1 christos else 172 1.1 christos { 173 1.1 christos malloca_rname = malloca (rlen + 1); 174 1.1 christos if (malloca_rname == NULL) 175 1.1 christos { 176 1.1 christos errno = ENOMEM; 177 1.1 christos return -1; 178 1.1 christos } 179 1.1 christos memcpy (malloca_rname, name, rlen); 180 1.1 christos malloca_rname[rlen] = '\0'; 181 1.1 christos rname = malloca_rname; 182 1.1 christos } 183 1.1 christos 184 1.1 christos /* There are two ways to get at the requested information: 185 1.1 christos - by scanning the parent directory and examining the relevant 186 1.1 christos directory entry, 187 1.1 christos - by opening the file directly. 188 1.1 christos The first approach fails for root directories (e.g. 'C:\') and 189 1.1 christos UNC root directories (e.g. '\\server\share'). 190 1.1 christos The second approach fails for some system files (e.g. 'C:\pagefile.sys' 191 1.1 christos and 'C:\hiberfil.sys'): ERROR_SHARING_VIOLATION. 192 1.1 christos The second approach gives more information (in particular, correct 193 1.1 christos st_dev, st_ino, st_nlink fields). 194 1.1 christos So we use the second approach and, as a fallback except for root and 195 1.1 christos UNC root directories, also the first approach. */ 196 1.1 christos { 197 1.1 christos int ret; 198 1.1 christos 199 1.1 christos { 200 1.1 christos /* Approach based on the file. */ 201 1.1 christos 202 1.1 christos /* Open a handle to the file. 203 1.1 christos CreateFile 204 1.1 christos <https://docs.microsoft.com/en-us/windows/desktop/api/fileapi/nf-fileapi-createfilea> 205 1.1 christos <https://docs.microsoft.com/en-us/windows/desktop/FileIO/creating-and-opening-files> */ 206 1.1 christos HANDLE h = 207 1.1 christos CreateFile (rname, 208 1.1 christos FILE_READ_ATTRIBUTES, 209 1.1 christos FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, 210 1.1 christos NULL, 211 1.1 christos OPEN_EXISTING, 212 1.1 christos /* FILE_FLAG_POSIX_SEMANTICS (treat file names that differ only 213 1.1 christos in case as different) makes sense only when applied to *all* 214 1.1 christos filesystem operations. */ 215 1.1 christos FILE_FLAG_BACKUP_SEMANTICS /* | FILE_FLAG_POSIX_SEMANTICS */, 216 1.1 christos NULL); 217 1.1 christos if (h != INVALID_HANDLE_VALUE) 218 1.1 christos { 219 1.1 christos ret = _gl_fstat_by_handle (h, rname, buf); 220 1.1 christos CloseHandle (h); 221 1.1 christos goto done; 222 1.1 christos } 223 1.1 christos } 224 1.1 christos 225 1.1 christos /* Test for root and UNC root directories. */ 226 1.1 christos if ((rlen == drive_prefix_len + 1 && ISSLASH (rname[drive_prefix_len])) 227 1.1 christos || is_unc_root (rname)) 228 1.1 christos goto failed; 229 1.1 christos 230 1.1 christos /* Fallback. */ 231 1.1 christos { 232 1.1 christos /* Approach based on the directory entry. */ 233 1.1 christos 234 1.1 christos if (strchr (rname, '?') != NULL || strchr (rname, '*') != NULL) 235 1.1 christos { 236 1.1 christos /* Other Windows API functions would fail with error 237 1.1 christos ERROR_INVALID_NAME. */ 238 1.1 christos if (malloca_rname != NULL) 239 1.1 christos freea (malloca_rname); 240 1.1 christos errno = ENOENT; 241 1.1 christos return -1; 242 1.1 christos } 243 1.1 christos 244 1.1 christos /* Get the details about the directory entry. This can be done through 245 1.1 christos FindFirstFile 246 1.1 christos <https://docs.microsoft.com/en-us/windows/desktop/api/fileapi/nf-fileapi-findfirstfilea> 247 1.1 christos <https://docs.microsoft.com/en-us/windows/desktop/api/minwinbase/ns-minwinbase-_win32_find_dataa> 248 1.1 christos or through 249 1.1 christos FindFirstFileEx with argument FindExInfoBasic 250 1.1 christos <https://docs.microsoft.com/en-us/windows/desktop/api/fileapi/nf-fileapi-findfirstfileexa> 251 1.1 christos <https://docs.microsoft.com/en-us/windows/desktop/api/minwinbase/ne-minwinbase-findex_info_levels> 252 1.1 christos <https://docs.microsoft.com/en-us/windows/desktop/api/minwinbase/ns-minwinbase-_win32_find_dataa> */ 253 1.1 christos WIN32_FIND_DATA info; 254 1.1 christos HANDLE h = FindFirstFile (rname, &info); 255 1.1 christos if (h == INVALID_HANDLE_VALUE) 256 1.1 christos goto failed; 257 1.1 christos 258 1.1 christos /* Test for error conditions before starting to fill *buf. */ 259 1.1 christos if (sizeof (buf->st_size) <= 4 && info.nFileSizeHigh > 0) 260 1.1 christos { 261 1.1 christos FindClose (h); 262 1.1 christos if (malloca_rname != NULL) 263 1.1 christos freea (malloca_rname); 264 1.1 christos errno = EOVERFLOW; 265 1.1 christos return -1; 266 1.1 christos } 267 1.1 christos 268 1.1 christos # if _GL_WINDOWS_STAT_INODES 269 1.1 christos buf->st_dev = 0; 270 1.1 christos # if _GL_WINDOWS_STAT_INODES == 2 271 1.1 christos buf->st_ino._gl_ino[0] = buf->st_ino._gl_ino[1] = 0; 272 1.1 christos # else /* _GL_WINDOWS_STAT_INODES == 1 */ 273 1.1 christos buf->st_ino = 0; 274 1.1 christos # endif 275 1.1 christos # else 276 1.1 christos /* st_ino is not wide enough for identifying a file on a device. 277 1.1 christos Without st_ino, st_dev is pointless. */ 278 1.1 christos buf->st_dev = 0; 279 1.1 christos buf->st_ino = 0; 280 1.1 christos # endif 281 1.1 christos 282 1.1 christos /* st_mode. */ 283 1.1 christos unsigned int mode = 284 1.1 christos /* XXX How to handle FILE_ATTRIBUTE_REPARSE_POINT ? */ 285 1.1 christos ((info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ? _S_IFDIR | S_IEXEC_UGO : _S_IFREG) 286 1.1 christos | S_IREAD_UGO 287 1.1 christos | ((info.dwFileAttributes & FILE_ATTRIBUTE_READONLY) ? 0 : S_IWRITE_UGO); 288 1.1 christos if (!(info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) 289 1.1 christos { 290 1.1 christos /* Determine whether the file is executable by looking at the file 291 1.1 christos name suffix. */ 292 1.1 christos if (info.nFileSizeHigh > 0 || info.nFileSizeLow > 0) 293 1.1 christos { 294 1.1 christos const char *last_dot = NULL; 295 1.1 christos const char *p; 296 1.1 christos for (p = info.cFileName; *p != '\0'; p++) 297 1.1 christos if (*p == '.') 298 1.1 christos last_dot = p; 299 1.1 christos if (last_dot != NULL) 300 1.1 christos { 301 1.1 christos const char *suffix = last_dot + 1; 302 1.1 christos if (_stricmp (suffix, "exe") == 0 303 1.1 christos || _stricmp (suffix, "bat") == 0 304 1.1 christos || _stricmp (suffix, "cmd") == 0 305 1.1 christos || _stricmp (suffix, "com") == 0) 306 1.1 christos mode |= S_IEXEC_UGO; 307 1.1 christos } 308 1.1 christos } 309 1.1 christos } 310 1.1 christos buf->st_mode = mode; 311 1.1 christos 312 1.1 christos /* st_nlink. Ignore hard links here. */ 313 1.1 christos buf->st_nlink = 1; 314 1.1 christos 315 1.1 christos /* There's no easy way to map the Windows SID concept to an integer. */ 316 1.1 christos buf->st_uid = 0; 317 1.1 christos buf->st_gid = 0; 318 1.1 christos 319 1.1 christos /* st_rdev is irrelevant for normal files and directories. */ 320 1.1 christos buf->st_rdev = 0; 321 1.1 christos 322 1.1 christos /* st_size. */ 323 1.1 christos if (sizeof (buf->st_size) <= 4) 324 1.1 christos /* Range check already done above. */ 325 1.1 christos buf->st_size = info.nFileSizeLow; 326 1.1 christos else 327 1.1 christos buf->st_size = ((long long) info.nFileSizeHigh << 32) | (long long) info.nFileSizeLow; 328 1.1 christos 329 1.1 christos /* st_atime, st_mtime, st_ctime. */ 330 1.1 christos # if _GL_WINDOWS_STAT_TIMESPEC 331 1.1 christos buf->st_atim = _gl_convert_FILETIME_to_timespec (&info.ftLastAccessTime); 332 1.1 christos buf->st_mtim = _gl_convert_FILETIME_to_timespec (&info.ftLastWriteTime); 333 1.1 christos buf->st_ctim = _gl_convert_FILETIME_to_timespec (&info.ftCreationTime); 334 1.1 christos # else 335 1.1 christos buf->st_atime = _gl_convert_FILETIME_to_POSIX (&info.ftLastAccessTime); 336 1.1 christos buf->st_mtime = _gl_convert_FILETIME_to_POSIX (&info.ftLastWriteTime); 337 1.1 christos buf->st_ctime = _gl_convert_FILETIME_to_POSIX (&info.ftCreationTime); 338 1.1 christos # endif 339 1.1 christos 340 1.1 christos FindClose (h); 341 1.1 christos 342 1.1 christos ret = 0; 343 1.1 christos } 344 1.1 christos 345 1.1 christos done: 346 1.1 christos if (ret >= 0 && check_dir && !S_ISDIR (buf->st_mode)) 347 1.1 christos { 348 1.1 christos errno = ENOTDIR; 349 1.1 christos ret = -1; 350 1.1 christos } 351 1.1 christos if (malloca_rname != NULL) 352 1.1 christos { 353 1.1 christos int saved_errno = errno; 354 1.1 christos freea (malloca_rname); 355 1.1 christos errno = saved_errno; 356 1.1 christos } 357 1.1 christos return ret; 358 1.1 christos } 359 1.1 christos 360 1.1 christos failed: 361 1.1 christos { 362 1.1 christos DWORD error = GetLastError (); 363 1.1 christos #if 0 364 1.1 christos fprintf (stderr, "rpl_stat error 0x%x\n", (unsigned int) error); 365 1.1 christos #endif 366 1.1 christos 367 1.1 christos if (malloca_rname != NULL) 368 1.1 christos freea (malloca_rname); 369 1.1 christos 370 1.1 christos switch (error) 371 1.1 christos { 372 1.1 christos /* Some of these errors probably cannot happen with the specific flags 373 1.1 christos that we pass to CreateFile. But who knows... */ 374 1.1 christos case ERROR_FILE_NOT_FOUND: /* The last component of rname does not exist. */ 375 1.1 christos case ERROR_PATH_NOT_FOUND: /* Some directory component in rname does not exist. */ 376 1.1 christos case ERROR_BAD_PATHNAME: /* rname is such as '\\server'. */ 377 1.1 christos case ERROR_BAD_NET_NAME: /* rname is such as '\\server\nonexistentshare'. */ 378 1.1 christos case ERROR_INVALID_NAME: /* rname contains wildcards, misplaced colon, etc. */ 379 1.1 christos case ERROR_DIRECTORY: 380 1.1 christos errno = ENOENT; 381 1.1 christos break; 382 1.1 christos 383 1.1 christos case ERROR_ACCESS_DENIED: /* rname is such as 'C:\System Volume Information\foo'. */ 384 1.1 christos case ERROR_SHARING_VIOLATION: /* rname is such as 'C:\pagefile.sys' (second approach only). */ 385 1.1 christos /* XXX map to EACCES or EPERM? */ 386 1.1 christos errno = EACCES; 387 1.1 christos break; 388 1.1 christos 389 1.1 christos case ERROR_OUTOFMEMORY: 390 1.1 christos errno = ENOMEM; 391 1.1 christos break; 392 1.1 christos 393 1.1 christos case ERROR_WRITE_PROTECT: 394 1.1 christos errno = EROFS; 395 1.1 christos break; 396 1.1 christos 397 1.1 christos case ERROR_WRITE_FAULT: 398 1.1 christos case ERROR_READ_FAULT: 399 1.1 christos case ERROR_GEN_FAILURE: 400 1.1 christos errno = EIO; 401 1.1 christos break; 402 1.1 christos 403 1.1 christos case ERROR_BUFFER_OVERFLOW: 404 1.1 christos case ERROR_FILENAME_EXCED_RANGE: 405 1.1 christos errno = ENAMETOOLONG; 406 1.1 christos break; 407 1.1 christos 408 1.1 christos case ERROR_DELETE_PENDING: /* XXX map to EACCES or EPERM? */ 409 1.1 christos errno = EPERM; 410 1.1 christos break; 411 1.1 christos 412 1.1 christos default: 413 1.1 christos errno = EINVAL; 414 1.1 christos break; 415 1.1 christos } 416 1.1 christos 417 1.1 christos return -1; 418 1.1 christos } 419 1.1 christos #else 420 1.1 christos int result = orig_stat (name, buf); 421 1.1 christos if (result == 0) 422 1.1 christos { 423 1.1 christos # if REPLACE_FUNC_STAT_FILE 424 1.1 christos /* Solaris 9 mistakenly succeeds when given a non-directory with a 425 1.1 christos trailing slash. */ 426 1.1 christos if (!S_ISDIR (buf->st_mode)) 427 1.1 christos { 428 1.1 christos size_t len = strlen (name); 429 1.1 christos if (ISSLASH (name[len - 1])) 430 1.1 christos { 431 1.1 christos errno = ENOTDIR; 432 1.1 christos return -1; 433 1.1 christos } 434 1.1 christos } 435 1.1 christos # endif /* REPLACE_FUNC_STAT_FILE */ 436 1.1 christos result = stat_time_normalize (result, buf); 437 1.1 christos } 438 1.1 christos return result; 439 1.1 christos #endif 440 1.1 christos } 441