Add directory_size call

Recursively iterate the specified directory, summing up the files
inside, and return the total size.
This commit is contained in:
Nathan Hourt 2014-10-13 15:17:14 -04:00
parent b63e6a8b81
commit 1a78fd2931
2 changed files with 22 additions and 0 deletions

View file

@ -152,6 +152,7 @@ namespace fc {
path make_relative(const path& from, const path& to); path make_relative(const path& from, const path& to);
path canonical( const path& p ); path canonical( const path& p );
uint64_t file_size( const path& p ); uint64_t file_size( const path& p );
uint64_t directory_size( const path& p );
bool remove( const path& p ); bool remove( const path& p );
void copy( const path& from, const path& to ); void copy( const path& from, const path& to );
void rename( const path& from, const path& to ); void rename( const path& from, const path& to );

View file

@ -217,6 +217,26 @@ namespace fc {
bool is_directory( const path& p ) { return boost::filesystem::is_directory(p); } bool is_directory( const path& p ) { return boost::filesystem::is_directory(p); }
bool is_regular_file( const path& p ) { return boost::filesystem::is_regular_file(p); } bool is_regular_file( const path& p ) { return boost::filesystem::is_regular_file(p); }
uint64_t file_size( const path& p ) { return boost::filesystem::file_size(p); } uint64_t file_size( const path& p ) { return boost::filesystem::file_size(p); }
uint64_t directory_size(const path& p)
{
try {
FC_ASSERT( is_directory( p ) );
recursive_directory_iterator end;
uint64_t size = 0;
for( recursive_directory_iterator itr( p ); itr != end; ++itr )
{
if( is_regular_file( *itr ) )
size += file_size( *itr );
}
return size;
} catch ( ... ) {
FC_THROW( "Unable to calculate size of directory ${path}", ("path", p )("inner", fc::except_str() ) );
}
}
void remove_all( const path& p ) { boost::filesystem::remove_all(p); } void remove_all( const path& p ) { boost::filesystem::remove_all(p); }
void copy( const path& f, const path& t ) { void copy( const path& f, const path& t ) {
try { try {
@ -445,4 +465,5 @@ namespace fc {
static fc::path appCurrentPath = boost::filesystem::current_path(); static fc::path appCurrentPath = boost::filesystem::current_path();
return appCurrentPath; return appCurrentPath;
} }
} }