summaryrefslogtreecommitdiffstats
path: root/otafault
diff options
context:
space:
mode:
authorEthan Yonker <dees_troy@teamw.in>2017-09-08 13:50:54 +0200
committerEthan Yonker <dees_troy@teamw.in>2017-09-08 14:14:59 +0200
commit8373cfe28cf1b5ad758faa1d502e21787c3665e4 (patch)
tree2f567c52cd6f89e1052481497d236b58d230b7e2 /otafault
parentDO NOT MERGE Android 8.0 stuff (diff)
parentrelease-request-f4ecf242-5d1c-45e0-8c7c-ede48d1a9e82-for-git_oc-release-4111650 snap-temp-L02200000075283731 (diff)
downloadandroid_bootable_recovery-8373cfe28cf1b5ad758faa1d502e21787c3665e4.tar
android_bootable_recovery-8373cfe28cf1b5ad758faa1d502e21787c3665e4.tar.gz
android_bootable_recovery-8373cfe28cf1b5ad758faa1d502e21787c3665e4.tar.bz2
android_bootable_recovery-8373cfe28cf1b5ad758faa1d502e21787c3665e4.tar.lz
android_bootable_recovery-8373cfe28cf1b5ad758faa1d502e21787c3665e4.tar.xz
android_bootable_recovery-8373cfe28cf1b5ad758faa1d502e21787c3665e4.tar.zst
android_bootable_recovery-8373cfe28cf1b5ad758faa1d502e21787c3665e4.zip
Diffstat (limited to 'otafault')
-rw-r--r--otafault/Android.mk11
-rw-r--r--otafault/config.cpp24
-rw-r--r--otafault/config.h4
-rw-r--r--otafault/ota_io.cpp31
-rw-r--r--otafault/ota_io.h24
5 files changed, 70 insertions, 24 deletions
diff --git a/otafault/Android.mk b/otafault/Android.mk
index e8241f4c4..1898e179f 100644
--- a/otafault/Android.mk
+++ b/otafault/Android.mk
@@ -23,11 +23,13 @@ endif
include $(CLEAR_VARS)
otafault_static_libs := \
- libbase \
- libminzip \
+ libziparchive \
libz \
- libselinux
+ libselinux \
+ libbase \
+ liblog
+LOCAL_CFLAGS := -Werror
LOCAL_SRC_FILES := config.cpp ota_io.cpp
LOCAL_MODULE_TAGS := eng
LOCAL_MODULE := libotafault
@@ -38,12 +40,15 @@ LOCAL_WHOLE_STATIC_LIBRARIES := $(otafault_static_libs)
include $(BUILD_STATIC_LIBRARY)
+# otafault_test (static executable)
+# ===============================
include $(CLEAR_VARS)
LOCAL_SRC_FILES := config.cpp ota_io.cpp test.cpp
LOCAL_MODULE_TAGS := tests
LOCAL_MODULE := otafault_test
LOCAL_STATIC_LIBRARIES := $(otafault_static_libs)
+LOCAL_CFLAGS := -Werror
LOCAL_C_INCLUDES := $(RECOVERY_PATH)
LOCAL_FORCE_STATIC_EXECUTABLE := true
diff --git a/otafault/config.cpp b/otafault/config.cpp
index b4567392d..8590833ee 100644
--- a/otafault/config.cpp
+++ b/otafault/config.cpp
@@ -21,38 +21,42 @@
#include <unistd.h>
#include <android-base/stringprintf.h>
+#include <ziparchive/zip_archive.h>
-#include "minzip/Zip.h"
#include "config.h"
#include "ota_io.h"
#define OTAIO_MAX_FNAME_SIZE 128
-static ZipArchive* archive;
+static ZipArchiveHandle archive;
+static bool is_retry = false;
static std::map<std::string, bool> should_inject_cache;
static std::string get_type_path(const char* io_type) {
return android::base::StringPrintf("%s/%s", OTAIO_BASE_DIR, io_type);
}
-void ota_io_init(ZipArchive* za) {
+void ota_io_init(ZipArchiveHandle za, bool retry) {
archive = za;
+ is_retry = retry;
ota_set_fault_files();
}
bool should_fault_inject(const char* io_type) {
// archive will be NULL if we used an entry point other
// than updater/updater.cpp:main
- if (archive == NULL) {
+ if (archive == nullptr || is_retry) {
return false;
}
const std::string type_path = get_type_path(io_type);
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;
- return entry != NULL;
+ ZipString zip_type_path(type_path.c_str());
+ ZipEntry entry;
+ int status = FindEntry(archive, zip_type_path, &entry);
+ should_inject_cache[type_path] = (status == 0);
+ return (status == 0);
}
bool should_hit_cache() {
@@ -63,7 +67,9 @@ std::string fault_fname(const char* io_type) {
std::string type_path = get_type_path(io_type);
std::string fname;
fname.resize(OTAIO_MAX_FNAME_SIZE);
- const ZipEntry* entry = mzFindZipEntry(archive, type_path.c_str());
- mzReadZipEntry(archive, entry, &fname[0], OTAIO_MAX_FNAME_SIZE);
+ ZipString zip_type_path(type_path.c_str());
+ ZipEntry entry;
+ int status = FindEntry(archive, zip_type_path, &entry);
+ ExtractToMemory(archive, &entry, reinterpret_cast<uint8_t*>(&fname[0]), OTAIO_MAX_FNAME_SIZE);
return fname;
}
diff --git a/otafault/config.h b/otafault/config.h
index 4430be3fb..4adbdd121 100644
--- a/otafault/config.h
+++ b/otafault/config.h
@@ -41,7 +41,7 @@
#include <stdbool.h>
-#include "minzip/Zip.h"
+#include <ziparchive/zip_archive.h>
#define OTAIO_BASE_DIR ".libotafault"
#define OTAIO_READ "READ"
@@ -52,7 +52,7 @@
/*
* Initialize libotafault by providing a reference to the OTA package.
*/
-void ota_io_init(ZipArchive* za);
+void ota_io_init(ZipArchiveHandle zip, bool retry);
/*
* Return true if a config file is present for the given IO type.
diff --git a/otafault/ota_io.cpp b/otafault/ota_io.cpp
index 04458537b..3a89bb5dd 100644
--- a/otafault/ota_io.cpp
+++ b/otafault/ota_io.cpp
@@ -14,7 +14,7 @@
* limitations under the License.
*/
-#include <map>
+#include "ota_io.h"
#include <errno.h>
#include <fcntl.h>
@@ -22,15 +22,17 @@
#include <sys/stat.h>
#include <unistd.h>
+#include <map>
+#include <memory>
+
#include "config.h"
-#include "ota_io.h"
static std::map<intptr_t, const char*> filename_cache;
static std::string read_fault_file_name = "";
static std::string write_fault_file_name = "";
static std::string fsync_fault_file_name = "";
-static bool get_hit_file(const char* cached_path, std::string ffn) {
+static bool get_hit_file(const char* cached_path, const std::string& ffn) {
return should_hit_cache()
? !strncmp(cached_path, OTAIO_CACHE_FNAME, strlen(cached_path))
: !strncmp(cached_path, ffn.c_str(), strlen(cached_path));
@@ -68,17 +70,33 @@ FILE* ota_fopen(const char* path, const char* mode) {
return fh;
}
-int ota_close(int fd) {
+static int __ota_close(int fd) {
// descriptors can be reused, so make sure not to leave them in the cache
filename_cache.erase(fd);
return close(fd);
}
-int ota_fclose(FILE* fh) {
- filename_cache.erase((intptr_t)fh);
+void OtaCloser::Close(int fd) {
+ __ota_close(fd);
+}
+
+int ota_close(unique_fd& fd) {
+ return __ota_close(fd.release());
+}
+
+static int __ota_fclose(FILE* fh) {
+ filename_cache.erase(reinterpret_cast<intptr_t>(fh));
return fclose(fh);
}
+void OtaFcloser::operator()(FILE* f) const {
+ __ota_fclose(f);
+};
+
+int ota_fclose(unique_file& fh) {
+ return __ota_fclose(fh.release());
+}
+
size_t ota_fread(void* ptr, size_t size, size_t nitems, FILE* stream) {
if (should_fault_inject(OTAIO_READ)) {
auto cached = filename_cache.find((intptr_t)stream);
@@ -92,6 +110,7 @@ size_t ota_fread(void* ptr, size_t size, size_t nitems, FILE* stream) {
}
}
size_t status = fread(ptr, size, nitems, stream);
+ // If I/O error occurs, set the retry-update flag.
if (status != nitems && errno == EIO) {
have_eio_error = true;
}
diff --git a/otafault/ota_io.h b/otafault/ota_io.h
index 84187a76e..9428f1b1f 100644
--- a/otafault/ota_io.h
+++ b/otafault/ota_io.h
@@ -26,6 +26,10 @@
#include <stdio.h>
#include <sys/stat.h>
+#include <memory>
+
+#include <android-base/unique_fd.h>
+
#define OTAIO_CACHE_FNAME "/cache/saved.file"
void ota_set_fault_files();
@@ -36,10 +40,6 @@ int ota_open(const char* path, int oflags, mode_t mode);
FILE* ota_fopen(const char* filename, const char* mode);
-int ota_close(int fd);
-
-int ota_fclose(FILE* fh);
-
size_t ota_fread(void* ptr, size_t size, size_t nitems, FILE* stream);
ssize_t ota_read(int fd, void* buf, size_t nbyte);
@@ -50,4 +50,20 @@ ssize_t ota_write(int fd, const void* buf, size_t nbyte);
int ota_fsync(int fd);
+struct OtaCloser {
+ static void Close(int);
+};
+
+using unique_fd = android::base::unique_fd_impl<OtaCloser>;
+
+int ota_close(unique_fd& fd);
+
+struct OtaFcloser {
+ void operator()(FILE*) const;
+};
+
+using unique_file = std::unique_ptr<FILE, OtaFcloser>;
+
+int ota_fclose(unique_file& fh);
+
#endif