Skip to content

Commit d1e51aa

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 <johannes.schindelin@gmx.de>
1 parent 318939f commit d1e51aa

File tree

5 files changed

+95
-1
lines changed

5 files changed

+95
-1
lines changed

compat/mingw.c

+76
Original file line numberDiff line numberDiff line change
@@ -1207,6 +1207,82 @@ struct tm *localtime_r(const time_t *timep, struct tm *result)
12071207
}
12081208
#endif
12091209

1210+
char *mingw_strbuf_realpath(struct strbuf *resolved, const char *path)
1211+
{
1212+
wchar_t wpath[MAX_PATH];
1213+
HANDLE h;
1214+
DWORD ret;
1215+
int len;
1216+
const char *last_component = NULL;
1217+
char *append = NULL;
1218+
1219+
if (xutftowcs_path(wpath, path) < 0)
1220+
return NULL;
1221+
1222+
h = CreateFileW(wpath, 0,
1223+
FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL,
1224+
OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);
1225+
1226+
/*
1227+
* strbuf_realpath() allows the last path component to not exist. If
1228+
* that is the case, now it's time to try without last component.
1229+
*/
1230+
if (h == INVALID_HANDLE_VALUE &&
1231+
GetLastError() == ERROR_FILE_NOT_FOUND) {
1232+
/* cut last component off of `wpath` */
1233+
wchar_t *p = wpath + wcslen(wpath);
1234+
1235+
while (p != wpath)
1236+
if (*(--p) == L'/' || *p == L'\\')
1237+
break; /* found start of last component */
1238+
1239+
if (p != wpath && (last_component = find_last_dir_sep(path))) {
1240+
append = xstrdup(last_component + 1); /* skip directory separator */
1241+
/*
1242+
* Do not strip the trailing slash at the drive root, otherwise
1243+
* the path would be e.g. `C:` (which resolves to the
1244+
* _current_ directory on that drive).
1245+
*/
1246+
if (p[-1] == L':')
1247+
p[1] = L'\0';
1248+
else
1249+
*p = L'\0';
1250+
h = CreateFileW(wpath, 0, FILE_SHARE_READ |
1251+
FILE_SHARE_WRITE | FILE_SHARE_DELETE,
1252+
NULL, OPEN_EXISTING,
1253+
FILE_FLAG_BACKUP_SEMANTICS, NULL);
1254+
}
1255+
}
1256+
1257+
if (h == INVALID_HANDLE_VALUE) {
1258+
realpath_failed:
1259+
FREE_AND_NULL(append);
1260+
return NULL;
1261+
}
1262+
1263+
ret = GetFinalPathNameByHandleW(h, wpath, ARRAY_SIZE(wpath), 0);
1264+
CloseHandle(h);
1265+
if (!ret || ret >= ARRAY_SIZE(wpath))
1266+
goto realpath_failed;
1267+
1268+
len = wcslen(wpath) * 3;
1269+
strbuf_grow(resolved, len);
1270+
len = xwcstoutf(resolved->buf, normalize_ntpath(wpath), len);
1271+
if (len < 0)
1272+
goto realpath_failed;
1273+
resolved->len = len;
1274+
1275+
if (append) {
1276+
/* Use forward-slash, like `normalize_ntpath()` */
1277+
strbuf_complete(resolved, '/');
1278+
strbuf_addstr(resolved, append);
1279+
FREE_AND_NULL(append);
1280+
}
1281+
1282+
return resolved->buf;
1283+
1284+
}
1285+
12101286
char *mingw_getcwd(char *pointer, int len)
12111287
{
12121288
wchar_t cwd[MAX_PATH], wpointer[MAX_PATH];

compat/mingw.h

+3
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

+8
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,14 @@ test_expect_success SYMLINKS 'real path works on symlinks' '
281281
test_cmp expect actual
282282
'
283283

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

t/t3700-add.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -548,7 +548,7 @@ test_expect_success CASE_INSENSITIVE_FS 'path is case-insensitive' '
548548
git add "$downcased"
549549
'
550550

551-
test_expect_failure MINGW 'can add files via NTFS junctions' '
551+
test_expect_success MINGW 'can add files via NTFS junctions' '
552552
test_when_finished "cmd //c rmdir junction && rm -rf target" &&
553553
test_create_repo target &&
554554
cmd //c "mklink /j junction target" &&

t/t5601-clone.sh

+7
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,13 @@ test_expect_success 'clone respects GIT_WORK_TREE' '
7878
7979
'
8080

81+
test_expect_success CASE_INSENSITIVE_FS 'core.worktree is not added due to path case' '
82+
83+
mkdir UPPERCASE &&
84+
git clone src "$(pwd)/uppercase" &&
85+
test "unset" = "$(git -C UPPERCASE config --default unset core.worktree)"
86+
'
87+
8188
test_expect_success 'clone from hooks' '
8289
8390
test_create_repo r0 &&

0 commit comments

Comments
 (0)