From 298665593093130d025e5c226c4647246712f7f0 Mon Sep 17 00:00:00 2001 From: Dmytro Bogovych Date: Wed, 3 Jun 2020 12:33:48 +0300 Subject: [PATCH] - check disk free space on Linuxes --- src/engine/helper/HL_File.cpp | 17 +++++++++++++++++ src/engine/helper/HL_File.h | 4 ++++ 2 files changed, 21 insertions(+) diff --git a/src/engine/helper/HL_File.cpp b/src/engine/helper/HL_File.cpp index 64f982ed..c042a93d 100644 --- a/src/engine/helper/HL_File.cpp +++ b/src/engine/helper/HL_File.cpp @@ -3,6 +3,8 @@ #if defined(TARGET_LINUX) || defined(TARGET_OSX) # include +# include +# include #endif 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; } + +// Returns free space on volume for path +size_t FileHelper::getFreespace(const std::string& path) +{ + size_t r = static_cast(-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; +} diff --git a/src/engine/helper/HL_File.h b/src/engine/helper/HL_File.h index 0911ac82..4616d8dd 100644 --- a/src/engine/helper/HL_File.h +++ b/src/engine/helper/HL_File.h @@ -19,6 +19,10 @@ public: static std::string addTrailingSlash(const std::string& s); 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