@@ -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 , void * cb )
252
273
{
253
274
if (!strcmp (var , "core.hidedotfiles" )) {
@@ -309,8 +330,8 @@ static wchar_t *normalize_ntpath(wchar_t *wbuf)
309
330
int mingw_unlink (const char * pathname )
310
331
{
311
332
int ret , tries = 0 ;
312
- wchar_t wpathname [MAX_PATH ];
313
- if (xutftowcs_path (wpathname , pathname ) < 0 )
333
+ wchar_t wpathname [MAX_LONG_PATH ];
334
+ if (xutftowcs_long_path (wpathname , pathname ) < 0 )
314
335
return -1 ;
315
336
316
337
if (DeleteFileW (wpathname ))
@@ -342,7 +363,7 @@ static int is_dir_empty(const wchar_t *wpath)
342
363
{
343
364
WIN32_FIND_DATAW findbuf ;
344
365
HANDLE handle ;
345
- wchar_t wbuf [MAX_PATH + 2 ];
366
+ wchar_t wbuf [MAX_LONG_PATH + 2 ];
346
367
wcscpy (wbuf , wpath );
347
368
wcscat (wbuf , L"\\*" );
348
369
handle = FindFirstFileW (wbuf , & findbuf );
@@ -363,7 +384,7 @@ static int is_dir_empty(const wchar_t *wpath)
363
384
int mingw_rmdir (const char * pathname )
364
385
{
365
386
int ret , tries = 0 ;
366
- wchar_t wpathname [MAX_PATH ];
387
+ wchar_t wpathname [MAX_LONG_PATH ];
367
388
struct stat st ;
368
389
369
390
/*
@@ -385,7 +406,7 @@ int mingw_rmdir(const char *pathname)
385
406
return -1 ;
386
407
}
387
408
388
- if (xutftowcs_path (wpathname , pathname ) < 0 )
409
+ if (xutftowcs_long_path (wpathname , pathname ) < 0 )
389
410
return -1 ;
390
411
391
412
while ((ret = _wrmdir (wpathname )) == -1 && tries < ARRAY_SIZE (delay )) {
@@ -464,15 +485,18 @@ static int set_hidden_flag(const wchar_t *path, int set)
464
485
int mingw_mkdir (const char * path , int mode )
465
486
{
466
487
int ret ;
467
- wchar_t wpath [MAX_PATH ];
488
+ wchar_t wpath [MAX_LONG_PATH ];
468
489
469
490
if (!is_valid_win32_path (path , 0 )) {
470
491
errno = EINVAL ;
471
492
return -1 ;
472
493
}
473
494
474
- if (xutftowcs_path (wpath , path ) < 0 )
495
+ /* CreateDirectoryW path limit is 248 (MAX_PATH - 8.3 file name) */
496
+ if (xutftowcs_path_ex (wpath , path , MAX_LONG_PATH , -1 , 248 ,
497
+ are_long_paths_enabled ()) < 0 )
475
498
return -1 ;
499
+
476
500
ret = _wmkdir (wpath );
477
501
if (!ret && needs_hiding (path ))
478
502
return set_hidden_flag (wpath , 1 );
@@ -559,7 +583,7 @@ int mingw_open (const char *filename, int oflags, ...)
559
583
va_list args ;
560
584
unsigned mode ;
561
585
int fd , create = (oflags & (O_CREAT | O_EXCL )) == (O_CREAT | O_EXCL );
562
- wchar_t wfilename [MAX_PATH ];
586
+ wchar_t wfilename [MAX_LONG_PATH ];
563
587
open_fn_t open_fn ;
564
588
565
589
va_start (args , oflags );
@@ -587,7 +611,7 @@ int mingw_open (const char *filename, int oflags, ...)
587
611
588
612
if (filename && !strcmp (filename , "/dev/null" ))
589
613
wcscpy (wfilename , L"nul" );
590
- else if (xutftowcs_path (wfilename , filename ) < 0 )
614
+ else if (xutftowcs_long_path (wfilename , filename ) < 0 )
591
615
return -1 ;
592
616
593
617
fd = open_fn (wfilename , oflags , mode );
@@ -645,14 +669,14 @@ FILE *mingw_fopen (const char *filename, const char *otype)
645
669
{
646
670
int hide = needs_hiding (filename );
647
671
FILE * file ;
648
- wchar_t wfilename [MAX_PATH ], wotype [4 ];
672
+ wchar_t wfilename [MAX_LONG_PATH ], wotype [4 ];
649
673
if (filename && !strcmp (filename , "/dev/null" ))
650
674
wcscpy (wfilename , L"nul" );
651
675
else if (!is_valid_win32_path (filename , 1 )) {
652
676
int create = otype && strchr (otype , 'w' );
653
677
errno = create ? EINVAL : ENOENT ;
654
678
return NULL ;
655
- } else if (xutftowcs_path (wfilename , filename ) < 0 )
679
+ } else if (xutftowcs_long_path (wfilename , filename ) < 0 )
656
680
return NULL ;
657
681
658
682
if (xutftowcs (wotype , otype , ARRAY_SIZE (wotype )) < 0 )
@@ -674,14 +698,14 @@ FILE *mingw_freopen (const char *filename, const char *otype, FILE *stream)
674
698
{
675
699
int hide = needs_hiding (filename );
676
700
FILE * file ;
677
- wchar_t wfilename [MAX_PATH ], wotype [4 ];
701
+ wchar_t wfilename [MAX_LONG_PATH ], wotype [4 ];
678
702
if (filename && !strcmp (filename , "/dev/null" ))
679
703
wcscpy (wfilename , L"nul" );
680
704
else if (!is_valid_win32_path (filename , 1 )) {
681
705
int create = otype && strchr (otype , 'w' );
682
706
errno = create ? EINVAL : ENOENT ;
683
707
return NULL ;
684
- } else if (xutftowcs_path (wfilename , filename ) < 0 )
708
+ } else if (xutftowcs_long_path (wfilename , filename ) < 0 )
685
709
return NULL ;
686
710
687
711
if (xutftowcs (wotype , otype , ARRAY_SIZE (wotype )) < 0 )
@@ -756,27 +780,33 @@ ssize_t mingw_write(int fd, const void *buf, size_t len)
756
780
757
781
int mingw_access (const char * filename , int mode )
758
782
{
759
- wchar_t wfilename [MAX_PATH ];
783
+ wchar_t wfilename [MAX_LONG_PATH ];
760
784
if (!strcmp ("nul" , filename ) || !strcmp ("/dev/null" , filename ))
761
785
return 0 ;
762
- if (xutftowcs_path (wfilename , filename ) < 0 )
786
+ if (xutftowcs_long_path (wfilename , filename ) < 0 )
763
787
return -1 ;
764
788
/* X_OK is not supported by the MSVCRT version */
765
789
return _waccess (wfilename , mode & ~X_OK );
766
790
}
767
791
792
+ /* cached length of current directory for handle_long_path */
793
+ static int current_directory_len = 0 ;
794
+
768
795
int mingw_chdir (const char * dirname )
769
796
{
770
- wchar_t wdirname [MAX_PATH ];
771
- if (xutftowcs_path (wdirname , dirname ) < 0 )
797
+ int result ;
798
+ wchar_t wdirname [MAX_LONG_PATH ];
799
+ if (xutftowcs_long_path (wdirname , dirname ) < 0 )
772
800
return -1 ;
773
- return _wchdir (wdirname );
801
+ result = _wchdir (wdirname );
802
+ current_directory_len = GetCurrentDirectoryW (0 , NULL );
803
+ return result ;
774
804
}
775
805
776
806
int mingw_chmod (const char * filename , int mode )
777
807
{
778
- wchar_t wfilename [MAX_PATH ];
779
- if (xutftowcs_path (wfilename , filename ) < 0 )
808
+ wchar_t wfilename [MAX_LONG_PATH ];
809
+ if (xutftowcs_long_path (wfilename , filename ) < 0 )
780
810
return -1 ;
781
811
return _wchmod (wfilename , mode );
782
812
}
@@ -824,8 +854,8 @@ static int has_valid_directory_prefix(wchar_t *wfilename)
824
854
static int do_lstat (int follow , const char * file_name , struct stat * buf )
825
855
{
826
856
WIN32_FILE_ATTRIBUTE_DATA fdata ;
827
- wchar_t wfilename [MAX_PATH ];
828
- if (xutftowcs_path (wfilename , file_name ) < 0 )
857
+ wchar_t wfilename [MAX_LONG_PATH ];
858
+ if (xutftowcs_long_path (wfilename , file_name ) < 0 )
829
859
return -1 ;
830
860
831
861
if (GetFileAttributesExW (wfilename , GetFileExInfoStandard , & fdata )) {
@@ -996,10 +1026,10 @@ int mingw_utime (const char *file_name, const struct utimbuf *times)
996
1026
FILETIME mft , aft ;
997
1027
int rc ;
998
1028
DWORD attrs ;
999
- wchar_t wfilename [MAX_PATH ];
1029
+ wchar_t wfilename [MAX_LONG_PATH ];
1000
1030
HANDLE osfilehandle ;
1001
1031
1002
- if (xutftowcs_path (wfilename , file_name ) < 0 )
1032
+ if (xutftowcs_long_path (wfilename , file_name ) < 0 )
1003
1033
return -1 ;
1004
1034
1005
1035
/* must have write permission */
@@ -1082,6 +1112,7 @@ char *mingw_mktemp(char *template)
1082
1112
wchar_t wtemplate [MAX_PATH ];
1083
1113
int offset = 0 ;
1084
1114
1115
+ /* we need to return the path, thus no long paths here! */
1085
1116
if (xutftowcs_path (wtemplate , template ) < 0 )
1086
1117
return NULL ;
1087
1118
@@ -1728,6 +1759,10 @@ static pid_t mingw_spawnve_fd(const char *cmd, const char **argv, char **deltaen
1728
1759
1729
1760
if (* argv && !strcmp (cmd , * argv ))
1730
1761
wcmd [0 ] = L'\0' ;
1762
+ /*
1763
+ * Paths to executables and to the current directory do not support
1764
+ * long paths, therefore we cannot use xutftowcs_long_path() here.
1765
+ */
1731
1766
else if (xutftowcs_path (wcmd , cmd ) < 0 )
1732
1767
return -1 ;
1733
1768
if (dir && xutftowcs_path (wdir , dir ) < 0 )
@@ -2376,8 +2411,9 @@ int mingw_rename(const char *pold, const char *pnew)
2376
2411
{
2377
2412
DWORD attrs , gle ;
2378
2413
int tries = 0 ;
2379
- wchar_t wpold [MAX_PATH ], wpnew [MAX_PATH ];
2380
- if (xutftowcs_path (wpold , pold ) < 0 || xutftowcs_path (wpnew , pnew ) < 0 )
2414
+ wchar_t wpold [MAX_LONG_PATH ], wpnew [MAX_LONG_PATH ];
2415
+ if (xutftowcs_long_path (wpold , pold ) < 0 ||
2416
+ xutftowcs_long_path (wpnew , pnew ) < 0 )
2381
2417
return -1 ;
2382
2418
2383
2419
/*
@@ -2691,9 +2727,9 @@ int mingw_raise(int sig)
2691
2727
2692
2728
int link (const char * oldpath , const char * newpath )
2693
2729
{
2694
- wchar_t woldpath [MAX_PATH ], wnewpath [MAX_PATH ];
2695
- if (xutftowcs_path (woldpath , oldpath ) < 0 ||
2696
- xutftowcs_path (wnewpath , newpath ) < 0 )
2730
+ wchar_t woldpath [MAX_LONG_PATH ], wnewpath [MAX_LONG_PATH ];
2731
+ if (xutftowcs_long_path (woldpath , oldpath ) < 0 ||
2732
+ xutftowcs_long_path (wnewpath , newpath ) < 0 )
2697
2733
return -1 ;
2698
2734
2699
2735
if (!CreateHardLinkW (wnewpath , woldpath , NULL )) {
@@ -2761,8 +2797,8 @@ int mingw_is_mount_point(struct strbuf *path)
2761
2797
{
2762
2798
WIN32_FIND_DATAW findbuf = { 0 };
2763
2799
HANDLE handle ;
2764
- wchar_t wfilename [MAX_PATH ];
2765
- int wlen = xutftowcs_path (wfilename , path -> buf );
2800
+ wchar_t wfilename [MAX_LONG_PATH ];
2801
+ int wlen = xutftowcs_long_path (wfilename , path -> buf );
2766
2802
if (wlen < 0 )
2767
2803
die (_ ("could not get long path for '%s'" ), path -> buf );
2768
2804
@@ -2907,9 +2943,9 @@ static size_t append_system_bin_dirs(char *path, size_t size)
2907
2943
2908
2944
static int is_system32_path (const char * path )
2909
2945
{
2910
- WCHAR system32 [MAX_PATH ], wpath [MAX_PATH ];
2946
+ WCHAR system32 [MAX_LONG_PATH ], wpath [MAX_LONG_PATH ];
2911
2947
2912
- if (xutftowcs_path (wpath , path ) < 0 ||
2948
+ if (xutftowcs_long_path (wpath , path ) < 0 ||
2913
2949
!GetSystemDirectoryW (system32 , ARRAY_SIZE (system32 )) ||
2914
2950
_wcsicmp (system32 , wpath ))
2915
2951
return 0 ;
@@ -3277,6 +3313,68 @@ int is_valid_win32_path(const char *path, int allow_literal_nul)
3277
3313
}
3278
3314
}
3279
3315
3316
+ int handle_long_path (wchar_t * path , int len , int max_path , int expand )
3317
+ {
3318
+ int result ;
3319
+ wchar_t buf [MAX_LONG_PATH ];
3320
+
3321
+ /*
3322
+ * we don't need special handling if path is relative to the current
3323
+ * directory, and current directory + path don't exceed the desired
3324
+ * max_path limit. This should cover > 99 % of cases with minimal
3325
+ * performance impact (git almost always uses relative paths).
3326
+ */
3327
+ if ((len < 2 || (!is_dir_sep (path [0 ]) && path [1 ] != ':' )) &&
3328
+ (current_directory_len + len < max_path ))
3329
+ return len ;
3330
+
3331
+ /*
3332
+ * handle everything else:
3333
+ * - absolute paths: "C:\dir\file"
3334
+ * - absolute UNC paths: "\\server\share\dir\file"
3335
+ * - absolute paths on current drive: "\dir\file"
3336
+ * - relative paths on other drive: "X:file"
3337
+ * - prefixed paths: "\\?\...", "\\.\..."
3338
+ */
3339
+
3340
+ /* convert to absolute path using GetFullPathNameW */
3341
+ result = GetFullPathNameW (path , MAX_LONG_PATH , buf , NULL );
3342
+ if (!result ) {
3343
+ errno = err_win_to_posix (GetLastError ());
3344
+ return -1 ;
3345
+ }
3346
+
3347
+ /*
3348
+ * return absolute path if it fits within max_path (even if
3349
+ * "cwd + path" doesn't due to '..' components)
3350
+ */
3351
+ if (result < max_path ) {
3352
+ wcscpy (path , buf );
3353
+ return result ;
3354
+ }
3355
+
3356
+ /* error out if we shouldn't expand the path or buf is too small */
3357
+ if (!expand || result >= MAX_LONG_PATH - 6 ) {
3358
+ errno = ENAMETOOLONG ;
3359
+ return -1 ;
3360
+ }
3361
+
3362
+ /* prefix full path with "\\?\" or "\\?\UNC\" */
3363
+ if (buf [0 ] == '\\' ) {
3364
+ /* ...unless already prefixed */
3365
+ if (buf [1 ] == '\\' && (buf [2 ] == '?' || buf [2 ] == '.' ))
3366
+ return len ;
3367
+
3368
+ wcscpy (path , L"\\\\?\\UNC\\" );
3369
+ wcscpy (path + 8 , buf + 2 );
3370
+ return result + 6 ;
3371
+ } else {
3372
+ wcscpy (path , L"\\\\?\\" );
3373
+ wcscpy (path + 4 , buf );
3374
+ return result + 4 ;
3375
+ }
3376
+ }
3377
+
3280
3378
#if !defined(_MSC_VER )
3281
3379
/*
3282
3380
* Disable MSVCRT command line wildcard expansion (__getmainargs called from
@@ -3438,6 +3536,9 @@ int wmain(int argc, const wchar_t **wargv)
3438
3536
/* initialize Unicode console */
3439
3537
winansi_init ();
3440
3538
3539
+ /* init length of current directory for handle_long_path */
3540
+ current_directory_len = GetCurrentDirectoryW (0 , NULL );
3541
+
3441
3542
/* invoke the real main() using our utf8 version of argv. */
3442
3543
exit_status = main (argc , argv );
3443
3544
0 commit comments