Skip to content

Commit d4efdb0

Browse files
dschoGit for Windows Build Agent
authored andcommitted
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 4bb1bdd commit d4efdb0

File tree

3 files changed

+34
-1
lines changed

3 files changed

+34
-1
lines changed

compat/mingw.c

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1047,6 +1047,37 @@ struct tm *localtime_r(const time_t *timep, struct tm *result)
10471047
return NULL;
10481048
}
10491049

1050+
char *mingw_strbuf_realpath(struct strbuf *resolved, const char *path)
1051+
{
1052+
wchar_t wpath[MAX_PATH];
1053+
HANDLE h;
1054+
DWORD ret;
1055+
int len;
1056+
1057+
if (xutftowcs_path(wpath, path) < 0)
1058+
return NULL;
1059+
1060+
h = CreateFileW(wpath, 0,
1061+
FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL,
1062+
OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);
1063+
if (h == INVALID_HANDLE_VALUE)
1064+
return NULL;
1065+
1066+
ret = GetFinalPathNameByHandleW(h, wpath, ARRAY_SIZE(wpath), 0);
1067+
CloseHandle(h);
1068+
if (!ret || ret >= ARRAY_SIZE(wpath))
1069+
return NULL;
1070+
1071+
len = wcslen(wpath) * 3;
1072+
strbuf_grow(resolved, len);
1073+
len = xwcstoutf(resolved->buf, normalize_ntpath(wpath), len);
1074+
if (len < 0)
1075+
return NULL;
1076+
resolved->len = len;
1077+
return resolved->buf;
1078+
1079+
}
1080+
10501081
char *mingw_getcwd(char *pointer, int len)
10511082
{
10521083
wchar_t cwd[MAX_PATH], wpointer[MAX_PATH];

compat/mingw.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -445,6 +445,8 @@ static inline void convert_slashes(char *path)
445445
#define PATH_SEP ';'
446446
char *mingw_query_user_email(void);
447447
#define query_user_email mingw_query_user_email
448+
char *mingw_strbuf_realpath(struct strbuf *resolved, const char *path);
449+
#define platform_strbuf_realpath mingw_strbuf_realpath
448450
#if !defined(__MINGW64_VERSION_MAJOR) && (!defined(_MSC_VER) || _MSC_VER < 1800)
449451
#define PRIuMAX "I64u"
450452
#define PRId64 "I64d"

t/t3700-add.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -423,7 +423,7 @@ test_expect_success CASE_INSENSITIVE_FS 'path is case-insensitive' '
423423
git add "$downcased"
424424
'
425425

426-
test_expect_failure MINGW 'can add files via NTFS junctions' '
426+
test_expect_success MINGW 'can add files via NTFS junctions' '
427427
test_when_finished "cmd //c rmdir junction && rm -rf target" &&
428428
test_create_repo target &&
429429
cmd //c "mklink /j junction target" &&

0 commit comments

Comments
 (0)