Skip to content

Commit b2e85f9

Browse files
committed
win32: rename git_win32__canonicalize_path
The internal API `git_win32__canonicalize_path` is far, far too easily confused with the internal API `git_win32_path_canonicalize`. The former removes the namespace prefix from a path (eg, given `\\?\C:\Temp\foo`, it returns `C:\Temp\foo`, and given `\\?\UNC\server\share`, it returns `\\server\share`). As such, rename it to `git_win32_path_remove_namespace`. `git_win32_path_canonicalize` remains unchanged.
1 parent 3f096ca commit b2e85f9

File tree

6 files changed

+116
-113
lines changed

6 files changed

+116
-113
lines changed

src/win32/path_w32.c

Lines changed: 90 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ int git_win32_path_from_utf8(git_win32_path out, const char *src)
220220
goto on_error;
221221
}
222222

223-
/* Skip the drive letter specification ("C:") */
223+
/* Skip the drive letter specification ("C:") */
224224
if (git__utf8_to_16(dest + 2, MAX_PATH - 2, src) < 0)
225225
goto on_error;
226226
}
@@ -315,7 +315,7 @@ static bool path_is_volume(wchar_t *target, size_t target_len)
315315
}
316316

317317
/* On success, returns the length, in characters, of the path stored in dest.
318-
* On failure, returns a negative value. */
318+
* On failure, returns a negative value. */
319319
int git_win32_path_readlink_w(git_win32_path dest, const git_win32_path path)
320320
{
321321
BYTE buf[MAXIMUM_REPARSE_DATA_BUFFER_SIZE];
@@ -360,16 +360,16 @@ int git_win32_path_readlink_w(git_win32_path dest, const git_win32_path path)
360360

361361
if (path_is_volume(target, target_len)) {
362362
/* This path is a reparse point that represents another volume mounted
363-
* at this location, it is not a symbolic link our input was canonical.
364-
*/
363+
* at this location, it is not a symbolic link our input was canonical.
364+
*/
365365
errno = EINVAL;
366366
error = -1;
367367
} else if (target_len) {
368-
/* The path may need to have a prefix removed. */
369-
target_len = git_win32__canonicalize_path(target, target_len);
368+
/* The path may need to have a namespace prefix removed. */
369+
target_len = git_win32_path_remove_namespace(target, target_len);
370370

371371
/* Need one additional character in the target buffer
372-
* for the terminating NULL. */
372+
* for the terminating NULL. */
373373
if (GIT_WIN_PATH_UTF16 > target_len) {
374374
wcscpy(dest, target);
375375
error = (int)target_len;
@@ -380,3 +380,86 @@ int git_win32_path_readlink_w(git_win32_path dest, const git_win32_path path)
380380
CloseHandle(handle);
381381
return error;
382382
}
383+
384+
/**
385+
* Removes any trailing backslashes from a path, except in the case of a drive
386+
* letter path (C:\, D:\, etc.). This function cannot fail.
387+
*
388+
* @param path The path which should be trimmed.
389+
* @return The length of the modified string (<= the input length)
390+
*/
391+
size_t git_win32_path_trim_end(wchar_t *str, size_t len)
392+
{
393+
while (1) {
394+
if (!len || str[len - 1] != L'\\')
395+
break;
396+
397+
/*
398+
* Don't trim backslashes from drive letter paths, which
399+
* are 3 characters long and of the form C:\, D:\, etc.
400+
*/
401+
if (len == 3 && git_win32__isalpha(str[0]) && str[1] == ':')
402+
break;
403+
404+
len--;
405+
}
406+
407+
str[len] = L'\0';
408+
409+
return len;
410+
}
411+
412+
/**
413+
* Removes any of the following namespace prefixes from a path,
414+
* if found: "\??\", "\\?\", "\\?\UNC\". This function cannot fail.
415+
*
416+
* @param path The path which should be converted.
417+
* @return The length of the modified string (<= the input length)
418+
*/
419+
size_t git_win32_path_remove_namespace(wchar_t *str, size_t len)
420+
{
421+
static const wchar_t dosdevices_prefix[] = L"\\\?\?\\";
422+
static const wchar_t nt_prefix[] = L"\\\\?\\";
423+
static const wchar_t unc_prefix[] = L"UNC\\";
424+
static const wchar_t unc_canonicalized_prefix[] = L"\\\\";
425+
426+
size_t to_advance = 0;
427+
428+
/* "\??\" -- DOS Devices prefix */
429+
if (len >= CONST_STRLEN(dosdevices_prefix) &&
430+
!wcsncmp(str, dosdevices_prefix, CONST_STRLEN(dosdevices_prefix))) {
431+
to_advance += CONST_STRLEN(dosdevices_prefix);
432+
len -= CONST_STRLEN(dosdevices_prefix);
433+
}
434+
/* "\\?\" -- NT namespace prefix */
435+
else if (len >= CONST_STRLEN(nt_prefix) &&
436+
!wcsncmp(str, nt_prefix, CONST_STRLEN(nt_prefix))) {
437+
to_advance += CONST_STRLEN(nt_prefix);
438+
len -= CONST_STRLEN(nt_prefix);
439+
}
440+
441+
/* "\??\UNC\", "\\?\UNC\" -- UNC prefix */
442+
if (to_advance && len >= CONST_STRLEN(unc_prefix) &&
443+
!wcsncmp(str + to_advance, unc_prefix, CONST_STRLEN(unc_prefix))) {
444+
445+
/*
446+
* The proper Win32 path for a UNC share has "\\" at beginning of it
447+
* and looks like "\\server\share\<folderStructure>".
448+
* So, remove the UNC prefix, but leave room for a "\\"
449+
*/
450+
to_advance += (CONST_STRLEN(unc_prefix) - CONST_STRLEN(unc_canonicalized_prefix));
451+
len -= (CONST_STRLEN(unc_prefix) - CONST_STRLEN(unc_canonicalized_prefix));
452+
453+
/**
454+
* Place a "\\" in the string so the result is "\\server\\share\<folderStructure>"
455+
*/
456+
memmove(str + to_advance, unc_canonicalized_prefix, CONST_STRLEN(unc_canonicalized_prefix) * sizeof(wchar_t));
457+
}
458+
459+
if (to_advance) {
460+
memmove(str, str + to_advance, len * sizeof(wchar_t));
461+
str[len] = L'\0';
462+
}
463+
464+
return git_win32_path_trim_end(str, len);
465+
}

src/win32/path_w32.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,4 +83,22 @@ extern char *git_win32_path_8dot3_name(const char *path);
8383

8484
extern int git_win32_path_readlink_w(git_win32_path dest, const git_win32_path path);
8585

86+
/**
87+
* Removes any trailing backslashes from a path, except in the case of a drive
88+
* letter path (C:\, D:\, etc.). This function cannot fail.
89+
*
90+
* @param path The path which should be trimmed.
91+
* @return The length of the modified string (<= the input length)
92+
*/
93+
size_t git_win32_path_trim_end(wchar_t *str, size_t len);
94+
95+
/**
96+
* Removes any of the following namespace prefixes from a path,
97+
* if found: "\??\", "\\?\", "\\?\UNC\". This function cannot fail.
98+
*
99+
* @param path The path which should be converted.
100+
* @return The length of the modified string (<= the input length)
101+
*/
102+
size_t git_win32_path_remove_namespace(wchar_t *str, size_t len);
103+
86104
#endif

src/win32/posix_w32.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,7 @@ static int do_lstat(const char *path, struct stat *buf, bool posixly_correct)
354354
if ((len = git_win32_path_from_utf8(path_w, path)) < 0)
355355
return -1;
356356

357-
git_win32__path_trim_end(path_w, len);
357+
git_win32_path_trim_end(path_w, len);
358358

359359
return lstat_w(path_w, buf, posixly_correct);
360360
}
@@ -648,8 +648,8 @@ static int getfinalpath_w(
648648
if (!dwChars || dwChars >= GIT_WIN_PATH_UTF16)
649649
return -1;
650650

651-
/* The path may be delivered to us with a prefix; canonicalize */
652-
return (int)git_win32__canonicalize_path(dest, dwChars);
651+
/* The path may be delivered to us with a namespace prefix; remove */
652+
return (int)git_win32_path_remove_namespace(dest, dwChars);
653653
}
654654

655655
static int follow_and_lstat_link(git_win32_path path, struct stat* buf)

src/win32/w32_util.c

Lines changed: 0 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -93,83 +93,3 @@ int git_win32__hidden(bool *out, const char *path)
9393
*out = (attrs & FILE_ATTRIBUTE_HIDDEN) ? true : false;
9494
return 0;
9595
}
96-
97-
/**
98-
* Removes any trailing backslashes from a path, except in the case of a drive
99-
* letter path (C:\, D:\, etc.). This function cannot fail.
100-
*
101-
* @param path The path which should be trimmed.
102-
* @return The length of the modified string (<= the input length)
103-
*/
104-
size_t git_win32__path_trim_end(wchar_t *str, size_t len)
105-
{
106-
while (1) {
107-
if (!len || str[len - 1] != L'\\')
108-
break;
109-
110-
/* Don't trim backslashes from drive letter paths, which
111-
* are 3 characters long and of the form C:\, D:\, etc. */
112-
if (len == 3 && git_win32__isalpha(str[0]) && str[1] == ':')
113-
break;
114-
115-
len--;
116-
}
117-
118-
str[len] = L'\0';
119-
120-
return len;
121-
}
122-
123-
/**
124-
* Removes any of the following namespace prefixes from a path,
125-
* if found: "\??\", "\\?\", "\\?\UNC\". This function cannot fail.
126-
*
127-
* @param path The path which should be converted.
128-
* @return The length of the modified string (<= the input length)
129-
*/
130-
size_t git_win32__canonicalize_path(wchar_t *str, size_t len)
131-
{
132-
static const wchar_t dosdevices_prefix[] = L"\\\?\?\\";
133-
static const wchar_t nt_prefix[] = L"\\\\?\\";
134-
static const wchar_t unc_prefix[] = L"UNC\\";
135-
static const wchar_t unc_canonicalized_prefix[] = L"\\\\";
136-
137-
size_t to_advance = 0;
138-
139-
/* "\??\" -- DOS Devices prefix */
140-
if (len >= CONST_STRLEN(dosdevices_prefix) &&
141-
!wcsncmp(str, dosdevices_prefix, CONST_STRLEN(dosdevices_prefix))) {
142-
to_advance += CONST_STRLEN(dosdevices_prefix);
143-
len -= CONST_STRLEN(dosdevices_prefix);
144-
}
145-
/* "\\?\" -- NT namespace prefix */
146-
else if (len >= CONST_STRLEN(nt_prefix) &&
147-
!wcsncmp(str, nt_prefix, CONST_STRLEN(nt_prefix))) {
148-
to_advance += CONST_STRLEN(nt_prefix);
149-
len -= CONST_STRLEN(nt_prefix);
150-
}
151-
152-
/* "\??\UNC\", "\\?\UNC\" -- UNC prefix */
153-
if (to_advance && len >= CONST_STRLEN(unc_prefix) &&
154-
!wcsncmp(str + to_advance, unc_prefix, CONST_STRLEN(unc_prefix))) {
155-
/**
156-
* The proper Win32 path for a UNC share has "\\" at beginning of it
157-
* and looks like "\\server\share\<folderStructure>".
158-
* So, remove th UNC prefix, but leave room for a "\\"
159-
*/
160-
to_advance += (CONST_STRLEN(unc_prefix) - CONST_STRLEN(unc_canonicalized_prefix));
161-
len -= (CONST_STRLEN(unc_prefix) - CONST_STRLEN(unc_canonicalized_prefix));
162-
163-
/**
164-
* Place a "\\" in the string so the result is "\\server\\share\<folderStructure>"
165-
*/
166-
memmove(str + to_advance, unc_canonicalized_prefix, CONST_STRLEN(unc_canonicalized_prefix) * sizeof(wchar_t));
167-
}
168-
169-
if (to_advance) {
170-
memmove(str, str + to_advance, len * sizeof(wchar_t));
171-
str[len] = L'\0';
172-
}
173-
174-
return git_win32__path_trim_end(str, len);
175-
}

src/win32/w32_util.h

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -59,24 +59,6 @@ extern int git_win32__set_hidden(const char *path, bool hidden);
5959
*/
6060
extern int git_win32__hidden(bool *hidden, const char *path);
6161

62-
/**
63-
* Removes any trailing backslashes from a path, except in the case of a drive
64-
* letter path (C:\, D:\, etc.). This function cannot fail.
65-
*
66-
* @param path The path which should be trimmed.
67-
* @return The length of the modified string (<= the input length)
68-
*/
69-
size_t git_win32__path_trim_end(wchar_t *str, size_t len);
70-
71-
/**
72-
* Removes any of the following namespace prefixes from a path,
73-
* if found: "\??\", "\\?\", "\\?\UNC\". This function cannot fail.
74-
*
75-
* @param path The path which should be converted.
76-
* @return The length of the modified string (<= the input length)
77-
*/
78-
size_t git_win32__canonicalize_path(wchar_t *str, size_t len);
79-
8062
/**
8163
* Converts a FILETIME structure to a struct timespec.
8264
*

tests/path/win32.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ void test_path_win32__absolute_from_relative(void)
129129
#endif
130130
}
131131

132-
void static test_canonicalize(const wchar_t *in, const wchar_t *expected)
132+
static void test_canonicalize(const wchar_t *in, const wchar_t *expected)
133133
{
134134
#ifdef GIT_WIN32
135135
git_win32_path canonical;
@@ -145,25 +145,25 @@ void static test_canonicalize(const wchar_t *in, const wchar_t *expected)
145145
#endif
146146
}
147147

148-
void static test_canonicalize_path(const wchar_t *in, const wchar_t *expected)
148+
static void test_remove_namespace(const wchar_t *in, const wchar_t *expected)
149149
{
150150
#ifdef GIT_WIN32
151151
git_win32_path canonical;
152152

153153
cl_assert(wcslen(in) < MAX_PATH);
154154
wcscpy(canonical, in);
155155

156-
cl_must_pass(git_win32__canonicalize_path(canonical, wcslen(in)));
156+
cl_must_pass(git_win32_path_remove_namespace(canonical, wcslen(in)));
157157
cl_assert_equal_wcs(expected, canonical);
158158
#else
159159
GIT_UNUSED(in);
160160
GIT_UNUSED(expected);
161161
#endif
162162
}
163163

164-
void test_path_win32__canonicalize_path(void)
164+
void test_path_win32__remove_namespace(void)
165165
{
166-
test_canonicalize_path(L"\\\\?\\UNC\\server\\C$\\folder", L"\\\\server\\C$\\folder");
166+
test_remove_namespace(L"\\\\?\\UNC\\server\\C$\\folder", L"\\\\server\\C$\\folder");
167167
}
168168

169169
void test_path_win32__canonicalize(void)

0 commit comments

Comments
 (0)