@@ -248,6 +248,27 @@ static enum hide_dotfiles_type hide_dotfiles = HIDE_DOTFILES_DOTGITONLY;
248
248
static char * unset_environment_variables ;
249
249
int core_fscache ;
250
250
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
+
251
272
int mingw_core_config (const char * var , const char * value ,
252
273
const struct config_context * ctx UNUSED ,
253
274
void * cb UNUSED )
@@ -313,8 +334,8 @@ static wchar_t *normalize_ntpath(wchar_t *wbuf)
313
334
int mingw_unlink (const char * pathname )
314
335
{
315
336
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 )
318
339
return -1 ;
319
340
320
341
if (DeleteFileW (wpathname ))
@@ -346,7 +367,7 @@ static int is_dir_empty(const wchar_t *wpath)
346
367
{
347
368
WIN32_FIND_DATAW findbuf ;
348
369
HANDLE handle ;
349
- wchar_t wbuf [MAX_PATH + 2 ];
370
+ wchar_t wbuf [MAX_LONG_PATH + 2 ];
350
371
wcscpy (wbuf , wpath );
351
372
wcscat (wbuf , L"\\*" );
352
373
handle = FindFirstFileW (wbuf , & findbuf );
@@ -367,7 +388,7 @@ static int is_dir_empty(const wchar_t *wpath)
367
388
int mingw_rmdir (const char * pathname )
368
389
{
369
390
int ret , tries = 0 ;
370
- wchar_t wpathname [MAX_PATH ];
391
+ wchar_t wpathname [MAX_LONG_PATH ];
371
392
struct stat st ;
372
393
373
394
/*
@@ -389,7 +410,7 @@ int mingw_rmdir(const char *pathname)
389
410
return -1 ;
390
411
}
391
412
392
- if (xutftowcs_path (wpathname , pathname ) < 0 )
413
+ if (xutftowcs_long_path (wpathname , pathname ) < 0 )
393
414
return -1 ;
394
415
395
416
while ((ret = _wrmdir (wpathname )) == -1 && tries < ARRAY_SIZE (delay )) {
@@ -468,15 +489,18 @@ static int set_hidden_flag(const wchar_t *path, int set)
468
489
int mingw_mkdir (const char * path , int mode UNUSED )
469
490
{
470
491
int ret ;
471
- wchar_t wpath [MAX_PATH ];
492
+ wchar_t wpath [MAX_LONG_PATH ];
472
493
473
494
if (!is_valid_win32_path (path , 0 )) {
474
495
errno = EINVAL ;
475
496
return -1 ;
476
497
}
477
498
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 )
479
502
return -1 ;
503
+
480
504
ret = _wmkdir (wpath );
481
505
if (!ret && needs_hiding (path ))
482
506
return set_hidden_flag (wpath , 1 );
@@ -563,7 +587,7 @@ int mingw_open (const char *filename, int oflags, ...)
563
587
va_list args ;
564
588
unsigned mode ;
565
589
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 ];
567
591
open_fn_t open_fn ;
568
592
569
593
va_start (args , oflags );
@@ -591,7 +615,7 @@ int mingw_open (const char *filename, int oflags, ...)
591
615
592
616
if (filename && !strcmp (filename , "/dev/null" ))
593
617
wcscpy (wfilename , L"nul" );
594
- else if (xutftowcs_path (wfilename , filename ) < 0 )
618
+ else if (xutftowcs_long_path (wfilename , filename ) < 0 )
595
619
return -1 ;
596
620
597
621
fd = open_fn (wfilename , oflags , mode );
@@ -649,14 +673,14 @@ FILE *mingw_fopen (const char *filename, const char *otype)
649
673
{
650
674
int hide = needs_hiding (filename );
651
675
FILE * file ;
652
- wchar_t wfilename [MAX_PATH ], wotype [4 ];
676
+ wchar_t wfilename [MAX_LONG_PATH ], wotype [4 ];
653
677
if (filename && !strcmp (filename , "/dev/null" ))
654
678
wcscpy (wfilename , L"nul" );
655
679
else if (!is_valid_win32_path (filename , 1 )) {
656
680
int create = otype && strchr (otype , 'w' );
657
681
errno = create ? EINVAL : ENOENT ;
658
682
return NULL ;
659
- } else if (xutftowcs_path (wfilename , filename ) < 0 )
683
+ } else if (xutftowcs_long_path (wfilename , filename ) < 0 )
660
684
return NULL ;
661
685
662
686
if (xutftowcs (wotype , otype , ARRAY_SIZE (wotype )) < 0 )
@@ -678,14 +702,14 @@ FILE *mingw_freopen (const char *filename, const char *otype, FILE *stream)
678
702
{
679
703
int hide = needs_hiding (filename );
680
704
FILE * file ;
681
- wchar_t wfilename [MAX_PATH ], wotype [4 ];
705
+ wchar_t wfilename [MAX_LONG_PATH ], wotype [4 ];
682
706
if (filename && !strcmp (filename , "/dev/null" ))
683
707
wcscpy (wfilename , L"nul" );
684
708
else if (!is_valid_win32_path (filename , 1 )) {
685
709
int create = otype && strchr (otype , 'w' );
686
710
errno = create ? EINVAL : ENOENT ;
687
711
return NULL ;
688
- } else if (xutftowcs_path (wfilename , filename ) < 0 )
712
+ } else if (xutftowcs_long_path (wfilename , filename ) < 0 )
689
713
return NULL ;
690
714
691
715
if (xutftowcs (wotype , otype , ARRAY_SIZE (wotype )) < 0 )
@@ -735,7 +759,7 @@ ssize_t mingw_write(int fd, const void *buf, size_t len)
735
759
HANDLE h = (HANDLE ) _get_osfhandle (fd );
736
760
if (GetFileType (h ) != FILE_TYPE_PIPE ) {
737
761
if (orig == EINVAL ) {
738
- wchar_t path [MAX_PATH ];
762
+ wchar_t path [MAX_LONG_PATH ];
739
763
DWORD ret = GetFinalPathNameByHandleW (h , path ,
740
764
ARRAY_SIZE (path ), 0 );
741
765
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)
772
796
773
797
int mingw_access (const char * filename , int mode )
774
798
{
775
- wchar_t wfilename [MAX_PATH ];
799
+ wchar_t wfilename [MAX_LONG_PATH ];
776
800
if (!strcmp ("nul" , filename ) || !strcmp ("/dev/null" , filename ))
777
801
return 0 ;
778
- if (xutftowcs_path (wfilename , filename ) < 0 )
802
+ if (xutftowcs_long_path (wfilename , filename ) < 0 )
779
803
return -1 ;
780
804
/* X_OK is not supported by the MSVCRT version */
781
805
return _waccess (wfilename , mode & ~X_OK );
782
806
}
783
807
808
+ /* cached length of current directory for handle_long_path */
809
+ static int current_directory_len = 0 ;
810
+
784
811
int mingw_chdir (const char * dirname )
785
812
{
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 )
788
816
return -1 ;
789
- return _wchdir (wdirname );
817
+ result = _wchdir (wdirname );
818
+ current_directory_len = GetCurrentDirectoryW (0 , NULL );
819
+ return result ;
790
820
}
791
821
792
822
int mingw_chmod (const char * filename , int mode )
793
823
{
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 )
796
826
return -1 ;
797
827
return _wchmod (wfilename , mode );
798
828
}
@@ -840,8 +870,8 @@ static int has_valid_directory_prefix(wchar_t *wfilename)
840
870
static int do_lstat (int follow , const char * file_name , struct stat * buf )
841
871
{
842
872
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 )
845
875
return -1 ;
846
876
847
877
if (GetFileAttributesExW (wfilename , GetFileExInfoStandard , & fdata )) {
@@ -1012,10 +1042,10 @@ int mingw_utime (const char *file_name, const struct utimbuf *times)
1012
1042
FILETIME mft , aft ;
1013
1043
int rc ;
1014
1044
DWORD attrs ;
1015
- wchar_t wfilename [MAX_PATH ];
1045
+ wchar_t wfilename [MAX_LONG_PATH ];
1016
1046
HANDLE osfilehandle ;
1017
1047
1018
- if (xutftowcs_path (wfilename , file_name ) < 0 )
1048
+ if (xutftowcs_long_path (wfilename , file_name ) < 0 )
1019
1049
return -1 ;
1020
1050
1021
1051
/* must have write permission */
@@ -1098,6 +1128,7 @@ char *mingw_mktemp(char *template)
1098
1128
wchar_t wtemplate [MAX_PATH ];
1099
1129
int offset = 0 ;
1100
1130
1131
+ /* we need to return the path, thus no long paths here! */
1101
1132
if (xutftowcs_path (wtemplate , template ) < 0 )
1102
1133
return NULL ;
1103
1134
@@ -1749,6 +1780,10 @@ static pid_t mingw_spawnve_fd(const char *cmd, const char **argv, char **deltaen
1749
1780
1750
1781
if (* argv && !strcmp (cmd , * argv ))
1751
1782
wcmd [0 ] = L'\0' ;
1783
+ /*
1784
+ * Paths to executables and to the current directory do not support
1785
+ * long paths, therefore we cannot use xutftowcs_long_path() here.
1786
+ */
1752
1787
else if (xutftowcs_path (wcmd , cmd ) < 0 )
1753
1788
return -1 ;
1754
1789
if (dir && xutftowcs_path (wdir , dir ) < 0 )
@@ -2397,8 +2432,9 @@ int mingw_rename(const char *pold, const char *pnew)
2397
2432
{
2398
2433
DWORD attrs , gle ;
2399
2434
int tries = 0 ;
2400
- wchar_t wpold [MAX_PATH ], wpnew [MAX_PATH ];
2401
- if (xutftowcs_path (wpold , pold ) < 0 || xutftowcs_path (wpnew , pnew ) < 0 )
2435
+ wchar_t wpold [MAX_LONG_PATH ], wpnew [MAX_LONG_PATH ];
2436
+ if (xutftowcs_long_path (wpold , pold ) < 0 ||
2437
+ xutftowcs_long_path (wpnew , pnew ) < 0 )
2402
2438
return -1 ;
2403
2439
2404
2440
/*
@@ -2716,9 +2752,9 @@ int mingw_raise(int sig)
2716
2752
2717
2753
int link (const char * oldpath , const char * newpath )
2718
2754
{
2719
- wchar_t woldpath [MAX_PATH ], wnewpath [MAX_PATH ];
2720
- if (xutftowcs_path (woldpath , oldpath ) < 0 ||
2721
- xutftowcs_path (wnewpath , newpath ) < 0 )
2755
+ wchar_t woldpath [MAX_LONG_PATH ], wnewpath [MAX_LONG_PATH ];
2756
+ if (xutftowcs_long_path (woldpath , oldpath ) < 0 ||
2757
+ xutftowcs_long_path (wnewpath , newpath ) < 0 )
2722
2758
return -1 ;
2723
2759
2724
2760
if (!CreateHardLinkW (wnewpath , woldpath , NULL )) {
@@ -2786,8 +2822,8 @@ int mingw_is_mount_point(struct strbuf *path)
2786
2822
{
2787
2823
WIN32_FIND_DATAW findbuf = { 0 };
2788
2824
HANDLE handle ;
2789
- wchar_t wfilename [MAX_PATH ];
2790
- int wlen = xutftowcs_path (wfilename , path -> buf );
2825
+ wchar_t wfilename [MAX_LONG_PATH ];
2826
+ int wlen = xutftowcs_long_path (wfilename , path -> buf );
2791
2827
if (wlen < 0 )
2792
2828
die (_ ("could not get long path for '%s'" ), path -> buf );
2793
2829
@@ -2932,9 +2968,9 @@ static size_t append_system_bin_dirs(char *path, size_t size)
2932
2968
2933
2969
static int is_system32_path (const char * path )
2934
2970
{
2935
- WCHAR system32 [MAX_PATH ], wpath [MAX_PATH ];
2971
+ WCHAR system32 [MAX_LONG_PATH ], wpath [MAX_LONG_PATH ];
2936
2972
2937
- if (xutftowcs_path (wpath , path ) < 0 ||
2973
+ if (xutftowcs_long_path (wpath , path ) < 0 ||
2938
2974
!GetSystemDirectoryW (system32 , ARRAY_SIZE (system32 )) ||
2939
2975
_wcsicmp (system32 , wpath ))
2940
2976
return 0 ;
@@ -3346,6 +3382,68 @@ int is_valid_win32_path(const char *path, int allow_literal_nul)
3346
3382
}
3347
3383
}
3348
3384
3385
+ int handle_long_path (wchar_t * path , int len , int max_path , int expand )
3386
+ {
3387
+ int result ;
3388
+ wchar_t buf [MAX_LONG_PATH ];
3389
+
3390
+ /*
3391
+ * we don't need special handling if path is relative to the current
3392
+ * directory, and current directory + path don't exceed the desired
3393
+ * max_path limit. This should cover > 99 % of cases with minimal
3394
+ * performance impact (git almost always uses relative paths).
3395
+ */
3396
+ if ((len < 2 || (!is_dir_sep (path [0 ]) && path [1 ] != ':' )) &&
3397
+ (current_directory_len + len < max_path ))
3398
+ return len ;
3399
+
3400
+ /*
3401
+ * handle everything else:
3402
+ * - absolute paths: "C:\dir\file"
3403
+ * - absolute UNC paths: "\\server\share\dir\file"
3404
+ * - absolute paths on current drive: "\dir\file"
3405
+ * - relative paths on other drive: "X:file"
3406
+ * - prefixed paths: "\\?\...", "\\.\..."
3407
+ */
3408
+
3409
+ /* convert to absolute path using GetFullPathNameW */
3410
+ result = GetFullPathNameW (path , MAX_LONG_PATH , buf , NULL );
3411
+ if (!result ) {
3412
+ errno = err_win_to_posix (GetLastError ());
3413
+ return -1 ;
3414
+ }
3415
+
3416
+ /*
3417
+ * return absolute path if it fits within max_path (even if
3418
+ * "cwd + path" doesn't due to '..' components)
3419
+ */
3420
+ if (result < max_path ) {
3421
+ wcscpy (path , buf );
3422
+ return result ;
3423
+ }
3424
+
3425
+ /* error out if we shouldn't expand the path or buf is too small */
3426
+ if (!expand || result >= MAX_LONG_PATH - 6 ) {
3427
+ errno = ENAMETOOLONG ;
3428
+ return -1 ;
3429
+ }
3430
+
3431
+ /* prefix full path with "\\?\" or "\\?\UNC\" */
3432
+ if (buf [0 ] == '\\' ) {
3433
+ /* ...unless already prefixed */
3434
+ if (buf [1 ] == '\\' && (buf [2 ] == '?' || buf [2 ] == '.' ))
3435
+ return len ;
3436
+
3437
+ wcscpy (path , L"\\\\?\\UNC\\" );
3438
+ wcscpy (path + 8 , buf + 2 );
3439
+ return result + 6 ;
3440
+ } else {
3441
+ wcscpy (path , L"\\\\?\\" );
3442
+ wcscpy (path + 4 , buf );
3443
+ return result + 4 ;
3444
+ }
3445
+ }
3446
+
3349
3447
#if !defined(_MSC_VER )
3350
3448
/*
3351
3449
* Disable MSVCRT command line wildcard expansion (__getmainargs called from
@@ -3507,6 +3605,9 @@ int wmain(int argc, const wchar_t **wargv)
3507
3605
/* initialize Unicode console */
3508
3606
winansi_init ();
3509
3607
3608
+ /* init length of current directory for handle_long_path */
3609
+ current_directory_len = GetCurrentDirectoryW (0 , NULL );
3610
+
3510
3611
/* invoke the real main() using our utf8 version of argv. */
3511
3612
exit_status = main (argc , argv );
3512
3613
0 commit comments