| 1 | /* | ||
| 2 | * arch_compat.h - the POSIX calls the extractor needs, on the platforms | ||
| 3 | * that have them and on Windows, which spells some of them differently | ||
| 4 | * and does not have the rest. | ||
| 5 | * | ||
| 6 | * Every name here is arch_-prefixed on purpose. Strawberry perl is built | ||
| 7 | * with PERL_IMPLICIT_SYS, under which XSUB.h rewrites the PLAIN names - | ||
| 8 | * mkdir, chmod, open, close, read, write, lstat, utime, unlink, stat - | ||
| 9 | * into macros that dereference my_perl. A shim defining the plain names | ||
| 10 | * would collide with those macros in any translation unit that includes | ||
| 11 | * XSUB.h, and would silently re-plumb calls that were meant to reach | ||
| 12 | * perl's own layer. Prefixed names cannot do either. | ||
| 13 | */ | ||
| 14 | |||
| 15 | #ifndef ARCH_COMPAT_H | ||
| 16 | #define ARCH_COMPAT_H | ||
| 17 | |||
| 18 | #include <errno.h> | ||
| 19 | #include <fcntl.h> | ||
| 20 | #include <sys/stat.h> | ||
| 21 | #include <sys/types.h> | ||
| 22 | |||
| 23 | #ifdef _WIN32 | ||
| 24 | # include <direct.h> | ||
| 25 | # include <io.h> | ||
| 26 | # include <sys/utime.h> | ||
| 27 | #else | ||
| 28 | # include <unistd.h> | ||
| 29 | # include <utime.h> | ||
| 30 | #endif | ||
| 31 | |||
| 32 | /* Windows opens text-mode by default, which would turn every LF in an | ||
| 33 | * extracted file into CRLF. Everything this dist writes is bytes. */ | ||
| 34 | #ifdef O_BINARY | ||
| 35 | # define ARCH_O_BINARY O_BINARY | ||
| 36 | #else | ||
| 37 | # define ARCH_O_BINARY 0 | ||
| 38 | #endif | ||
| 39 | |||
| 40 | #ifndef ENOSYS | ||
| 41 | # define ENOSYS EINVAL | ||
| 42 | #endif | ||
| 43 | |||
| 44 | /* mkdir(2). Windows takes no mode - the permission bits have no meaning | ||
| 45 | * on NTFS - so the caller's mode is dropped there, exactly as it is for | ||
| 46 | * an existing directory on POSIX. */ | ||
| 47 | static int | ||
| 48 | 0 | arch_mkdir(const char *path, unsigned int mode) | |
| 49 | { | ||
| 50 | #ifdef _WIN32 | ||
| 51 | (void)mode; | ||
| 52 | return _mkdir(path); | ||
| 53 | #else | ||
| 54 | 0 | return mkdir(path, (mode_t)mode); | |
| 55 | #endif | ||
| 56 | } | ||
| 57 | |||
| 58 | /* lstat(2). Windows has no symlink for it to decline to follow, so stat | ||
| 59 | * is the whole story there. */ | ||
| 60 | static int | ||
| 61 | 0 | arch_lstat(const char *path, struct stat *st) | |
| 62 | { | ||
| 63 | #ifdef _WIN32 | ||
| 64 | return stat(path, st); | ||
| 65 | #else | ||
| 66 | 0 | return lstat(path, st); | |
| 67 | #endif | ||
| 68 | } | ||
| 69 | |||
| 70 | static int | ||
| 71 | 0 | arch_is_symlink(const struct stat *st) | |
| 72 | { | ||
| 73 | #ifdef S_ISLNK | ||
| 74 | 0 | return S_ISLNK(st->st_mode) ? 1 : 0; | |
| 75 | #else | ||
| 76 | (void)st; | ||
| 77 | return 0; | ||
| 78 | #endif | ||
| 79 | } | ||
| 80 | |||
| 81 | /* symlink(2). Refused on Windows rather than faked: creating one needs a | ||
| 82 | * privilege an ordinary account does not have, and writing a copy of the | ||
| 83 | * target instead would extract a different archive than the one asked | ||
| 84 | * for. The caller warns and carries on to the next entry. */ | ||
| 85 | static int | ||
| 86 | 0 | arch_symlink(const char *target, const char *path) | |
| 87 | { | ||
| 88 | #ifdef _WIN32 | ||
| 89 | (void)target; | ||
| 90 | (void)path; | ||
| 91 | errno = ENOSYS; | ||
| 92 | return -1; | ||
| 93 | #else | ||
| 94 | 0 | return symlink(target, path); | |
| 95 | #endif | ||
| 96 | } | ||
| 97 | |||
| 98 | /* fchmod(2). Windows has no descriptor-based mode at all, and the only | ||
| 99 | * bit its by-path _chmod carries is read-only. Extraction must not fail | ||
| 100 | * over permission bits the filesystem cannot hold, so this succeeds | ||
| 101 | * without doing anything there. */ | ||
| 102 | static int | ||
| 103 | 0 | arch_fchmod(int fd, unsigned int mode) | |
| 104 | { | ||
| 105 | #ifdef _WIN32 | ||
| 106 | (void)fd; | ||
| 107 | (void)mode; | ||
| 108 | return 0; | ||
| 109 | #else | ||
| 110 | 0 | return fchmod(fd, (mode_t)mode); | |
| 111 | #endif | ||
| 112 | } | ||
| 113 | |||
| 114 | #endif /* ARCH_COMPAT_H */ |