Skip to content

Commit 4b0abd5

Browse files
dschogitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 4426fb5 commit 4b0abd5

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

compat/mingw.c

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -454,6 +454,39 @@ static inline time_t filetime_to_time_t(const FILETIME *ft)
454454
return (time_t)(filetime_to_hnsec(ft) / 10000000);
455455
}
456456

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+
457490
/* We keep the do_lstat code in a separate function to avoid recursion.
458491
* When a path ends with a slash, the stat will fail with ENOENT. In
459492
* 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)
514547
case ERROR_NOT_ENOUGH_MEMORY:
515548
errno = ENOMEM;
516549
break;
550+
case ERROR_PATH_NOT_FOUND:
551+
if (!has_valid_directory_prefix(wfilename)) {
552+
errno = ENOTDIR;
553+
break;
554+
}
555+
/* fallthru */
517556
default:
518557
errno = ENOENT;
519558
break;

0 commit comments

Comments
 (0)