blob: 2d08fdef286236d4a802ebe9fcfdba673841d8f3 [file] [log] [blame]
Jed Estepa7b9a462015-12-15 16:04:53 -08001/*
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
Tao Baoce586882016-03-17 22:29:23 +000017#if defined (TARGET_INJECT_FAULTS)
Jed Estepa7b9a462015-12-15 16:04:53 -080018#include <map>
Tao Baoce586882016-03-17 22:29:23 +000019#endif
Jed Estepa7b9a462015-12-15 16:04:53 -080020
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
Tao Baoce586882016-03-17 22:29:23 +000029#if defined (TARGET_INJECT_FAULTS)
30static std::map<int, const char*> FilenameCache;
31static 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)
Jed Estepf1fc48c2015-12-15 16:04:53 -080040
Tianjie Xu3c62b672016-02-05 18:25:58 -080041bool have_eio_error = false;
Jed Estep39c1b5e2015-12-15 16:04:53 -080042
Jed Estepa7b9a462015-12-15 16:04:53 -080043int ota_open(const char* path, int oflags) {
Tao Baoce586882016-03-17 22:29:23 +000044#if defined (TARGET_INJECT_FAULTS)
Jed Estepa7b9a462015-12-15 16:04:53 -080045 // Let the caller handle errors; we do not care if open succeeds or fails
46 int fd = open(path, oflags);
Tao Baoce586882016-03-17 22:29:23 +000047 FilenameCache[fd] = path;
Jed Estepa7b9a462015-12-15 16:04:53 -080048 return fd;
Tao Baoce586882016-03-17 22:29:23 +000049#else
50 return open(path, oflags);
51#endif
Jed Estepa7b9a462015-12-15 16:04:53 -080052}
53
54int ota_open(const char* path, int oflags, mode_t mode) {
Tao Baoce586882016-03-17 22:29:23 +000055#if defined (TARGET_INJECT_FAULTS)
Jed Estepa7b9a462015-12-15 16:04:53 -080056 int fd = open(path, oflags, mode);
Tao Baoce586882016-03-17 22:29:23 +000057 FilenameCache[fd] = path;
58 return fd;
59#else
60 return open(path, oflags, mode);
61#endif
62}
Jed Estepa7b9a462015-12-15 16:04:53 -080063
64FILE* ota_fopen(const char* path, const char* mode) {
Tao Baoce586882016-03-17 22:29:23 +000065#if defined (TARGET_INJECT_FAULTS)
Jed Estepa7b9a462015-12-15 16:04:53 -080066 FILE* fh = fopen(path, mode);
Tao Baoce586882016-03-17 22:29:23 +000067 FilenameCache[(intptr_t)fh] = path;
Jed Estepa7b9a462015-12-15 16:04:53 -080068 return fh;
Tao Baoce586882016-03-17 22:29:23 +000069#else
70 return fopen(path, mode);
71#endif
Jed Estepa7b9a462015-12-15 16:04:53 -080072}
73
74int ota_close(int fd) {
Tao Baoce586882016-03-17 22:29:23 +000075#if defined (TARGET_INJECT_FAULTS)
76 // descriptors can be reused, so make sure not to leave them in the cahce
77 FilenameCache.erase(fd);
78#endif
Jed Estepa7b9a462015-12-15 16:04:53 -080079 return close(fd);
80}
81
82int ota_fclose(FILE* fh) {
Tao Baoce586882016-03-17 22:29:23 +000083#if defined (TARGET_INJECT_FAULTS)
84 FilenameCache.erase((intptr_t)fh);
85#endif
Jed Estepa7b9a462015-12-15 16:04:53 -080086 return fclose(fh);
87}
88
89size_t ota_fread(void* ptr, size_t size, size_t nitems, FILE* stream) {
Tao Baoce586882016-03-17 22:29:23 +000090#if defined (TARGET_READ_FAULT)
91 if (FilenameCache.find((intptr_t)stream) != FilenameCache.end()
92 && FilenameCache[(intptr_t)stream] == FaultFileName) {
93 FaultFileName = "";
94 errno = EIO;
95 have_eio_error = true;
96 return 0;
97 } else {
98 size_t status = fread(ptr, size, nitems, stream);
99 // If I/O error occurs, set the retry-update flag.
100 if (status != nitems && errno == EIO) {
Tianjie Xu3c62b672016-02-05 18:25:58 -0800101 have_eio_error = true;
Jed Estep39c1b5e2015-12-15 16:04:53 -0800102 }
Tao Baoce586882016-03-17 22:29:23 +0000103 return status;
Jed Estepa7b9a462015-12-15 16:04:53 -0800104 }
Tao Baoce586882016-03-17 22:29:23 +0000105#else
Tianjie Xu3c62b672016-02-05 18:25:58 -0800106 size_t status = fread(ptr, size, nitems, stream);
107 // If I/O error occurs, set the retry-update flag.
108 if (status != nitems && errno == EIO) {
109 have_eio_error = true;
110 }
111 return status;
Tao Baoce586882016-03-17 22:29:23 +0000112#endif
Jed Estepa7b9a462015-12-15 16:04:53 -0800113}
114
115ssize_t ota_read(int fd, void* buf, size_t nbyte) {
Tao Baoce586882016-03-17 22:29:23 +0000116#if defined (TARGET_READ_FAULT)
117 if (FilenameCache.find(fd) != FilenameCache.end()
118 && FilenameCache[fd] == FaultFileName) {
119 FaultFileName = "";
120 errno = EIO;
121 have_eio_error = true;
122 return -1;
123 } else {
124 ssize_t status = read(fd, buf, nbyte);
125 if (status == -1 && errno == EIO) {
Tianjie Xu3c62b672016-02-05 18:25:58 -0800126 have_eio_error = true;
Jed Estep39c1b5e2015-12-15 16:04:53 -0800127 }
Tao Baoce586882016-03-17 22:29:23 +0000128 return status;
Jed Estepa7b9a462015-12-15 16:04:53 -0800129 }
Tao Baoce586882016-03-17 22:29:23 +0000130#else
Tianjie Xu3c62b672016-02-05 18:25:58 -0800131 ssize_t status = read(fd, buf, nbyte);
132 if (status == -1 && errno == EIO) {
133 have_eio_error = true;
134 }
135 return status;
Tao Baoce586882016-03-17 22:29:23 +0000136#endif
Jed Estepa7b9a462015-12-15 16:04:53 -0800137}
138
139size_t ota_fwrite(const void* ptr, size_t size, size_t count, FILE* stream) {
Tao Baoce586882016-03-17 22:29:23 +0000140#if defined (TARGET_WRITE_FAULT)
141 if (FilenameCache.find((intptr_t)stream) != FilenameCache.end()
142 && FilenameCache[(intptr_t)stream] == FaultFileName) {
143 FaultFileName = "";
144 errno = EIO;
145 have_eio_error = true;
146 return 0;
147 } else {
148 size_t status = fwrite(ptr, size, count, stream);
149 if (status != count && errno == EIO) {
Tianjie Xu3c62b672016-02-05 18:25:58 -0800150 have_eio_error = true;
Jed Estep39c1b5e2015-12-15 16:04:53 -0800151 }
Tao Baoce586882016-03-17 22:29:23 +0000152 return status;
Jed Estepa7b9a462015-12-15 16:04:53 -0800153 }
Tao Baoce586882016-03-17 22:29:23 +0000154#else
Tianjie Xu3c62b672016-02-05 18:25:58 -0800155 size_t status = fwrite(ptr, size, count, stream);
156 if (status != count && errno == EIO) {
157 have_eio_error = true;
158 }
159 return status;
Tao Baoce586882016-03-17 22:29:23 +0000160#endif
Jed Estepa7b9a462015-12-15 16:04:53 -0800161}
162
163ssize_t ota_write(int fd, const void* buf, size_t nbyte) {
Tao Baoce586882016-03-17 22:29:23 +0000164#if defined (TARGET_WRITE_FAULT)
165 if (FilenameCache.find(fd) != FilenameCache.end()
166 && FilenameCache[fd] == FaultFileName) {
167 FaultFileName = "";
168 errno = EIO;
169 have_eio_error = true;
170 return -1;
171 } else {
172 ssize_t status = write(fd, buf, nbyte);
173 if (status == -1 && errno == EIO) {
Tianjie Xu3c62b672016-02-05 18:25:58 -0800174 have_eio_error = true;
Jed Estep39c1b5e2015-12-15 16:04:53 -0800175 }
Tao Baoce586882016-03-17 22:29:23 +0000176 return status;
Jed Estepa7b9a462015-12-15 16:04:53 -0800177 }
Tao Baoce586882016-03-17 22:29:23 +0000178#else
Tianjie Xu3c62b672016-02-05 18:25:58 -0800179 ssize_t status = write(fd, buf, nbyte);
180 if (status == -1 && errno == EIO) {
181 have_eio_error = true;
182 }
183 return status;
Tao Baoce586882016-03-17 22:29:23 +0000184#endif
Jed Estepa7b9a462015-12-15 16:04:53 -0800185}
186
187int ota_fsync(int fd) {
Tao Baoce586882016-03-17 22:29:23 +0000188#if defined (TARGET_FSYNC_FAULT)
189 if (FilenameCache.find(fd) != FilenameCache.end()
190 && FilenameCache[fd] == FaultFileName) {
191 FaultFileName = "";
192 errno = EIO;
193 have_eio_error = true;
194 return -1;
195 } else {
196 int status = fsync(fd);
197 if (status == -1 && errno == EIO) {
Tianjie Xu3c62b672016-02-05 18:25:58 -0800198 have_eio_error = true;
Jed Estep39c1b5e2015-12-15 16:04:53 -0800199 }
Tao Baoce586882016-03-17 22:29:23 +0000200 return status;
Jed Estepa7b9a462015-12-15 16:04:53 -0800201 }
Tao Baoce586882016-03-17 22:29:23 +0000202#else
Tianjie Xu3c62b672016-02-05 18:25:58 -0800203 int status = fsync(fd);
204 if (status == -1 && errno == EIO) {
205 have_eio_error = true;
206 }
207 return status;
Tao Baoce586882016-03-17 22:29:23 +0000208#endif
Jed Estepa7b9a462015-12-15 16:04:53 -0800209}