- check disk free space on Linuxes

This commit is contained in:
Dmytro Bogovych 2020-06-03 12:33:48 +03:00
parent 086581a2b1
commit 2986655930
2 changed files with 21 additions and 0 deletions

View File

@ -3,6 +3,8 @@
#if defined(TARGET_LINUX) || defined(TARGET_OSX) #if defined(TARGET_LINUX) || defined(TARGET_OSX)
# include <unistd.h> # include <unistd.h>
# include <sys/statvfs.h>
# include <memory.h>
#endif #endif
bool FileHelper::exists(const std::string& s) bool FileHelper::exists(const std::string& s)
@ -88,3 +90,18 @@ std::string FileHelper::mergePathes(const std::string& s1, const std::string& s2
return result; return result;
} }
// Returns free space on volume for path
size_t FileHelper::getFreespace(const std::string& path)
{
size_t r = static_cast<size_t>(-1);
#if defined(TARGET_LINUX)
struct statvfs stats; memset(&stats, 0, sizeof stats);
int retcode = statvfs(path.c_str(), &stats);
if (retcode == 0)
r = stats.f_bfree * stats.f_bsize;
#endif
return r;
}

View File

@ -19,6 +19,10 @@ public:
static std::string addTrailingSlash(const std::string& s); static std::string addTrailingSlash(const std::string& s);
static std::string mergePathes(const std::string& s1, const std::string& s2); static std::string mergePathes(const std::string& s1, const std::string& s2);
// Returns free space on volume for path
// Works for Linux only. For other systems (size_t)-1 is returned (for errors too)
static size_t getFreespace(const std::string& path);
}; };
#endif #endif