• Home
  • History
  • Annotate
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifndef ART_LIBARTTOOLS_INCLUDE_TOOLS_TOOLS_H_
18 #define ART_LIBARTTOOLS_INCLUDE_TOOLS_TOOLS_H_
19 
20 #include <cstdint>
21 #include <string>
22 #include <string_view>
23 #include <vector>
24 
25 #include "android-base/result.h"
26 #include "fstab/fstab.h"
27 
28 namespace art {
29 namespace tools {
30 
31 // Searches in a filesystem, starting from `root_dir`. Returns all regular files (i.e., excluding
32 // directories, symlinks, etc.) that match at least one pattern in `patterns`. Each pattern is an
33 // absolute path that contains zero or more wildcards. The scan does not follow symlinks to
34 // directories.
35 //
36 // Supported wildcards are:
37 // - Those documented in glob(7)
38 // - '**': Matches zero or more path elements. This is only recognised by itself as a path segment.
39 //
40 // For simplicity and efficiency, at most one '**' is allowed.
41 std::vector<std::string> Glob(const std::vector<std::string>& patterns,
42                               std::string_view root_dir = "/");
43 
44 // Escapes a string so that it's not recognized as a wildcard pattern for `Glob`.
45 std::string EscapeGlob(const std::string& str);
46 
47 // Returns true if `path` starts with `prefix` (i.e., if `prefix` represents a directory that
48 // contains a file/directory at `path`, or if `prefix` and `path` represents the same
49 // file/directory). Only supports absolute paths.
50 bool PathStartsWith(std::string_view path, std::string_view prefix);
51 
52 // Returns the fstab entries in /proc/mounts where the mount point is a prefix of the given path.
53 android::base::Result<std::vector<android::fs_mgr::FstabEntry>> GetProcMountsAncestorsOfPath(
54     std::string_view path);
55 
56 // Returns the fstab entries in /proc/mounts where the given path is a prefix of the mount point.
57 android::base::Result<std::vector<android::fs_mgr::FstabEntry>> GetProcMountsDescendantsOfPath(
58     std::string_view path);
59 
60 // See `ArtJni.ensureNoProcessInDir`.
61 android::base::Result<void> EnsureNoProcessInDir(const std::string& dir,
62                                                  uint32_t timeout_ms,
63                                                  bool try_kill);
64 
65 }  // namespace tools
66 }  // namespace art
67 
68 #endif  // ART_LIBARTTOOLS_INCLUDE_TOOLS_TOOLS_H_
69