Home | History | Annotate | Download | only in nacl_io
      1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 // The entire file is wrapped in this #if. We do this so this .cc file can be
      6 // compiled, even on a non-Windows build.
      7 #if defined(WIN32)
      8 
      9 #include "nacl_io/kernel_wrap.h"
     10 #include <errno.h>
     11 #include <fcntl.h>
     12 #include <stdarg.h>
     13 #include <stdlib.h>
     14 #include <string.h>
     15 #include <sys/types.h>  // This must be included before <sys/stat.h>.
     16 #include <sys/stat.h>
     17 #include "nacl_io/kernel_intercept.h"
     18 
     19 #include <windows.h>
     20 
     21 namespace {
     22 
     23 template <typename SrcStat, typename DstStat>
     24 void CopyStat(const SrcStat* src, DstStat* dst) {
     25   memset(dst, 0, sizeof(DstStat));
     26   dst->st_dev = src->st_dev;
     27   dst->st_ino = src->st_ino;
     28   dst->st_mode = src->st_mode;
     29   dst->st_nlink = src->st_nlink;
     30   dst->st_uid = src->st_uid;
     31   dst->st_gid = src->st_gid;
     32   dst->st_rdev = src->st_rdev;
     33   dst->st_size = src->st_size;
     34   dst->st_atime = src->st_atime;
     35   dst->st_mtime = src->st_mtime;
     36   dst->st_ctime = src->st_ctime;
     37 }
     38 
     39 }  // namespace
     40 
     41 EXTERN_C_BEGIN
     42 
     43 // This needs to be included because it is defined in read.c, which we wish to
     44 // override. Define with dummy values for now... though this seems like it will
     45 // break ftelli64/fgetpos/fstream.
     46 char _lookuptrailbytes[256] = {0};
     47 
     48 int _access(const char* path, int amode) {
     49   return ki_access(path, amode);
     50 }
     51 
     52 int _chdir(const char* path) {
     53   return ki_chdir(path);
     54 }
     55 
     56 int _chmod(const char* path, mode_t mode) {
     57   return ki_chmod(path, mode);
     58 }
     59 
     60 int _close(int fd) {
     61   return ki_close(fd);
     62 }
     63 
     64 int _close_nolock(int fd) {
     65   return ki_close(fd);
     66 }
     67 
     68 int _dup(int oldfd) {
     69   return ki_dup(oldfd);
     70 }
     71 
     72 int _dup2(int oldfd, int newfd) {
     73   return ki_dup2(oldfd, newfd);
     74 }
     75 
     76 int _fstat32(int fd, struct _stat32* buf) {
     77   struct stat ki_buf;
     78   int res = ki_fstat(fd, &ki_buf);
     79   CopyStat(&ki_buf, buf);
     80   return res;
     81 }
     82 
     83 int _fstat64(int fd, struct _stat64* buf) {
     84   struct stat ki_buf;
     85   int res = ki_fstat(fd, &ki_buf);
     86   CopyStat(&ki_buf, buf);
     87   return res;
     88 }
     89 
     90 int _fstat32i64(int fd, struct _stat32i64* buf) {
     91   struct stat ki_buf;
     92   int res = ki_fstat(fd, &ki_buf);
     93   CopyStat(&ki_buf, buf);
     94   return res;
     95 }
     96 
     97 int _fstat64i32(int fd, struct _stat64i32* buf) {
     98   struct stat ki_buf;
     99   int res = ki_fstat(fd, &ki_buf);
    100   CopyStat(&ki_buf, buf);
    101   return res;
    102 }
    103 
    104 char* _getcwd(char* buf, int size) {
    105   // If size is 0, allocate as much as we need.
    106   if (size == 0) {
    107     char stack_buf[MAX_PATH + 1];
    108     if (!ki_getcwd(stack_buf, MAX_PATH))
    109       return NULL;
    110     size = strlen(stack_buf) + 1;
    111   }
    112   // Allocate the buffer if needed
    113   if (buf == NULL) {
    114     buf = static_cast<char*>(malloc(size));
    115   }
    116   return ki_getcwd(buf, size);
    117 }
    118 
    119 int _isatty(int fd) {
    120   return ki_isatty(fd);
    121 }
    122 
    123 off_t _lseek(int fd, off_t offset, int whence) {
    124   return ki_lseek(fd, offset, whence);
    125 }
    126 
    127 int _mkdir(const char* path) {
    128   return ki_mkdir(path, 0777);
    129 }
    130 
    131 int _open(const char* path, int oflag, ...) {
    132 #if 0
    133   // TODO(binji): ki_open should use the pmode parameter. When it does, this
    134   // will be necessary to add in.
    135   va_list list;
    136   int pmode = 0;
    137   if (oflag & _O_CREAT) {
    138     va_start(list, oflag);
    139     pmode = va_arg(list, int);
    140     va_end(list);
    141   }
    142 #endif
    143   return ki_open(path, oflag);
    144 }
    145 
    146 int _sopen(const char* path, int oflag, int shflag) {
    147   return ki_open(path, oflag);
    148 }
    149 
    150 errno_t _sopen_s(int* pfh, const char* path, int oflag, int shflag, int pmode) {
    151   *pfh = ki_open(path, oflag);
    152   return (*pfh < 0) ? errno : 0;
    153 }
    154 
    155 int _read(int fd, void* buf, size_t nbyte) {
    156   if (!ki_is_initialized())
    157     return 0;
    158 
    159   return ki_read(fd, buf, nbyte);
    160 }
    161 
    162 int _read_nolock(int fd, void* buf, size_t nbyte) {
    163   if (!ki_is_initialized())
    164     return 0;
    165 
    166   return ki_read(fd, buf, nbyte);
    167 }
    168 
    169 int _rmdir(const char* path) {
    170   return ki_rmdir(path);
    171 }
    172 
    173 int setenv(const char* name, const char* value, int overwrite) {
    174   if (0 == overwrite && NULL != getenv(name)) {
    175     return 0;
    176   }
    177   errno_t result = _putenv_s(name, value);
    178   if (result != 0) {
    179     errno = result;
    180     return -1;
    181   } else {
    182     return 0;
    183   }
    184 }
    185 
    186 int _stat32(const char* path, struct _stat32* buf) {
    187   struct stat ki_buf;
    188   int res = ki_stat(path, &ki_buf);
    189   CopyStat(&ki_buf, buf);
    190   return res;
    191 }
    192 
    193 int _stat64(const char* path, struct _stat64* buf) {
    194   struct stat ki_buf;
    195   int res = ki_stat(path, &ki_buf);
    196   CopyStat(&ki_buf, buf);
    197   return res;
    198 }
    199 
    200 int _stat64i32(const char* path, struct _stat64i32* buf) {
    201   struct stat ki_buf;
    202   int res = ki_stat(path, &ki_buf);
    203   CopyStat(&ki_buf, buf);
    204   return res;
    205 }
    206 
    207 int _stat32i64(const char* path, struct _stat32i64* buf) {
    208   struct stat ki_buf;
    209   int res = ki_stat(path, &ki_buf);
    210   CopyStat(&ki_buf, buf);
    211   return res;
    212 }
    213 
    214 int _unlink(const char* path) {
    215   return ki_unlink(path);
    216 }
    217 
    218 int _utime(const char* filename, const struct utimbuf* times) {
    219   return ki_utime(filename, times);
    220 }
    221 
    222 int _write(int fd, const void* buf, size_t nbyte) {
    223   if (!ki_is_initialized())
    224     return 0;
    225 
    226   return ki_write(fd, buf, nbyte);
    227 }
    228 
    229 // Do nothing for Windows, we replace the library at link time.
    230 void kernel_wrap_init() {
    231 }
    232 
    233 void kernel_wrap_uninit() {
    234 }
    235 
    236 EXTERN_C_END
    237 
    238 #endif   // defined(WIN32)
    239