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