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<size_t>(-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
diff --git a/applypatch/applypatch.cpp b/applypatch/applypatch.cpp
index eb0a2a7..13e4b1a 100644
--- a/applypatch/applypatch.cpp
+++ b/applypatch/applypatch.cpp
@@ -23,7 +23,6 @@
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
-#include <sys/statfs.h>
#include <sys/types.h>
#include <unistd.h>
@@ -421,15 +420,6 @@
return done;
}
-size_t FreeSpaceForFile(const std::string& filename) {
- struct statfs sf;
- if (statfs(filename.c_str(), &sf) != 0) {
- PLOG(ERROR) << "Failed to statfs " << filename;
- return -1;
- }
- return sf.f_bsize * sf.f_bavail;
-}
-
int CacheSizeCheck(size_t bytes) {
if (MakeFreeSpaceOnCache(bytes) < 0) {
LOG(ERROR) << "Failed to make " << bytes << " bytes available on /cache";
diff --git a/applypatch/freecache.cpp b/applypatch/freecache.cpp
index 5a08a63..4989b74 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 @@
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 @@
}
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 @@
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 @@
}
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;
diff --git a/applypatch/include/applypatch/applypatch.h b/applypatch/include/applypatch/applypatch.h
index 92db59c..88659b8 100644
--- a/applypatch/include/applypatch/applypatch.h
+++ b/applypatch/include/applypatch/applypatch.h
@@ -40,10 +40,6 @@
int ShowLicenses();
-// Returns the amount of free space (in bytes) on the filesystem containing filename, or -1 on
-// error. filename must exist.
-size_t FreeSpaceForFile(const std::string& filename);
-
// Checks whether /cache partition has at least 'bytes'-byte free space. Returns 0 on having
// sufficient space.
int CacheSizeCheck(size_t bytes);
@@ -119,8 +115,8 @@
int MakeFreeSpaceOnCache(size_t bytes_needed);
-// Removes the files in |dirname| until we have at least |bytes_needed| bytes of free space on
-// the partition. The size of the free space is returned by calling |space_checker|.
+// 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.
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);
#endif