|
2 | 2 | #include "win32.h"
|
3 | 3 | #include <conio.h>
|
4 | 4 | #include <wchar.h>
|
| 5 | +#include <winioctl.h> |
5 | 6 | #include "../strbuf.h"
|
6 | 7 | #include "../run-command.h"
|
7 | 8 | #include "../cache.h"
|
@@ -2263,6 +2264,103 @@ int link(const char *oldpath, const char *newpath)
|
2263 | 2264 | return 0;
|
2264 | 2265 | }
|
2265 | 2266 |
|
| 2267 | +#ifndef _WINNT_H |
| 2268 | +/* |
| 2269 | + * The REPARSE_DATA_BUFFER structure is defined in the Windows DDK (in |
| 2270 | + * ntifs.h) and in MSYS1's winnt.h (which defines _WINNT_H). So define |
| 2271 | + * it ourselves if we are on MSYS2 (whose winnt.h defines _WINNT_). |
| 2272 | + */ |
| 2273 | +typedef struct _REPARSE_DATA_BUFFER { |
| 2274 | + DWORD ReparseTag; |
| 2275 | + WORD ReparseDataLength; |
| 2276 | + WORD Reserved; |
| 2277 | +#ifndef _MSC_VER |
| 2278 | + _ANONYMOUS_UNION |
| 2279 | +#endif |
| 2280 | + union { |
| 2281 | + struct { |
| 2282 | + WORD SubstituteNameOffset; |
| 2283 | + WORD SubstituteNameLength; |
| 2284 | + WORD PrintNameOffset; |
| 2285 | + WORD PrintNameLength; |
| 2286 | + ULONG Flags; |
| 2287 | + WCHAR PathBuffer[1]; |
| 2288 | + } SymbolicLinkReparseBuffer; |
| 2289 | + struct { |
| 2290 | + WORD SubstituteNameOffset; |
| 2291 | + WORD SubstituteNameLength; |
| 2292 | + WORD PrintNameOffset; |
| 2293 | + WORD PrintNameLength; |
| 2294 | + WCHAR PathBuffer[1]; |
| 2295 | + } MountPointReparseBuffer; |
| 2296 | + struct { |
| 2297 | + BYTE DataBuffer[1]; |
| 2298 | + } GenericReparseBuffer; |
| 2299 | + } DUMMYUNIONNAME; |
| 2300 | +} REPARSE_DATA_BUFFER, *PREPARSE_DATA_BUFFER; |
| 2301 | +#endif |
| 2302 | + |
| 2303 | +int readlink(const char *path, char *buf, size_t bufsiz) |
| 2304 | +{ |
| 2305 | + HANDLE handle; |
| 2306 | + WCHAR wpath[MAX_LONG_PATH], *wbuf; |
| 2307 | + REPARSE_DATA_BUFFER *b = alloca(MAXIMUM_REPARSE_DATA_BUFFER_SIZE); |
| 2308 | + DWORD dummy; |
| 2309 | + char tmpbuf[MAX_LONG_PATH]; |
| 2310 | + int len; |
| 2311 | + |
| 2312 | + if (xutftowcs_long_path(wpath, path) < 0) |
| 2313 | + return -1; |
| 2314 | + |
| 2315 | + /* read reparse point data */ |
| 2316 | + handle = CreateFileW(wpath, 0, |
| 2317 | + FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL, |
| 2318 | + OPEN_EXISTING, |
| 2319 | + FILE_FLAG_BACKUP_SEMANTICS | FILE_FLAG_OPEN_REPARSE_POINT, NULL); |
| 2320 | + if (handle == INVALID_HANDLE_VALUE) { |
| 2321 | + errno = err_win_to_posix(GetLastError()); |
| 2322 | + return -1; |
| 2323 | + } |
| 2324 | + if (!DeviceIoControl(handle, FSCTL_GET_REPARSE_POINT, NULL, 0, b, |
| 2325 | + MAXIMUM_REPARSE_DATA_BUFFER_SIZE, &dummy, NULL)) { |
| 2326 | + errno = err_win_to_posix(GetLastError()); |
| 2327 | + CloseHandle(handle); |
| 2328 | + return -1; |
| 2329 | + } |
| 2330 | + CloseHandle(handle); |
| 2331 | + |
| 2332 | + /* get target path for symlinks or mount points (aka 'junctions') */ |
| 2333 | + switch (b->ReparseTag) { |
| 2334 | + case IO_REPARSE_TAG_SYMLINK: |
| 2335 | + wbuf = (WCHAR*) (((char*) b->SymbolicLinkReparseBuffer.PathBuffer) |
| 2336 | + + b->SymbolicLinkReparseBuffer.SubstituteNameOffset); |
| 2337 | + *(WCHAR*) (((char*) wbuf) |
| 2338 | + + b->SymbolicLinkReparseBuffer.SubstituteNameLength) = 0; |
| 2339 | + break; |
| 2340 | + case IO_REPARSE_TAG_MOUNT_POINT: |
| 2341 | + wbuf = (WCHAR*) (((char*) b->MountPointReparseBuffer.PathBuffer) |
| 2342 | + + b->MountPointReparseBuffer.SubstituteNameOffset); |
| 2343 | + *(WCHAR*) (((char*) wbuf) |
| 2344 | + + b->MountPointReparseBuffer.SubstituteNameLength) = 0; |
| 2345 | + break; |
| 2346 | + default: |
| 2347 | + errno = EINVAL; |
| 2348 | + return -1; |
| 2349 | + } |
| 2350 | + |
| 2351 | + /* |
| 2352 | + * Adapt to strange readlink() API: Copy up to bufsiz *bytes*, potentially |
| 2353 | + * cutting off a UTF-8 sequence. Insufficient bufsize is *not* a failure |
| 2354 | + * condition. There is no conversion function that produces invalid UTF-8, |
| 2355 | + * so convert to a (hopefully large enough) temporary buffer, then memcpy |
| 2356 | + * the requested number of bytes (including '\0' for robustness). |
| 2357 | + */ |
| 2358 | + if ((len = xwcstoutf(tmpbuf, normalize_ntpath(wbuf), MAX_LONG_PATH)) < 0) |
| 2359 | + return -1; |
| 2360 | + memcpy(buf, tmpbuf, min(bufsiz, len + 1)); |
| 2361 | + return min(bufsiz, len); |
| 2362 | +} |
| 2363 | + |
2266 | 2364 | pid_t waitpid(pid_t pid, int *status, int options)
|
2267 | 2365 | {
|
2268 | 2366 | HANDLE h = OpenProcess(SYNCHRONIZE | PROCESS_QUERY_INFORMATION,
|
|
0 commit comments