From: Joel Rosdahl Date: Sun, 29 Sep 2019 13:07:24 +0000 (+0200) Subject: Add Util::get_file_size X-Git-Tag: v4.0~763 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=a6c2e201e5763ea1f04b39c4715f982ee300cd84;p=thirdparty%2Fccache.git Add Util::get_file_size --- diff --git a/src/Util.cpp b/src/Util.cpp index e33af7013..26dfac1f0 100644 --- a/src/Util.cpp +++ b/src/Util.cpp @@ -163,6 +163,18 @@ for_each_level_1_subdir(const std::string& cache_dir, progress_receiver(1.0); } +bool +get_file_size(const std::string& path, uint64_t& size) +{ + struct stat st; + if (stat(path.c_str(), &st) == 0) { + size = st.st_size; + return true; + } else { + return false; + } +} + void get_level_1_files(const std::string& dir, const ProgressReceiver& progress_receiver, diff --git a/src/Util.hpp b/src/Util.hpp index 21eca3538..e2bd39574 100644 --- a/src/Util.hpp +++ b/src/Util.hpp @@ -71,6 +71,9 @@ void for_each_level_1_subdir(const std::string& cache_dir, const SubdirVisitor& visitor, const ProgressReceiver& progress_receiver); +// Get file size. Returns true if file exists, otherwise false. +bool get_file_size(const std::string& path, uint64_t& size); + // Get a list of files in a level 1 subdirectory of the cache. // // The function works under the assumption that directory entries with one diff --git a/unittest/test_Util.cpp b/unittest/test_Util.cpp index a9fcfd205..b6d205923 100644 --- a/unittest/test_Util.cpp +++ b/unittest/test_Util.cpp @@ -104,6 +104,16 @@ TEST_CASE("Util::for_each_level_1_subdir") CHECK(actual == expected); } +TEST_CASE("Util::get_file_size") +{ + uint64_t size; + CHECK(!Util::get_file_size("does not exist", size)); + + Util::write_file("foo", "foo"); + CHECK(Util::get_file_size("foo", size)); + CHECK(size == 3); +} + TEST_CASE("Util::get_level_1_files") { Util::create_dir("e/m/p/t/y");