From 641fa97def3e6ef52471e47ad25d0658ef4d943e Mon Sep 17 00:00:00 2001 From: Tao Bao Date: Wed, 25 Apr 2018 18:59:40 -0700 Subject: Rename CacheLocation to Paths. We have a general need for overriding more paths (e.g. "/tmp"), mostly for testing purpose. Rename CacheLocation to Paths, and use that to manage TEMPORARY_{INSTALL,LOG}_FILE. Test: mmma -j bootable/recovery Test: recovery_component_test Change-Id: Ia8ce8e5695df37ca434f13ac4d3206de1e8e9396 --- recovery.cpp | 147 +++++++++++++++++++++++++++++------------------------------ 1 file changed, 72 insertions(+), 75 deletions(-) (limited to 'recovery.cpp') diff --git a/recovery.cpp b/recovery.cpp index ceb19413a..dbbac7a7d 100644 --- a/recovery.cpp +++ b/recovery.cpp @@ -68,6 +68,7 @@ #include "minui/minui.h" #include "otautil/DirUtil.h" #include "otautil/error_code.h" +#include "otautil/paths.h" #include "roots.h" #include "rotate_logs.h" #include "screen_ui.h" @@ -110,8 +111,6 @@ static const char *CACHE_ROOT = "/cache"; static const char *DATA_ROOT = "/data"; static const char* METADATA_ROOT = "/metadata"; static const char *SDCARD_ROOT = "/sdcard"; -static const char *TEMPORARY_LOG_FILE = "/tmp/recovery.log"; -static const char *TEMPORARY_INSTALL_FILE = "/tmp/last_install"; static const char *LAST_KMSG_FILE = "/cache/recovery/last_kmsg"; static const char *LAST_LOG_FILE = "/cache/recovery/last_log"; // We will try to apply the update package 5 times at most in case of an I/O error or @@ -185,8 +184,8 @@ struct selabel_handle* sehandle; */ // Open a given path, mounting partitions as necessary. -FILE* fopen_path(const char* path, const char* mode) { - if (ensure_path_mounted(path) != 0) { +FILE* fopen_path(const std::string& path, const char* mode) { + if (ensure_path_mounted(path.c_str()) != 0) { LOG(ERROR) << "Can't mount " << path; return nullptr; } @@ -196,19 +195,19 @@ FILE* fopen_path(const char* path, const char* mode) { if (strchr("wa", mode[0])) { mkdir_recursively(path, 0777, true, sehandle); } - return fopen(path, mode); + return fopen(path.c_str(), mode); } // close a file, log an error if the error indicator is set -static void check_and_fclose(FILE *fp, const char *name) { - fflush(fp); - if (fsync(fileno(fp)) == -1) { - PLOG(ERROR) << "Failed to fsync " << name; - } - if (ferror(fp)) { - PLOG(ERROR) << "Error in " << name; - } - fclose(fp); +static void check_and_fclose(FILE* fp, const std::string& name) { + fflush(fp); + if (fsync(fileno(fp)) == -1) { + PLOG(ERROR) << "Failed to fsync " << name; + } + if (ferror(fp)) { + PLOG(ERROR) << "Error in " << name; + } + fclose(fp); } bool is_ro_debuggable() { @@ -408,27 +407,27 @@ static void save_kernel_log(const char* destination) { android::base::WriteStringToFile(buffer, destination); } -// write content to the current pmsg session. -static ssize_t __pmsg_write(const char *filename, const char *buf, size_t len) { - return __android_log_pmsg_file_write(LOG_ID_SYSTEM, ANDROID_LOG_INFO, - filename, buf, len); +// Writes content to the current pmsg session. +static ssize_t __pmsg_write(const std::string& filename, const std::string& buf) { + return __android_log_pmsg_file_write(LOG_ID_SYSTEM, ANDROID_LOG_INFO, filename.c_str(), + buf.data(), buf.size()); } -static void copy_log_file_to_pmsg(const char* source, const char* destination) { - std::string content; - android::base::ReadFileToString(source, &content); - __pmsg_write(destination, content.c_str(), content.length()); +static void copy_log_file_to_pmsg(const std::string& source, const std::string& destination) { + std::string content; + android::base::ReadFileToString(source, &content); + __pmsg_write(destination, content); } // How much of the temp log we have copied to the copy in cache. static off_t tmplog_offset = 0; -static void copy_log_file(const char* source, const char* destination, bool append) { +static void copy_log_file(const std::string& source, const std::string& destination, bool append) { FILE* dest_fp = fopen_path(destination, append ? "ae" : "we"); if (dest_fp == nullptr) { PLOG(ERROR) << "Can't open " << destination; } else { - FILE* source_fp = fopen(source, "re"); + FILE* source_fp = fopen(source.c_str(), "re"); if (source_fp != nullptr) { if (append) { fseeko(source_fp, tmplog_offset, SEEK_SET); // Since last write @@ -448,39 +447,38 @@ static void copy_log_file(const char* source, const char* destination, bool appe } static void copy_logs() { - // We only rotate and record the log of the current session if there are - // actual attempts to modify the flash, such as wipes, installs from BCB - // or menu selections. This is to avoid unnecessary rotation (and - // possible deletion) of log files, if it does not do anything loggable. - if (!modified_flash) { - return; - } + // We only rotate and record the log of the current session if there are actual attempts to modify + // the flash, such as wipes, installs from BCB or menu selections. This is to avoid unnecessary + // rotation (and possible deletion) of log files, if it does not do anything loggable. + if (!modified_flash) { + return; + } - // Always write to pmsg, this allows the OTA logs to be caught in logcat -L - copy_log_file_to_pmsg(TEMPORARY_LOG_FILE, LAST_LOG_FILE); - copy_log_file_to_pmsg(TEMPORARY_INSTALL_FILE, LAST_INSTALL_FILE); + // Always write to pmsg, this allows the OTA logs to be caught in `logcat -L`. + copy_log_file_to_pmsg(Paths::Get().temporary_log_file(), LAST_LOG_FILE); + copy_log_file_to_pmsg(Paths::Get().temporary_install_file(), LAST_INSTALL_FILE); - // We can do nothing for now if there's no /cache partition. - if (!has_cache) { - return; - } + // We can do nothing for now if there's no /cache partition. + if (!has_cache) { + return; + } - ensure_path_mounted(LAST_LOG_FILE); - ensure_path_mounted(LAST_KMSG_FILE); - rotate_logs(LAST_LOG_FILE, LAST_KMSG_FILE); - - // Copy logs to cache so the system can find out what happened. - copy_log_file(TEMPORARY_LOG_FILE, LOG_FILE, true); - copy_log_file(TEMPORARY_LOG_FILE, LAST_LOG_FILE, false); - copy_log_file(TEMPORARY_INSTALL_FILE, LAST_INSTALL_FILE, false); - save_kernel_log(LAST_KMSG_FILE); - chmod(LOG_FILE, 0600); - chown(LOG_FILE, AID_SYSTEM, AID_SYSTEM); - chmod(LAST_KMSG_FILE, 0600); - chown(LAST_KMSG_FILE, AID_SYSTEM, AID_SYSTEM); - chmod(LAST_LOG_FILE, 0640); - chmod(LAST_INSTALL_FILE, 0644); - sync(); + ensure_path_mounted(LAST_LOG_FILE); + ensure_path_mounted(LAST_KMSG_FILE); + rotate_logs(LAST_LOG_FILE, LAST_KMSG_FILE); + + // Copy logs to cache so the system can find out what happened. + copy_log_file(Paths::Get().temporary_log_file(), LOG_FILE, true); + copy_log_file(Paths::Get().temporary_log_file(), LAST_LOG_FILE, false); + copy_log_file(Paths::Get().temporary_install_file(), LAST_INSTALL_FILE, false); + save_kernel_log(LAST_KMSG_FILE); + chmod(LOG_FILE, 0600); + chown(LOG_FILE, AID_SYSTEM, AID_SYSTEM); + chmod(LAST_KMSG_FILE, 0600); + chown(LAST_KMSG_FILE, AID_SYSTEM, AID_SYSTEM); + chmod(LAST_LOG_FILE, 0640); + chmod(LAST_INSTALL_FILE, 0644); + sync(); } // Clear the recovery command and prepare to boot a (hopefully working) system, @@ -966,10 +964,10 @@ static void choose_recovery_file(Device* device) { } } else { // If cache partition is not found, view /tmp/recovery.log instead. - if (access(TEMPORARY_LOG_FILE, R_OK) == -1) { + if (access(Paths::Get().temporary_log_file().c_str(), R_OK) == -1) { return; } else { - entries.push_back(TEMPORARY_LOG_FILE); + entries.push_back(Paths::Get().temporary_log_file()); } } @@ -1091,8 +1089,7 @@ static int apply_from_sdcard(Device* device, bool* wipe_cache) { } } - result = install_package(FUSE_SIDELOAD_HOST_PATHNAME, wipe_cache, - TEMPORARY_INSTALL_FILE, false, 0/*retry_count*/); + result = install_package(FUSE_SIDELOAD_HOST_PATHNAME, wipe_cache, false, 0 /*retry_count*/); break; } @@ -1169,7 +1166,7 @@ static Device::BuiltinAction prompt_and_wait(Device* device, int status) { { bool adb = (chosen_action == Device::APPLY_ADB_SIDELOAD); if (adb) { - status = apply_from_adb(&should_wipe_cache, TEMPORARY_INSTALL_FILE); + status = apply_from_adb(&should_wipe_cache); } else { status = apply_from_sdcard(device, &should_wipe_cache); } @@ -1351,19 +1348,20 @@ static bool bootreason_in_blacklist() { return false; } -static void log_failure_code(ErrorCode code, const char *update_package) { - std::vector log_buffer = { - update_package, - "0", // install result - "error: " + std::to_string(code), - }; - std::string log_content = android::base::Join(log_buffer, "\n"); - if (!android::base::WriteStringToFile(log_content, TEMPORARY_INSTALL_FILE)) { - PLOG(ERROR) << "failed to write " << TEMPORARY_INSTALL_FILE; - } +static void log_failure_code(ErrorCode code, const std::string& update_package) { + std::vector log_buffer = { + update_package, + "0", // install result + "error: " + std::to_string(code), + }; + std::string log_content = android::base::Join(log_buffer, "\n"); + const std::string& install_file = Paths::Get().temporary_install_file(); + if (!android::base::WriteStringToFile(log_content, install_file)) { + PLOG(ERROR) << "Failed to write " << install_file; + } - // Also write the info into last_log. - LOG(INFO) << log_content; + // Also write the info into last_log. + LOG(INFO) << log_content; } int main(int argc, char **argv) { @@ -1396,7 +1394,7 @@ int main(int argc, char **argv) { // redirect_stdio should be called only in non-sideload mode. Otherwise // we may have two logger instances with different timestamps. - redirect_stdio(TEMPORARY_LOG_FILE); + redirect_stdio(Paths::Get().temporary_log_file().c_str()); printf("Starting recovery (pid %d) on %s", getpid(), ctime(&start)); @@ -1565,8 +1563,7 @@ int main(int argc, char **argv) { set_retry_bootloader_message(retry_count + 1, args); } - status = install_package(update_package, &should_wipe_cache, TEMPORARY_INSTALL_FILE, true, - retry_count); + status = install_package(update_package, &should_wipe_cache, true, retry_count); if (status == INSTALL_SUCCESS && should_wipe_cache) { wipe_cache(false, device); } @@ -1627,7 +1624,7 @@ int main(int argc, char **argv) { if (!sideload_auto_reboot) { ui->ShowText(true); } - status = apply_from_adb(&should_wipe_cache, TEMPORARY_INSTALL_FILE); + status = apply_from_adb(&should_wipe_cache); if (status == INSTALL_SUCCESS && should_wipe_cache) { if (!wipe_cache(false, device)) { status = INSTALL_ERROR; -- cgit v1.2.3