From 49750f15ed2db55247cc03170fec1eed617d9454 Mon Sep 17 00:00:00 2001 From: Tao Bao Date: Wed, 11 Jul 2018 16:32:10 -0700 Subject: applypatch: Fix the return type of FreeSpaceForFile(). Prior to this CL, FreeSpaceForFile() was returning `size_t`, which may overflow on ILP32 when called on a partition with 4GiB+ free space. Additionally, it was returning static_cast(-1) on error, but the caller in freecache.cpp didn't check for that. This CL changes its return type to `int64_t`, and moves the function into freecache.cpp since there's no external caller. Test: Run recovery_unit_test on marlin. Test: Code search shows no external user of FreeSpaceForFile(). Change-Id: I00f501a057726e1f1ab69f367c46c77b30f2d774 --- applypatch/freecache.cpp | 32 ++++++++++++++++++++++++++++++-- 1 file changed, 30 insertions(+), 2 deletions(-) (limited to 'applypatch/freecache.cpp') 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 #include +#include #include #include #include #include +#include #include #include @@ -130,6 +132,24 @@ static unsigned int GetLogIndex(const std::string& log_name) { return std::numeric_limits::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(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& space_checker) { + const std::function& 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; -- cgit v1.2.3