From 1a78fd2931c61f0c3b1332f9f094d8a6cebb3e27 Mon Sep 17 00:00:00 2001 From: Nathan Hourt Date: Mon, 13 Oct 2014 15:17:14 -0400 Subject: [PATCH] Add directory_size call Recursively iterate the specified directory, summing up the files inside, and return the total size. --- include/fc/filesystem.hpp | 1 + src/filesystem.cpp | 21 +++++++++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/include/fc/filesystem.hpp b/include/fc/filesystem.hpp index 8e6bc7c..9e8ebb9 100644 --- a/include/fc/filesystem.hpp +++ b/include/fc/filesystem.hpp @@ -152,6 +152,7 @@ namespace fc { path make_relative(const path& from, const path& to); path canonical( const path& p ); uint64_t file_size( const path& p ); + uint64_t directory_size( const path& p ); bool remove( const path& p ); void copy( const path& from, const path& to ); void rename( const path& from, const path& to ); diff --git a/src/filesystem.cpp b/src/filesystem.cpp index 7f0d887..e06bb6e 100644 --- a/src/filesystem.cpp +++ b/src/filesystem.cpp @@ -217,6 +217,26 @@ namespace fc { 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); } 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 copy( const path& f, const path& t ) { try { @@ -445,4 +465,5 @@ namespace fc { static fc::path appCurrentPath = boost::filesystem::current_path(); return appCurrentPath; } + }