Jed Estep | f1fc48c | 2015-12-15 16:04:53 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #if defined (TARGET_INJECT_FAULTS) |
| 18 | #include <map> |
| 19 | #endif |
| 20 | |
| 21 | #include <errno.h> |
| 22 | #include <fcntl.h> |
| 23 | #include <stdio.h> |
| 24 | #include <sys/stat.h> |
| 25 | #include <unistd.h> |
| 26 | |
| 27 | #include "ota_io.h" |
| 28 | |
| 29 | #if defined (TARGET_INJECT_FAULTS) |
| 30 | static std::map<int, const char*> FilenameCache; |
| 31 | static std::string FaultFileName = |
| 32 | #if defined (TARGET_READ_FAULT) |
| 33 | TARGET_READ_FAULT; |
| 34 | #elif defined (TARGET_WRITE_FAULT) |
| 35 | TARGET_WRITE_FAULT; |
| 36 | #elif defined (TARGET_FSYNC_FAULT) |
| 37 | TARGET_FSYNC_FAULT; |
| 38 | #endif // defined (TARGET_READ_FAULT) |
| 39 | #endif // defined (TARGET_INJECT_FAULTS) |
| 40 | |
| 41 | int ota_open(const char* path, int oflags) { |
| 42 | #if defined (TARGET_INJECT_FAULTS) |
| 43 | // Let the caller handle errors; we do not care if open succeeds or fails |
| 44 | int fd = open(path, oflags); |
| 45 | FilenameCache[fd] = path; |
| 46 | return fd; |
| 47 | #else |
| 48 | return open(path, oflags); |
| 49 | #endif |
| 50 | } |
| 51 | |
| 52 | int ota_open(const char* path, int oflags, mode_t mode) { |
| 53 | #if defined (TARGET_INJECT_FAULTS) |
| 54 | int fd = open(path, oflags, mode); |
| 55 | FilenameCache[fd] = path; |
| 56 | return fd; |
| 57 | #else |
| 58 | return open(path, oflags, mode); |
| 59 | #endif |
| 60 | } |
| 61 | |
| 62 | FILE* ota_fopen(const char* path, const char* mode) { |
| 63 | #if defined (TARGET_INJECT_FAULTS) |
| 64 | FILE* fh = fopen(path, mode); |
| 65 | FilenameCache[(intptr_t)fh] = path; |
| 66 | return fh; |
| 67 | #else |
| 68 | return fopen(path, mode); |
| 69 | #endif |
| 70 | } |
| 71 | |
| 72 | int ota_close(int fd) { |
| 73 | #if defined (TARGET_INJECT_FAULTS) |
| 74 | // descriptors can be reused, so make sure not to leave them in the cahce |
| 75 | FilenameCache.erase(fd); |
| 76 | #endif |
| 77 | return close(fd); |
| 78 | } |
| 79 | |
| 80 | int ota_fclose(FILE* fh) { |
| 81 | #if defined (TARGET_INJECT_FAULTS) |
| 82 | FilenameCache.erase((intptr_t)fh); |
| 83 | #endif |
| 84 | return fclose(fh); |
| 85 | } |
| 86 | |
| 87 | size_t ota_fread(void* ptr, size_t size, size_t nitems, FILE* stream) { |
| 88 | #if defined (TARGET_READ_FAULT) |
| 89 | if (FilenameCache.find((intptr_t)stream) != FilenameCache.end() |
| 90 | && FilenameCache[(intptr_t)stream] == FaultFileName) { |
| 91 | FaultFileName = ""; |
| 92 | errno = EIO; |
| 93 | return 0; |
| 94 | } else { |
| 95 | return fread(ptr, size, nitems, stream); |
| 96 | } |
| 97 | #else |
| 98 | return fread(ptr, size, nitems, stream); |
| 99 | #endif |
| 100 | } |
| 101 | |
| 102 | ssize_t ota_read(int fd, void* buf, size_t nbyte) { |
| 103 | #if defined (TARGET_READ_FAULT) |
| 104 | if (FilenameCache.find(fd) != FilenameCache.end() |
| 105 | && FilenameCache[fd] == FaultFileName) { |
| 106 | FaultFileName = ""; |
| 107 | errno = EIO; |
| 108 | return -1; |
| 109 | } else { |
| 110 | return read(fd, buf, nbyte); |
| 111 | } |
| 112 | #else |
| 113 | return read(fd, buf, nbyte); |
| 114 | #endif |
| 115 | } |
| 116 | |
| 117 | size_t ota_fwrite(const void* ptr, size_t size, size_t count, FILE* stream) { |
| 118 | #if defined (TARGET_WRITE_FAULT) |
| 119 | if (FilenameCache.find((intptr_t)stream) != FilenameCache.end() |
| 120 | && FilenameCache[(intptr_t)stream] == FaultFileName) { |
| 121 | FaultFileName = ""; |
| 122 | errno = EIO; |
| 123 | return 0; |
| 124 | } else { |
| 125 | return fwrite(ptr, size, count, stream); |
| 126 | } |
| 127 | #else |
| 128 | return fwrite(ptr, size, count, stream); |
| 129 | #endif |
| 130 | } |
| 131 | |
| 132 | ssize_t ota_write(int fd, const void* buf, size_t nbyte) { |
| 133 | #if defined (TARGET_WRITE_FAULT) |
| 134 | if (FilenameCache.find(fd) != FilenameCache.end() |
| 135 | && FilenameCache[fd] == FaultFileName) { |
| 136 | FaultFileName = ""; |
| 137 | errno = EIO; |
| 138 | return -1; |
| 139 | } else { |
| 140 | return write(fd, buf, nbyte); |
| 141 | } |
| 142 | #else |
| 143 | return write(fd, buf, nbyte); |
| 144 | #endif |
| 145 | } |
| 146 | |
| 147 | int ota_fsync(int fd) { |
| 148 | #if defined (TARGET_FSYNC_FAULT) |
| 149 | if (FilenameCache.find(fd) != FilenameCache.end() |
| 150 | && FilenameCache[fd] == FaultFileName) { |
| 151 | FaultFileName = ""; |
| 152 | errno = EIO; |
| 153 | return -1; |
| 154 | } else { |
| 155 | return fsync(fd); |
| 156 | } |
| 157 | #else |
| 158 | return fsync(fd); |
| 159 | #endif |
| 160 | } |