Add util::freespace to check for free space on a path. (with tests)

Originally committed to SVN as r5346.
This commit is contained in:
Amar Takhar 2011-02-10 23:55:59 +00:00
parent bbc0f6f859
commit 8240d3e6b6
4 changed files with 60 additions and 2 deletions

View file

@ -19,10 +19,12 @@
/// @ingroup libaegisub /// @ingroup libaegisub
#ifndef LAGI_PRE #ifndef LAGI_PRE
#include <stdio.h> #include <sys/param.h>
#include <stdlib.h> #include <sys/mount.h>
#include <errno.h> #include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <climits> #include <climits>
#include <locale> #include <locale>
@ -41,6 +43,7 @@ void str_lower(std::string &str) {
} }
} }
int strtoi(std::string &str) { int strtoi(std::string &str) {
long l = strtol(str.c_str(), NULL, 10); long l = strtol(str.c_str(), NULL, 10);
@ -50,5 +53,24 @@ int strtoi(std::string &str) {
return (int)l; return (int)l;
} }
uint64_t freespace(std::string &path, PathType type) {
struct statfs fs;
std::string check(path);
if (type == TypeFile)
check.assign(DirName(path));
acs::CheckDirRead(check);
if ((statfs(check.c_str(), &fs)) == 0) {
return fs.f_bsize * fs.f_bavail;
} else {
/// @todo We need a collective set of exceptions for ENOTDIR, EIO etc.
throw("Failed getting free space");
}
}
} // namespace util } // namespace util
} // namespace agi } // namespace agi

View file

@ -32,6 +32,13 @@
namespace agi { namespace agi {
namespace util { namespace util {
/// Whether the path is a file or directory.
enum PathType {
TypeFile, ///< File
TypeDir ///< Directory
};
// Calculate midpoint from a list of Integers // Calculate midpoint from a list of Integers
template<typename T> inline T mid(T a, T b, T c) { return std::max(a, std::min(b, c)); } template<typename T> inline T mid(T a, T b, T c) { return std::max(a, std::min(b, c)); }
@ -56,6 +63,11 @@ namespace agi {
// @param str Input string // @param str Input string
int strtoi(std::string &str); int strtoi(std::string &str);
/// Check for amount of free space on a Path.
// @param path[in] Path to check
// @param type PathType (default is TypeDir)
uint64_t freespace(std::string &path, PathType type=TypeDir);
struct delete_ptr { struct delete_ptr {
template<class T> template<class T>
void operator()(T* ptr) const { void operator()(T* ptr) const {

View file

@ -10,6 +10,8 @@
#include <stdint.h> #include <stdint.h>
#include <string.h> #include <string.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <sys/param.h>
#include <sys/mount.h>
#ifdef HAVE_SYS_TIME_H #ifdef HAVE_SYS_TIME_H
# include <sys/time.h> # include <sys/time.h>
#else #else

View file

@ -97,6 +97,28 @@ TEST_F(lagi_util, UtilstrtoiValidString) {
EXPECT_EQ(i, 24); EXPECT_EQ(i, 24);
} }
TEST_F(lagi_util, UtilfreespaceFile) {
std::string path("./data/somefile");
EXPECT_NO_THROW(util::freespace(path, util::TypeFile));
EXPECT_ANY_THROW(util::freespace(path));
}
TEST_F(lagi_util, UtilfreespaceDir) {
std::string path("./data");
EXPECT_NO_THROW(util::freespace(path));
}
TEST_F(lagi_util, UtilfreespaceNoAccess) {
std::string path("./data/dir_access_denied");
EXPECT_THROW(util::freespace(path), acs::AcsAccess);
}
TEST_F(lagi_util, UtilfreespaceInvalid) {
std::string path("/nonexistent");
EXPECT_ANY_THROW(util::freespace(path));
}