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