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 GIT_INLINE(int) path__cwd(wchar_t *path
, int size
)
32 if ((len
= GetCurrentDirectoryW(size
, path
)) == 0) {
33 errno
= GetLastError() == ERROR_ACCESS_DENIED
? EACCES
: ENOENT
;
35 } else if (len
> size
) {
40 /* The Win32 APIs may return "\\?\" once you've used it first.
41 * But it may not. What a gloriously predictible API!
43 if (wcsncmp(path
, PATH__NT_NAMESPACE
, PATH__NT_NAMESPACE_LEN
))
46 len
-= PATH__NT_NAMESPACE_LEN
;
48 memmove(path
, path
+ PATH__NT_NAMESPACE_LEN
, sizeof(wchar_t) * len
);
52 static wchar_t *path__skip_server(wchar_t *path
)
56 for (c
= path
; *c
; c
++) {
57 if (git_path_is_dirsep(*c
))
64 static wchar_t *path__skip_prefix(wchar_t *path
)
66 if (path__is_nt_namespace(path
)) {
67 path
+= PATH__NT_NAMESPACE_LEN
;
69 if (wcsncmp(path
, L
"UNC\\", 4) == 0)
70 path
= path__skip_server(path
+ 4);
71 else if (git_path_is_absolute(path
))
72 path
+= PATH__ABSOLUTE_LEN
;
73 } else if (git_path_is_absolute(path
)) {
74 path
+= PATH__ABSOLUTE_LEN
;
75 } else if (path__is_unc(path
)) {
76 path
= path__skip_server(path
+ 2);
82 int git_win32_path_canonicalize(git_win32_path path
)
84 wchar_t *base
, *from
, *to
, *next
;
87 base
= to
= path__skip_prefix(path
);
89 /* Unposixify if the prefix */
90 for (from
= path
; from
< to
; from
++) {
96 for (next
= from
; *next
; ++next
) {
108 if (len
== 1 && from
[0] == L
'.')
109 /* do nothing with singleton dot */;
111 else if (len
== 2 && from
[0] == L
'.' && from
[1] == L
'.') {
113 /* no more path segments to strip, eat the "../" */
119 /* back up a path segment */
120 while (to
> base
&& to
[-1] == L
'\\') to
--;
121 while (to
> base
&& to
[-1] != L
'\\') to
--;
124 if (*next
== L
'\\' && *from
!= L
'\\')
128 memmove(to
, from
, sizeof(wchar_t) * len
);
135 while (*from
== L
'\\') from
++;
138 /* Strip trailing backslashes */
139 while (to
> base
&& to
[-1] == L
'\\') to
--;
146 int git_win32_path__cwd(wchar_t *out
, size_t len
)
150 if ((cwd_len
= path__cwd(out
, len
)) < 0)
154 if (wcsncmp(L
"\\\\", out
, 2) == 0) {
155 /* Our buffer must be at least 5 characters larger than the
156 * current working directory: we swallow one of the leading
157 * '\'s, but we we add a 'UNC' specifier to the path, plus
158 * a trailing directory separator, plus a NUL.
160 if (cwd_len
> MAX_PATH
- 4) {
161 errno
= ENAMETOOLONG
;
165 memmove(out
+2, out
, sizeof(wchar_t) * cwd_len
);
173 /* Our buffer must be at least 2 characters larger than the current
174 * working directory. (One character for the directory separator,
177 else if (cwd_len
> MAX_PATH
- 2) {
178 errno
= ENAMETOOLONG
;
185 int git_win32_path_from_utf8(git_win32_path out
, const char *src
)
189 /* All win32 paths are in NT-prefixed format, beginning with "\\?\". */
190 memcpy(dest
, PATH__NT_NAMESPACE
, sizeof(wchar_t) * PATH__NT_NAMESPACE_LEN
);
191 dest
+= PATH__NT_NAMESPACE_LEN
;
193 /* See if this is an absolute path (beginning with a drive letter) */
194 if (git_path_is_absolute(src
)) {
195 if (git__utf8_to_16(dest
, MAX_PATH
, src
) < 0)
198 /* File-prefixed NT-style paths beginning with \\?\ */
199 else if (path__is_nt_namespace(src
)) {
200 /* Skip the NT prefix, the destination already contains it */
201 if (git__utf8_to_16(dest
, MAX_PATH
, src
+ PATH__NT_NAMESPACE_LEN
) < 0)
205 else if (path__is_unc(src
)) {
206 memcpy(dest
, L
"UNC\\", sizeof(wchar_t) * 4);
209 /* Skip the leading "\\" */
210 if (git__utf8_to_16(dest
, MAX_PATH
- 2, src
+ 2) < 0)
213 /* Absolute paths omitting the drive letter */
214 else if (src
[0] == '\\' || src
[0] == '/') {
215 if (path__cwd(dest
, MAX_PATH
) < 0)
218 if (!git_path_is_absolute(dest
)) {
223 /* Skip the drive letter specification ("C:") */
224 if (git__utf8_to_16(dest
+ 2, MAX_PATH
- 2, src
) < 0)
231 if ((cwd_len
= git_win32_path__cwd(dest
, MAX_PATH
)) < 0)
234 dest
[cwd_len
++] = L
'\\';
236 if (git__utf8_to_16(dest
+ cwd_len
, MAX_PATH
- cwd_len
, src
) < 0)
240 return git_win32_path_canonicalize(out
);
243 /* set windows error code so we can use its error message */
244 if (errno
== ENAMETOOLONG
)
245 SetLastError(ERROR_FILENAME_EXCED_RANGE
);
250 int git_win32_path_to_utf8(git_win32_utf8_path dest
, const wchar_t *src
)
255 /* Strip NT namespacing "\\?\" */
256 if (path__is_nt_namespace(src
)) {
259 /* "\\?\UNC\server\share" -> "\\server\share" */
260 if (wcsncmp(src
, L
"UNC\\", 4) == 0) {
263 memcpy(dest
, "\\\\", 2);
268 if ((len
= git__utf16_to_8(out
, GIT_WIN_PATH_UTF8
, src
)) < 0)
271 git_path_mkposix(dest
);
276 char *git_win32_path_8dot3_name(const char *path
)
278 git_win32_path longpath
, shortpath
;
281 int len
, namelen
= 1;
283 if (git_win32_path_from_utf8(longpath
, path
) < 0)
286 len
= GetShortPathNameW(longpath
, shortpath
, GIT_WIN_PATH_UTF16
);
288 while (len
&& shortpath
[len
-1] == L
'\\')
289 shortpath
[--len
] = L
'\0';
291 if (len
== 0 || len
>= GIT_WIN_PATH_UTF16
)
294 for (start
= shortpath
+ (len
- 1);
295 start
> shortpath
&& *(start
-1) != '/' && *(start
-1) != '\\';
299 /* We may not have actually been given a short name. But if we have,
300 * it will be in the ASCII byte range, so we don't need to worry about
301 * multi-byte sequences and can allocate naively.
303 if (namelen
> 12 || (shortname
= git__malloc(namelen
+ 1)) == NULL
)
306 if ((len
= git__utf16_to_8(shortname
, namelen
+ 1, start
)) < 0)
312 static bool path_is_volume(wchar_t *target
, size_t target_len
)
314 return (target_len
&& wcsncmp(target
, L
"\\??\\Volume{", 11) == 0);
317 /* On success, returns the length, in characters, of the path stored in dest.
318 * On failure, returns a negative value. */
319 int git_win32_path_readlink_w(git_win32_path dest
, const git_win32_path path
)
321 BYTE buf
[MAXIMUM_REPARSE_DATA_BUFFER_SIZE
];
322 GIT_REPARSE_DATA_BUFFER
*reparse_buf
= (GIT_REPARSE_DATA_BUFFER
*)buf
;
323 HANDLE handle
= NULL
;
330 handle
= CreateFileW(path
, GENERIC_READ
,
331 FILE_SHARE_READ
| FILE_SHARE_DELETE
, NULL
, OPEN_EXISTING
,
332 FILE_FLAG_OPEN_REPARSE_POINT
| FILE_FLAG_BACKUP_SEMANTICS
, NULL
);
334 if (handle
== INVALID_HANDLE_VALUE
) {
339 if (!DeviceIoControl(handle
, FSCTL_GET_REPARSE_POINT
, NULL
, 0,
340 reparse_buf
, sizeof(buf
), &ioctl_ret
, NULL
)) {
345 switch (reparse_buf
->ReparseTag
) {
346 case IO_REPARSE_TAG_SYMLINK
:
347 target
= reparse_buf
->SymbolicLinkReparseBuffer
.PathBuffer
+
348 (reparse_buf
->SymbolicLinkReparseBuffer
.SubstituteNameOffset
/ sizeof(WCHAR
));
349 target_len
= reparse_buf
->SymbolicLinkReparseBuffer
.SubstituteNameLength
/ sizeof(WCHAR
);
351 case IO_REPARSE_TAG_MOUNT_POINT
:
352 target
= reparse_buf
->MountPointReparseBuffer
.PathBuffer
+
353 (reparse_buf
->MountPointReparseBuffer
.SubstituteNameOffset
/ sizeof(WCHAR
));
354 target_len
= reparse_buf
->MountPointReparseBuffer
.SubstituteNameLength
/ sizeof(WCHAR
);
361 if (path_is_volume(target
, target_len
)) {
362 /* 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.
367 } else if (target_len
) {
368 /* The path may need to have a namespace prefix removed. */
369 target_len
= git_win32_path_remove_namespace(target
, target_len
);
371 /* Need one additional character in the target buffer
372 * for the terminating NULL. */
373 if (GIT_WIN_PATH_UTF16
> target_len
) {
374 wcscpy(dest
, target
);
375 error
= (int)target_len
;
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.
388 * @param path The path which should be trimmed.
389 * @return The length of the modified string (<= the input length)
391 size_t git_win32_path_trim_end(wchar_t *str
, size_t len
)
394 if (!len
|| str
[len
- 1] != L
'\\')
398 * Don't trim backslashes from drive letter paths, which
399 * are 3 characters long and of the form C:\, D:\, etc.
401 if (len
== 3 && git_win32__isalpha(str
[0]) && str
[1] == ':')
413 * Removes any of the following namespace prefixes from a path,
414 * if found: "\??\", "\\?\", "\\?\UNC\". This function cannot fail.
416 * @param path The path which should be converted.
417 * @return The length of the modified string (<= the input length)
419 size_t git_win32_path_remove_namespace(wchar_t *str
, size_t len
)
421 static const wchar_t dosdevices_namespace
[] = L
"\\\?\?\\";
422 static const wchar_t nt_namespace
[] = L
"\\\\?\\";
423 static const wchar_t unc_namespace_remainder
[] = L
"UNC\\";
424 static const wchar_t unc_prefix
[] = L
"\\\\";
426 const wchar_t *prefix
= NULL
, *remainder
= NULL
;
427 size_t prefix_len
= 0, remainder_len
= 0;
429 /* "\??\" -- DOS Devices prefix */
430 if (len
>= CONST_STRLEN(dosdevices_namespace
) &&
431 !wcsncmp(str
, dosdevices_namespace
, CONST_STRLEN(dosdevices_namespace
))) {
432 remainder
= str
+ CONST_STRLEN(dosdevices_namespace
);
433 remainder_len
= len
- CONST_STRLEN(dosdevices_namespace
);
435 /* "\\?\" -- NT namespace prefix */
436 else if (len
>= CONST_STRLEN(nt_namespace
) &&
437 !wcsncmp(str
, nt_namespace
, CONST_STRLEN(nt_namespace
))) {
438 remainder
= str
+ CONST_STRLEN(nt_namespace
);
439 remainder_len
= len
- CONST_STRLEN(nt_namespace
);
442 /* "\??\UNC\", "\\?\UNC\" -- UNC prefix */
443 if (remainder_len
>= CONST_STRLEN(unc_namespace_remainder
) &&
444 !wcsncmp(remainder
, unc_namespace_remainder
, CONST_STRLEN(unc_namespace_remainder
))) {
447 * The proper Win32 path for a UNC share has "\\" at beginning of it
448 * and looks like "\\server\share\<folderStructure>". So remove the
449 * UNC namespace and add a prefix of "\\" in its place.
451 remainder
+= CONST_STRLEN(unc_namespace_remainder
);
452 remainder_len
-= CONST_STRLEN(unc_namespace_remainder
);
455 prefix_len
= CONST_STRLEN(unc_prefix
);
460 * Sanity check that the new string isn't longer than the old one.
461 * (This could only happen due to programmer error introducing a
462 * prefix longer than the namespace it replaces.)
464 assert(len
>= remainder_len
+ prefix_len
);
467 memmove(str
, prefix
, prefix_len
* sizeof(wchar_t));
469 memmove(str
+ prefix_len
, remainder
, remainder_len
* sizeof(wchar_t));
471 len
= remainder_len
+ prefix_len
;
475 return git_win32_path_trim_end(str
, len
);