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_dirsep(p) ((p) == '/' || (p) == '\\')
23 #define path__is_absolute(p) \
24 (git__isalpha((p)[0]) && (p)[1] == ':' && ((p)[2] == '\\' || (p)[2] == '/'))
26 #define path__is_nt_namespace(p) \
27 (((p)[0] == '\\' && (p)[1] == '\\' && (p)[2] == '?' && (p)[3] == '\\') || \
28 ((p)[0] == '/' && (p)[1] == '/' && (p)[2] == '?' && (p)[3] == '/'))
30 #define path__is_unc(p) \
31 (((p)[0] == '\\' && (p)[1] == '\\') || ((p)[0] == '/' && (p)[1] == '/'))
33 GIT_INLINE(int) path__cwd(wchar_t *path
, int size
)
37 if ((len
= GetCurrentDirectoryW(size
, path
)) == 0) {
38 errno
= GetLastError() == ERROR_ACCESS_DENIED
? EACCES
: ENOENT
;
40 } else if (len
> size
) {
45 /* The Win32 APIs may return "\\?\" once you've used it first.
46 * But it may not. What a gloriously predictible API!
48 if (wcsncmp(path
, PATH__NT_NAMESPACE
, PATH__NT_NAMESPACE_LEN
))
51 len
-= PATH__NT_NAMESPACE_LEN
;
53 memmove(path
, path
+ PATH__NT_NAMESPACE_LEN
, sizeof(wchar_t) * len
);
57 static wchar_t *path__skip_server(wchar_t *path
)
61 for (c
= path
; *c
; c
++) {
62 if (path__is_dirsep(*c
))
69 static wchar_t *path__skip_prefix(wchar_t *path
)
71 if (path__is_nt_namespace(path
)) {
72 path
+= PATH__NT_NAMESPACE_LEN
;
74 if (wcsncmp(path
, L
"UNC\\", 4) == 0)
75 path
= path__skip_server(path
+ 4);
76 else if (path__is_absolute(path
))
77 path
+= PATH__ABSOLUTE_LEN
;
78 } else if (path__is_absolute(path
)) {
79 path
+= PATH__ABSOLUTE_LEN
;
80 } else if (path__is_unc(path
)) {
81 path
= path__skip_server(path
+ 2);
87 int git_win32_path_canonicalize(git_win32_path path
)
89 wchar_t *base
, *from
, *to
, *next
;
92 base
= to
= path__skip_prefix(path
);
94 /* Unposixify if the prefix */
95 for (from
= path
; from
< to
; from
++) {
101 for (next
= from
; *next
; ++next
) {
113 if (len
== 1 && from
[0] == L
'.')
114 /* do nothing with singleton dot */;
116 else if (len
== 2 && from
[0] == L
'.' && from
[1] == L
'.') {
118 /* no more path segments to strip, eat the "../" */
124 /* back up a path segment */
125 while (to
> base
&& to
[-1] == L
'\\') to
--;
126 while (to
> base
&& to
[-1] != L
'\\') to
--;
129 if (*next
== L
'\\' && *from
!= L
'\\')
133 memmove(to
, from
, sizeof(wchar_t) * len
);
140 while (*from
== L
'\\') from
++;
143 /* Strip trailing backslashes */
144 while (to
> base
&& to
[-1] == L
'\\') to
--;
151 int git_win32_path__cwd(wchar_t *out
, size_t len
)
155 if ((cwd_len
= path__cwd(out
, len
)) < 0)
159 if (wcsncmp(L
"\\\\", out
, 2) == 0) {
160 /* Our buffer must be at least 5 characters larger than the
161 * current working directory: we swallow one of the leading
162 * '\'s, but we we add a 'UNC' specifier to the path, plus
163 * a trailing directory separator, plus a NUL.
165 if (cwd_len
> MAX_PATH
- 4) {
166 errno
= ENAMETOOLONG
;
170 memmove(out
+2, out
, sizeof(wchar_t) * cwd_len
);
178 /* Our buffer must be at least 2 characters larger than the current
179 * working directory. (One character for the directory separator,
182 else if (cwd_len
> MAX_PATH
- 2) {
183 errno
= ENAMETOOLONG
;
190 int git_win32_path_from_utf8(git_win32_path out
, const char *src
)
194 /* All win32 paths are in NT-prefixed format, beginning with "\\?\". */
195 memcpy(dest
, PATH__NT_NAMESPACE
, sizeof(wchar_t) * PATH__NT_NAMESPACE_LEN
);
196 dest
+= PATH__NT_NAMESPACE_LEN
;
198 /* See if this is an absolute path (beginning with a drive letter) */
199 if (path__is_absolute(src
)) {
200 if (git__utf8_to_16(dest
, MAX_PATH
, src
) < 0)
203 /* File-prefixed NT-style paths beginning with \\?\ */
204 else if (path__is_nt_namespace(src
)) {
205 /* Skip the NT prefix, the destination already contains it */
206 if (git__utf8_to_16(dest
, MAX_PATH
, src
+ PATH__NT_NAMESPACE_LEN
) < 0)
210 else if (path__is_unc(src
)) {
211 memcpy(dest
, L
"UNC\\", sizeof(wchar_t) * 4);
214 /* Skip the leading "\\" */
215 if (git__utf8_to_16(dest
, MAX_PATH
- 2, src
+ 2) < 0)
218 /* Absolute paths omitting the drive letter */
219 else if (src
[0] == '\\' || src
[0] == '/') {
220 if (path__cwd(dest
, MAX_PATH
) < 0)
223 if (!path__is_absolute(dest
)) {
228 /* Skip the drive letter specification ("C:") */
229 if (git__utf8_to_16(dest
+ 2, MAX_PATH
- 2, src
) < 0)
236 if ((cwd_len
= git_win32_path__cwd(dest
, MAX_PATH
)) < 0)
239 dest
[cwd_len
++] = L
'\\';
241 if (git__utf8_to_16(dest
+ cwd_len
, MAX_PATH
- cwd_len
, src
) < 0)
245 return git_win32_path_canonicalize(out
);
248 /* set windows error code so we can use its error message */
249 if (errno
== ENAMETOOLONG
)
250 SetLastError(ERROR_FILENAME_EXCED_RANGE
);
255 int git_win32_path_to_utf8(git_win32_utf8_path dest
, const wchar_t *src
)
260 /* Strip NT namespacing "\\?\" */
261 if (path__is_nt_namespace(src
)) {
264 /* "\\?\UNC\server\share" -> "\\server\share" */
265 if (wcsncmp(src
, L
"UNC\\", 4) == 0) {
268 memcpy(dest
, "\\\\", 2);
273 if ((len
= git__utf16_to_8(out
, GIT_WIN_PATH_UTF8
, src
)) < 0)
276 git_path_mkposix(dest
);
281 char *git_win32_path_8dot3_name(const char *path
)
283 git_win32_path longpath
, shortpath
;
286 int len
, namelen
= 1;
288 if (git_win32_path_from_utf8(longpath
, path
) < 0)
291 len
= GetShortPathNameW(longpath
, shortpath
, GIT_WIN_PATH_UTF16
);
293 while (len
&& shortpath
[len
-1] == L
'\\')
294 shortpath
[--len
] = L
'\0';
296 if (len
== 0 || len
>= GIT_WIN_PATH_UTF16
)
299 for (start
= shortpath
+ (len
- 1);
300 start
> shortpath
&& *(start
-1) != '/' && *(start
-1) != '\\';
304 /* We may not have actually been given a short name. But if we have,
305 * it will be in the ASCII byte range, so we don't need to worry about
306 * multi-byte sequences and can allocate naively.
308 if (namelen
> 12 || (shortname
= git__malloc(namelen
+ 1)) == NULL
)
311 if ((len
= git__utf16_to_8(shortname
, namelen
+ 1, start
)) < 0)
317 static bool path_is_volume(wchar_t *target
, size_t target_len
)
319 return (target_len
&& wcsncmp(target
, L
"\\??\\Volume{", 11) == 0);
322 /* On success, returns the length, in characters, of the path stored in dest.
323 * On failure, returns a negative value. */
324 int git_win32_path_readlink_w(git_win32_path dest
, const git_win32_path path
)
326 BYTE buf
[MAXIMUM_REPARSE_DATA_BUFFER_SIZE
];
327 GIT_REPARSE_DATA_BUFFER
*reparse_buf
= (GIT_REPARSE_DATA_BUFFER
*)buf
;
328 HANDLE handle
= NULL
;
335 handle
= CreateFileW(path
, GENERIC_READ
,
336 FILE_SHARE_READ
| FILE_SHARE_DELETE
, NULL
, OPEN_EXISTING
,
337 FILE_FLAG_OPEN_REPARSE_POINT
| FILE_FLAG_BACKUP_SEMANTICS
, NULL
);
339 if (handle
== INVALID_HANDLE_VALUE
) {
344 if (!DeviceIoControl(handle
, FSCTL_GET_REPARSE_POINT
, NULL
, 0,
345 reparse_buf
, sizeof(buf
), &ioctl_ret
, NULL
)) {
350 switch (reparse_buf
->ReparseTag
) {
351 case IO_REPARSE_TAG_SYMLINK
:
352 target
= reparse_buf
->SymbolicLinkReparseBuffer
.PathBuffer
+
353 (reparse_buf
->SymbolicLinkReparseBuffer
.SubstituteNameOffset
/ sizeof(WCHAR
));
354 target_len
= reparse_buf
->SymbolicLinkReparseBuffer
.SubstituteNameLength
/ sizeof(WCHAR
);
356 case IO_REPARSE_TAG_MOUNT_POINT
:
357 target
= reparse_buf
->MountPointReparseBuffer
.PathBuffer
+
358 (reparse_buf
->MountPointReparseBuffer
.SubstituteNameOffset
/ sizeof(WCHAR
));
359 target_len
= reparse_buf
->MountPointReparseBuffer
.SubstituteNameLength
/ sizeof(WCHAR
);
366 if (path_is_volume(target
, target_len
)) {
367 /* This path is a reparse point that represents another volume mounted
368 * at this location, it is not a symbolic link our input was canonical.
372 } else if (target_len
) {
373 /* The path may need to have a prefix removed. */
374 target_len
= git_win32__canonicalize_path(target
, target_len
);
376 /* Need one additional character in the target buffer
377 * for the terminating NULL. */
378 if (GIT_WIN_PATH_UTF16
> target_len
) {
379 wcscpy(dest
, target
);
380 error
= (int)target_len
;