@@ -206,8 +206,8 @@ static int ask_yes_no_if_possible(const char *format, ...)
206
206
int mingw_unlink (const char * pathname )
207
207
{
208
208
int ret , tries = 0 ;
209
- wchar_t wpathname [MAX_PATH ];
210
- if (xutftowcs_path (wpathname , pathname ) < 0 )
209
+ wchar_t wpathname [MAX_LONG_PATH ];
210
+ if (xutftowcs_long_path (wpathname , pathname ) < 0 )
211
211
return -1 ;
212
212
213
213
/* read-only files cannot be removed */
@@ -236,7 +236,7 @@ static int is_dir_empty(const wchar_t *wpath)
236
236
{
237
237
WIN32_FIND_DATAW findbuf ;
238
238
HANDLE handle ;
239
- wchar_t wbuf [MAX_PATH + 2 ];
239
+ wchar_t wbuf [MAX_LONG_PATH + 2 ];
240
240
wcscpy (wbuf , wpath );
241
241
wcscat (wbuf , L"\\*" );
242
242
handle = FindFirstFileW (wbuf , & findbuf );
@@ -257,8 +257,8 @@ static int is_dir_empty(const wchar_t *wpath)
257
257
int mingw_rmdir (const char * pathname )
258
258
{
259
259
int ret , tries = 0 ;
260
- wchar_t wpathname [MAX_PATH ];
261
- if (xutftowcs_path (wpathname , pathname ) < 0 )
260
+ wchar_t wpathname [MAX_LONG_PATH ];
261
+ if (xutftowcs_long_path (wpathname , pathname ) < 0 )
262
262
return -1 ;
263
263
264
264
while ((ret = _wrmdir (wpathname )) == -1 && tries < ARRAY_SIZE (delay )) {
@@ -298,9 +298,9 @@ static int make_hidden(const wchar_t *path)
298
298
299
299
void mingw_mark_as_git_dir (const char * dir )
300
300
{
301
- wchar_t wdir [MAX_PATH ];
301
+ wchar_t wdir [MAX_LONG_PATH ];
302
302
if (hide_dotfiles != HIDE_DOTFILES_FALSE && !is_bare_repository ())
303
- if (xutftowcs_path (wdir , dir ) < 0 || make_hidden (wdir ))
303
+ if (xutftowcs_long_path (wdir , dir ) < 0 || make_hidden (wdir ))
304
304
warning ("Failed to make '%s' hidden" , dir );
305
305
git_config_set ("core.hideDotFiles" ,
306
306
hide_dotfiles == HIDE_DOTFILES_FALSE ? "false" :
@@ -311,9 +311,12 @@ void mingw_mark_as_git_dir(const char *dir)
311
311
int mingw_mkdir (const char * path , int mode )
312
312
{
313
313
int ret ;
314
- wchar_t wpath [MAX_PATH ];
315
- if (xutftowcs_path (wpath , path ) < 0 )
314
+ wchar_t wpath [MAX_LONG_PATH ];
315
+ /* CreateDirectoryW path limit is 248 (MAX_PATH - 8.3 file name) */
316
+ if (xutftowcs_path_ex (wpath , path , MAX_LONG_PATH , -1 , 248 ,
317
+ core_long_paths ) < 0 )
316
318
return -1 ;
319
+
317
320
ret = _wmkdir (wpath );
318
321
if (!ret && hide_dotfiles == HIDE_DOTFILES_TRUE ) {
319
322
/*
@@ -333,7 +336,7 @@ int mingw_open (const char *filename, int oflags, ...)
333
336
va_list args ;
334
337
unsigned mode ;
335
338
int fd ;
336
- wchar_t wfilename [MAX_PATH ];
339
+ wchar_t wfilename [MAX_LONG_PATH ];
337
340
338
341
va_start (args , oflags );
339
342
mode = va_arg (args , int );
@@ -342,7 +345,7 @@ int mingw_open (const char *filename, int oflags, ...)
342
345
if (filename && !strcmp (filename , "/dev/null" ))
343
346
filename = "nul" ;
344
347
345
- if (xutftowcs_path (wfilename , filename ) < 0 )
348
+ if (xutftowcs_long_path (wfilename , filename ) < 0 )
346
349
return -1 ;
347
350
fd = _wopen (wfilename , oflags , mode );
348
351
@@ -395,13 +398,13 @@ FILE *mingw_fopen (const char *filename, const char *otype)
395
398
{
396
399
int hide = 0 ;
397
400
FILE * file ;
398
- wchar_t wfilename [MAX_PATH ], wotype [4 ];
401
+ wchar_t wfilename [MAX_LONG_PATH ], wotype [4 ];
399
402
if (hide_dotfiles == HIDE_DOTFILES_TRUE &&
400
403
basename ((char * )filename )[0 ] == '.' )
401
404
hide = access (filename , F_OK );
402
405
if (filename && !strcmp (filename , "/dev/null" ))
403
406
filename = "nul" ;
404
- if (xutftowcs_path (wfilename , filename ) < 0 ||
407
+ if (xutftowcs_long_path (wfilename , filename ) < 0 ||
405
408
xutftowcs (wotype , otype , ARRAY_SIZE (wotype )) < 0 )
406
409
return NULL ;
407
410
file = _wfopen (wfilename , wotype );
@@ -414,13 +417,13 @@ FILE *mingw_freopen (const char *filename, const char *otype, FILE *stream)
414
417
{
415
418
int hide = 0 ;
416
419
FILE * file ;
417
- wchar_t wfilename [MAX_PATH ], wotype [4 ];
420
+ wchar_t wfilename [MAX_LONG_PATH ], wotype [4 ];
418
421
if (hide_dotfiles == HIDE_DOTFILES_TRUE &&
419
422
basename ((char * )filename )[0 ] == '.' )
420
423
hide = access (filename , F_OK );
421
424
if (filename && !strcmp (filename , "/dev/null" ))
422
425
filename = "nul" ;
423
- if (xutftowcs_path (wfilename , filename ) < 0 ||
426
+ if (xutftowcs_long_path (wfilename , filename ) < 0 ||
424
427
xutftowcs (wotype , otype , ARRAY_SIZE (wotype )) < 0 )
425
428
return NULL ;
426
429
file = _wfreopen (wfilename , wotype , stream );
@@ -453,25 +456,32 @@ int mingw_fflush(FILE *stream)
453
456
454
457
int mingw_access (const char * filename , int mode )
455
458
{
456
- wchar_t wfilename [MAX_PATH ];
457
- if (xutftowcs_path (wfilename , filename ) < 0 )
459
+ wchar_t wfilename [MAX_LONG_PATH ];
460
+ if (xutftowcs_long_path (wfilename , filename ) < 0 )
458
461
return -1 ;
459
462
/* X_OK is not supported by the MSVCRT version */
460
463
return _waccess (wfilename , mode & ~X_OK );
461
464
}
462
465
466
+ /* cached length of current directory for handle_long_path */
467
+ static int current_directory_len = 0 ;
468
+
463
469
int mingw_chdir (const char * dirname )
464
470
{
471
+ int result ;
465
472
wchar_t wdirname [MAX_PATH ];
473
+ /* SetCurrentDirectoryW doesn't support long paths */
466
474
if (xutftowcs_path (wdirname , dirname ) < 0 )
467
475
return -1 ;
468
- return _wchdir (wdirname );
476
+ result = _wchdir (wdirname );
477
+ current_directory_len = GetCurrentDirectoryW (0 , NULL );
478
+ return result ;
469
479
}
470
480
471
481
int mingw_chmod (const char * filename , int mode )
472
482
{
473
- wchar_t wfilename [MAX_PATH ];
474
- if (xutftowcs_path (wfilename , filename ) < 0 )
483
+ wchar_t wfilename [MAX_LONG_PATH ];
484
+ if (xutftowcs_long_path (wfilename , filename ) < 0 )
475
485
return -1 ;
476
486
return _wchmod (wfilename , mode );
477
487
}
@@ -486,8 +496,8 @@ int mingw_chmod(const char *filename, int mode)
486
496
static int do_lstat (int follow , const char * file_name , struct stat * buf )
487
497
{
488
498
WIN32_FILE_ATTRIBUTE_DATA fdata ;
489
- wchar_t wfilename [MAX_PATH ];
490
- if (xutftowcs_path (wfilename , file_name ) < 0 )
499
+ wchar_t wfilename [MAX_LONG_PATH ];
500
+ if (xutftowcs_long_path (wfilename , file_name ) < 0 )
491
501
return -1 ;
492
502
493
503
if (GetFileAttributesExW (wfilename , GetFileExInfoStandard , & fdata )) {
@@ -552,7 +562,7 @@ static int do_lstat(int follow, const char *file_name, struct stat *buf)
552
562
static int do_stat_internal (int follow , const char * file_name , struct stat * buf )
553
563
{
554
564
int namelen ;
555
- char alt_name [PATH_MAX ];
565
+ char alt_name [MAX_LONG_PATH ];
556
566
557
567
if (!do_lstat (follow , file_name , buf ))
558
568
return 0 ;
@@ -568,7 +578,7 @@ static int do_stat_internal(int follow, const char *file_name, struct stat *buf)
568
578
return -1 ;
569
579
while (namelen && file_name [namelen - 1 ] == '/' )
570
580
-- namelen ;
571
- if (!namelen || namelen >= PATH_MAX )
581
+ if (!namelen || namelen >= MAX_LONG_PATH )
572
582
return -1 ;
573
583
574
584
memcpy (alt_name , file_name , namelen );
@@ -630,8 +640,8 @@ int mingw_utime (const char *file_name, const struct utimbuf *times)
630
640
FILETIME mft , aft ;
631
641
int fh , rc ;
632
642
DWORD attrs ;
633
- wchar_t wfilename [MAX_PATH ];
634
- if (xutftowcs_path (wfilename , file_name ) < 0 )
643
+ wchar_t wfilename [MAX_LONG_PATH ];
644
+ if (xutftowcs_long_path (wfilename , file_name ) < 0 )
635
645
return -1 ;
636
646
637
647
/* must have write permission */
@@ -679,6 +689,7 @@ unsigned int sleep (unsigned int seconds)
679
689
char * mingw_mktemp (char * template )
680
690
{
681
691
wchar_t wtemplate [MAX_PATH ];
692
+ /* we need to return the path, thus no long paths here! */
682
693
if (xutftowcs_path (wtemplate , template ) < 0 )
683
694
return NULL ;
684
695
if (!_wmktemp (wtemplate ))
@@ -1039,6 +1050,7 @@ static pid_t mingw_spawnve_fd(const char *cmd, const char **argv, char **deltaen
1039
1050
si .hStdOutput = winansi_get_osfhandle (fhout );
1040
1051
si .hStdError = winansi_get_osfhandle (fherr );
1041
1052
1053
+ /* executables and the current directory don't support long paths */
1042
1054
if (xutftowcs_path (wcmd , cmd ) < 0 )
1043
1055
return -1 ;
1044
1056
if (dir && xutftowcs_path (wdir , dir ) < 0 )
@@ -1624,8 +1636,9 @@ int mingw_rename(const char *pold, const char *pnew)
1624
1636
{
1625
1637
DWORD attrs , gle ;
1626
1638
int tries = 0 ;
1627
- wchar_t wpold [MAX_PATH ], wpnew [MAX_PATH ];
1628
- if (xutftowcs_path (wpold , pold ) < 0 || xutftowcs_path (wpnew , pnew ) < 0 )
1639
+ wchar_t wpold [MAX_LONG_PATH ], wpnew [MAX_LONG_PATH ];
1640
+ if (xutftowcs_long_path (wpold , pold ) < 0 ||
1641
+ xutftowcs_long_path (wpnew , pnew ) < 0 )
1629
1642
return -1 ;
1630
1643
1631
1644
/*
@@ -1906,9 +1919,9 @@ int link(const char *oldpath, const char *newpath)
1906
1919
{
1907
1920
typedef BOOL (WINAPI * T )(LPCWSTR , LPCWSTR , LPSECURITY_ATTRIBUTES );
1908
1921
static T create_hard_link = NULL ;
1909
- wchar_t woldpath [MAX_PATH ], wnewpath [MAX_PATH ];
1910
- if (xutftowcs_path (woldpath , oldpath ) < 0 ||
1911
- xutftowcs_path (wnewpath , newpath ) < 0 )
1922
+ wchar_t woldpath [MAX_LONG_PATH ], wnewpath [MAX_LONG_PATH ];
1923
+ if (xutftowcs_long_path (woldpath , oldpath ) < 0 ||
1924
+ xutftowcs_long_path (wnewpath , newpath ) < 0 )
1912
1925
return -1 ;
1913
1926
1914
1927
if (!create_hard_link ) {
@@ -2089,6 +2102,68 @@ int xwcstoutf(char *utf, const wchar_t *wcs, size_t utflen)
2089
2102
return -1 ;
2090
2103
}
2091
2104
2105
+ int handle_long_path (wchar_t * path , int len , int max_path , int expand )
2106
+ {
2107
+ int result ;
2108
+ wchar_t buf [MAX_LONG_PATH ];
2109
+
2110
+ /*
2111
+ * we don't need special handling if path is relative to the current
2112
+ * directory, and current directory + path don't exceed the desired
2113
+ * max_path limit. This should cover > 99 % of cases with minimal
2114
+ * performance impact (git almost always uses relative paths).
2115
+ */
2116
+ if ((len < 2 || (!is_dir_sep (path [0 ]) && path [1 ] != ':' )) &&
2117
+ (current_directory_len + len < max_path ))
2118
+ return len ;
2119
+
2120
+ /*
2121
+ * handle everything else:
2122
+ * - absolute paths: "C:\dir\file"
2123
+ * - absolute UNC paths: "\\server\share\dir\file"
2124
+ * - absolute paths on current drive: "\dir\file"
2125
+ * - relative paths on other drive: "X:file"
2126
+ * - prefixed paths: "\\?\...", "\\.\..."
2127
+ */
2128
+
2129
+ /* convert to absolute path using GetFullPathNameW */
2130
+ result = GetFullPathNameW (path , MAX_LONG_PATH , buf , NULL );
2131
+ if (!result ) {
2132
+ errno = err_win_to_posix (GetLastError ());
2133
+ return -1 ;
2134
+ }
2135
+
2136
+ /*
2137
+ * return absolute path if it fits within max_path (even if
2138
+ * "cwd + path" doesn't due to '..' components)
2139
+ */
2140
+ if (result < max_path ) {
2141
+ wcscpy (path , buf );
2142
+ return result ;
2143
+ }
2144
+
2145
+ /* error out if we shouldn't expand the path or buf is too small */
2146
+ if (!expand || result >= MAX_LONG_PATH - 6 ) {
2147
+ errno = ENAMETOOLONG ;
2148
+ return -1 ;
2149
+ }
2150
+
2151
+ /* prefix full path with "\\?\" or "\\?\UNC\" */
2152
+ if (buf [0 ] == '\\' ) {
2153
+ /* ...unless already prefixed */
2154
+ if (buf [1 ] == '\\' && (buf [2 ] == '?' || buf [2 ] == '.' ))
2155
+ return len ;
2156
+
2157
+ wcscpy (path , L"\\\\?\\UNC\\" );
2158
+ wcscpy (path + 8 , buf + 2 );
2159
+ return result + 6 ;
2160
+ } else {
2161
+ wcscpy (path , L"\\\\?\\" );
2162
+ wcscpy (path + 4 , buf );
2163
+ return result + 4 ;
2164
+ }
2165
+ }
2166
+
2092
2167
/*
2093
2168
* Disable MSVCRT command line wildcard expansion (__getmainargs called from
2094
2169
* mingw startup code, see init.c in mingw runtime).
@@ -2210,6 +2285,9 @@ void mingw_startup()
2210
2285
2211
2286
/* initialize Unicode console */
2212
2287
winansi_init ();
2288
+
2289
+ /* init length of current directory for handle_long_path */
2290
+ current_directory_len = GetCurrentDirectoryW (0 , NULL );
2213
2291
}
2214
2292
2215
2293
int mingw_isatty (int fd ) {
0 commit comments