Jed Estep | 39c1b5e | 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 | #include <map> |
| 18 | #include <string> |
| 19 | |
| 20 | #include <stdio.h> |
| 21 | #include <unistd.h> |
| 22 | |
Jed Estep | e326b22 | 2016-03-22 15:25:27 -0700 | [diff] [blame] | 23 | #include <android-base/stringprintf.h> |
| 24 | |
Jed Estep | 39c1b5e | 2015-12-15 16:04:53 -0800 | [diff] [blame] | 25 | #include "minzip/Zip.h" |
| 26 | #include "config.h" |
| 27 | #include "ota_io.h" |
| 28 | |
| 29 | #define OTAIO_MAX_FNAME_SIZE 128 |
| 30 | |
| 31 | static ZipArchive* archive; |
Jed Estep | e326b22 | 2016-03-22 15:25:27 -0700 | [diff] [blame] | 32 | static std::map<std::string, bool> should_inject_cache; |
Jed Estep | 39c1b5e | 2015-12-15 16:04:53 -0800 | [diff] [blame] | 33 | |
Jed Estep | e326b22 | 2016-03-22 15:25:27 -0700 | [diff] [blame] | 34 | static std::string get_type_path(const char* io_type) { |
| 35 | return android::base::StringPrintf("%s/%s", OTAIO_BASE_DIR, io_type); |
Jed Estep | 39c1b5e | 2015-12-15 16:04:53 -0800 | [diff] [blame] | 36 | } |
| 37 | |
| 38 | void ota_io_init(ZipArchive* za) { |
| 39 | archive = za; |
| 40 | ota_set_fault_files(); |
| 41 | } |
| 42 | |
| 43 | bool should_fault_inject(const char* io_type) { |
Jed Estep | e326b22 | 2016-03-22 15:25:27 -0700 | [diff] [blame] | 44 | // archive will be NULL if we used an entry point other |
| 45 | // than updater/updater.cpp:main |
| 46 | if (archive == NULL) { |
| 47 | return false; |
Jed Estep | 39c1b5e | 2015-12-15 16:04:53 -0800 | [diff] [blame] | 48 | } |
Jed Estep | e326b22 | 2016-03-22 15:25:27 -0700 | [diff] [blame] | 49 | const std::string type_path = get_type_path(io_type); |
| 50 | if (should_inject_cache.find(type_path) != should_inject_cache.end()) { |
| 51 | return should_inject_cache[type_path]; |
| 52 | } |
| 53 | const ZipEntry* entry = mzFindZipEntry(archive, type_path.c_str()); |
Jed Estep | 39c1b5e | 2015-12-15 16:04:53 -0800 | [diff] [blame] | 54 | should_inject_cache[type_path] = entry != nullptr; |
Jed Estep | 39c1b5e | 2015-12-15 16:04:53 -0800 | [diff] [blame] | 55 | return entry != NULL; |
| 56 | } |
| 57 | |
| 58 | bool should_hit_cache() { |
| 59 | return should_fault_inject(OTAIO_CACHE); |
| 60 | } |
| 61 | |
| 62 | std::string fault_fname(const char* io_type) { |
Jed Estep | e326b22 | 2016-03-22 15:25:27 -0700 | [diff] [blame] | 63 | std::string type_path = get_type_path(io_type); |
| 64 | std::string fname; |
| 65 | fname.resize(OTAIO_MAX_FNAME_SIZE); |
| 66 | const ZipEntry* entry = mzFindZipEntry(archive, type_path.c_str()); |
| 67 | mzReadZipEntry(archive, entry, &fname[0], OTAIO_MAX_FNAME_SIZE); |
| 68 | return fname; |
Jed Estep | 39c1b5e | 2015-12-15 16:04:53 -0800 | [diff] [blame] | 69 | } |