summaryrefslogtreecommitdiff
path: root/compat/mingw.c
diff options
context:
space:
mode:
authorJunio C Hamano <gitster@pobox.com>2018-10-30 06:43:48 (GMT)
committerJunio C Hamano <gitster@pobox.com>2018-10-30 06:43:48 (GMT)
commitcc6748720f9014b2c2cea8a603acd9ff6a62b87c (patch)
treeae7a1739d032f57b79741eff5acbf40017f793d2 /compat/mingw.c
parentd1622fdbdd83831983e8b99250792e5fca7b9159 (diff)
parent4745feeb8b501b309f2eb2e0330abd6c24d6fb60 (diff)
downloadgit-cc6748720f9014b2c2cea8a603acd9ff6a62b87c.zip
git-cc6748720f9014b2c2cea8a603acd9ff6a62b87c.tar.gz
git-cc6748720f9014b2c2cea8a603acd9ff6a62b87c.tar.bz2
Merge branch 'js/mingw-getcwd'
The way the Windows port figures out the current directory has been improved. * js/mingw-getcwd: mingw: fix getcwd when the parent directory cannot be queried mingw: ensure `getcwd()` reports the correct case
Diffstat (limited to 'compat/mingw.c')
-rw-r--r--compat/mingw.c50
1 files changed, 48 insertions, 2 deletions
diff --git a/compat/mingw.c b/compat/mingw.c
index 44264fe..6c3c4ca 100644
--- a/compat/mingw.c
+++ b/compat/mingw.c
@@ -203,6 +203,31 @@ static int ask_yes_no_if_possible(const char *format, ...)
}
}
+/* Normalizes NT paths as returned by some low-level APIs. */
+static wchar_t *normalize_ntpath(wchar_t *wbuf)
+{
+ int i;
+ /* fix absolute path prefixes */
+ if (wbuf[0] == '\\') {
+ /* strip NT namespace prefixes */
+ if (!wcsncmp(wbuf, L"\\??\\", 4) ||
+ !wcsncmp(wbuf, L"\\\\?\\", 4))
+ wbuf += 4;
+ else if (!wcsnicmp(wbuf, L"\\DosDevices\\", 12))
+ wbuf += 12;
+ /* replace remaining '...UNC\' with '\\' */
+ if (!wcsnicmp(wbuf, L"UNC\\", 4)) {
+ wbuf += 2;
+ *wbuf = '\\';
+ }
+ }
+ /* convert backslashes to slashes */
+ for (i = 0; wbuf[i]; i++)
+ if (wbuf[i] == '\\')
+ wbuf[i] = '/';
+ return wbuf;
+}
+
int mingw_unlink(const char *pathname)
{
int ret, tries = 0;
@@ -918,8 +943,29 @@ struct tm *localtime_r(const time_t *timep, struct tm *result)
char *mingw_getcwd(char *pointer, int len)
{
- wchar_t wpointer[MAX_PATH];
- if (!_wgetcwd(wpointer, ARRAY_SIZE(wpointer)))
+ wchar_t cwd[MAX_PATH], wpointer[MAX_PATH];
+ DWORD ret = GetCurrentDirectoryW(ARRAY_SIZE(cwd), cwd);
+
+ if (!ret || ret >= ARRAY_SIZE(cwd)) {
+ errno = ret ? ENAMETOOLONG : err_win_to_posix(GetLastError());
+ return NULL;
+ }
+ ret = GetLongPathNameW(cwd, wpointer, ARRAY_SIZE(wpointer));
+ if (!ret && GetLastError() == ERROR_ACCESS_DENIED) {
+ HANDLE hnd = CreateFileW(cwd, 0,
+ FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL,
+ OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);
+ if (hnd == INVALID_HANDLE_VALUE)
+ return NULL;
+ ret = GetFinalPathNameByHandleW(hnd, wpointer, ARRAY_SIZE(wpointer), 0);
+ CloseHandle(hnd);
+ if (!ret || ret >= ARRAY_SIZE(wpointer))
+ return NULL;
+ if (xwcstoutf(pointer, normalize_ntpath(wpointer), len) < 0)
+ return NULL;
+ return pointer;
+ }
+ if (!ret || ret >= ARRAY_SIZE(wpointer))
return NULL;
if (xwcstoutf(pointer, wpointer, len) < 0)
return NULL;