add Stat::BytesOnDisk()
authorGerd von Egidy <gerd.von.egidy@intra2net.com>
Wed, 16 Dec 2015 14:19:55 +0000 (15:19 +0100)
committerGerd von Egidy <gerd.von.egidy@intra2net.com>
Wed, 16 Dec 2015 14:19:55 +0000 (15:19 +0100)
src/filefunc.cpp
src/filefunc.hxx
test/test_filefunc.cpp

index 8fd48cd..6f07011 100644 (file)
@@ -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;
index 7e71311..e338172 100644 (file)
@@ -28,6 +28,12 @@ on this file might be covered by the GNU General Public License.
 #include "userfunc.hpp"
 #include <set>
 
+// make sure we have proper large file support
+#include <features.h>
+#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;
index fd1efb0..3335241 100644 (file)
@@ -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