]> git.ipfire.org Git - thirdparty/ccache.git/commitdiff
enhance: Remember path in Stat
authorJoel Rosdahl <joel@rosdahl.net>
Thu, 10 Nov 2022 08:03:08 +0000 (09:03 +0100)
committerJoel Rosdahl <joel@rosdahl.net>
Sun, 27 Nov 2022 20:33:51 +0000 (21:33 +0100)
src/Stat.cpp
src/Stat.hpp
unittest/test_Stat.cpp

index 356bd920c63b0b3a34d3cf06bced885e34b166cf..87b20203c07ce732f569427d0d26b7d56b967def 100644 (file)
@@ -207,6 +207,7 @@ Stat::Stat(StatFunction stat_function,
            const std::string& path,
            Stat::OnError on_error)
 {
+  m_path = path;
   int result = stat_function(path.c_str(), &m_stat);
   if (result == 0) {
     m_errno = 0;
index 5c81a5bad6cb3926c128d799ab83e65a8e2fc158..42fe563f5e5d61759b81b2f692638f34cb37244a 100644 (file)
@@ -116,6 +116,9 @@ public:
   // otherwise false.
   operator bool() const;
 
+  // Return the path that this stat result refers to.
+  const std::string& path() const;
+
   // Return whether this object refers to the same device and i-node as `other`
   // does.
   bool same_inode_as(const Stat& other) const;
@@ -148,6 +151,7 @@ protected:
   Stat(StatFunction stat_function, const std::string& path, OnError on_error);
 
 private:
+  std::string m_path;
   stat_t m_stat;
   int m_errno;
 
@@ -170,6 +174,12 @@ Stat::same_inode_as(const Stat& other) const
   return m_errno == 0 && device() == other.device() && inode() == other.inode();
 }
 
+inline const std::string&
+Stat::path() const
+{
+  return m_path;
+}
+
 inline int
 Stat::error_number() const
 {
index fbd0afe5c5ea49ed6bf9b31b9e11a2436281a82d..58830819959273df1b803b2f43dad4c5542dc0af 100644 (file)
@@ -198,6 +198,15 @@ TEST_CASE("Same i-node as")
   CHECK(!Stat::stat("nonexistent").same_inode_as(Stat::stat("nonexistent")));
 }
 
+TEST_CASE("Get path")
+{
+  TestContext test_context;
+
+  util::write_file("a", "");
+  CHECK(Stat::stat("a").path() == "a");
+  CHECK(Stat::stat("does_not_exist").path() == "does_not_exist");
+}
+
 TEST_CASE("Return values when file is missing")
 {
   auto stat = Stat::stat("does_not_exist");