Skip to content

Commit f8bde49

Browse files
kbleesGit 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, added support for chdir(), etc] 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 775f921 commit f8bde49

File tree

7 files changed

+321
-55
lines changed

7 files changed

+321
-55
lines changed

Documentation/config/core.txt

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

560+
core.longpaths::
561+
Enable long path (> 260) support for builtin commands in Git for
562+
Windows. This is disabled by default, as long paths are not supported
563+
by Windows Explorer, cmd.exe and the Git for Windows tool chain
564+
(msys, bash, tcl, perl...). Only enable this if you know what you're
565+
doing and are prepared to live with a few quirks.
566+
560567
core.unsetenvvars::
561568
Windows-only: comma-separated list of environment variables'
562569
names that need to be unset before spawning any other process.

compat/mingw.c

Lines changed: 115 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,7 @@ enum hide_dotfiles_type {
229229
static enum hide_dotfiles_type hide_dotfiles = HIDE_DOTFILES_DOTGITONLY;
230230
static char *unset_environment_variables;
231231
int core_fscache;
232+
int core_long_paths;
232233

233234
int mingw_core_config(const char *var, const char *value, void *cb)
234235
{
@@ -245,6 +246,11 @@ int mingw_core_config(const char *var, const char *value, void *cb)
245246
return 0;
246247
}
247248

249+
if (!strcmp(var, "core.longpaths")) {
250+
core_long_paths = git_config_bool(var, value);
251+
return 0;
252+
}
253+
248254
if (!strcmp(var, "core.unsetenvvars")) {
249255
free(unset_environment_variables);
250256
unset_environment_variables = xstrdup(value);
@@ -282,8 +288,8 @@ static wchar_t *normalize_ntpath(wchar_t *wbuf)
282288
int mingw_unlink(const char *pathname)
283289
{
284290
int ret, tries = 0;
285-
wchar_t wpathname[MAX_PATH];
286-
if (xutftowcs_path(wpathname, pathname) < 0)
291+
wchar_t wpathname[MAX_LONG_PATH];
292+
if (xutftowcs_long_path(wpathname, pathname) < 0)
287293
return -1;
288294

289295
/* read-only files cannot be removed */
@@ -312,7 +318,7 @@ static int is_dir_empty(const wchar_t *wpath)
312318
{
313319
WIN32_FIND_DATAW findbuf;
314320
HANDLE handle;
315-
wchar_t wbuf[MAX_PATH + 2];
321+
wchar_t wbuf[MAX_LONG_PATH + 2];
316322
wcscpy(wbuf, wpath);
317323
wcscat(wbuf, L"\\*");
318324
handle = FindFirstFileW(wbuf, &findbuf);
@@ -333,8 +339,8 @@ static int is_dir_empty(const wchar_t *wpath)
333339
int mingw_rmdir(const char *pathname)
334340
{
335341
int ret, tries = 0;
336-
wchar_t wpathname[MAX_PATH];
337-
if (xutftowcs_path(wpathname, pathname) < 0)
342+
wchar_t wpathname[MAX_LONG_PATH];
343+
if (xutftowcs_long_path(wpathname, pathname) < 0)
338344
return -1;
339345

340346
while ((ret = _wrmdir(wpathname)) == -1 && tries < ARRAY_SIZE(delay)) {
@@ -409,9 +415,12 @@ static int set_hidden_flag(const wchar_t *path, int set)
409415
int mingw_mkdir(const char *path, int mode)
410416
{
411417
int ret;
412-
wchar_t wpath[MAX_PATH];
413-
if (xutftowcs_path(wpath, path) < 0)
418+
wchar_t wpath[MAX_LONG_PATH];
419+
/* CreateDirectoryW path limit is 248 (MAX_PATH - 8.3 file name) */
420+
if (xutftowcs_path_ex(wpath, path, MAX_LONG_PATH, -1, 248,
421+
core_long_paths) < 0)
414422
return -1;
423+
415424
ret = _wmkdir(wpath);
416425
if (!ret && needs_hiding(path))
417426
return set_hidden_flag(wpath, 1);
@@ -484,7 +493,7 @@ int mingw_open (const char *filename, int oflags, ...)
484493
va_list args;
485494
unsigned mode;
486495
int fd;
487-
wchar_t wfilename[MAX_PATH];
496+
wchar_t wfilename[MAX_LONG_PATH];
488497
open_fn_t open_fn;
489498

490499
va_start(args, oflags);
@@ -499,7 +508,7 @@ int mingw_open (const char *filename, int oflags, ...)
499508
else
500509
open_fn = _wopen;
501510

502-
if (xutftowcs_path(wfilename, filename) < 0)
511+
if (xutftowcs_long_path(wfilename, filename) < 0)
503512
return -1;
504513
fd = open_fn(wfilename, oflags, mode);
505514

@@ -556,10 +565,10 @@ FILE *mingw_fopen (const char *filename, const char *otype)
556565
{
557566
int hide = needs_hiding(filename);
558567
FILE *file;
559-
wchar_t wfilename[MAX_PATH], wotype[4];
568+
wchar_t wfilename[MAX_LONG_PATH], wotype[4];
560569
if (filename && !strcmp(filename, "/dev/null"))
561570
filename = "nul";
562-
if (xutftowcs_path(wfilename, filename) < 0 ||
571+
if (xutftowcs_long_path(wfilename, filename) < 0 ||
563572
xutftowcs(wotype, otype, ARRAY_SIZE(wotype)) < 0)
564573
return NULL;
565574
if (hide && !access(filename, F_OK) && set_hidden_flag(wfilename, 0)) {
@@ -578,10 +587,10 @@ FILE *mingw_freopen (const char *filename, const char *otype, FILE *stream)
578587
{
579588
int hide = needs_hiding(filename);
580589
FILE *file;
581-
wchar_t wfilename[MAX_PATH], wotype[4];
590+
wchar_t wfilename[MAX_LONG_PATH], wotype[4];
582591
if (filename && !strcmp(filename, "/dev/null"))
583592
filename = "nul";
584-
if (xutftowcs_path(wfilename, filename) < 0 ||
593+
if (xutftowcs_long_path(wfilename, filename) < 0 ||
585594
xutftowcs(wotype, otype, ARRAY_SIZE(wotype)) < 0)
586595
return NULL;
587596
if (hide && !access(filename, F_OK) && set_hidden_flag(wfilename, 0)) {
@@ -635,25 +644,31 @@ ssize_t mingw_write(int fd, const void *buf, size_t len)
635644

636645
int mingw_access(const char *filename, int mode)
637646
{
638-
wchar_t wfilename[MAX_PATH];
639-
if (xutftowcs_path(wfilename, filename) < 0)
647+
wchar_t wfilename[MAX_LONG_PATH];
648+
if (xutftowcs_long_path(wfilename, filename) < 0)
640649
return -1;
641650
/* X_OK is not supported by the MSVCRT version */
642651
return _waccess(wfilename, mode & ~X_OK);
643652
}
644653

654+
/* cached length of current directory for handle_long_path */
655+
static int current_directory_len = 0;
656+
645657
int mingw_chdir(const char *dirname)
646658
{
647-
wchar_t wdirname[MAX_PATH];
648-
if (xutftowcs_path(wdirname, dirname) < 0)
659+
int result;
660+
wchar_t wdirname[MAX_LONG_PATH];
661+
if (xutftowcs_long_path(wdirname, dirname) < 0)
649662
return -1;
650-
return _wchdir(wdirname);
663+
result = _wchdir(wdirname);
664+
current_directory_len = GetCurrentDirectoryW(0, NULL);
665+
return result;
651666
}
652667

653668
int mingw_chmod(const char *filename, int mode)
654669
{
655-
wchar_t wfilename[MAX_PATH];
656-
if (xutftowcs_path(wfilename, filename) < 0)
670+
wchar_t wfilename[MAX_LONG_PATH];
671+
if (xutftowcs_long_path(wfilename, filename) < 0)
657672
return -1;
658673
return _wchmod(wfilename, mode);
659674
}
@@ -701,8 +716,8 @@ static int has_valid_directory_prefix(wchar_t *wfilename)
701716
static int do_lstat(int follow, const char *file_name, struct stat *buf)
702717
{
703718
WIN32_FILE_ATTRIBUTE_DATA fdata;
704-
wchar_t wfilename[MAX_PATH];
705-
if (xutftowcs_path(wfilename, file_name) < 0)
719+
wchar_t wfilename[MAX_LONG_PATH];
720+
if (xutftowcs_long_path(wfilename, file_name) < 0)
706721
return -1;
707722

708723
if (GetFileAttributesExW(wfilename, GetFileExInfoStandard, &fdata)) {
@@ -873,8 +888,8 @@ int mingw_utime (const char *file_name, const struct utimbuf *times)
873888
FILETIME mft, aft;
874889
int fh, rc;
875890
DWORD attrs;
876-
wchar_t wfilename[MAX_PATH];
877-
if (xutftowcs_path(wfilename, file_name) < 0)
891+
wchar_t wfilename[MAX_LONG_PATH];
892+
if (xutftowcs_long_path(wfilename, file_name) < 0)
878893
return -1;
879894

880895
/* must have write permission */
@@ -935,6 +950,7 @@ char *mingw_mktemp(char *template)
935950
wchar_t wtemplate[MAX_PATH];
936951
int offset = 0;
937952

953+
/* we need to return the path, thus no long paths here! */
938954
if (xutftowcs_path(wtemplate, template) < 0)
939955
return NULL;
940956

@@ -1459,6 +1475,10 @@ static pid_t mingw_spawnve_fd(const char *cmd, const char **argv, char **deltaen
14591475

14601476
if (*argv && !strcmp(cmd, *argv))
14611477
wcmd[0] = L'\0';
1478+
/*
1479+
* Paths to executables and to the current directory do not support
1480+
* long paths, therefore we cannot use xutftowcs_long_path() here.
1481+
*/
14621482
else if (xutftowcs_path(wcmd, cmd) < 0)
14631483
return -1;
14641484
if (dir && xutftowcs_path(wdir, dir) < 0)
@@ -1891,8 +1911,9 @@ int mingw_rename(const char *pold, const char *pnew)
18911911
{
18921912
DWORD attrs, gle;
18931913
int tries = 0;
1894-
wchar_t wpold[MAX_PATH], wpnew[MAX_PATH];
1895-
if (xutftowcs_path(wpold, pold) < 0 || xutftowcs_path(wpnew, pnew) < 0)
1914+
wchar_t wpold[MAX_LONG_PATH], wpnew[MAX_LONG_PATH];
1915+
if (xutftowcs_long_path(wpold, pold) < 0 ||
1916+
xutftowcs_long_path(wpnew, pnew) < 0)
18961917
return -1;
18971918

18981919
/*
@@ -2206,9 +2227,9 @@ int mingw_raise(int sig)
22062227

22072228
int link(const char *oldpath, const char *newpath)
22082229
{
2209-
wchar_t woldpath[MAX_PATH], wnewpath[MAX_PATH];
2210-
if (xutftowcs_path(woldpath, oldpath) < 0 ||
2211-
xutftowcs_path(wnewpath, newpath) < 0)
2230+
wchar_t woldpath[MAX_LONG_PATH], wnewpath[MAX_LONG_PATH];
2231+
if (xutftowcs_long_path(woldpath, oldpath) < 0 ||
2232+
xutftowcs_long_path(wnewpath, newpath) < 0)
22122233
return -1;
22132234

22142235
if (!CreateHardLinkW(wnewpath, woldpath, NULL)) {
@@ -2408,6 +2429,68 @@ static void setup_windows_environment(void)
24082429
}
24092430
}
24102431

2432+
int handle_long_path(wchar_t *path, int len, int max_path, int expand)
2433+
{
2434+
int result;
2435+
wchar_t buf[MAX_LONG_PATH];
2436+
2437+
/*
2438+
* we don't need special handling if path is relative to the current
2439+
* directory, and current directory + path don't exceed the desired
2440+
* max_path limit. This should cover > 99 % of cases with minimal
2441+
* performance impact (git almost always uses relative paths).
2442+
*/
2443+
if ((len < 2 || (!is_dir_sep(path[0]) && path[1] != ':')) &&
2444+
(current_directory_len + len < max_path))
2445+
return len;
2446+
2447+
/*
2448+
* handle everything else:
2449+
* - absolute paths: "C:\dir\file"
2450+
* - absolute UNC paths: "\\server\share\dir\file"
2451+
* - absolute paths on current drive: "\dir\file"
2452+
* - relative paths on other drive: "X:file"
2453+
* - prefixed paths: "\\?\...", "\\.\..."
2454+
*/
2455+
2456+
/* convert to absolute path using GetFullPathNameW */
2457+
result = GetFullPathNameW(path, MAX_LONG_PATH, buf, NULL);
2458+
if (!result) {
2459+
errno = err_win_to_posix(GetLastError());
2460+
return -1;
2461+
}
2462+
2463+
/*
2464+
* return absolute path if it fits within max_path (even if
2465+
* "cwd + path" doesn't due to '..' components)
2466+
*/
2467+
if (result < max_path) {
2468+
wcscpy(path, buf);
2469+
return result;
2470+
}
2471+
2472+
/* error out if we shouldn't expand the path or buf is too small */
2473+
if (!expand || result >= MAX_LONG_PATH - 6) {
2474+
errno = ENAMETOOLONG;
2475+
return -1;
2476+
}
2477+
2478+
/* prefix full path with "\\?\" or "\\?\UNC\" */
2479+
if (buf[0] == '\\') {
2480+
/* ...unless already prefixed */
2481+
if (buf[1] == '\\' && (buf[2] == '?' || buf[2] == '.'))
2482+
return len;
2483+
2484+
wcscpy(path, L"\\\\?\\UNC\\");
2485+
wcscpy(path + 8, buf + 2);
2486+
return result + 6;
2487+
} else {
2488+
wcscpy(path, L"\\\\?\\");
2489+
wcscpy(path + 4, buf);
2490+
return result + 4;
2491+
}
2492+
}
2493+
24112494
#if !defined(_MSC_VER)
24122495
/*
24132496
* Disable MSVCRT command line wildcard expansion (__getmainargs called from
@@ -2569,6 +2652,9 @@ int wmain(int argc, const wchar_t **wargv)
25692652
/* initialize Unicode console */
25702653
winansi_init();
25712654

2655+
/* init length of current directory for handle_long_path */
2656+
current_directory_len = GetCurrentDirectoryW(0, NULL);
2657+
25722658
/* invoke the real main() using our utf8 version of argv. */
25732659
exit_status = main(argc, argv);
25742660

0 commit comments

Comments
 (0)