Skip to content

Commit c932cc5

Browse files
dschoGit for Windows Build Agent
authored andcommitted
Win32: 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] Thanks-to: Martin W. Kirst <[email protected]> Thanks-to: Doug Kelly <[email protected]> Signed-off-by: Karsten Blees <[email protected]> Original-test-by: Andrey Rogozhnikov <[email protected]> Signed-off-by: Stepan Kasal <[email protected]> Signed-off-by: Johannes Schindelin <[email protected]>
1 parent ed330d2 commit c932cc5

File tree

7 files changed

+319
-55
lines changed

7 files changed

+319
-55
lines changed

Documentation/config.txt

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

769+
core.longpaths::
770+
Enable long path (> 260) support for builtin commands in Git for
771+
Windows. This is disabled by default, as long paths are not supported
772+
by Windows Explorer, cmd.exe and the Git for Windows tool chain
773+
(msys, bash, tcl, perl...). Only enable this if you know what you're
774+
doing and are prepared to live with a few quirks.
775+
769776
core.unsetenvvars::
770777
EXPERIMENTAL, Windows-only: comma-separated list of environment
771778
variables' names that need to be unset before spawning any other

compat/mingw.c

Lines changed: 111 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,7 @@ enum hide_dotfiles_type {
212212
static enum hide_dotfiles_type hide_dotfiles = HIDE_DOTFILES_DOTGITONLY;
213213
static char *unset_environment_variables;
214214
int core_fscache;
215+
int core_long_paths;
215216

216217
int mingw_core_config(const char *var, const char *value)
217218
{
@@ -228,6 +229,11 @@ int mingw_core_config(const char *var, const char *value)
228229
return 0;
229230
}
230231

232+
if (!strcmp(var, "core.longpaths")) {
233+
core_long_paths = git_config_bool(var, value);
234+
return 0;
235+
}
236+
231237
if (!strcmp(var, "core.unsetenvvars")) {
232238
free(unset_environment_variables);
233239
unset_environment_variables = xstrdup(value);
@@ -240,8 +246,8 @@ int mingw_core_config(const char *var, const char *value)
240246
int mingw_unlink(const char *pathname)
241247
{
242248
int ret, tries = 0;
243-
wchar_t wpathname[MAX_PATH];
244-
if (xutftowcs_path(wpathname, pathname) < 0)
249+
wchar_t wpathname[MAX_LONG_PATH];
250+
if (xutftowcs_long_path(wpathname, pathname) < 0)
245251
return -1;
246252

247253
/* read-only files cannot be removed */
@@ -270,7 +276,7 @@ static int is_dir_empty(const wchar_t *wpath)
270276
{
271277
WIN32_FIND_DATAW findbuf;
272278
HANDLE handle;
273-
wchar_t wbuf[MAX_PATH + 2];
279+
wchar_t wbuf[MAX_LONG_PATH + 2];
274280
wcscpy(wbuf, wpath);
275281
wcscat(wbuf, L"\\*");
276282
handle = FindFirstFileW(wbuf, &findbuf);
@@ -291,8 +297,8 @@ static int is_dir_empty(const wchar_t *wpath)
291297
int mingw_rmdir(const char *pathname)
292298
{
293299
int ret, tries = 0;
294-
wchar_t wpathname[MAX_PATH];
295-
if (xutftowcs_path(wpathname, pathname) < 0)
300+
wchar_t wpathname[MAX_LONG_PATH];
301+
if (xutftowcs_long_path(wpathname, pathname) < 0)
296302
return -1;
297303

298304
while ((ret = _wrmdir(wpathname)) == -1 && tries < ARRAY_SIZE(delay)) {
@@ -367,9 +373,12 @@ static int set_hidden_flag(const wchar_t *path, int set)
367373
int mingw_mkdir(const char *path, int mode)
368374
{
369375
int ret;
370-
wchar_t wpath[MAX_PATH];
371-
if (xutftowcs_path(wpath, path) < 0)
376+
wchar_t wpath[MAX_LONG_PATH];
377+
/* CreateDirectoryW path limit is 248 (MAX_PATH - 8.3 file name) */
378+
if (xutftowcs_path_ex(wpath, path, MAX_LONG_PATH, -1, 248,
379+
core_long_paths) < 0)
372380
return -1;
381+
373382
ret = _wmkdir(wpath);
374383
if (!ret && needs_hiding(path))
375384
return set_hidden_flag(wpath, 1);
@@ -381,7 +390,7 @@ int mingw_open (const char *filename, int oflags, ...)
381390
va_list args;
382391
unsigned mode;
383392
int fd;
384-
wchar_t wfilename[MAX_PATH];
393+
wchar_t wfilename[MAX_LONG_PATH];
385394

386395
va_start(args, oflags);
387396
mode = va_arg(args, int);
@@ -390,7 +399,7 @@ int mingw_open (const char *filename, int oflags, ...)
390399
if (filename && !strcmp(filename, "/dev/null"))
391400
filename = "nul";
392401

393-
if (xutftowcs_path(wfilename, filename) < 0)
402+
if (xutftowcs_long_path(wfilename, filename) < 0)
394403
return -1;
395404
fd = _wopen(wfilename, oflags, mode);
396405

@@ -447,10 +456,10 @@ FILE *mingw_fopen (const char *filename, const char *otype)
447456
{
448457
int hide = needs_hiding(filename);
449458
FILE *file;
450-
wchar_t wfilename[MAX_PATH], wotype[4];
459+
wchar_t wfilename[MAX_LONG_PATH], wotype[4];
451460
if (filename && !strcmp(filename, "/dev/null"))
452461
filename = "nul";
453-
if (xutftowcs_path(wfilename, filename) < 0 ||
462+
if (xutftowcs_long_path(wfilename, filename) < 0 ||
454463
xutftowcs(wotype, otype, ARRAY_SIZE(wotype)) < 0)
455464
return NULL;
456465
if (hide && !access(filename, F_OK) && set_hidden_flag(wfilename, 0)) {
@@ -467,10 +476,10 @@ FILE *mingw_freopen (const char *filename, const char *otype, FILE *stream)
467476
{
468477
int hide = needs_hiding(filename);
469478
FILE *file;
470-
wchar_t wfilename[MAX_PATH], wotype[4];
479+
wchar_t wfilename[MAX_LONG_PATH], wotype[4];
471480
if (filename && !strcmp(filename, "/dev/null"))
472481
filename = "nul";
473-
if (xutftowcs_path(wfilename, filename) < 0 ||
482+
if (xutftowcs_long_path(wfilename, filename) < 0 ||
474483
xutftowcs(wotype, otype, ARRAY_SIZE(wotype)) < 0)
475484
return NULL;
476485
if (hide && !access(filename, F_OK) && set_hidden_flag(wfilename, 0)) {
@@ -524,25 +533,32 @@ ssize_t mingw_write(int fd, const void *buf, size_t len)
524533

525534
int mingw_access(const char *filename, int mode)
526535
{
527-
wchar_t wfilename[MAX_PATH];
528-
if (xutftowcs_path(wfilename, filename) < 0)
536+
wchar_t wfilename[MAX_LONG_PATH];
537+
if (xutftowcs_long_path(wfilename, filename) < 0)
529538
return -1;
530539
/* X_OK is not supported by the MSVCRT version */
531540
return _waccess(wfilename, mode & ~X_OK);
532541
}
533542

543+
/* cached length of current directory for handle_long_path */
544+
static int current_directory_len = 0;
545+
534546
int mingw_chdir(const char *dirname)
535547
{
548+
int result;
536549
wchar_t wdirname[MAX_PATH];
550+
/* SetCurrentDirectoryW doesn't support long paths */
537551
if (xutftowcs_path(wdirname, dirname) < 0)
538552
return -1;
539-
return _wchdir(wdirname);
553+
result = _wchdir(wdirname);
554+
current_directory_len = GetCurrentDirectoryW(0, NULL);
555+
return result;
540556
}
541557

542558
int mingw_chmod(const char *filename, int mode)
543559
{
544-
wchar_t wfilename[MAX_PATH];
545-
if (xutftowcs_path(wfilename, filename) < 0)
560+
wchar_t wfilename[MAX_LONG_PATH];
561+
if (xutftowcs_long_path(wfilename, filename) < 0)
546562
return -1;
547563
return _wchmod(wfilename, mode);
548564
}
@@ -590,8 +606,8 @@ static int has_valid_directory_prefix(wchar_t *wfilename)
590606
static int do_lstat(int follow, const char *file_name, struct stat *buf)
591607
{
592608
WIN32_FILE_ATTRIBUTE_DATA fdata;
593-
wchar_t wfilename[MAX_PATH];
594-
if (xutftowcs_path(wfilename, file_name) < 0)
609+
wchar_t wfilename[MAX_LONG_PATH];
610+
if (xutftowcs_long_path(wfilename, file_name) < 0)
595611
return -1;
596612

597613
if (GetFileAttributesExW(wfilename, GetFileExInfoStandard, &fdata)) {
@@ -740,8 +756,8 @@ int mingw_utime (const char *file_name, const struct utimbuf *times)
740756
FILETIME mft, aft;
741757
int fh, rc;
742758
DWORD attrs;
743-
wchar_t wfilename[MAX_PATH];
744-
if (xutftowcs_path(wfilename, file_name) < 0)
759+
wchar_t wfilename[MAX_LONG_PATH];
760+
if (xutftowcs_long_path(wfilename, file_name) < 0)
745761
return -1;
746762

747763
/* must have write permission */
@@ -789,6 +805,7 @@ unsigned int sleep (unsigned int seconds)
789805
char *mingw_mktemp(char *template)
790806
{
791807
wchar_t wtemplate[MAX_PATH];
808+
/* we need to return the path, thus no long paths here! */
792809
if (xutftowcs_path(wtemplate, template) < 0)
793810
return NULL;
794811
if (!_wmktemp(wtemplate))
@@ -1163,6 +1180,7 @@ static pid_t mingw_spawnve_fd(const char *cmd, const char **argv, char **deltaen
11631180
si.hStdOutput = winansi_get_osfhandle(fhout);
11641181
si.hStdError = winansi_get_osfhandle(fherr);
11651182

1183+
/* executables and the current directory don't support long paths */
11661184
if (xutftowcs_path(wcmd, cmd) < 0)
11671185
return -1;
11681186
if (dir && xutftowcs_path(wdir, dir) < 0)
@@ -1738,8 +1756,9 @@ int mingw_rename(const char *pold, const char *pnew)
17381756
{
17391757
DWORD attrs, gle;
17401758
int tries = 0;
1741-
wchar_t wpold[MAX_PATH], wpnew[MAX_PATH];
1742-
if (xutftowcs_path(wpold, pold) < 0 || xutftowcs_path(wpnew, pnew) < 0)
1759+
wchar_t wpold[MAX_LONG_PATH], wpnew[MAX_LONG_PATH];
1760+
if (xutftowcs_long_path(wpold, pold) < 0 ||
1761+
xutftowcs_long_path(wpnew, pnew) < 0)
17431762
return -1;
17441763

17451764
/*
@@ -1979,9 +1998,9 @@ int link(const char *oldpath, const char *newpath)
19791998
{
19801999
typedef BOOL (WINAPI *T)(LPCWSTR, LPCWSTR, LPSECURITY_ATTRIBUTES);
19812000
static T create_hard_link = NULL;
1982-
wchar_t woldpath[MAX_PATH], wnewpath[MAX_PATH];
1983-
if (xutftowcs_path(woldpath, oldpath) < 0 ||
1984-
xutftowcs_path(wnewpath, newpath) < 0)
2001+
wchar_t woldpath[MAX_LONG_PATH], wnewpath[MAX_LONG_PATH];
2002+
if (xutftowcs_long_path(woldpath, oldpath) < 0 ||
2003+
xutftowcs_long_path(wnewpath, newpath) < 0)
19852004
return -1;
19862005

19872006
if (!create_hard_link) {
@@ -2194,6 +2213,68 @@ static void setup_windows_environment(void)
21942213
setenv("TERM", "cygwin", 1);
21952214
}
21962215

2216+
int handle_long_path(wchar_t *path, int len, int max_path, int expand)
2217+
{
2218+
int result;
2219+
wchar_t buf[MAX_LONG_PATH];
2220+
2221+
/*
2222+
* we don't need special handling if path is relative to the current
2223+
* directory, and current directory + path don't exceed the desired
2224+
* max_path limit. This should cover > 99 % of cases with minimal
2225+
* performance impact (git almost always uses relative paths).
2226+
*/
2227+
if ((len < 2 || (!is_dir_sep(path[0]) && path[1] != ':')) &&
2228+
(current_directory_len + len < max_path))
2229+
return len;
2230+
2231+
/*
2232+
* handle everything else:
2233+
* - absolute paths: "C:\dir\file"
2234+
* - absolute UNC paths: "\\server\share\dir\file"
2235+
* - absolute paths on current drive: "\dir\file"
2236+
* - relative paths on other drive: "X:file"
2237+
* - prefixed paths: "\\?\...", "\\.\..."
2238+
*/
2239+
2240+
/* convert to absolute path using GetFullPathNameW */
2241+
result = GetFullPathNameW(path, MAX_LONG_PATH, buf, NULL);
2242+
if (!result) {
2243+
errno = err_win_to_posix(GetLastError());
2244+
return -1;
2245+
}
2246+
2247+
/*
2248+
* return absolute path if it fits within max_path (even if
2249+
* "cwd + path" doesn't due to '..' components)
2250+
*/
2251+
if (result < max_path) {
2252+
wcscpy(path, buf);
2253+
return result;
2254+
}
2255+
2256+
/* error out if we shouldn't expand the path or buf is too small */
2257+
if (!expand || result >= MAX_LONG_PATH - 6) {
2258+
errno = ENAMETOOLONG;
2259+
return -1;
2260+
}
2261+
2262+
/* prefix full path with "\\?\" or "\\?\UNC\" */
2263+
if (buf[0] == '\\') {
2264+
/* ...unless already prefixed */
2265+
if (buf[1] == '\\' && (buf[2] == '?' || buf[2] == '.'))
2266+
return len;
2267+
2268+
wcscpy(path, L"\\\\?\\UNC\\");
2269+
wcscpy(path + 8, buf + 2);
2270+
return result + 6;
2271+
} else {
2272+
wcscpy(path, L"\\\\?\\");
2273+
wcscpy(path + 4, buf);
2274+
return result + 4;
2275+
}
2276+
}
2277+
21972278
/*
21982279
* Disable MSVCRT command line wildcard expansion (__getmainargs called from
21992280
* mingw startup code, see init.c in mingw runtime).
@@ -2287,6 +2368,9 @@ void mingw_startup(void)
22872368

22882369
/* initialize Unicode console */
22892370
winansi_init();
2371+
2372+
/* init length of current directory for handle_long_path */
2373+
current_directory_len = GetCurrentDirectoryW(0, NULL);
22902374
}
22912375

22922376
int uname(struct utsname *buf)

0 commit comments

Comments
 (0)