source: trunk/src/kash/shfile.c@ 2293

Last change on this file since 2293 was 2293, checked in by bird, 16 years ago

kash: forking on windows (almost there).

  • Property svn:eol-style set to LF
  • Property svn:keywords set to Id
File size: 29.7 KB
Line 
1/* $Id: shfile.c 2293 2009-02-28 07:25:12Z bird $ */
2/** @file
3 *
4 * File management.
5 *
6 * Copyright (c) 2007-2009 knut st. osmundsen <bird-kBuild-spamix@anduin.net>
7 *
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 2 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, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 *
25 */
26
27#include "shfile.h"
28#include "shinstance.h" /* TRACE2 */
29#include <stdlib.h>
30#include <stdio.h>
31#include <assert.h>
32
33#if K_OS == K_OS_WINDOWS
34# include <limits.h>
35# ifndef PIPE_BUF
36# define PIPE_BUF 512
37# endif
38# include <Windows.h>
39#else
40# include <unistd.h>
41# include <fcntl.h>
42# include <dirent.h>
43#endif
44
45#ifdef DEBUG
46extern FILE *tracefile;
47#endif
48
49
50/*******************************************************************************
51* Defined Constants And Macros *
52*******************************************************************************/
53/** @def SHFILE_IN_USE
54 * Whether the file descriptor table stuff is actually in use or not.
55 */
56#if !defined(SH_PURE_STUB_MODE) \
57 && ( K_OS == K_OS_WINDOWS \
58 || ( !defined(SH_STUB_MODE) \
59 && !defined(SH_FORKED_MODE)) \
60 )
61# define SHFILE_IN_USE
62#endif
63/** The max file table size. */
64#define SHFILE_MAX 1024
65/** The file table growth rate. */
66#define SHFILE_GROW 64
67/** The min native unix file descriptor. */
68#define SHFILE_UNIX_MIN_FD 32
69/** The path buffer size we use. */
70#define SHFILE_MAX_PATH 4096
71
72/** Set errno and return. Doing a trace in debug build. */
73#define RETURN_ERROR(rc, err, msg) \
74 do { \
75 TRACE2((NULL, "%s: " ## msg ## " - returning %d / %d\n", __FUNCTION__, (rc), (err))); \
76 errno = (err); \
77 return (rc); \
78 } while (0)
79
80
81#ifdef SHFILE_IN_USE
82
83/**
84 * Close the specified native handle.
85 *
86 * @param native The native file handle.
87 * @param flags The flags in case they might come in handy later.
88 */
89static void shfile_native_close(intptr_t native, unsigned flags)
90{
91#if K_OS == K_OS_WINDOWS
92 BOOL fRc = CloseHandle((HANDLE)native);
93 assert(fRc); (void)fRc;
94#else
95 int s = errno;
96 close(native);
97 errno = s;
98#endif
99 (void)flags;
100}
101
102/**
103 * Inserts the file into the descriptor table.
104 *
105 * If we're out of memory and cannot extend the table, we'll close the
106 * file, set errno to EMFILE and return -1.
107 *
108 * @returns The file descriptor number. -1 and errno on failure.
109 * @param pfdtab The file descriptor table.
110 * @param native The native file handle.
111 * @param flags The flags the it was created with.
112 * @param fdMin The minimum file descriptor number, pass -1 if any is ok.
113 * @param who Who we're doing this for (for logging purposes).
114 */
115static int shfile_insert(shfdtab *pfdtab, intptr_t native, unsigned flags, int fdMin, const char *who)
116{
117 shmtxtmp tmp;
118 int fd;
119 int i;
120
121 /*
122 * Fend of bad stuff.
123 */
124 if (fdMin >= SHFILE_MAX)
125 {
126 errno = EMFILE;
127 return -1;
128 }
129
130 shmtx_enter(&pfdtab->mtx, &tmp);
131
132 /*
133 * Search for a fitting unused location.
134 */
135 fd = -1;
136 for (i = 0; (unsigned)i < pfdtab->size; i++)
137 if ( i >= fdMin
138 && pfdtab->tab[i].fd == -1)
139 {
140 fd = i;
141 break;
142 }
143 if (fd == -1)
144 {
145 /*
146 * Grow the descriptor table.
147 */
148 shfile *new_tab;
149 int new_size = pfdtab->size + SHFILE_GROW;
150 while (new_size < fdMin)
151 new_size += SHFILE_GROW;
152 new_tab = sh_realloc(NULL, pfdtab->tab, new_size * sizeof(shfile));
153 if (new_tab)
154 {
155 fd = i = pfdtab->size;
156 if (fd < fdMin)
157 fd = fdMin;
158 for (i = pfdtab->size; i < new_size; i++)
159 {
160 new_tab[i].fd = -1;
161 new_tab[i].flags = 0;
162 new_tab[i].native = -1;
163 }
164
165 pfdtab->tab = new_tab;
166 pfdtab->size = new_size;
167 }
168 }
169
170 /*
171 * Fill in the entry if we've found one.
172 */
173 if (fd != -1)
174 {
175 pfdtab->tab[fd].fd = fd;
176 pfdtab->tab[fd].flags = flags;
177 pfdtab->tab[fd].cloexec = 0;
178 pfdtab->tab[fd].native = native;
179 }
180 else
181 shfile_native_close(native, flags);
182
183 shmtx_leave(&pfdtab->mtx, &tmp);
184
185 if (fd == -1)
186 errno = EMFILE;
187 (void)who;
188 return fd;
189}
190
191/**
192 * Gets a file descriptor and lock the file descriptor table.
193 *
194 * @returns Pointer to the file and table ownership on success,
195 * NULL and errno set to EBADF on failure.
196 * @param pfdtab The file descriptor table.
197 * @param fd The file descriptor number.
198 * @param ptmp See shmtx_enter.
199 */
200static shfile *shfile_get(shfdtab *pfdtab, int fd, shmtxtmp *ptmp)
201{
202 shfile *file = NULL;
203 if ( fd >= 0
204 && (unsigned)fd < pfdtab->size)
205 {
206 shmtx_enter(&pfdtab->mtx, ptmp);
207 if ((unsigned)fd < pfdtab->size
208 && pfdtab->tab[fd].fd != -1)
209 file = &pfdtab->tab[fd];
210 else
211 shmtx_leave(&pfdtab->mtx, ptmp);
212 }
213 if (!file)
214 errno = EBADF;
215 return file;
216}
217
218/**
219 * Puts back a file descriptor and releases the table ownership.
220 *
221 * @param pfdtab The file descriptor table.
222 * @param file The file.
223 * @param ptmp See shmtx_leave.
224 */
225static void shfile_put(shfdtab *pfdtab, shfile *file, shmtxtmp *ptmp)
226{
227 shmtx_leave(&pfdtab->mtx, ptmp);
228 assert(file);
229 (void)file;
230}
231
232/**
233 * Constructs a path from the current directory and the passed in path.
234 *
235 * @returns 0 on success, -1 and errno set to ENAMETOOLONG or EINVAL on failure.
236 *
237 * @param pfdtab The file descriptor table
238 * @param path The path the caller supplied.
239 * @param buf Where to put the path. This is assumed to be SHFILE_MAX_PATH
240 * chars in size.
241 */
242int shfile_make_path(shfdtab *pfdtab, const char *path, char *buf)
243{
244 size_t path_len = strlen(path);
245 if (path_len == 0)
246 {
247 errno = EINVAL;
248 return -1;
249 }
250 if (path_len >= SHFILE_MAX_PATH)
251 {
252 errno = ENAMETOOLONG;
253 return -1;
254 }
255 if ( *path == '/'
256#if K_OS == K_OS_WINDOWS || K_OS == K_OS_OS2
257 || *path == '\\'
258 || ( *path
259 && path[1] == ':'
260 && ( (*path >= 'A' && *path <= 'Z')
261 || (*path >= 'a' && *path <= 'z')))
262#endif
263 )
264 {
265 memcpy(buf, path, path_len + 1);
266 }
267 else
268 {
269 size_t cwd_len;
270 shmtxtmp tmp;
271
272 shmtx_enter(&pfdtab->mtx, &tmp);
273
274 cwd_len = strlen(pfdtab->cwd);
275 memcpy(buf, pfdtab->cwd, cwd_len);
276
277 shmtx_leave(&pfdtab->mtx, &tmp);
278
279 if (cwd_len + path_len + 1 >= SHFILE_MAX_PATH)
280 {
281 errno = ENAMETOOLONG;
282 return -1;
283 }
284 if ( !cwd_len
285 || buf[cwd_len - 1] != '/')
286 buf[cwd_len++] = '/';
287 memcpy(buf + cwd_len, path, path_len + 1);
288 }
289
290#if K_OS == K_OS_WINDOWS || K_OS == K_OS_OS2
291 if (!strcmp(buf, "/dev/null"))
292 strcpy(buf, "NUL");
293#endif
294 return 0;
295}
296
297# if K_OS == K_OS_WINDOWS
298/**
299 * Converts a DOS(/Windows) error code to errno,
300 * assigning it to errno.
301 *
302 * @returns -1
303 * @param err The DOS error.
304 */
305static int shfile_dos2errno(int err)
306{
307 switch (err)
308 {
309 case ERROR_BAD_ENVIRONMENT: errno = E2BIG; break;
310 case ERROR_ACCESS_DENIED: errno = EACCES; break;
311 case ERROR_CURRENT_DIRECTORY: errno = EACCES; break;
312 case ERROR_LOCK_VIOLATION: errno = EACCES; break;
313 case ERROR_NETWORK_ACCESS_DENIED: errno = EACCES; break;
314 case ERROR_CANNOT_MAKE: errno = EACCES; break;
315 case ERROR_FAIL_I24: errno = EACCES; break;
316 case ERROR_DRIVE_LOCKED: errno = EACCES; break;
317 case ERROR_SEEK_ON_DEVICE: errno = EACCES; break;
318 case ERROR_NOT_LOCKED: errno = EACCES; break;
319 case ERROR_LOCK_FAILED: errno = EACCES; break;
320 case ERROR_NO_PROC_SLOTS: errno = EAGAIN; break;
321 case ERROR_MAX_THRDS_REACHED: errno = EAGAIN; break;
322 case ERROR_NESTING_NOT_ALLOWED: errno = EAGAIN; break;
323 case ERROR_INVALID_HANDLE: errno = EBADF; break;
324 case ERROR_INVALID_TARGET_HANDLE: errno = EBADF; break;
325 case ERROR_DIRECT_ACCESS_HANDLE: errno = EBADF; break;
326 case ERROR_WAIT_NO_CHILDREN: errno = ECHILD; break;
327 case ERROR_CHILD_NOT_COMPLETE: errno = ECHILD; break;
328 case ERROR_FILE_EXISTS: errno = EEXIST; break;
329 case ERROR_ALREADY_EXISTS: errno = EEXIST; break;
330 case ERROR_INVALID_FUNCTION: errno = EINVAL; break;
331 case ERROR_INVALID_ACCESS: errno = EINVAL; break;
332 case ERROR_INVALID_DATA: errno = EINVAL; break;
333 case ERROR_INVALID_PARAMETER: errno = EINVAL; break;
334 case ERROR_NEGATIVE_SEEK: errno = EINVAL; break;
335 case ERROR_TOO_MANY_OPEN_FILES: errno = EMFILE; break;
336 case ERROR_FILE_NOT_FOUND: errno = ENOENT; break;
337 case ERROR_PATH_NOT_FOUND: errno = ENOENT; break;
338 case ERROR_INVALID_DRIVE: errno = ENOENT; break;
339 case ERROR_NO_MORE_FILES: errno = ENOENT; break;
340 case ERROR_BAD_NETPATH: errno = ENOENT; break;
341 case ERROR_BAD_NET_NAME: errno = ENOENT; break;
342 case ERROR_BAD_PATHNAME: errno = ENOENT; break;
343 case ERROR_FILENAME_EXCED_RANGE: errno = ENOENT; break;
344 case ERROR_BAD_FORMAT: errno = ENOEXEC; break;
345 case ERROR_ARENA_TRASHED: errno = ENOMEM; break;
346 case ERROR_NOT_ENOUGH_MEMORY: errno = ENOMEM; break;
347 case ERROR_INVALID_BLOCK: errno = ENOMEM; break;
348 case ERROR_NOT_ENOUGH_QUOTA: errno = ENOMEM; break;
349 case ERROR_DISK_FULL: errno = ENOSPC; break;
350 case ERROR_DIR_NOT_EMPTY: errno = ENOTEMPTY; break;
351 case ERROR_BROKEN_PIPE: errno = EPIPE; break;
352 case ERROR_NOT_SAME_DEVICE: errno = EXDEV; break;
353 default: errno = EINVAL; break;
354 }
355 return -1;
356}
357# endif /* K_OS == K_OS_WINDOWS */
358
359#endif /* SHFILE_IN_USE */
360
361/**
362 * Initializes a file descriptor table.
363 *
364 * @returns 0 on success, -1 and errno on failure.
365 * @param pfdtab The table to initialize.
366 * @param inherit File descriptor table to inherit from. If not specified
367 * we will inherit from the current process as it were.
368 */
369int shfile_init(shfdtab *pfdtab, shfdtab *inherit)
370{
371 int rc;
372
373 pfdtab->cwd = NULL;
374 pfdtab->size = 0;
375 pfdtab->tab = NULL;
376 rc = shmtx_init(&pfdtab->mtx);
377 if (!rc)
378 {
379#ifdef SHFILE_IN_USE
380 char buf[SHFILE_MAX_PATH];
381 if (getcwd(buf, sizeof(buf)))
382 {
383 pfdtab->cwd = strdup(buf);
384 if (!inherit)
385 {
386# if K_OS == K_OS_WINDOWS
387 static const struct
388 {
389 DWORD dwStdHandle;
390 unsigned flags;
391 } aStdHandles[3] =
392 {
393 { STD_INPUT_HANDLE, _O_RDONLY },
394 { STD_OUTPUT_HANDLE, _O_WRONLY },
395 { STD_ERROR_HANDLE, _O_WRONLY }
396 };
397 unsigned i;
398 STARTUPINFO Info;
399
400 rc = 0;
401
402 /* Try pick up the Visual C++ CRT file descriptor info. */
403 __try {
404 GetStartupInfo(&Info);
405 } __except (EXCEPTION_EXECUTE_HANDLER) {
406 memset(&Info, 0, sizeof(Info));
407 }
408 if ( Info.cbReserved2 <= sizeof(int)
409 && (uintptr_t)Info.lpReserved2 >= 0x1000
410 && *(int *)Info.lpReserved2 * (sizeof(int) + sizeof(intptr_t)) + 4 <= Info.cbReserved2
411 && *(int *)Info.lpReserved2 <= 2048
412 && *(int *)Info.lpReserved2 >= 1)
413 {
414 unsigned c = *(int *)Info.lpReserved2;
415 char *aosfile = (char *)Info.lpReserved2 + sizeof(int);
416 intptr_t *aosfhnd = (intptr_t *)(aosfile + c);
417
418 /** @todo process */
419
420 }
421
422 /* Check the three standard handles. */
423 for (i = 0; i < 3; i++)
424 {
425 HANDLE hFile = GetStdHandle(aStdHandles[i].dwStdHandle);
426 if ( hFile != INVALID_HANDLE_VALUE
427 && ( (unsigned)i >= pfdtab->size
428 || pfdtab->tab[i].fd == -1))
429 {
430 int fd2 = shfile_insert(pfdtab, (intptr_t)hFile, aStdHandles[i].flags, i, "shtab_init");
431 assert(fd2 == i); (void)fd2;
432 if (fd2 != i)
433 rc = -1;
434 }
435 }
436# else
437# endif
438 }
439 else
440 {
441 /** @todo */
442 errno = ENOSYS;
443 rc = -1;
444 }
445 }
446 else
447 rc = -1;
448#endif
449 }
450 return rc;
451}
452
453#if K_OS == K_OS_WINDOWS && defined(SHFILE_IN_USE) //&& defined(SH_FORKED_MODE)
454/**
455 * Helper for shfork.
456 *
457 * @param pfdtab The file descriptor table.
458 * @param set Whether to make all handles inheritable (1) or
459 * to restore them to the rigth state (0).
460 * @param hndls Where to store the three standard handles.
461 */
462void shfile_fork_win(shfdtab *pfdtab, int set, intptr_t *hndls)
463{
464 shmtxtmp tmp;
465 unsigned i;
466
467 shmtx_enter(&pfdtab->mtx, &tmp);
468
469 i = pfdtab->size;
470 while (i-- > 0)
471 {
472 if (pfdtab->tab[i].fd == i)
473 {
474 HANDLE hFile = (HANDLE)pfdtab->tab[i].native;
475 DWORD fFlag = set || !pfdtab->tab[i].cloexec ? HANDLE_FLAG_INHERIT : 0;
476 if (!SetHandleInformation(hFile, HANDLE_FLAG_INHERIT, fFlag))
477 {
478 DWORD err = GetLastError();
479 assert(0);
480 }
481 }
482 }
483
484 if (hndls)
485 {
486 for (i = 0; i < 3; i++)
487 {
488 if ( pfdtab->size > i
489 && pfdtab->tab[i].fd == 0)
490 hndls[i] = pfdtab->tab[i].native;
491 else
492 hndls[i] = (intptr_t)INVALID_HANDLE_VALUE;
493 }
494 }
495
496 shmtx_leave(&pfdtab->mtx, &tmp);
497}
498#endif
499
500
501/**
502 * open().
503 */
504int shfile_open(shfdtab *pfdtab, const char *name, unsigned flags, mode_t mode)
505{
506 int fd;
507#ifdef SHFILE_IN_USE
508 char absname[SHFILE_MAX_PATH];
509# if K_OS == K_OS_WINDOWS
510 HANDLE hFile;
511 DWORD dwDesiredAccess;
512 DWORD dwShareMode;
513 DWORD dwCreationDisposition;
514 DWORD dwFlagsAndAttributes;
515 SECURITY_ATTRIBUTES SecurityAttributes;
516
517# ifndef _O_ACCMODE
518# define _O_ACCMODE (_O_RDONLY|_O_WRONLY|_O_RDWR)
519# endif
520 switch (flags & (_O_ACCMODE | _O_APPEND))
521 {
522 case _O_RDONLY: dwDesiredAccess = GENERIC_READ; break;
523 case _O_RDONLY | _O_APPEND: dwDesiredAccess = GENERIC_READ; break;
524 case _O_WRONLY: dwDesiredAccess = GENERIC_WRITE; break;
525 case _O_WRONLY | _O_APPEND: dwDesiredAccess = (FILE_GENERIC_WRITE & ~FILE_WRITE_DATA); break;
526 case _O_RDWR: dwDesiredAccess = GENERIC_READ | GENERIC_WRITE; break;
527 case _O_RDWR | _O_APPEND: dwDesiredAccess = GENERIC_READ | (FILE_GENERIC_WRITE & ~FILE_WRITE_DATA); break;
528
529 default:
530 RETURN_ERROR(-1, EINVAL, "invalid mode");
531 }
532
533 dwShareMode = FILE_SHARE_DELETE | FILE_SHARE_READ | FILE_SHARE_WRITE;
534
535 SecurityAttributes.nLength = sizeof(SecurityAttributes);
536 SecurityAttributes.lpSecurityDescriptor = NULL;
537 SecurityAttributes.bInheritHandle = (flags & _O_NOINHERIT) == 0;
538
539 if (flags & _O_CREAT)
540 {
541 if ((flags & (_O_EXCL | _O_TRUNC)) == (_O_EXCL | _O_TRUNC))
542 RETURN_ERROR(-1, EINVAL, "_O_EXCL | _O_TRUNC");
543
544 if (flags & _O_TRUNC)
545 dwCreationDisposition = CREATE_ALWAYS; /* not 100%, but close enough */
546 else if (flags & _O_EXCL)
547 dwCreationDisposition = CREATE_NEW;
548 else
549 dwCreationDisposition = OPEN_ALWAYS;
550 }
551 else if (flags & _O_TRUNC)
552 dwCreationDisposition = TRUNCATE_EXISTING;
553 else
554 dwCreationDisposition = OPEN_EXISTING;
555
556 if (!(flags & _O_CREAT) || (mode & 0222))
557 dwFlagsAndAttributes = FILE_ATTRIBUTE_NORMAL;
558 else
559 dwFlagsAndAttributes = FILE_ATTRIBUTE_READONLY;
560
561 fd = shfile_make_path(pfdtab, name, &absname[0]);
562 if (!fd)
563 {
564 SetLastError(0);
565 hFile = CreateFileA(absname,
566 dwDesiredAccess,
567 dwShareMode,
568 &SecurityAttributes,
569 dwCreationDisposition,
570 dwFlagsAndAttributes,
571 NULL /* hTemplateFile */);
572 if (hFile != INVALID_HANDLE_VALUE)
573 fd = shfile_insert(pfdtab, (intptr_t)hFile, flags, -1, "shfile_open");
574 else
575 fd = shfile_dos2errno(GetLastError());
576 }
577
578# else /* K_OS != K_OS_WINDOWS */
579 fd = shfile_make_path(pfdtab, name, &absname[0]);
580 if (!fd)
581 {
582 fd = open(absname, flag, mode);
583 if (fd != -1)
584 {
585 int native = fcntl(fd, F_DUPFD, SHFILE_UNIX_MIN_FD);
586 int s = errno;
587 close(fd);
588 errno = s;
589 if (native != -1)
590 fd = shfile_insert(pfdtab, native, flags, -1, "shfile_open");
591 else
592 fd = -1;
593 }
594 }
595
596# endif /* K_OS != K_OS_WINDOWS */
597
598#elif defined(SH_PURE_STUB_MODE)
599 fd = -1;
600 errno = ENOSYS;
601
602#elif defined(SH_STUB_MODE) || defined(SH_FORKED_MODE)
603 fd = open(name, flags, mode);
604#endif
605
606#ifdef DEBUG
607 if (tracefile)
608 TRACE2((NULL, "shfile_open(%p:{%s}, %#x, 0%o) -> %d [%d]\n", name, name, flags, mode, fd, errno));
609#endif
610 return fd;
611}
612
613int shfile_pipe(shfdtab *pfdtab, int fds[2])
614{
615#ifdef SH_PURE_STUB_MODE
616 return -1;
617
618#elif defined(SH_STUB_MODE) || defined(SH_FORKED_MODE)
619# ifdef _MSC_VER
620 return _pipe(fds, PIPE_BUF, O_BINARY);
621# else
622 return pipe(fds);
623# endif
624
625#else
626#endif
627}
628
629int shfile_dup(shfdtab *pfdtab, int fd)
630{
631 int rc;
632#ifdef SH_PURE_STUB_MODE
633 rc = -1;
634
635#elif defined(SH_STUB_MODE) || defined(SH_FORKED_MODE)
636 rc = dup(fd);
637
638#else
639#endif
640
641 TRACE2((NULL, "shfile_dup(%d) -> %d [%d]\n", fd, rc, errno));
642 return rc;
643}
644
645/**
646 * close().
647 */
648int shfile_close(shfdtab *pfdtab, unsigned fd)
649{
650 int rc;
651#ifdef SHFILE_IN_USE
652 shmtxtmp tmp;
653 shfile *file = shfile_get(pfdtab, fd, &tmp);
654 if (file)
655 {
656 shfile_native_close(file->native, file->flags);
657
658 file->fd = -1;
659 file->flags = 0;
660 file->native = -1;
661
662 shfile_put(pfdtab, file, &tmp);
663 rc = 0;
664 }
665 else
666 rc = -1;
667
668#elif defined(SH_PURE_STUB_MODE)
669 rc = -1;
670
671#elif defined(SH_STUB_MODE) || defined(SH_FORKED_MODE)
672 rc = close(fd);
673#endif
674
675 TRACE2((NULL, "shfile_close(%d) -> %d [%d]\n", fd, rc, errno));
676 return rc;
677}
678
679/**
680 * read().
681 */
682long shfile_read(shfdtab *pfdtab, int fd, void *buf, size_t len)
683{
684 long rc;
685#ifdef SHFILE_IN_USE
686 shmtxtmp tmp;
687 shfile *file = shfile_get(pfdtab, fd, &tmp);
688 if (file)
689 {
690# if K_OS == K_OS_WINDOWS
691 DWORD dwRead = 0;
692 if (ReadFile((HANDLE)file->native, buf, (DWORD)len, &dwRead, NULL))
693 rc = dwRead;
694 else
695 rc = shfile_dos2errno(GetLastError());
696# else
697 rc = read(file->native, buf, len);
698# endif
699
700 shfile_put(pfdtab, file, &tmp);
701 }
702 else
703 rc = -1;
704
705#elif defined(SH_PURE_STUB_MODE)
706 rc = -1;
707
708#elif defined(SH_STUB_MODE) || defined(SH_FORKED_MODE)
709# ifdef _MSC_VER
710 rc = read(fd, buf, (unsigned)len);
711# else
712 rc = read(fd, buf, len);
713# endif
714#endif
715 return rc;
716}
717
718/**
719 * write().
720 */
721long shfile_write(shfdtab *pfdtab, int fd, const void *buf, size_t len)
722{
723 long rc;
724#ifdef SHFILE_IN_USE
725 shmtxtmp tmp;
726 shfile *file = shfile_get(pfdtab, fd, &tmp);
727 if (file)
728 {
729# if K_OS == K_OS_WINDOWS
730 DWORD dwWritten = 0;
731 if (WriteFile((HANDLE)file->native, buf, (DWORD)len, &dwWritten, NULL))
732 rc = dwWritten;
733 else
734 rc = shfile_dos2errno(GetLastError());
735# else
736 rc = write(file->native, buf, len);
737# endif
738
739 shfile_put(pfdtab, file, &tmp);
740 }
741 else
742 rc = -1;
743
744#elif defined(SH_PURE_STUB_MODE)
745 rc = -1;
746
747#elif defined(SH_STUB_MODE) || defined(SH_FORKED_MODE)
748# ifdef _MSC_VER
749 rc = write(fd, buf, (unsigned)len);
750# else
751 rc = write(fd, buf, len);
752# endif
753#endif
754 return rc;
755}
756
757/**
758 * lseek().
759 */
760long shfile_lseek(shfdtab *pfdtab, int fd, long off, int whench)
761{
762 long rc;
763#ifdef SHFILE_IN_USE
764 shmtxtmp tmp;
765 shfile *file = shfile_get(pfdtab, fd, &tmp);
766 if (file)
767 {
768# if K_OS == K_OS_WINDOWS
769 assert(SEEK_SET == FILE_BEGIN);
770 assert(SEEK_CUR == FILE_CURRENT);
771 assert(SEEK_END == FILE_END);
772 rc = SetFilePointer((HANDLE)file->native, off, NULL, whench);
773 if (rc == INVALID_SET_FILE_POINTER)
774 rc = shfile_dos2errno(GetLastError());
775# else
776 rc = lseek(file->native, off, whench);
777# endif
778
779 shfile_put(pfdtab, file, &tmp);
780 }
781 else
782 rc = -1;
783
784#elif defined(SH_PURE_STUB_MODE)
785 rc = -1;
786
787#elif defined(SH_STUB_MODE) || defined(SH_FORKED_MODE)
788 rc = lseek(fd, off, whench);
789#endif
790
791 return rc;
792}
793
794int shfile_fcntl(shfdtab *pfdtab, int fd, int cmd, int arg)
795{
796 int rc;
797#ifdef SHFILE_IN_USE
798 shmtxtmp tmp;
799 shfile *file = shfile_get(pfdtab, fd, &tmp);
800 if (file)
801 {
802 switch (cmd)
803 {
804 case F_GETFL:
805 rc = file->flags;
806 break;
807
808 case F_SETFL:
809 {
810 unsigned mask = O_NONBLOCK | O_APPEND | O_BINARY | O_TEXT;
811# ifdef O_DIRECT
812 mask |= O_DIRECT;
813# endif
814# ifdef O_ASYNC
815 mask |= O_ASYNC;
816# endif
817# ifdef O_SYNC
818 mask |= O_SYNC;
819# endif
820 if ((file->flags & mask) == (arg & mask))
821 rc = 0;
822 else
823 {
824# if K_OS == K_OS_WINDOWS
825 assert(0);
826 errno = EINVAL;
827 rc = -1;
828# else
829 rc = fcntl(file->native, F_SETFL, arg);
830 if (rc != -1)
831 file->flags = (file->flags & ~mask) | (arg & mask);
832# endif
833 }
834 break;
835 }
836
837 case F_DUPFD:
838 {
839# if K_OS == K_OS_WINDOWS
840 HANDLE hNew = INVALID_HANDLE_VALUE;
841 if (DuplicateHandle(GetCurrentProcess(),
842 (HANDLE)file->native,
843 GetCurrentProcess(),
844 &hNew,
845 0,
846 TRUE /* bInheritHandle */,
847 DUPLICATE_SAME_ACCESS))
848 rc = shfile_insert(pfdtab, (intptr_t)hNew, file->flags, arg, "shfile_fcntl");
849 else
850 rc = shfile_dos2errno(GetLastError());
851# else
852 int nativeNew = fcntl(file->native F_DUPFD, SHFILE_UNIX_MIN_FD);
853 if (nativeNew != -1)
854 rc = shfile_insert(pfdtab, nativeNew, file->flags, arg, "shfile_fcntl");
855# endif
856 break;
857 }
858
859 default:
860 errno = -EINVAL;
861 rc = -1;
862 break;
863 }
864
865 shfile_put(pfdtab, file, &tmp);
866 }
867 else
868 rc = -1;
869
870#elif defined(SH_PURE_STUB_MODE)
871 rc = -1;
872
873#elif defined(SH_STUB_MODE) || defined(SH_FORKED_MODE)
874# ifdef _MSC_VER
875 switch (cmd)
876 {
877 /* Just enough F_GETFL/F_SETFL to get along with. */
878 case F_GETFL:
879 errno = 0;
880 rc = _isatty(fd);
881 if (errno == EBADF)
882 rc = -1;
883 break;
884
885 case F_SETFL:
886 errno = 0;
887 rc = _isatty(fd);
888 if (errno != EBADF)
889 {
890 if (!arg)
891 rc = 0;
892 else
893 {
894 errno = EINVAL;
895 rc = -1;
896 }
897 }
898 else
899 rc = -1;
900 break;
901
902 case F_DUPFD:
903 {
904 /* the brute force approach. */
905 int i = 0;
906 int fds[256];
907 for (i = 0; i < 256; i++)
908 {
909 fds[i] = -1;
910 rc = _dup(fd);
911 if (rc >= arg)
912 break;
913 fds[i] = rc;
914 }
915 while (i-- > 0)
916 close(fds[i]);
917 if (rc < arg)
918 {
919 errno = EMFILE;
920 rc = -1;
921 }
922 break;
923 }
924 }
925# else
926 rc = fcntl(fd, cmd, arg);
927# endif
928#endif
929
930#ifdef DEBUG
931 if (tracefile)
932 switch (cmd)
933 {
934 case F_GETFL:
935 TRACE2((NULL, "shfile_fcntl(%d,F_GETFL,ignored=%d) -> %d [%d]\n", fd, arg, rc, errno));
936 break;
937 case F_SETFL:
938 TRACE2((NULL, "shfile_fcntl(%d,F_SETFL,newflags=%#x) -> %d [%d]\n", fd, arg, rc, errno));
939 break;
940 case F_DUPFD:
941 TRACE2((NULL, "shfile_fcntl(%d,F_DUPFS,minfd=%d) -> %d [%d]\n", fd, arg, rc, errno));
942 break;
943 default:
944 TRACE2((NULL, "shfile_fcntl(%d,%d,%d) -> %d [%d]\n", fd, cmd, arg, rc, errno));
945 break;
946 }
947#endif
948 return rc;
949}
950
951int shfile_stat(shfdtab *pfdtab, const char *path, struct stat *pst)
952{
953#ifdef SH_PURE_STUB_MODE
954 return -1;
955
956#elif defined(SH_STUB_MODE) || defined(SH_FORKED_MODE)
957 return stat(path, pst);
958
959#else
960#endif
961}
962
963int shfile_lstat(shfdtab *pfdtab, const char *link, struct stat *pst)
964{
965#ifdef SH_PURE_STUB_MODE
966 return -1;
967
968#elif defined(SH_STUB_MODE) || defined(SH_FORKED_MODE)
969# ifdef _MSC_VER
970 return stat(link, pst);
971# else
972 return lstat(link, pst);
973# endif
974
975#else
976#endif
977}
978
979int shfile_chdir(shfdtab *pfdtab, const char *path)
980{
981#ifdef SH_PURE_STUB_MODE
982 return -1;
983
984#elif defined(SH_STUB_MODE) || defined(SH_FORKED_MODE)
985# ifdef _MSC_VER //???
986 return chdir(path);
987# else
988 return chdir(path);
989# endif
990
991#else
992#endif
993}
994
995char *shfile_getcwd(shfdtab *pfdtab, char *buf, int len)
996{
997#ifdef SH_PURE_STUB_MODE
998 return NULL;
999
1000#elif defined(SH_STUB_MODE) || defined(SH_FORKED_MODE)
1001 return getcwd(buf, len);
1002
1003#else
1004#endif
1005}
1006
1007int shfile_access(shfdtab *pfdtab, const char *path, int type)
1008{
1009#ifdef SH_PURE_STUB_MODE
1010 return -1;
1011
1012#elif defined(SH_STUB_MODE) || defined(SH_FORKED_MODE)
1013# ifdef _MSC_VER
1014 type &= ~X_OK;
1015 return access(path, type);
1016# else
1017 return access(path, type);
1018# endif
1019
1020#else
1021#endif
1022}
1023
1024int shfile_isatty(shfdtab *pfdtab, int fd)
1025{
1026 int rc;
1027
1028#ifdef SH_PURE_STUB_MODE
1029 rc = 0;
1030#elif defined(SH_STUB_MODE) || defined(SH_FORKED_MODE)
1031 rc = isatty(fd);
1032#else
1033#endif
1034
1035 TRACE2((NULL, "isatty(%d) -> %d [%d]\n", fd, rc, errno));
1036 return rc;
1037}
1038
1039
1040int shfile_cloexec(shfdtab *pfdtab, int fd, int closeit)
1041{
1042 int rc;
1043
1044#ifdef SH_PURE_STUB_MODE
1045 rc = -1;
1046
1047#elif defined(SH_STUB_MODE) || defined(SH_FORKED_MODE)
1048# ifdef _MSC_VER
1049 errno = ENOSYS;
1050 rc = -1;
1051# else
1052 rc = fcntl(fd, F_SETFD, fcntl(fd, F_GETFD, 0)
1053 | (closeit ? FD_CLOEXEC : 0));
1054# endif
1055
1056#else
1057#endif
1058
1059 TRACE2((NULL, "shfile_cloexec(%d, %d) -> %d [%d]\n", fd, closeit, rc, errno));
1060 return rc;
1061}
1062
1063
1064int shfile_ioctl(shfdtab *pfdtab, int fd, unsigned long request, void *buf)
1065{
1066 int rc;
1067
1068#ifdef SH_PURE_STUB_MODE
1069 rc = -1;
1070
1071#elif defined(SH_STUB_MODE) || defined(SH_FORKED_MODE)
1072# ifdef _MSC_VER
1073 errno = ENOSYS;
1074 rc = -1;
1075# else
1076 rc = ioctl(fd, request, buf);
1077# endif
1078
1079#else
1080#endif
1081
1082 TRACE2((NULL, "ioctl(%d, %#x, %p) -> %d\n", fd, request, buf, rc));
1083 return rc;
1084}
1085
1086
1087mode_t shfile_get_umask(shfdtab *pfdtab)
1088{
1089#ifdef SH_PURE_STUB_MODE
1090 return 022;
1091
1092#elif defined(SH_STUB_MODE) || defined(SH_FORKED_MODE)
1093 return 022;
1094
1095#else
1096#endif
1097}
1098
1099void shfile_set_umask(shfdtab *pfdtab, mode_t mask)
1100{
1101 (void)mask;
1102}
1103
1104
1105shdir *shfile_opendir(shfdtab *pfdtab, const char *dir)
1106{
1107#ifdef SH_PURE_STUB_MODE
1108 return NULL;
1109
1110#elif defined(SH_STUB_MODE) || defined(SH_FORKED_MODE)
1111# ifdef _MSC_VER
1112 errno = ENOSYS;
1113 return NULL;
1114# else
1115 return (shdir *)opendir(dir);
1116# endif
1117
1118#else
1119#endif
1120}
1121
1122shdirent *shfile_readdir(struct shdir *pdir)
1123{
1124#ifdef SH_PURE_STUB_MODE
1125 return NULL;
1126
1127#elif defined(SH_STUB_MODE) || defined(SH_FORKED_MODE)
1128# ifdef _MSC_VER
1129 errno = ENOSYS;
1130 return NULL;
1131# else
1132 struct dirent *pde = readdir((DIR *)pdir);
1133 return pde ? (shdirent *)&pde->d_name[0] : NULL;
1134# endif
1135
1136#else
1137#endif
1138}
1139
1140void shfile_closedir(struct shdir *pdir)
1141{
1142#ifdef SH_PURE_STUB_MODE
1143 return NULL;
1144
1145#elif defined(SH_STUB_MODE) || defined(SH_FORKED_MODE)
1146# ifdef _MSC_VER
1147 errno = ENOSYS;
1148# else
1149 closedir((DIR *)pdir);
1150# endif
1151
1152#else
1153#endif
1154}
Note: See TracBrowser for help on using the repository browser.