Skip to content

Commit 39fb64e

Browse files
committed
mingw: implement a platform-specific strbuf_realpath()
There is a Win32 API function to resolve symbolic links, and we can use that instead of resolving them manually. Even better, this function also resolves NTFS junction points (which are somewhat similar to bind mounts). This fixes #2481. Signed-off-by: Johannes Schindelin <[email protected]>
1 parent b32320c commit 39fb64e

File tree

5 files changed

+95
-1
lines changed

5 files changed

+95
-1
lines changed

compat/mingw.c

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1125,6 +1125,82 @@ struct tm *localtime_r(const time_t *timep, struct tm *result)
11251125
}
11261126
#endif
11271127

1128+
char *mingw_strbuf_realpath(struct strbuf *resolved, const char *path)
1129+
{
1130+
wchar_t wpath[MAX_PATH];
1131+
HANDLE h;
1132+
DWORD ret;
1133+
int len;
1134+
const char *last_component = NULL;
1135+
char *append = NULL;
1136+
1137+
if (xutftowcs_path(wpath, path) < 0)
1138+
return NULL;
1139+
1140+
h = CreateFileW(wpath, 0,
1141+
FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL,
1142+
OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);
1143+
1144+
/*
1145+
* strbuf_realpath() allows the last path component to not exist. If
1146+
* that is the case, now it's time to try without last component.
1147+
*/
1148+
if (h == INVALID_HANDLE_VALUE &&
1149+
GetLastError() == ERROR_FILE_NOT_FOUND) {
1150+
/* cut last component off of `wpath` */
1151+
wchar_t *p = wpath + wcslen(wpath);
1152+
1153+
while (p != wpath)
1154+
if (*(--p) == L'/' || *p == L'\\')
1155+
break; /* found start of last component */
1156+
1157+
if (p != wpath && (last_component = find_last_dir_sep(path))) {
1158+
append = xstrdup(last_component + 1); /* skip directory separator */
1159+
/*
1160+
* Do not strip the trailing slash at the drive root, otherwise
1161+
* the path would be e.g. `C:` (which resolves to the
1162+
* _current_ directory on that drive).
1163+
*/
1164+
if (p[-1] == L':')
1165+
p[1] = L'\0';
1166+
else
1167+
*p = L'\0';
1168+
h = CreateFileW(wpath, 0, FILE_SHARE_READ |
1169+
FILE_SHARE_WRITE | FILE_SHARE_DELETE,
1170+
NULL, OPEN_EXISTING,
1171+
FILE_FLAG_BACKUP_SEMANTICS, NULL);
1172+
}
1173+
}
1174+
1175+
if (h == INVALID_HANDLE_VALUE) {
1176+
realpath_failed:
1177+
FREE_AND_NULL(append);
1178+
return NULL;
1179+
}
1180+
1181+
ret = GetFinalPathNameByHandleW(h, wpath, ARRAY_SIZE(wpath), 0);
1182+
CloseHandle(h);
1183+
if (!ret || ret >= ARRAY_SIZE(wpath))
1184+
goto realpath_failed;
1185+
1186+
len = wcslen(wpath) * 3;
1187+
strbuf_grow(resolved, len);
1188+
len = xwcstoutf(resolved->buf, normalize_ntpath(wpath), len);
1189+
if (len < 0)
1190+
goto realpath_failed;
1191+
resolved->len = len;
1192+
1193+
if (append) {
1194+
/* Use forward-slash, like `normalize_ntpath()` */
1195+
strbuf_complete(resolved, '/');
1196+
strbuf_addstr(resolved, append);
1197+
FREE_AND_NULL(append);
1198+
}
1199+
1200+
return resolved->buf;
1201+
1202+
}
1203+
11281204
char *mingw_getcwd(char *pointer, int len)
11291205
{
11301206
wchar_t cwd[MAX_PATH], wpointer[MAX_PATH];

compat/mingw.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -457,6 +457,9 @@ static inline void convert_slashes(char *path)
457457
#define PATH_SEP ';'
458458
char *mingw_query_user_email(void);
459459
#define query_user_email mingw_query_user_email
460+
struct strbuf;
461+
char *mingw_strbuf_realpath(struct strbuf *resolved, const char *path);
462+
#define platform_strbuf_realpath mingw_strbuf_realpath
460463
#if !defined(__MINGW64_VERSION_MAJOR) && (!defined(_MSC_VER) || _MSC_VER < 1800)
461464
#define PRIuMAX "I64u"
462465
#define PRId64 "I64d"

t/t0060-path-utils.sh

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,14 @@ test_expect_success SYMLINKS 'real path works on symlinks' '
282282
test_cmp expect actual
283283
'
284284

285+
test_expect_success MINGW 'real path works near drive root' '
286+
# we need a non-existing path at the drive root; simply skip if C:/xyz exists
287+
if test ! -e C:/xyz
288+
then
289+
test C:/xyz = $(test-tool path-utils real_path C:/xyz)
290+
fi
291+
'
292+
285293
test_expect_success SYMLINKS 'prefix_path works with absolute paths to work tree symlinks' '
286294
ln -s target symlink &&
287295
echo "symlink" >expect &&

t/t3700-add.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -506,7 +506,7 @@ test_expect_success CASE_INSENSITIVE_FS 'path is case-insensitive' '
506506
git add "$downcased"
507507
'
508508

509-
test_expect_failure MINGW 'can add files via NTFS junctions' '
509+
test_expect_success MINGW 'can add files via NTFS junctions' '
510510
test_when_finished "cmd //c rmdir junction && rm -rf target" &&
511511
test_create_repo target &&
512512
cmd //c "mklink /j junction target" &&

t/t5601-clone.sh

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,13 @@ test_expect_success 'clone respects GIT_WORK_TREE' '
7171
7272
'
7373

74+
test_expect_success CASE_INSENSITIVE_FS 'core.worktree is not added due to path case' '
75+
76+
mkdir UPPERCASE &&
77+
git clone src "$(pwd)/uppercase" &&
78+
test "unset" = "$(git -C UPPERCASE config --default unset core.worktree)"
79+
'
80+
7481
test_expect_success 'clone from hooks' '
7582
7683
test_create_repo r0 &&

0 commit comments

Comments
 (0)