From 45e0aa69b2918b76b667859b69689be0648e9370 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Steffen=20Schu=CC=88mann?= Date: Sun, 12 Jul 2026 20:53:20 +0200 Subject: [PATCH] Add symlink handling tests and fix `recursive_directory_iterator` behavior with symlinks (refs #178) --- include/ghc/filesystem.hpp | 5 +++-- test/filesystem_test.cpp | 15 +++++++++++++++ 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/include/ghc/filesystem.hpp b/include/ghc/filesystem.hpp index eb01524..b47e40d 100644 --- a/include/ghc/filesystem.hpp +++ b/include/ghc/filesystem.hpp @@ -6047,12 +6047,13 @@ GHC_INLINE recursive_directory_iterator& recursive_directory_iterator::operator+ GHC_INLINE recursive_directory_iterator& recursive_directory_iterator::increment(std::error_code& ec) noexcept { bool isSymLink = (*this)->is_symlink(ec); - bool isDir = !ec && (*this)->is_directory(ec); + const bool followSymlink = (options() & directory_options::follow_directory_symlink) != directory_options::none; + bool isDir = !ec && (!isSymLink || followSymlink) && (*this)->is_directory(ec); if (isSymLink && detail::is_not_found_error(ec)) { ec.clear(); } if (!ec) { - if (recursion_pending() && isDir && (!isSymLink || (options() & directory_options::follow_directory_symlink) != directory_options::none)) { + if (recursion_pending() && isDir) { _impl->_dir_iter_stack.push(directory_iterator((*this)->path(), _impl->_options, ec)); } else { diff --git a/test/filesystem_test.cpp b/test/filesystem_test.cpp index 9c89277..f72ec94 100644 --- a/test/filesystem_test.cpp +++ b/test/filesystem_test.cpp @@ -1681,6 +1681,21 @@ TEST_CASE("fs.class.rec.dir.itr - class recursive_directory_iterator", "[filesys } CHECK(os.str() == "d2/b,d2/ds1,d2/ds2,"); } + if (is_symlink_creation_supported()) { + TemporaryDirectory t(TempOpt::change_path); + generateFile("regular"); + fs::create_symlink("self", "self"); + std::multiset result; + REQUIRE_NOTHROW([&]() { + for (const auto& de : fs::recursive_directory_iterator(".")) { + result.insert(de.path().generic_string()); + } + }()); + CHECK(result.size() == 2); + CHECK(result.count("./regular") == 1); + CHECK(result.count("./self") == 1); + CHECK(fs::remove("self")); + } } TEST_CASE("fs.op.absolute - absolute", "[filesystem][operations][fs.op.absolute]")