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
This commit is contained in:
Tao Bao
2018-07-11 15:55:32 -07:00
parent b46e565fd8
commit 5ee25666cc
5 changed files with 16 additions and 25 deletions

View File

@@ -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<std::string> 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,