Skip to content

Commit 754fd35

Browse files
authored
Merge pull request libgit2#5857 from libgit2/ethomson/longpaths
Support `core.longpaths` on Windows
2 parents 525516b + 33667c1 commit 754fd35

File tree

6 files changed

+129
-67
lines changed

6 files changed

+129
-67
lines changed

src/win32/path_w32.c

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ static int win32_path_cwd(wchar_t *out, size_t len)
170170
* '\'s, but we we add a 'UNC' specifier to the path, plus
171171
* a trailing directory separator, plus a NUL.
172172
*/
173-
if (cwd_len > MAX_PATH - 4) {
173+
if (cwd_len > GIT_WIN_PATH_MAX - 4) {
174174
errno = ENAMETOOLONG;
175175
return -1;
176176
}
@@ -187,7 +187,7 @@ static int win32_path_cwd(wchar_t *out, size_t len)
187187
* working directory. (One character for the directory separator,
188188
* one for the null.
189189
*/
190-
else if (cwd_len > MAX_PATH - 2) {
190+
else if (cwd_len > GIT_WIN_PATH_MAX - 2) {
191191
errno = ENAMETOOLONG;
192192
return -1;
193193
}
@@ -205,13 +205,13 @@ int git_win32_path_from_utf8(git_win32_path out, const char *src)
205205

206206
/* See if this is an absolute path (beginning with a drive letter) */
207207
if (git_path_is_absolute(src)) {
208-
if (git__utf8_to_16(dest, MAX_PATH, src) < 0)
208+
if (git__utf8_to_16(dest, GIT_WIN_PATH_MAX, src) < 0)
209209
goto on_error;
210210
}
211211
/* File-prefixed NT-style paths beginning with \\?\ */
212212
else if (path__is_nt_namespace(src)) {
213213
/* Skip the NT prefix, the destination already contains it */
214-
if (git__utf8_to_16(dest, MAX_PATH, src + PATH__NT_NAMESPACE_LEN) < 0)
214+
if (git__utf8_to_16(dest, GIT_WIN_PATH_MAX, src + PATH__NT_NAMESPACE_LEN) < 0)
215215
goto on_error;
216216
}
217217
/* UNC paths */
@@ -220,12 +220,12 @@ int git_win32_path_from_utf8(git_win32_path out, const char *src)
220220
dest += 4;
221221

222222
/* Skip the leading "\\" */
223-
if (git__utf8_to_16(dest, MAX_PATH - 2, src + 2) < 0)
223+
if (git__utf8_to_16(dest, GIT_WIN_PATH_MAX - 2, src + 2) < 0)
224224
goto on_error;
225225
}
226226
/* Absolute paths omitting the drive letter */
227227
else if (path__startswith_slash(src)) {
228-
if (path__cwd(dest, MAX_PATH) < 0)
228+
if (path__cwd(dest, GIT_WIN_PATH_MAX) < 0)
229229
goto on_error;
230230

231231
if (!git_path_is_absolute(dest)) {
@@ -234,19 +234,19 @@ int git_win32_path_from_utf8(git_win32_path out, const char *src)
234234
}
235235

236236
/* Skip the drive letter specification ("C:") */
237-
if (git__utf8_to_16(dest + 2, MAX_PATH - 2, src) < 0)
237+
if (git__utf8_to_16(dest + 2, GIT_WIN_PATH_MAX - 2, src) < 0)
238238
goto on_error;
239239
}
240240
/* Relative paths */
241241
else {
242242
int cwd_len;
243243

244-
if ((cwd_len = win32_path_cwd(dest, MAX_PATH)) < 0)
244+
if ((cwd_len = win32_path_cwd(dest, GIT_WIN_PATH_MAX)) < 0)
245245
goto on_error;
246246

247247
dest[cwd_len++] = L'\\';
248248

249-
if (git__utf8_to_16(dest + cwd_len, MAX_PATH - cwd_len, src) < 0)
249+
if (git__utf8_to_16(dest + cwd_len, GIT_WIN_PATH_MAX - cwd_len, src) < 0)
250250
goto on_error;
251251
}
252252

@@ -273,7 +273,7 @@ int git_win32_path_relative_from_utf8(git_win32_path out, const char *src)
273273
return git_win32_path_from_utf8(out, src);
274274
}
275275

276-
if ((len = git__utf8_to_16(dest, MAX_PATH, src)) < 0)
276+
if ((len = git__utf8_to_16(dest, GIT_WIN_PATH_MAX, src)) < 0)
277277
return -1;
278278

279279
for (p = dest; p < (dest + len); p++) {

src/win32/w32_common.h

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,24 +8,33 @@
88
#ifndef INCLUDE_win32_w32_common_h__
99
#define INCLUDE_win32_w32_common_h__
1010

11+
#include <git2/common.h>
12+
13+
/*
14+
* 4096 is the max allowed Git path. `MAX_PATH` (260) is the typical max allowed
15+
* Windows path length, however win32 Unicode APIs generally allow up to 32,767
16+
* if prefixed with "\\?\" (i.e. converted to an NT-style name).
17+
*/
18+
#define GIT_WIN_PATH_MAX GIT_PATH_MAX
19+
1120
/*
12-
* Provides a large enough buffer to support Windows paths: MAX_PATH is
13-
* 260, corresponding to a maximum path length of 259 characters plus a
14-
* NULL terminator. Prefixing with "\\?\" adds 4 characters, but if the
15-
* original was a UNC path, then we turn "\\server\share" into
21+
* Provides a large enough buffer to support Windows Git paths:
22+
* GIT_WIN_PATH_MAX is 4096, corresponding to a maximum path length of 4095
23+
* characters plus a NULL terminator. Prefixing with "\\?\" adds 4 characters,
24+
* but if the original was a UNC path, then we turn "\\server\share" into
1625
* "\\?\UNC\server\share". So we replace the first two characters with
17-
* 8 characters, a net gain of 6, so the maximum length is MAX_PATH+6.
26+
* 8 characters, a net gain of 6, so the maximum length is GIT_WIN_PATH_MAX+6.
1827
*/
19-
#define GIT_WIN_PATH_UTF16 MAX_PATH+6
28+
#define GIT_WIN_PATH_UTF16 GIT_WIN_PATH_MAX+6
2029

21-
/* Maximum size of a UTF-8 Win32 path. We remove the "\\?\" or "\\?\UNC\"
22-
* prefixes for presentation, bringing us back to 259 (non-NULL)
30+
/* Maximum size of a UTF-8 Win32 Git path. We remove the "\\?\" or "\\?\UNC\"
31+
* prefixes for presentation, bringing us back to 4095 (non-NULL)
2332
* characters. UTF-8 does have 4-byte sequences, but they are encoded in
2433
* UTF-16 using surrogate pairs, which takes up the space of two characters.
2534
* Two characters in the range U+0800 -> U+FFFF take up more space in UTF-8
2635
* (6 bytes) than one surrogate pair (4 bytes).
2736
*/
28-
#define GIT_WIN_PATH_UTF8 (259 * 3 + 1)
37+
#define GIT_WIN_PATH_UTF8 ((GIT_WIN_PATH_MAX - 1) * 3 + 1)
2938

3039
/*
3140
* The length of a Windows "shortname", for 8.3 compatibility.

tests/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ SET(CLAR_FIXTURES "${CMAKE_CURRENT_SOURCE_DIR}/resources/")
99
SET(CLAR_PATH "${CMAKE_CURRENT_SOURCE_DIR}")
1010
ADD_DEFINITIONS(-DCLAR_FIXTURE_PATH=\"${CLAR_FIXTURES}\")
1111
ADD_DEFINITIONS(-DCLAR_TMPDIR=\"libgit2_tests\")
12+
ADD_DEFINITIONS(-DCLAR_WIN32_LONGPATHS)
1213
ADD_DEFINITIONS(-D_FILE_OFFSET_BITS=64)
1314

1415
# Ensure that we do not use deprecated functions internally

tests/clar/fs.h

Lines changed: 60 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,12 @@
88

99
#ifdef _WIN32
1010

11+
#ifdef CLAR_WIN32_LONGPATHS
12+
# define CLAR_MAX_PATH 4096
13+
#else
14+
# define CLAR_MAX_PATH MAX_PATH
15+
#endif
16+
1117
#define RM_RETRY_COUNT 5
1218
#define RM_RETRY_DELAY 10
1319

@@ -48,21 +54,48 @@ fs_rmdir_rmdir(WCHAR *_wpath)
4854
return 0;
4955
}
5056

57+
static void translate_path(WCHAR *path, size_t path_size)
58+
{
59+
size_t path_len, i;
60+
61+
if (wcsncmp(path, L"\\\\?\\", 4) == 0)
62+
return;
63+
64+
path_len = wcslen(path);
65+
cl_assert(path_size > path_len + 4);
66+
67+
for (i = path_len; i > 0; i--) {
68+
WCHAR c = path[i - 1];
69+
70+
if (c == L'/')
71+
path[i + 3] = L'\\';
72+
else
73+
path[i + 3] = path[i - 1];
74+
}
75+
76+
path[0] = L'\\';
77+
path[1] = L'\\';
78+
path[2] = L'?';
79+
path[3] = L'\\';
80+
path[path_len + 4] = L'\0';
81+
}
82+
5183
static void
5284
fs_rmdir_helper(WCHAR *_wsource)
5385
{
54-
WCHAR buffer[MAX_PATH];
86+
WCHAR buffer[CLAR_MAX_PATH];
5587
HANDLE find_handle;
5688
WIN32_FIND_DATAW find_data;
5789
size_t buffer_prefix_len;
5890

5991
/* Set up the buffer and capture the length */
60-
wcscpy_s(buffer, MAX_PATH, _wsource);
61-
wcscat_s(buffer, MAX_PATH, L"\\");
92+
wcscpy_s(buffer, CLAR_MAX_PATH, _wsource);
93+
translate_path(buffer, CLAR_MAX_PATH);
94+
wcscat_s(buffer, CLAR_MAX_PATH, L"\\");
6295
buffer_prefix_len = wcslen(buffer);
6396

6497
/* FindFirstFile needs a wildcard to match multiple items */
65-
wcscat_s(buffer, MAX_PATH, L"*");
98+
wcscat_s(buffer, CLAR_MAX_PATH, L"*");
6699
find_handle = FindFirstFileW(buffer, &find_data);
67100
cl_assert(INVALID_HANDLE_VALUE != find_handle);
68101

@@ -72,7 +105,7 @@ fs_rmdir_helper(WCHAR *_wsource)
72105
if (fs__dotordotdot(find_data.cFileName))
73106
continue;
74107

75-
wcscpy_s(buffer + buffer_prefix_len, MAX_PATH - buffer_prefix_len, find_data.cFileName);
108+
wcscpy_s(buffer + buffer_prefix_len, CLAR_MAX_PATH - buffer_prefix_len, find_data.cFileName);
76109

77110
if (FILE_ATTRIBUTE_DIRECTORY & find_data.dwFileAttributes)
78111
fs_rmdir_helper(buffer);
@@ -123,7 +156,7 @@ fs_rm_wait(WCHAR *_wpath)
123156
static void
124157
fs_rm(const char *_source)
125158
{
126-
WCHAR wsource[MAX_PATH];
159+
WCHAR wsource[CLAR_MAX_PATH];
127160
DWORD attrs;
128161

129162
/* The input path is UTF-8. Convert it to wide characters
@@ -133,7 +166,9 @@ fs_rm(const char *_source)
133166
_source,
134167
-1, /* Indicates NULL termination */
135168
wsource,
136-
MAX_PATH));
169+
CLAR_MAX_PATH));
170+
171+
translate_path(wsource, CLAR_MAX_PATH);
137172

138173
/* Does the item exist? If not, we have no work to do */
139174
attrs = GetFileAttributesW(wsource);
@@ -158,21 +193,23 @@ fs_rm(const char *_source)
158193
static void
159194
fs_copydir_helper(WCHAR *_wsource, WCHAR *_wdest)
160195
{
161-
WCHAR buf_source[MAX_PATH], buf_dest[MAX_PATH];
196+
WCHAR buf_source[CLAR_MAX_PATH], buf_dest[CLAR_MAX_PATH];
162197
HANDLE find_handle;
163198
WIN32_FIND_DATAW find_data;
164199
size_t buf_source_prefix_len, buf_dest_prefix_len;
165200

166-
wcscpy_s(buf_source, MAX_PATH, _wsource);
167-
wcscat_s(buf_source, MAX_PATH, L"\\");
201+
wcscpy_s(buf_source, CLAR_MAX_PATH, _wsource);
202+
wcscat_s(buf_source, CLAR_MAX_PATH, L"\\");
203+
translate_path(buf_source, CLAR_MAX_PATH);
168204
buf_source_prefix_len = wcslen(buf_source);
169205

170-
wcscpy_s(buf_dest, MAX_PATH, _wdest);
171-
wcscat_s(buf_dest, MAX_PATH, L"\\");
206+
wcscpy_s(buf_dest, CLAR_MAX_PATH, _wdest);
207+
wcscat_s(buf_dest, CLAR_MAX_PATH, L"\\");
208+
translate_path(buf_dest, CLAR_MAX_PATH);
172209
buf_dest_prefix_len = wcslen(buf_dest);
173210

174211
/* Get an enumerator for the items in the source. */
175-
wcscat_s(buf_source, MAX_PATH, L"*");
212+
wcscat_s(buf_source, CLAR_MAX_PATH, L"*");
176213
find_handle = FindFirstFileW(buf_source, &find_data);
177214
cl_assert(INVALID_HANDLE_VALUE != find_handle);
178215

@@ -185,8 +222,8 @@ fs_copydir_helper(WCHAR *_wsource, WCHAR *_wdest)
185222
if (fs__dotordotdot(find_data.cFileName))
186223
continue;
187224

188-
wcscpy_s(buf_source + buf_source_prefix_len, MAX_PATH - buf_source_prefix_len, find_data.cFileName);
189-
wcscpy_s(buf_dest + buf_dest_prefix_len, MAX_PATH - buf_dest_prefix_len, find_data.cFileName);
225+
wcscpy_s(buf_source + buf_source_prefix_len, CLAR_MAX_PATH - buf_source_prefix_len, find_data.cFileName);
226+
wcscpy_s(buf_dest + buf_dest_prefix_len, CLAR_MAX_PATH - buf_dest_prefix_len, find_data.cFileName);
190227

191228
if (FILE_ATTRIBUTE_DIRECTORY & find_data.dwFileAttributes)
192229
fs_copydir_helper(buf_source, buf_dest);
@@ -205,7 +242,7 @@ fs_copydir_helper(WCHAR *_wsource, WCHAR *_wdest)
205242
static void
206243
fs_copy(const char *_source, const char *_dest)
207244
{
208-
WCHAR wsource[MAX_PATH], wdest[MAX_PATH];
245+
WCHAR wsource[CLAR_MAX_PATH], wdest[CLAR_MAX_PATH];
209246
DWORD source_attrs, dest_attrs;
210247
HANDLE find_handle;
211248
WIN32_FIND_DATAW find_data;
@@ -217,14 +254,17 @@ fs_copy(const char *_source, const char *_dest)
217254
_source,
218255
-1,
219256
wsource,
220-
MAX_PATH));
257+
CLAR_MAX_PATH));
221258

222259
cl_assert(MultiByteToWideChar(CP_UTF8,
223260
MB_ERR_INVALID_CHARS,
224261
_dest,
225262
-1,
226263
wdest,
227-
MAX_PATH));
264+
CLAR_MAX_PATH));
265+
266+
translate_path(wsource, CLAR_MAX_PATH);
267+
translate_path(wdest, CLAR_MAX_PATH);
228268

229269
/* Check the source for existence */
230270
source_attrs = GetFileAttributesW(wsource);
@@ -238,8 +278,8 @@ fs_copy(const char *_source, const char *_dest)
238278
* Use FindFirstFile to parse the path */
239279
find_handle = FindFirstFileW(wsource, &find_data);
240280
cl_assert(INVALID_HANDLE_VALUE != find_handle);
241-
wcscat_s(wdest, MAX_PATH, L"\\");
242-
wcscat_s(wdest, MAX_PATH, find_data.cFileName);
281+
wcscat_s(wdest, CLAR_MAX_PATH, L"\\");
282+
wcscat_s(wdest, CLAR_MAX_PATH, find_data.cFileName);
243283
FindClose(find_handle);
244284

245285
/* Check the new target for existence */

tests/path/win32.c

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -78,13 +78,11 @@ void test_path_win32__honors_max_path(void)
7878
#ifdef GIT_WIN32
7979
git_win32_path path_utf16;
8080

81-
test_utf8_to_utf16("C:\\This path is 259 chars and is the max length in windows\\0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij",
82-
L"\\\\?\\C:\\This path is 259 chars and is the max length in windows\\0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij");
83-
test_utf8_to_utf16("\\\\unc\\paths may also be 259 characters including the server\\123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij",
84-
L"\\\\?\\UNC\\unc\\paths may also be 259 characters including the server\\123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij");
81+
test_utf8_to_utf16("C:\\This path is 261 characters which is fine for our path handling functions which cope with paths longer than MAX_PATH\\0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghijk",
82+
L"\\\\?\\C:\\This path is 261 characters which is fine for our path handling functions which cope with paths longer than MAX_PATH\\0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghijk");
83+
84+
cl_check_fail(git_win32_path_from_utf8(path_utf16, "C:\\This path is 4097 chars and exceeds our maximum path length on Windows which is limited to 4096 characters\\alas\\0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij01"));
8585

86-
cl_check_fail(git_win32_path_from_utf8(path_utf16, "C:\\This path is 260 chars and is sadly too long for windows\\0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij"));
87-
cl_check_fail(git_win32_path_from_utf8(path_utf16, "\\\\unc\\paths are also bound by 260 character restrictions\\including the server name portion\\bcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij"));
8886
#endif
8987
}
9088

0 commit comments

Comments
 (0)