From: Joel Rosdahl Date: Thu, 20 Feb 2020 20:20:07 +0000 (+0100) Subject: Add Stat::same_inode_as method X-Git-Tag: v4.0~591 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=7caa6c00710844800763534fa2b6654c5da804f8;p=thirdparty%2Fccache.git Add Stat::same_inode_as method --- diff --git a/src/Stat.hpp b/src/Stat.hpp index 19550c20b..9f9fa1601 100644 --- a/src/Stat.hpp +++ b/src/Stat.hpp @@ -61,6 +61,10 @@ public: // otherwise false. operator bool() const; + // Return whether this object refers to the same device and i-node as `other` + // does. + bool same_inode_as(const Stat& other) const; + // Return errno from the (l)stat call (0 if successful). int error_number() const; @@ -85,6 +89,9 @@ protected: private: struct stat m_stat; int m_errno; + + bool operator==(const Stat&) const; + bool operator!=(const Stat&) const; }; inline Stat::Stat() : m_stat{}, m_errno(-1) @@ -115,6 +122,12 @@ inline Stat::operator bool() const return m_errno == 0; } +inline bool +Stat::same_inode_as(const Stat& other) const +{ + return device() == other.device() && inode() == other.inode(); +} + inline int Stat::error_number() const { diff --git a/unittest/test_Stat.cpp b/unittest/test_Stat.cpp index 9f5c6ad16..ba140bd8a 100644 --- a/unittest/test_Stat.cpp +++ b/unittest/test_Stat.cpp @@ -52,6 +52,25 @@ TEST_CASE("Named constructors") Equals("failed to stat does_not_exist: No such file or directory")); } +TEST_CASE("Same i-node as") +{ + Util::write_file("a", ""); + Util::write_file("b", ""); + auto a_stat = Stat::stat("a"); + auto b_stat = Stat::stat("b"); + + CHECK(a_stat.same_inode_as(a_stat)); +#ifdef _WIN32 // no i-node concept + (void)b_stat; +#else + CHECK(!a_stat.same_inode_as(b_stat)); +#endif + + Util::write_file("a", "change size"); + auto new_a_stat = Stat::stat("a"); + CHECK(new_a_stat.same_inode_as(a_stat)); +} + TEST_CASE("Return values when file is missing") { auto stat = Stat::stat("does_not_exist");