Chris Wailes | 2c5cb26 | 2021-01-12 16:37:57 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2021 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include "tools.h" |
| 18 | |
Jiakai Zhang | 2061ae2 | 2022-12-21 12:43:56 +0000 | [diff] [blame] | 19 | #include <errno.h> |
| 20 | #include <fnmatch.h> |
| 21 | |
| 22 | #include <algorithm> |
| 23 | #include <filesystem> |
| 24 | #include <functional> |
| 25 | #include <string> |
| 26 | #include <string_view> |
| 27 | #include <system_error> |
| 28 | #include <vector> |
| 29 | |
| 30 | #include "android-base/logging.h" |
Krzysztof Kosiński | 489049d | 2023-04-30 01:22:28 +0000 | [diff] [blame] | 31 | #include "base/macros.h" |
Jiakai Zhang | 2061ae2 | 2022-12-21 12:43:56 +0000 | [diff] [blame] | 32 | |
Chris Wailes | 2c5cb26 | 2021-01-12 16:37:57 -0800 | [diff] [blame] | 33 | namespace art { |
| 34 | namespace tools { |
| 35 | |
Jiakai Zhang | 2061ae2 | 2022-12-21 12:43:56 +0000 | [diff] [blame] | 36 | namespace { |
| 37 | |
| 38 | using ::std::placeholders::_1; |
| 39 | |
Jiakai Zhang | 2061ae2 | 2022-12-21 12:43:56 +0000 | [diff] [blame] | 40 | // Returns true if `path_prefix` matches `pattern` or can be a prefix of a path that matches |
| 41 | // `pattern` (i.e., `path_prefix` represents a directory that may contain a file whose path matches |
| 42 | // `pattern`). |
| 43 | bool PartialMatch(const std::filesystem::path& pattern, const std::filesystem::path& path_prefix) { |
| 44 | for (std::filesystem::path::const_iterator pattern_it = pattern.begin(), |
| 45 | path_prefix_it = path_prefix.begin(); |
| 46 | ; // NOLINT |
| 47 | pattern_it++, path_prefix_it++) { |
| 48 | if (path_prefix_it == path_prefix.end()) { |
| 49 | return true; |
| 50 | } |
| 51 | if (pattern_it == pattern.end()) { |
| 52 | return false; |
| 53 | } |
| 54 | if (*pattern_it == "**") { |
| 55 | return true; |
| 56 | } |
| 57 | if (fnmatch(pattern_it->c_str(), path_prefix_it->c_str(), /*flags=*/0) != 0) { |
| 58 | return false; |
| 59 | } |
| 60 | } |
Chris Wailes | 2c5cb26 | 2021-01-12 16:37:57 -0800 | [diff] [blame] | 61 | } |
| 62 | |
Jiakai Zhang | 2061ae2 | 2022-12-21 12:43:56 +0000 | [diff] [blame] | 63 | bool FullMatchRecursive(const std::filesystem::path& pattern, |
| 64 | std::filesystem::path::const_iterator pattern_it, |
| 65 | const std::filesystem::path& path, |
| 66 | std::filesystem::path::const_iterator path_it, |
| 67 | bool double_asterisk_visited = false) { |
| 68 | if (pattern_it == pattern.end() && path_it == path.end()) { |
| 69 | return true; |
| 70 | } |
| 71 | if (pattern_it == pattern.end()) { |
| 72 | return false; |
| 73 | } |
| 74 | if (*pattern_it == "**") { |
| 75 | DCHECK(!double_asterisk_visited); |
| 76 | std::filesystem::path::const_iterator next_pattern_it = pattern_it; |
| 77 | return FullMatchRecursive( |
| 78 | pattern, ++next_pattern_it, path, path_it, /*double_asterisk_visited=*/true) || |
| 79 | (path_it != path.end() && FullMatchRecursive(pattern, pattern_it, path, ++path_it)); |
| 80 | } |
| 81 | if (path_it == path.end()) { |
| 82 | return false; |
| 83 | } |
| 84 | if (fnmatch(pattern_it->c_str(), path_it->c_str(), /*flags=*/0) != 0) { |
| 85 | return false; |
| 86 | } |
| 87 | return FullMatchRecursive(pattern, ++pattern_it, path, ++path_it); |
Chris Wailes | 2c5cb26 | 2021-01-12 16:37:57 -0800 | [diff] [blame] | 88 | } |
Jiakai Zhang | 2061ae2 | 2022-12-21 12:43:56 +0000 | [diff] [blame] | 89 | |
| 90 | // Returns true if `path` fully matches `pattern`. |
| 91 | bool FullMatch(const std::filesystem::path& pattern, const std::filesystem::path& path) { |
| 92 | return FullMatchRecursive(pattern, pattern.begin(), path, path.begin()); |
Chris Wailes | 2c5cb26 | 2021-01-12 16:37:57 -0800 | [diff] [blame] | 93 | } |
Jiakai Zhang | 2061ae2 | 2022-12-21 12:43:56 +0000 | [diff] [blame] | 94 | |
| 95 | void MatchGlobRecursive(const std::vector<std::filesystem::path>& patterns, |
| 96 | const std::filesystem::path& root_dir, |
| 97 | /*out*/ std::vector<std::string>* results) { |
| 98 | std::error_code ec; |
| 99 | for (auto it = std::filesystem::recursive_directory_iterator( |
| 100 | root_dir, std::filesystem::directory_options::skip_permission_denied, ec); |
Jiakai Zhang | ea71e00 | 2023-04-25 14:50:23 +0100 | [diff] [blame] | 101 | !ec && it != std::filesystem::end(it); |
| 102 | it.increment(ec)) { |
Jiakai Zhang | 2061ae2 | 2022-12-21 12:43:56 +0000 | [diff] [blame] | 103 | const std::filesystem::directory_entry& entry = *it; |
| 104 | if (std::none_of(patterns.begin(), patterns.end(), std::bind(PartialMatch, _1, entry.path()))) { |
| 105 | // Avoid unnecessary I/O and SELinux denials. |
| 106 | it.disable_recursion_pending(); |
| 107 | continue; |
| 108 | } |
| 109 | std::error_code ec2; |
| 110 | if (entry.is_regular_file(ec2) && |
| 111 | std::any_of(patterns.begin(), patterns.end(), std::bind(FullMatch, _1, entry.path()))) { |
| 112 | results->push_back(entry.path()); |
| 113 | } |
| 114 | if (ec2) { |
| 115 | // It's expected that we don't have permission to stat some dirs/files, and we don't care |
| 116 | // about them. |
| 117 | if (ec2.value() != EACCES) { |
Krzysztof Kosiński | 489049d | 2023-04-30 01:22:28 +0000 | [diff] [blame] | 118 | LOG(ERROR) << ART_FORMAT("Unable to lstat '{}': {}", entry.path().string(), ec2.message()); |
Jiakai Zhang | 2061ae2 | 2022-12-21 12:43:56 +0000 | [diff] [blame] | 119 | } |
| 120 | continue; |
| 121 | } |
| 122 | } |
| 123 | if (ec) { |
Krzysztof Kosiński | 489049d | 2023-04-30 01:22:28 +0000 | [diff] [blame] | 124 | LOG(ERROR) << ART_FORMAT("Unable to walk through '{}': {}", root_dir.string(), ec.message()); |
Jiakai Zhang | 2061ae2 | 2022-12-21 12:43:56 +0000 | [diff] [blame] | 125 | } |
| 126 | } |
| 127 | |
| 128 | } // namespace |
| 129 | |
| 130 | std::vector<std::string> Glob(const std::vector<std::string>& patterns, std::string_view root_dir) { |
| 131 | std::vector<std::filesystem::path> parsed_patterns; |
| 132 | parsed_patterns.reserve(patterns.size()); |
| 133 | for (std::string_view pattern : patterns) { |
| 134 | parsed_patterns.emplace_back(pattern); |
| 135 | } |
| 136 | std::vector<std::string> results; |
| 137 | MatchGlobRecursive(parsed_patterns, root_dir, &results); |
| 138 | return results; |
| 139 | } |
| 140 | |
| 141 | } // namespace tools |
| 142 | } // namespace art |