OLD | NEW |
(Empty) | |
| 1 // Copyright 2014 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 #include <errno.h> |
| 6 #include <sys/types.h> |
| 7 #include <sys/stat.h> |
| 8 #include <fcntl.h> |
| 9 |
| 10 #include "base/logging.h" |
| 11 #include "components/nacl/loader/nonsfi/abi_conversion.h" |
| 12 #include "components/nacl/loader/nonsfi/irt_interfaces.h" |
| 13 #include "native_client/src/trusted/service_runtime/include/sys/fcntl.h" |
| 14 #include "native_client/src/trusted/service_runtime/include/sys/stat.h" |
| 15 |
| 16 namespace nacl { |
| 17 namespace nonsfi { |
| 18 namespace { |
| 19 |
| 20 int IrtOpen( |
| 21 const char* pathname, int oflag, nacl_abi_mode_t cmode, int* new_fd) { |
| 22 // Some flags are ignored. Please see also NaClSysOpen |
| 23 // native_client/src/trusted/service_runtime/sys_filename.c. |
| 24 int flags; |
| 25 switch(oflag & NACL_ABI_O_ACCMODE) { |
| 26 case NACL_ABI_O_RDONLY: |
| 27 flags = O_RDONLY; |
| 28 break; |
| 29 case NACL_ABI_O_WRONLY: |
| 30 flags = O_WRONLY; |
| 31 break; |
| 32 case NACL_ABI_O_RDWR: |
| 33 flags = O_RDWR; |
| 34 break; |
| 35 default: |
| 36 LOG(WARNING) << "Unknown accmode: " << (oflag & NACL_ABI_O_ACCMODE); |
| 37 flags = O_RDONLY; |
| 38 } |
| 39 if (oflag & NACL_ABI_O_CREAT) |
| 40 flags |= O_CREAT; |
| 41 if (oflag & NACL_ABI_O_TRUNC) |
| 42 flags |= O_TRUNC; |
| 43 if (oflag & NACL_ABI_O_APPEND) |
| 44 flags |= O_APPEND; |
| 45 |
| 46 mode_t mode = 0; |
| 47 if (cmode & NACL_ABI_S_IRUSR) |
| 48 mode |= S_IRUSR; |
| 49 if (cmode & NACL_ABI_S_IWUSR) |
| 50 mode |= S_IWUSR; |
| 51 if (cmode & NACL_ABI_S_IXUSR) |
| 52 mode |= S_IXUSR; |
| 53 |
| 54 int fd = ::open(pathname, flags, mode); |
| 55 if (fd < 0) |
| 56 return errno; |
| 57 |
| 58 *new_fd = fd; |
| 59 return 0; |
| 60 } |
| 61 |
| 62 int IrtStat(const char* pathname, struct nacl_abi_stat* st) { |
| 63 struct stat host_st; |
| 64 if (::stat(pathname, &host_st) < 0) |
| 65 return errno; |
| 66 |
| 67 StatToNaClAbiStat(host_st, st); |
| 68 return 0; |
| 69 } |
| 70 |
| 71 } // namespace |
| 72 |
| 73 // Both open and stat should use nacl_abi_X for their argument types, rather |
| 74 // than host type, such as struct stat or mode_t. However, the definition |
| 75 // of nacl_irt_filename uses host types, so here we need to cast them. |
| 76 const nacl_irt_filename kIrtFileName = { |
| 77 reinterpret_cast<int(*)(const char*, int, mode_t, int*)>(IrtOpen), |
| 78 reinterpret_cast<int(*)(const char*, struct stat*)>(IrtStat), |
| 79 }; |
| 80 |
| 81 } // namespace nonsfi |
| 82 } // namespace nacl |
OLD | NEW |