Skip to content

Commit

Permalink
fixup! mingw: implement a platform-specific strbuf_realpath()
Browse files Browse the repository at this point in the history
Let's be careful to make this function work near the drive root: to
resolve the drive root path itself, we _need_ a trailing backslash:
if a file handle to the path `C:` is created, it does not actually refer
to the drive root. Instead, it refers to this very Windows-only concept
of a "per-drive current directory".

This also requires the code that wants to re-append the last component
to be more careful and only append a slash _if necessary_.

This commit fixes the problem with `scalar unregister C:/foo` that was
reported at #4200.

Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
  • Loading branch information
dscho committed Jan 27, 2023
1 parent 622f956 commit b9457d6
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
12 changes: 10 additions & 2 deletions compat/mingw.c
Original file line number Diff line number Diff line change
Expand Up @@ -1410,7 +1410,15 @@ char *mingw_strbuf_realpath(struct strbuf *resolved, const char *path)

if (p != wpath && (last_component = find_last_dir_sep(path))) {
append = xstrdup(last_component + 1); /* skip directory separator */
*p = L'\0';
/*
* Do not strip the trailing slash at the drive root, otherwise
* the path would be e.g. `C:` (which resolves to the
* _current_ directory on that drive).
*/
if (p[-1] == L':')
p[1] = L'\0';
else
*p = L'\0';
h = CreateFileW(wpath, 0, FILE_SHARE_READ |
FILE_SHARE_WRITE | FILE_SHARE_DELETE,
NULL, OPEN_EXISTING,
Expand Down Expand Up @@ -1438,7 +1446,7 @@ char *mingw_strbuf_realpath(struct strbuf *resolved, const char *path)

if (append) {
/* Use forward-slash, like `normalize_ntpath()` */
strbuf_addch(resolved, '/');
strbuf_complete(resolved, '/');
strbuf_addstr(resolved, append);
FREE_AND_NULL(append);
}
Expand Down
6 changes: 6 additions & 0 deletions t/t0060-path-utils.sh
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,12 @@ test_expect_success SYMLINKS 'real path works on symlinks' '
test "$sym" = "$(test-tool path-utils real_path "$dir2/syml")"
'

test_expect_success MINGW 'real path works near drive root' '
# we need a non-existing path at the drive root; simply skip if C:/xyz exists
test -e C:/xyz ||
test C:/xyz = $(test-tool path-utils real_path C:/xyz)
'

test_expect_success SYMLINKS 'prefix_path works with absolute paths to work tree symlinks' '
ln -s target symlink &&
test "$(test-tool path-utils prefix_path prefix "$(pwd)/symlink")" = "symlink"
Expand Down

0 comments on commit b9457d6

Please sign in to comment.