summaryrefslogtreecommitdiffstats
path: root/applypatch/freecache.cpp
diff options
context:
space:
mode:
authorTao Bao <tbao@google.com>2018-07-12 01:32:10 +0200
committerTao Bao <tbao@google.com>2018-07-12 19:42:00 +0200
commit49750f15ed2db55247cc03170fec1eed617d9454 (patch)
tree8745ee7e013df3e0cf9fb8373d0bc66cf7453906 /applypatch/freecache.cpp
parentMerge "applypatch: Switch freecache.cpp to libbase logging." (diff)
downloadandroid_bootable_recovery-49750f15ed2db55247cc03170fec1eed617d9454.tar
android_bootable_recovery-49750f15ed2db55247cc03170fec1eed617d9454.tar.gz
android_bootable_recovery-49750f15ed2db55247cc03170fec1eed617d9454.tar.bz2
android_bootable_recovery-49750f15ed2db55247cc03170fec1eed617d9454.tar.lz
android_bootable_recovery-49750f15ed2db55247cc03170fec1eed617d9454.tar.xz
android_bootable_recovery-49750f15ed2db55247cc03170fec1eed617d9454.tar.zst
android_bootable_recovery-49750f15ed2db55247cc03170fec1eed617d9454.zip
Diffstat (limited to 'applypatch/freecache.cpp')
-rw-r--r--applypatch/freecache.cpp32
1 files changed, 30 insertions, 2 deletions
diff --git a/applypatch/freecache.cpp b/applypatch/freecache.cpp
index 5a08a63ed..4989b7496 100644
--- a/applypatch/freecache.cpp
+++ b/applypatch/freecache.cpp
@@ -16,10 +16,12 @@
#include <dirent.h>
#include <errno.h>
+#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
+#include <sys/statfs.h>
#include <unistd.h>
#include <algorithm>
@@ -130,6 +132,24 @@ static unsigned int GetLogIndex(const std::string& log_name) {
return std::numeric_limits<unsigned int>::max();
}
+// Returns the amount of free space (in bytes) on the filesystem containing filename, or -1 on
+// error.
+static int64_t FreeSpaceForFile(const std::string& filename) {
+ struct statfs sf;
+ if (statfs(filename.c_str(), &sf) == -1) {
+ PLOG(ERROR) << "Failed to statfs " << filename;
+ return -1;
+ }
+
+ int64_t free_space = static_cast<int64_t>(sf.f_bsize) * sf.f_bavail;
+ if (sf.f_bsize == 0 || free_space / sf.f_bsize != sf.f_bavail) {
+ LOG(ERROR) << "Invalid block size or overflow (sf.f_bsize " << sf.f_bsize << ", sf.f_bavail "
+ << sf.f_bavail << ")";
+ return -1;
+ }
+ return free_space;
+}
+
int MakeFreeSpaceOnCache(size_t bytes_needed) {
#ifndef __ANDROID__
// TODO(xunchang): Implement a heuristic cache size check during host simulation.
@@ -149,7 +169,7 @@ int MakeFreeSpaceOnCache(size_t bytes_needed) {
}
bool RemoveFilesInDirectory(size_t bytes_needed, const std::string& dirname,
- const std::function<size_t(const std::string&)>& space_checker) {
+ const std::function<int64_t(const std::string&)>& space_checker) {
struct stat st;
if (stat(dirname.c_str(), &st) == -1) {
PLOG(ERROR) << "Failed to stat " << dirname;
@@ -160,7 +180,11 @@ bool RemoveFilesInDirectory(size_t bytes_needed, const std::string& dirname,
return false;
}
- size_t free_now = space_checker(dirname);
+ int64_t free_now = space_checker(dirname);
+ if (free_now == -1) {
+ LOG(ERROR) << "Failed to check free space for " << dirname;
+ return false;
+ }
LOG(INFO) << free_now << " bytes free on " << dirname << " (" << bytes_needed << " needed)";
if (free_now >= bytes_needed) {
@@ -201,6 +225,10 @@ bool RemoveFilesInDirectory(size_t bytes_needed, const std::string& dirname,
}
free_now = space_checker(dirname);
+ if (free_now == -1) {
+ LOG(ERROR) << "Failed to check free space for " << dirname;
+ return false;
+ }
LOG(INFO) << "Deleted " << file << "; now " << free_now << " bytes free";
if (free_now >= bytes_needed) {
return true;