Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 2718734

Browse files
committed
Merge pull request libgit2#3435 from ethomson/nametoolong
win32: give better error message on long paths
2 parents aebddbe + 9768ebb commit 2718734

File tree

2 files changed

+77
-8
lines changed

2 files changed

+77
-8
lines changed

src/win32/path_w32.c

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -198,13 +198,13 @@ int git_win32_path_from_utf8(git_win32_path out, const char *src)
198198
/* See if this is an absolute path (beginning with a drive letter) */
199199
if (path__is_absolute(src)) {
200200
if (git__utf8_to_16(dest, MAX_PATH, src) < 0)
201-
return -1;
201+
goto on_error;
202202
}
203203
/* File-prefixed NT-style paths beginning with \\?\ */
204204
else if (path__is_nt_namespace(src)) {
205205
/* Skip the NT prefix, the destination already contains it */
206206
if (git__utf8_to_16(dest, MAX_PATH, src + PATH__NT_NAMESPACE_LEN) < 0)
207-
return -1;
207+
goto on_error;
208208
}
209209
/* UNC paths */
210210
else if (path__is_unc(src)) {
@@ -213,36 +213,43 @@ int git_win32_path_from_utf8(git_win32_path out, const char *src)
213213

214214
/* Skip the leading "\\" */
215215
if (git__utf8_to_16(dest, MAX_PATH - 2, src + 2) < 0)
216-
return -1;
216+
goto on_error;
217217
}
218218
/* Absolute paths omitting the drive letter */
219219
else if (src[0] == '\\' || src[0] == '/') {
220220
if (path__cwd(dest, MAX_PATH) < 0)
221-
return -1;
221+
goto on_error;
222222

223223
if (!path__is_absolute(dest)) {
224224
errno = ENOENT;
225-
return -1;
225+
goto on_error;
226226
}
227227

228228
/* Skip the drive letter specification ("C:") */
229229
if (git__utf8_to_16(dest + 2, MAX_PATH - 2, src) < 0)
230-
return -1;
230+
goto on_error;
231231
}
232232
/* Relative paths */
233233
else {
234234
int cwd_len;
235235

236236
if ((cwd_len = git_win32_path__cwd(dest, MAX_PATH)) < 0)
237-
return -1;
237+
goto on_error;
238238

239239
dest[cwd_len++] = L'\\';
240240

241241
if (git__utf8_to_16(dest + cwd_len, MAX_PATH - cwd_len, src) < 0)
242-
return -1;
242+
goto on_error;
243243
}
244244

245245
return git_win32_path_canonicalize(out);
246+
247+
on_error:
248+
/* set windows error code so we can use its error message */
249+
if (errno == ENAMETOOLONG)
250+
SetLastError(ERROR_FILENAME_EXCED_RANGE);
251+
252+
return -1;
246253
}
247254

248255
int git_win32_path_to_utf8(git_win32_utf8_path dest, const wchar_t *src)

tests/win32/longpath.c

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
#include "clar_libgit2.h"
2+
3+
#include "git2/clone.h"
4+
#include "clone.h"
5+
#include "buffer.h"
6+
#include "fileops.h"
7+
8+
static git_buf path = GIT_BUF_INIT;
9+
10+
void test_win32_longpath__initialize(void)
11+
{
12+
#ifdef GIT_WIN32
13+
const char *base = clar_sandbox_path();
14+
size_t base_len = strlen(base);
15+
size_t remain = MAX_PATH - base_len;
16+
size_t i;
17+
18+
git_buf_clear(&path);
19+
git_buf_puts(&path, base);
20+
git_buf_putc(&path, '/');
21+
22+
cl_assert(remain < (MAX_PATH - 5));
23+
24+
for (i = 0; i < (remain - 5); i++)
25+
git_buf_putc(&path, 'a');
26+
27+
printf("%s %" PRIuZ "\n", path.ptr, path.size);
28+
#endif
29+
}
30+
31+
void test_win32_longpath__cleanup(void)
32+
{
33+
git_buf_free(&path);
34+
}
35+
36+
#ifdef GIT_WIN32
37+
void assert_name_too_long(void)
38+
{
39+
const git_error *err;
40+
size_t expected_len, actual_len;
41+
const char *expected_msg;
42+
43+
err = giterr_last();
44+
actual_len = strlen(err->message);
45+
46+
expected_msg = git_win32_get_error_message(ERROR_FILENAME_EXCED_RANGE);
47+
expected_len = strlen(expected_msg);
48+
49+
/* check the suffix */
50+
cl_assert_equal_s(expected_msg, err->message + (actual_len - expected_len));
51+
}
52+
#endif
53+
54+
void test_win32_longpath__errmsg_on_checkout(void)
55+
{
56+
#ifdef GIT_WIN32
57+
git_repository *repo;
58+
59+
cl_git_fail(git_clone(&repo, cl_fixture("testrepo.git"), path.ptr, NULL));
60+
assert_name_too_long();
61+
#endif
62+
}

0 commit comments

Comments
 (0)