blob: 8590833ee63650ccb09e595093af21132fdaaf2c [file] [log] [blame]
Jed Estepff6df892015-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#include <map>
18#include <string>
19
20#include <stdio.h>
21#include <unistd.h>
22
Jed Estep88dd7792016-03-22 15:25:27 -070023#include <android-base/stringprintf.h>
Tianjie Xu8cf5c8f2016-09-08 20:10:11 -070024#include <ziparchive/zip_archive.h>
Jed Estep88dd7792016-03-22 15:25:27 -070025
Jed Estepff6df892015-12-15 16:04:53 -080026#include "config.h"
27#include "ota_io.h"
28
29#define OTAIO_MAX_FNAME_SIZE 128
30
Tianjie Xu8cf5c8f2016-09-08 20:10:11 -070031static ZipArchiveHandle archive;
Tianjie Xu47282422017-01-09 13:45:37 -080032static bool is_retry = false;
Jed Estep88dd7792016-03-22 15:25:27 -070033static std::map<std::string, bool> should_inject_cache;
Jed Estepff6df892015-12-15 16:04:53 -080034
Jed Estep88dd7792016-03-22 15:25:27 -070035static std::string get_type_path(const char* io_type) {
36 return android::base::StringPrintf("%s/%s", OTAIO_BASE_DIR, io_type);
Jed Estepff6df892015-12-15 16:04:53 -080037}
38
Tianjie Xu47282422017-01-09 13:45:37 -080039void ota_io_init(ZipArchiveHandle za, bool retry) {
Jed Estepff6df892015-12-15 16:04:53 -080040 archive = za;
Tianjie Xu47282422017-01-09 13:45:37 -080041 is_retry = retry;
Jed Estepff6df892015-12-15 16:04:53 -080042 ota_set_fault_files();
43}
44
45bool should_fault_inject(const char* io_type) {
46 // archive will be NULL if we used an entry point other
47 // than updater/updater.cpp:main
Tianjie Xu47282422017-01-09 13:45:37 -080048 if (archive == nullptr || is_retry) {
Jed Estepff6df892015-12-15 16:04:53 -080049 return false;
50 }
Jed Estep88dd7792016-03-22 15:25:27 -070051 const std::string type_path = get_type_path(io_type);
52 if (should_inject_cache.find(type_path) != should_inject_cache.end()) {
53 return should_inject_cache[type_path];
Jed Estepff6df892015-12-15 16:04:53 -080054 }
Tianjie Xu8cf5c8f2016-09-08 20:10:11 -070055 ZipString zip_type_path(type_path.c_str());
56 ZipEntry entry;
57 int status = FindEntry(archive, zip_type_path, &entry);
58 should_inject_cache[type_path] = (status == 0);
59 return (status == 0);
Jed Estepff6df892015-12-15 16:04:53 -080060}
61
62bool should_hit_cache() {
63 return should_fault_inject(OTAIO_CACHE);
64}
65
66std::string fault_fname(const char* io_type) {
Jed Estep88dd7792016-03-22 15:25:27 -070067 std::string type_path = get_type_path(io_type);
68 std::string fname;
69 fname.resize(OTAIO_MAX_FNAME_SIZE);
Tianjie Xu8cf5c8f2016-09-08 20:10:11 -070070 ZipString zip_type_path(type_path.c_str());
71 ZipEntry entry;
72 int status = FindEntry(archive, zip_type_path, &entry);
73 ExtractToMemory(archive, &entry, reinterpret_cast<uint8_t*>(&fname[0]), OTAIO_MAX_FNAME_SIZE);
Jed Estep88dd7792016-03-22 15:25:27 -070074 return fname;
Jed Estepff6df892015-12-15 16:04:53 -080075}