blob: 2efd3004ac3984214ff4cb3848b70bc500338f83 [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
Jed Estepa7b9a462015-12-15 16:04:53 -080017#include <map>
Jed Estepa7b9a462015-12-15 16:04:53 -080018
19#include <errno.h>
20#include <fcntl.h>
21#include <stdio.h>
22#include <sys/stat.h>
23#include <unistd.h>
24
Jed Estepff6df892015-12-15 16:04:53 -080025#include "config.h"
Jed Estepa7b9a462015-12-15 16:04:53 -080026#include "ota_io.h"
27
Jed Estepff6df892015-12-15 16:04:53 -080028static std::map<intptr_t, const char*> filename_cache;
29static std::string read_fault_file_name = "";
30static std::string write_fault_file_name = "";
31static std::string fsync_fault_file_name = "";
32
Chih-Hung Hsieh23abfd32016-07-27 10:19:47 -070033static bool get_hit_file(const char* cached_path, const std::string& ffn) {
Jed Estepff6df892015-12-15 16:04:53 -080034 return should_hit_cache()
35 ? !strncmp(cached_path, OTAIO_CACHE_FNAME, strlen(cached_path))
36 : !strncmp(cached_path, ffn.c_str(), strlen(cached_path));
37}
38
39void ota_set_fault_files() {
40 if (should_fault_inject(OTAIO_READ)) {
41 read_fault_file_name = fault_fname(OTAIO_READ);
42 }
43 if (should_fault_inject(OTAIO_WRITE)) {
44 write_fault_file_name = fault_fname(OTAIO_WRITE);
45 }
46 if (should_fault_inject(OTAIO_FSYNC)) {
47 fsync_fault_file_name = fault_fname(OTAIO_FSYNC);
48 }
49}
Jed Estepf1fc48c2015-12-15 16:04:53 -080050
Tianjie Xu3c62b672016-02-05 18:25:58 -080051bool have_eio_error = false;
Jed Estep39c1b5e2015-12-15 16:04:53 -080052
Jed Estepa7b9a462015-12-15 16:04:53 -080053int ota_open(const char* path, int oflags) {
Jed Estepa7b9a462015-12-15 16:04:53 -080054 // Let the caller handle errors; we do not care if open succeeds or fails
55 int fd = open(path, oflags);
Jed Estepff6df892015-12-15 16:04:53 -080056 filename_cache[fd] = path;
Jed Estepa7b9a462015-12-15 16:04:53 -080057 return fd;
Jed Estepa7b9a462015-12-15 16:04:53 -080058}
59
60int ota_open(const char* path, int oflags, mode_t mode) {
Jed Estepa7b9a462015-12-15 16:04:53 -080061 int fd = open(path, oflags, mode);
Jed Estepff6df892015-12-15 16:04:53 -080062 filename_cache[fd] = path;
63 return fd; }
Jed Estepa7b9a462015-12-15 16:04:53 -080064
65FILE* ota_fopen(const char* path, const char* mode) {
Jed Estepa7b9a462015-12-15 16:04:53 -080066 FILE* fh = fopen(path, mode);
Jed Estepff6df892015-12-15 16:04:53 -080067 filename_cache[(intptr_t)fh] = path;
Jed Estepa7b9a462015-12-15 16:04:53 -080068 return fh;
Jed Estepa7b9a462015-12-15 16:04:53 -080069}
70
71int ota_close(int fd) {
Jed Estepff6df892015-12-15 16:04:53 -080072 // descriptors can be reused, so make sure not to leave them in the cache
73 filename_cache.erase(fd);
Jed Estepa7b9a462015-12-15 16:04:53 -080074 return close(fd);
75}
76
77int ota_fclose(FILE* fh) {
Jed Estepff6df892015-12-15 16:04:53 -080078 filename_cache.erase((intptr_t)fh);
Jed Estepa7b9a462015-12-15 16:04:53 -080079 return fclose(fh);
80}
81
82size_t ota_fread(void* ptr, size_t size, size_t nitems, FILE* stream) {
Jed Estepff6df892015-12-15 16:04:53 -080083 if (should_fault_inject(OTAIO_READ)) {
84 auto cached = filename_cache.find((intptr_t)stream);
85 const char* cached_path = cached->second;
86 if (cached != filename_cache.end() &&
87 get_hit_file(cached_path, read_fault_file_name)) {
88 read_fault_file_name = "";
89 errno = EIO;
Tianjie Xu3c62b672016-02-05 18:25:58 -080090 have_eio_error = true;
Jed Estepff6df892015-12-15 16:04:53 -080091 return 0;
Jed Estep39c1b5e2015-12-15 16:04:53 -080092 }
Jed Estepa7b9a462015-12-15 16:04:53 -080093 }
Tianjie Xu3c62b672016-02-05 18:25:58 -080094 size_t status = fread(ptr, size, nitems, stream);
95 // If I/O error occurs, set the retry-update flag.
96 if (status != nitems && errno == EIO) {
97 have_eio_error = true;
98 }
99 return status;
Jed Estepa7b9a462015-12-15 16:04:53 -0800100}
101
102ssize_t ota_read(int fd, void* buf, size_t nbyte) {
Jed Estepff6df892015-12-15 16:04:53 -0800103 if (should_fault_inject(OTAIO_READ)) {
104 auto cached = filename_cache.find(fd);
105 const char* cached_path = cached->second;
106 if (cached != filename_cache.end()
107 && get_hit_file(cached_path, read_fault_file_name)) {
108 read_fault_file_name = "";
109 errno = EIO;
Tianjie Xu3c62b672016-02-05 18:25:58 -0800110 have_eio_error = true;
Jed Estepff6df892015-12-15 16:04:53 -0800111 return -1;
Jed Estep39c1b5e2015-12-15 16:04:53 -0800112 }
Jed Estepa7b9a462015-12-15 16:04:53 -0800113 }
Tianjie Xu3c62b672016-02-05 18:25:58 -0800114 ssize_t status = read(fd, buf, nbyte);
115 if (status == -1 && errno == EIO) {
116 have_eio_error = true;
117 }
118 return status;
Jed Estepa7b9a462015-12-15 16:04:53 -0800119}
120
121size_t ota_fwrite(const void* ptr, size_t size, size_t count, FILE* stream) {
Jed Estepff6df892015-12-15 16:04:53 -0800122 if (should_fault_inject(OTAIO_WRITE)) {
123 auto cached = filename_cache.find((intptr_t)stream);
124 const char* cached_path = cached->second;
125 if (cached != filename_cache.end() &&
126 get_hit_file(cached_path, write_fault_file_name)) {
127 write_fault_file_name = "";
128 errno = EIO;
Tianjie Xu3c62b672016-02-05 18:25:58 -0800129 have_eio_error = true;
Jed Estepff6df892015-12-15 16:04:53 -0800130 return 0;
Jed Estep39c1b5e2015-12-15 16:04:53 -0800131 }
Jed Estepa7b9a462015-12-15 16:04:53 -0800132 }
Tianjie Xu3c62b672016-02-05 18:25:58 -0800133 size_t status = fwrite(ptr, size, count, stream);
134 if (status != count && errno == EIO) {
135 have_eio_error = true;
136 }
137 return status;
Jed Estepa7b9a462015-12-15 16:04:53 -0800138}
139
140ssize_t ota_write(int fd, const void* buf, size_t nbyte) {
Jed Estepff6df892015-12-15 16:04:53 -0800141 if (should_fault_inject(OTAIO_WRITE)) {
142 auto cached = filename_cache.find(fd);
143 const char* cached_path = cached->second;
144 if (cached != filename_cache.end() &&
145 get_hit_file(cached_path, write_fault_file_name)) {
146 write_fault_file_name = "";
147 errno = EIO;
Tianjie Xu3c62b672016-02-05 18:25:58 -0800148 have_eio_error = true;
Jed Estepff6df892015-12-15 16:04:53 -0800149 return -1;
Jed Estep39c1b5e2015-12-15 16:04:53 -0800150 }
Jed Estepa7b9a462015-12-15 16:04:53 -0800151 }
Tianjie Xu3c62b672016-02-05 18:25:58 -0800152 ssize_t status = write(fd, buf, nbyte);
153 if (status == -1 && errno == EIO) {
154 have_eio_error = true;
155 }
156 return status;
Jed Estepa7b9a462015-12-15 16:04:53 -0800157}
158
159int ota_fsync(int fd) {
Jed Estepff6df892015-12-15 16:04:53 -0800160 if (should_fault_inject(OTAIO_FSYNC)) {
161 auto cached = filename_cache.find(fd);
162 const char* cached_path = cached->second;
163 if (cached != filename_cache.end() &&
164 get_hit_file(cached_path, fsync_fault_file_name)) {
165 fsync_fault_file_name = "";
166 errno = EIO;
Tianjie Xu3c62b672016-02-05 18:25:58 -0800167 have_eio_error = true;
Jed Estepff6df892015-12-15 16:04:53 -0800168 return -1;
Jed Estep39c1b5e2015-12-15 16:04:53 -0800169 }
Jed Estepa7b9a462015-12-15 16:04:53 -0800170 }
Tianjie Xu3c62b672016-02-05 18:25:58 -0800171 int status = fsync(fd);
172 if (status == -1 && errno == EIO) {
173 have_eio_error = true;
174 }
175 return status;
Jed Estepa7b9a462015-12-15 16:04:53 -0800176}
Jed Estepff6df892015-12-15 16:04:53 -0800177