|
4 | 4 | #include <wchar.h>
|
5 | 5 | #include <aclapi.h>
|
6 | 6 | #include <sddl.h>
|
| 7 | +#include <winioctl.h> |
7 | 8 | #include "../strbuf.h"
|
8 | 9 | #include "../run-command.h"
|
9 | 10 | #include "../cache.h"
|
@@ -2226,6 +2227,103 @@ int link(const char *oldpath, const char *newpath)
|
2226 | 2227 | return 0;
|
2227 | 2228 | }
|
2228 | 2229 |
|
| 2230 | +#ifndef _WINNT_H |
| 2231 | +/* |
| 2232 | + * The REPARSE_DATA_BUFFER structure is defined in the Windows DDK (in |
| 2233 | + * ntifs.h) and in MSYS1's winnt.h (which defines _WINNT_H). So define |
| 2234 | + * it ourselves if we are on MSYS2 (whose winnt.h defines _WINNT_). |
| 2235 | + */ |
| 2236 | +typedef struct _REPARSE_DATA_BUFFER { |
| 2237 | + DWORD ReparseTag; |
| 2238 | + WORD ReparseDataLength; |
| 2239 | + WORD Reserved; |
| 2240 | +#ifndef _MSC_VER |
| 2241 | + _ANONYMOUS_UNION |
| 2242 | +#endif |
| 2243 | + union { |
| 2244 | + struct { |
| 2245 | + WORD SubstituteNameOffset; |
| 2246 | + WORD SubstituteNameLength; |
| 2247 | + WORD PrintNameOffset; |
| 2248 | + WORD PrintNameLength; |
| 2249 | + ULONG Flags; |
| 2250 | + WCHAR PathBuffer[1]; |
| 2251 | + } SymbolicLinkReparseBuffer; |
| 2252 | + struct { |
| 2253 | + WORD SubstituteNameOffset; |
| 2254 | + WORD SubstituteNameLength; |
| 2255 | + WORD PrintNameOffset; |
| 2256 | + WORD PrintNameLength; |
| 2257 | + WCHAR PathBuffer[1]; |
| 2258 | + } MountPointReparseBuffer; |
| 2259 | + struct { |
| 2260 | + BYTE DataBuffer[1]; |
| 2261 | + } GenericReparseBuffer; |
| 2262 | + } DUMMYUNIONNAME; |
| 2263 | +} REPARSE_DATA_BUFFER, *PREPARSE_DATA_BUFFER; |
| 2264 | +#endif |
| 2265 | + |
| 2266 | +int readlink(const char *path, char *buf, size_t bufsiz) |
| 2267 | +{ |
| 2268 | + HANDLE handle; |
| 2269 | + WCHAR wpath[MAX_LONG_PATH], *wbuf; |
| 2270 | + REPARSE_DATA_BUFFER *b = alloca(MAXIMUM_REPARSE_DATA_BUFFER_SIZE); |
| 2271 | + DWORD dummy; |
| 2272 | + char tmpbuf[MAX_LONG_PATH]; |
| 2273 | + int len; |
| 2274 | + |
| 2275 | + if (xutftowcs_long_path(wpath, path) < 0) |
| 2276 | + return -1; |
| 2277 | + |
| 2278 | + /* read reparse point data */ |
| 2279 | + handle = CreateFileW(wpath, 0, |
| 2280 | + FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL, |
| 2281 | + OPEN_EXISTING, |
| 2282 | + FILE_FLAG_BACKUP_SEMANTICS | FILE_FLAG_OPEN_REPARSE_POINT, NULL); |
| 2283 | + if (handle == INVALID_HANDLE_VALUE) { |
| 2284 | + errno = err_win_to_posix(GetLastError()); |
| 2285 | + return -1; |
| 2286 | + } |
| 2287 | + if (!DeviceIoControl(handle, FSCTL_GET_REPARSE_POINT, NULL, 0, b, |
| 2288 | + MAXIMUM_REPARSE_DATA_BUFFER_SIZE, &dummy, NULL)) { |
| 2289 | + errno = err_win_to_posix(GetLastError()); |
| 2290 | + CloseHandle(handle); |
| 2291 | + return -1; |
| 2292 | + } |
| 2293 | + CloseHandle(handle); |
| 2294 | + |
| 2295 | + /* get target path for symlinks or mount points (aka 'junctions') */ |
| 2296 | + switch (b->ReparseTag) { |
| 2297 | + case IO_REPARSE_TAG_SYMLINK: |
| 2298 | + wbuf = (WCHAR*) (((char*) b->SymbolicLinkReparseBuffer.PathBuffer) |
| 2299 | + + b->SymbolicLinkReparseBuffer.SubstituteNameOffset); |
| 2300 | + *(WCHAR*) (((char*) wbuf) |
| 2301 | + + b->SymbolicLinkReparseBuffer.SubstituteNameLength) = 0; |
| 2302 | + break; |
| 2303 | + case IO_REPARSE_TAG_MOUNT_POINT: |
| 2304 | + wbuf = (WCHAR*) (((char*) b->MountPointReparseBuffer.PathBuffer) |
| 2305 | + + b->MountPointReparseBuffer.SubstituteNameOffset); |
| 2306 | + *(WCHAR*) (((char*) wbuf) |
| 2307 | + + b->MountPointReparseBuffer.SubstituteNameLength) = 0; |
| 2308 | + break; |
| 2309 | + default: |
| 2310 | + errno = EINVAL; |
| 2311 | + return -1; |
| 2312 | + } |
| 2313 | + |
| 2314 | + /* |
| 2315 | + * Adapt to strange readlink() API: Copy up to bufsiz *bytes*, potentially |
| 2316 | + * cutting off a UTF-8 sequence. Insufficient bufsize is *not* a failure |
| 2317 | + * condition. There is no conversion function that produces invalid UTF-8, |
| 2318 | + * so convert to a (hopefully large enough) temporary buffer, then memcpy |
| 2319 | + * the requested number of bytes (including '\0' for robustness). |
| 2320 | + */ |
| 2321 | + if ((len = xwcstoutf(tmpbuf, normalize_ntpath(wbuf), MAX_LONG_PATH)) < 0) |
| 2322 | + return -1; |
| 2323 | + memcpy(buf, tmpbuf, min(bufsiz, len + 1)); |
| 2324 | + return min(bufsiz, len); |
| 2325 | +} |
| 2326 | + |
2229 | 2327 | pid_t waitpid(pid_t pid, int *status, int options)
|
2230 | 2328 | {
|
2231 | 2329 | HANDLE h = OpenProcess(SYNCHRONIZE | PROCESS_QUERY_INFORMATION,
|
|
0 commit comments