Skip to content

Commit 381a90a

Browse files
committed
mingw: keep trailing slashes when switching directories
This is needed so that `_wchdir()` can be used with drive root directories, e.g. C:\ (`_wchdir("C:")` fails to switch the directory to the root directory). This fixes msysgit#359 (in Git for Windows 2.x only, though). Signed-off-by: Johannes Schindelin <[email protected]>
1 parent 18e8d51 commit 381a90a

File tree

1 file changed

+6
-5
lines changed

1 file changed

+6
-5
lines changed

compat/mingw.c

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,7 @@ static void process_phantom_symlinks(void)
316316
}
317317

318318
/* Normalizes NT paths as returned by some low-level APIs. */
319-
static wchar_t *normalize_ntpath(wchar_t *wbuf)
319+
static wchar_t *normalize_ntpath(wchar_t *wbuf, int strip_trailing_slashes)
320320
{
321321
int i;
322322
/* fix absolute path prefixes */
@@ -338,8 +338,9 @@ static wchar_t *normalize_ntpath(wchar_t *wbuf)
338338
if (wbuf[i] == '\\')
339339
wbuf[i] = '/';
340340
/* remove potential trailing slashes */
341-
while (i && wbuf[i - 1] == '/')
342-
wbuf[--i] = 0;
341+
if (strip_trailing_slashes)
342+
while (i && wbuf[i - 1] == '/')
343+
wbuf[--i] = 0;
343344
return wbuf;
344345
}
345346

@@ -621,7 +622,7 @@ int mingw_chdir(const char *dirname)
621622
CloseHandle(hnd);
622623
}
623624

624-
result = _wchdir(normalize_ntpath(wdirname));
625+
result = _wchdir(normalize_ntpath(wdirname, 0));
625626
current_directory_len = GetCurrentDirectoryW(0, NULL);
626627
return result;
627628
}
@@ -2290,7 +2291,7 @@ int readlink(const char *path, char *buf, size_t bufsiz)
22902291
* so convert to a (hopefully large enough) temporary buffer, then memcpy
22912292
* the requested number of bytes (including '\0' for robustness).
22922293
*/
2293-
if ((len = xwcstoutf(tmpbuf, normalize_ntpath(wbuf), MAX_LONG_PATH)) < 0)
2294+
if ((len = xwcstoutf(tmpbuf, normalize_ntpath(wbuf, 1), MAX_LONG_PATH)) < 0)
22942295
return -1;
22952296
memcpy(buf, tmpbuf, min(bufsiz, len + 1));
22962297
return min(bufsiz, len);

0 commit comments

Comments
 (0)