From 5ee25666cc819e9ebc9b72c7a44c4bc9bab9e4e3 Mon Sep 17 00:00:00 2001 From: Tao Bao Date: Wed, 11 Jul 2018 15:55:32 -0700 Subject: applypatch: Consolidate CacheSizeCheck() and MakeFreeSpaceOnCache(). They are doing exactly the same thing, except for the slightly different error return value (1 vs -1). int CacheSizeCheck(size_t bytes); int MakeFreeSpaceOnCache(size_t bytes_needed); This CL consolidates the two functions and uses bool as its return type. // Checks whether /cache partition has at least 'bytes'-byte free space. Returns true immediately // if so. Otherwise, it will try to free some space by removing older logs, checks again and // returns the checking result. bool CheckAndFreeSpaceOnCache(size_t bytes); Test: Run recovery_unit_test and recovery_component_test on marlin. Change-Id: I94a96934d2b18713f8f39ad5aa96a02c98d87963 --- applypatch/applypatch.cpp | 12 ++---------- applypatch/freecache.cpp | 12 ++++++------ applypatch/include/applypatch/applypatch.h | 9 ++++----- 3 files changed, 12 insertions(+), 21 deletions(-) (limited to 'applypatch') diff --git a/applypatch/applypatch.cpp b/applypatch/applypatch.cpp index 13e4b1ae0..12e139930 100644 --- a/applypatch/applypatch.cpp +++ b/applypatch/applypatch.cpp @@ -420,14 +420,6 @@ static size_t FileSink(const unsigned char* data, size_t len, int fd) { return done; } -int CacheSizeCheck(size_t bytes) { - if (MakeFreeSpaceOnCache(bytes) < 0) { - LOG(ERROR) << "Failed to make " << bytes << " bytes available on /cache"; - return 1; - } - return 0; -} - int applypatch(const char* source_filename, const char* target_filename, const char* target_sha1_str, size_t /* target_size */, const std::vector& patch_sha1s, @@ -562,8 +554,8 @@ static int GenerateTarget(const FileContents& source_file, const std::unique_ptr CHECK(android::base::StartsWith(target_filename, "EMMC:")); - // We still write the original source to cache, in case the partition write is interrupted. - if (MakeFreeSpaceOnCache(source_file.data.size()) < 0) { + // We write the original source to cache, in case the partition write is interrupted. + if (!CheckAndFreeSpaceOnCache(source_file.data.size())) { LOG(ERROR) << "Not enough free space on /cache"; return 1; } diff --git a/applypatch/freecache.cpp b/applypatch/freecache.cpp index 4989b7496..e4878655e 100644 --- a/applypatch/freecache.cpp +++ b/applypatch/freecache.cpp @@ -150,22 +150,22 @@ static int64_t FreeSpaceForFile(const std::string& filename) { return free_space; } -int MakeFreeSpaceOnCache(size_t bytes_needed) { +bool CheckAndFreeSpaceOnCache(size_t bytes) { #ifndef __ANDROID__ // TODO(xunchang): Implement a heuristic cache size check during host simulation. - LOG(WARNING) << "Skipped making (" << bytes_needed + LOG(WARNING) << "Skipped making (" << bytes << ") bytes free space on /cache; program is running on host"; - return 0; + return true; #endif std::vector dirs{ "/cache", Paths::Get().cache_log_directory() }; for (const auto& dirname : dirs) { - if (RemoveFilesInDirectory(bytes_needed, dirname, FreeSpaceForFile)) { - return 0; + if (RemoveFilesInDirectory(bytes, dirname, FreeSpaceForFile)) { + return true; } } - return -1; + return false; } bool RemoveFilesInDirectory(size_t bytes_needed, const std::string& dirname, diff --git a/applypatch/include/applypatch/applypatch.h b/applypatch/include/applypatch/applypatch.h index 88659b86a..28dba7e68 100644 --- a/applypatch/include/applypatch/applypatch.h +++ b/applypatch/include/applypatch/applypatch.h @@ -40,10 +40,6 @@ using SinkFn = std::function; int ShowLicenses(); -// Checks whether /cache partition has at least 'bytes'-byte free space. Returns 0 on having -// sufficient space. -int CacheSizeCheck(size_t bytes); - // Parses a given string of 40 hex digits into 20-byte array 'digest'. 'str' may contain only the // digest or be of the form ":". Returns 0 on success, or -1 on any error. int ParseSha1(const std::string& str, uint8_t* digest); @@ -113,7 +109,10 @@ int ApplyImagePatch(const unsigned char* old_data, size_t old_size, const Value& // freecache.cpp -int MakeFreeSpaceOnCache(size_t bytes_needed); +// Checks whether /cache partition has at least 'bytes'-byte free space. Returns true immediately +// if so. Otherwise, it will try to free some space by removing older logs, checks again and +// returns the checking result. +bool CheckAndFreeSpaceOnCache(size_t bytes); // Removes the files in |dirname| until we have at least |bytes_needed| bytes of free space on the // partition. |space_checker| should return the size of the free space, or -1 on error. -- cgit v1.2.3