From 1b095e725e8f1e84701a87f0111e68ecb4193ed3 Mon Sep 17 00:00:00 2001 From: Thilo Graf Date: Sat, 7 Feb 2015 14:59:20 +0100 Subject: [PATCH] CFileHelpers: add function getDirSize() --- src/system/helpers.cpp | 28 ++++++++++++++++++++++++++++ src/system/helpers.h | 2 ++ 2 files changed, 30 insertions(+) diff --git a/src/system/helpers.cpp b/src/system/helpers.cpp index abdaeb1a3..0b5b82a49 100644 --- a/src/system/helpers.cpp +++ b/src/system/helpers.cpp @@ -722,6 +722,34 @@ bool CFileHelpers::removeDir(const char *Dir) return true; } +u_int64_t CFileHelpers::getDirSize(const char *dirname) +{ + DIR *d; + struct dirent *de; + struct stat buf; + int exists; + u_int64_t total_size = 0; + + d = opendir(dirname); + if (d == NULL) { + perror("prsize"); + return 0; + } + + for (de = readdir(d); de != NULL; de = readdir(d)) { + exists = stat(de->d_name, &buf); + if (exists < 0) { + fprintf(stderr, "Couldn't stat %s\n", de->d_name); + } else { + total_size += buf.st_size; + } + } + + closedir(d); + + return total_size; +} + static int hdd_open_dev(const char * fname) { FILE * fp; diff --git a/src/system/helpers.h b/src/system/helpers.h index bf1269620..3b9060c60 100644 --- a/src/system/helpers.h +++ b/src/system/helpers.h @@ -91,6 +91,8 @@ class CFileHelpers static int createDir(std::string& Dir, mode_t mode = 755); static int createDir(const char *Dir, mode_t mode = 755){std::string dir = std::string(Dir);return createDir(dir, mode);} static bool removeDir(const char *Dir); + static uint64_t getDirSize(const char *dir); + static uint64_t getDirSize(const std::string& dir){return getDirSize(dir.c_str());}; }; std::string to_string(int);