source: trunk/src/kmk/kmkbuiltin/mscfakes.c@ 2476

Last change on this file since 2476 was 2476, checked in by bird, 14 years ago

kmk_install: Added --hard-link-files-when-possible for use with the staging.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 14.5 KB
Line 
1/* $Id: mscfakes.c 2476 2011-07-20 00:52:30Z bird $ */
2/** @file
3 * Fake Unix stuff for MSC.
4 */
5
6/*
7 * Copyright (c) 2005-2010 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 <stdarg.h>
31#include <stdio.h>
32#include <stdlib.h>
33#include <string.h>
34#include <errno.h>
35#include <io.h>
36#include <fcntl.h>
37#include <sys/stat.h>
38#include "err.h"
39#include "mscfakes.h"
40
41#define timeval windows_timeval
42#include <Windows.h>
43#undef timeval
44
45
46/**
47 * Makes corrections to a directory path that ends with a trailing slash.
48 *
49 * @returns temporary buffer to free.
50 * @param ppszPath The path pointer. This is updated when necessary.
51 * @param pfMustBeDir This is set if it must be a directory, otherwise it's cleared.
52 */
53static char *
54msc_fix_path(const char **ppszPath, int *pfMustBeDir)
55{
56 const char *pszPath = *ppszPath;
57 const char *psz;
58 char *pszNew;
59 *pfMustBeDir = 0;
60
61 /*
62 * Skip any compusory trailing slashes
63 */
64 if (pszPath[0] == '/' || pszPath[0] == '\\')
65 {
66 if ( (pszPath[1] == '/' || pszPath[1] == '\\')
67 && pszPath[2] != '/'
68 && pszPath[2] != '\\')
69 /* unc */
70 pszPath += 2;
71 else
72 /* root slash(es) */
73 pszPath++;
74 }
75 else if ( isalpha(pszPath[0])
76 && pszPath[1] == ':')
77 {
78 if (pszPath[2] == '/' || pszPath[2] == '\\')
79 /* drive w/ slash */
80 pszPath += 3;
81 else
82 /* drive relative path. */
83 pszPath += 2;
84 }
85 /* else: relative path, no skipping necessary. */
86
87 /*
88 * Any trailing slashes to drop off?
89 */
90 psz = strchr(pszPath, '\0');
91 if (pszPath <= psz)
92 return NULL;
93 if ( psz[-1] != '/'
94 || psz[-1] != '\\')
95 return NULL;
96
97 /* figure how many, make a copy and strip them off. */
98 while ( psz > pszPath
99 && ( psz[-1] == '/'
100 || psz[-1] == '\\'))
101 psz--;
102 pszNew = strdup(pszPath);
103 pszNew[psz - pszPath] = '\0';
104
105 *pfMustBeDir = 1;
106 *ppszPath = pszNew; /* use this one */
107 return pszNew;
108}
109
110
111static int
112msc_set_errno(DWORD dwErr)
113{
114 switch (dwErr)
115 {
116 default:
117 case ERROR_INVALID_FUNCTION: errno = EINVAL; break;
118 case ERROR_FILE_NOT_FOUND: errno = ENOENT; break;
119 case ERROR_PATH_NOT_FOUND: errno = ENOENT; break;
120 case ERROR_TOO_MANY_OPEN_FILES: errno = EMFILE; break;
121 case ERROR_ACCESS_DENIED: errno = EACCES; break;
122 case ERROR_INVALID_HANDLE: errno = EBADF; break;
123 case ERROR_ARENA_TRASHED: errno = ENOMEM; break;
124 case ERROR_NOT_ENOUGH_MEMORY: errno = ENOMEM; break;
125 case ERROR_INVALID_BLOCK: errno = ENOMEM; break;
126 case ERROR_BAD_ENVIRONMENT: errno = E2BIG; break;
127 case ERROR_BAD_FORMAT: errno = ENOEXEC; break;
128 case ERROR_INVALID_ACCESS: errno = EINVAL; break;
129 case ERROR_INVALID_DATA: errno = EINVAL; break;
130 case ERROR_INVALID_DRIVE: errno = ENOENT; break;
131 case ERROR_CURRENT_DIRECTORY: errno = EACCES; break;
132 case ERROR_NOT_SAME_DEVICE: errno = EXDEV; break;
133 case ERROR_NO_MORE_FILES: errno = ENOENT; break;
134 case ERROR_LOCK_VIOLATION: errno = EACCES; break;
135 case ERROR_BAD_NETPATH: errno = ENOENT; break;
136 case ERROR_NETWORK_ACCESS_DENIED: errno = EACCES; break;
137 case ERROR_BAD_NET_NAME: errno = ENOENT; break;
138 case ERROR_FILE_EXISTS: errno = EEXIST; break;
139 case ERROR_CANNOT_MAKE: errno = EACCES; break;
140 case ERROR_FAIL_I24: errno = EACCES; break;
141 case ERROR_INVALID_PARAMETER: errno = EINVAL; break;
142 case ERROR_NO_PROC_SLOTS: errno = EAGAIN; break;
143 case ERROR_DRIVE_LOCKED: errno = EACCES; break;
144 case ERROR_BROKEN_PIPE: errno = EPIPE; break;
145 case ERROR_DISK_FULL: errno = ENOSPC; break;
146 case ERROR_INVALID_TARGET_HANDLE: errno = EBADF; break;
147 case ERROR_WAIT_NO_CHILDREN: errno = ECHILD; break;
148 case ERROR_CHILD_NOT_COMPLETE: errno = ECHILD; break;
149 case ERROR_DIRECT_ACCESS_HANDLE: errno = EBADF; break;
150 case ERROR_NEGATIVE_SEEK: errno = EINVAL; break;
151 case ERROR_SEEK_ON_DEVICE: errno = EACCES; break;
152 case ERROR_DIR_NOT_EMPTY: errno = ENOTEMPTY; break;
153 case ERROR_NOT_LOCKED: errno = EACCES; break;
154 case ERROR_BAD_PATHNAME: errno = ENOENT; break;
155 case ERROR_MAX_THRDS_REACHED: errno = EAGAIN; break;
156 case ERROR_LOCK_FAILED: errno = EACCES; break;
157 case ERROR_ALREADY_EXISTS: errno = EEXIST; break;
158 case ERROR_FILENAME_EXCED_RANGE: errno = ENOENT; break;
159 case ERROR_NESTING_NOT_ALLOWED: errno = EAGAIN; break;
160#ifdef EMLINK
161 case ERROR_TOO_MANY_LINKS: errno = EMLINK; break;
162#endif
163 }
164
165 return -1;
166}
167
168char *dirname(char *path)
169{
170 /** @todo later */
171 return path;
172}
173
174
175int lchmod(const char *pszPath, mode_t mode)
176{
177 int rc = 0;
178 int fMustBeDir;
179 char *pszPathFree = msc_fix_path(&pszPath, &fMustBeDir);
180
181 /*
182 * Get the current attributes
183 */
184 DWORD fAttr = GetFileAttributes(pszPath);
185 if (fAttr == INVALID_FILE_ATTRIBUTES)
186 rc = msc_set_errno(GetLastError());
187 else if (fMustBeDir & !(fAttr & FILE_ATTRIBUTE_DIRECTORY))
188 {
189 errno = ENOTDIR;
190 rc = -1;
191 }
192 else
193 {
194 /*
195 * Modify the attributes and try set them.
196 */
197 if (mode & _S_IWRITE)
198 fAttr &= ~FILE_ATTRIBUTE_READONLY;
199 else
200 fAttr |= FILE_ATTRIBUTE_READONLY;
201 if (!SetFileAttributes(pszPath, fAttr))
202 rc = msc_set_errno(GetLastError());
203 }
204
205 if (pszPathFree)
206 {
207 int saved_errno = errno;
208 free(pszPathFree);
209 errno = saved_errno;
210 }
211 return rc;
212}
213
214
215int msc_chmod(const char *pszPath, mode_t mode)
216{
217 int rc = 0;
218 int saved_errno;
219 int fMustBeDir;
220 char *pszPathFree = msc_fix_path(&pszPath, &fMustBeDir);
221
222 /*
223 * Get the current attributes.
224 */
225 DWORD fAttr = GetFileAttributes(pszPath);
226 if (fAttr == INVALID_FILE_ATTRIBUTES)
227 rc = msc_set_errno(GetLastError());
228 else if (fMustBeDir & !(fAttr & FILE_ATTRIBUTE_DIRECTORY))
229 {
230 errno = ENOTDIR;
231 rc = -1;
232 }
233 else if (fAttr & FILE_ATTRIBUTE_REPARSE_POINT)
234 {
235 errno = ENOSYS; /** @todo resolve symbolic link / rewrite to NtSetInformationFile. */
236 rc = -1;
237 }
238 else
239 {
240 /*
241 * Modify the attributes and try set them.
242 */
243 if (mode & _S_IWRITE)
244 fAttr &= ~FILE_ATTRIBUTE_READONLY;
245 else
246 fAttr |= FILE_ATTRIBUTE_READONLY;
247 if (!SetFileAttributes(pszPath, fAttr))
248 rc = msc_set_errno(GetLastError());
249 }
250
251 if (pszPathFree)
252 {
253 int saved_errno = errno;
254 free(pszPathFree);
255 errno = saved_errno;
256 }
257 return rc;
258}
259
260
261int link(const char *pszDst, const char *pszLink)
262{
263 if (CreateHardLink(pszDst, pszLink, NULL))
264 return 0;
265 return msc_set_errno(GetLastError());
266}
267
268
269int mkdir_msc(const char *path, mode_t mode)
270{
271 int rc = (mkdir)(path);
272 if (rc)
273 {
274 size_t len = strlen(path);
275 if (len > 0 && (path[len - 1] == '/' || path[len - 1] == '\\'))
276 {
277 char *str = strdup(path);
278 while (len > 0 && (str[len - 1] == '/' || str[len - 1] == '\\'))
279 str[--len] = '\0';
280 rc = (mkdir)(str);
281 free(str);
282 }
283 }
284 return rc;
285}
286
287int rmdir_msc(const char *path)
288{
289 int rc = (rmdir)(path);
290 if (rc)
291 {
292 size_t len = strlen(path);
293 if (len > 0 && (path[len - 1] == '/' || path[len - 1] == '\\'))
294 {
295 char *str = strdup(path);
296 while (len > 0 && (str[len - 1] == '/' || str[len - 1] == '\\'))
297 str[--len] = '\0';
298 rc = (rmdir)(str);
299 free(str);
300 }
301 }
302 return rc;
303}
304
305
306static int doname(char *pszX, char *pszEnd)
307{
308 static char s_szChars[] = "Xabcdefghijklmnopqrstuwvxyz1234567890";
309 int rc = 0;
310 do
311 {
312 char ch;
313
314 pszEnd++;
315 ch = *(strchr(s_szChars, *pszEnd) + 1);
316 if (ch)
317 {
318 *pszEnd = ch;
319 return 0;
320 }
321 *pszEnd = 'a';
322 } while (pszEnd != pszX);
323 return 1;
324}
325
326
327int mkstemp(char *temp)
328{
329 char *pszX = strchr(temp, 'X');
330 char *pszEnd = strchr(pszX, '\0');
331 int cTries = 1000;
332 while (--cTries > 0)
333 {
334 int fd;
335 if (doname(pszX, pszEnd))
336 return -1;
337 fd = open(temp, _O_EXCL | _O_CREAT | _O_BINARY | _O_RDWR, 0777);
338 if (fd >= 0)
339 return fd;
340 }
341 return -1;
342}
343
344
345/** Unix to DOS. */
346static char *fix_slashes(char *psz)
347{
348 char *pszRet = psz;
349 for (; *psz; psz++)
350 if (*psz == '/')
351 *psz = '\\';
352 return pszRet;
353}
354
355
356/** Calcs the SYMBOLIC_LINK_FLAG_DIRECTORY flag for CreatesymbolcLink. */
357static DWORD is_directory(const char *pszPath, const char *pszRelativeTo)
358{
359 size_t cchPath = strlen(pszPath);
360 struct stat st;
361 if (cchPath > 0 && pszPath[cchPath - 1] == '\\' || pszPath[cchPath - 1] == '/')
362 return 1; /* SYMBOLIC_LINK_FLAG_DIRECTORY */
363
364 if (stat(pszPath, &st))
365 {
366 size_t cchRelativeTo = strlen(pszRelativeTo);
367 char *psz = malloc(cchPath + cchRelativeTo + 4);
368 memcpy(psz, pszRelativeTo, cchRelativeTo);
369 memcpy(psz + cchRelativeTo, "\\", 1);
370 memcpy(psz + cchRelativeTo + 1, pszPath, cchPath + 1);
371 if (stat(pszPath, &st))
372 st.st_mode = _S_IFREG;
373 free(psz);
374 }
375
376 return (st.st_mode & _S_IFMT) == _S_IFDIR ? 1 : 0;
377}
378
379
380int symlink(const char *pszDst, const char *pszLink)
381{
382 static BOOLEAN (WINAPI *s_pfnCreateSymbolicLinkA)(LPCSTR, LPCSTR, DWORD) = 0;
383 static BOOL s_fTried = FALSE;
384
385 if (!s_fTried)
386 {
387 HMODULE hmod = LoadLibrary("KERNEL32.DLL");
388 if (hmod)
389 *(FARPROC *)&s_pfnCreateSymbolicLinkA = GetProcAddress(hmod, "CreateSymbolicLinkA");
390 s_fTried = TRUE;
391 }
392
393 if (s_pfnCreateSymbolicLinkA)
394 {
395 char *pszDstCopy = fix_slashes(strdup(pszDst));
396 char *pszLinkCopy = fix_slashes(strdup(pszLink));
397 BOOLEAN fRc = s_pfnCreateSymbolicLinkA(pszLinkCopy, pszDstCopy,
398 is_directory(pszDstCopy, pszLinkCopy));
399 DWORD err = GetLastError();
400 free(pszDstCopy);
401 free(pszLinkCopy);
402 if (fRc)
403 return 0;
404 switch (err)
405 {
406 case ERROR_NOT_SUPPORTED: errno = ENOSYS; break;
407 case ERROR_ALREADY_EXISTS:
408 case ERROR_FILE_EXISTS: errno = EEXIST; break;
409 case ERROR_DIRECTORY: errno = ENOTDIR; break;
410 case ERROR_ACCESS_DENIED:
411 case ERROR_PRIVILEGE_NOT_HELD: errno = EPERM; break;
412 default: errno = EINVAL; break;
413 }
414 return -1;
415 }
416
417 errno = ENOSYS;
418 err(1, "symlink() is not implemented on windows!");
419 return -1;
420}
421
422
423#if _MSC_VER < 1400
424int snprintf(char *buf, size_t size, const char *fmt, ...)
425{
426 int cch;
427 va_list args;
428 va_start(args, fmt);
429 cch = vsprintf(buf, fmt, args);
430 va_end(args);
431 return cch;
432}
433#endif
434
435
436int utimes(const char *pszPath, const struct timeval *paTimes)
437{
438 /** @todo implement me! */
439 return 0;
440}
441
442
443int writev(int fd, const struct iovec *vector, int count)
444{
445 int size = 0;
446 int i;
447 for (i = 0; i < count; i++)
448 {
449 int cb = (int)write(fd, vector[i].iov_base, (int)vector[i].iov_len);
450 if (cb < 0)
451 return -1;
452 size += cb;
453 }
454 return size;
455}
456
457
458intmax_t strtoimax(const char *nptr, char **endptr, int base)
459{
460 return strtol(nptr, endptr, base); /** @todo fix this. */
461}
462
463
464uintmax_t strtoumax(const char *nptr, char **endptr, int base)
465{
466 return strtoul(nptr, endptr, base); /** @todo fix this. */
467}
468
469
470int asprintf(char **strp, const char *fmt, ...)
471{
472 int rc;
473 va_list va;
474 va_start(va, fmt);
475 rc = vasprintf(strp, fmt, va);
476 va_end(va);
477 return rc;
478}
479
480
481int vasprintf(char **strp, const char *fmt, va_list va)
482{
483 int rc;
484 char *psz;
485 size_t cb = 1024;
486
487 *strp = NULL;
488 for (;;)
489 {
490 va_list va2;
491
492 psz = malloc(cb);
493 if (!psz)
494 return -1;
495
496#ifdef va_copy
497 va_copy(va2, va);
498 rc = snprintf(psz, cb, fmt, va2);
499 va_end(vaCopy);
500#else
501 va2 = va;
502 rc = snprintf(psz, cb, fmt, va2);
503#endif
504 if (rc < 0 || (size_t)rc < cb)
505 break;
506 cb *= 2;
507 free(psz);
508 }
509
510 *strp = psz;
511 return rc;
512}
513
514
515#undef stat
516/*
517 * Workaround for directory names with trailing slashes.
518 * Added by bird reasons stated.
519 */
520int
521my_other_stat(const char *path, struct stat *st)
522{
523 int rc = stat(path, st);
524 if ( rc != 0
525 && errno == ENOENT
526 && *path != '\0')
527 {
528 char *slash = strchr(path, '\0') - 1;
529 if (*slash == '/' || *slash == '\\')
530 {
531 size_t len_path = slash - path + 1;
532 char *tmp = alloca(len_path + 4);
533 memcpy(tmp, path, len_path);
534 tmp[len_path] = '.';
535 tmp[len_path + 1] = '\0';
536 errno = 0;
537 rc = stat(tmp, st);
538 if ( rc == 0
539 && !S_ISDIR(st->st_mode))
540 {
541 errno = ENOTDIR;
542 rc = -1;
543 }
544 }
545 }
546 return rc;
547}
548
Note: See TracBrowser for help on using the repository browser.