Skip to content

Commit a027d43

Browse files
committed
Merge branch 'tb/mingw-rmdir-symlink-to-directory' into next
Windows rmdir() equivalent behaves differently from POSIX ones in that when used on a symbolic link that points at a directory, the target directory gets removed, which has been corrected. * tb/mingw-rmdir-symlink-to-directory: mingw: align symlinks-related rmdir() behavior with Linux
2 parents d80a07d + 3e7d488 commit a027d43

File tree

3 files changed

+37
-0
lines changed

3 files changed

+37
-0
lines changed

compat/mingw.c

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,27 @@ int mingw_rmdir(const char *pathname)
341341
{
342342
int ret, tries = 0;
343343
wchar_t wpathname[MAX_PATH];
344+
struct stat st;
345+
346+
/*
347+
* Contrary to Linux' `rmdir()`, Windows' _wrmdir() and _rmdir()
348+
* (and `RemoveDirectoryW()`) will attempt to remove the target of a
349+
* symbolic link (if it points to a directory).
350+
*
351+
* This behavior breaks the assumption of e.g. `remove_path()` which
352+
* upon successful deletion of a file will attempt to remove its parent
353+
* directories recursively until failure (which usually happens when
354+
* the directory is not empty).
355+
*
356+
* Therefore, before calling `_wrmdir()`, we first check if the path is
357+
* a symbolic link. If it is, we exit and return the same error as
358+
* Linux' `rmdir()` would, i.e. `ENOTDIR`.
359+
*/
360+
if (!mingw_lstat(pathname, &st) && S_ISLNK(st.st_mode)) {
361+
errno = ENOTDIR;
362+
return -1;
363+
}
364+
344365
if (xutftowcs_path(wpathname, pathname) < 0)
345366
return -1;
346367

t/t3400-rebase.sh

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -406,4 +406,14 @@ test_expect_success 'refuse to switch to branch checked out elsewhere' '
406406
test_i18ngrep "already checked out" err
407407
'
408408

409+
test_expect_success MINGW,SYMLINKS_WINDOWS 'rebase when .git/logs is a symlink' '
410+
git checkout main &&
411+
mv .git/logs actual_logs &&
412+
cmd //c "mklink /D .git\logs ..\actual_logs" &&
413+
git rebase -f HEAD^ &&
414+
test -L .git/logs &&
415+
rm .git/logs &&
416+
mv actual_logs .git/logs
417+
'
418+
409419
test_done

t/test-lib.sh

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1545,6 +1545,12 @@ test_lazy_prereq SYMLINKS '
15451545
ln -s x y && test -h y
15461546
'
15471547

1548+
test_lazy_prereq SYMLINKS_WINDOWS '
1549+
# test whether symbolic links are enabled on Windows
1550+
test_have_prereq MINGW &&
1551+
cmd //c "mklink y x" &> /dev/null && test -h y
1552+
'
1553+
15481554
test_lazy_prereq FILEMODE '
15491555
test "$(git config --bool core.filemode)" = true
15501556
'

0 commit comments

Comments
 (0)