blob: 9434ebea3553206fe832a8535fae76a715d52b6e [file] [log] [blame]
Jed Estepf1fc48c2015-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 Baob1e41002016-03-17 22:29:23 +000017#if defined (TARGET_INJECT_FAULTS)
Jed Estepf1fc48c2015-12-15 16:04:53 -080018#include <map>
Tao Baob1e41002016-03-17 22:29:23 +000019#endif
Jed Estepf1fc48c2015-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 Baob1e41002016-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 Xufa12b972016-02-05 18:25:58 -080041bool have_eio_error = false;
42
Jed Estepf1fc48c2015-12-15 16:04:53 -080043int ota_open(const char* path, int oflags) {
Tao Baob1e41002016-03-17 22:29:23 +000044#if defined (TARGET_INJECT_FAULTS)
Jed Estepf1fc48c2015-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 Baob1e41002016-03-17 22:29:23 +000047 FilenameCache[fd] = path;
Jed Estepf1fc48c2015-12-15 16:04:53 -080048 return fd;
Tao Baob1e41002016-03-17 22:29:23 +000049#else
50 return open(path, oflags);
51#endif
Jed Estepf1fc48c2015-12-15 16:04:53 -080052}
53
54int ota_open(const char* path, int oflags, mode_t mode) {
Tao Baob1e41002016-03-17 22:29:23 +000055#if defined (TARGET_INJECT_FAULTS)
Jed Estepf1fc48c2015-12-15 16:04:53 -080056 int fd = open(path, oflags, mode);
Tao Baob1e41002016-03-17 22:29:23 +000057 FilenameCache[fd] = path;
58 return fd;
59#else
60 return open(path, oflags, mode);
61#endif
62}
Jed Estepf1fc48c2015-12-15 16:04:53 -080063
64FILE* ota_fopen(const char* path, const char* mode) {
Tao Baob1e41002016-03-17 22:29:23 +000065#if defined (TARGET_INJECT_FAULTS)
Jed Estepf1fc48c2015-12-15 16:04:53 -080066 FILE* fh = fopen(path, mode);
Tao Baob1e41002016-03-17 22:29:23 +000067 FilenameCache[(intptr_t)fh] = path;
Jed Estepf1fc48c2015-12-15 16:04:53 -080068 return fh;
Tao Baob1e41002016-03-17 22:29:23 +000069#else
70 return fopen(path, mode);
71#endif
Jed Estepf1fc48c2015-12-15 16:04:53 -080072}
73
74int ota_close(int fd) {
Tao Baob1e41002016-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 Estepf1fc48c2015-12-15 16:04:53 -080079 return close(fd);
80}
81
82int ota_fclose(FILE* fh) {
Tao Baob1e41002016-03-17 22:29:23 +000083#if defined (TARGET_INJECT_FAULTS)
84 FilenameCache.erase((intptr_t)fh);
85#endif
Jed Estepf1fc48c2015-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 Baob1e41002016-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 Xufa12b972016-02-05 18:25:58 -0800101 have_eio_error = true;
102 }
Tao Baob1e41002016-03-17 22:29:23 +0000103 return status;
Jed Estepf1fc48c2015-12-15 16:04:53 -0800104 }
Tao Baob1e41002016-03-17 22:29:23 +0000105#else
Tianjie Xufa12b972016-02-05 18:25:58 -0800106 size_t status = fread(ptr, size, nitems, stream);
107 if (status != nitems && errno == EIO) {
108 have_eio_error = true;
109 }
110 return status;
Tao Baob1e41002016-03-17 22:29:23 +0000111#endif
Jed Estepf1fc48c2015-12-15 16:04:53 -0800112}
113
114ssize_t ota_read(int fd, void* buf, size_t nbyte) {
Tao Baob1e41002016-03-17 22:29:23 +0000115#if defined (TARGET_READ_FAULT)
116 if (FilenameCache.find(fd) != FilenameCache.end()
117 && FilenameCache[fd] == FaultFileName) {
118 FaultFileName = "";
119 errno = EIO;
120 have_eio_error = true;
121 return -1;
122 } else {
123 ssize_t status = read(fd, buf, nbyte);
124 if (status == -1 && errno == EIO) {
Tianjie Xufa12b972016-02-05 18:25:58 -0800125 have_eio_error = true;
126 }
Tao Baob1e41002016-03-17 22:29:23 +0000127 return status;
Jed Estepf1fc48c2015-12-15 16:04:53 -0800128 }
Tao Baob1e41002016-03-17 22:29:23 +0000129#else
Tianjie Xufa12b972016-02-05 18:25:58 -0800130 ssize_t status = read(fd, buf, nbyte);
131 if (status == -1 && errno == EIO) {
132 have_eio_error = true;
133 }
134 return status;
Tao Baob1e41002016-03-17 22:29:23 +0000135#endif
Jed Estepf1fc48c2015-12-15 16:04:53 -0800136}
137
138size_t ota_fwrite(const void* ptr, size_t size, size_t count, FILE* stream) {
Tao Baob1e41002016-03-17 22:29:23 +0000139#if defined (TARGET_WRITE_FAULT)
140 if (FilenameCache.find((intptr_t)stream) != FilenameCache.end()
141 && FilenameCache[(intptr_t)stream] == FaultFileName) {
142 FaultFileName = "";
143 errno = EIO;
144 have_eio_error = true;
145 return 0;
146 } else {
147 size_t status = fwrite(ptr, size, count, stream);
148 if (status != count && errno == EIO) {
Tianjie Xufa12b972016-02-05 18:25:58 -0800149 have_eio_error = true;
150 }
Tao Baob1e41002016-03-17 22:29:23 +0000151 return status;
Jed Estepf1fc48c2015-12-15 16:04:53 -0800152 }
Tao Baob1e41002016-03-17 22:29:23 +0000153#else
Tianjie Xufa12b972016-02-05 18:25:58 -0800154 size_t status = fwrite(ptr, size, count, stream);
155 if (status != count && errno == EIO) {
156 have_eio_error = true;
157 }
158 return status;
Tao Baob1e41002016-03-17 22:29:23 +0000159#endif
Jed Estepf1fc48c2015-12-15 16:04:53 -0800160}
161
162ssize_t ota_write(int fd, const void* buf, size_t nbyte) {
Tao Baob1e41002016-03-17 22:29:23 +0000163#if defined (TARGET_WRITE_FAULT)
164 if (FilenameCache.find(fd) != FilenameCache.end()
165 && FilenameCache[fd] == FaultFileName) {
166 FaultFileName = "";
167 errno = EIO;
168 have_eio_error = true;
169 return -1;
170 } else {
171 ssize_t status = write(fd, buf, nbyte);
172 if (status == -1 && errno == EIO) {
Tianjie Xufa12b972016-02-05 18:25:58 -0800173 have_eio_error = true;
174 }
Tao Baob1e41002016-03-17 22:29:23 +0000175 return status;
Jed Estepf1fc48c2015-12-15 16:04:53 -0800176 }
Tao Baob1e41002016-03-17 22:29:23 +0000177#else
Tianjie Xufa12b972016-02-05 18:25:58 -0800178 ssize_t status = write(fd, buf, nbyte);
179 if (status == -1 && errno == EIO) {
180 have_eio_error = true;
181 }
182 return status;
Tao Baob1e41002016-03-17 22:29:23 +0000183#endif
Jed Estepf1fc48c2015-12-15 16:04:53 -0800184}
185
186int ota_fsync(int fd) {
Tao Baob1e41002016-03-17 22:29:23 +0000187#if defined (TARGET_FSYNC_FAULT)
188 if (FilenameCache.find(fd) != FilenameCache.end()
189 && FilenameCache[fd] == FaultFileName) {
190 FaultFileName = "";
191 errno = EIO;
192 have_eio_error = true;
193 return -1;
194 } else {
195 int status = fsync(fd);
196 if (status == -1 && errno == EIO) {
Tianjie Xufa12b972016-02-05 18:25:58 -0800197 have_eio_error = true;
198 }
Tao Baob1e41002016-03-17 22:29:23 +0000199 return status;
Jed Estepf1fc48c2015-12-15 16:04:53 -0800200 }
Tao Baob1e41002016-03-17 22:29:23 +0000201#else
Tianjie Xufa12b972016-02-05 18:25:58 -0800202 int status = fsync(fd);
203 if (status == -1 && errno == EIO) {
204 have_eio_error = true;
205 }
206 return status;
Tao Baob1e41002016-03-17 22:29:23 +0000207#endif
Jed Estepf1fc48c2015-12-15 16:04:53 -0800208}