]> git.ipfire.org Git - thirdparty/ccache.git/commitdiff
Add Stat::same_inode_as method
authorJoel Rosdahl <joel@rosdahl.net>
Thu, 20 Feb 2020 20:20:07 +0000 (21:20 +0100)
committerJoel Rosdahl <joel@rosdahl.net>
Sun, 23 Feb 2020 20:11:03 +0000 (21:11 +0100)
src/Stat.hpp
unittest/test_Stat.cpp

index 19550c20b85268438e5971015dc741899ae9790d..9f9fa1601f252d5a3ef6770b28643a8216bba28c 100644 (file)
@@ -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
 {
index 9f5c6ad16a30eb311111a74dcb4a9883e138e5dc..ba140bd8a25464241c6ea8d18029831e8adbafb4 100644 (file)
@@ -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");