[370] | 1 | /* $Id: mscfakes.c 3219 2018-03-30 22:30:15Z bird $ */
|
---|
| 2 | /** @file
|
---|
| 3 | * Fake Unix stuff for MSC.
|
---|
[2019] | 4 | */
|
---|
| 5 |
|
---|
| 6 | /*
|
---|
[2756] | 7 | * Copyright (c) 2005-2015 knut st. osmundsen <bird-kBuild-spamx@anduin.net>
|
---|
[370] | 8 | *
|
---|
[2019] | 9 | * This file is part of kBuild.
|
---|
[370] | 10 | *
|
---|
[2019] | 11 | * kBuild is free software; you can redistribute it and/or modify
|
---|
[370] | 12 | * it under the terms of the GNU General Public License as published by
|
---|
[2019] | 13 | * the Free Software Foundation; either version 3 of the License, or
|
---|
[370] | 14 | * (at your option) any later version.
|
---|
| 15 | *
|
---|
[2019] | 16 | * kBuild is distributed in the hope that it will be useful,
|
---|
[370] | 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 19 | * GNU General Public License for more details.
|
---|
| 20 | *
|
---|
| 21 | * You should have received a copy of the GNU General Public License
|
---|
[2019] | 22 | * along with kBuild. If not, see <http://www.gnu.org/licenses/>
|
---|
[370] | 23 | *
|
---|
| 24 | */
|
---|
| 25 |
|
---|
[2019] | 26 | /*******************************************************************************
|
---|
| 27 | * Header Files *
|
---|
| 28 | *******************************************************************************/
|
---|
[2113] | 29 | #include "config.h"
|
---|
[2756] | 30 | #include <assert.h>
|
---|
[370] | 31 | #include <stdarg.h>
|
---|
| 32 | #include <stdio.h>
|
---|
| 33 | #include <stdlib.h>
|
---|
| 34 | #include <string.h>
|
---|
| 35 | #include <errno.h>
|
---|
| 36 | #include <io.h>
|
---|
| 37 | #include <fcntl.h>
|
---|
[1108] | 38 | #include <sys/stat.h>
|
---|
[3060] | 39 | #include <sys/timeb.h>
|
---|
[370] | 40 | #include "err.h"
|
---|
| 41 | #include "mscfakes.h"
|
---|
| 42 |
|
---|
[3060] | 43 | #include "nt/ntutimes.h"
|
---|
| 44 | #undef utimes
|
---|
| 45 | #undef lutimes
|
---|
| 46 |
|
---|
[3188] | 47 | #include "console.h"
|
---|
[3060] | 48 |
|
---|
[3117] | 49 |
|
---|
[3188] | 50 |
|
---|
[2756] | 51 | /*******************************************************************************
|
---|
| 52 | * Internal Functions *
|
---|
| 53 | *******************************************************************************/
|
---|
| 54 | static BOOL isPipeFd(int fd);
|
---|
[1108] | 55 |
|
---|
[2756] | 56 |
|
---|
[1710] | 57 | /**
|
---|
| 58 | * Makes corrections to a directory path that ends with a trailing slash.
|
---|
| 59 | *
|
---|
| 60 | * @returns temporary buffer to free.
|
---|
| 61 | * @param ppszPath The path pointer. This is updated when necessary.
|
---|
| 62 | * @param pfMustBeDir This is set if it must be a directory, otherwise it's cleared.
|
---|
| 63 | */
|
---|
| 64 | static char *
|
---|
| 65 | msc_fix_path(const char **ppszPath, int *pfMustBeDir)
|
---|
| 66 | {
|
---|
| 67 | const char *pszPath = *ppszPath;
|
---|
| 68 | const char *psz;
|
---|
| 69 | char *pszNew;
|
---|
| 70 | *pfMustBeDir = 0;
|
---|
| 71 |
|
---|
| 72 | /*
|
---|
| 73 | * Skip any compusory trailing slashes
|
---|
| 74 | */
|
---|
| 75 | if (pszPath[0] == '/' || pszPath[0] == '\\')
|
---|
| 76 | {
|
---|
| 77 | if ( (pszPath[1] == '/' || pszPath[1] == '\\')
|
---|
| 78 | && pszPath[2] != '/'
|
---|
| 79 | && pszPath[2] != '\\')
|
---|
| 80 | /* unc */
|
---|
| 81 | pszPath += 2;
|
---|
| 82 | else
|
---|
| 83 | /* root slash(es) */
|
---|
| 84 | pszPath++;
|
---|
| 85 | }
|
---|
| 86 | else if ( isalpha(pszPath[0])
|
---|
| 87 | && pszPath[1] == ':')
|
---|
| 88 | {
|
---|
| 89 | if (pszPath[2] == '/' || pszPath[2] == '\\')
|
---|
| 90 | /* drive w/ slash */
|
---|
| 91 | pszPath += 3;
|
---|
| 92 | else
|
---|
| 93 | /* drive relative path. */
|
---|
| 94 | pszPath += 2;
|
---|
| 95 | }
|
---|
| 96 | /* else: relative path, no skipping necessary. */
|
---|
| 97 |
|
---|
| 98 | /*
|
---|
| 99 | * Any trailing slashes to drop off?
|
---|
| 100 | */
|
---|
| 101 | psz = strchr(pszPath, '\0');
|
---|
| 102 | if (pszPath <= psz)
|
---|
| 103 | return NULL;
|
---|
| 104 | if ( psz[-1] != '/'
|
---|
| 105 | || psz[-1] != '\\')
|
---|
| 106 | return NULL;
|
---|
| 107 |
|
---|
| 108 | /* figure how many, make a copy and strip them off. */
|
---|
| 109 | while ( psz > pszPath
|
---|
| 110 | && ( psz[-1] == '/'
|
---|
| 111 | || psz[-1] == '\\'))
|
---|
| 112 | psz--;
|
---|
| 113 | pszNew = strdup(pszPath);
|
---|
| 114 | pszNew[psz - pszPath] = '\0';
|
---|
| 115 |
|
---|
| 116 | *pfMustBeDir = 1;
|
---|
| 117 | *ppszPath = pszNew; /* use this one */
|
---|
| 118 | return pszNew;
|
---|
| 119 | }
|
---|
| 120 |
|
---|
| 121 |
|
---|
[2702] | 122 | int
|
---|
[2733] | 123 | birdSetErrno(unsigned dwErr)
|
---|
[1710] | 124 | {
|
---|
| 125 | switch (dwErr)
|
---|
| 126 | {
|
---|
| 127 | default:
|
---|
| 128 | case ERROR_INVALID_FUNCTION: errno = EINVAL; break;
|
---|
| 129 | case ERROR_FILE_NOT_FOUND: errno = ENOENT; break;
|
---|
| 130 | case ERROR_PATH_NOT_FOUND: errno = ENOENT; break;
|
---|
| 131 | case ERROR_TOO_MANY_OPEN_FILES: errno = EMFILE; break;
|
---|
| 132 | case ERROR_ACCESS_DENIED: errno = EACCES; break;
|
---|
| 133 | case ERROR_INVALID_HANDLE: errno = EBADF; break;
|
---|
| 134 | case ERROR_ARENA_TRASHED: errno = ENOMEM; break;
|
---|
| 135 | case ERROR_NOT_ENOUGH_MEMORY: errno = ENOMEM; break;
|
---|
| 136 | case ERROR_INVALID_BLOCK: errno = ENOMEM; break;
|
---|
| 137 | case ERROR_BAD_ENVIRONMENT: errno = E2BIG; break;
|
---|
| 138 | case ERROR_BAD_FORMAT: errno = ENOEXEC; break;
|
---|
| 139 | case ERROR_INVALID_ACCESS: errno = EINVAL; break;
|
---|
| 140 | case ERROR_INVALID_DATA: errno = EINVAL; break;
|
---|
| 141 | case ERROR_INVALID_DRIVE: errno = ENOENT; break;
|
---|
| 142 | case ERROR_CURRENT_DIRECTORY: errno = EACCES; break;
|
---|
| 143 | case ERROR_NOT_SAME_DEVICE: errno = EXDEV; break;
|
---|
| 144 | case ERROR_NO_MORE_FILES: errno = ENOENT; break;
|
---|
| 145 | case ERROR_LOCK_VIOLATION: errno = EACCES; break;
|
---|
| 146 | case ERROR_BAD_NETPATH: errno = ENOENT; break;
|
---|
| 147 | case ERROR_NETWORK_ACCESS_DENIED: errno = EACCES; break;
|
---|
| 148 | case ERROR_BAD_NET_NAME: errno = ENOENT; break;
|
---|
| 149 | case ERROR_FILE_EXISTS: errno = EEXIST; break;
|
---|
| 150 | case ERROR_CANNOT_MAKE: errno = EACCES; break;
|
---|
| 151 | case ERROR_FAIL_I24: errno = EACCES; break;
|
---|
| 152 | case ERROR_INVALID_PARAMETER: errno = EINVAL; break;
|
---|
| 153 | case ERROR_NO_PROC_SLOTS: errno = EAGAIN; break;
|
---|
| 154 | case ERROR_DRIVE_LOCKED: errno = EACCES; break;
|
---|
| 155 | case ERROR_BROKEN_PIPE: errno = EPIPE; break;
|
---|
| 156 | case ERROR_DISK_FULL: errno = ENOSPC; break;
|
---|
| 157 | case ERROR_INVALID_TARGET_HANDLE: errno = EBADF; break;
|
---|
| 158 | case ERROR_WAIT_NO_CHILDREN: errno = ECHILD; break;
|
---|
| 159 | case ERROR_CHILD_NOT_COMPLETE: errno = ECHILD; break;
|
---|
| 160 | case ERROR_DIRECT_ACCESS_HANDLE: errno = EBADF; break;
|
---|
| 161 | case ERROR_NEGATIVE_SEEK: errno = EINVAL; break;
|
---|
| 162 | case ERROR_SEEK_ON_DEVICE: errno = EACCES; break;
|
---|
| 163 | case ERROR_DIR_NOT_EMPTY: errno = ENOTEMPTY; break;
|
---|
| 164 | case ERROR_NOT_LOCKED: errno = EACCES; break;
|
---|
| 165 | case ERROR_BAD_PATHNAME: errno = ENOENT; break;
|
---|
| 166 | case ERROR_MAX_THRDS_REACHED: errno = EAGAIN; break;
|
---|
| 167 | case ERROR_LOCK_FAILED: errno = EACCES; break;
|
---|
| 168 | case ERROR_ALREADY_EXISTS: errno = EEXIST; break;
|
---|
| 169 | case ERROR_FILENAME_EXCED_RANGE: errno = ENOENT; break;
|
---|
| 170 | case ERROR_NESTING_NOT_ALLOWED: errno = EAGAIN; break;
|
---|
[2476] | 171 | #ifdef EMLINK
|
---|
| 172 | case ERROR_TOO_MANY_LINKS: errno = EMLINK; break;
|
---|
| 173 | #endif
|
---|
[1710] | 174 | }
|
---|
| 175 |
|
---|
| 176 | return -1;
|
---|
| 177 | }
|
---|
| 178 |
|
---|
[370] | 179 | char *dirname(char *path)
|
---|
[775] | 180 | {
|
---|
[370] | 181 | /** @todo later */
|
---|
| 182 | return path;
|
---|
| 183 | }
|
---|
| 184 |
|
---|
| 185 |
|
---|
[1710] | 186 | int lchmod(const char *pszPath, mode_t mode)
|
---|
| 187 | {
|
---|
| 188 | int rc = 0;
|
---|
| 189 | int fMustBeDir;
|
---|
| 190 | char *pszPathFree = msc_fix_path(&pszPath, &fMustBeDir);
|
---|
| 191 |
|
---|
| 192 | /*
|
---|
| 193 | * Get the current attributes
|
---|
| 194 | */
|
---|
| 195 | DWORD fAttr = GetFileAttributes(pszPath);
|
---|
| 196 | if (fAttr == INVALID_FILE_ATTRIBUTES)
|
---|
[2702] | 197 | rc = birdSetErrno(GetLastError());
|
---|
[1710] | 198 | else if (fMustBeDir & !(fAttr & FILE_ATTRIBUTE_DIRECTORY))
|
---|
| 199 | {
|
---|
| 200 | errno = ENOTDIR;
|
---|
| 201 | rc = -1;
|
---|
| 202 | }
|
---|
| 203 | else
|
---|
| 204 | {
|
---|
| 205 | /*
|
---|
| 206 | * Modify the attributes and try set them.
|
---|
| 207 | */
|
---|
| 208 | if (mode & _S_IWRITE)
|
---|
| 209 | fAttr &= ~FILE_ATTRIBUTE_READONLY;
|
---|
| 210 | else
|
---|
| 211 | fAttr |= FILE_ATTRIBUTE_READONLY;
|
---|
| 212 | if (!SetFileAttributes(pszPath, fAttr))
|
---|
[2702] | 213 | rc = birdSetErrno(GetLastError());
|
---|
[1710] | 214 | }
|
---|
| 215 |
|
---|
| 216 | if (pszPathFree)
|
---|
| 217 | {
|
---|
| 218 | int saved_errno = errno;
|
---|
| 219 | free(pszPathFree);
|
---|
| 220 | errno = saved_errno;
|
---|
| 221 | }
|
---|
| 222 | return rc;
|
---|
| 223 | }
|
---|
| 224 |
|
---|
| 225 |
|
---|
| 226 | int msc_chmod(const char *pszPath, mode_t mode)
|
---|
| 227 | {
|
---|
| 228 | int rc = 0;
|
---|
| 229 | int fMustBeDir;
|
---|
| 230 | char *pszPathFree = msc_fix_path(&pszPath, &fMustBeDir);
|
---|
| 231 |
|
---|
| 232 | /*
|
---|
| 233 | * Get the current attributes.
|
---|
| 234 | */
|
---|
| 235 | DWORD fAttr = GetFileAttributes(pszPath);
|
---|
| 236 | if (fAttr == INVALID_FILE_ATTRIBUTES)
|
---|
[2702] | 237 | rc = birdSetErrno(GetLastError());
|
---|
[1710] | 238 | else if (fMustBeDir & !(fAttr & FILE_ATTRIBUTE_DIRECTORY))
|
---|
| 239 | {
|
---|
| 240 | errno = ENOTDIR;
|
---|
| 241 | rc = -1;
|
---|
| 242 | }
|
---|
| 243 | else if (fAttr & FILE_ATTRIBUTE_REPARSE_POINT)
|
---|
| 244 | {
|
---|
| 245 | errno = ENOSYS; /** @todo resolve symbolic link / rewrite to NtSetInformationFile. */
|
---|
| 246 | rc = -1;
|
---|
| 247 | }
|
---|
| 248 | else
|
---|
| 249 | {
|
---|
| 250 | /*
|
---|
| 251 | * Modify the attributes and try set them.
|
---|
| 252 | */
|
---|
| 253 | if (mode & _S_IWRITE)
|
---|
| 254 | fAttr &= ~FILE_ATTRIBUTE_READONLY;
|
---|
| 255 | else
|
---|
| 256 | fAttr |= FILE_ATTRIBUTE_READONLY;
|
---|
| 257 | if (!SetFileAttributes(pszPath, fAttr))
|
---|
[2702] | 258 | rc = birdSetErrno(GetLastError());
|
---|
[1710] | 259 | }
|
---|
| 260 |
|
---|
| 261 | if (pszPathFree)
|
---|
| 262 | {
|
---|
| 263 | int saved_errno = errno;
|
---|
| 264 | free(pszPathFree);
|
---|
| 265 | errno = saved_errno;
|
---|
| 266 | }
|
---|
| 267 | return rc;
|
---|
| 268 | }
|
---|
| 269 |
|
---|
| 270 |
|
---|
[2484] | 271 | typedef BOOL (WINAPI *PFNCREATEHARDLINKA)(LPCSTR, LPCSTR, LPSECURITY_ATTRIBUTES);
|
---|
[370] | 272 | int link(const char *pszDst, const char *pszLink)
|
---|
| 273 | {
|
---|
[2484] | 274 | static PFNCREATEHARDLINKA s_pfnCreateHardLinkA = NULL;
|
---|
| 275 | static int s_fTried = FALSE;
|
---|
| 276 |
|
---|
| 277 | /* The API was introduced in Windows 2000, so resolve it dynamically. */
|
---|
| 278 | if (!s_pfnCreateHardLinkA)
|
---|
| 279 | {
|
---|
| 280 | if (!s_fTried)
|
---|
| 281 | {
|
---|
| 282 | HMODULE hmod = LoadLibrary("KERNEL32.DLL");
|
---|
| 283 | if (hmod)
|
---|
| 284 | *(FARPROC *)&s_pfnCreateHardLinkA = GetProcAddress(hmod, "CreateHardLinkA");
|
---|
| 285 | s_fTried = TRUE;
|
---|
| 286 | }
|
---|
| 287 | if (!s_pfnCreateHardLinkA)
|
---|
| 288 | {
|
---|
| 289 | errno = ENOSYS;
|
---|
| 290 | return -1;
|
---|
| 291 | }
|
---|
| 292 | }
|
---|
| 293 |
|
---|
| 294 | if (s_pfnCreateHardLinkA(pszLink, pszDst, NULL))
|
---|
[2476] | 295 | return 0;
|
---|
[2702] | 296 | return birdSetErrno(GetLastError());
|
---|
[370] | 297 | }
|
---|
| 298 |
|
---|
| 299 |
|
---|
| 300 | int mkdir_msc(const char *path, mode_t mode)
|
---|
| 301 | {
|
---|
[936] | 302 | int rc = (mkdir)(path);
|
---|
[370] | 303 | if (rc)
|
---|
| 304 | {
|
---|
[1108] | 305 | size_t len = strlen(path);
|
---|
[370] | 306 | if (len > 0 && (path[len - 1] == '/' || path[len - 1] == '\\'))
|
---|
| 307 | {
|
---|
| 308 | char *str = strdup(path);
|
---|
| 309 | while (len > 0 && (str[len - 1] == '/' || str[len - 1] == '\\'))
|
---|
| 310 | str[--len] = '\0';
|
---|
[936] | 311 | rc = (mkdir)(str);
|
---|
[370] | 312 | free(str);
|
---|
| 313 | }
|
---|
| 314 | }
|
---|
| 315 | return rc;
|
---|
| 316 | }
|
---|
| 317 |
|
---|
[936] | 318 | int rmdir_msc(const char *path)
|
---|
| 319 | {
|
---|
| 320 | int rc = (rmdir)(path);
|
---|
| 321 | if (rc)
|
---|
| 322 | {
|
---|
[1108] | 323 | size_t len = strlen(path);
|
---|
[936] | 324 | if (len > 0 && (path[len - 1] == '/' || path[len - 1] == '\\'))
|
---|
| 325 | {
|
---|
| 326 | char *str = strdup(path);
|
---|
| 327 | while (len > 0 && (str[len - 1] == '/' || str[len - 1] == '\\'))
|
---|
| 328 | str[--len] = '\0';
|
---|
| 329 | rc = (rmdir)(str);
|
---|
| 330 | free(str);
|
---|
| 331 | }
|
---|
| 332 | }
|
---|
| 333 | return rc;
|
---|
| 334 | }
|
---|
[370] | 335 |
|
---|
[936] | 336 |
|
---|
[370] | 337 | static int doname(char *pszX, char *pszEnd)
|
---|
| 338 | {
|
---|
| 339 | static char s_szChars[] = "Xabcdefghijklmnopqrstuwvxyz1234567890";
|
---|
| 340 | int rc = 0;
|
---|
| 341 | do
|
---|
| 342 | {
|
---|
| 343 | char ch;
|
---|
| 344 |
|
---|
| 345 | pszEnd++;
|
---|
| 346 | ch = *(strchr(s_szChars, *pszEnd) + 1);
|
---|
| 347 | if (ch)
|
---|
| 348 | {
|
---|
| 349 | *pszEnd = ch;
|
---|
| 350 | return 0;
|
---|
| 351 | }
|
---|
| 352 | *pszEnd = 'a';
|
---|
| 353 | } while (pszEnd != pszX);
|
---|
| 354 | return 1;
|
---|
| 355 | }
|
---|
| 356 |
|
---|
| 357 |
|
---|
| 358 | int mkstemp(char *temp)
|
---|
| 359 | {
|
---|
| 360 | char *pszX = strchr(temp, 'X');
|
---|
| 361 | char *pszEnd = strchr(pszX, '\0');
|
---|
| 362 | int cTries = 1000;
|
---|
| 363 | while (--cTries > 0)
|
---|
| 364 | {
|
---|
| 365 | int fd;
|
---|
| 366 | if (doname(pszX, pszEnd))
|
---|
| 367 | return -1;
|
---|
[3219] | 368 | fd = open(temp, _O_EXCL | _O_CREAT | _O_BINARY | _O_RDWR | KMK_OPEN_NO_INHERIT, 0777);
|
---|
[370] | 369 | if (fd >= 0)
|
---|
| 370 | return fd;
|
---|
| 371 | }
|
---|
| 372 | return -1;
|
---|
| 373 | }
|
---|
| 374 |
|
---|
| 375 |
|
---|
[1108] | 376 | /** Unix to DOS. */
|
---|
| 377 | static char *fix_slashes(char *psz)
|
---|
| 378 | {
|
---|
| 379 | char *pszRet = psz;
|
---|
| 380 | for (; *psz; psz++)
|
---|
| 381 | if (*psz == '/')
|
---|
| 382 | *psz = '\\';
|
---|
| 383 | return pszRet;
|
---|
| 384 | }
|
---|
| 385 |
|
---|
| 386 |
|
---|
| 387 | /** Calcs the SYMBOLIC_LINK_FLAG_DIRECTORY flag for CreatesymbolcLink. */
|
---|
| 388 | static DWORD is_directory(const char *pszPath, const char *pszRelativeTo)
|
---|
| 389 | {
|
---|
| 390 | size_t cchPath = strlen(pszPath);
|
---|
| 391 | struct stat st;
|
---|
| 392 | if (cchPath > 0 && pszPath[cchPath - 1] == '\\' || pszPath[cchPath - 1] == '/')
|
---|
| 393 | return 1; /* SYMBOLIC_LINK_FLAG_DIRECTORY */
|
---|
| 394 |
|
---|
| 395 | if (stat(pszPath, &st))
|
---|
| 396 | {
|
---|
| 397 | size_t cchRelativeTo = strlen(pszRelativeTo);
|
---|
| 398 | char *psz = malloc(cchPath + cchRelativeTo + 4);
|
---|
| 399 | memcpy(psz, pszRelativeTo, cchRelativeTo);
|
---|
| 400 | memcpy(psz + cchRelativeTo, "\\", 1);
|
---|
| 401 | memcpy(psz + cchRelativeTo + 1, pszPath, cchPath + 1);
|
---|
| 402 | if (stat(pszPath, &st))
|
---|
| 403 | st.st_mode = _S_IFREG;
|
---|
| 404 | free(psz);
|
---|
| 405 | }
|
---|
| 406 |
|
---|
| 407 | return (st.st_mode & _S_IFMT) == _S_IFDIR ? 1 : 0;
|
---|
| 408 | }
|
---|
| 409 |
|
---|
| 410 |
|
---|
[370] | 411 | int symlink(const char *pszDst, const char *pszLink)
|
---|
| 412 | {
|
---|
[1717] | 413 | static BOOLEAN (WINAPI *s_pfnCreateSymbolicLinkA)(LPCSTR, LPCSTR, DWORD) = 0;
|
---|
[1108] | 414 | static BOOL s_fTried = FALSE;
|
---|
| 415 |
|
---|
| 416 | if (!s_fTried)
|
---|
| 417 | {
|
---|
| 418 | HMODULE hmod = LoadLibrary("KERNEL32.DLL");
|
---|
| 419 | if (hmod)
|
---|
| 420 | *(FARPROC *)&s_pfnCreateSymbolicLinkA = GetProcAddress(hmod, "CreateSymbolicLinkA");
|
---|
| 421 | s_fTried = TRUE;
|
---|
| 422 | }
|
---|
| 423 |
|
---|
| 424 | if (s_pfnCreateSymbolicLinkA)
|
---|
| 425 | {
|
---|
| 426 | char *pszDstCopy = fix_slashes(strdup(pszDst));
|
---|
| 427 | char *pszLinkCopy = fix_slashes(strdup(pszLink));
|
---|
[1717] | 428 | BOOLEAN fRc = s_pfnCreateSymbolicLinkA(pszLinkCopy, pszDstCopy,
|
---|
| 429 | is_directory(pszDstCopy, pszLinkCopy));
|
---|
[1108] | 430 | DWORD err = GetLastError();
|
---|
| 431 | free(pszDstCopy);
|
---|
| 432 | free(pszLinkCopy);
|
---|
| 433 | if (fRc)
|
---|
| 434 | return 0;
|
---|
| 435 | switch (err)
|
---|
| 436 | {
|
---|
| 437 | case ERROR_NOT_SUPPORTED: errno = ENOSYS; break;
|
---|
| 438 | case ERROR_ALREADY_EXISTS:
|
---|
| 439 | case ERROR_FILE_EXISTS: errno = EEXIST; break;
|
---|
| 440 | case ERROR_DIRECTORY: errno = ENOTDIR; break;
|
---|
| 441 | case ERROR_ACCESS_DENIED:
|
---|
| 442 | case ERROR_PRIVILEGE_NOT_HELD: errno = EPERM; break;
|
---|
| 443 | default: errno = EINVAL; break;
|
---|
| 444 | }
|
---|
| 445 | return -1;
|
---|
| 446 | }
|
---|
| 447 |
|
---|
[3192] | 448 | fprintf(stderr, "warning: symlink() is available on this version of Windows!\n");
|
---|
[370] | 449 | errno = ENOSYS;
|
---|
| 450 | return -1;
|
---|
| 451 | }
|
---|
| 452 |
|
---|
| 453 |
|
---|
| 454 | #if _MSC_VER < 1400
|
---|
| 455 | int snprintf(char *buf, size_t size, const char *fmt, ...)
|
---|
| 456 | {
|
---|
| 457 | int cch;
|
---|
| 458 | va_list args;
|
---|
| 459 | va_start(args, fmt);
|
---|
| 460 | cch = vsprintf(buf, fmt, args);
|
---|
| 461 | va_end(args);
|
---|
| 462 | return cch;
|
---|
| 463 | }
|
---|
[775] | 464 | #endif
|
---|
[370] | 465 |
|
---|
| 466 |
|
---|
[2759] | 467 | /* We override the libc write function (in our modules only, unfortunately) so
|
---|
| 468 | we can kludge our way around a ENOSPC problem observed on build servers
|
---|
| 469 | capturing STDOUT and STDERR via pipes. Apparently this may happen when the
|
---|
| 470 | pipe buffer is full, even with the mscfake_init hack in place.
|
---|
| 471 |
|
---|
| 472 | XXX: Probably need to hook into fwrite as well. */
|
---|
| 473 | ssize_t msc_write(int fd, const void *pvSrc, size_t cbSrc)
|
---|
[370] | 474 | {
|
---|
[3117] | 475 | #define MSC_WRITE_MAX_CHUNK (UINT_MAX / 32)
|
---|
[2759] | 476 | ssize_t cbRet;
|
---|
[3117] | 477 | if (cbSrc <= MSC_WRITE_MAX_CHUNK)
|
---|
[370] | 478 | {
|
---|
[3117] | 479 | /* Console output optimization: */
|
---|
[3188] | 480 | if (cbSrc > 0 && is_console(fd))
|
---|
[3117] | 481 | return maybe_con_write(fd, pvSrc, cbSrc);
|
---|
| 482 |
|
---|
[2759] | 483 | #ifndef MSC_WRITE_TEST
|
---|
| 484 | cbRet = _write(fd, pvSrc, (unsigned int)cbSrc);
|
---|
| 485 | #else
|
---|
| 486 | cbRet = -1; errno = ENOSPC;
|
---|
| 487 | #endif
|
---|
| 488 | if (cbRet < 0)
|
---|
[2756] | 489 | {
|
---|
| 490 | /* ENOSPC on pipe kludge. */
|
---|
[3117] | 491 | unsigned int cbLimit;
|
---|
[2759] | 492 | int cSinceLastSuccess;
|
---|
[2756] | 493 |
|
---|
[2759] | 494 | if (cbSrc == 0)
|
---|
| 495 | return 0;
|
---|
[2756] | 496 | if (errno != ENOSPC)
|
---|
| 497 | return -1;
|
---|
[2759] | 498 | #ifndef MSC_WRITE_TEST
|
---|
[2756] | 499 | if (!isPipeFd(fd))
|
---|
| 500 | {
|
---|
| 501 | errno = ENOSPC;
|
---|
| 502 | return -1;
|
---|
| 503 | }
|
---|
[2759] | 504 | #endif
|
---|
[2756] | 505 |
|
---|
| 506 | /* Likely a full pipe buffer, try write smaller amounts and do some
|
---|
| 507 | sleeping inbetween each unsuccessful one. */
|
---|
[3117] | 508 | cbLimit = (unsigned)(cbSrc / 4);
|
---|
[2756] | 509 | if (cbLimit < 4)
|
---|
| 510 | cbLimit = 4;
|
---|
| 511 | else if (cbLimit > 512)
|
---|
| 512 | cbLimit = 512;
|
---|
| 513 | cSinceLastSuccess = 0;
|
---|
[2759] | 514 | cbRet = 0;
|
---|
| 515 | #ifdef MSC_WRITE_TEST
|
---|
| 516 | cbLimit = 4;
|
---|
| 517 | #endif
|
---|
[2756] | 518 |
|
---|
[3117] | 519 | while ((ssize_t)cbSrc > 0)
|
---|
[2756] | 520 | {
|
---|
[3117] | 521 | unsigned int cbAttempt = cbSrc > cbLimit ? cbLimit : (unsigned int)cbSrc;
|
---|
[2759] | 522 | ssize_t cbActual = _write(fd, pvSrc, cbAttempt);
|
---|
| 523 | if (cbActual > 0)
|
---|
[2756] | 524 | {
|
---|
[3117] | 525 | /* For some reason, it seems like we cannot trust _write to return
|
---|
| 526 | a number that's less or equal to the number of bytes we passed
|
---|
| 527 | in to the call. (Also reason for signed check in loop.) */
|
---|
| 528 | if (cbActual > cbAttempt)
|
---|
| 529 | cbActual = cbAttempt;
|
---|
| 530 |
|
---|
[2759] | 531 | pvSrc = (char *)pvSrc + cbActual;
|
---|
| 532 | cbSrc -= cbActual;
|
---|
| 533 | cbRet += cbActual;
|
---|
| 534 | #ifndef MSC_WRITE_TEST
|
---|
[2756] | 535 | if (cbLimit < 32)
|
---|
| 536 | cbLimit = 32;
|
---|
[2759] | 537 | #endif
|
---|
[2756] | 538 | cSinceLastSuccess = 0;
|
---|
| 539 | }
|
---|
| 540 | else if (errno != ENOSPC)
|
---|
| 541 | return -1;
|
---|
| 542 | else
|
---|
| 543 | {
|
---|
| 544 | /* Delay for about 30 seconds, then just give up. */
|
---|
| 545 | cSinceLastSuccess++;
|
---|
| 546 | if (cSinceLastSuccess > 1860)
|
---|
| 547 | return -1;
|
---|
| 548 | if (cSinceLastSuccess <= 2)
|
---|
| 549 | Sleep(0);
|
---|
| 550 | else if (cSinceLastSuccess <= 66)
|
---|
| 551 | {
|
---|
| 552 | if (cbLimit >= 8)
|
---|
| 553 | cbLimit /= 2; /* Just in case the pipe buffer is very very small. */
|
---|
| 554 | Sleep(1);
|
---|
| 555 | }
|
---|
| 556 | else
|
---|
| 557 | Sleep(16);
|
---|
| 558 | }
|
---|
| 559 | }
|
---|
| 560 | }
|
---|
[2759] | 561 | }
|
---|
| 562 | else
|
---|
| 563 | {
|
---|
| 564 | /*
|
---|
| 565 | * Type limit exceeded. Split the job up.
|
---|
| 566 | */
|
---|
| 567 | cbRet = 0;
|
---|
| 568 | while (cbSrc > 0)
|
---|
| 569 | {
|
---|
[3117] | 570 | size_t cbToWrite = cbSrc > MSC_WRITE_MAX_CHUNK ? MSC_WRITE_MAX_CHUNK : cbSrc;
|
---|
[2759] | 571 | ssize_t cbWritten = msc_write(fd, pvSrc, cbToWrite);
|
---|
| 572 | if (cbWritten > 0)
|
---|
| 573 | {
|
---|
| 574 | pvSrc = (char *)pvSrc + (size_t)cbWritten;
|
---|
| 575 | cbSrc -= (size_t)cbWritten;
|
---|
| 576 | cbRet += (size_t)cbWritten;
|
---|
| 577 | }
|
---|
| 578 | else if (cbWritten == 0 || cbRet > 0)
|
---|
| 579 | break;
|
---|
| 580 | else
|
---|
| 581 | return -1;
|
---|
| 582 | }
|
---|
| 583 | }
|
---|
| 584 | return cbRet;
|
---|
| 585 | }
|
---|
| 586 |
|
---|
| 587 | ssize_t writev(int fd, const struct iovec *vector, int count)
|
---|
| 588 | {
|
---|
[3188] | 589 | ssize_t size = 0;
|
---|
| 590 | if (count > 0)
|
---|
[2759] | 591 | {
|
---|
[3188] | 592 | int i;
|
---|
| 593 |
|
---|
| 594 | /* To get consistent console output, we must try combine the segments
|
---|
| 595 | when outputing to the console. */
|
---|
| 596 | if (count > 1 && is_console(fd))
|
---|
| 597 | {
|
---|
| 598 | char *pbTmp;
|
---|
| 599 | ssize_t cbTotal;
|
---|
| 600 | if (count == 1)
|
---|
| 601 | return maybe_con_write(fd, vector[0].iov_base, (int)vector[0].iov_len);
|
---|
| 602 |
|
---|
| 603 | cbTotal = 0;
|
---|
| 604 | for (i = 0; i < count; i++)
|
---|
| 605 | cbTotal += vector[i].iov_len;
|
---|
| 606 | pbTmp = malloc(cbTotal);
|
---|
| 607 | if (pbTmp)
|
---|
| 608 | {
|
---|
| 609 | char *pbCur = pbTmp;
|
---|
| 610 | for (i = 0; i < count; i++)
|
---|
| 611 | {
|
---|
| 612 | memcpy(pbCur, vector[i].iov_base, vector[i].iov_len);
|
---|
| 613 | pbCur += vector[i].iov_len;
|
---|
| 614 | }
|
---|
| 615 | size = maybe_con_write(fd, pbTmp, cbTotal);
|
---|
| 616 | free(pbTmp);
|
---|
| 617 | return size;
|
---|
| 618 | }
|
---|
| 619 |
|
---|
| 620 | /* fall back on segment by segment output. */
|
---|
| 621 | }
|
---|
| 622 |
|
---|
| 623 | for (i = 0; i < count; i++)
|
---|
| 624 | {
|
---|
| 625 | int cb = msc_write(fd, vector[i].iov_base, (int)vector[i].iov_len);
|
---|
| 626 | if (cb < 0)
|
---|
| 627 | return cb;
|
---|
| 628 | size += cb;
|
---|
| 629 | }
|
---|
[370] | 630 | }
|
---|
| 631 | return size;
|
---|
| 632 | }
|
---|
| 633 |
|
---|
[775] | 634 |
|
---|
| 635 | intmax_t strtoimax(const char *nptr, char **endptr, int base)
|
---|
| 636 | {
|
---|
[2645] | 637 | if (*nptr != '-')
|
---|
| 638 | return _strtoui64(nptr, endptr, base);
|
---|
| 639 | return -(intmax_t)_strtoui64(nptr + 1, endptr, base);
|
---|
[775] | 640 | }
|
---|
| 641 |
|
---|
| 642 |
|
---|
| 643 | uintmax_t strtoumax(const char *nptr, char **endptr, int base)
|
---|
| 644 | {
|
---|
[2645] | 645 | return _strtoui64(nptr, endptr, base);
|
---|
[775] | 646 | }
|
---|
| 647 |
|
---|
| 648 |
|
---|
| 649 | int asprintf(char **strp, const char *fmt, ...)
|
---|
| 650 | {
|
---|
| 651 | int rc;
|
---|
| 652 | va_list va;
|
---|
| 653 | va_start(va, fmt);
|
---|
| 654 | rc = vasprintf(strp, fmt, va);
|
---|
| 655 | va_end(va);
|
---|
| 656 | return rc;
|
---|
| 657 | }
|
---|
| 658 |
|
---|
| 659 |
|
---|
| 660 | int vasprintf(char **strp, const char *fmt, va_list va)
|
---|
| 661 | {
|
---|
| 662 | int rc;
|
---|
| 663 | char *psz;
|
---|
| 664 | size_t cb = 1024;
|
---|
| 665 |
|
---|
| 666 | *strp = NULL;
|
---|
| 667 | for (;;)
|
---|
| 668 | {
|
---|
| 669 | va_list va2;
|
---|
| 670 |
|
---|
| 671 | psz = malloc(cb);
|
---|
| 672 | if (!psz)
|
---|
| 673 | return -1;
|
---|
| 674 |
|
---|
| 675 | #ifdef va_copy
|
---|
| 676 | va_copy(va2, va);
|
---|
[2901] | 677 | rc = vsnprintf(psz, cb, fmt, va2);
|
---|
[775] | 678 | va_end(vaCopy);
|
---|
| 679 | #else
|
---|
| 680 | va2 = va;
|
---|
[2901] | 681 | rc = vsnprintf(psz, cb, fmt, va2);
|
---|
[775] | 682 | #endif
|
---|
| 683 | if (rc < 0 || (size_t)rc < cb)
|
---|
| 684 | break;
|
---|
| 685 | cb *= 2;
|
---|
| 686 | free(psz);
|
---|
| 687 | }
|
---|
| 688 |
|
---|
| 689 | *strp = psz;
|
---|
| 690 | return rc;
|
---|
| 691 | }
|
---|
| 692 |
|
---|
[1321] | 693 |
|
---|
[3140] | 694 | int utimes(const char *pszPath, const struct msc_timeval *paTimes)
|
---|
[3060] | 695 | {
|
---|
[3094] | 696 | if (paTimes)
|
---|
| 697 | {
|
---|
| 698 | BirdTimeVal_T aTimes[2];
|
---|
| 699 | aTimes[0].tv_sec = paTimes[0].tv_sec;
|
---|
| 700 | aTimes[0].tv_usec = paTimes[0].tv_usec;
|
---|
| 701 | aTimes[1].tv_sec = paTimes[1].tv_sec;
|
---|
| 702 | aTimes[1].tv_usec = paTimes[1].tv_usec;
|
---|
| 703 | return birdUtimes(pszPath, aTimes);
|
---|
| 704 | }
|
---|
| 705 | return birdUtimes(pszPath, NULL);
|
---|
[3060] | 706 | }
|
---|
| 707 |
|
---|
| 708 |
|
---|
[3140] | 709 | int lutimes(const char *pszPath, const struct msc_timeval *paTimes)
|
---|
[3060] | 710 | {
|
---|
[3094] | 711 | if (paTimes)
|
---|
| 712 | {
|
---|
| 713 | BirdTimeVal_T aTimes[2];
|
---|
| 714 | aTimes[0].tv_sec = paTimes[0].tv_sec;
|
---|
| 715 | aTimes[0].tv_usec = paTimes[0].tv_usec;
|
---|
| 716 | aTimes[1].tv_sec = paTimes[1].tv_sec;
|
---|
| 717 | aTimes[1].tv_usec = paTimes[1].tv_usec;
|
---|
| 718 | return birdUtimes(pszPath, aTimes);
|
---|
| 719 | }
|
---|
| 720 | return birdUtimes(pszPath, NULL);
|
---|
[3060] | 721 | }
|
---|
| 722 |
|
---|
| 723 |
|
---|
[3140] | 724 | int gettimeofday(struct msc_timeval *pNow, void *pvIgnored)
|
---|
[3060] | 725 | {
|
---|
| 726 | struct __timeb64 Now;
|
---|
| 727 | int rc = _ftime64_s(&Now);
|
---|
| 728 | if (rc == 0)
|
---|
| 729 | {
|
---|
| 730 | pNow->tv_sec = Now.time;
|
---|
| 731 | pNow->tv_usec = Now.millitm * 1000;
|
---|
| 732 | return 0;
|
---|
| 733 | }
|
---|
| 734 | errno = rc;
|
---|
| 735 | return -1;
|
---|
| 736 | }
|
---|
| 737 |
|
---|
| 738 |
|
---|
| 739 | struct tm *localtime_r(const __time64_t *pNow, struct tm *pResult)
|
---|
| 740 | {
|
---|
| 741 | int rc = _localtime64_s(pResult, pNow);
|
---|
| 742 | if (rc == 0)
|
---|
| 743 | return pResult;
|
---|
| 744 | errno = rc;
|
---|
| 745 | return NULL;
|
---|
| 746 | }
|
---|
| 747 |
|
---|
| 748 |
|
---|
| 749 | __time64_t timegm(struct tm *pNow)
|
---|
| 750 | {
|
---|
| 751 | return _mkgmtime64(pNow);
|
---|
| 752 | }
|
---|
| 753 |
|
---|
| 754 |
|
---|
[2733] | 755 | /**
|
---|
[2756] | 756 | * Checks if the given file descriptor is a pipe or not.
|
---|
[2733] | 757 | *
|
---|
[2756] | 758 | * @returns TRUE if pipe, FALSE if not.
|
---|
[2733] | 759 | * @param fd The libc file descriptor number.
|
---|
| 760 | */
|
---|
[2756] | 761 | static BOOL isPipeFd(int fd)
|
---|
[2733] | 762 | {
|
---|
| 763 | /* Is pipe? */
|
---|
| 764 | HANDLE hFile = (HANDLE)_get_osfhandle(fd);
|
---|
| 765 | if (hFile != INVALID_HANDLE_VALUE)
|
---|
| 766 | {
|
---|
| 767 | DWORD fType = GetFileType(hFile);
|
---|
| 768 | fType &= ~FILE_TYPE_REMOTE;
|
---|
| 769 | if (fType == FILE_TYPE_PIPE)
|
---|
[2756] | 770 | return TRUE;
|
---|
| 771 | }
|
---|
| 772 | return FALSE;
|
---|
| 773 | }
|
---|
| 774 |
|
---|
| 775 |
|
---|
| 776 | /**
|
---|
| 777 | * This is a kludge to make pipe handles blocking.
|
---|
| 778 | *
|
---|
| 779 | * @returns TRUE if it's now blocking, FALSE if not a pipe or we failed to fix
|
---|
| 780 | * the blocking mode.
|
---|
| 781 | * @param fd The libc file descriptor number.
|
---|
| 782 | */
|
---|
| 783 | static BOOL makePipeBlocking(int fd)
|
---|
| 784 | {
|
---|
| 785 | if (isPipeFd(fd))
|
---|
| 786 | {
|
---|
| 787 | /* Try fix it. */
|
---|
| 788 | HANDLE hFile = (HANDLE)_get_osfhandle(fd);
|
---|
| 789 | DWORD fState = 0;
|
---|
| 790 | if (GetNamedPipeHandleState(hFile, &fState, NULL, NULL, NULL, NULL, 0))
|
---|
[2733] | 791 | {
|
---|
[2756] | 792 | fState &= ~PIPE_NOWAIT;
|
---|
| 793 | fState |= PIPE_WAIT;
|
---|
| 794 | if (SetNamedPipeHandleState(hFile, &fState, NULL, NULL))
|
---|
| 795 | return TRUE;
|
---|
[2733] | 796 | }
|
---|
| 797 | }
|
---|
| 798 | return FALSE;
|
---|
| 799 | }
|
---|
| 800 |
|
---|
| 801 |
|
---|
| 802 | /**
|
---|
| 803 | * Initializes the msc fake stuff.
|
---|
| 804 | * @returns 0 on success (non-zero would indicate failure, see rterr.h).
|
---|
| 805 | */
|
---|
| 806 | int mscfake_init(void)
|
---|
| 807 | {
|
---|
| 808 | /*
|
---|
| 809 | * Kludge against _write returning ENOSPC on non-blocking pipes.
|
---|
| 810 | */
|
---|
| 811 | makePipeBlocking(STDOUT_FILENO);
|
---|
| 812 | makePipeBlocking(STDERR_FILENO);
|
---|
| 813 |
|
---|
| 814 | return 0;
|
---|
| 815 | }
|
---|
| 816 |
|
---|
| 817 | /*
|
---|
| 818 | * Do this before main is called.
|
---|
| 819 | */
|
---|
| 820 | #pragma section(".CRT$XIA", read)
|
---|
| 821 | #pragma section(".CRT$XIU", read)
|
---|
| 822 | #pragma section(".CRT$XIZ", read)
|
---|
| 823 | typedef int (__cdecl *PFNCRTINIT)(void);
|
---|
| 824 | static __declspec(allocate(".CRT$XIU")) PFNCRTINIT g_MscFakeInitVectorEntry = mscfake_init;
|
---|
| 825 |
|
---|