Add check for available disk space

This commit is contained in:
2025-08-17 10:08:49 -04:00
parent 2e4a713b1c
commit f7facd3319
4 changed files with 37 additions and 5 deletions

View File

@@ -3,6 +3,8 @@
#include <stdio.h>
#include <limits>
#include <cmath>
#include <filesystem>
#include <sys/statvfs.h>
#include <common/string.hpp>
class Utility
@@ -16,6 +18,7 @@ class Utility
static bool fmod(double dividend,double divisor);
static bool fmod(double dividend, double divisor,double epsilon);
static String byteCountToString(double bytesTransferred, bool perSecond = true);
static size_t getAvailableDiskSpace(const String &path);
private:
static String pad(String theString,char padChar,int toLength);
static String addCommas(String& theString);
@@ -172,4 +175,23 @@ bool Utility::fmod(double dividend, double divisor)
// Check if the absolute remainder is less than epsilon
return std::fabs(remainder) < epsilon;
}
// inline
// size_t Utility::getAvailableDiskSpace(const String& path)
// {
// struct statvfs stat;
// if (::statvfs(path, &stat) != 0)
// {
// throw new Exception("Error calling statvfs to get available disk space.");
// }
// // Calculate available space in bytes. Multiply Number of free inodes * Fragment size
// return (size_t)stat.f_bavail * stat.f_frsize;
// }
inline
size_t Utility::getAvailableDiskSpace(const String& path)
{
std::filesystem::space_info si = std::filesystem::space(path.str());
return si.available;
}
#endif