Add util::freespace to check for free space on a path. (with tests)
Originally committed to SVN as r5346.
This commit is contained in:
parent
bbc0f6f859
commit
8240d3e6b6
4 changed files with 60 additions and 2 deletions
|
@ -19,10 +19,12 @@
|
|||
/// @ingroup libaegisub
|
||||
|
||||
#ifndef LAGI_PRE
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <sys/param.h>
|
||||
#include <sys/mount.h>
|
||||
|
||||
#include <errno.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include <climits>
|
||||
#include <locale>
|
||||
|
@ -41,6 +43,7 @@ void str_lower(std::string &str) {
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
int strtoi(std::string &str) {
|
||||
long l = strtol(str.c_str(), NULL, 10);
|
||||
|
||||
|
@ -50,5 +53,24 @@ int strtoi(std::string &str) {
|
|||
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 agi
|
||||
|
|
|
@ -32,6 +32,13 @@
|
|||
namespace agi {
|
||||
namespace util {
|
||||
|
||||
|
||||
/// Whether the path is a file or directory.
|
||||
enum PathType {
|
||||
TypeFile, ///< File
|
||||
TypeDir ///< Directory
|
||||
};
|
||||
|
||||
// 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)); }
|
||||
|
||||
|
@ -56,6 +63,11 @@ namespace agi {
|
|||
// @param str Input string
|
||||
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 {
|
||||
template<class T>
|
||||
void operator()(T* ptr) const {
|
||||
|
|
|
@ -10,6 +10,8 @@
|
|||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/param.h>
|
||||
#include <sys/mount.h>
|
||||
#ifdef HAVE_SYS_TIME_H
|
||||
# include <sys/time.h>
|
||||
#else
|
||||
|
|
|
@ -97,6 +97,28 @@ TEST_F(lagi_util, UtilstrtoiValidString) {
|
|||
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));
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
|
Loading…
Reference in a new issue