From eaa5b40a880eaceb72567d82981cda27624d72ae Mon Sep 17 00:00:00 2001 From: Joel Rosdahl Date: Thu, 10 Nov 2022 09:03:08 +0100 Subject: [PATCH] enhance: Remember path in Stat --- src/Stat.cpp | 1 + src/Stat.hpp | 10 ++++++++++ unittest/test_Stat.cpp | 9 +++++++++ 3 files changed, 20 insertions(+) diff --git a/src/Stat.cpp b/src/Stat.cpp index 356bd920c..87b20203c 100644 --- a/src/Stat.cpp +++ b/src/Stat.cpp @@ -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; diff --git a/src/Stat.hpp b/src/Stat.hpp index 5c81a5bad..42fe563f5 100644 --- a/src/Stat.hpp +++ b/src/Stat.hpp @@ -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 { diff --git a/unittest/test_Stat.cpp b/unittest/test_Stat.cpp index fbd0afe5c..588308199 100644 --- a/unittest/test_Stat.cpp +++ b/unittest/test_Stat.cpp @@ -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"); -- 2.47.2