1 /*
2 * Copyright (C) 2022 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_ARTD_PATH_UTILS_H_
18 #define ART_ARTD_PATH_UTILS_H_
19
20 #include <string>
21 #include <type_traits>
22 #include <vector>
23
24 #include "aidl/com/android/server/art/BnArtd.h"
25 #include "android-base/logging.h"
26 #include "android-base/result.h"
27 #include "base/macros.h"
28
29 namespace art {
30 namespace artd {
31
32 struct RawArtifactsPath {
33 std::string oat_path;
34 std::string vdex_path;
35 std::string art_path;
36 };
37
38 android::base::Result<std::string> GetAndroidDataOrError();
39
40 android::base::Result<std::string> GetAndroidExpandOrError();
41
42 android::base::Result<std::string> GetArtRootOrError();
43
44 // Returns all existing files that are managed by artd.
45 std::vector<std::string> ListManagedFiles(const std::string& android_data,
46 const std::string& android_expand);
47
48 std::vector<std::string> ListRuntimeArtifactsFiles(
49 const std::string& android_data,
50 const std::string& android_expand,
51 const aidl::com::android::server::art::RuntimeArtifactsPath& runtime_artifacts_path);
52
53 android::base::Result<void> ValidateRuntimeArtifactsPath(
54 const aidl::com::android::server::art::RuntimeArtifactsPath& runtime_artifacts_path);
55
56 android::base::Result<std::string> BuildArtBinPath(const std::string& binary_name);
57
58 android::base::Result<std::string> BuildOatPath(const std::string& dex_path,
59 const std::string& isa_str,
60 bool is_in_dalvik_cache);
61
62 // Returns the absolute paths to files built from the `ArtifactsPath`.
63 android::base::Result<RawArtifactsPath> BuildArtifactsPath(
64 const aidl::com::android::server::art::ArtifactsPath& artifacts_path);
65
66 android::base::Result<std::string> BuildPrimaryRefProfilePath(
67 const aidl::com::android::server::art::ProfilePath::PrimaryRefProfilePath&
68 primary_ref_profile_path);
69
70 android::base::Result<std::string> BuildPrebuiltProfilePath(
71 const aidl::com::android::server::art::ProfilePath::PrebuiltProfilePath& prebuilt_profile_path);
72
73 android::base::Result<std::string> BuildPrimaryCurProfilePath(
74 const aidl::com::android::server::art::ProfilePath::PrimaryCurProfilePath&
75 primary_cur_profile_path);
76
77 android::base::Result<std::string> BuildSecondaryRefProfilePath(
78 const aidl::com::android::server::art::ProfilePath::SecondaryRefProfilePath&
79 secondary_ref_profile_path);
80
81 android::base::Result<std::string> BuildSecondaryCurProfilePath(
82 const aidl::com::android::server::art::ProfilePath::SecondaryCurProfilePath&
83 secondary_cur_profile_path);
84
85 android::base::Result<std::string> BuildWritableProfilePath(
86 const aidl::com::android::server::art::ProfilePath::WritableProfilePath& profile_path);
87
88 android::base::Result<std::string> BuildFinalProfilePath(
89 const aidl::com::android::server::art::ProfilePath::TmpProfilePath& tmp_profile_path);
90
91 android::base::Result<std::string> BuildTmpProfilePath(
92 const aidl::com::android::server::art::ProfilePath::TmpProfilePath& tmp_profile_path);
93
94 android::base::Result<std::string> BuildDexMetadataPath(
95 const aidl::com::android::server::art::DexMetadataPath& dex_metadata_path);
96
97 android::base::Result<std::string> BuildProfileOrDmPath(
98 const aidl::com::android::server::art::ProfilePath& profile_path);
99
100 android::base::Result<std::string> BuildVdexPath(
101 const aidl::com::android::server::art::VdexPath& vdex_path);
102
103 android::base::Result<std::string> BuildSdmPath(
104 const aidl::com::android::server::art::SecureDexMetadataWithCompanionPaths& sdm_path);
105
106 android::base::Result<std::string> BuildSdcPath(
107 const aidl::com::android::server::art::SecureDexMetadataWithCompanionPaths& sdc_path);
108
109 // Takes an argument of type `WritableProfilePath`. Returns the pre-reboot flag by value if the
110 // argument is const, or by reference otherwise.
111 template <typename T,
112 typename = std::enable_if_t<
113 std::is_same_v<std::remove_cv_t<T>,
114 aidl::com::android::server::art::ProfilePath::WritableProfilePath>>>
PreRebootFlag(T & profile_path)115 std::conditional_t<std::is_const_v<T>, bool, bool&> PreRebootFlag(T& profile_path) {
116 switch (profile_path.getTag()) {
117 case T::forPrimary:
118 return profile_path.template get<T::forPrimary>().isPreReboot;
119 case T::forSecondary:
120 return profile_path.template get<T::forSecondary>().isPreReboot;
121 // No default. All cases should be explicitly handled, or the compilation will fail.
122 }
123 // This should never happen. Just in case we get a non-enumerator value.
124 LOG(FATAL) << ART_FORMAT("Unexpected writable profile path type {}",
125 fmt::underlying(profile_path.getTag()));
126 }
127
128 template bool PreRebootFlag(
129 const aidl::com::android::server::art::ProfilePath::WritableProfilePath& profile_path);
130 template bool& PreRebootFlag(
131 aidl::com::android::server::art::ProfilePath::WritableProfilePath& profile_path);
132
133 bool PreRebootFlag(const aidl::com::android::server::art::ProfilePath& profile_path);
134 bool PreRebootFlag(
135 const aidl::com::android::server::art::ProfilePath::TmpProfilePath& tmp_profile_path);
136 bool PreRebootFlag(const aidl::com::android::server::art::OutputProfile& profile);
137 bool PreRebootFlag(const aidl::com::android::server::art::ArtifactsPath& artifacts_path);
138 bool PreRebootFlag(const aidl::com::android::server::art::OutputArtifacts& artifacts);
139 bool PreRebootFlag(const aidl::com::android::server::art::VdexPath& vdex_path);
140
141 bool IsPreRebootStagedFile(std::string_view filename);
142
143 // Sets the root dir for `ListManagedFiles` and `ListRuntimeImageFiles`.
144 // The passed string must be alive until the test ends.
145 // For testing use only.
146 void TestOnlySetListRootDir(std::string_view root_dir);
147
148 } // namespace artd
149 } // namespace art
150
151 #endif // ART_ARTD_PATH_UTILS_H_
152