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 // Returns the absolute paths to files built from the `ArtifactsPath`.
59 android::base::Result<RawArtifactsPath> BuildArtifactsPath(
60 const aidl::com::android::server::art::ArtifactsPath& artifacts_path);
61
62 android::base::Result<std::string> BuildPrimaryRefProfilePath(
63 const aidl::com::android::server::art::ProfilePath::PrimaryRefProfilePath&
64 primary_ref_profile_path);
65
66 android::base::Result<std::string> BuildPrebuiltProfilePath(
67 const aidl::com::android::server::art::ProfilePath::PrebuiltProfilePath& prebuilt_profile_path);
68
69 android::base::Result<std::string> BuildPrimaryCurProfilePath(
70 const aidl::com::android::server::art::ProfilePath::PrimaryCurProfilePath&
71 primary_cur_profile_path);
72
73 android::base::Result<std::string> BuildSecondaryRefProfilePath(
74 const aidl::com::android::server::art::ProfilePath::SecondaryRefProfilePath&
75 secondary_ref_profile_path);
76
77 android::base::Result<std::string> BuildSecondaryCurProfilePath(
78 const aidl::com::android::server::art::ProfilePath::SecondaryCurProfilePath&
79 secondary_cur_profile_path);
80
81 android::base::Result<std::string> BuildWritableProfilePath(
82 const aidl::com::android::server::art::ProfilePath::WritableProfilePath& profile_path);
83
84 android::base::Result<std::string> BuildFinalProfilePath(
85 const aidl::com::android::server::art::ProfilePath::TmpProfilePath& tmp_profile_path);
86
87 android::base::Result<std::string> BuildTmpProfilePath(
88 const aidl::com::android::server::art::ProfilePath::TmpProfilePath& tmp_profile_path);
89
90 android::base::Result<std::string> BuildDexMetadataPath(
91 const aidl::com::android::server::art::DexMetadataPath& dex_metadata_path);
92
93 android::base::Result<std::string> BuildProfileOrDmPath(
94 const aidl::com::android::server::art::ProfilePath& profile_path);
95
96 android::base::Result<std::string> BuildVdexPath(
97 const aidl::com::android::server::art::VdexPath& vdex_path);
98
99 // Takes an argument of type `WritableProfilePath`. Returns the pre-reboot flag by value if the
100 // argument is const, or by reference otherwise.
101 template <typename T,
102 typename = std::enable_if_t<
103 std::is_same_v<std::remove_cv_t<T>,
104 aidl::com::android::server::art::ProfilePath::WritableProfilePath>>>
PreRebootFlag(T & profile_path)105 std::conditional_t<std::is_const_v<T>, bool, bool&> PreRebootFlag(T& profile_path) {
106 switch (profile_path.getTag()) {
107 case T::forPrimary:
108 return profile_path.template get<T::forPrimary>().isPreReboot;
109 case T::forSecondary:
110 return profile_path.template get<T::forSecondary>().isPreReboot;
111 // No default. All cases should be explicitly handled, or the compilation will fail.
112 }
113 // This should never happen. Just in case we get a non-enumerator value.
114 LOG(FATAL) << ART_FORMAT("Unexpected writable profile path type {}",
115 fmt::underlying(profile_path.getTag()));
116 }
117
118 template bool PreRebootFlag(
119 const aidl::com::android::server::art::ProfilePath::WritableProfilePath& profile_path);
120 template bool& PreRebootFlag(
121 aidl::com::android::server::art::ProfilePath::WritableProfilePath& profile_path);
122
123 bool PreRebootFlag(const aidl::com::android::server::art::ProfilePath& profile_path);
124 bool PreRebootFlag(
125 const aidl::com::android::server::art::ProfilePath::TmpProfilePath& tmp_profile_path);
126 bool PreRebootFlag(const aidl::com::android::server::art::OutputProfile& profile);
127 bool PreRebootFlag(const aidl::com::android::server::art::ArtifactsPath& artifacts_path);
128 bool PreRebootFlag(const aidl::com::android::server::art::OutputArtifacts& artifacts);
129 bool PreRebootFlag(const aidl::com::android::server::art::VdexPath& vdex_path);
130
131 bool IsPreRebootStagedFile(std::string_view filename);
132
133 // Sets the root dir for `ListManagedFiles` and `ListRuntimeImageFiles`.
134 // The passed string must be alive until the test ends.
135 // For testing use only.
136 void TestOnlySetListRootDir(std::string_view root_dir);
137
138 } // namespace artd
139 } // namespace art
140
141 #endif // ART_ARTD_PATH_UTILS_H_
142