xref: /aosp_15_r20/art/libarttools/include/tools/tools.h (revision 795d594fd825385562da6b089ea9b2033f3abf5a)
1*795d594fSAndroid Build Coastguard Worker /*
2*795d594fSAndroid Build Coastguard Worker  * Copyright (C) 2021 The Android Open Source Project
3*795d594fSAndroid Build Coastguard Worker  *
4*795d594fSAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*795d594fSAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*795d594fSAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*795d594fSAndroid Build Coastguard Worker  *
8*795d594fSAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*795d594fSAndroid Build Coastguard Worker  *
10*795d594fSAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*795d594fSAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*795d594fSAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*795d594fSAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*795d594fSAndroid Build Coastguard Worker  * limitations under the License.
15*795d594fSAndroid Build Coastguard Worker  */
16*795d594fSAndroid Build Coastguard Worker 
17*795d594fSAndroid Build Coastguard Worker #ifndef ART_LIBARTTOOLS_INCLUDE_TOOLS_TOOLS_H_
18*795d594fSAndroid Build Coastguard Worker #define ART_LIBARTTOOLS_INCLUDE_TOOLS_TOOLS_H_
19*795d594fSAndroid Build Coastguard Worker 
20*795d594fSAndroid Build Coastguard Worker #include <cstdint>
21*795d594fSAndroid Build Coastguard Worker #include <string>
22*795d594fSAndroid Build Coastguard Worker #include <string_view>
23*795d594fSAndroid Build Coastguard Worker #include <vector>
24*795d594fSAndroid Build Coastguard Worker 
25*795d594fSAndroid Build Coastguard Worker #include "android-base/result.h"
26*795d594fSAndroid Build Coastguard Worker #include "fstab/fstab.h"
27*795d594fSAndroid Build Coastguard Worker 
28*795d594fSAndroid Build Coastguard Worker namespace art {
29*795d594fSAndroid Build Coastguard Worker namespace tools {
30*795d594fSAndroid Build Coastguard Worker 
31*795d594fSAndroid Build Coastguard Worker // Searches in a filesystem, starting from `root_dir`. Returns all regular files (i.e., excluding
32*795d594fSAndroid Build Coastguard Worker // directories, symlinks, etc.) that match at least one pattern in `patterns`. Each pattern is an
33*795d594fSAndroid Build Coastguard Worker // absolute path that contains zero or more wildcards. The scan does not follow symlinks to
34*795d594fSAndroid Build Coastguard Worker // directories.
35*795d594fSAndroid Build Coastguard Worker //
36*795d594fSAndroid Build Coastguard Worker // Supported wildcards are:
37*795d594fSAndroid Build Coastguard Worker // - Those documented in glob(7)
38*795d594fSAndroid Build Coastguard Worker // - '**': Matches zero or more path elements. This is only recognised by itself as a path segment.
39*795d594fSAndroid Build Coastguard Worker //
40*795d594fSAndroid Build Coastguard Worker // For simplicity and efficiency, at most one '**' is allowed.
41*795d594fSAndroid Build Coastguard Worker std::vector<std::string> Glob(const std::vector<std::string>& patterns,
42*795d594fSAndroid Build Coastguard Worker                               std::string_view root_dir = "/");
43*795d594fSAndroid Build Coastguard Worker 
44*795d594fSAndroid Build Coastguard Worker // Escapes a string so that it's not recognized as a wildcard pattern for `Glob`.
45*795d594fSAndroid Build Coastguard Worker std::string EscapeGlob(const std::string& str);
46*795d594fSAndroid Build Coastguard Worker 
47*795d594fSAndroid Build Coastguard Worker // Returns true if `path` starts with `prefix` (i.e., if `prefix` represents a directory that
48*795d594fSAndroid Build Coastguard Worker // contains a file/directory at `path`, or if `prefix` and `path` represents the same
49*795d594fSAndroid Build Coastguard Worker // file/directory). Only supports absolute paths.
50*795d594fSAndroid Build Coastguard Worker bool PathStartsWith(std::string_view path, std::string_view prefix);
51*795d594fSAndroid Build Coastguard Worker 
52*795d594fSAndroid Build Coastguard Worker // Returns the fstab entries in /proc/mounts where the mount point is a prefix of the given path.
53*795d594fSAndroid Build Coastguard Worker android::base::Result<std::vector<android::fs_mgr::FstabEntry>> GetProcMountsAncestorsOfPath(
54*795d594fSAndroid Build Coastguard Worker     std::string_view path);
55*795d594fSAndroid Build Coastguard Worker 
56*795d594fSAndroid Build Coastguard Worker // Returns the fstab entries in /proc/mounts where the given path is a prefix of the mount point.
57*795d594fSAndroid Build Coastguard Worker android::base::Result<std::vector<android::fs_mgr::FstabEntry>> GetProcMountsDescendantsOfPath(
58*795d594fSAndroid Build Coastguard Worker     std::string_view path);
59*795d594fSAndroid Build Coastguard Worker 
60*795d594fSAndroid Build Coastguard Worker // See `ArtJni.ensureNoProcessInDir`.
61*795d594fSAndroid Build Coastguard Worker android::base::Result<void> EnsureNoProcessInDir(const std::string& dir,
62*795d594fSAndroid Build Coastguard Worker                                                  uint32_t timeout_ms,
63*795d594fSAndroid Build Coastguard Worker                                                  bool try_kill);
64*795d594fSAndroid Build Coastguard Worker 
65*795d594fSAndroid Build Coastguard Worker }  // namespace tools
66*795d594fSAndroid Build Coastguard Worker }  // namespace art
67*795d594fSAndroid Build Coastguard Worker 
68*795d594fSAndroid Build Coastguard Worker #endif  // ART_LIBARTTOOLS_INCLUDE_TOOLS_TOOLS_H_
69