Skip to content

Commit

Permalink
Add getFreeSpace()
Browse files Browse the repository at this point in the history
  • Loading branch information
halfgaar committed Nov 20, 2024
1 parent 016057b commit ac997ee
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
13 changes: 13 additions & 0 deletions utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ See LICENSE for license details.
#include "utils.h"

#include <sys/time.h>
#include <sys/statvfs.h>
#include <algorithm>
#include <cstdio>
#include <cstring>
Expand Down Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down

0 comments on commit ac997ee

Please sign in to comment.