| 9 | class MountRuleParser { |
| 10 | public: |
| 11 | struct MountRule { |
| 12 | std::vector<std::string> rootSubstrs; |
| 13 | std::vector<std::string> mountPointSubstrs; |
| 14 | std::unordered_set<std::string> fsTypes; |
| 15 | std::vector<std::string> sources; // changed from unordered_set to vector for wildcard matching |
| 16 | |
| 17 | explicit operator bool() const { |
| 18 | return !(rootSubstrs.empty() && mountPointSubstrs.empty() && fsTypes.empty() && sources.empty()); |
| 19 | } |
| 20 | |
| 21 | bool matches(const std::string& root, const std::string& mountPoint, |
| 22 | const std::string& fsType, const std::string& source) const { |
| 23 | return matchList(rootSubstrs, root) && |
| 24 | matchList(mountPointSubstrs, mountPoint) && |
| 25 | (fsTypes.empty() || fsTypes.count(fsType) > 0) && |
| 26 | matchSourceList(sources, source); |
| 27 | } |
| 28 | |
| 29 | bool matches(const std::vector<std::string>& roots, const std::string& mountPoint, |
| 30 | const std::string& fsType, const std::string& source) const { |
| 31 | if (!matchList(mountPointSubstrs, mountPoint) || |
| 32 | (!fsTypes.empty() && fsTypes.count(fsType) == 0) || |
| 33 | !matchSourceList(sources, source)) { |
| 34 | return false; |
| 35 | } |
| 36 | for (const auto& root : roots) { |
| 37 | if (matchList(rootSubstrs, root)) { |
| 38 | return true; |
| 39 | } |
| 40 | } |
| 41 | return false; |
| 42 | } |
| 43 | |
| 44 | |
| 45 | private: |
| 46 | static bool match_with_wildcard(const std::string& value, const std::string& rawPattern) { |
| 47 | std::string pattern; |
| 48 | std::vector<bool> isEscaped; |
| 49 | |
| 50 | for (size_t i = 0; i < rawPattern.size(); ++i) { |
| 51 | if (rawPattern[i] == '\\' && i + 1 < rawPattern.size()) { |
| 52 | ++i; |
| 53 | pattern += rawPattern[i]; |
| 54 | isEscaped.push_back(true); |
| 55 | } else { |
| 56 | pattern += rawPattern[i]; |
| 57 | isEscaped.push_back(false); |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | bool startsWithWildcard = !pattern.empty() && pattern.front() == '*' && !isEscaped[0]; |
| 62 | bool endsWithWildcard = !pattern.empty() && |
| 63 | pattern.back() == '*' && |
| 64 | !isEscaped[pattern.size() - 1]; |
| 65 | |
| 66 | if (startsWithWildcard && endsWithWildcard && pattern.size() > 2) { |
| 67 | return value.find(pattern.substr(1, pattern.size() - 2)) != std::string::npos; |
| 68 | } else if (startsWithWildcard) { |
nothing calls this directly
no outgoing calls
no test coverage detected