@@ -251,6 +251,27 @@ static enum hide_dotfiles_type hide_dotfiles = HIDE_DOTFILES_DOTGITONLY;
251251static char * unset_environment_variables ;
252252int core_fscache ;
253253
254+ int are_long_paths_enabled (void )
255+ {
256+ /* default to `false` during initialization */
257+ static const int fallback = 0 ;
258+
259+ static int enabled = -1 ;
260+
261+ if (enabled < 0 ) {
262+ /* avoid infinite recursion */
263+ if (!the_repository )
264+ return fallback ;
265+
266+ if (the_repository -> config &&
267+ the_repository -> config -> hash_initialized &&
268+ git_config_get_bool ("core.longpaths" , & enabled ) < 0 )
269+ enabled = 0 ;
270+ }
271+
272+ return enabled < 0 ? fallback : enabled ;
273+ }
274+
254275int mingw_core_config (const char * var , const char * value ,
255276 const struct config_context * ctx UNUSED ,
256277 void * cb UNUSED )
@@ -307,8 +328,8 @@ static wchar_t *normalize_ntpath(wchar_t *wbuf)
307328int mingw_unlink (const char * pathname , int handle_in_use_error )
308329{
309330 int ret , tries = 0 ;
310- wchar_t wpathname [MAX_PATH ];
311- if (xutftowcs_path (wpathname , pathname ) < 0 )
331+ wchar_t wpathname [MAX_LONG_PATH ];
332+ if (xutftowcs_long_path (wpathname , pathname ) < 0 )
312333 return -1 ;
313334
314335 if (DeleteFileW (wpathname ))
@@ -343,7 +364,7 @@ static int is_dir_empty(const wchar_t *wpath)
343364{
344365 WIN32_FIND_DATAW findbuf ;
345366 HANDLE handle ;
346- wchar_t wbuf [MAX_PATH + 2 ];
367+ wchar_t wbuf [MAX_LONG_PATH + 2 ];
347368 wcscpy (wbuf , wpath );
348369 wcscat (wbuf , L"\\*" );
349370 handle = FindFirstFileW (wbuf , & findbuf );
@@ -364,7 +385,7 @@ static int is_dir_empty(const wchar_t *wpath)
364385int mingw_rmdir (const char * pathname )
365386{
366387 int ret , tries = 0 ;
367- wchar_t wpathname [MAX_PATH ];
388+ wchar_t wpathname [MAX_LONG_PATH ];
368389 struct stat st ;
369390
370391 /*
@@ -386,7 +407,7 @@ int mingw_rmdir(const char *pathname)
386407 return -1 ;
387408 }
388409
389- if (xutftowcs_path (wpathname , pathname ) < 0 )
410+ if (xutftowcs_long_path (wpathname , pathname ) < 0 )
390411 return -1 ;
391412
392413 while ((ret = _wrmdir (wpathname )) == -1 && tries < ARRAY_SIZE (delay )) {
@@ -465,15 +486,18 @@ static int set_hidden_flag(const wchar_t *path, int set)
465486int mingw_mkdir (const char * path , int mode UNUSED )
466487{
467488 int ret ;
468- wchar_t wpath [MAX_PATH ];
489+ wchar_t wpath [MAX_LONG_PATH ];
469490
470491 if (!is_valid_win32_path (path , 0 )) {
471492 errno = EINVAL ;
472493 return -1 ;
473494 }
474495
475- if (xutftowcs_path (wpath , path ) < 0 )
496+ /* CreateDirectoryW path limit is 248 (MAX_PATH - 8.3 file name) */
497+ if (xutftowcs_path_ex (wpath , path , MAX_LONG_PATH , -1 , 248 ,
498+ are_long_paths_enabled ()) < 0 )
476499 return -1 ;
500+
477501 ret = _wmkdir (wpath );
478502 if (!ret && needs_hiding (path ))
479503 return set_hidden_flag (wpath , 1 );
@@ -635,7 +659,7 @@ int mingw_open (const char *filename, int oflags, ...)
635659 va_list args ;
636660 unsigned mode ;
637661 int fd , create = (oflags & (O_CREAT | O_EXCL )) == (O_CREAT | O_EXCL );
638- wchar_t wfilename [MAX_PATH ];
662+ wchar_t wfilename [MAX_LONG_PATH ];
639663 open_fn_t open_fn ;
640664
641665 DECLARE_PROC_ADDR (ntdll .dll , NTSTATUS , NTAPI , RtlGetLastNtStatus , void );
@@ -667,7 +691,7 @@ int mingw_open (const char *filename, int oflags, ...)
667691
668692 if (filename && !strcmp (filename , "/dev/null" ))
669693 wcscpy (wfilename , L"nul" );
670- else if (xutftowcs_path (wfilename , filename ) < 0 )
694+ else if (xutftowcs_long_path (wfilename , filename ) < 0 )
671695 return -1 ;
672696
673697 fd = open_fn (wfilename , oflags , mode );
@@ -740,14 +764,14 @@ FILE *mingw_fopen (const char *filename, const char *otype)
740764{
741765 int hide = needs_hiding (filename );
742766 FILE * file ;
743- wchar_t wfilename [MAX_PATH ], wotype [4 ];
767+ wchar_t wfilename [MAX_LONG_PATH ], wotype [4 ];
744768 if (filename && !strcmp (filename , "/dev/null" ))
745769 wcscpy (wfilename , L"nul" );
746770 else if (!is_valid_win32_path (filename , 1 )) {
747771 int create = otype && strchr (otype , 'w' );
748772 errno = create ? EINVAL : ENOENT ;
749773 return NULL ;
750- } else if (xutftowcs_path (wfilename , filename ) < 0 )
774+ } else if (xutftowcs_long_path (wfilename , filename ) < 0 )
751775 return NULL ;
752776
753777 if (xutftowcs (wotype , otype , ARRAY_SIZE (wotype )) < 0 )
@@ -769,14 +793,14 @@ FILE *mingw_freopen (const char *filename, const char *otype, FILE *stream)
769793{
770794 int hide = needs_hiding (filename );
771795 FILE * file ;
772- wchar_t wfilename [MAX_PATH ], wotype [4 ];
796+ wchar_t wfilename [MAX_LONG_PATH ], wotype [4 ];
773797 if (filename && !strcmp (filename , "/dev/null" ))
774798 wcscpy (wfilename , L"nul" );
775799 else if (!is_valid_win32_path (filename , 1 )) {
776800 int create = otype && strchr (otype , 'w' );
777801 errno = create ? EINVAL : ENOENT ;
778802 return NULL ;
779- } else if (xutftowcs_path (wfilename , filename ) < 0 )
803+ } else if (xutftowcs_long_path (wfilename , filename ) < 0 )
780804 return NULL ;
781805
782806 if (xutftowcs (wotype , otype , ARRAY_SIZE (wotype )) < 0 )
@@ -826,7 +850,7 @@ ssize_t mingw_write(int fd, const void *buf, size_t len)
826850 HANDLE h = (HANDLE ) _get_osfhandle (fd );
827851 if (GetFileType (h ) != FILE_TYPE_PIPE ) {
828852 if (orig == EINVAL ) {
829- wchar_t path [MAX_PATH ];
853+ wchar_t path [MAX_LONG_PATH ];
830854 DWORD ret = GetFinalPathNameByHandleW (h , path ,
831855 ARRAY_SIZE (path ), 0 );
832856 UINT drive_type = ret > 0 && ret < ARRAY_SIZE (path ) ?
@@ -863,27 +887,33 @@ ssize_t mingw_write(int fd, const void *buf, size_t len)
863887
864888int mingw_access (const char * filename , int mode )
865889{
866- wchar_t wfilename [MAX_PATH ];
890+ wchar_t wfilename [MAX_LONG_PATH ];
867891 if (!strcmp ("nul" , filename ) || !strcmp ("/dev/null" , filename ))
868892 return 0 ;
869- if (xutftowcs_path (wfilename , filename ) < 0 )
893+ if (xutftowcs_long_path (wfilename , filename ) < 0 )
870894 return -1 ;
871895 /* X_OK is not supported by the MSVCRT version */
872896 return _waccess (wfilename , mode & ~X_OK );
873897}
874898
899+ /* cached length of current directory for handle_long_path */
900+ static int current_directory_len = 0 ;
901+
875902int mingw_chdir (const char * dirname )
876903{
877- wchar_t wdirname [MAX_PATH ];
878- if (xutftowcs_path (wdirname , dirname ) < 0 )
904+ int result ;
905+ wchar_t wdirname [MAX_LONG_PATH ];
906+ if (xutftowcs_long_path (wdirname , dirname ) < 0 )
879907 return -1 ;
880- return _wchdir (wdirname );
908+ result = _wchdir (wdirname );
909+ current_directory_len = GetCurrentDirectoryW (0 , NULL );
910+ return result ;
881911}
882912
883913int mingw_chmod (const char * filename , int mode )
884914{
885- wchar_t wfilename [MAX_PATH ];
886- if (xutftowcs_path (wfilename , filename ) < 0 )
915+ wchar_t wfilename [MAX_LONG_PATH ];
916+ if (xutftowcs_long_path (wfilename , filename ) < 0 )
887917 return -1 ;
888918 return _wchmod (wfilename , mode );
889919}
@@ -931,8 +961,8 @@ static int has_valid_directory_prefix(wchar_t *wfilename)
931961static int do_lstat (int follow , const char * file_name , struct stat * buf )
932962{
933963 WIN32_FILE_ATTRIBUTE_DATA fdata ;
934- wchar_t wfilename [MAX_PATH ];
935- if (xutftowcs_path (wfilename , file_name ) < 0 )
964+ wchar_t wfilename [MAX_LONG_PATH ];
965+ if (xutftowcs_long_path (wfilename , file_name ) < 0 )
936966 return -1 ;
937967
938968 if (GetFileAttributesExW (wfilename , GetFileExInfoStandard , & fdata )) {
@@ -1103,10 +1133,10 @@ int mingw_utime (const char *file_name, const struct utimbuf *times)
11031133 FILETIME mft , aft ;
11041134 int rc ;
11051135 DWORD attrs ;
1106- wchar_t wfilename [MAX_PATH ];
1136+ wchar_t wfilename [MAX_LONG_PATH ];
11071137 HANDLE osfilehandle ;
11081138
1109- if (xutftowcs_path (wfilename , file_name ) < 0 )
1139+ if (xutftowcs_long_path (wfilename , file_name ) < 0 )
11101140 return -1 ;
11111141
11121142 /* must have write permission */
@@ -1189,6 +1219,7 @@ char *mingw_mktemp(char *template)
11891219 wchar_t wtemplate [MAX_PATH ];
11901220 int offset = 0 ;
11911221
1222+ /* we need to return the path, thus no long paths here! */
11921223 if (xutftowcs_path (wtemplate , template ) < 0 )
11931224 return NULL ;
11941225
@@ -1830,6 +1861,10 @@ static pid_t mingw_spawnve_fd(const char *cmd, const char **argv, char **deltaen
18301861
18311862 if (* argv && !strcmp (cmd , * argv ))
18321863 wcmd [0 ] = L'\0' ;
1864+ /*
1865+ * Paths to executables and to the current directory do not support
1866+ * long paths, therefore we cannot use xutftowcs_long_path() here.
1867+ */
18331868 else if (xutftowcs_path (wcmd , cmd ) < 0 )
18341869 return -1 ;
18351870 if (dir && xutftowcs_path (wdir , dir ) < 0 )
@@ -2519,12 +2554,12 @@ int mingw_rename(const char *pold, const char *pnew)
25192554 static int supports_file_rename_info_ex = 1 ;
25202555 DWORD attrs , gle ;
25212556 int tries = 0 ;
2522- wchar_t wpold [MAX_PATH ], wpnew [MAX_PATH ];
2557+ wchar_t wpold [MAX_LONG_PATH ], wpnew [MAX_LONG_PATH ];
25232558 int wpnew_len ;
25242559
2525- if (xutftowcs_path (wpold , pold ) < 0 )
2560+ if (xutftowcs_long_path (wpold , pold ) < 0 )
25262561 return -1 ;
2527- wpnew_len = xutftowcs_path (wpnew , pnew );
2562+ wpnew_len = xutftowcs_long_path (wpnew , pnew );
25282563 if (wpnew_len < 0 )
25292564 return -1 ;
25302565
@@ -2563,9 +2598,9 @@ int mingw_rename(const char *pold, const char *pnew)
25632598 * flex array so that the structure has to be allocated on
25642599 * the heap. As we declare this structure ourselves though
25652600 * we can avoid the allocation and define FileName to have
2566- * MAX_PATH bytes.
2601+ * MAX_LONG_PATH bytes.
25672602 */
2568- WCHAR FileName [MAX_PATH ];
2603+ WCHAR FileName [MAX_LONG_PATH ];
25692604 } rename_info = { 0 };
25702605 HANDLE old_handle = INVALID_HANDLE_VALUE ;
25712606 BOOL success ;
@@ -2918,9 +2953,9 @@ int mingw_raise(int sig)
29182953
29192954int link (const char * oldpath , const char * newpath )
29202955{
2921- wchar_t woldpath [MAX_PATH ], wnewpath [MAX_PATH ];
2922- if (xutftowcs_path (woldpath , oldpath ) < 0 ||
2923- xutftowcs_path (wnewpath , newpath ) < 0 )
2956+ wchar_t woldpath [MAX_LONG_PATH ], wnewpath [MAX_LONG_PATH ];
2957+ if (xutftowcs_long_path (woldpath , oldpath ) < 0 ||
2958+ xutftowcs_long_path (wnewpath , newpath ) < 0 )
29242959 return -1 ;
29252960
29262961 if (!CreateHardLinkW (wnewpath , woldpath , NULL )) {
@@ -2988,8 +3023,8 @@ int mingw_is_mount_point(struct strbuf *path)
29883023{
29893024 WIN32_FIND_DATAW findbuf = { 0 };
29903025 HANDLE handle ;
2991- wchar_t wfilename [MAX_PATH ];
2992- int wlen = xutftowcs_path (wfilename , path -> buf );
3026+ wchar_t wfilename [MAX_LONG_PATH ];
3027+ int wlen = xutftowcs_long_path (wfilename , path -> buf );
29933028 if (wlen < 0 )
29943029 die (_ ("could not get long path for '%s'" ), path -> buf );
29953030
@@ -3141,9 +3176,9 @@ static size_t append_system_bin_dirs(char *path, size_t size)
31413176
31423177static int is_system32_path (const char * path )
31433178{
3144- WCHAR system32 [MAX_PATH ], wpath [MAX_PATH ];
3179+ WCHAR system32 [MAX_LONG_PATH ], wpath [MAX_LONG_PATH ];
31453180
3146- if (xutftowcs_path (wpath , path ) < 0 ||
3181+ if (xutftowcs_long_path (wpath , path ) < 0 ||
31473182 !GetSystemDirectoryW (system32 , ARRAY_SIZE (system32 )) ||
31483183 _wcsicmp (system32 , wpath ))
31493184 return 0 ;
@@ -3576,6 +3611,68 @@ int is_valid_win32_path(const char *path, int allow_literal_nul)
35763611 }
35773612}
35783613
3614+ int handle_long_path (wchar_t * path , int len , int max_path , int expand )
3615+ {
3616+ int result ;
3617+ wchar_t buf [MAX_LONG_PATH ];
3618+
3619+ /*
3620+ * we don't need special handling if path is relative to the current
3621+ * directory, and current directory + path don't exceed the desired
3622+ * max_path limit. This should cover > 99 % of cases with minimal
3623+ * performance impact (git almost always uses relative paths).
3624+ */
3625+ if ((len < 2 || (!is_dir_sep (path [0 ]) && path [1 ] != ':' )) &&
3626+ (current_directory_len + len < max_path ))
3627+ return len ;
3628+
3629+ /*
3630+ * handle everything else:
3631+ * - absolute paths: "C:\dir\file"
3632+ * - absolute UNC paths: "\\server\share\dir\file"
3633+ * - absolute paths on current drive: "\dir\file"
3634+ * - relative paths on other drive: "X:file"
3635+ * - prefixed paths: "\\?\...", "\\.\..."
3636+ */
3637+
3638+ /* convert to absolute path using GetFullPathNameW */
3639+ result = GetFullPathNameW (path , MAX_LONG_PATH , buf , NULL );
3640+ if (!result ) {
3641+ errno = err_win_to_posix (GetLastError ());
3642+ return -1 ;
3643+ }
3644+
3645+ /*
3646+ * return absolute path if it fits within max_path (even if
3647+ * "cwd + path" doesn't due to '..' components)
3648+ */
3649+ if (result < max_path ) {
3650+ wcscpy (path , buf );
3651+ return result ;
3652+ }
3653+
3654+ /* error out if we shouldn't expand the path or buf is too small */
3655+ if (!expand || result >= MAX_LONG_PATH - 6 ) {
3656+ errno = ENAMETOOLONG ;
3657+ return -1 ;
3658+ }
3659+
3660+ /* prefix full path with "\\?\" or "\\?\UNC\" */
3661+ if (buf [0 ] == '\\' ) {
3662+ /* ...unless already prefixed */
3663+ if (buf [1 ] == '\\' && (buf [2 ] == '?' || buf [2 ] == '.' ))
3664+ return len ;
3665+
3666+ wcscpy (path , L"\\\\?\\UNC\\" );
3667+ wcscpy (path + 8 , buf + 2 );
3668+ return result + 6 ;
3669+ } else {
3670+ wcscpy (path , L"\\\\?\\" );
3671+ wcscpy (path + 4 , buf );
3672+ return result + 4 ;
3673+ }
3674+ }
3675+
35793676#if !defined(_MSC_VER )
35803677/*
35813678 * Disable MSVCRT command line wildcard expansion (__getmainargs called from
@@ -3738,6 +3835,9 @@ int wmain(int argc, const wchar_t **wargv)
37383835 /* initialize Unicode console */
37393836 winansi_init ();
37403837
3838+ /* init length of current directory for handle_long_path */
3839+ current_directory_len = GetCurrentDirectoryW (0 , NULL );
3840+
37413841 /* invoke the real main() using our utf8 version of argv. */
37423842 exit_status = main (argc , argv );
37433843
0 commit comments