Skip to content

Commit 0a6c2b2

Browse files
kbleesvdye
authored andcommitted
mingw: support long paths
Windows paths are typically limited to MAX_PATH = 260 characters, even though the underlying NTFS file system supports paths up to 32,767 chars. This limitation is also evident in Windows Explorer, cmd.exe and many other applications (including IDEs). Particularly annoying is that most Windows APIs return bogus error codes if a relative path only barely exceeds MAX_PATH in conjunction with the current directory, e.g. ERROR_PATH_NOT_FOUND / ENOENT instead of the infinitely more helpful ERROR_FILENAME_EXCED_RANGE / ENAMETOOLONG. Many Windows wide char APIs support longer than MAX_PATH paths through the file namespace prefix ('\\?\' or '\\?\UNC\') followed by an absolute path. Notable exceptions include functions dealing with executables and the current directory (CreateProcess, LoadLibrary, Get/SetCurrentDirectory) as well as the entire shell API (ShellExecute, SHGetSpecialFolderPath...). Introduce a handle_long_path function to check the length of a specified path properly (and fail with ENAMETOOLONG), and to optionally expand long paths using the '\\?\' file namespace prefix. Short paths will not be modified, so we don't need to worry about device names (NUL, CON, AUX). Contrary to MSDN docs, the GetFullPathNameW function doesn't seem to be limited to MAX_PATH (at least not on Win7), so we can use it to do the heavy lifting of the conversion (translate '/' to '\', eliminate '.' and '..', and make an absolute path). Add long path error checking to xutftowcs_path for APIs with hard MAX_PATH limit. Add a new MAX_LONG_PATH constant and xutftowcs_long_path function for APIs that support long paths. While improved error checking is always active, long paths support must be explicitly enabled via 'core.longpaths' option. This is to prevent end users to shoot themselves in the foot by checking out files that Windows Explorer, cmd/bash or their favorite IDE cannot handle. Test suite: Test the case is when the full pathname length of a dir is close to 260 (MAX_PATH). Bug report and an original reproducer by Andrey Rogozhnikov: msysgit#122 (comment) [jes: adjusted test number to avoid conflicts, added support for chdir(), etc] Thanks-to: Martin W. Kirst <[email protected]> Thanks-to: Doug Kelly <[email protected]> Original-test-by: Andrey Rogozhnikov <[email protected]> Signed-off-by: Karsten Blees <[email protected]> Signed-off-by: Stepan Kasal <[email protected]> Signed-off-by: Johannes Schindelin <[email protected]>
1 parent 4909d3c commit 0a6c2b2

File tree

7 files changed

+329
-63
lines changed

7 files changed

+329
-63
lines changed

Documentation/config/core.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -661,6 +661,13 @@ core.fscache::
661661
Git for Windows uses this to bulk-read and cache lstat data of entire
662662
directories (instead of doing lstat file by file).
663663

664+
core.longpaths::
665+
Enable long path (> 260) support for builtin commands in Git for
666+
Windows. This is disabled by default, as long paths are not supported
667+
by Windows Explorer, cmd.exe and the Git for Windows tool chain
668+
(msys, bash, tcl, perl...). Only enable this if you know what you're
669+
doing and are prepared to live with a few quirks.
670+
664671
core.unsetenvvars::
665672
Windows-only: comma-separated list of environment variables'
666673
names that need to be unset before spawning any other process.

compat/mingw.c

Lines changed: 119 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,7 @@ static int core_restrict_inherited_handles = -1;
233233
static enum hide_dotfiles_type hide_dotfiles = HIDE_DOTFILES_DOTGITONLY;
234234
static char *unset_environment_variables;
235235
int core_fscache;
236+
int core_long_paths;
236237

237238
int mingw_core_config(const char *var, const char *value, void *cb)
238239
{
@@ -249,6 +250,11 @@ int mingw_core_config(const char *var, const char *value, void *cb)
249250
return 0;
250251
}
251252

253+
if (!strcmp(var, "core.longpaths")) {
254+
core_long_paths = git_config_bool(var, value);
255+
return 0;
256+
}
257+
252258
if (!strcmp(var, "core.unsetenvvars")) {
253259
free(unset_environment_variables);
254260
unset_environment_variables = xstrdup(value);
@@ -295,8 +301,8 @@ static wchar_t *normalize_ntpath(wchar_t *wbuf)
295301
int mingw_unlink(const char *pathname)
296302
{
297303
int ret, tries = 0;
298-
wchar_t wpathname[MAX_PATH];
299-
if (xutftowcs_path(wpathname, pathname) < 0)
304+
wchar_t wpathname[MAX_LONG_PATH];
305+
if (xutftowcs_long_path(wpathname, pathname) < 0)
300306
return -1;
301307

302308
if (DeleteFileW(wpathname))
@@ -328,7 +334,7 @@ static int is_dir_empty(const wchar_t *wpath)
328334
{
329335
WIN32_FIND_DATAW findbuf;
330336
HANDLE handle;
331-
wchar_t wbuf[MAX_PATH + 2];
337+
wchar_t wbuf[MAX_LONG_PATH + 2];
332338
wcscpy(wbuf, wpath);
333339
wcscat(wbuf, L"\\*");
334340
handle = FindFirstFileW(wbuf, &findbuf);
@@ -349,7 +355,7 @@ static int is_dir_empty(const wchar_t *wpath)
349355
int mingw_rmdir(const char *pathname)
350356
{
351357
int ret, tries = 0;
352-
wchar_t wpathname[MAX_PATH];
358+
wchar_t wpathname[MAX_LONG_PATH];
353359
struct stat st;
354360

355361
/*
@@ -371,7 +377,7 @@ int mingw_rmdir(const char *pathname)
371377
return -1;
372378
}
373379

374-
if (xutftowcs_path(wpathname, pathname) < 0)
380+
if (xutftowcs_long_path(wpathname, pathname) < 0)
375381
return -1;
376382

377383
while ((ret = _wrmdir(wpathname)) == -1 && tries < ARRAY_SIZE(delay)) {
@@ -450,15 +456,18 @@ static int set_hidden_flag(const wchar_t *path, int set)
450456
int mingw_mkdir(const char *path, int mode)
451457
{
452458
int ret;
453-
wchar_t wpath[MAX_PATH];
459+
wchar_t wpath[MAX_LONG_PATH];
454460

455461
if (!is_valid_win32_path(path, 0)) {
456462
errno = EINVAL;
457463
return -1;
458464
}
459465

460-
if (xutftowcs_path(wpath, path) < 0)
466+
/* CreateDirectoryW path limit is 248 (MAX_PATH - 8.3 file name) */
467+
if (xutftowcs_path_ex(wpath, path, MAX_LONG_PATH, -1, 248,
468+
core_long_paths) < 0)
461469
return -1;
470+
462471
ret = _wmkdir(wpath);
463472
if (!ret && needs_hiding(path))
464473
return set_hidden_flag(wpath, 1);
@@ -544,7 +553,7 @@ int mingw_open (const char *filename, int oflags, ...)
544553
va_list args;
545554
unsigned mode;
546555
int fd, create = (oflags & (O_CREAT | O_EXCL)) == (O_CREAT | O_EXCL);
547-
wchar_t wfilename[MAX_PATH];
556+
wchar_t wfilename[MAX_LONG_PATH];
548557
open_fn_t open_fn;
549558

550559
va_start(args, oflags);
@@ -563,7 +572,7 @@ int mingw_open (const char *filename, int oflags, ...)
563572

564573
if (filename && !strcmp(filename, "/dev/null"))
565574
wcscpy(wfilename, L"nul");
566-
else if (xutftowcs_path(wfilename, filename) < 0)
575+
else if (xutftowcs_long_path(wfilename, filename) < 0)
567576
return -1;
568577

569578
fd = open_fn(wfilename, oflags, mode);
@@ -621,14 +630,14 @@ FILE *mingw_fopen (const char *filename, const char *otype)
621630
{
622631
int hide = needs_hiding(filename);
623632
FILE *file;
624-
wchar_t wfilename[MAX_PATH], wotype[4];
633+
wchar_t wfilename[MAX_LONG_PATH], wotype[4];
625634
if (filename && !strcmp(filename, "/dev/null"))
626635
wcscpy(wfilename, L"nul");
627636
else if (!is_valid_win32_path(filename, 1)) {
628637
int create = otype && strchr(otype, 'w');
629638
errno = create ? EINVAL : ENOENT;
630639
return NULL;
631-
} else if (xutftowcs_path(wfilename, filename) < 0)
640+
} else if (xutftowcs_long_path(wfilename, filename) < 0)
632641
return NULL;
633642

634643
if (xutftowcs(wotype, otype, ARRAY_SIZE(wotype)) < 0)
@@ -650,14 +659,14 @@ FILE *mingw_freopen (const char *filename, const char *otype, FILE *stream)
650659
{
651660
int hide = needs_hiding(filename);
652661
FILE *file;
653-
wchar_t wfilename[MAX_PATH], wotype[4];
662+
wchar_t wfilename[MAX_LONG_PATH], wotype[4];
654663
if (filename && !strcmp(filename, "/dev/null"))
655664
wcscpy(wfilename, L"nul");
656665
else if (!is_valid_win32_path(filename, 1)) {
657666
int create = otype && strchr(otype, 'w');
658667
errno = create ? EINVAL : ENOENT;
659668
return NULL;
660-
} else if (xutftowcs_path(wfilename, filename) < 0)
669+
} else if (xutftowcs_long_path(wfilename, filename) < 0)
661670
return NULL;
662671

663672
if (xutftowcs(wotype, otype, ARRAY_SIZE(wotype)) < 0)
@@ -714,27 +723,33 @@ ssize_t mingw_write(int fd, const void *buf, size_t len)
714723

715724
int mingw_access(const char *filename, int mode)
716725
{
717-
wchar_t wfilename[MAX_PATH];
726+
wchar_t wfilename[MAX_LONG_PATH];
718727
if (!strcmp("nul", filename) || !strcmp("/dev/null", filename))
719728
return 0;
720-
if (xutftowcs_path(wfilename, filename) < 0)
729+
if (xutftowcs_long_path(wfilename, filename) < 0)
721730
return -1;
722731
/* X_OK is not supported by the MSVCRT version */
723732
return _waccess(wfilename, mode & ~X_OK);
724733
}
725734

735+
/* cached length of current directory for handle_long_path */
736+
static int current_directory_len = 0;
737+
726738
int mingw_chdir(const char *dirname)
727739
{
728-
wchar_t wdirname[MAX_PATH];
729-
if (xutftowcs_path(wdirname, dirname) < 0)
740+
int result;
741+
wchar_t wdirname[MAX_LONG_PATH];
742+
if (xutftowcs_long_path(wdirname, dirname) < 0)
730743
return -1;
731-
return _wchdir(wdirname);
744+
result = _wchdir(wdirname);
745+
current_directory_len = GetCurrentDirectoryW(0, NULL);
746+
return result;
732747
}
733748

734749
int mingw_chmod(const char *filename, int mode)
735750
{
736-
wchar_t wfilename[MAX_PATH];
737-
if (xutftowcs_path(wfilename, filename) < 0)
751+
wchar_t wfilename[MAX_LONG_PATH];
752+
if (xutftowcs_long_path(wfilename, filename) < 0)
738753
return -1;
739754
return _wchmod(wfilename, mode);
740755
}
@@ -782,8 +797,8 @@ static int has_valid_directory_prefix(wchar_t *wfilename)
782797
static int do_lstat(int follow, const char *file_name, struct stat *buf)
783798
{
784799
WIN32_FILE_ATTRIBUTE_DATA fdata;
785-
wchar_t wfilename[MAX_PATH];
786-
if (xutftowcs_path(wfilename, file_name) < 0)
800+
wchar_t wfilename[MAX_LONG_PATH];
801+
if (xutftowcs_long_path(wfilename, file_name) < 0)
787802
return -1;
788803

789804
if (GetFileAttributesExW(wfilename, GetFileExInfoStandard, &fdata)) {
@@ -954,10 +969,10 @@ int mingw_utime (const char *file_name, const struct utimbuf *times)
954969
FILETIME mft, aft;
955970
int rc;
956971
DWORD attrs;
957-
wchar_t wfilename[MAX_PATH];
972+
wchar_t wfilename[MAX_LONG_PATH];
958973
HANDLE osfilehandle;
959974

960-
if (xutftowcs_path(wfilename, file_name) < 0)
975+
if (xutftowcs_long_path(wfilename, file_name) < 0)
961976
return -1;
962977

963978
/* must have write permission */
@@ -1040,6 +1055,7 @@ char *mingw_mktemp(char *template)
10401055
wchar_t wtemplate[MAX_PATH];
10411056
int offset = 0;
10421057

1058+
/* we need to return the path, thus no long paths here! */
10431059
if (xutftowcs_path(wtemplate, template) < 0)
10441060
return NULL;
10451061

@@ -1677,6 +1693,10 @@ static pid_t mingw_spawnve_fd(const char *cmd, const char **argv, char **deltaen
16771693

16781694
if (*argv && !strcmp(cmd, *argv))
16791695
wcmd[0] = L'\0';
1696+
/*
1697+
* Paths to executables and to the current directory do not support
1698+
* long paths, therefore we cannot use xutftowcs_long_path() here.
1699+
*/
16801700
else if (xutftowcs_path(wcmd, cmd) < 0)
16811701
return -1;
16821702
if (dir && xutftowcs_path(wdir, dir) < 0)
@@ -2328,8 +2348,9 @@ int mingw_rename(const char *pold, const char *pnew)
23282348
{
23292349
DWORD attrs, gle;
23302350
int tries = 0;
2331-
wchar_t wpold[MAX_PATH], wpnew[MAX_PATH];
2332-
if (xutftowcs_path(wpold, pold) < 0 || xutftowcs_path(wpnew, pnew) < 0)
2351+
wchar_t wpold[MAX_LONG_PATH], wpnew[MAX_LONG_PATH];
2352+
if (xutftowcs_long_path(wpold, pold) < 0 ||
2353+
xutftowcs_long_path(wpnew, pnew) < 0)
23332354
return -1;
23342355

23352356
/*
@@ -2643,9 +2664,9 @@ int mingw_raise(int sig)
26432664

26442665
int link(const char *oldpath, const char *newpath)
26452666
{
2646-
wchar_t woldpath[MAX_PATH], wnewpath[MAX_PATH];
2647-
if (xutftowcs_path(woldpath, oldpath) < 0 ||
2648-
xutftowcs_path(wnewpath, newpath) < 0)
2667+
wchar_t woldpath[MAX_LONG_PATH], wnewpath[MAX_LONG_PATH];
2668+
if (xutftowcs_long_path(woldpath, oldpath) < 0 ||
2669+
xutftowcs_long_path(wnewpath, newpath) < 0)
26492670
return -1;
26502671

26512672
if (!CreateHardLinkW(wnewpath, woldpath, NULL)) {
@@ -2713,8 +2734,8 @@ int mingw_is_mount_point(struct strbuf *path)
27132734
{
27142735
WIN32_FIND_DATAW findbuf = { 0 };
27152736
HANDLE handle;
2716-
wchar_t wfilename[MAX_PATH];
2717-
int wlen = xutftowcs_path(wfilename, path->buf);
2737+
wchar_t wfilename[MAX_LONG_PATH];
2738+
int wlen = xutftowcs_long_path(wfilename, path->buf);
27182739
if (wlen < 0)
27192740
die(_("could not get long path for '%s'"), path->buf);
27202741

@@ -2859,9 +2880,9 @@ static size_t append_system_bin_dirs(char *path, size_t size)
28592880

28602881
static int is_system32_path(const char *path)
28612882
{
2862-
WCHAR system32[MAX_PATH], wpath[MAX_PATH];
2883+
WCHAR system32[MAX_LONG_PATH], wpath[MAX_LONG_PATH];
28632884

2864-
if (xutftowcs_path(wpath, path) < 0 ||
2885+
if (xutftowcs_long_path(wpath, path) < 0 ||
28652886
!GetSystemDirectoryW(system32, ARRAY_SIZE(system32)) ||
28662887
_wcsicmp(system32, wpath))
28672888
return 0;
@@ -3078,6 +3099,68 @@ int is_valid_win32_path(const char *path, int allow_literal_nul)
30783099
}
30793100
}
30803101

3102+
int handle_long_path(wchar_t *path, int len, int max_path, int expand)
3103+
{
3104+
int result;
3105+
wchar_t buf[MAX_LONG_PATH];
3106+
3107+
/*
3108+
* we don't need special handling if path is relative to the current
3109+
* directory, and current directory + path don't exceed the desired
3110+
* max_path limit. This should cover > 99 % of cases with minimal
3111+
* performance impact (git almost always uses relative paths).
3112+
*/
3113+
if ((len < 2 || (!is_dir_sep(path[0]) && path[1] != ':')) &&
3114+
(current_directory_len + len < max_path))
3115+
return len;
3116+
3117+
/*
3118+
* handle everything else:
3119+
* - absolute paths: "C:\dir\file"
3120+
* - absolute UNC paths: "\\server\share\dir\file"
3121+
* - absolute paths on current drive: "\dir\file"
3122+
* - relative paths on other drive: "X:file"
3123+
* - prefixed paths: "\\?\...", "\\.\..."
3124+
*/
3125+
3126+
/* convert to absolute path using GetFullPathNameW */
3127+
result = GetFullPathNameW(path, MAX_LONG_PATH, buf, NULL);
3128+
if (!result) {
3129+
errno = err_win_to_posix(GetLastError());
3130+
return -1;
3131+
}
3132+
3133+
/*
3134+
* return absolute path if it fits within max_path (even if
3135+
* "cwd + path" doesn't due to '..' components)
3136+
*/
3137+
if (result < max_path) {
3138+
wcscpy(path, buf);
3139+
return result;
3140+
}
3141+
3142+
/* error out if we shouldn't expand the path or buf is too small */
3143+
if (!expand || result >= MAX_LONG_PATH - 6) {
3144+
errno = ENAMETOOLONG;
3145+
return -1;
3146+
}
3147+
3148+
/* prefix full path with "\\?\" or "\\?\UNC\" */
3149+
if (buf[0] == '\\') {
3150+
/* ...unless already prefixed */
3151+
if (buf[1] == '\\' && (buf[2] == '?' || buf[2] == '.'))
3152+
return len;
3153+
3154+
wcscpy(path, L"\\\\?\\UNC\\");
3155+
wcscpy(path + 8, buf + 2);
3156+
return result + 6;
3157+
} else {
3158+
wcscpy(path, L"\\\\?\\");
3159+
wcscpy(path + 4, buf);
3160+
return result + 4;
3161+
}
3162+
}
3163+
30813164
#if !defined(_MSC_VER)
30823165
/*
30833166
* Disable MSVCRT command line wildcard expansion (__getmainargs called from
@@ -3239,6 +3322,9 @@ int wmain(int argc, const wchar_t **wargv)
32393322
/* initialize Unicode console */
32403323
winansi_init();
32413324

3325+
/* init length of current directory for handle_long_path */
3326+
current_directory_len = GetCurrentDirectoryW(0, NULL);
3327+
32423328
/* invoke the real main() using our utf8 version of argv. */
32433329
exit_status = main(argc, argv);
32443330

0 commit comments

Comments
 (0)