git/compat
Eliah Kagan 975fc0471a compat/mingw: rename the symlink, not the target
Since 183ea3ea (Merge branch 'ps/mingw-rename', 2024-11-13),
a new technique is used on Windows to rename files, where supported.
The first step of this technique is to open the file with
`CreateFileW`. At that time, `FILE_ATTRIBUTE_NORMAL` was passed as
the value of the `dwFlagsAndAttributes` argument. In b30404df [2], this
was improved by passing `FILE_FLAG_BACKUP_SEMANTICS`, to support
directories as well as regular files.

However, neither value of `dwFlagsAndAttributes` is sufficient to open
a symbolic link with the correct semantics to rename it. Symlinks on
Windows are reparse points. Attempting to open a reparse point with
`CreateFileW` dereferences the reparse point and opens the target
instead, unless `FILE_FLAG_OPEN_REPARSE_POINT` is included in
`dwFlagsAndAttributes`. This is documented for that flag and in the
"Symbolic Link Behavior" section of the `CreateFileW` docs [3].

This produces a regression where attempting to rename a symlink on
Windows renames its target to the intended new name and location of the
symlink. For example, if `symlink` points to `file`, then running

    git mv symlink symlink-renamed

leaves `symlink` in place and unchanged, but renames `file` to
`symlink-renamed` [4].

This regression is detectable by existing tests in `t7001-mv.sh`, but
the tests must be run by a Windows user with the ability to create
symlinks, and the `ln -s` command used to create the initial symlink
must also be able to create a real symlink (such as by setting the
`MSYS` environment variable to `winsymlinks:nativestrict`). Then
these two tests fail if the regression is present, and pass otherwise:

    38 - git mv should overwrite file with a symlink
    39 - check moved symlink

Let's fix this, so that renaming a symlink again renames the symlink
itself and leaves the target unchanged, by passing

    FILE_FLAG_BACKUP_SEMANTICS | FILE_FLAG_OPEN_REPARSE_POINT

as the `dwFlagsAndAttributes` argument. This is sufficient (and safe)
because including `FILE_FLAG_OPEN_REPARSE_POINT` causes no harm even
when used to open a file or directory that is not a reparse point. In
that case, as noted in [3], this flag is simply ignored.

[1]: 183ea3eabf
[2]: b30404dfc0
[3]: https://learn.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-createfilew
[4]: https://github.com/git-for-windows/git/issues/5436

Signed-off-by: Eliah Kagan <eliah.kagan@gmail.com>
Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2025-02-21 10:24:43 -08:00
..
fsmonitor global: trivial conversions to fix `-Wsign-compare` warnings 2024-12-06 20:20:04 +09:00
linux treewide: remove unnecessary cache.h inclusion from several sources 2023-03-21 10:56:51 -07:00
nedmalloc compat: disable -Wunused-parameter in 3rd-party code 2024-08-28 09:51:18 -07:00
poll global: mark code units that generate warnings with `-Wsign-compare` 2024-12-06 20:20:02 +09:00
regex compat/regex: explicitly ignore "-Wsign-compare" warnings 2024-12-06 20:20:01 +09:00
simple-ipc Merge branch 'ak/typofixes' 2024-10-25 14:02:04 -04:00
stub compat: mark unused parameters in win32/mingw functions 2024-08-28 09:51:18 -07:00
vcbuild mingw.c: Fix complier warnings for a 64 bit msvc 2024-10-17 14:42:27 -04:00
win32 compat/win32: fix -Wsign-compare warning in "wWinMain()" 2024-12-06 20:20:01 +09:00
.gitattributes
access.c
apple-common-crypto.h
basename.c compat/win32: fix const-correctness with string constants 2024-06-07 10:30:52 -07:00
bswap.h bswap.h: squelch potential sparse -Wcast-truncate warnings 2025-01-21 08:42:55 -08:00
compiler.h mingw.c: Fix complier warnings for a 64 bit msvc 2024-10-17 14:42:27 -04:00
disk.h compat: drop inclusion of <git-compat-util.h> 2024-02-24 14:37:41 -08:00
fileno.c
fopen.c
hstrerror.c
inet_ntop.c
inet_pton.c
memmem.c
mingw.c compat/mingw: rename the symlink, not the target 2025-02-21 10:24:43 -08:00
mingw.h compat: mark unused parameters in win32/mingw functions 2024-08-28 09:51:18 -07:00
mkdir.c
mkdtemp.c
mmap.c
msvc.c
msvc.h
nonblock.c mark unused parameters in trivial compat functions 2022-10-17 21:24:03 -07:00
nonblock.h compat: add function to enable nonblocking pipes 2022-08-17 09:21:40 -07:00
obstack.c
obstack.h
open.c
pread.c treewide: remove double forward declaration of read_in_full 2023-04-11 08:52:11 -07:00
precompose_utf8.c global: prepare for hiding away repo-less config functions 2024-08-13 10:01:05 -07:00
precompose_utf8.h
qsort_s.c
regcomp_enhanced.c use enhanced basic regular expressions on macOS 2023-01-08 10:06:34 +09:00
setenv.c
sha1-chunked.c hash-ll: merge with "hash.h" 2024-06-14 10:26:33 -07:00
sha1-chunked.h
snprintf.c
stat.c
strcasestr.c
strdup.c
strlcpy.c
strtoimax.c
strtoumax.c
terminal.c global: trivial conversions to fix `-Wsign-compare` warnings 2024-12-06 20:20:04 +09:00
terminal.h
unsetenv.c
win32.h
win32mmap.c global: mark code units that generate warnings with `-Wsign-compare` 2024-12-06 20:20:02 +09:00
winansi.c global: mark code units that generate warnings with `-Wsign-compare` 2024-12-06 20:20:02 +09:00
zlib-compat.h compat/zlib: allow use of zlib-ng as backend 2025-01-28 13:03:23 -08:00