From 57d249078849c793f43e91a39980af05ca60754e Mon Sep 17 00:00:00 2001 From: Marco Edoardo Santimaria Date: Thu, 18 Dec 2025 15:16:21 +0100 Subject: [PATCH] Fixed bug in matching of glob patterns --- src/Engine.cpp | 8 ++++---- tests/cpp/test_engine.hpp | 26 ++++++++++++++++++++++++++ 2 files changed, 30 insertions(+), 4 deletions(-) diff --git a/src/Engine.cpp b/src/Engine.cpp index 67bcb74..213ed1d 100644 --- a/src/Engine.cpp +++ b/src/Engine.cpp @@ -154,7 +154,7 @@ void capiocl::engine::Engine::_newFile(const std::filesystem::path &path) const std::string matchKey; size_t matchSize = 0; for (const auto &[filename, data] : _capio_cl_entries) { - if (const bool match = fnmatch(filename.c_str(), path.c_str(), FNM_PATHNAME) == 0; + if (const bool match = fnmatch(filename.c_str(), path.c_str(), FNM_NOESCAPE) == 0; match && filename.length() > matchSize) { matchSize = filename.length(); matchKey = filename; @@ -202,7 +202,7 @@ void capiocl::engine::Engine::compute_directory_entry_count( bool capiocl::engine::Engine::contains(const std::filesystem::path &file) const { return std::any_of(_capio_cl_entries.begin(), _capio_cl_entries.end(), [&](auto const &entry) { - return fnmatch(entry.first.c_str(), file.c_str(), FNM_PATHNAME) == 0; + return fnmatch(entry.first.c_str(), file.c_str(), FNM_NOESCAPE) == 0; }); } @@ -524,7 +524,7 @@ bool capiocl::engine::Engine::isConsumer(const std::filesystem::path &path, } for (const auto &[pattern, entry] : _capio_cl_entries) { - if (fnmatch(pattern.c_str(), path.c_str(), FNM_PATHNAME) == 0) { + if (fnmatch(pattern.c_str(), path.c_str(), FNM_NOESCAPE) == 0) { const auto &consumers = entry.consumers; if (std::find(consumers.begin(), consumers.end(), app_name) != consumers.end()) { return true; @@ -556,7 +556,7 @@ bool capiocl::engine::Engine::isProducer(const std::filesystem::path &path, } for (const auto &[pattern, entry] : _capio_cl_entries) { - if (fnmatch(pattern.c_str(), path.c_str(), FNM_PATHNAME) == 0) { + if (fnmatch(pattern.c_str(), path.c_str(), FNM_NOESCAPE) == 0) { const auto &producers = entry.producers; if (std::find(producers.begin(), producers.end(), app_name) != producers.end()) { return true; diff --git a/tests/cpp/test_engine.hpp b/tests/cpp/test_engine.hpp index be5fb1d..5a81274 100644 --- a/tests/cpp/test_engine.hpp +++ b/tests/cpp/test_engine.hpp @@ -612,4 +612,30 @@ TEST(ENGINE_SUITE_NAME, TestGetPaths) { EXPECT_TRUE(std::find(paths.begin(), paths.end(), "/tmp") != paths.end()); } +TEST(ENGINE_SUITE_NAME, TestSubfolderMatching) { + capiocl::engine::Engine engine; + std::string producer_name = "producer"; + engine.addProducer("/a/*", producer_name); + EXPECT_TRUE(engine.isProducer("/a/b", producer_name)); + EXPECT_TRUE(engine.isProducer("/a/b/", producer_name)); + EXPECT_TRUE(engine.isProducer("/a/b/c", producer_name)); +} + +TEST(ENGINE_SUITE_NAME, TestInheritanceFromParentPaths) { + capiocl::engine::Engine engine; + + engine.newFile("/test/*"); + engine.setCommitRule("/test/*", capiocl::commitRules::ON_CLOSE); + engine.setFireRule("/test/*", capiocl::fireRules::NO_UPDATE); + + engine.newFile("/test/a/b/c/d"); + EXPECT_TRUE(engine.getCommitRule("/test/a/b/c/d") == capiocl::commitRules::ON_CLOSE); + EXPECT_TRUE(engine.getFireRule("/test/a/b/c/d") == capiocl::fireRules::NO_UPDATE); + + engine.setCommitRule("/test/*", capiocl::commitRules::ON_TERMINATION); + engine.setFireRule("/test/*", capiocl::fireRules::UPDATE); + EXPECT_TRUE(engine.getCommitRule("/test/a/b/c/d") == capiocl::commitRules::ON_CLOSE); + EXPECT_TRUE(engine.getFireRule("/test/a/b/c/d") == capiocl::fireRules::NO_UPDATE); +} + #endif // CAPIO_CL_ENGINE_HPP \ No newline at end of file