blob: c87f9a631c14b36ca4b8675623cb7c40c34816cb [file] [log] [blame]
Jed Estep39c1b5e2015-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
23#include "minzip/Zip.h"
24#include "config.h"
25#include "ota_io.h"
26
27#define OTAIO_MAX_FNAME_SIZE 128
28
29static ZipArchive* archive;
30static std::map<const char*, bool> should_inject_cache;
31
32static const char* get_type_path(const char* io_type) {
33 char* path = (char*)calloc(strlen(io_type) + strlen(OTAIO_BASE_DIR) + 2, sizeof(char));
34 sprintf(path, "%s/%s", OTAIO_BASE_DIR, io_type);
35 return path;
36}
37
38void ota_io_init(ZipArchive* za) {
39 archive = za;
40 ota_set_fault_files();
41}
42
43bool should_fault_inject(const char* io_type) {
44 if (should_inject_cache.find(io_type) != should_inject_cache.end()) {
45 return should_inject_cache[io_type];
46 }
47 const char* type_path = get_type_path(io_type);
48 const ZipEntry* entry = mzFindZipEntry(archive, type_path);
49 should_inject_cache[type_path] = entry != nullptr;
50 free((void*)type_path);
51 return entry != NULL;
52}
53
54bool should_hit_cache() {
55 return should_fault_inject(OTAIO_CACHE);
56}
57
58std::string fault_fname(const char* io_type) {
59 const char* type_path = get_type_path(io_type);
60 char* fname = (char*) calloc(OTAIO_MAX_FNAME_SIZE, sizeof(char));
61 const ZipEntry* entry = mzFindZipEntry(archive, type_path);
62 mzReadZipEntry(archive, entry, fname, OTAIO_MAX_FNAME_SIZE);
63 free((void*)type_path);
64 return std::string(fname);
65}