diff --git a/utils.cpp b/utils.cpp index 6b78bfd..57944d2 100644 --- a/utils.cpp +++ b/utils.cpp @@ -13,6 +13,7 @@ See LICENSE for license details. #include "utils.h" #include +#include #include #include #include @@ -571,6 +572,18 @@ size_t getFileSize(const std::string &path) return statbuf.st_size; } +size_t getFreeSpace(const std::string &path) +{ + struct statvfs statbuf; + memset(&statbuf, 0, sizeof(struct statvfs)); + + if (statvfs(path.c_str(), &statbuf) < 0) + throw std::runtime_error("Can't get free space of " + path); + + const size_t result {statbuf.f_bsize * statbuf.f_bfree}; + return result; +} + std::string sockaddrToString(const sockaddr *addr) { if (!addr) diff --git a/utils.h b/utils.h index 29ec347..3931722 100644 --- a/utils.h +++ b/utils.h @@ -192,6 +192,7 @@ std::string_view dirnameOf(std::string_view fname); BindAddr getBindAddr(int family, const std::string &bindAddress, int port); size_t getFileSize(const std::string &path); +size_t getFreeSpace(const std::string &path); std::string sockaddrToString(const struct sockaddr *addr);