blob: a378040889d5f0f74678f45e0b1e270549f0ea5b [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
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)
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)
40
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) {
44#if defined (TARGET_INJECT_FAULTS)
45 // Let the caller handle errors; we do not care if open succeeds or fails
46 int fd = open(path, oflags);
47 FilenameCache[fd] = path;
48 return fd;
49#else
50 return open(path, oflags);
51#endif
52}
53
54int ota_open(const char* path, int oflags, mode_t mode) {
55#if defined (TARGET_INJECT_FAULTS)
56 int fd = open(path, oflags, mode);
57 FilenameCache[fd] = path;
58 return fd;
59#else
60 return open(path, oflags, mode);
61#endif
62}
63
64FILE* ota_fopen(const char* path, const char* mode) {
65#if defined (TARGET_INJECT_FAULTS)
66 FILE* fh = fopen(path, mode);
67 FilenameCache[(intptr_t)fh] = path;
68 return fh;
69#else
70 return fopen(path, mode);
71#endif
72}
73
74int ota_close(int fd) {
75#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
79 return close(fd);
80}
81
82int ota_fclose(FILE* fh) {
83#if defined (TARGET_INJECT_FAULTS)
84 FilenameCache.erase((intptr_t)fh);
85#endif
86 return fclose(fh);
87}
88
89size_t ota_fread(void* ptr, size_t size, size_t nitems, FILE* stream) {
90#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;
Tianjie Xufa12b972016-02-05 18:25:58 -080095 have_eio_error = true;
Jed Estepf1fc48c2015-12-15 16:04:53 -080096 return 0;
97 } else {
Tianjie Xufa12b972016-02-05 18:25:58 -080098 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) {
101 have_eio_error = true;
102 }
103 return status;
Jed Estepf1fc48c2015-12-15 16:04:53 -0800104 }
105#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;
Jed Estepf1fc48c2015-12-15 16:04:53 -0800111#endif
112}
113
114ssize_t ota_read(int fd, void* buf, size_t nbyte) {
115#if defined (TARGET_READ_FAULT)
116 if (FilenameCache.find(fd) != FilenameCache.end()
117 && FilenameCache[fd] == FaultFileName) {
118 FaultFileName = "";
119 errno = EIO;
Tianjie Xufa12b972016-02-05 18:25:58 -0800120 have_eio_error = true;
Jed Estepf1fc48c2015-12-15 16:04:53 -0800121 return -1;
122 } else {
Tianjie Xufa12b972016-02-05 18:25:58 -0800123 ssize_t status = read(fd, buf, nbyte);
124 if (status == -1 && errno == EIO) {
125 have_eio_error = true;
126 }
127 return status;
Jed Estepf1fc48c2015-12-15 16:04:53 -0800128 }
129#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;
Jed Estepf1fc48c2015-12-15 16:04:53 -0800135#endif
136}
137
138size_t ota_fwrite(const void* ptr, size_t size, size_t count, FILE* stream) {
139#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;
Tianjie Xufa12b972016-02-05 18:25:58 -0800144 have_eio_error = true;
Jed Estepf1fc48c2015-12-15 16:04:53 -0800145 return 0;
146 } else {
Tianjie Xufa12b972016-02-05 18:25:58 -0800147 size_t status = fwrite(ptr, size, count, stream);
148 if (status != count && errno == EIO) {
149 have_eio_error = true;
150 }
151 return status;
Jed Estepf1fc48c2015-12-15 16:04:53 -0800152 }
153#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;
Jed Estepf1fc48c2015-12-15 16:04:53 -0800159#endif
160}
161
162ssize_t ota_write(int fd, const void* buf, size_t nbyte) {
163#if defined (TARGET_WRITE_FAULT)
164 if (FilenameCache.find(fd) != FilenameCache.end()
165 && FilenameCache[fd] == FaultFileName) {
166 FaultFileName = "";
167 errno = EIO;
Tianjie Xufa12b972016-02-05 18:25:58 -0800168 have_eio_error = true;
Jed Estepf1fc48c2015-12-15 16:04:53 -0800169 return -1;
170 } else {
Tianjie Xufa12b972016-02-05 18:25:58 -0800171 ssize_t status = write(fd, buf, nbyte);
172 if (status == -1 && errno == EIO) {
173 have_eio_error = true;
174 }
175 return status;
Jed Estepf1fc48c2015-12-15 16:04:53 -0800176 }
177#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;
Jed Estepf1fc48c2015-12-15 16:04:53 -0800183#endif
184}
185
186int ota_fsync(int fd) {
187#if defined (TARGET_FSYNC_FAULT)
188 if (FilenameCache.find(fd) != FilenameCache.end()
189 && FilenameCache[fd] == FaultFileName) {
190 FaultFileName = "";
191 errno = EIO;
192 return -1;
Tianjie Xufa12b972016-02-05 18:25:58 -0800193 have_eio_error = true;
Jed Estepf1fc48c2015-12-15 16:04:53 -0800194 } else {
Tianjie Xufa12b972016-02-05 18:25:58 -0800195 int status = fsync(fd);
196 if (status == -1 && errno == EIO) {
197 have_eio_error = true;
198 }
199 return status;
Jed Estepf1fc48c2015-12-15 16:04:53 -0800200 }
201#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;
Jed Estepf1fc48c2015-12-15 16:04:53 -0800207#endif
208}