mingw: let lstat() fail with errno == ENOTDIR when appropriate
POSIX semantics requires lstat() to fail with ENOTDIR when "[a] component of the path prefix names an existing file that is neither a directory nor a symbolic link to a directory". See http://pubs.opengroup.org/onlinepubs/9699919799/functions/lstat.html This behavior is expected by t1404-update-ref-df-conflicts now. Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Johannes Schindelin committed
Jan 26, 2016 at 15:34 UTC
4b0abd5c695c87bf600e57b6a5c7d6844707d34c
1 file changed
+39
compat/mingw.c
+39
@@ -454,6 +454,39 @@ static inline time_t filetime_to_time_t(const FILETIME *ft)
454
return (time_t)(filetime_to_hnsec(ft) / 10000000);
455
}
456
457
+/**
458
+ * Verifies that safe_create_leading_directories() would succeed.
459
+ */
460
+static int has_valid_directory_prefix(wchar_t *wfilename)
461
+{
462
+ int n = wcslen(wfilename);
463
+
464
+ while (n > 0) {
465
+ wchar_t c = wfilename[--n];
466
+ DWORD attributes;
467
+
468
+ if (!is_dir_sep(c))
469
+ continue;
470
+
471
+ wfilename[n] = L'\0';
472
+ attributes = GetFileAttributesW(wfilename);
473
+ wfilename[n] = c;
474
+ if (attributes == FILE_ATTRIBUTE_DIRECTORY ||
475
+ attributes == FILE_ATTRIBUTE_DEVICE)
476
+ return 1;
477
+ if (attributes == INVALID_FILE_ATTRIBUTES)
478
+ switch (GetLastError()) {
479
+ case ERROR_PATH_NOT_FOUND:
480
+ continue;
481
+ case ERROR_FILE_NOT_FOUND:
482
+ /* This implies parent directory exists. */
483
+ return 1;
484
+ }
485
+ return 0;
486
+ }
487
+ return 1;
488
+}
489
+
490
/* We keep the do_lstat code in a separate function to avoid recursion.
491
* When a path ends with a slash, the stat will fail with ENOENT. In
492
* this case, we strip the trailing slashes and stat again.
@@ -514,6 +547,12 @@ static int do_lstat(int follow, const char *file_name, struct stat *buf)
547
case ERROR_NOT_ENOUGH_MEMORY:
548
errno = ENOMEM;
549
break;
550
+ case ERROR_PATH_NOT_FOUND:
551
+ if (!has_valid_directory_prefix(wfilename)) {
552
+ errno = ENOTDIR;
553
+ break;
554
+ }
555
+ /* fallthru */
556
default:
557
errno = ENOENT;
558
break;