@@ -204,8 +204,8 @@ static int ask_yes_no_if_possible(const char *format, ...)
204
204
int mingw_unlink (const char * pathname )
205
205
{
206
206
int ret , tries = 0 ;
207
- wchar_t wpathname [MAX_PATH ];
208
- if (xutftowcs_path (wpathname , pathname ) < 0 )
207
+ wchar_t wpathname [MAX_LONG_PATH ];
208
+ if (xutftowcs_long_path (wpathname , pathname ) < 0 )
209
209
return -1 ;
210
210
211
211
/* read-only files cannot be removed */
@@ -234,7 +234,7 @@ static int is_dir_empty(const wchar_t *wpath)
234
234
{
235
235
WIN32_FIND_DATAW findbuf ;
236
236
HANDLE handle ;
237
- wchar_t wbuf [MAX_PATH + 2 ];
237
+ wchar_t wbuf [MAX_LONG_PATH + 2 ];
238
238
wcscpy (wbuf , wpath );
239
239
wcscat (wbuf , L"\\*" );
240
240
handle = FindFirstFileW (wbuf , & findbuf );
@@ -255,8 +255,8 @@ static int is_dir_empty(const wchar_t *wpath)
255
255
int mingw_rmdir (const char * pathname )
256
256
{
257
257
int ret , tries = 0 ;
258
- wchar_t wpathname [MAX_PATH ];
259
- if (xutftowcs_path (wpathname , pathname ) < 0 )
258
+ wchar_t wpathname [MAX_LONG_PATH ];
259
+ if (xutftowcs_long_path (wpathname , pathname ) < 0 )
260
260
return -1 ;
261
261
262
262
while ((ret = _wrmdir (wpathname )) == -1 && tries < ARRAY_SIZE (delay )) {
@@ -296,9 +296,9 @@ static int make_hidden(const wchar_t *path)
296
296
297
297
void mingw_mark_as_git_dir (const char * dir )
298
298
{
299
- wchar_t wdir [MAX_PATH ];
299
+ wchar_t wdir [MAX_LONG_PATH ];
300
300
if (hide_dotfiles != HIDE_DOTFILES_FALSE && !is_bare_repository ())
301
- if (xutftowcs_path (wdir , dir ) < 0 || make_hidden (wdir ))
301
+ if (xutftowcs_long_path (wdir , dir ) < 0 || make_hidden (wdir ))
302
302
warning ("Failed to make '%s' hidden" , dir );
303
303
git_config_set ("core.hideDotFiles" ,
304
304
hide_dotfiles == HIDE_DOTFILES_FALSE ? "false" :
@@ -309,9 +309,12 @@ void mingw_mark_as_git_dir(const char *dir)
309
309
int mingw_mkdir (const char * path , int mode )
310
310
{
311
311
int ret ;
312
- wchar_t wpath [MAX_PATH ];
313
- if (xutftowcs_path (wpath , path ) < 0 )
312
+ wchar_t wpath [MAX_LONG_PATH ];
313
+ /* CreateDirectoryW path limit is 248 (MAX_PATH - 8.3 file name) */
314
+ if (xutftowcs_path_ex (wpath , path , MAX_LONG_PATH , -1 , 248 ,
315
+ core_long_paths ) < 0 )
314
316
return -1 ;
317
+
315
318
ret = _wmkdir (wpath );
316
319
if (!ret && hide_dotfiles == HIDE_DOTFILES_TRUE ) {
317
320
/*
@@ -331,7 +334,7 @@ int mingw_open (const char *filename, int oflags, ...)
331
334
va_list args ;
332
335
unsigned mode ;
333
336
int fd ;
334
- wchar_t wfilename [MAX_PATH ];
337
+ wchar_t wfilename [MAX_LONG_PATH ];
335
338
336
339
va_start (args , oflags );
337
340
mode = va_arg (args , int );
@@ -340,7 +343,7 @@ int mingw_open (const char *filename, int oflags, ...)
340
343
if (filename && !strcmp (filename , "/dev/null" ))
341
344
filename = "nul" ;
342
345
343
- if (xutftowcs_path (wfilename , filename ) < 0 )
346
+ if (xutftowcs_long_path (wfilename , filename ) < 0 )
344
347
return -1 ;
345
348
fd = _wopen (wfilename , oflags , mode );
346
349
@@ -410,13 +413,13 @@ FILE *mingw_fopen (const char *filename, const char *otype)
410
413
{
411
414
int hide = 0 ;
412
415
FILE * file ;
413
- wchar_t wfilename [MAX_PATH ], wotype [4 ];
416
+ wchar_t wfilename [MAX_LONG_PATH ], wotype [4 ];
414
417
if (hide_dotfiles == HIDE_DOTFILES_TRUE &&
415
418
basename ((char * )filename )[0 ] == '.' )
416
419
hide = access (filename , F_OK );
417
420
if (filename && !strcmp (filename , "/dev/null" ))
418
421
filename = "nul" ;
419
- if (xutftowcs_path (wfilename , filename ) < 0 ||
422
+ if (xutftowcs_long_path (wfilename , filename ) < 0 ||
420
423
xutftowcs (wotype , otype , ARRAY_SIZE (wotype )) < 0 )
421
424
return NULL ;
422
425
file = _wfopen (wfilename , wotype );
@@ -429,13 +432,13 @@ FILE *mingw_freopen (const char *filename, const char *otype, FILE *stream)
429
432
{
430
433
int hide = 0 ;
431
434
FILE * file ;
432
- wchar_t wfilename [MAX_PATH ], wotype [4 ];
435
+ wchar_t wfilename [MAX_LONG_PATH ], wotype [4 ];
433
436
if (hide_dotfiles == HIDE_DOTFILES_TRUE &&
434
437
basename ((char * )filename )[0 ] == '.' )
435
438
hide = access (filename , F_OK );
436
439
if (filename && !strcmp (filename , "/dev/null" ))
437
440
filename = "nul" ;
438
- if (xutftowcs_path (wfilename , filename ) < 0 ||
441
+ if (xutftowcs_long_path (wfilename , filename ) < 0 ||
439
442
xutftowcs (wotype , otype , ARRAY_SIZE (wotype )) < 0 )
440
443
return NULL ;
441
444
file = _wfreopen (wfilename , wotype , stream );
@@ -468,25 +471,32 @@ int mingw_fflush(FILE *stream)
468
471
469
472
int mingw_access (const char * filename , int mode )
470
473
{
471
- wchar_t wfilename [MAX_PATH ];
472
- if (xutftowcs_path (wfilename , filename ) < 0 )
474
+ wchar_t wfilename [MAX_LONG_PATH ];
475
+ if (xutftowcs_long_path (wfilename , filename ) < 0 )
473
476
return -1 ;
474
477
/* X_OK is not supported by the MSVCRT version */
475
478
return _waccess (wfilename , mode & ~X_OK );
476
479
}
477
480
481
+ /* cached length of current directory for handle_long_path */
482
+ static int current_directory_len = 0 ;
483
+
478
484
int mingw_chdir (const char * dirname )
479
485
{
486
+ int result ;
480
487
wchar_t wdirname [MAX_PATH ];
488
+ /* SetCurrentDirectoryW doesn't support long paths */
481
489
if (xutftowcs_path (wdirname , dirname ) < 0 )
482
490
return -1 ;
483
- return _wchdir (wdirname );
491
+ result = _wchdir (wdirname );
492
+ current_directory_len = GetCurrentDirectoryW (0 , NULL );
493
+ return result ;
484
494
}
485
495
486
496
int mingw_chmod (const char * filename , int mode )
487
497
{
488
- wchar_t wfilename [MAX_PATH ];
489
- if (xutftowcs_path (wfilename , filename ) < 0 )
498
+ wchar_t wfilename [MAX_LONG_PATH ];
499
+ if (xutftowcs_long_path (wfilename , filename ) < 0 )
490
500
return -1 ;
491
501
return _wchmod (wfilename , mode );
492
502
}
@@ -501,8 +511,8 @@ int mingw_chmod(const char *filename, int mode)
501
511
static int do_lstat (int follow , const char * file_name , struct stat * buf )
502
512
{
503
513
WIN32_FILE_ATTRIBUTE_DATA fdata ;
504
- wchar_t wfilename [MAX_PATH ];
505
- if (xutftowcs_path (wfilename , file_name ) < 0 )
514
+ wchar_t wfilename [MAX_LONG_PATH ];
515
+ if (xutftowcs_long_path (wfilename , file_name ) < 0 )
506
516
return -1 ;
507
517
508
518
if (GetFileAttributesExW (wfilename , GetFileExInfoStandard , & fdata )) {
@@ -645,8 +655,8 @@ int mingw_utime (const char *file_name, const struct utimbuf *times)
645
655
FILETIME mft , aft ;
646
656
int fh , rc ;
647
657
DWORD attrs ;
648
- wchar_t wfilename [MAX_PATH ];
649
- if (xutftowcs_path (wfilename , file_name ) < 0 )
658
+ wchar_t wfilename [MAX_LONG_PATH ];
659
+ if (xutftowcs_long_path (wfilename , file_name ) < 0 )
650
660
return -1 ;
651
661
652
662
/* must have write permission */
@@ -694,6 +704,7 @@ unsigned int sleep (unsigned int seconds)
694
704
char * mingw_mktemp (char * template )
695
705
{
696
706
wchar_t wtemplate [MAX_PATH ];
707
+ /* we need to return the path, thus no long paths here! */
697
708
if (xutftowcs_path (wtemplate , template ) < 0 )
698
709
return NULL ;
699
710
if (!_wmktemp (wtemplate ))
@@ -1136,6 +1147,7 @@ static pid_t mingw_spawnve_fd(const char *cmd, const char **argv, char **deltaen
1136
1147
si .hStdOutput = winansi_get_osfhandle (fhout );
1137
1148
si .hStdError = winansi_get_osfhandle (fherr );
1138
1149
1150
+ /* executables and the current directory don't support long paths */
1139
1151
if (xutftowcs_path (wcmd , cmd ) < 0 )
1140
1152
return -1 ;
1141
1153
if (dir && xutftowcs_path (wdir , dir ) < 0 )
@@ -1628,8 +1640,9 @@ int mingw_rename(const char *pold, const char *pnew)
1628
1640
{
1629
1641
DWORD attrs , gle ;
1630
1642
int tries = 0 ;
1631
- wchar_t wpold [MAX_PATH ], wpnew [MAX_PATH ];
1632
- if (xutftowcs_path (wpold , pold ) < 0 || xutftowcs_path (wpnew , pnew ) < 0 )
1643
+ wchar_t wpold [MAX_LONG_PATH ], wpnew [MAX_LONG_PATH ];
1644
+ if (xutftowcs_long_path (wpold , pold ) < 0 ||
1645
+ xutftowcs_long_path (wpnew , pnew ) < 0 )
1633
1646
return -1 ;
1634
1647
1635
1648
/*
@@ -1905,9 +1918,9 @@ int link(const char *oldpath, const char *newpath)
1905
1918
{
1906
1919
typedef BOOL (WINAPI * T )(LPCWSTR , LPCWSTR , LPSECURITY_ATTRIBUTES );
1907
1920
static T create_hard_link = NULL ;
1908
- wchar_t woldpath [MAX_PATH ], wnewpath [MAX_PATH ];
1909
- if (xutftowcs_path (woldpath , oldpath ) < 0 ||
1910
- xutftowcs_path (wnewpath , newpath ) < 0 )
1921
+ wchar_t woldpath [MAX_LONG_PATH ], wnewpath [MAX_LONG_PATH ];
1922
+ if (xutftowcs_long_path (woldpath , oldpath ) < 0 ||
1923
+ xutftowcs_long_path (wnewpath , newpath ) < 0 )
1911
1924
return -1 ;
1912
1925
1913
1926
if (!create_hard_link ) {
@@ -2106,6 +2119,68 @@ int xwcstoutf(char *utf, const wchar_t *wcs, size_t utflen)
2106
2119
return -1 ;
2107
2120
}
2108
2121
2122
+ int handle_long_path (wchar_t * path , int len , int max_path , int expand )
2123
+ {
2124
+ int result ;
2125
+ wchar_t buf [MAX_LONG_PATH ];
2126
+
2127
+ /*
2128
+ * we don't need special handling if path is relative to the current
2129
+ * directory, and current directory + path don't exceed the desired
2130
+ * max_path limit. This should cover > 99 % of cases with minimal
2131
+ * performance impact (git almost always uses relative paths).
2132
+ */
2133
+ if ((len < 2 || (!is_dir_sep (path [0 ]) && path [1 ] != ':' )) &&
2134
+ (current_directory_len + len < max_path ))
2135
+ return len ;
2136
+
2137
+ /*
2138
+ * handle everything else:
2139
+ * - absolute paths: "C:\dir\file"
2140
+ * - absolute UNC paths: "\\server\share\dir\file"
2141
+ * - absolute paths on current drive: "\dir\file"
2142
+ * - relative paths on other drive: "X:file"
2143
+ * - prefixed paths: "\\?\...", "\\.\..."
2144
+ */
2145
+
2146
+ /* convert to absolute path using GetFullPathNameW */
2147
+ result = GetFullPathNameW (path , MAX_LONG_PATH , buf , NULL );
2148
+ if (!result ) {
2149
+ errno = err_win_to_posix (GetLastError ());
2150
+ return -1 ;
2151
+ }
2152
+
2153
+ /*
2154
+ * return absolute path if it fits within max_path (even if
2155
+ * "cwd + path" doesn't due to '..' components)
2156
+ */
2157
+ if (result < max_path ) {
2158
+ wcscpy (path , buf );
2159
+ return result ;
2160
+ }
2161
+
2162
+ /* error out if we shouldn't expand the path or buf is too small */
2163
+ if (!expand || result >= MAX_LONG_PATH - 6 ) {
2164
+ errno = ENAMETOOLONG ;
2165
+ return -1 ;
2166
+ }
2167
+
2168
+ /* prefix full path with "\\?\" or "\\?\UNC\" */
2169
+ if (buf [0 ] == '\\' ) {
2170
+ /* ...unless already prefixed */
2171
+ if (buf [1 ] == '\\' && (buf [2 ] == '?' || buf [2 ] == '.' ))
2172
+ return len ;
2173
+
2174
+ wcscpy (path , L"\\\\?\\UNC\\" );
2175
+ wcscpy (path + 8 , buf + 2 );
2176
+ return result + 6 ;
2177
+ } else {
2178
+ wcscpy (path , L"\\\\?\\" );
2179
+ wcscpy (path + 4 , buf );
2180
+ return result + 4 ;
2181
+ }
2182
+ }
2183
+
2109
2184
/*
2110
2185
* Disable MSVCRT command line wildcard expansion (__getmainargs called from
2111
2186
* mingw startup code, see init.c in mingw runtime).
@@ -2209,4 +2284,7 @@ void mingw_startup()
2209
2284
2210
2285
/* initialize Unicode console */
2211
2286
winansi_init ();
2287
+
2288
+ /* init length of current directory for handle_long_path */
2289
+ current_directory_len = GetCurrentDirectoryW (0 , NULL );
2212
2290
}
0 commit comments