Skip to content

Commit 8370ca5

Browse files
kbleesdscho
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 ffa7715 commit 8370ca5

File tree

7 files changed

+346
-64
lines changed

7 files changed

+346
-64
lines changed

Documentation/config/core.txt

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

694+
core.longpaths::
695+
Enable long path (> 260) support for builtin commands in Git for
696+
Windows. This is disabled by default, as long paths are not supported
697+
by Windows Explorer, cmd.exe and the Git for Windows tool chain
698+
(msys, bash, tcl, perl...). Only enable this if you know what you're
699+
doing and are prepared to live with a few quirks.
700+
694701
core.unsetenvvars::
695702
Windows-only: comma-separated list of environment variables'
696703
names that need to be unset before spawning any other process.

compat/mingw.c

Lines changed: 135 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,27 @@ static enum hide_dotfiles_type hide_dotfiles = HIDE_DOTFILES_DOTGITONLY;
248248
static char *unset_environment_variables;
249249
int core_fscache;
250250

251+
int are_long_paths_enabled(void)
252+
{
253+
/* default to `false` during initialization */
254+
static const int fallback = 0;
255+
256+
static int enabled = -1;
257+
258+
if (enabled < 0) {
259+
/* avoid infinite recursion */
260+
if (!the_repository)
261+
return fallback;
262+
263+
if (the_repository->config &&
264+
the_repository->config->hash_initialized &&
265+
git_config_get_bool("core.longpaths", &enabled) < 0)
266+
enabled = 0;
267+
}
268+
269+
return enabled < 0 ? fallback : enabled;
270+
}
271+
251272
int mingw_core_config(const char *var, const char *value,
252273
const struct config_context *ctx UNUSED,
253274
void *cb UNUSED)
@@ -313,8 +334,8 @@ static wchar_t *normalize_ntpath(wchar_t *wbuf)
313334
int mingw_unlink(const char *pathname)
314335
{
315336
int ret, tries = 0;
316-
wchar_t wpathname[MAX_PATH];
317-
if (xutftowcs_path(wpathname, pathname) < 0)
337+
wchar_t wpathname[MAX_LONG_PATH];
338+
if (xutftowcs_long_path(wpathname, pathname) < 0)
318339
return -1;
319340

320341
if (DeleteFileW(wpathname))
@@ -346,7 +367,7 @@ static int is_dir_empty(const wchar_t *wpath)
346367
{
347368
WIN32_FIND_DATAW findbuf;
348369
HANDLE handle;
349-
wchar_t wbuf[MAX_PATH + 2];
370+
wchar_t wbuf[MAX_LONG_PATH + 2];
350371
wcscpy(wbuf, wpath);
351372
wcscat(wbuf, L"\\*");
352373
handle = FindFirstFileW(wbuf, &findbuf);
@@ -367,7 +388,7 @@ static int is_dir_empty(const wchar_t *wpath)
367388
int mingw_rmdir(const char *pathname)
368389
{
369390
int ret, tries = 0;
370-
wchar_t wpathname[MAX_PATH];
391+
wchar_t wpathname[MAX_LONG_PATH];
371392
struct stat st;
372393

373394
/*
@@ -389,7 +410,7 @@ int mingw_rmdir(const char *pathname)
389410
return -1;
390411
}
391412

392-
if (xutftowcs_path(wpathname, pathname) < 0)
413+
if (xutftowcs_long_path(wpathname, pathname) < 0)
393414
return -1;
394415

395416
while ((ret = _wrmdir(wpathname)) == -1 && tries < ARRAY_SIZE(delay)) {
@@ -468,15 +489,18 @@ static int set_hidden_flag(const wchar_t *path, int set)
468489
int mingw_mkdir(const char *path, int mode UNUSED)
469490
{
470491
int ret;
471-
wchar_t wpath[MAX_PATH];
492+
wchar_t wpath[MAX_LONG_PATH];
472493

473494
if (!is_valid_win32_path(path, 0)) {
474495
errno = EINVAL;
475496
return -1;
476497
}
477498

478-
if (xutftowcs_path(wpath, path) < 0)
499+
/* CreateDirectoryW path limit is 248 (MAX_PATH - 8.3 file name) */
500+
if (xutftowcs_path_ex(wpath, path, MAX_LONG_PATH, -1, 248,
501+
are_long_paths_enabled()) < 0)
479502
return -1;
503+
480504
ret = _wmkdir(wpath);
481505
if (!ret && needs_hiding(path))
482506
return set_hidden_flag(wpath, 1);
@@ -563,7 +587,7 @@ int mingw_open (const char *filename, int oflags, ...)
563587
va_list args;
564588
unsigned mode;
565589
int fd, create = (oflags & (O_CREAT | O_EXCL)) == (O_CREAT | O_EXCL);
566-
wchar_t wfilename[MAX_PATH];
590+
wchar_t wfilename[MAX_LONG_PATH];
567591
open_fn_t open_fn;
568592

569593
va_start(args, oflags);
@@ -591,7 +615,7 @@ int mingw_open (const char *filename, int oflags, ...)
591615

592616
if (filename && !strcmp(filename, "/dev/null"))
593617
wcscpy(wfilename, L"nul");
594-
else if (xutftowcs_path(wfilename, filename) < 0)
618+
else if (xutftowcs_long_path(wfilename, filename) < 0)
595619
return -1;
596620

597621
fd = open_fn(wfilename, oflags, mode);
@@ -649,14 +673,14 @@ FILE *mingw_fopen (const char *filename, const char *otype)
649673
{
650674
int hide = needs_hiding(filename);
651675
FILE *file;
652-
wchar_t wfilename[MAX_PATH], wotype[4];
676+
wchar_t wfilename[MAX_LONG_PATH], wotype[4];
653677
if (filename && !strcmp(filename, "/dev/null"))
654678
wcscpy(wfilename, L"nul");
655679
else if (!is_valid_win32_path(filename, 1)) {
656680
int create = otype && strchr(otype, 'w');
657681
errno = create ? EINVAL : ENOENT;
658682
return NULL;
659-
} else if (xutftowcs_path(wfilename, filename) < 0)
683+
} else if (xutftowcs_long_path(wfilename, filename) < 0)
660684
return NULL;
661685

662686
if (xutftowcs(wotype, otype, ARRAY_SIZE(wotype)) < 0)
@@ -678,14 +702,14 @@ FILE *mingw_freopen (const char *filename, const char *otype, FILE *stream)
678702
{
679703
int hide = needs_hiding(filename);
680704
FILE *file;
681-
wchar_t wfilename[MAX_PATH], wotype[4];
705+
wchar_t wfilename[MAX_LONG_PATH], wotype[4];
682706
if (filename && !strcmp(filename, "/dev/null"))
683707
wcscpy(wfilename, L"nul");
684708
else if (!is_valid_win32_path(filename, 1)) {
685709
int create = otype && strchr(otype, 'w');
686710
errno = create ? EINVAL : ENOENT;
687711
return NULL;
688-
} else if (xutftowcs_path(wfilename, filename) < 0)
712+
} else if (xutftowcs_long_path(wfilename, filename) < 0)
689713
return NULL;
690714

691715
if (xutftowcs(wotype, otype, ARRAY_SIZE(wotype)) < 0)
@@ -735,7 +759,7 @@ ssize_t mingw_write(int fd, const void *buf, size_t len)
735759
HANDLE h = (HANDLE) _get_osfhandle(fd);
736760
if (GetFileType(h) != FILE_TYPE_PIPE) {
737761
if (orig == EINVAL) {
738-
wchar_t path[MAX_PATH];
762+
wchar_t path[MAX_LONG_PATH];
739763
DWORD ret = GetFinalPathNameByHandleW(h, path,
740764
ARRAY_SIZE(path), 0);
741765
UINT drive_type = ret > 0 && ret < ARRAY_SIZE(path) ?
@@ -772,27 +796,33 @@ ssize_t mingw_write(int fd, const void *buf, size_t len)
772796

773797
int mingw_access(const char *filename, int mode)
774798
{
775-
wchar_t wfilename[MAX_PATH];
799+
wchar_t wfilename[MAX_LONG_PATH];
776800
if (!strcmp("nul", filename) || !strcmp("/dev/null", filename))
777801
return 0;
778-
if (xutftowcs_path(wfilename, filename) < 0)
802+
if (xutftowcs_long_path(wfilename, filename) < 0)
779803
return -1;
780804
/* X_OK is not supported by the MSVCRT version */
781805
return _waccess(wfilename, mode & ~X_OK);
782806
}
783807

808+
/* cached length of current directory for handle_long_path */
809+
static int current_directory_len = 0;
810+
784811
int mingw_chdir(const char *dirname)
785812
{
786-
wchar_t wdirname[MAX_PATH];
787-
if (xutftowcs_path(wdirname, dirname) < 0)
813+
int result;
814+
wchar_t wdirname[MAX_LONG_PATH];
815+
if (xutftowcs_long_path(wdirname, dirname) < 0)
788816
return -1;
789-
return _wchdir(wdirname);
817+
result = _wchdir(wdirname);
818+
current_directory_len = GetCurrentDirectoryW(0, NULL);
819+
return result;
790820
}
791821

792822
int mingw_chmod(const char *filename, int mode)
793823
{
794-
wchar_t wfilename[MAX_PATH];
795-
if (xutftowcs_path(wfilename, filename) < 0)
824+
wchar_t wfilename[MAX_LONG_PATH];
825+
if (xutftowcs_long_path(wfilename, filename) < 0)
796826
return -1;
797827
return _wchmod(wfilename, mode);
798828
}
@@ -840,8 +870,8 @@ static int has_valid_directory_prefix(wchar_t *wfilename)
840870
static int do_lstat(int follow, const char *file_name, struct stat *buf)
841871
{
842872
WIN32_FILE_ATTRIBUTE_DATA fdata;
843-
wchar_t wfilename[MAX_PATH];
844-
if (xutftowcs_path(wfilename, file_name) < 0)
873+
wchar_t wfilename[MAX_LONG_PATH];
874+
if (xutftowcs_long_path(wfilename, file_name) < 0)
845875
return -1;
846876

847877
if (GetFileAttributesExW(wfilename, GetFileExInfoStandard, &fdata)) {
@@ -1012,10 +1042,10 @@ int mingw_utime (const char *file_name, const struct utimbuf *times)
10121042
FILETIME mft, aft;
10131043
int rc;
10141044
DWORD attrs;
1015-
wchar_t wfilename[MAX_PATH];
1045+
wchar_t wfilename[MAX_LONG_PATH];
10161046
HANDLE osfilehandle;
10171047

1018-
if (xutftowcs_path(wfilename, file_name) < 0)
1048+
if (xutftowcs_long_path(wfilename, file_name) < 0)
10191049
return -1;
10201050

10211051
/* must have write permission */
@@ -1098,6 +1128,7 @@ char *mingw_mktemp(char *template)
10981128
wchar_t wtemplate[MAX_PATH];
10991129
int offset = 0;
11001130

1131+
/* we need to return the path, thus no long paths here! */
11011132
if (xutftowcs_path(wtemplate, template) < 0)
11021133
return NULL;
11031134

@@ -1750,6 +1781,10 @@ static pid_t mingw_spawnve_fd(const char *cmd, const char **argv, char **deltaen
17501781

17511782
if (*argv && !strcmp(cmd, *argv))
17521783
wcmd[0] = L'\0';
1784+
/*
1785+
* Paths to executables and to the current directory do not support
1786+
* long paths, therefore we cannot use xutftowcs_long_path() here.
1787+
*/
17531788
else if (xutftowcs_path(wcmd, cmd) < 0)
17541789
return -1;
17551790
if (dir && xutftowcs_path(wdir, dir) < 0)
@@ -2401,8 +2436,9 @@ int mingw_rename(const char *pold, const char *pnew)
24012436
{
24022437
DWORD attrs, gle;
24032438
int tries = 0;
2404-
wchar_t wpold[MAX_PATH], wpnew[MAX_PATH];
2405-
if (xutftowcs_path(wpold, pold) < 0 || xutftowcs_path(wpnew, pnew) < 0)
2439+
wchar_t wpold[MAX_LONG_PATH], wpnew[MAX_LONG_PATH];
2440+
if (xutftowcs_long_path(wpold, pold) < 0 ||
2441+
xutftowcs_long_path(wpnew, pnew) < 0)
24062442
return -1;
24072443

24082444
/*
@@ -2720,9 +2756,9 @@ int mingw_raise(int sig)
27202756

27212757
int link(const char *oldpath, const char *newpath)
27222758
{
2723-
wchar_t woldpath[MAX_PATH], wnewpath[MAX_PATH];
2724-
if (xutftowcs_path(woldpath, oldpath) < 0 ||
2725-
xutftowcs_path(wnewpath, newpath) < 0)
2759+
wchar_t woldpath[MAX_LONG_PATH], wnewpath[MAX_LONG_PATH];
2760+
if (xutftowcs_long_path(woldpath, oldpath) < 0 ||
2761+
xutftowcs_long_path(wnewpath, newpath) < 0)
27262762
return -1;
27272763

27282764
if (!CreateHardLinkW(wnewpath, woldpath, NULL)) {
@@ -2790,8 +2826,8 @@ int mingw_is_mount_point(struct strbuf *path)
27902826
{
27912827
WIN32_FIND_DATAW findbuf = { 0 };
27922828
HANDLE handle;
2793-
wchar_t wfilename[MAX_PATH];
2794-
int wlen = xutftowcs_path(wfilename, path->buf);
2829+
wchar_t wfilename[MAX_LONG_PATH];
2830+
int wlen = xutftowcs_long_path(wfilename, path->buf);
27952831
if (wlen < 0)
27962832
die(_("could not get long path for '%s'"), path->buf);
27972833

@@ -2936,9 +2972,9 @@ static size_t append_system_bin_dirs(char *path, size_t size)
29362972

29372973
static int is_system32_path(const char *path)
29382974
{
2939-
WCHAR system32[MAX_PATH], wpath[MAX_PATH];
2975+
WCHAR system32[MAX_LONG_PATH], wpath[MAX_LONG_PATH];
29402976

2941-
if (xutftowcs_path(wpath, path) < 0 ||
2977+
if (xutftowcs_long_path(wpath, path) < 0 ||
29422978
!GetSystemDirectoryW(system32, ARRAY_SIZE(system32)) ||
29432979
_wcsicmp(system32, wpath))
29442980
return 0;
@@ -3350,6 +3386,68 @@ int is_valid_win32_path(const char *path, int allow_literal_nul)
33503386
}
33513387
}
33523388

3389+
int handle_long_path(wchar_t *path, int len, int max_path, int expand)
3390+
{
3391+
int result;
3392+
wchar_t buf[MAX_LONG_PATH];
3393+
3394+
/*
3395+
* we don't need special handling if path is relative to the current
3396+
* directory, and current directory + path don't exceed the desired
3397+
* max_path limit. This should cover > 99 % of cases with minimal
3398+
* performance impact (git almost always uses relative paths).
3399+
*/
3400+
if ((len < 2 || (!is_dir_sep(path[0]) && path[1] != ':')) &&
3401+
(current_directory_len + len < max_path))
3402+
return len;
3403+
3404+
/*
3405+
* handle everything else:
3406+
* - absolute paths: "C:\dir\file"
3407+
* - absolute UNC paths: "\\server\share\dir\file"
3408+
* - absolute paths on current drive: "\dir\file"
3409+
* - relative paths on other drive: "X:file"
3410+
* - prefixed paths: "\\?\...", "\\.\..."
3411+
*/
3412+
3413+
/* convert to absolute path using GetFullPathNameW */
3414+
result = GetFullPathNameW(path, MAX_LONG_PATH, buf, NULL);
3415+
if (!result) {
3416+
errno = err_win_to_posix(GetLastError());
3417+
return -1;
3418+
}
3419+
3420+
/*
3421+
* return absolute path if it fits within max_path (even if
3422+
* "cwd + path" doesn't due to '..' components)
3423+
*/
3424+
if (result < max_path) {
3425+
wcscpy(path, buf);
3426+
return result;
3427+
}
3428+
3429+
/* error out if we shouldn't expand the path or buf is too small */
3430+
if (!expand || result >= MAX_LONG_PATH - 6) {
3431+
errno = ENAMETOOLONG;
3432+
return -1;
3433+
}
3434+
3435+
/* prefix full path with "\\?\" or "\\?\UNC\" */
3436+
if (buf[0] == '\\') {
3437+
/* ...unless already prefixed */
3438+
if (buf[1] == '\\' && (buf[2] == '?' || buf[2] == '.'))
3439+
return len;
3440+
3441+
wcscpy(path, L"\\\\?\\UNC\\");
3442+
wcscpy(path + 8, buf + 2);
3443+
return result + 6;
3444+
} else {
3445+
wcscpy(path, L"\\\\?\\");
3446+
wcscpy(path + 4, buf);
3447+
return result + 4;
3448+
}
3449+
}
3450+
33533451
#if !defined(_MSC_VER)
33543452
/*
33553453
* Disable MSVCRT command line wildcard expansion (__getmainargs called from
@@ -3512,6 +3610,9 @@ int wmain(int argc, const wchar_t **wargv)
35123610
/* initialize Unicode console */
35133611
winansi_init();
35143612

3613+
/* init length of current directory for handle_long_path */
3614+
current_directory_len = GetCurrentDirectoryW(0, NULL);
3615+
35153616
/* invoke the real main() using our utf8 version of argv. */
35163617
exit_status = main(argc, argv);
35173618

0 commit comments

Comments
 (0)