Merge "applypatch: Consolidate CacheSizeCheck() and MakeFreeSpaceOnCache()."

am: 624b6b6cd5

Change-Id: I16278b00abcdcc532928b534462ff0762c64c1a2
This commit is contained in:
Tao Bao
2018-07-13 12:48:45 -07:00
committed by android-build-merger
5 changed files with 16 additions and 25 deletions
+2 -10
View File
@@ -420,14 +420,6 @@ static size_t FileSink(const unsigned char* data, size_t len, int fd) {
return done; 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, int applypatch(const char* source_filename, const char* target_filename,
const char* target_sha1_str, size_t /* target_size */, const char* target_sha1_str, size_t /* target_size */,
const std::vector<std::string>& patch_sha1s, const std::vector<std::string>& patch_sha1s,
@@ -562,8 +554,8 @@ static int GenerateTarget(const FileContents& source_file, const std::unique_ptr
CHECK(android::base::StartsWith(target_filename, "EMMC:")); CHECK(android::base::StartsWith(target_filename, "EMMC:"));
// We still write the original source to cache, in case the partition write is interrupted. // We write the original source to cache, in case the partition write is interrupted.
if (MakeFreeSpaceOnCache(source_file.data.size()) < 0) { if (!CheckAndFreeSpaceOnCache(source_file.data.size())) {
LOG(ERROR) << "Not enough free space on /cache"; LOG(ERROR) << "Not enough free space on /cache";
return 1; return 1;
} }
+6 -6
View File
@@ -150,22 +150,22 @@ static int64_t FreeSpaceForFile(const std::string& filename) {
return free_space; return free_space;
} }
int MakeFreeSpaceOnCache(size_t bytes_needed) { bool CheckAndFreeSpaceOnCache(size_t bytes) {
#ifndef __ANDROID__ #ifndef __ANDROID__
// TODO(xunchang): Implement a heuristic cache size check during host simulation. // 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"; << ") bytes free space on /cache; program is running on host";
return 0; return true;
#endif #endif
std::vector<std::string> dirs{ "/cache", Paths::Get().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, dirname, FreeSpaceForFile)) {
return 0; return true;
} }
} }
return -1; return false;
} }
bool RemoveFilesInDirectory(size_t bytes_needed, const std::string& dirname, bool RemoveFilesInDirectory(size_t bytes_needed, const std::string& dirname,
+4 -5
View File
@@ -40,10 +40,6 @@ using SinkFn = std::function<size_t(const unsigned char*, size_t)>;
int ShowLicenses(); 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 // Parses a given string of 40 hex digits into 20-byte array 'digest'. 'str' may contain only the
// digest or be of the form "<digest>:<anything>". Returns 0 on success, or -1 on any error. // digest or be of the form "<digest>:<anything>". Returns 0 on success, or -1 on any error.
int ParseSha1(const std::string& str, uint8_t* digest); 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 // 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 // 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. // partition. |space_checker| should return the size of the free space, or -1 on error.
+3 -3
View File
@@ -827,7 +827,7 @@ static int WriteStash(const std::string& base, const std::string& id, int blocks
return -1; return -1;
} }
if (checkspace && CacheSizeCheck(blocks * BLOCKSIZE) != 0) { if (checkspace && !CheckAndFreeSpaceOnCache(blocks * BLOCKSIZE)) {
LOG(ERROR) << "not enough space to write stash"; LOG(ERROR) << "not enough space to write stash";
return -1; return -1;
} }
@@ -919,7 +919,7 @@ static int CreateStash(State* state, size_t maxblocks, const std::string& base)
return -1; return -1;
} }
if (CacheSizeCheck(max_stash_size) != 0) { if (!CheckAndFreeSpaceOnCache(max_stash_size)) {
ErrorAbort(state, kStashCreationFailure, "not enough space for stash (%zu needed)", ErrorAbort(state, kStashCreationFailure, "not enough space for stash (%zu needed)",
max_stash_size); max_stash_size);
return -1; return -1;
@@ -951,7 +951,7 @@ static int CreateStash(State* state, size_t maxblocks, const std::string& base)
if (max_stash_size > existing) { if (max_stash_size > existing) {
size_t needed = max_stash_size - existing; size_t needed = max_stash_size - existing;
if (CacheSizeCheck(needed) != 0) { if (!CheckAndFreeSpaceOnCache(needed)) {
ErrorAbort(state, kStashCreationFailure, "not enough space for stash (%zu more needed)", ErrorAbort(state, kStashCreationFailure, "not enough space for stash (%zu more needed)",
needed); needed);
return -1; return -1;
+1 -1
View File
@@ -686,7 +686,7 @@ Value* ApplyPatchSpaceFn(const char* name, State* state,
} }
// Skip the cache size check if the update is a retry. // Skip the cache size check if the update is a retry.
if (state->is_retry || CacheSizeCheck(bytes) == 0) { if (state->is_retry || CheckAndFreeSpaceOnCache(bytes)) {
return StringValue("t"); return StringValue("t");
} }
return StringValue(""); return StringValue("");