Jed Estep | ff6df89 | 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 | |
Tao Bao | ac27a7a | 2017-09-28 17:43:53 -0700 | [diff] [blame] | 17 | #include "config.h" |
| 18 | |
Jed Estep | ff6df89 | 2015-12-15 16:04:53 -0800 | [diff] [blame] | 19 | #include <map> |
| 20 | #include <string> |
| 21 | |
Jed Estep | 88dd779 | 2016-03-22 15:25:27 -0700 | [diff] [blame] | 22 | #include <android-base/stringprintf.h> |
Tianjie Xu | 8cf5c8f | 2016-09-08 20:10:11 -0700 | [diff] [blame] | 23 | #include <ziparchive/zip_archive.h> |
Jed Estep | 88dd779 | 2016-03-22 15:25:27 -0700 | [diff] [blame] | 24 | |
Jed Estep | ff6df89 | 2015-12-15 16:04:53 -0800 | [diff] [blame] | 25 | #include "ota_io.h" |
| 26 | |
| 27 | #define OTAIO_MAX_FNAME_SIZE 128 |
| 28 | |
Tianjie Xu | 8cf5c8f | 2016-09-08 20:10:11 -0700 | [diff] [blame] | 29 | static ZipArchiveHandle archive; |
Tianjie Xu | 4728242 | 2017-01-09 13:45:37 -0800 | [diff] [blame] | 30 | static bool is_retry = false; |
Jed Estep | 88dd779 | 2016-03-22 15:25:27 -0700 | [diff] [blame] | 31 | static std::map<std::string, bool> should_inject_cache; |
Jed Estep | ff6df89 | 2015-12-15 16:04:53 -0800 | [diff] [blame] | 32 | |
Jed Estep | 88dd779 | 2016-03-22 15:25:27 -0700 | [diff] [blame] | 33 | static std::string get_type_path(const char* io_type) { |
| 34 | return android::base::StringPrintf("%s/%s", OTAIO_BASE_DIR, io_type); |
Jed Estep | ff6df89 | 2015-12-15 16:04:53 -0800 | [diff] [blame] | 35 | } |
| 36 | |
Tianjie Xu | 4728242 | 2017-01-09 13:45:37 -0800 | [diff] [blame] | 37 | void ota_io_init(ZipArchiveHandle za, bool retry) { |
Jed Estep | ff6df89 | 2015-12-15 16:04:53 -0800 | [diff] [blame] | 38 | archive = za; |
Tianjie Xu | 4728242 | 2017-01-09 13:45:37 -0800 | [diff] [blame] | 39 | is_retry = retry; |
Jed Estep | ff6df89 | 2015-12-15 16:04:53 -0800 | [diff] [blame] | 40 | ota_set_fault_files(); |
| 41 | } |
| 42 | |
| 43 | bool should_fault_inject(const char* io_type) { |
| 44 | // archive will be NULL if we used an entry point other |
| 45 | // than updater/updater.cpp:main |
Tianjie Xu | 4728242 | 2017-01-09 13:45:37 -0800 | [diff] [blame] | 46 | if (archive == nullptr || is_retry) { |
Jed Estep | ff6df89 | 2015-12-15 16:04:53 -0800 | [diff] [blame] | 47 | return false; |
| 48 | } |
Jed Estep | 88dd779 | 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]; |
Jed Estep | ff6df89 | 2015-12-15 16:04:53 -0800 | [diff] [blame] | 52 | } |
Tianjie Xu | 8cf5c8f | 2016-09-08 20:10:11 -0700 | [diff] [blame] | 53 | ZipString zip_type_path(type_path.c_str()); |
| 54 | ZipEntry entry; |
| 55 | int status = FindEntry(archive, zip_type_path, &entry); |
| 56 | should_inject_cache[type_path] = (status == 0); |
| 57 | return (status == 0); |
Jed Estep | ff6df89 | 2015-12-15 16:04:53 -0800 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | bool should_hit_cache() { |
| 61 | return should_fault_inject(OTAIO_CACHE); |
| 62 | } |
| 63 | |
| 64 | std::string fault_fname(const char* io_type) { |
Jed Estep | 88dd779 | 2016-03-22 15:25:27 -0700 | [diff] [blame] | 65 | std::string type_path = get_type_path(io_type); |
| 66 | std::string fname; |
| 67 | fname.resize(OTAIO_MAX_FNAME_SIZE); |
Tianjie Xu | 8cf5c8f | 2016-09-08 20:10:11 -0700 | [diff] [blame] | 68 | ZipString zip_type_path(type_path.c_str()); |
| 69 | ZipEntry entry; |
Tianjie Xu | c89d1e7 | 2017-08-28 14:15:07 -0700 | [diff] [blame] | 70 | if (FindEntry(archive, zip_type_path, &entry) != 0) { |
| 71 | return {}; |
| 72 | } |
Tianjie Xu | 8cf5c8f | 2016-09-08 20:10:11 -0700 | [diff] [blame] | 73 | ExtractToMemory(archive, &entry, reinterpret_cast<uint8_t*>(&fname[0]), OTAIO_MAX_FNAME_SIZE); |
Jed Estep | 88dd779 | 2016-03-22 15:25:27 -0700 | [diff] [blame] | 74 | return fname; |
Jed Estep | ff6df89 | 2015-12-15 16:04:53 -0800 | [diff] [blame] | 75 | } |