From 3bbb20f557790c015e44098098375eb6cc376a42 Mon Sep 17 00:00:00 2001 From: Tianjie Xu Date: Tue, 27 Feb 2018 15:56:11 -0800 Subject: Add a singleton CacheLocation to replace the hard coded locations This class allows us to set the following locations dynamically: cache_temp_source, last_command_file, stash_directory_base. In the updater's main function, we reset the values of these variables to their default locations in /cache; while we can set them to temp files in unit tests or host simulation. Test: unit tests pass Change-Id: I528652650caa41373617ab055d41b1f1a4ec0f87 --- updater/blockimg.cpp | 9 +++++---- updater/include/updater/blockimg.h | 1 - updater/updater.cpp | 5 +++++ 3 files changed, 10 insertions(+), 5 deletions(-) (limited to 'updater') diff --git a/updater/blockimg.cpp b/updater/blockimg.cpp index 4f085b204..e93196b27 100644 --- a/updater/blockimg.cpp +++ b/updater/blockimg.cpp @@ -53,6 +53,7 @@ #include "edify/expr.h" #include "otafault/ota_io.h" +#include "otautil/cache_location.h" #include "otautil/error_code.h" #include "otautil/print_sha1.h" #include "otautil/rangeset.h" @@ -65,17 +66,15 @@ #define DEBUG_ERASE 0 static constexpr size_t BLOCKSIZE = 4096; -static constexpr const char* STASH_DIRECTORY_BASE = "/cache/recovery"; static constexpr mode_t STASH_DIRECTORY_MODE = 0700; static constexpr mode_t STASH_FILE_MODE = 0600; -std::string last_command_file = "/cache/recovery/last_command"; - static CauseCode failure_type = kNoCause; static bool is_retry = false; static std::unordered_map stash_map; static void DeleteLastCommandFile() { + std::string last_command_file = CacheLocation::location().last_command_file(); if (unlink(last_command_file.c_str()) == -1 && errno != ENOENT) { PLOG(ERROR) << "Failed to unlink: " << last_command_file; } @@ -84,6 +83,7 @@ static void DeleteLastCommandFile() { // Parse the last command index of the last update and save the result to |last_command_index|. // Return true if we successfully read the index. static bool ParseLastCommandFile(int* last_command_index) { + std::string last_command_file = CacheLocation::location().last_command_file(); android::base::unique_fd fd(TEMP_FAILURE_RETRY(open(last_command_file.c_str(), O_RDONLY))); if (fd == -1) { if (errno != ENOENT) { @@ -119,6 +119,7 @@ static bool ParseLastCommandFile(int* last_command_index) { // Update the last command index in the last_command_file if the current command writes to the // stash either explicitly or implicitly. static bool UpdateLastCommandIndex(int command_index, const std::string& command_string) { + std::string last_command_file = CacheLocation::location().last_command_file(); std::string last_command_tmp = last_command_file + ".tmp"; std::string content = std::to_string(command_index) + "\n" + command_string; android::base::unique_fd wfd( @@ -676,7 +677,7 @@ static std::string GetStashFileName(const std::string& base, const std::string& return ""; } - std::string fn(STASH_DIRECTORY_BASE); + std::string fn(CacheLocation::location().stash_directory_base()); fn += "/" + base + "/" + id + postfix; return fn; diff --git a/updater/include/updater/blockimg.h b/updater/include/updater/blockimg.h index 2cc68ce9d..71733b303 100644 --- a/updater/include/updater/blockimg.h +++ b/updater/include/updater/blockimg.h @@ -19,7 +19,6 @@ #include -extern std::string last_command_file; void RegisterBlockImageFunctions(); #endif diff --git a/updater/updater.cpp b/updater/updater.cpp index f55a0d3bd..f063e5fa1 100644 --- a/updater/updater.cpp +++ b/updater/updater.cpp @@ -34,6 +34,7 @@ #include "otafault/config.h" #include "otautil/DirUtil.h" #include "otautil/SysUtil.h" +#include "otautil/cache_location.h" #include "otautil/error_code.h" #include "updater/blockimg.h" #include "updater/install.h" @@ -168,6 +169,10 @@ int main(int argc, char** argv) { } ota_io_init(za, state.is_retry); + // Initialize the cache_temp_source, last_command_file and stash_directory_base to their default + // locations. + CacheLocation::location().ResetLocations(); + std::string result; bool status = Evaluate(&state, root, &result); -- cgit v1.2.3