Skip to content

Commit 2bd6fef

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 0308a96 commit 2bd6fef

File tree

7 files changed

+327
-61
lines changed

7 files changed

+327
-61
lines changed

Documentation/config/core.txt

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

573+
core.longpaths::
574+
Enable long path (> 260) support for builtin commands in Git for
575+
Windows. This is disabled by default, as long paths are not supported
576+
by Windows Explorer, cmd.exe and the Git for Windows tool chain
577+
(msys, bash, tcl, perl...). Only enable this if you know what you're
578+
doing and are prepared to live with a few quirks.
579+
573580
core.unsetenvvars::
574581
Windows-only: comma-separated list of environment variables'
575582
names that need to be unset before spawning any other process.

compat/mingw.c

Lines changed: 117 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,7 @@ static int core_restrict_inherited_handles = -1;
231231
static enum hide_dotfiles_type hide_dotfiles = HIDE_DOTFILES_DOTGITONLY;
232232
static char *unset_environment_variables;
233233
int core_fscache;
234+
int core_long_paths;
234235

235236
int mingw_core_config(const char *var, const char *value, void *cb)
236237
{
@@ -247,6 +248,11 @@ int mingw_core_config(const char *var, const char *value, void *cb)
247248
return 0;
248249
}
249250

251+
if (!strcmp(var, "core.longpaths")) {
252+
core_long_paths = git_config_bool(var, value);
253+
return 0;
254+
}
255+
250256
if (!strcmp(var, "core.unsetenvvars")) {
251257
free(unset_environment_variables);
252258
unset_environment_variables = xstrdup(value);
@@ -293,8 +299,8 @@ static wchar_t *normalize_ntpath(wchar_t *wbuf)
293299
int mingw_unlink(const char *pathname)
294300
{
295301
int ret, tries = 0;
296-
wchar_t wpathname[MAX_PATH];
297-
if (xutftowcs_path(wpathname, pathname) < 0)
302+
wchar_t wpathname[MAX_LONG_PATH];
303+
if (xutftowcs_long_path(wpathname, pathname) < 0)
298304
return -1;
299305

300306
if (DeleteFileW(wpathname))
@@ -326,7 +332,7 @@ static int is_dir_empty(const wchar_t *wpath)
326332
{
327333
WIN32_FIND_DATAW findbuf;
328334
HANDLE handle;
329-
wchar_t wbuf[MAX_PATH + 2];
335+
wchar_t wbuf[MAX_LONG_PATH + 2];
330336
wcscpy(wbuf, wpath);
331337
wcscat(wbuf, L"\\*");
332338
handle = FindFirstFileW(wbuf, &findbuf);
@@ -347,8 +353,8 @@ static int is_dir_empty(const wchar_t *wpath)
347353
int mingw_rmdir(const char *pathname)
348354
{
349355
int ret, tries = 0;
350-
wchar_t wpathname[MAX_PATH];
351-
if (xutftowcs_path(wpathname, pathname) < 0)
356+
wchar_t wpathname[MAX_LONG_PATH];
357+
if (xutftowcs_long_path(wpathname, pathname) < 0)
352358
return -1;
353359

354360
while ((ret = _wrmdir(wpathname)) == -1 && tries < ARRAY_SIZE(delay)) {
@@ -425,15 +431,18 @@ static int set_hidden_flag(const wchar_t *path, int set)
425431
int mingw_mkdir(const char *path, int mode)
426432
{
427433
int ret;
428-
wchar_t wpath[MAX_PATH];
434+
wchar_t wpath[MAX_LONG_PATH];
429435

430436
if (!is_valid_win32_path(path, 0)) {
431437
errno = EINVAL;
432438
return -1;
433439
}
434440

435-
if (xutftowcs_path(wpath, path) < 0)
441+
/* CreateDirectoryW path limit is 248 (MAX_PATH - 8.3 file name) */
442+
if (xutftowcs_path_ex(wpath, path, MAX_LONG_PATH, -1, 248,
443+
core_long_paths) < 0)
436444
return -1;
445+
437446
ret = _wmkdir(wpath);
438447
if (!ret && needs_hiding(path))
439448
return set_hidden_flag(wpath, 1);
@@ -519,7 +528,7 @@ int mingw_open (const char *filename, int oflags, ...)
519528
va_list args;
520529
unsigned mode;
521530
int fd, create = (oflags & (O_CREAT | O_EXCL)) == (O_CREAT | O_EXCL);
522-
wchar_t wfilename[MAX_PATH];
531+
wchar_t wfilename[MAX_LONG_PATH];
523532
open_fn_t open_fn;
524533

525534
va_start(args, oflags);
@@ -538,7 +547,7 @@ int mingw_open (const char *filename, int oflags, ...)
538547

539548
if (filename && !strcmp(filename, "/dev/null"))
540549
wcscpy(wfilename, L"nul");
541-
else if (xutftowcs_path(wfilename, filename) < 0)
550+
else if (xutftowcs_long_path(wfilename, filename) < 0)
542551
return -1;
543552

544553
fd = open_fn(wfilename, oflags, mode);
@@ -596,14 +605,14 @@ FILE *mingw_fopen (const char *filename, const char *otype)
596605
{
597606
int hide = needs_hiding(filename);
598607
FILE *file;
599-
wchar_t wfilename[MAX_PATH], wotype[4];
608+
wchar_t wfilename[MAX_LONG_PATH], wotype[4];
600609
if (filename && !strcmp(filename, "/dev/null"))
601610
wcscpy(wfilename, L"nul");
602611
else if (!is_valid_win32_path(filename, 1)) {
603612
int create = otype && strchr(otype, 'w');
604613
errno = create ? EINVAL : ENOENT;
605614
return NULL;
606-
} else if (xutftowcs_path(wfilename, filename) < 0)
615+
} else if (xutftowcs_long_path(wfilename, filename) < 0)
607616
return NULL;
608617

609618
if (xutftowcs(wotype, otype, ARRAY_SIZE(wotype)) < 0)
@@ -625,14 +634,14 @@ FILE *mingw_freopen (const char *filename, const char *otype, FILE *stream)
625634
{
626635
int hide = needs_hiding(filename);
627636
FILE *file;
628-
wchar_t wfilename[MAX_PATH], wotype[4];
637+
wchar_t wfilename[MAX_LONG_PATH], wotype[4];
629638
if (filename && !strcmp(filename, "/dev/null"))
630639
wcscpy(wfilename, L"nul");
631640
else if (!is_valid_win32_path(filename, 1)) {
632641
int create = otype && strchr(otype, 'w');
633642
errno = create ? EINVAL : ENOENT;
634643
return NULL;
635-
} else if (xutftowcs_path(wfilename, filename) < 0)
644+
} else if (xutftowcs_long_path(wfilename, filename) < 0)
636645
return NULL;
637646

638647
if (xutftowcs(wotype, otype, ARRAY_SIZE(wotype)) < 0)
@@ -689,25 +698,31 @@ ssize_t mingw_write(int fd, const void *buf, size_t len)
689698

690699
int mingw_access(const char *filename, int mode)
691700
{
692-
wchar_t wfilename[MAX_PATH];
693-
if (xutftowcs_path(wfilename, filename) < 0)
701+
wchar_t wfilename[MAX_LONG_PATH];
702+
if (xutftowcs_long_path(wfilename, filename) < 0)
694703
return -1;
695704
/* X_OK is not supported by the MSVCRT version */
696705
return _waccess(wfilename, mode & ~X_OK);
697706
}
698707

708+
/* cached length of current directory for handle_long_path */
709+
static int current_directory_len = 0;
710+
699711
int mingw_chdir(const char *dirname)
700712
{
701-
wchar_t wdirname[MAX_PATH];
702-
if (xutftowcs_path(wdirname, dirname) < 0)
713+
int result;
714+
wchar_t wdirname[MAX_LONG_PATH];
715+
if (xutftowcs_long_path(wdirname, dirname) < 0)
703716
return -1;
704-
return _wchdir(wdirname);
717+
result = _wchdir(wdirname);
718+
current_directory_len = GetCurrentDirectoryW(0, NULL);
719+
return result;
705720
}
706721

707722
int mingw_chmod(const char *filename, int mode)
708723
{
709-
wchar_t wfilename[MAX_PATH];
710-
if (xutftowcs_path(wfilename, filename) < 0)
724+
wchar_t wfilename[MAX_LONG_PATH];
725+
if (xutftowcs_long_path(wfilename, filename) < 0)
711726
return -1;
712727
return _wchmod(wfilename, mode);
713728
}
@@ -755,8 +770,8 @@ static int has_valid_directory_prefix(wchar_t *wfilename)
755770
static int do_lstat(int follow, const char *file_name, struct stat *buf)
756771
{
757772
WIN32_FILE_ATTRIBUTE_DATA fdata;
758-
wchar_t wfilename[MAX_PATH];
759-
if (xutftowcs_path(wfilename, file_name) < 0)
773+
wchar_t wfilename[MAX_LONG_PATH];
774+
if (xutftowcs_long_path(wfilename, file_name) < 0)
760775
return -1;
761776

762777
if (GetFileAttributesExW(wfilename, GetFileExInfoStandard, &fdata)) {
@@ -927,8 +942,8 @@ int mingw_utime (const char *file_name, const struct utimbuf *times)
927942
FILETIME mft, aft;
928943
int fh, rc;
929944
DWORD attrs;
930-
wchar_t wfilename[MAX_PATH];
931-
if (xutftowcs_path(wfilename, file_name) < 0)
945+
wchar_t wfilename[MAX_LONG_PATH];
946+
if (xutftowcs_long_path(wfilename, file_name) < 0)
932947
return -1;
933948

934949
/* must have write permission */
@@ -998,6 +1013,7 @@ char *mingw_mktemp(char *template)
9981013
wchar_t wtemplate[MAX_PATH];
9991014
int offset = 0;
10001015

1016+
/* we need to return the path, thus no long paths here! */
10011017
if (xutftowcs_path(wtemplate, template) < 0)
10021018
return NULL;
10031019

@@ -1629,6 +1645,10 @@ static pid_t mingw_spawnve_fd(const char *cmd, const char **argv, char **deltaen
16291645

16301646
if (*argv && !strcmp(cmd, *argv))
16311647
wcmd[0] = L'\0';
1648+
/*
1649+
* Paths to executables and to the current directory do not support
1650+
* long paths, therefore we cannot use xutftowcs_long_path() here.
1651+
*/
16321652
else if (xutftowcs_path(wcmd, cmd) < 0)
16331653
return -1;
16341654
if (dir && xutftowcs_path(wdir, dir) < 0)
@@ -2280,8 +2300,9 @@ int mingw_rename(const char *pold, const char *pnew)
22802300
{
22812301
DWORD attrs, gle;
22822302
int tries = 0;
2283-
wchar_t wpold[MAX_PATH], wpnew[MAX_PATH];
2284-
if (xutftowcs_path(wpold, pold) < 0 || xutftowcs_path(wpnew, pnew) < 0)
2303+
wchar_t wpold[MAX_LONG_PATH], wpnew[MAX_LONG_PATH];
2304+
if (xutftowcs_long_path(wpold, pold) < 0 ||
2305+
xutftowcs_long_path(wpnew, pnew) < 0)
22852306
return -1;
22862307

22872308
/*
@@ -2595,9 +2616,9 @@ int mingw_raise(int sig)
25952616

25962617
int link(const char *oldpath, const char *newpath)
25972618
{
2598-
wchar_t woldpath[MAX_PATH], wnewpath[MAX_PATH];
2599-
if (xutftowcs_path(woldpath, oldpath) < 0 ||
2600-
xutftowcs_path(wnewpath, newpath) < 0)
2619+
wchar_t woldpath[MAX_LONG_PATH], wnewpath[MAX_LONG_PATH];
2620+
if (xutftowcs_long_path(woldpath, oldpath) < 0 ||
2621+
xutftowcs_long_path(wnewpath, newpath) < 0)
26012622
return -1;
26022623

26032624
if (!CreateHardLinkW(wnewpath, woldpath, NULL)) {
@@ -2665,8 +2686,8 @@ int mingw_is_mount_point(struct strbuf *path)
26652686
{
26662687
WIN32_FIND_DATAW findbuf = { 0 };
26672688
HANDLE handle;
2668-
wchar_t wfilename[MAX_PATH];
2669-
int wlen = xutftowcs_path(wfilename, path->buf);
2689+
wchar_t wfilename[MAX_LONG_PATH];
2690+
int wlen = xutftowcs_long_path(wfilename, path->buf);
26702691
if (wlen < 0)
26712692
die(_("could not get long path for '%s'"), path->buf);
26722693

@@ -3030,6 +3051,68 @@ int is_valid_win32_path(const char *path, int allow_literal_nul)
30303051
}
30313052
}
30323053

3054+
int handle_long_path(wchar_t *path, int len, int max_path, int expand)
3055+
{
3056+
int result;
3057+
wchar_t buf[MAX_LONG_PATH];
3058+
3059+
/*
3060+
* we don't need special handling if path is relative to the current
3061+
* directory, and current directory + path don't exceed the desired
3062+
* max_path limit. This should cover > 99 % of cases with minimal
3063+
* performance impact (git almost always uses relative paths).
3064+
*/
3065+
if ((len < 2 || (!is_dir_sep(path[0]) && path[1] != ':')) &&
3066+
(current_directory_len + len < max_path))
3067+
return len;
3068+
3069+
/*
3070+
* handle everything else:
3071+
* - absolute paths: "C:\dir\file"
3072+
* - absolute UNC paths: "\\server\share\dir\file"
3073+
* - absolute paths on current drive: "\dir\file"
3074+
* - relative paths on other drive: "X:file"
3075+
* - prefixed paths: "\\?\...", "\\.\..."
3076+
*/
3077+
3078+
/* convert to absolute path using GetFullPathNameW */
3079+
result = GetFullPathNameW(path, MAX_LONG_PATH, buf, NULL);
3080+
if (!result) {
3081+
errno = err_win_to_posix(GetLastError());
3082+
return -1;
3083+
}
3084+
3085+
/*
3086+
* return absolute path if it fits within max_path (even if
3087+
* "cwd + path" doesn't due to '..' components)
3088+
*/
3089+
if (result < max_path) {
3090+
wcscpy(path, buf);
3091+
return result;
3092+
}
3093+
3094+
/* error out if we shouldn't expand the path or buf is too small */
3095+
if (!expand || result >= MAX_LONG_PATH - 6) {
3096+
errno = ENAMETOOLONG;
3097+
return -1;
3098+
}
3099+
3100+
/* prefix full path with "\\?\" or "\\?\UNC\" */
3101+
if (buf[0] == '\\') {
3102+
/* ...unless already prefixed */
3103+
if (buf[1] == '\\' && (buf[2] == '?' || buf[2] == '.'))
3104+
return len;
3105+
3106+
wcscpy(path, L"\\\\?\\UNC\\");
3107+
wcscpy(path + 8, buf + 2);
3108+
return result + 6;
3109+
} else {
3110+
wcscpy(path, L"\\\\?\\");
3111+
wcscpy(path + 4, buf);
3112+
return result + 4;
3113+
}
3114+
}
3115+
30333116
#if !defined(_MSC_VER)
30343117
/*
30353118
* Disable MSVCRT command line wildcard expansion (__getmainargs called from
@@ -3191,6 +3274,9 @@ int wmain(int argc, const wchar_t **wargv)
31913274
/* initialize Unicode console */
31923275
winansi_init();
31933276

3277+
/* init length of current directory for handle_long_path */
3278+
current_directory_len = GetCurrentDirectoryW(0, NULL);
3279+
31943280
/* invoke the real main() using our utf8 version of argv. */
31953281
exit_status = main(argc, argv);
31963282

0 commit comments

Comments
 (0)