mirror of
https://github.com/wesnoth/wesnoth
synced 2025-05-07 01:34:38 +00:00
fs: Add dir_size() function to obtain the total size of directory's contents
This commit is contained in:
parent
8601c79647
commit
c5e81ca5f5
@ -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
|
||||
{
|
||||
|
@ -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);
|
||||
|
||||
/**
|
||||
|
Loading…
x
Reference in New Issue
Block a user