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
This commit is contained in:
Tao Bao 2018-04-25 18:59:40 -07:00
parent d97b643de4
commit 641fa97def
16 changed files with 229 additions and 212 deletions

View file

@ -70,7 +70,7 @@ static void maybe_restart_adbd() {
} }
} }
int apply_from_adb(bool* wipe_cache, const char* install_file) { int apply_from_adb(bool* wipe_cache) {
modified_flash = true; modified_flash = true;
stop_adbd(); stop_adbd();
@ -113,7 +113,7 @@ int apply_from_adb(bool* wipe_cache, const char* install_file) {
break; break;
} }
} }
result = install_package(FUSE_SIDELOAD_HOST_PATHNAME, wipe_cache, install_file, false, 0); result = install_package(FUSE_SIDELOAD_HOST_PATHNAME, wipe_cache, false, 0);
break; break;
} }

View file

@ -17,6 +17,6 @@
#ifndef _ADB_INSTALL_H #ifndef _ADB_INSTALL_H
#define _ADB_INSTALL_H #define _ADB_INSTALL_H
int apply_from_adb(bool* wipe_cache, const char* install_file); int apply_from_adb(bool* wipe_cache);
#endif #endif

View file

@ -40,7 +40,7 @@
#include "edify/expr.h" #include "edify/expr.h"
#include "otafault/ota_io.h" #include "otafault/ota_io.h"
#include "otautil/cache_location.h" #include "otautil/paths.h"
#include "otautil/print_sha1.h" #include "otautil/print_sha1.h"
static int LoadPartitionContents(const std::string& filename, FileContents* file); static int LoadPartitionContents(const std::string& filename, FileContents* file);
@ -403,7 +403,7 @@ int applypatch_check(const char* filename, const std::vector<std::string>& patch
// If the source file is missing or corrupted, it might be because we were killed in the middle // If the source file is missing or corrupted, it might be because we were killed in the middle
// of patching it. A copy of it should have been made in cache_temp_source. If that file // of patching it. A copy of it should have been made in cache_temp_source. If that file
// exists and matches the sha1 we're looking for, the check still passes. // exists and matches the sha1 we're looking for, the check still passes.
if (LoadFileContents(CacheLocation::location().cache_temp_source().c_str(), &file) != 0) { if (LoadFileContents(Paths::Get().cache_temp_source().c_str(), &file) != 0) {
printf("failed to load cache file\n"); printf("failed to load cache file\n");
return 1; return 1;
} }
@ -525,7 +525,7 @@ int applypatch(const char* source_filename, const char* target_filename,
printf("source file is bad; trying copy\n"); printf("source file is bad; trying copy\n");
FileContents copy_file; FileContents copy_file;
if (LoadFileContents(CacheLocation::location().cache_temp_source().c_str(), &copy_file) < 0) { if (LoadFileContents(Paths::Get().cache_temp_source().c_str(), &copy_file) < 0) {
printf("failed to read copy file\n"); printf("failed to read copy file\n");
return 1; return 1;
} }
@ -620,7 +620,7 @@ static int GenerateTarget(const FileContents& source_file, const std::unique_ptr
printf("not enough free space on /cache\n"); printf("not enough free space on /cache\n");
return 1; return 1;
} }
if (SaveFileContents(CacheLocation::location().cache_temp_source().c_str(), &source_file) < 0) { if (SaveFileContents(Paths::Get().cache_temp_source().c_str(), &source_file) < 0) {
printf("failed to back up source file\n"); printf("failed to back up source file\n");
return 1; return 1;
} }
@ -685,7 +685,7 @@ static int GenerateTarget(const FileContents& source_file, const std::unique_ptr
} }
// Delete the backup copy of the source. // Delete the backup copy of the source.
unlink(CacheLocation::location().cache_temp_source().c_str()); unlink(Paths::Get().cache_temp_source().c_str());
// Success! // Success!
return 0; return 0;

View file

@ -38,7 +38,7 @@
#include <android-base/strings.h> #include <android-base/strings.h>
#include "applypatch/applypatch.h" #include "applypatch/applypatch.h"
#include "otautil/cache_location.h" #include "otautil/paths.h"
static int EliminateOpenFiles(const std::string& dirname, std::set<std::string>* files) { static int EliminateOpenFiles(const std::string& dirname, std::set<std::string>* files) {
std::unique_ptr<DIR, decltype(&closedir)> d(opendir("/proc"), closedir); std::unique_ptr<DIR, decltype(&closedir)> d(opendir("/proc"), closedir);
@ -95,7 +95,7 @@ static std::vector<std::string> FindExpendableFiles(
// We can't delete cache_temp_source; if it's there we might have restarted during // We can't delete cache_temp_source; if it's there we might have restarted during
// installation and could be depending on it to be there. // installation and could be depending on it to be there.
if (path == CacheLocation::location().cache_temp_source()) { if (path == Paths::Get().cache_temp_source()) {
continue; continue;
} }
@ -142,7 +142,7 @@ int MakeFreeSpaceOnCache(size_t bytes_needed) {
return 0; return 0;
#endif #endif
std::vector<std::string> dirs = { "/cache", CacheLocation::location().cache_log_directory() }; std::vector<std::string> dirs = { "/cache", Paths::Get().cache_log_directory() };
for (const auto& dirname : dirs) { for (const auto& dirname : dirs) {
if (RemoveFilesInDirectory(bytes_needed, dirname, FreeSpaceForFile)) { if (RemoveFilesInDirectory(bytes_needed, dirname, FreeSpaceForFile)) {
return 0; return 0;
@ -172,7 +172,7 @@ bool RemoveFilesInDirectory(size_t bytes_needed, const std::string& dirname,
} }
std::vector<std::string> files; std::vector<std::string> files;
if (dirname == CacheLocation::location().cache_log_directory()) { if (dirname == Paths::Get().cache_log_directory()) {
// Deletes the log files only. // Deletes the log files only.
auto log_filter = [](const std::string& file_name) { auto log_filter = [](const std::string& file_name) {
return android::base::StartsWith(file_name, "last_log") || return android::base::StartsWith(file_name, "last_log") ||

View file

@ -17,8 +17,8 @@
#ifndef RECOVERY_COMMON_H #ifndef RECOVERY_COMMON_H
#define RECOVERY_COMMON_H #define RECOVERY_COMMON_H
#include <stdio.h>
#include <stdarg.h> #include <stdarg.h>
#include <stdio.h>
#include <string> #include <string>
@ -38,7 +38,7 @@ extern std::string stage;
extern const char* reason; extern const char* reason;
// fopen a file, mounting volumes and making parent dirs as necessary. // fopen a file, mounting volumes and making parent dirs as necessary.
FILE* fopen_path(const char *path, const char *mode); FILE* fopen_path(const std::string& path, const char* mode);
void ui_print(const char* format, ...); void ui_print(const char* format, ...);

View file

@ -52,6 +52,7 @@
#include "otautil/SysUtil.h" #include "otautil/SysUtil.h"
#include "otautil/ThermalUtil.h" #include "otautil/ThermalUtil.h"
#include "otautil/error_code.h" #include "otautil/error_code.h"
#include "otautil/paths.h"
#include "private/install.h" #include "private/install.h"
#include "roots.h" #include "roots.h"
#include "ui.h" #include "ui.h"
@ -627,10 +628,8 @@ static int really_install_package(const std::string& path, bool* wipe_cache, boo
return result; return result;
} }
int install_package(const std::string& path, bool* wipe_cache, const std::string& install_file, int install_package(const std::string& path, bool* wipe_cache, bool needs_mount, int retry_count) {
bool needs_mount, int retry_count) {
CHECK(!path.empty()); CHECK(!path.empty());
CHECK(!install_file.empty());
CHECK(wipe_cache != nullptr); CHECK(wipe_cache != nullptr);
modified_flash = true; modified_flash = true;
@ -693,6 +692,7 @@ int install_package(const std::string& path, bool* wipe_cache, const std::string
std::string log_content = std::string log_content =
android::base::Join(log_header, "\n") + "\n" + android::base::Join(log_buffer, "\n") + "\n"; android::base::Join(log_header, "\n") + "\n" + android::base::Join(log_buffer, "\n") + "\n";
const std::string& install_file = Paths::Get().temporary_install_file();
if (!android::base::WriteStringToFile(log_content, install_file)) { if (!android::base::WriteStringToFile(log_content, install_file)) {
PLOG(ERROR) << "failed to write " << install_file; PLOG(ERROR) << "failed to write " << install_file;
} }

View file

@ -17,7 +17,10 @@
#ifndef RECOVERY_INSTALL_H_ #ifndef RECOVERY_INSTALL_H_
#define RECOVERY_INSTALL_H_ #define RECOVERY_INSTALL_H_
#include <stddef.h>
#include <string> #include <string>
#include <ziparchive/zip_archive.h> #include <ziparchive/zip_archive.h>
enum { INSTALL_SUCCESS, INSTALL_ERROR, INSTALL_CORRUPT, INSTALL_NONE, INSTALL_SKIPPED, enum { INSTALL_SUCCESS, INSTALL_ERROR, INSTALL_CORRUPT, INSTALL_NONE, INSTALL_SKIPPED,
@ -25,8 +28,8 @@ enum { INSTALL_SUCCESS, INSTALL_ERROR, INSTALL_CORRUPT, INSTALL_NONE, INSTALL_SK
// Installs the given update package. If INSTALL_SUCCESS is returned and *wipe_cache is true on // Installs the given update package. If INSTALL_SUCCESS is returned and *wipe_cache is true on
// exit, caller should wipe the cache partition. // exit, caller should wipe the cache partition.
int install_package(const std::string& package, bool* wipe_cache, const std::string& install_file, int install_package(const std::string& package, bool* wipe_cache, bool needs_mount,
bool needs_mount, int retry_count); int retry_count);
// Verify the package by ota keys. Return true if the package is verified successfully, // Verify the package by ota keys. Return true if the package is verified successfully,
// otherwise return false. // otherwise return false.

View file

@ -21,7 +21,7 @@ cc_library_static {
"SysUtil.cpp", "SysUtil.cpp",
"DirUtil.cpp", "DirUtil.cpp",
"ThermalUtil.cpp", "ThermalUtil.cpp",
"cache_location.cpp", "paths.cpp",
"rangeset.cpp", "rangeset.cpp",
], ],

View file

@ -1,79 +0,0 @@
/*
* Copyright (C) 2018 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef _OTAUTIL_OTAUTIL_CACHE_LOCATION_H_
#define _OTAUTIL_OTAUTIL_CACHE_LOCATION_H_
#include <string>
#include "android-base/macros.h"
// A singleton class to maintain the update related locations. The locations should be only set
// once at the start of the program.
class CacheLocation {
public:
static CacheLocation& location();
// getter and setter functions.
std::string cache_temp_source() const {
return cache_temp_source_;
}
void set_cache_temp_source(const std::string& temp_source) {
cache_temp_source_ = temp_source;
}
std::string last_command_file() const {
return last_command_file_;
}
void set_last_command_file(const std::string& last_command) {
last_command_file_ = last_command;
}
std::string stash_directory_base() const {
return stash_directory_base_;
}
void set_stash_directory_base(const std::string& base) {
stash_directory_base_ = base;
}
std::string cache_log_directory() const {
return cache_log_directory_;
}
void set_cache_log_directory(const std::string& log_dir) {
cache_log_directory_ = log_dir;
}
private:
CacheLocation();
DISALLOW_COPY_AND_ASSIGN(CacheLocation);
// When there isn't enough room on the target filesystem to hold the patched version of the file,
// we copy the original here and delete it to free up space. If the expected source file doesn't
// exist, or is corrupted, we look to see if the cached file contains the bits we want and use it
// as the source instead. The default location for the cached source is "/cache/saved.file".
std::string cache_temp_source_;
// Location to save the last command that stashes blocks.
std::string last_command_file_;
// The base directory to write stashes during update.
std::string stash_directory_base_;
// The location of last_log & last_kmsg.
std::string cache_log_directory_;
};
#endif // _OTAUTIL_OTAUTIL_CACHE_LOCATION_H_

View file

@ -0,0 +1,98 @@
/*
* Copyright (C) 2018 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef _OTAUTIL_PATHS_H_
#define _OTAUTIL_PATHS_H_
#include <string>
#include <android-base/macros.h>
// A singleton class to maintain the update related paths. The paths should be only set once at the
// start of the program.
class Paths {
public:
static Paths& Get();
std::string cache_log_directory() const {
return cache_log_directory_;
}
void set_cache_log_directory(const std::string& log_dir) {
cache_log_directory_ = log_dir;
}
std::string cache_temp_source() const {
return cache_temp_source_;
}
void set_cache_temp_source(const std::string& temp_source) {
cache_temp_source_ = temp_source;
}
std::string last_command_file() const {
return last_command_file_;
}
void set_last_command_file(const std::string& last_command_file) {
last_command_file_ = last_command_file;
}
std::string stash_directory_base() const {
return stash_directory_base_;
}
void set_stash_directory_base(const std::string& base) {
stash_directory_base_ = base;
}
std::string temporary_install_file() const {
return temporary_install_file_;
}
void set_temporary_install_file(const std::string& install_file) {
temporary_install_file_ = install_file;
}
std::string temporary_log_file() const {
return temporary_log_file_;
}
void set_temporary_log_file(const std::string& log_file) {
temporary_log_file_ = log_file;
}
private:
Paths();
DISALLOW_COPY_AND_ASSIGN(Paths);
// Path to the directory that contains last_log and last_kmsg log files.
std::string cache_log_directory_;
// Path to the temporary source file on /cache. When there isn't enough room on the target
// filesystem to hold the patched version of the file, we copy the original here and delete it to
// free up space. If the expected source file doesn't exist, or is corrupted, we look to see if
// the cached file contains the bits we want and use it as the source instead.
std::string cache_temp_source_;
// Path to the last command file.
std::string last_command_file_;
// Path to the base directory to write stashes during update.
std::string stash_directory_base_;
// Path to the temporary file that contains the install result.
std::string temporary_install_file_;
// Path to the temporary log file while under recovery.
std::string temporary_log_file_;
};
#endif // _OTAUTIL_PATHS_H_

View file

@ -14,20 +14,24 @@
* limitations under the License. * limitations under the License.
*/ */
#include "otautil/cache_location.h" #include "otautil/paths.h"
constexpr const char kDefaultCacheLogDirectory[] = "/cache/recovery";
constexpr const char kDefaultCacheTempSource[] = "/cache/saved.file"; constexpr const char kDefaultCacheTempSource[] = "/cache/saved.file";
constexpr const char kDefaultLastCommandFile[] = "/cache/recovery/last_command"; constexpr const char kDefaultLastCommandFile[] = "/cache/recovery/last_command";
constexpr const char kDefaultStashDirectoryBase[] = "/cache/recovery"; constexpr const char kDefaultStashDirectoryBase[] = "/cache/recovery";
constexpr const char kDefaultCacheLogDirectory[] = "/cache/recovery"; constexpr const char kDefaultTemporaryInstallFile[] = "/tmp/last_install";
constexpr const char kDefaultTemporaryLogFile[] = "/tmp/recovery.log";
CacheLocation& CacheLocation::location() { Paths& Paths::Get() {
static CacheLocation cache_location; static Paths paths;
return cache_location; return paths;
} }
CacheLocation::CacheLocation() Paths::Paths()
: cache_temp_source_(kDefaultCacheTempSource), : cache_log_directory_(kDefaultCacheLogDirectory),
cache_temp_source_(kDefaultCacheTempSource),
last_command_file_(kDefaultLastCommandFile), last_command_file_(kDefaultLastCommandFile),
stash_directory_base_(kDefaultStashDirectoryBase), stash_directory_base_(kDefaultStashDirectoryBase),
cache_log_directory_(kDefaultCacheLogDirectory) {} temporary_install_file_(kDefaultTemporaryInstallFile),
temporary_log_file_(kDefaultTemporaryLogFile) {}

View file

@ -68,6 +68,7 @@
#include "minui/minui.h" #include "minui/minui.h"
#include "otautil/DirUtil.h" #include "otautil/DirUtil.h"
#include "otautil/error_code.h" #include "otautil/error_code.h"
#include "otautil/paths.h"
#include "roots.h" #include "roots.h"
#include "rotate_logs.h" #include "rotate_logs.h"
#include "screen_ui.h" #include "screen_ui.h"
@ -110,8 +111,6 @@ static const char *CACHE_ROOT = "/cache";
static const char *DATA_ROOT = "/data"; static const char *DATA_ROOT = "/data";
static const char* METADATA_ROOT = "/metadata"; static const char* METADATA_ROOT = "/metadata";
static const char *SDCARD_ROOT = "/sdcard"; 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_KMSG_FILE = "/cache/recovery/last_kmsg";
static const char *LAST_LOG_FILE = "/cache/recovery/last_log"; 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 // 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. // Open a given path, mounting partitions as necessary.
FILE* fopen_path(const char* path, const char* mode) { FILE* fopen_path(const std::string& path, const char* mode) {
if (ensure_path_mounted(path) != 0) { if (ensure_path_mounted(path.c_str()) != 0) {
LOG(ERROR) << "Can't mount " << path; LOG(ERROR) << "Can't mount " << path;
return nullptr; return nullptr;
} }
@ -196,11 +195,11 @@ FILE* fopen_path(const char* path, const char* mode) {
if (strchr("wa", mode[0])) { if (strchr("wa", mode[0])) {
mkdir_recursively(path, 0777, true, sehandle); 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 // close a file, log an error if the error indicator is set
static void check_and_fclose(FILE *fp, const char *name) { static void check_and_fclose(FILE* fp, const std::string& name) {
fflush(fp); fflush(fp);
if (fsync(fileno(fp)) == -1) { if (fsync(fileno(fp)) == -1) {
PLOG(ERROR) << "Failed to fsync " << name; PLOG(ERROR) << "Failed to fsync " << name;
@ -408,27 +407,27 @@ static void save_kernel_log(const char* destination) {
android::base::WriteStringToFile(buffer, destination); android::base::WriteStringToFile(buffer, destination);
} }
// write content to the current pmsg session. // Writes content to the current pmsg session.
static ssize_t __pmsg_write(const char *filename, const char *buf, size_t len) { 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, return __android_log_pmsg_file_write(LOG_ID_SYSTEM, ANDROID_LOG_INFO, filename.c_str(),
filename, buf, len); buf.data(), buf.size());
} }
static void copy_log_file_to_pmsg(const char* source, const char* destination) { static void copy_log_file_to_pmsg(const std::string& source, const std::string& destination) {
std::string content; std::string content;
android::base::ReadFileToString(source, &content); android::base::ReadFileToString(source, &content);
__pmsg_write(destination, content.c_str(), content.length()); __pmsg_write(destination, content);
} }
// How much of the temp log we have copied to the copy in cache. // How much of the temp log we have copied to the copy in cache.
static off_t tmplog_offset = 0; 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"); FILE* dest_fp = fopen_path(destination, append ? "ae" : "we");
if (dest_fp == nullptr) { if (dest_fp == nullptr) {
PLOG(ERROR) << "Can't open " << destination; PLOG(ERROR) << "Can't open " << destination;
} else { } else {
FILE* source_fp = fopen(source, "re"); FILE* source_fp = fopen(source.c_str(), "re");
if (source_fp != nullptr) { if (source_fp != nullptr) {
if (append) { if (append) {
fseeko(source_fp, tmplog_offset, SEEK_SET); // Since last write fseeko(source_fp, tmplog_offset, SEEK_SET); // Since last write
@ -448,17 +447,16 @@ static void copy_log_file(const char* source, const char* destination, bool appe
} }
static void copy_logs() { static void copy_logs() {
// We only rotate and record the log of the current session if there are // We only rotate and record the log of the current session if there are actual attempts to modify
// actual attempts to modify the flash, such as wipes, installs from BCB // the flash, such as wipes, installs from BCB or menu selections. This is to avoid unnecessary
// or menu selections. This is to avoid unnecessary rotation (and // rotation (and possible deletion) of log files, if it does not do anything loggable.
// possible deletion) of log files, if it does not do anything loggable.
if (!modified_flash) { if (!modified_flash) {
return; return;
} }
// Always write to pmsg, this allows the OTA logs to be caught in logcat -L // 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(Paths::Get().temporary_log_file(), LAST_LOG_FILE);
copy_log_file_to_pmsg(TEMPORARY_INSTALL_FILE, LAST_INSTALL_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. // We can do nothing for now if there's no /cache partition.
if (!has_cache) { if (!has_cache) {
@ -470,9 +468,9 @@ static void copy_logs() {
rotate_logs(LAST_LOG_FILE, LAST_KMSG_FILE); rotate_logs(LAST_LOG_FILE, LAST_KMSG_FILE);
// Copy logs to cache so the system can find out what happened. // Copy logs to cache so the system can find out what happened.
copy_log_file(TEMPORARY_LOG_FILE, LOG_FILE, true); copy_log_file(Paths::Get().temporary_log_file(), LOG_FILE, true);
copy_log_file(TEMPORARY_LOG_FILE, LAST_LOG_FILE, false); copy_log_file(Paths::Get().temporary_log_file(), LAST_LOG_FILE, false);
copy_log_file(TEMPORARY_INSTALL_FILE, LAST_INSTALL_FILE, false); copy_log_file(Paths::Get().temporary_install_file(), LAST_INSTALL_FILE, false);
save_kernel_log(LAST_KMSG_FILE); save_kernel_log(LAST_KMSG_FILE);
chmod(LOG_FILE, 0600); chmod(LOG_FILE, 0600);
chown(LOG_FILE, AID_SYSTEM, AID_SYSTEM); chown(LOG_FILE, AID_SYSTEM, AID_SYSTEM);
@ -966,10 +964,10 @@ static void choose_recovery_file(Device* device) {
} }
} else { } else {
// If cache partition is not found, view /tmp/recovery.log instead. // 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; return;
} else { } 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, result = install_package(FUSE_SIDELOAD_HOST_PATHNAME, wipe_cache, false, 0 /*retry_count*/);
TEMPORARY_INSTALL_FILE, false, 0/*retry_count*/);
break; break;
} }
@ -1169,7 +1166,7 @@ static Device::BuiltinAction prompt_and_wait(Device* device, int status) {
{ {
bool adb = (chosen_action == Device::APPLY_ADB_SIDELOAD); bool adb = (chosen_action == Device::APPLY_ADB_SIDELOAD);
if (adb) { if (adb) {
status = apply_from_adb(&should_wipe_cache, TEMPORARY_INSTALL_FILE); status = apply_from_adb(&should_wipe_cache);
} else { } else {
status = apply_from_sdcard(device, &should_wipe_cache); status = apply_from_sdcard(device, &should_wipe_cache);
} }
@ -1351,15 +1348,16 @@ static bool bootreason_in_blacklist() {
return false; return false;
} }
static void log_failure_code(ErrorCode code, const char *update_package) { static void log_failure_code(ErrorCode code, const std::string& update_package) {
std::vector<std::string> log_buffer = { std::vector<std::string> log_buffer = {
update_package, update_package,
"0", // install result "0", // install result
"error: " + std::to_string(code), "error: " + std::to_string(code),
}; };
std::string log_content = android::base::Join(log_buffer, "\n"); std::string log_content = android::base::Join(log_buffer, "\n");
if (!android::base::WriteStringToFile(log_content, TEMPORARY_INSTALL_FILE)) { const std::string& install_file = Paths::Get().temporary_install_file();
PLOG(ERROR) << "failed to write " << 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. // Also write the info into last_log.
@ -1396,7 +1394,7 @@ int main(int argc, char **argv) {
// redirect_stdio should be called only in non-sideload mode. Otherwise // redirect_stdio should be called only in non-sideload mode. Otherwise
// we may have two logger instances with different timestamps. // 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)); 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); set_retry_bootloader_message(retry_count + 1, args);
} }
status = install_package(update_package, &should_wipe_cache, TEMPORARY_INSTALL_FILE, true, status = install_package(update_package, &should_wipe_cache, true, retry_count);
retry_count);
if (status == INSTALL_SUCCESS && should_wipe_cache) { if (status == INSTALL_SUCCESS && should_wipe_cache) {
wipe_cache(false, device); wipe_cache(false, device);
} }
@ -1627,7 +1624,7 @@ int main(int argc, char **argv) {
if (!sideload_auto_reboot) { if (!sideload_auto_reboot) {
ui->ShowText(true); 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 (status == INSTALL_SUCCESS && should_wipe_cache) {
if (!wipe_cache(false, device)) { if (!wipe_cache(false, device)) {
status = INSTALL_ERROR; status = INSTALL_ERROR;

View file

@ -16,7 +16,6 @@
#include <dirent.h> #include <dirent.h>
#include <fcntl.h> #include <fcntl.h>
#include <gtest/gtest.h>
#include <libgen.h> #include <libgen.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
@ -36,12 +35,13 @@
#include <android-base/test_utils.h> #include <android-base/test_utils.h>
#include <android-base/unique_fd.h> #include <android-base/unique_fd.h>
#include <bsdiff/bsdiff.h> #include <bsdiff/bsdiff.h>
#include <gtest/gtest.h>
#include <openssl/sha.h> #include <openssl/sha.h>
#include "applypatch/applypatch.h" #include "applypatch/applypatch.h"
#include "applypatch/applypatch_modes.h" #include "applypatch/applypatch_modes.h"
#include "common/test_constants.h" #include "common/test_constants.h"
#include "otautil/cache_location.h" #include "otautil/paths.h"
#include "otautil/print_sha1.h" #include "otautil/print_sha1.h"
using namespace std::string_literals; using namespace std::string_literals;
@ -110,14 +110,14 @@ class ApplyPatchCacheTest : public ApplyPatchTest {
protected: protected:
void SetUp() override { void SetUp() override {
ApplyPatchTest::SetUp(); ApplyPatchTest::SetUp();
CacheLocation::location().set_cache_temp_source(old_file); Paths::Get().set_cache_temp_source(old_file);
} }
}; };
class ApplyPatchModesTest : public ::testing::Test { class ApplyPatchModesTest : public ::testing::Test {
protected: protected:
void SetUp() override { void SetUp() override {
CacheLocation::location().set_cache_temp_source(cache_source.path); Paths::Get().set_cache_temp_source(cache_source.path);
android::base::InitLogging(nullptr, &test_logger); android::base::InitLogging(nullptr, &test_logger);
android::base::SetMinimumLogSeverity(android::base::LogSeverity::DEBUG); android::base::SetMinimumLogSeverity(android::base::LogSeverity::DEBUG);
} }
@ -157,7 +157,7 @@ class FreeCacheTest : public ::testing::Test {
} }
void SetUp() override { void SetUp() override {
CacheLocation::location().set_cache_log_directory(mock_log_dir.path); Paths::Get().set_cache_log_directory(mock_log_dir.path);
} }
// A mock method to calculate the free space. It assumes the partition has a total size of 40960 // A mock method to calculate the free space. It assumes the partition has a total size of 40960

View file

@ -41,8 +41,8 @@
#include "common/test_constants.h" #include "common/test_constants.h"
#include "edify/expr.h" #include "edify/expr.h"
#include "otautil/SysUtil.h" #include "otautil/SysUtil.h"
#include "otautil/cache_location.h"
#include "otautil/error_code.h" #include "otautil/error_code.h"
#include "otautil/paths.h"
#include "otautil/print_sha1.h" #include "otautil/print_sha1.h"
#include "updater/blockimg.h" #include "updater/blockimg.h"
#include "updater/install.h" #include "updater/install.h"
@ -106,10 +106,9 @@ class UpdaterTest : public ::testing::Test {
RegisterInstallFunctions(); RegisterInstallFunctions();
RegisterBlockImageFunctions(); RegisterBlockImageFunctions();
// Mock the location of last_command_file. Paths::Get().set_cache_temp_source(temp_saved_source_.path);
CacheLocation::location().set_cache_temp_source(temp_saved_source_.path); Paths::Get().set_last_command_file(temp_last_command_.path);
CacheLocation::location().set_last_command_file(temp_last_command_.path); Paths::Get().set_stash_directory_base(temp_stash_base_.path);
CacheLocation::location().set_stash_directory_base(temp_stash_base_.path);
} }
TemporaryFile temp_saved_source_; TemporaryFile temp_saved_source_;
@ -719,7 +718,7 @@ TEST_F(UpdaterTest, brotli_new_data) {
} }
TEST_F(UpdaterTest, last_command_update) { TEST_F(UpdaterTest, last_command_update) {
std::string last_command_file = CacheLocation::location().last_command_file(); std::string last_command_file = Paths::Get().last_command_file();
std::string block1 = std::string(4096, '1'); std::string block1 = std::string(4096, '1');
std::string block2 = std::string(4096, '2'); std::string block2 = std::string(4096, '2');
@ -806,7 +805,7 @@ TEST_F(UpdaterTest, last_command_update) {
} }
TEST_F(UpdaterTest, last_command_update_unresumable) { TEST_F(UpdaterTest, last_command_update_unresumable) {
std::string last_command_file = CacheLocation::location().last_command_file(); std::string last_command_file = Paths::Get().last_command_file();
std::string block1 = std::string(4096, '1'); std::string block1 = std::string(4096, '1');
std::string block2 = std::string(4096, '2'); std::string block2 = std::string(4096, '2');
@ -861,7 +860,7 @@ TEST_F(UpdaterTest, last_command_update_unresumable) {
} }
TEST_F(UpdaterTest, last_command_verify) { TEST_F(UpdaterTest, last_command_verify) {
std::string last_command_file = CacheLocation::location().last_command_file(); std::string last_command_file = Paths::Get().last_command_file();
std::string block1 = std::string(4096, '1'); std::string block1 = std::string(4096, '1');
std::string block2 = std::string(4096, '2'); std::string block2 = std::string(4096, '2');

View file

@ -15,8 +15,8 @@
*/ */
#include <ctype.h> #include <ctype.h>
#include <errno.h>
#include <dirent.h> #include <dirent.h>
#include <errno.h>
#include <fcntl.h> #include <fcntl.h>
#include <inttypes.h> #include <inttypes.h>
#include <linux/fs.h> #include <linux/fs.h>
@ -25,13 +25,12 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <sys/ioctl.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <sys/types.h> #include <sys/types.h>
#include <sys/wait.h> #include <sys/wait.h>
#include <sys/ioctl.h>
#include <time.h> #include <time.h>
#include <unistd.h> #include <unistd.h>
#include <fec/io.h>
#include <functional> #include <functional>
#include <limits> #include <limits>
@ -47,14 +46,15 @@
#include <android-base/unique_fd.h> #include <android-base/unique_fd.h>
#include <applypatch/applypatch.h> #include <applypatch/applypatch.h>
#include <brotli/decode.h> #include <brotli/decode.h>
#include <fec/io.h>
#include <openssl/sha.h> #include <openssl/sha.h>
#include <private/android_filesystem_config.h> #include <private/android_filesystem_config.h>
#include <ziparchive/zip_archive.h> #include <ziparchive/zip_archive.h>
#include "edify/expr.h" #include "edify/expr.h"
#include "otafault/ota_io.h" #include "otafault/ota_io.h"
#include "otautil/cache_location.h"
#include "otautil/error_code.h" #include "otautil/error_code.h"
#include "otautil/paths.h"
#include "otautil/print_sha1.h" #include "otautil/print_sha1.h"
#include "otautil/rangeset.h" #include "otautil/rangeset.h"
#include "updater/install.h" #include "updater/install.h"
@ -74,7 +74,7 @@ static bool is_retry = false;
static std::unordered_map<std::string, RangeSet> stash_map; static std::unordered_map<std::string, RangeSet> stash_map;
static void DeleteLastCommandFile() { static void DeleteLastCommandFile() {
std::string last_command_file = CacheLocation::location().last_command_file(); const std::string& last_command_file = Paths::Get().last_command_file();
if (unlink(last_command_file.c_str()) == -1 && errno != ENOENT) { if (unlink(last_command_file.c_str()) == -1 && errno != ENOENT) {
PLOG(ERROR) << "Failed to unlink: " << last_command_file; PLOG(ERROR) << "Failed to unlink: " << last_command_file;
} }
@ -83,7 +83,7 @@ static void DeleteLastCommandFile() {
// Parse the last command index of the last update and save the result to |last_command_index|. // 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. // Return true if we successfully read the index.
static bool ParseLastCommandFile(int* last_command_index) { static bool ParseLastCommandFile(int* last_command_index) {
std::string last_command_file = CacheLocation::location().last_command_file(); const std::string& last_command_file = Paths::Get().last_command_file();
android::base::unique_fd fd(TEMP_FAILURE_RETRY(open(last_command_file.c_str(), O_RDONLY))); android::base::unique_fd fd(TEMP_FAILURE_RETRY(open(last_command_file.c_str(), O_RDONLY)));
if (fd == -1) { if (fd == -1) {
if (errno != ENOENT) { if (errno != ENOENT) {
@ -119,7 +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 // Update the last command index in the last_command_file if the current command writes to the
// stash either explicitly or implicitly. // stash either explicitly or implicitly.
static bool UpdateLastCommandIndex(int command_index, const std::string& command_string) { static bool UpdateLastCommandIndex(int command_index, const std::string& command_string) {
std::string last_command_file = CacheLocation::location().last_command_file(); const std::string& last_command_file = Paths::Get().last_command_file();
std::string last_command_tmp = last_command_file + ".tmp"; std::string last_command_tmp = last_command_file + ".tmp";
std::string content = std::to_string(command_index) + "\n" + command_string; std::string content = std::to_string(command_index) + "\n" + command_string;
android::base::unique_fd wfd( android::base::unique_fd wfd(
@ -676,11 +676,7 @@ static std::string GetStashFileName(const std::string& base, const std::string&
if (base.empty()) { if (base.empty()) {
return ""; return "";
} }
return Paths::Get().stash_directory_base() + "/" + base + "/" + id + postfix;
std::string fn(CacheLocation::location().stash_directory_base());
fn += "/" + base + "/" + id + postfix;
return fn;
} }
// Does a best effort enumeration of stash files. Ignores possible non-file items in the stash // Does a best effort enumeration of stash files. Ignores possible non-file items in the stash

View file

@ -17,9 +17,9 @@
#include "updater/updater.h" #include "updater/updater.h"
#include <stdio.h> #include <stdio.h>
#include <unistd.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <unistd.h>
#include <string> #include <string>
@ -34,7 +34,6 @@
#include "otafault/config.h" #include "otafault/config.h"
#include "otautil/DirUtil.h" #include "otautil/DirUtil.h"
#include "otautil/SysUtil.h" #include "otautil/SysUtil.h"
#include "otautil/cache_location.h"
#include "otautil/error_code.h" #include "otautil/error_code.h"
#include "updater/blockimg.h" #include "updater/blockimg.h"
#include "updater/install.h" #include "updater/install.h"