@@ -228,6 +228,7 @@ enum hide_dotfiles_type {
228
228
static enum hide_dotfiles_type hide_dotfiles = HIDE_DOTFILES_DOTGITONLY ;
229
229
static char * unset_environment_variables ;
230
230
int core_fscache ;
231
+ int core_long_paths ;
231
232
232
233
int mingw_core_config (const char * var , const char * value , void * cb )
233
234
{
@@ -244,6 +245,11 @@ int mingw_core_config(const char *var, const char *value, void *cb)
244
245
return 0 ;
245
246
}
246
247
248
+ if (!strcmp (var , "core.longpaths" )) {
249
+ core_long_paths = git_config_bool (var , value );
250
+ return 0 ;
251
+ }
252
+
247
253
if (!strcmp (var , "core.unsetenvvars" )) {
248
254
free (unset_environment_variables );
249
255
unset_environment_variables = xstrdup (value );
@@ -281,8 +287,8 @@ static wchar_t *normalize_ntpath(wchar_t *wbuf)
281
287
int mingw_unlink (const char * pathname )
282
288
{
283
289
int ret , tries = 0 ;
284
- wchar_t wpathname [MAX_PATH ];
285
- if (xutftowcs_path (wpathname , pathname ) < 0 )
290
+ wchar_t wpathname [MAX_LONG_PATH ];
291
+ if (xutftowcs_long_path (wpathname , pathname ) < 0 )
286
292
return -1 ;
287
293
288
294
/* read-only files cannot be removed */
@@ -311,7 +317,7 @@ static int is_dir_empty(const wchar_t *wpath)
311
317
{
312
318
WIN32_FIND_DATAW findbuf ;
313
319
HANDLE handle ;
314
- wchar_t wbuf [MAX_PATH + 2 ];
320
+ wchar_t wbuf [MAX_LONG_PATH + 2 ];
315
321
wcscpy (wbuf , wpath );
316
322
wcscat (wbuf , L"\\*" );
317
323
handle = FindFirstFileW (wbuf , & findbuf );
@@ -332,8 +338,8 @@ static int is_dir_empty(const wchar_t *wpath)
332
338
int mingw_rmdir (const char * pathname )
333
339
{
334
340
int ret , tries = 0 ;
335
- wchar_t wpathname [MAX_PATH ];
336
- if (xutftowcs_path (wpathname , pathname ) < 0 )
341
+ wchar_t wpathname [MAX_LONG_PATH ];
342
+ if (xutftowcs_long_path (wpathname , pathname ) < 0 )
337
343
return -1 ;
338
344
339
345
while ((ret = _wrmdir (wpathname )) == -1 && tries < ARRAY_SIZE (delay )) {
@@ -408,9 +414,12 @@ static int set_hidden_flag(const wchar_t *path, int set)
408
414
int mingw_mkdir (const char * path , int mode )
409
415
{
410
416
int ret ;
411
- wchar_t wpath [MAX_PATH ];
412
- if (xutftowcs_path (wpath , path ) < 0 )
417
+ wchar_t wpath [MAX_LONG_PATH ];
418
+ /* CreateDirectoryW path limit is 248 (MAX_PATH - 8.3 file name) */
419
+ if (xutftowcs_path_ex (wpath , path , MAX_LONG_PATH , -1 , 248 ,
420
+ core_long_paths ) < 0 )
413
421
return -1 ;
422
+
414
423
ret = _wmkdir (wpath );
415
424
if (!ret && needs_hiding (path ))
416
425
return set_hidden_flag (wpath , 1 );
@@ -483,7 +492,7 @@ int mingw_open (const char *filename, int oflags, ...)
483
492
va_list args ;
484
493
unsigned mode ;
485
494
int fd ;
486
- wchar_t wfilename [MAX_PATH ];
495
+ wchar_t wfilename [MAX_LONG_PATH ];
487
496
open_fn_t open_fn ;
488
497
489
498
va_start (args , oflags );
@@ -498,7 +507,7 @@ int mingw_open (const char *filename, int oflags, ...)
498
507
else
499
508
open_fn = _wopen ;
500
509
501
- if (xutftowcs_path (wfilename , filename ) < 0 )
510
+ if (xutftowcs_long_path (wfilename , filename ) < 0 )
502
511
return -1 ;
503
512
fd = open_fn (wfilename , oflags , mode );
504
513
@@ -555,10 +564,10 @@ FILE *mingw_fopen (const char *filename, const char *otype)
555
564
{
556
565
int hide = needs_hiding (filename );
557
566
FILE * file ;
558
- wchar_t wfilename [MAX_PATH ], wotype [4 ];
567
+ wchar_t wfilename [MAX_LONG_PATH ], wotype [4 ];
559
568
if (filename && !strcmp (filename , "/dev/null" ))
560
569
filename = "nul" ;
561
- if (xutftowcs_path (wfilename , filename ) < 0 ||
570
+ if (xutftowcs_long_path (wfilename , filename ) < 0 ||
562
571
xutftowcs (wotype , otype , ARRAY_SIZE (wotype )) < 0 )
563
572
return NULL ;
564
573
if (hide && !access (filename , F_OK ) && set_hidden_flag (wfilename , 0 )) {
@@ -577,10 +586,10 @@ FILE *mingw_freopen (const char *filename, const char *otype, FILE *stream)
577
586
{
578
587
int hide = needs_hiding (filename );
579
588
FILE * file ;
580
- wchar_t wfilename [MAX_PATH ], wotype [4 ];
589
+ wchar_t wfilename [MAX_LONG_PATH ], wotype [4 ];
581
590
if (filename && !strcmp (filename , "/dev/null" ))
582
591
filename = "nul" ;
583
- if (xutftowcs_path (wfilename , filename ) < 0 ||
592
+ if (xutftowcs_long_path (wfilename , filename ) < 0 ||
584
593
xutftowcs (wotype , otype , ARRAY_SIZE (wotype )) < 0 )
585
594
return NULL ;
586
595
if (hide && !access (filename , F_OK ) && set_hidden_flag (wfilename , 0 )) {
@@ -634,25 +643,31 @@ ssize_t mingw_write(int fd, const void *buf, size_t len)
634
643
635
644
int mingw_access (const char * filename , int mode )
636
645
{
637
- wchar_t wfilename [MAX_PATH ];
638
- if (xutftowcs_path (wfilename , filename ) < 0 )
646
+ wchar_t wfilename [MAX_LONG_PATH ];
647
+ if (xutftowcs_long_path (wfilename , filename ) < 0 )
639
648
return -1 ;
640
649
/* X_OK is not supported by the MSVCRT version */
641
650
return _waccess (wfilename , mode & ~X_OK );
642
651
}
643
652
653
+ /* cached length of current directory for handle_long_path */
654
+ static int current_directory_len = 0 ;
655
+
644
656
int mingw_chdir (const char * dirname )
645
657
{
646
- wchar_t wdirname [MAX_PATH ];
647
- if (xutftowcs_path (wdirname , dirname ) < 0 )
658
+ int result ;
659
+ wchar_t wdirname [MAX_LONG_PATH ];
660
+ if (xutftowcs_long_path (wdirname , dirname ) < 0 )
648
661
return -1 ;
649
- return _wchdir (wdirname );
662
+ result = _wchdir (wdirname );
663
+ current_directory_len = GetCurrentDirectoryW (0 , NULL );
664
+ return result ;
650
665
}
651
666
652
667
int mingw_chmod (const char * filename , int mode )
653
668
{
654
- wchar_t wfilename [MAX_PATH ];
655
- if (xutftowcs_path (wfilename , filename ) < 0 )
669
+ wchar_t wfilename [MAX_LONG_PATH ];
670
+ if (xutftowcs_long_path (wfilename , filename ) < 0 )
656
671
return -1 ;
657
672
return _wchmod (wfilename , mode );
658
673
}
@@ -700,8 +715,8 @@ static int has_valid_directory_prefix(wchar_t *wfilename)
700
715
static int do_lstat (int follow , const char * file_name , struct stat * buf )
701
716
{
702
717
WIN32_FILE_ATTRIBUTE_DATA fdata ;
703
- wchar_t wfilename [MAX_PATH ];
704
- if (xutftowcs_path (wfilename , file_name ) < 0 )
718
+ wchar_t wfilename [MAX_LONG_PATH ];
719
+ if (xutftowcs_long_path (wfilename , file_name ) < 0 )
705
720
return -1 ;
706
721
707
722
if (GetFileAttributesExW (wfilename , GetFileExInfoStandard , & fdata )) {
@@ -772,7 +787,7 @@ static int do_lstat(int follow, const char *file_name, struct stat *buf)
772
787
static int do_stat_internal (int follow , const char * file_name , struct stat * buf )
773
788
{
774
789
int namelen ;
775
- char alt_name [PATH_MAX ];
790
+ char alt_name [MAX_LONG_PATH ];
776
791
777
792
if (!do_lstat (follow , file_name , buf ))
778
793
return 0 ;
@@ -788,7 +803,7 @@ static int do_stat_internal(int follow, const char *file_name, struct stat *buf)
788
803
return -1 ;
789
804
while (namelen && file_name [namelen - 1 ] == '/' )
790
805
-- namelen ;
791
- if (!namelen || namelen >= PATH_MAX )
806
+ if (!namelen || namelen >= MAX_LONG_PATH )
792
807
return -1 ;
793
808
794
809
memcpy (alt_name , file_name , namelen );
@@ -872,8 +887,8 @@ int mingw_utime (const char *file_name, const struct utimbuf *times)
872
887
FILETIME mft , aft ;
873
888
int fh , rc ;
874
889
DWORD attrs ;
875
- wchar_t wfilename [MAX_PATH ];
876
- if (xutftowcs_path (wfilename , file_name ) < 0 )
890
+ wchar_t wfilename [MAX_LONG_PATH ];
891
+ if (xutftowcs_long_path (wfilename , file_name ) < 0 )
877
892
return -1 ;
878
893
879
894
/* must have write permission */
@@ -934,6 +949,7 @@ char *mingw_mktemp(char *template)
934
949
wchar_t wtemplate [MAX_PATH ];
935
950
int offset = 0 ;
936
951
952
+ /* we need to return the path, thus no long paths here! */
937
953
if (xutftowcs_path (wtemplate , template ) < 0 )
938
954
return NULL ;
939
955
@@ -1457,6 +1473,10 @@ static pid_t mingw_spawnve_fd(const char *cmd, const char **argv, char **deltaen
1457
1473
1458
1474
if (* argv && !strcmp (cmd , * argv ))
1459
1475
wcmd [0 ] = L'\0' ;
1476
+ /*
1477
+ * Paths to executables and to the current directory do not support
1478
+ * long paths, therefore we cannot use xutftowcs_long_path() here.
1479
+ */
1460
1480
else if (xutftowcs_path (wcmd , cmd ) < 0 )
1461
1481
return -1 ;
1462
1482
if (dir && xutftowcs_path (wdir , dir ) < 0 )
@@ -1864,8 +1884,9 @@ int mingw_rename(const char *pold, const char *pnew)
1864
1884
{
1865
1885
DWORD attrs , gle ;
1866
1886
int tries = 0 ;
1867
- wchar_t wpold [MAX_PATH ], wpnew [MAX_PATH ];
1868
- if (xutftowcs_path (wpold , pold ) < 0 || xutftowcs_path (wpnew , pnew ) < 0 )
1887
+ wchar_t wpold [MAX_LONG_PATH ], wpnew [MAX_LONG_PATH ];
1888
+ if (xutftowcs_long_path (wpold , pold ) < 0 ||
1889
+ xutftowcs_long_path (wpnew , pnew ) < 0 )
1869
1890
return -1 ;
1870
1891
1871
1892
/*
@@ -2174,9 +2195,9 @@ int mingw_raise(int sig)
2174
2195
2175
2196
int link (const char * oldpath , const char * newpath )
2176
2197
{
2177
- wchar_t woldpath [MAX_PATH ], wnewpath [MAX_PATH ];
2178
- if (xutftowcs_path (woldpath , oldpath ) < 0 ||
2179
- xutftowcs_path (wnewpath , newpath ) < 0 )
2198
+ wchar_t woldpath [MAX_LONG_PATH ], wnewpath [MAX_LONG_PATH ];
2199
+ if (xutftowcs_long_path (woldpath , oldpath ) < 0 ||
2200
+ xutftowcs_long_path (wnewpath , newpath ) < 0 )
2180
2201
return -1 ;
2181
2202
2182
2203
if (!CreateHardLinkW (wnewpath , woldpath , NULL )) {
@@ -2376,6 +2397,68 @@ static void setup_windows_environment(void)
2376
2397
}
2377
2398
}
2378
2399
2400
+ int handle_long_path (wchar_t * path , int len , int max_path , int expand )
2401
+ {
2402
+ int result ;
2403
+ wchar_t buf [MAX_LONG_PATH ];
2404
+
2405
+ /*
2406
+ * we don't need special handling if path is relative to the current
2407
+ * directory, and current directory + path don't exceed the desired
2408
+ * max_path limit. This should cover > 99 % of cases with minimal
2409
+ * performance impact (git almost always uses relative paths).
2410
+ */
2411
+ if ((len < 2 || (!is_dir_sep (path [0 ]) && path [1 ] != ':' )) &&
2412
+ (current_directory_len + len < max_path ))
2413
+ return len ;
2414
+
2415
+ /*
2416
+ * handle everything else:
2417
+ * - absolute paths: "C:\dir\file"
2418
+ * - absolute UNC paths: "\\server\share\dir\file"
2419
+ * - absolute paths on current drive: "\dir\file"
2420
+ * - relative paths on other drive: "X:file"
2421
+ * - prefixed paths: "\\?\...", "\\.\..."
2422
+ */
2423
+
2424
+ /* convert to absolute path using GetFullPathNameW */
2425
+ result = GetFullPathNameW (path , MAX_LONG_PATH , buf , NULL );
2426
+ if (!result ) {
2427
+ errno = err_win_to_posix (GetLastError ());
2428
+ return -1 ;
2429
+ }
2430
+
2431
+ /*
2432
+ * return absolute path if it fits within max_path (even if
2433
+ * "cwd + path" doesn't due to '..' components)
2434
+ */
2435
+ if (result < max_path ) {
2436
+ wcscpy (path , buf );
2437
+ return result ;
2438
+ }
2439
+
2440
+ /* error out if we shouldn't expand the path or buf is too small */
2441
+ if (!expand || result >= MAX_LONG_PATH - 6 ) {
2442
+ errno = ENAMETOOLONG ;
2443
+ return -1 ;
2444
+ }
2445
+
2446
+ /* prefix full path with "\\?\" or "\\?\UNC\" */
2447
+ if (buf [0 ] == '\\' ) {
2448
+ /* ...unless already prefixed */
2449
+ if (buf [1 ] == '\\' && (buf [2 ] == '?' || buf [2 ] == '.' ))
2450
+ return len ;
2451
+
2452
+ wcscpy (path , L"\\\\?\\UNC\\" );
2453
+ wcscpy (path + 8 , buf + 2 );
2454
+ return result + 6 ;
2455
+ } else {
2456
+ wcscpy (path , L"\\\\?\\" );
2457
+ wcscpy (path + 4 , buf );
2458
+ return result + 4 ;
2459
+ }
2460
+ }
2461
+
2379
2462
#if !defined(_MSC_VER )
2380
2463
/*
2381
2464
* Disable MSVCRT command line wildcard expansion (__getmainargs called from
@@ -2534,6 +2617,9 @@ int wmain(int argc, const wchar_t **wargv)
2534
2617
/* initialize Unicode console */
2535
2618
winansi_init ();
2536
2619
2620
+ /* init length of current directory for handle_long_path */
2621
+ current_directory_len = GetCurrentDirectoryW (0 , NULL );
2622
+
2537
2623
/* invoke the real main() using our utf8 version of argv. */
2538
2624
exit_status = main (argc , argv );
2539
2625
0 commit comments