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