From ac997ee9c720975033296374ea506faed32732de Mon Sep 17 00:00:00 2001 From: Wiebe Cazemier Date: Wed, 20 Nov 2024 21:37:42 +0100 Subject: [PATCH] Add getFreeSpace() --- utils.cpp | 13 +++++++++++++ utils.h | 1 + 2 files changed, 14 insertions(+) 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);