From: Gerd von Egidy Date: Wed, 16 Dec 2015 14:19:55 +0000 (+0100) Subject: add Stat::BytesOnDisk() X-Git-Tag: v2.8~14 X-Git-Url: http://developer.intra2net.com/git/?a=commitdiff_plain;h=a9f2cccc242a912111c187537f62e2e67eebeb63;p=libi2ncommon add Stat::BytesOnDisk() --- diff --git a/src/filefunc.cpp b/src/filefunc.cpp index 8fd48cd..6f07011 100644 --- a/src/filefunc.cpp +++ b/src/filefunc.cpp @@ -117,6 +117,10 @@ void Stat::stat(const std::string& path, bool follow_links) Mtime = stat_info.st_mtime; Ctime = stat_info.st_atime; + // the stat(2) manpage for linux defines that st_blocks is given in a number of 512-byte-blocks. + BytesOnDisk = stat_info.st_blocks; + BytesOnDisk*=(long long)512; + IsLink= S_ISLNK( stat_info.st_mode ); IsRegular= S_ISREG( stat_info.st_mode ); IsDirectory= S_ISDIR( stat_info.st_mode ); @@ -145,6 +149,7 @@ void Stat::clear() Gid = 0; DeviceType = 0; Size = 0; + BytesOnDisk = 0; Atime = 0; Mtime = 0; Ctime = 0; diff --git a/src/filefunc.hxx b/src/filefunc.hxx index 7e71311..e338172 100644 --- a/src/filefunc.hxx +++ b/src/filefunc.hxx @@ -28,6 +28,12 @@ on this file might be covered by the GNU General Public License. #include "userfunc.hpp" #include +// make sure we have proper large file support +#include +#ifndef _GNU_SOURCE + #define _GNU_SOURCE +#endif + namespace I2n { /** @@ -61,6 +67,9 @@ public: time_t mtime() const { return Mtime; } time_t ctime() const { return Ctime; } + // bytes used on disk, calculated from allocated blocks + long long bytes_on_disk() const { return BytesOnDisk; } + nlink_t num_hard_links() const { return NumLinks; } dev_t device_type() const { return DeviceType; } time_t last_modified_time() const { return Mtime; } @@ -125,6 +134,7 @@ protected: gid_t Gid; dev_t DeviceType; off_t Size; + long long BytesOnDisk; time_t Atime; time_t Mtime; time_t Ctime; diff --git a/test/test_filefunc.cpp b/test/test_filefunc.cpp index fd1efb0..3335241 100644 --- a/test/test_filefunc.cpp +++ b/test/test_filefunc.cpp @@ -114,6 +114,20 @@ BOOST_AUTO_TEST_CASE(StatTest1) BOOST_CHECK_EQUAL( false, stat.is_block_device() ); } // eo StatTest1 +BOOST_AUTO_TEST_CASE(StatSize) +{ + write_file("/tmp/test","some nice content to make sure the file system optimizations don't inline this into the inode block"); + + I2n::Stat stat("/tmp/test"); + + BOOST_CHECK_EQUAL( true, (bool)stat ); + BOOST_CHECK_EQUAL( true, stat.is_regular_file() ); + + BOOST_CHECK_EQUAL( true, ( stat.bytes_on_disk() >= 512 ) ); + + unlink("/tmp/test"); +} // eo StatSize + BOOST_AUTO_TEST_CASE(StatRecheck) { // just to be sure