@@ -235,6 +235,7 @@ static int core_restrict_inherited_handles = -1;
235
235
static enum hide_dotfiles_type hide_dotfiles = HIDE_DOTFILES_DOTGITONLY ;
236
236
static char * unset_environment_variables ;
237
237
int core_fscache ;
238
+ int core_long_paths ;
238
239
239
240
int mingw_core_config (const char * var , const char * value , void * cb )
240
241
{
@@ -251,6 +252,11 @@ int mingw_core_config(const char *var, const char *value, void *cb)
251
252
return 0 ;
252
253
}
253
254
255
+ if (!strcmp (var , "core.longpaths" )) {
256
+ core_long_paths = git_config_bool (var , value );
257
+ return 0 ;
258
+ }
259
+
254
260
if (!strcmp (var , "core.unsetenvvars" )) {
255
261
free (unset_environment_variables );
256
262
unset_environment_variables = xstrdup (value );
@@ -297,8 +303,8 @@ static wchar_t *normalize_ntpath(wchar_t *wbuf)
297
303
int mingw_unlink (const char * pathname )
298
304
{
299
305
int ret , tries = 0 ;
300
- wchar_t wpathname [MAX_PATH ];
301
- if (xutftowcs_path (wpathname , pathname ) < 0 )
306
+ wchar_t wpathname [MAX_LONG_PATH ];
307
+ if (xutftowcs_long_path (wpathname , pathname ) < 0 )
302
308
return -1 ;
303
309
304
310
if (DeleteFileW (wpathname ))
@@ -330,7 +336,7 @@ static int is_dir_empty(const wchar_t *wpath)
330
336
{
331
337
WIN32_FIND_DATAW findbuf ;
332
338
HANDLE handle ;
333
- wchar_t wbuf [MAX_PATH + 2 ];
339
+ wchar_t wbuf [MAX_LONG_PATH + 2 ];
334
340
wcscpy (wbuf , wpath );
335
341
wcscat (wbuf , L"\\*" );
336
342
handle = FindFirstFileW (wbuf , & findbuf );
@@ -351,7 +357,7 @@ static int is_dir_empty(const wchar_t *wpath)
351
357
int mingw_rmdir (const char * pathname )
352
358
{
353
359
int ret , tries = 0 ;
354
- wchar_t wpathname [MAX_PATH ];
360
+ wchar_t wpathname [MAX_LONG_PATH ];
355
361
struct stat st ;
356
362
357
363
/*
@@ -373,7 +379,7 @@ int mingw_rmdir(const char *pathname)
373
379
return -1 ;
374
380
}
375
381
376
- if (xutftowcs_path (wpathname , pathname ) < 0 )
382
+ if (xutftowcs_long_path (wpathname , pathname ) < 0 )
377
383
return -1 ;
378
384
379
385
while ((ret = _wrmdir (wpathname )) == -1 && tries < ARRAY_SIZE (delay )) {
@@ -452,15 +458,18 @@ static int set_hidden_flag(const wchar_t *path, int set)
452
458
int mingw_mkdir (const char * path , int mode )
453
459
{
454
460
int ret ;
455
- wchar_t wpath [MAX_PATH ];
461
+ wchar_t wpath [MAX_LONG_PATH ];
456
462
457
463
if (!is_valid_win32_path (path , 0 )) {
458
464
errno = EINVAL ;
459
465
return -1 ;
460
466
}
461
467
462
- if (xutftowcs_path (wpath , path ) < 0 )
468
+ /* CreateDirectoryW path limit is 248 (MAX_PATH - 8.3 file name) */
469
+ if (xutftowcs_path_ex (wpath , path , MAX_LONG_PATH , -1 , 248 ,
470
+ core_long_paths ) < 0 )
463
471
return -1 ;
472
+
464
473
ret = _wmkdir (wpath );
465
474
if (!ret && needs_hiding (path ))
466
475
return set_hidden_flag (wpath , 1 );
@@ -546,7 +555,7 @@ int mingw_open (const char *filename, int oflags, ...)
546
555
va_list args ;
547
556
unsigned mode ;
548
557
int fd , create = (oflags & (O_CREAT | O_EXCL )) == (O_CREAT | O_EXCL );
549
- wchar_t wfilename [MAX_PATH ];
558
+ wchar_t wfilename [MAX_LONG_PATH ];
550
559
open_fn_t open_fn ;
551
560
552
561
va_start (args , oflags );
@@ -565,7 +574,7 @@ int mingw_open (const char *filename, int oflags, ...)
565
574
566
575
if (filename && !strcmp (filename , "/dev/null" ))
567
576
wcscpy (wfilename , L"nul" );
568
- else if (xutftowcs_path (wfilename , filename ) < 0 )
577
+ else if (xutftowcs_long_path (wfilename , filename ) < 0 )
569
578
return -1 ;
570
579
571
580
fd = open_fn (wfilename , oflags , mode );
@@ -623,14 +632,14 @@ FILE *mingw_fopen (const char *filename, const char *otype)
623
632
{
624
633
int hide = needs_hiding (filename );
625
634
FILE * file ;
626
- wchar_t wfilename [MAX_PATH ], wotype [4 ];
635
+ wchar_t wfilename [MAX_LONG_PATH ], wotype [4 ];
627
636
if (filename && !strcmp (filename , "/dev/null" ))
628
637
wcscpy (wfilename , L"nul" );
629
638
else if (!is_valid_win32_path (filename , 1 )) {
630
639
int create = otype && strchr (otype , 'w' );
631
640
errno = create ? EINVAL : ENOENT ;
632
641
return NULL ;
633
- } else if (xutftowcs_path (wfilename , filename ) < 0 )
642
+ } else if (xutftowcs_long_path (wfilename , filename ) < 0 )
634
643
return NULL ;
635
644
636
645
if (xutftowcs (wotype , otype , ARRAY_SIZE (wotype )) < 0 )
@@ -652,14 +661,14 @@ FILE *mingw_freopen (const char *filename, const char *otype, FILE *stream)
652
661
{
653
662
int hide = needs_hiding (filename );
654
663
FILE * file ;
655
- wchar_t wfilename [MAX_PATH ], wotype [4 ];
664
+ wchar_t wfilename [MAX_LONG_PATH ], wotype [4 ];
656
665
if (filename && !strcmp (filename , "/dev/null" ))
657
666
wcscpy (wfilename , L"nul" );
658
667
else if (!is_valid_win32_path (filename , 1 )) {
659
668
int create = otype && strchr (otype , 'w' );
660
669
errno = create ? EINVAL : ENOENT ;
661
670
return NULL ;
662
- } else if (xutftowcs_path (wfilename , filename ) < 0 )
671
+ } else if (xutftowcs_long_path (wfilename , filename ) < 0 )
663
672
return NULL ;
664
673
665
674
if (xutftowcs (wotype , otype , ARRAY_SIZE (wotype )) < 0 )
@@ -716,27 +725,33 @@ ssize_t mingw_write(int fd, const void *buf, size_t len)
716
725
717
726
int mingw_access (const char * filename , int mode )
718
727
{
719
- wchar_t wfilename [MAX_PATH ];
728
+ wchar_t wfilename [MAX_LONG_PATH ];
720
729
if (!strcmp ("nul" , filename ) || !strcmp ("/dev/null" , filename ))
721
730
return 0 ;
722
- if (xutftowcs_path (wfilename , filename ) < 0 )
731
+ if (xutftowcs_long_path (wfilename , filename ) < 0 )
723
732
return -1 ;
724
733
/* X_OK is not supported by the MSVCRT version */
725
734
return _waccess (wfilename , mode & ~X_OK );
726
735
}
727
736
737
+ /* cached length of current directory for handle_long_path */
738
+ static int current_directory_len = 0 ;
739
+
728
740
int mingw_chdir (const char * dirname )
729
741
{
730
- wchar_t wdirname [MAX_PATH ];
731
- if (xutftowcs_path (wdirname , dirname ) < 0 )
742
+ int result ;
743
+ wchar_t wdirname [MAX_LONG_PATH ];
744
+ if (xutftowcs_long_path (wdirname , dirname ) < 0 )
732
745
return -1 ;
733
- return _wchdir (wdirname );
746
+ result = _wchdir (wdirname );
747
+ current_directory_len = GetCurrentDirectoryW (0 , NULL );
748
+ return result ;
734
749
}
735
750
736
751
int mingw_chmod (const char * filename , int mode )
737
752
{
738
- wchar_t wfilename [MAX_PATH ];
739
- if (xutftowcs_path (wfilename , filename ) < 0 )
753
+ wchar_t wfilename [MAX_LONG_PATH ];
754
+ if (xutftowcs_long_path (wfilename , filename ) < 0 )
740
755
return -1 ;
741
756
return _wchmod (wfilename , mode );
742
757
}
@@ -784,8 +799,8 @@ static int has_valid_directory_prefix(wchar_t *wfilename)
784
799
static int do_lstat (int follow , const char * file_name , struct stat * buf )
785
800
{
786
801
WIN32_FILE_ATTRIBUTE_DATA fdata ;
787
- wchar_t wfilename [MAX_PATH ];
788
- if (xutftowcs_path (wfilename , file_name ) < 0 )
802
+ wchar_t wfilename [MAX_LONG_PATH ];
803
+ if (xutftowcs_long_path (wfilename , file_name ) < 0 )
789
804
return -1 ;
790
805
791
806
if (GetFileAttributesExW (wfilename , GetFileExInfoStandard , & fdata )) {
@@ -956,10 +971,10 @@ int mingw_utime (const char *file_name, const struct utimbuf *times)
956
971
FILETIME mft , aft ;
957
972
int rc ;
958
973
DWORD attrs ;
959
- wchar_t wfilename [MAX_PATH ];
974
+ wchar_t wfilename [MAX_LONG_PATH ];
960
975
HANDLE osfilehandle ;
961
976
962
- if (xutftowcs_path (wfilename , file_name ) < 0 )
977
+ if (xutftowcs_long_path (wfilename , file_name ) < 0 )
963
978
return -1 ;
964
979
965
980
/* must have write permission */
@@ -1042,6 +1057,7 @@ char *mingw_mktemp(char *template)
1042
1057
wchar_t wtemplate [MAX_PATH ];
1043
1058
int offset = 0 ;
1044
1059
1060
+ /* we need to return the path, thus no long paths here! */
1045
1061
if (xutftowcs_path (wtemplate , template ) < 0 )
1046
1062
return NULL ;
1047
1063
@@ -1679,6 +1695,10 @@ static pid_t mingw_spawnve_fd(const char *cmd, const char **argv, char **deltaen
1679
1695
1680
1696
if (* argv && !strcmp (cmd , * argv ))
1681
1697
wcmd [0 ] = L'\0' ;
1698
+ /*
1699
+ * Paths to executables and to the current directory do not support
1700
+ * long paths, therefore we cannot use xutftowcs_long_path() here.
1701
+ */
1682
1702
else if (xutftowcs_path (wcmd , cmd ) < 0 )
1683
1703
return -1 ;
1684
1704
if (dir && xutftowcs_path (wdir , dir ) < 0 )
@@ -2330,8 +2350,9 @@ int mingw_rename(const char *pold, const char *pnew)
2330
2350
{
2331
2351
DWORD attrs , gle ;
2332
2352
int tries = 0 ;
2333
- wchar_t wpold [MAX_PATH ], wpnew [MAX_PATH ];
2334
- if (xutftowcs_path (wpold , pold ) < 0 || xutftowcs_path (wpnew , pnew ) < 0 )
2353
+ wchar_t wpold [MAX_LONG_PATH ], wpnew [MAX_LONG_PATH ];
2354
+ if (xutftowcs_long_path (wpold , pold ) < 0 ||
2355
+ xutftowcs_long_path (wpnew , pnew ) < 0 )
2335
2356
return -1 ;
2336
2357
2337
2358
/*
@@ -2645,9 +2666,9 @@ int mingw_raise(int sig)
2645
2666
2646
2667
int link (const char * oldpath , const char * newpath )
2647
2668
{
2648
- wchar_t woldpath [MAX_PATH ], wnewpath [MAX_PATH ];
2649
- if (xutftowcs_path (woldpath , oldpath ) < 0 ||
2650
- xutftowcs_path (wnewpath , newpath ) < 0 )
2669
+ wchar_t woldpath [MAX_LONG_PATH ], wnewpath [MAX_LONG_PATH ];
2670
+ if (xutftowcs_long_path (woldpath , oldpath ) < 0 ||
2671
+ xutftowcs_long_path (wnewpath , newpath ) < 0 )
2651
2672
return -1 ;
2652
2673
2653
2674
if (!CreateHardLinkW (wnewpath , woldpath , NULL )) {
@@ -2715,8 +2736,8 @@ int mingw_is_mount_point(struct strbuf *path)
2715
2736
{
2716
2737
WIN32_FIND_DATAW findbuf = { 0 };
2717
2738
HANDLE handle ;
2718
- wchar_t wfilename [MAX_PATH ];
2719
- int wlen = xutftowcs_path (wfilename , path -> buf );
2739
+ wchar_t wfilename [MAX_LONG_PATH ];
2740
+ int wlen = xutftowcs_long_path (wfilename , path -> buf );
2720
2741
if (wlen < 0 )
2721
2742
die (_ ("could not get long path for '%s'" ), path -> buf );
2722
2743
@@ -2861,9 +2882,9 @@ static size_t append_system_bin_dirs(char *path, size_t size)
2861
2882
2862
2883
static int is_system32_path (const char * path )
2863
2884
{
2864
- WCHAR system32 [MAX_PATH ], wpath [MAX_PATH ];
2885
+ WCHAR system32 [MAX_LONG_PATH ], wpath [MAX_LONG_PATH ];
2865
2886
2866
- if (xutftowcs_path (wpath , path ) < 0 ||
2887
+ if (xutftowcs_long_path (wpath , path ) < 0 ||
2867
2888
!GetSystemDirectoryW (system32 , ARRAY_SIZE (system32 )) ||
2868
2889
_wcsicmp (system32 , wpath ))
2869
2890
return 0 ;
@@ -3205,6 +3226,68 @@ int is_valid_win32_path(const char *path, int allow_literal_nul)
3205
3226
}
3206
3227
}
3207
3228
3229
+ int handle_long_path (wchar_t * path , int len , int max_path , int expand )
3230
+ {
3231
+ int result ;
3232
+ wchar_t buf [MAX_LONG_PATH ];
3233
+
3234
+ /*
3235
+ * we don't need special handling if path is relative to the current
3236
+ * directory, and current directory + path don't exceed the desired
3237
+ * max_path limit. This should cover > 99 % of cases with minimal
3238
+ * performance impact (git almost always uses relative paths).
3239
+ */
3240
+ if ((len < 2 || (!is_dir_sep (path [0 ]) && path [1 ] != ':' )) &&
3241
+ (current_directory_len + len < max_path ))
3242
+ return len ;
3243
+
3244
+ /*
3245
+ * handle everything else:
3246
+ * - absolute paths: "C:\dir\file"
3247
+ * - absolute UNC paths: "\\server\share\dir\file"
3248
+ * - absolute paths on current drive: "\dir\file"
3249
+ * - relative paths on other drive: "X:file"
3250
+ * - prefixed paths: "\\?\...", "\\.\..."
3251
+ */
3252
+
3253
+ /* convert to absolute path using GetFullPathNameW */
3254
+ result = GetFullPathNameW (path , MAX_LONG_PATH , buf , NULL );
3255
+ if (!result ) {
3256
+ errno = err_win_to_posix (GetLastError ());
3257
+ return -1 ;
3258
+ }
3259
+
3260
+ /*
3261
+ * return absolute path if it fits within max_path (even if
3262
+ * "cwd + path" doesn't due to '..' components)
3263
+ */
3264
+ if (result < max_path ) {
3265
+ wcscpy (path , buf );
3266
+ return result ;
3267
+ }
3268
+
3269
+ /* error out if we shouldn't expand the path or buf is too small */
3270
+ if (!expand || result >= MAX_LONG_PATH - 6 ) {
3271
+ errno = ENAMETOOLONG ;
3272
+ return -1 ;
3273
+ }
3274
+
3275
+ /* prefix full path with "\\?\" or "\\?\UNC\" */
3276
+ if (buf [0 ] == '\\' ) {
3277
+ /* ...unless already prefixed */
3278
+ if (buf [1 ] == '\\' && (buf [2 ] == '?' || buf [2 ] == '.' ))
3279
+ return len ;
3280
+
3281
+ wcscpy (path , L"\\\\?\\UNC\\" );
3282
+ wcscpy (path + 8 , buf + 2 );
3283
+ return result + 6 ;
3284
+ } else {
3285
+ wcscpy (path , L"\\\\?\\" );
3286
+ wcscpy (path + 4 , buf );
3287
+ return result + 4 ;
3288
+ }
3289
+ }
3290
+
3208
3291
#if !defined(_MSC_VER )
3209
3292
/*
3210
3293
* Disable MSVCRT command line wildcard expansion (__getmainargs called from
@@ -3366,6 +3449,9 @@ int wmain(int argc, const wchar_t **wargv)
3366
3449
/* initialize Unicode console */
3367
3450
winansi_init ();
3368
3451
3452
+ /* init length of current directory for handle_long_path */
3453
+ current_directory_len = GetCurrentDirectoryW (0 , NULL );
3454
+
3369
3455
/* invoke the real main() using our utf8 version of argv. */
3370
3456
exit_status = main (argc , argv );
3371
3457
0 commit comments