Merge "Correct caching behavior for should_inject_cache"

This commit is contained in:
Tao Bao
2016-03-25 19:03:28 +00:00
committed by Gerrit Code Review
2 changed files with 23 additions and 18 deletions

View File

@@ -19,9 +19,10 @@ LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS) include $(CLEAR_VARS)
otafault_static_libs := \ otafault_static_libs := \
libbase \
libminzip \ libminzip \
libselinux \ libz \
libz libselinux
LOCAL_SRC_FILES := config.cpp ota_io.cpp LOCAL_SRC_FILES := config.cpp ota_io.cpp
LOCAL_MODULE := libotafault LOCAL_MODULE := libotafault

View File

@@ -20,6 +20,8 @@
#include <stdio.h> #include <stdio.h>
#include <unistd.h> #include <unistd.h>
#include <android-base/stringprintf.h>
#include "minzip/Zip.h" #include "minzip/Zip.h"
#include "config.h" #include "config.h"
#include "ota_io.h" #include "ota_io.h"
@@ -27,12 +29,10 @@
#define OTAIO_MAX_FNAME_SIZE 128 #define OTAIO_MAX_FNAME_SIZE 128
static ZipArchive* archive; static ZipArchive* archive;
static std::map<const char*, bool> should_inject_cache; static std::map<std::string, bool> should_inject_cache;
static const char* get_type_path(const char* io_type) { static std::string get_type_path(const char* io_type) {
char* path = (char*)calloc(strlen(io_type) + strlen(OTAIO_BASE_DIR) + 2, sizeof(char)); return android::base::StringPrintf("%s/%s", OTAIO_BASE_DIR, io_type);
sprintf(path, "%s/%s", OTAIO_BASE_DIR, io_type);
return path;
} }
void ota_io_init(ZipArchive* za) { void ota_io_init(ZipArchive* za) {
@@ -41,13 +41,17 @@ void ota_io_init(ZipArchive* za) {
} }
bool should_fault_inject(const char* io_type) { bool should_fault_inject(const char* io_type) {
if (should_inject_cache.find(io_type) != should_inject_cache.end()) { // archive will be NULL if we used an entry point other
return should_inject_cache[io_type]; // than updater/updater.cpp:main
if (archive == NULL) {
return false;
} }
const char* type_path = get_type_path(io_type); const std::string type_path = get_type_path(io_type);
const ZipEntry* entry = mzFindZipEntry(archive, type_path); if (should_inject_cache.find(type_path) != should_inject_cache.end()) {
return should_inject_cache[type_path];
}
const ZipEntry* entry = mzFindZipEntry(archive, type_path.c_str());
should_inject_cache[type_path] = entry != nullptr; should_inject_cache[type_path] = entry != nullptr;
free((void*)type_path);
return entry != NULL; return entry != NULL;
} }
@@ -56,10 +60,10 @@ bool should_hit_cache() {
} }
std::string fault_fname(const char* io_type) { std::string fault_fname(const char* io_type) {
const char* type_path = get_type_path(io_type); std::string type_path = get_type_path(io_type);
char* fname = (char*) calloc(OTAIO_MAX_FNAME_SIZE, sizeof(char)); std::string fname;
const ZipEntry* entry = mzFindZipEntry(archive, type_path); fname.resize(OTAIO_MAX_FNAME_SIZE);
mzReadZipEntry(archive, entry, fname, OTAIO_MAX_FNAME_SIZE); const ZipEntry* entry = mzFindZipEntry(archive, type_path.c_str());
free((void*)type_path); mzReadZipEntry(archive, entry, &fname[0], OTAIO_MAX_FNAME_SIZE);
return std::string(fname); return fname;
} }