@@ -233,6 +233,7 @@ static int core_restrict_inherited_handles = -1;
233
233
static enum hide_dotfiles_type hide_dotfiles = HIDE_DOTFILES_DOTGITONLY ;
234
234
static char * unset_environment_variables ;
235
235
int core_fscache ;
236
+ int core_long_paths ;
236
237
237
238
int mingw_core_config (const char * var , const char * value , void * cb )
238
239
{
@@ -249,6 +250,11 @@ int mingw_core_config(const char *var, const char *value, void *cb)
249
250
return 0 ;
250
251
}
251
252
253
+ if (!strcmp (var , "core.longpaths" )) {
254
+ core_long_paths = git_config_bool (var , value );
255
+ return 0 ;
256
+ }
257
+
252
258
if (!strcmp (var , "core.unsetenvvars" )) {
253
259
free (unset_environment_variables );
254
260
unset_environment_variables = xstrdup (value );
@@ -295,8 +301,8 @@ static wchar_t *normalize_ntpath(wchar_t *wbuf)
295
301
int mingw_unlink (const char * pathname )
296
302
{
297
303
int ret , tries = 0 ;
298
- wchar_t wpathname [MAX_PATH ];
299
- if (xutftowcs_path (wpathname , pathname ) < 0 )
304
+ wchar_t wpathname [MAX_LONG_PATH ];
305
+ if (xutftowcs_long_path (wpathname , pathname ) < 0 )
300
306
return -1 ;
301
307
302
308
if (DeleteFileW (wpathname ))
@@ -328,7 +334,7 @@ static int is_dir_empty(const wchar_t *wpath)
328
334
{
329
335
WIN32_FIND_DATAW findbuf ;
330
336
HANDLE handle ;
331
- wchar_t wbuf [MAX_PATH + 2 ];
337
+ wchar_t wbuf [MAX_LONG_PATH + 2 ];
332
338
wcscpy (wbuf , wpath );
333
339
wcscat (wbuf , L"\\*" );
334
340
handle = FindFirstFileW (wbuf , & findbuf );
@@ -349,7 +355,7 @@ static int is_dir_empty(const wchar_t *wpath)
349
355
int mingw_rmdir (const char * pathname )
350
356
{
351
357
int ret , tries = 0 ;
352
- wchar_t wpathname [MAX_PATH ];
358
+ wchar_t wpathname [MAX_LONG_PATH ];
353
359
struct stat st ;
354
360
355
361
/*
@@ -371,7 +377,7 @@ int mingw_rmdir(const char *pathname)
371
377
return -1 ;
372
378
}
373
379
374
- if (xutftowcs_path (wpathname , pathname ) < 0 )
380
+ if (xutftowcs_long_path (wpathname , pathname ) < 0 )
375
381
return -1 ;
376
382
377
383
while ((ret = _wrmdir (wpathname )) == -1 && tries < ARRAY_SIZE (delay )) {
@@ -450,15 +456,18 @@ static int set_hidden_flag(const wchar_t *path, int set)
450
456
int mingw_mkdir (const char * path , int mode )
451
457
{
452
458
int ret ;
453
- wchar_t wpath [MAX_PATH ];
459
+ wchar_t wpath [MAX_LONG_PATH ];
454
460
455
461
if (!is_valid_win32_path (path , 0 )) {
456
462
errno = EINVAL ;
457
463
return -1 ;
458
464
}
459
465
460
- if (xutftowcs_path (wpath , path ) < 0 )
466
+ /* CreateDirectoryW path limit is 248 (MAX_PATH - 8.3 file name) */
467
+ if (xutftowcs_path_ex (wpath , path , MAX_LONG_PATH , -1 , 248 ,
468
+ core_long_paths ) < 0 )
461
469
return -1 ;
470
+
462
471
ret = _wmkdir (wpath );
463
472
if (!ret && needs_hiding (path ))
464
473
return set_hidden_flag (wpath , 1 );
@@ -544,7 +553,7 @@ int mingw_open (const char *filename, int oflags, ...)
544
553
va_list args ;
545
554
unsigned mode ;
546
555
int fd , create = (oflags & (O_CREAT | O_EXCL )) == (O_CREAT | O_EXCL );
547
- wchar_t wfilename [MAX_PATH ];
556
+ wchar_t wfilename [MAX_LONG_PATH ];
548
557
open_fn_t open_fn ;
549
558
550
559
va_start (args , oflags );
@@ -563,7 +572,7 @@ int mingw_open (const char *filename, int oflags, ...)
563
572
564
573
if (filename && !strcmp (filename , "/dev/null" ))
565
574
wcscpy (wfilename , L"nul" );
566
- else if (xutftowcs_path (wfilename , filename ) < 0 )
575
+ else if (xutftowcs_long_path (wfilename , filename ) < 0 )
567
576
return -1 ;
568
577
569
578
fd = open_fn (wfilename , oflags , mode );
@@ -621,14 +630,14 @@ FILE *mingw_fopen (const char *filename, const char *otype)
621
630
{
622
631
int hide = needs_hiding (filename );
623
632
FILE * file ;
624
- wchar_t wfilename [MAX_PATH ], wotype [4 ];
633
+ wchar_t wfilename [MAX_LONG_PATH ], wotype [4 ];
625
634
if (filename && !strcmp (filename , "/dev/null" ))
626
635
wcscpy (wfilename , L"nul" );
627
636
else if (!is_valid_win32_path (filename , 1 )) {
628
637
int create = otype && strchr (otype , 'w' );
629
638
errno = create ? EINVAL : ENOENT ;
630
639
return NULL ;
631
- } else if (xutftowcs_path (wfilename , filename ) < 0 )
640
+ } else if (xutftowcs_long_path (wfilename , filename ) < 0 )
632
641
return NULL ;
633
642
634
643
if (xutftowcs (wotype , otype , ARRAY_SIZE (wotype )) < 0 )
@@ -650,14 +659,14 @@ FILE *mingw_freopen (const char *filename, const char *otype, FILE *stream)
650
659
{
651
660
int hide = needs_hiding (filename );
652
661
FILE * file ;
653
- wchar_t wfilename [MAX_PATH ], wotype [4 ];
662
+ wchar_t wfilename [MAX_LONG_PATH ], wotype [4 ];
654
663
if (filename && !strcmp (filename , "/dev/null" ))
655
664
wcscpy (wfilename , L"nul" );
656
665
else if (!is_valid_win32_path (filename , 1 )) {
657
666
int create = otype && strchr (otype , 'w' );
658
667
errno = create ? EINVAL : ENOENT ;
659
668
return NULL ;
660
- } else if (xutftowcs_path (wfilename , filename ) < 0 )
669
+ } else if (xutftowcs_long_path (wfilename , filename ) < 0 )
661
670
return NULL ;
662
671
663
672
if (xutftowcs (wotype , otype , ARRAY_SIZE (wotype )) < 0 )
@@ -714,27 +723,33 @@ ssize_t mingw_write(int fd, const void *buf, size_t len)
714
723
715
724
int mingw_access (const char * filename , int mode )
716
725
{
717
- wchar_t wfilename [MAX_PATH ];
726
+ wchar_t wfilename [MAX_LONG_PATH ];
718
727
if (!strcmp ("nul" , filename ) || !strcmp ("/dev/null" , filename ))
719
728
return 0 ;
720
- if (xutftowcs_path (wfilename , filename ) < 0 )
729
+ if (xutftowcs_long_path (wfilename , filename ) < 0 )
721
730
return -1 ;
722
731
/* X_OK is not supported by the MSVCRT version */
723
732
return _waccess (wfilename , mode & ~X_OK );
724
733
}
725
734
735
+ /* cached length of current directory for handle_long_path */
736
+ static int current_directory_len = 0 ;
737
+
726
738
int mingw_chdir (const char * dirname )
727
739
{
728
- wchar_t wdirname [MAX_PATH ];
729
- if (xutftowcs_path (wdirname , dirname ) < 0 )
740
+ int result ;
741
+ wchar_t wdirname [MAX_LONG_PATH ];
742
+ if (xutftowcs_long_path (wdirname , dirname ) < 0 )
730
743
return -1 ;
731
- return _wchdir (wdirname );
744
+ result = _wchdir (wdirname );
745
+ current_directory_len = GetCurrentDirectoryW (0 , NULL );
746
+ return result ;
732
747
}
733
748
734
749
int mingw_chmod (const char * filename , int mode )
735
750
{
736
- wchar_t wfilename [MAX_PATH ];
737
- if (xutftowcs_path (wfilename , filename ) < 0 )
751
+ wchar_t wfilename [MAX_LONG_PATH ];
752
+ if (xutftowcs_long_path (wfilename , filename ) < 0 )
738
753
return -1 ;
739
754
return _wchmod (wfilename , mode );
740
755
}
@@ -782,8 +797,8 @@ static int has_valid_directory_prefix(wchar_t *wfilename)
782
797
static int do_lstat (int follow , const char * file_name , struct stat * buf )
783
798
{
784
799
WIN32_FILE_ATTRIBUTE_DATA fdata ;
785
- wchar_t wfilename [MAX_PATH ];
786
- if (xutftowcs_path (wfilename , file_name ) < 0 )
800
+ wchar_t wfilename [MAX_LONG_PATH ];
801
+ if (xutftowcs_long_path (wfilename , file_name ) < 0 )
787
802
return -1 ;
788
803
789
804
if (GetFileAttributesExW (wfilename , GetFileExInfoStandard , & fdata )) {
@@ -954,8 +969,8 @@ int mingw_utime (const char *file_name, const struct utimbuf *times)
954
969
FILETIME mft , aft ;
955
970
int fh , rc ;
956
971
DWORD attrs ;
957
- wchar_t wfilename [MAX_PATH ];
958
- if (xutftowcs_path (wfilename , file_name ) < 0 )
972
+ wchar_t wfilename [MAX_LONG_PATH ];
973
+ if (xutftowcs_long_path (wfilename , file_name ) < 0 )
959
974
return -1 ;
960
975
961
976
/* must have write permission */
@@ -1025,6 +1040,7 @@ char *mingw_mktemp(char *template)
1025
1040
wchar_t wtemplate [MAX_PATH ];
1026
1041
int offset = 0 ;
1027
1042
1043
+ /* we need to return the path, thus no long paths here! */
1028
1044
if (xutftowcs_path (wtemplate , template ) < 0 )
1029
1045
return NULL ;
1030
1046
@@ -1662,6 +1678,10 @@ static pid_t mingw_spawnve_fd(const char *cmd, const char **argv, char **deltaen
1662
1678
1663
1679
if (* argv && !strcmp (cmd , * argv ))
1664
1680
wcmd [0 ] = L'\0' ;
1681
+ /*
1682
+ * Paths to executables and to the current directory do not support
1683
+ * long paths, therefore we cannot use xutftowcs_long_path() here.
1684
+ */
1665
1685
else if (xutftowcs_path (wcmd , cmd ) < 0 )
1666
1686
return -1 ;
1667
1687
if (dir && xutftowcs_path (wdir , dir ) < 0 )
@@ -2313,8 +2333,9 @@ int mingw_rename(const char *pold, const char *pnew)
2313
2333
{
2314
2334
DWORD attrs , gle ;
2315
2335
int tries = 0 ;
2316
- wchar_t wpold [MAX_PATH ], wpnew [MAX_PATH ];
2317
- if (xutftowcs_path (wpold , pold ) < 0 || xutftowcs_path (wpnew , pnew ) < 0 )
2336
+ wchar_t wpold [MAX_LONG_PATH ], wpnew [MAX_LONG_PATH ];
2337
+ if (xutftowcs_long_path (wpold , pold ) < 0 ||
2338
+ xutftowcs_long_path (wpnew , pnew ) < 0 )
2318
2339
return -1 ;
2319
2340
2320
2341
/*
@@ -2628,9 +2649,9 @@ int mingw_raise(int sig)
2628
2649
2629
2650
int link (const char * oldpath , const char * newpath )
2630
2651
{
2631
- wchar_t woldpath [MAX_PATH ], wnewpath [MAX_PATH ];
2632
- if (xutftowcs_path (woldpath , oldpath ) < 0 ||
2633
- xutftowcs_path (wnewpath , newpath ) < 0 )
2652
+ wchar_t woldpath [MAX_LONG_PATH ], wnewpath [MAX_LONG_PATH ];
2653
+ if (xutftowcs_long_path (woldpath , oldpath ) < 0 ||
2654
+ xutftowcs_long_path (wnewpath , newpath ) < 0 )
2634
2655
return -1 ;
2635
2656
2636
2657
if (!CreateHardLinkW (wnewpath , woldpath , NULL )) {
@@ -2698,8 +2719,8 @@ int mingw_is_mount_point(struct strbuf *path)
2698
2719
{
2699
2720
WIN32_FIND_DATAW findbuf = { 0 };
2700
2721
HANDLE handle ;
2701
- wchar_t wfilename [MAX_PATH ];
2702
- int wlen = xutftowcs_path (wfilename , path -> buf );
2722
+ wchar_t wfilename [MAX_LONG_PATH ];
2723
+ int wlen = xutftowcs_long_path (wfilename , path -> buf );
2703
2724
if (wlen < 0 )
2704
2725
die (_ ("could not get long path for '%s'" ), path -> buf );
2705
2726
@@ -2844,9 +2865,9 @@ static size_t append_system_bin_dirs(char *path, size_t size)
2844
2865
2845
2866
static int is_system32_path (const char * path )
2846
2867
{
2847
- WCHAR system32 [MAX_PATH ], wpath [MAX_PATH ];
2868
+ WCHAR system32 [MAX_LONG_PATH ], wpath [MAX_LONG_PATH ];
2848
2869
2849
- if (xutftowcs_path (wpath , path ) < 0 ||
2870
+ if (xutftowcs_long_path (wpath , path ) < 0 ||
2850
2871
!GetSystemDirectoryW (system32 , ARRAY_SIZE (system32 )) ||
2851
2872
_wcsicmp (system32 , wpath ))
2852
2873
return 0 ;
@@ -3063,6 +3084,68 @@ int is_valid_win32_path(const char *path, int allow_literal_nul)
3063
3084
}
3064
3085
}
3065
3086
3087
+ int handle_long_path (wchar_t * path , int len , int max_path , int expand )
3088
+ {
3089
+ int result ;
3090
+ wchar_t buf [MAX_LONG_PATH ];
3091
+
3092
+ /*
3093
+ * we don't need special handling if path is relative to the current
3094
+ * directory, and current directory + path don't exceed the desired
3095
+ * max_path limit. This should cover > 99 % of cases with minimal
3096
+ * performance impact (git almost always uses relative paths).
3097
+ */
3098
+ if ((len < 2 || (!is_dir_sep (path [0 ]) && path [1 ] != ':' )) &&
3099
+ (current_directory_len + len < max_path ))
3100
+ return len ;
3101
+
3102
+ /*
3103
+ * handle everything else:
3104
+ * - absolute paths: "C:\dir\file"
3105
+ * - absolute UNC paths: "\\server\share\dir\file"
3106
+ * - absolute paths on current drive: "\dir\file"
3107
+ * - relative paths on other drive: "X:file"
3108
+ * - prefixed paths: "\\?\...", "\\.\..."
3109
+ */
3110
+
3111
+ /* convert to absolute path using GetFullPathNameW */
3112
+ result = GetFullPathNameW (path , MAX_LONG_PATH , buf , NULL );
3113
+ if (!result ) {
3114
+ errno = err_win_to_posix (GetLastError ());
3115
+ return -1 ;
3116
+ }
3117
+
3118
+ /*
3119
+ * return absolute path if it fits within max_path (even if
3120
+ * "cwd + path" doesn't due to '..' components)
3121
+ */
3122
+ if (result < max_path ) {
3123
+ wcscpy (path , buf );
3124
+ return result ;
3125
+ }
3126
+
3127
+ /* error out if we shouldn't expand the path or buf is too small */
3128
+ if (!expand || result >= MAX_LONG_PATH - 6 ) {
3129
+ errno = ENAMETOOLONG ;
3130
+ return -1 ;
3131
+ }
3132
+
3133
+ /* prefix full path with "\\?\" or "\\?\UNC\" */
3134
+ if (buf [0 ] == '\\' ) {
3135
+ /* ...unless already prefixed */
3136
+ if (buf [1 ] == '\\' && (buf [2 ] == '?' || buf [2 ] == '.' ))
3137
+ return len ;
3138
+
3139
+ wcscpy (path , L"\\\\?\\UNC\\" );
3140
+ wcscpy (path + 8 , buf + 2 );
3141
+ return result + 6 ;
3142
+ } else {
3143
+ wcscpy (path , L"\\\\?\\" );
3144
+ wcscpy (path + 4 , buf );
3145
+ return result + 4 ;
3146
+ }
3147
+ }
3148
+
3066
3149
#if !defined(_MSC_VER )
3067
3150
/*
3068
3151
* Disable MSVCRT command line wildcard expansion (__getmainargs called from
@@ -3224,6 +3307,9 @@ int wmain(int argc, const wchar_t **wargv)
3224
3307
/* initialize Unicode console */
3225
3308
winansi_init ();
3226
3309
3310
+ /* init length of current directory for handle_long_path */
3311
+ current_directory_len = GetCurrentDirectoryW (0 , NULL );
3312
+
3227
3313
/* invoke the real main() using our utf8 version of argv. */
3228
3314
exit_status = main (argc , argv );
3229
3315
0 commit comments