blob: 4ec9d9a750474016609c673164bec85f339cce4b [file] [log] [blame]
Chris Wailes2c5cb262021-01-12 16:37:57 -08001/*
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 Zhang2061ae22022-12-21 12:43:56 +000019#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ński489049d2023-04-30 01:22:28 +000031#include "base/macros.h"
Jiakai Zhang2061ae22022-12-21 12:43:56 +000032
Chris Wailes2c5cb262021-01-12 16:37:57 -080033namespace art {
34namespace tools {
35
Jiakai Zhang2061ae22022-12-21 12:43:56 +000036namespace {
37
38using ::std::placeholders::_1;
39
Jiakai Zhang2061ae22022-12-21 12:43:56 +000040// 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`).
43bool 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 Wailes2c5cb262021-01-12 16:37:57 -080061}
62
Jiakai Zhang2061ae22022-12-21 12:43:56 +000063bool 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 Wailes2c5cb262021-01-12 16:37:57 -080088}
Jiakai Zhang2061ae22022-12-21 12:43:56 +000089
90// Returns true if `path` fully matches `pattern`.
91bool FullMatch(const std::filesystem::path& pattern, const std::filesystem::path& path) {
92 return FullMatchRecursive(pattern, pattern.begin(), path, path.begin());
Chris Wailes2c5cb262021-01-12 16:37:57 -080093}
Jiakai Zhang2061ae22022-12-21 12:43:56 +000094
95void 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 Zhangea71e002023-04-25 14:50:23 +0100101 !ec && it != std::filesystem::end(it);
102 it.increment(ec)) {
Jiakai Zhang2061ae22022-12-21 12:43:56 +0000103 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ński489049d2023-04-30 01:22:28 +0000118 LOG(ERROR) << ART_FORMAT("Unable to lstat '{}': {}", entry.path().string(), ec2.message());
Jiakai Zhang2061ae22022-12-21 12:43:56 +0000119 }
120 continue;
121 }
122 }
123 if (ec) {
Krzysztof Kosiński489049d2023-04-30 01:22:28 +0000124 LOG(ERROR) << ART_FORMAT("Unable to walk through '{}': {}", root_dir.string(), ec.message());
Jiakai Zhang2061ae22022-12-21 12:43:56 +0000125 }
126}
127
128} // namespace
129
130std::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