2 * Copyright (C) the libgit2 contributors. All rights reserved.
4 * This file is part of libgit2, distributed under the GNU GPL v2 with
5 * a Linking Exception. For full terms see the included COPYING file.
16 #define PATH__NT_NAMESPACE L"\\\\?\\"
17 #define PATH__NT_NAMESPACE_LEN 4
19 #define PATH__ABSOLUTE_LEN 3
21 #define path__is_nt_namespace(p) \
22 (((p)[0] == '\\' && (p)[1] == '\\' && (p)[2] == '?' && (p)[3] == '\\') || \
23 ((p)[0] == '/' && (p)[1] == '/' && (p)[2] == '?' && (p)[3] == '/'))
25 #define path__is_unc(p) \
26 (((p)[0] == '\\' && (p)[1] == '\\') || ((p)[0] == '/' && (p)[1] == '/'))
28 #define path__startswith_slash(p) \
29 ((p)[0] == '\\' || (p)[0] == '/')
31 GIT_INLINE(int) path__cwd(wchar_t *path
, int size
)
35 if ((len
= GetCurrentDirectoryW(size
, path
)) == 0) {
36 errno
= GetLastError() == ERROR_ACCESS_DENIED
? EACCES
: ENOENT
;
38 } else if (len
> size
) {
43 /* The Win32 APIs may return "\\?\" once you've used it first.
44 * But it may not. What a gloriously predictible API!
46 if (wcsncmp(path
, PATH__NT_NAMESPACE
, PATH__NT_NAMESPACE_LEN
))
49 len
-= PATH__NT_NAMESPACE_LEN
;
51 memmove(path
, path
+ PATH__NT_NAMESPACE_LEN
, sizeof(wchar_t) * len
);
55 static wchar_t *path__skip_server(wchar_t *path
)
59 for (c
= path
; *c
; c
++) {
60 if (git_path_is_dirsep(*c
))
67 static wchar_t *path__skip_prefix(wchar_t *path
)
69 if (path__is_nt_namespace(path
)) {
70 path
+= PATH__NT_NAMESPACE_LEN
;
72 if (wcsncmp(path
, L
"UNC\\", 4) == 0)
73 path
= path__skip_server(path
+ 4);
74 else if (git_path_is_absolute(path
))
75 path
+= PATH__ABSOLUTE_LEN
;
76 } else if (git_path_is_absolute(path
)) {
77 path
+= PATH__ABSOLUTE_LEN
;
78 } else if (path__is_unc(path
)) {
79 path
= path__skip_server(path
+ 2);
85 int git_win32_path_canonicalize(git_win32_path path
)
87 wchar_t *base
, *from
, *to
, *next
;
90 base
= to
= path__skip_prefix(path
);
92 /* Unposixify if the prefix */
93 for (from
= path
; from
< to
; from
++) {
99 for (next
= from
; *next
; ++next
) {
111 if (len
== 1 && from
[0] == L
'.')
112 /* do nothing with singleton dot */;
114 else if (len
== 2 && from
[0] == L
'.' && from
[1] == L
'.') {
116 /* no more path segments to strip, eat the "../" */
122 /* back up a path segment */
123 while (to
> base
&& to
[-1] == L
'\\') to
--;
124 while (to
> base
&& to
[-1] != L
'\\') to
--;
127 if (*next
== L
'\\' && *from
!= L
'\\')
131 memmove(to
, from
, sizeof(wchar_t) * len
);
138 while (*from
== L
'\\') from
++;
141 /* Strip trailing backslashes */
142 while (to
> base
&& to
[-1] == L
'\\') to
--;
146 if ((to
- path
) > INT_MAX
) {
147 SetLastError(ERROR_FILENAME_EXCED_RANGE
);
151 return (int)(to
- path
);
154 static int win32_path_cwd(wchar_t *out
, size_t len
)
159 errno
= ENAMETOOLONG
;
163 if ((cwd_len
= path__cwd(out
, (int)len
)) < 0)
167 if (wcsncmp(L
"\\\\", out
, 2) == 0) {
168 /* Our buffer must be at least 5 characters larger than the
169 * current working directory: we swallow one of the leading
170 * '\'s, but we we add a 'UNC' specifier to the path, plus
171 * a trailing directory separator, plus a NUL.
173 if (cwd_len
> MAX_PATH
- 4) {
174 errno
= ENAMETOOLONG
;
178 memmove(out
+2, out
, sizeof(wchar_t) * cwd_len
);
186 /* Our buffer must be at least 2 characters larger than the current
187 * working directory. (One character for the directory separator,
190 else if (cwd_len
> MAX_PATH
- 2) {
191 errno
= ENAMETOOLONG
;
198 int git_win32_path_from_utf8(git_win32_path out
, const char *src
)
202 /* All win32 paths are in NT-prefixed format, beginning with "\\?\". */
203 memcpy(dest
, PATH__NT_NAMESPACE
, sizeof(wchar_t) * PATH__NT_NAMESPACE_LEN
);
204 dest
+= PATH__NT_NAMESPACE_LEN
;
206 /* See if this is an absolute path (beginning with a drive letter) */
207 if (git_path_is_absolute(src
)) {
208 if (git__utf8_to_16(dest
, MAX_PATH
, src
) < 0)
211 /* File-prefixed NT-style paths beginning with \\?\ */
212 else if (path__is_nt_namespace(src
)) {
213 /* Skip the NT prefix, the destination already contains it */
214 if (git__utf8_to_16(dest
, MAX_PATH
, src
+ PATH__NT_NAMESPACE_LEN
) < 0)
218 else if (path__is_unc(src
)) {
219 memcpy(dest
, L
"UNC\\", sizeof(wchar_t) * 4);
222 /* Skip the leading "\\" */
223 if (git__utf8_to_16(dest
, MAX_PATH
- 2, src
+ 2) < 0)
226 /* Absolute paths omitting the drive letter */
227 else if (path__startswith_slash(src
)) {
228 if (path__cwd(dest
, MAX_PATH
) < 0)
231 if (!git_path_is_absolute(dest
)) {
236 /* Skip the drive letter specification ("C:") */
237 if (git__utf8_to_16(dest
+ 2, MAX_PATH
- 2, src
) < 0)
244 if ((cwd_len
= win32_path_cwd(dest
, MAX_PATH
)) < 0)
247 dest
[cwd_len
++] = L
'\\';
249 if (git__utf8_to_16(dest
+ cwd_len
, MAX_PATH
- cwd_len
, src
) < 0)
253 return git_win32_path_canonicalize(out
);
256 /* set windows error code so we can use its error message */
257 if (errno
== ENAMETOOLONG
)
258 SetLastError(ERROR_FILENAME_EXCED_RANGE
);
263 int git_win32_path_relative_from_utf8(git_win32_path out
, const char *src
)
265 wchar_t *dest
= out
, *p
;
268 /* Handle absolute paths */
269 if (git_path_is_absolute(src
) ||
270 path__is_nt_namespace(src
) ||
272 path__startswith_slash(src
)) {
273 return git_win32_path_from_utf8(out
, src
);
276 if ((len
= git__utf8_to_16(dest
, MAX_PATH
, src
)) < 0)
279 for (p
= dest
; p
< (dest
+ len
); p
++) {
287 int git_win32_path_to_utf8(git_win32_utf8_path dest
, const wchar_t *src
)
292 /* Strip NT namespacing "\\?\" */
293 if (path__is_nt_namespace(src
)) {
296 /* "\\?\UNC\server\share" -> "\\server\share" */
297 if (wcsncmp(src
, L
"UNC\\", 4) == 0) {
300 memcpy(dest
, "\\\\", 2);
305 if ((len
= git__utf16_to_8(out
, GIT_WIN_PATH_UTF8
, src
)) < 0)
308 git_path_mkposix(dest
);
313 char *git_win32_path_8dot3_name(const char *path
)
315 git_win32_path longpath
, shortpath
;
318 int len
, namelen
= 1;
320 if (git_win32_path_from_utf8(longpath
, path
) < 0)
323 len
= GetShortPathNameW(longpath
, shortpath
, GIT_WIN_PATH_UTF16
);
325 while (len
&& shortpath
[len
-1] == L
'\\')
326 shortpath
[--len
] = L
'\0';
328 if (len
== 0 || len
>= GIT_WIN_PATH_UTF16
)
331 for (start
= shortpath
+ (len
- 1);
332 start
> shortpath
&& *(start
-1) != '/' && *(start
-1) != '\\';
336 /* We may not have actually been given a short name. But if we have,
337 * it will be in the ASCII byte range, so we don't need to worry about
338 * multi-byte sequences and can allocate naively.
340 if (namelen
> 12 || (shortname
= git__malloc(namelen
+ 1)) == NULL
)
343 if ((len
= git__utf16_to_8(shortname
, namelen
+ 1, start
)) < 0)
349 static bool path_is_volume(wchar_t *target
, size_t target_len
)
351 return (target_len
&& wcsncmp(target
, L
"\\??\\Volume{", 11) == 0);
354 /* On success, returns the length, in characters, of the path stored in dest.
355 * On failure, returns a negative value. */
356 int git_win32_path_readlink_w(git_win32_path dest
, const git_win32_path path
)
358 BYTE buf
[MAXIMUM_REPARSE_DATA_BUFFER_SIZE
];
359 GIT_REPARSE_DATA_BUFFER
*reparse_buf
= (GIT_REPARSE_DATA_BUFFER
*)buf
;
360 HANDLE handle
= NULL
;
367 handle
= CreateFileW(path
, GENERIC_READ
,
368 FILE_SHARE_READ
| FILE_SHARE_DELETE
, NULL
, OPEN_EXISTING
,
369 FILE_FLAG_OPEN_REPARSE_POINT
| FILE_FLAG_BACKUP_SEMANTICS
, NULL
);
371 if (handle
== INVALID_HANDLE_VALUE
) {
376 if (!DeviceIoControl(handle
, FSCTL_GET_REPARSE_POINT
, NULL
, 0,
377 reparse_buf
, sizeof(buf
), &ioctl_ret
, NULL
)) {
382 switch (reparse_buf
->ReparseTag
) {
383 case IO_REPARSE_TAG_SYMLINK
:
384 target
= reparse_buf
->SymbolicLinkReparseBuffer
.PathBuffer
+
385 (reparse_buf
->SymbolicLinkReparseBuffer
.SubstituteNameOffset
/ sizeof(WCHAR
));
386 target_len
= reparse_buf
->SymbolicLinkReparseBuffer
.SubstituteNameLength
/ sizeof(WCHAR
);
388 case IO_REPARSE_TAG_MOUNT_POINT
:
389 target
= reparse_buf
->MountPointReparseBuffer
.PathBuffer
+
390 (reparse_buf
->MountPointReparseBuffer
.SubstituteNameOffset
/ sizeof(WCHAR
));
391 target_len
= reparse_buf
->MountPointReparseBuffer
.SubstituteNameLength
/ sizeof(WCHAR
);
398 if (path_is_volume(target
, target_len
)) {
399 /* This path is a reparse point that represents another volume mounted
400 * at this location, it is not a symbolic link our input was canonical.
404 } else if (target_len
) {
405 /* The path may need to have a namespace prefix removed. */
406 target_len
= git_win32_path_remove_namespace(target
, target_len
);
408 /* Need one additional character in the target buffer
409 * for the terminating NULL. */
410 if (GIT_WIN_PATH_UTF16
> target_len
) {
411 wcscpy(dest
, target
);
412 error
= (int)target_len
;
422 * Removes any trailing backslashes from a path, except in the case of a drive
423 * letter path (C:\, D:\, etc.). This function cannot fail.
425 * @param path The path which should be trimmed.
426 * @return The length of the modified string (<= the input length)
428 size_t git_win32_path_trim_end(wchar_t *str
, size_t len
)
431 if (!len
|| str
[len
- 1] != L
'\\')
435 * Don't trim backslashes from drive letter paths, which
436 * are 3 characters long and of the form C:\, D:\, etc.
438 if (len
== 3 && git_win32__isalpha(str
[0]) && str
[1] == ':')
450 * Removes any of the following namespace prefixes from a path,
451 * if found: "\??\", "\\?\", "\\?\UNC\". This function cannot fail.
453 * @param path The path which should be converted.
454 * @return The length of the modified string (<= the input length)
456 size_t git_win32_path_remove_namespace(wchar_t *str
, size_t len
)
458 static const wchar_t dosdevices_namespace
[] = L
"\\\?\?\\";
459 static const wchar_t nt_namespace
[] = L
"\\\\?\\";
460 static const wchar_t unc_namespace_remainder
[] = L
"UNC\\";
461 static const wchar_t unc_prefix
[] = L
"\\\\";
463 const wchar_t *prefix
= NULL
, *remainder
= NULL
;
464 size_t prefix_len
= 0, remainder_len
= 0;
466 /* "\??\" -- DOS Devices prefix */
467 if (len
>= CONST_STRLEN(dosdevices_namespace
) &&
468 !wcsncmp(str
, dosdevices_namespace
, CONST_STRLEN(dosdevices_namespace
))) {
469 remainder
= str
+ CONST_STRLEN(dosdevices_namespace
);
470 remainder_len
= len
- CONST_STRLEN(dosdevices_namespace
);
472 /* "\\?\" -- NT namespace prefix */
473 else if (len
>= CONST_STRLEN(nt_namespace
) &&
474 !wcsncmp(str
, nt_namespace
, CONST_STRLEN(nt_namespace
))) {
475 remainder
= str
+ CONST_STRLEN(nt_namespace
);
476 remainder_len
= len
- CONST_STRLEN(nt_namespace
);
479 /* "\??\UNC\", "\\?\UNC\" -- UNC prefix */
480 if (remainder_len
>= CONST_STRLEN(unc_namespace_remainder
) &&
481 !wcsncmp(remainder
, unc_namespace_remainder
, CONST_STRLEN(unc_namespace_remainder
))) {
484 * The proper Win32 path for a UNC share has "\\" at beginning of it
485 * and looks like "\\server\share\<folderStructure>". So remove the
486 * UNC namespace and add a prefix of "\\" in its place.
488 remainder
+= CONST_STRLEN(unc_namespace_remainder
);
489 remainder_len
-= CONST_STRLEN(unc_namespace_remainder
);
492 prefix_len
= CONST_STRLEN(unc_prefix
);
497 * Sanity check that the new string isn't longer than the old one.
498 * (This could only happen due to programmer error introducing a
499 * prefix longer than the namespace it replaces.)
501 assert(len
>= remainder_len
+ prefix_len
);
504 memmove(str
, prefix
, prefix_len
* sizeof(wchar_t));
506 memmove(str
+ prefix_len
, remainder
, remainder_len
* sizeof(wchar_t));
508 len
= remainder_len
+ prefix_len
;
512 return git_win32_path_trim_end(str
, len
);