fs: Add dir_size() function to obtain the total size of directory's contents

This commit is contained in:
Ignacio R. Morelle 2014-06-19 19:43:57 -04:00
parent 8601c79647
commit c5e81ca5f5
2 changed files with 24 additions and 0 deletions

View File

@ -914,6 +914,27 @@ int file_size(const std::string& fname)
return buf.st_size;
}
int dir_size(const std::string& path)
{
std::vector<std::string> files, dirs;
get_files_in_dir(path, &files, &dirs, ENTIRE_FILE_PATH);
int res = 0;
BOOST_FOREACH(const std::string& file_path, files)
{
res += file_size(file_path);
}
BOOST_FOREACH(const std::string& dir_path, dirs)
{
// FIXME: this could result in infinite recursion with symlinks!!
res += dir_size(dir_path);
}
return res;
}
std::string file_name(const std::string& file)
// Analogous to POSIX basename(3), but for C++ string-object pathnames
{

View File

@ -159,6 +159,9 @@ const file_tree_checksum& data_tree_checksum(bool reset = false);
/** Returns the size of a file, or -1 if the file doesn't exist. */
int file_size(const std::string& fname);
/** Returns the sum of the sizes of the files contained in a directory. */
int dir_size(const std::string& path);
bool ends_with(const std::string& str, const std::string& suffix);
/**