xref: /aosp_15_r20/art/artd/path_utils.cc (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 #include "path_utils.h"
18*795d594fSAndroid Build Coastguard Worker 
19*795d594fSAndroid Build Coastguard Worker #include <filesystem>
20*795d594fSAndroid Build Coastguard Worker #include <string>
21*795d594fSAndroid Build Coastguard Worker #include <vector>
22*795d594fSAndroid Build Coastguard Worker 
23*795d594fSAndroid Build Coastguard Worker #include "aidl/com/android/server/art/ArtConstants.h"
24*795d594fSAndroid Build Coastguard Worker #include "aidl/com/android/server/art/BnArtd.h"
25*795d594fSAndroid Build Coastguard Worker #include "android-base/errors.h"
26*795d594fSAndroid Build Coastguard Worker #include "android-base/result.h"
27*795d594fSAndroid Build Coastguard Worker #include "arch/instruction_set.h"
28*795d594fSAndroid Build Coastguard Worker #include "base/file_utils.h"
29*795d594fSAndroid Build Coastguard Worker #include "base/macros.h"
30*795d594fSAndroid Build Coastguard Worker #include "file_utils.h"
31*795d594fSAndroid Build Coastguard Worker #include "oat/oat_file_assistant.h"
32*795d594fSAndroid Build Coastguard Worker #include "runtime_image.h"
33*795d594fSAndroid Build Coastguard Worker #include "service.h"
34*795d594fSAndroid Build Coastguard Worker #include "tools/tools.h"
35*795d594fSAndroid Build Coastguard Worker 
36*795d594fSAndroid Build Coastguard Worker namespace art {
37*795d594fSAndroid Build Coastguard Worker namespace artd {
38*795d594fSAndroid Build Coastguard Worker 
39*795d594fSAndroid Build Coastguard Worker namespace {
40*795d594fSAndroid Build Coastguard Worker 
41*795d594fSAndroid Build Coastguard Worker using ::aidl::com::android::server::art::ArtConstants;
42*795d594fSAndroid Build Coastguard Worker using ::aidl::com::android::server::art::ArtifactsPath;
43*795d594fSAndroid Build Coastguard Worker using ::aidl::com::android::server::art::DexMetadataPath;
44*795d594fSAndroid Build Coastguard Worker using ::aidl::com::android::server::art::OutputArtifacts;
45*795d594fSAndroid Build Coastguard Worker using ::aidl::com::android::server::art::OutputProfile;
46*795d594fSAndroid Build Coastguard Worker using ::aidl::com::android::server::art::ProfilePath;
47*795d594fSAndroid Build Coastguard Worker using ::aidl::com::android::server::art::RuntimeArtifactsPath;
48*795d594fSAndroid Build Coastguard Worker using ::aidl::com::android::server::art::VdexPath;
49*795d594fSAndroid Build Coastguard Worker using ::android::base::Error;
50*795d594fSAndroid Build Coastguard Worker using ::android::base::Result;
51*795d594fSAndroid Build Coastguard Worker using ::art::service::ValidateDexPath;
52*795d594fSAndroid Build Coastguard Worker using ::art::service::ValidatePathElement;
53*795d594fSAndroid Build Coastguard Worker using ::art::service::ValidatePathElementSubstring;
54*795d594fSAndroid Build Coastguard Worker 
55*795d594fSAndroid Build Coastguard Worker using PrebuiltProfilePath = ProfilePath::PrebuiltProfilePath;
56*795d594fSAndroid Build Coastguard Worker using PrimaryCurProfilePath = ProfilePath::PrimaryCurProfilePath;
57*795d594fSAndroid Build Coastguard Worker using PrimaryRefProfilePath = ProfilePath::PrimaryRefProfilePath;
58*795d594fSAndroid Build Coastguard Worker using SecondaryCurProfilePath = ProfilePath::SecondaryCurProfilePath;
59*795d594fSAndroid Build Coastguard Worker using SecondaryRefProfilePath = ProfilePath::SecondaryRefProfilePath;
60*795d594fSAndroid Build Coastguard Worker using TmpProfilePath = ProfilePath::TmpProfilePath;
61*795d594fSAndroid Build Coastguard Worker using WritableProfilePath = ProfilePath::WritableProfilePath;
62*795d594fSAndroid Build Coastguard Worker 
63*795d594fSAndroid Build Coastguard Worker constexpr const char* kPreRebootSuffix = ".staged";
64*795d594fSAndroid Build Coastguard Worker 
65*795d594fSAndroid Build Coastguard Worker // Only to be changed for testing.
66*795d594fSAndroid Build Coastguard Worker std::string_view gListRootDir = "/";
67*795d594fSAndroid Build Coastguard Worker 
68*795d594fSAndroid Build Coastguard Worker }  // namespace
69*795d594fSAndroid Build Coastguard Worker 
GetAndroidDataOrError()70*795d594fSAndroid Build Coastguard Worker Result<std::string> GetAndroidDataOrError() {
71*795d594fSAndroid Build Coastguard Worker   std::string error_msg;
72*795d594fSAndroid Build Coastguard Worker   std::string result = GetAndroidDataSafe(&error_msg);
73*795d594fSAndroid Build Coastguard Worker   if (!error_msg.empty()) {
74*795d594fSAndroid Build Coastguard Worker     return Error() << error_msg;
75*795d594fSAndroid Build Coastguard Worker   }
76*795d594fSAndroid Build Coastguard Worker   return result;
77*795d594fSAndroid Build Coastguard Worker }
78*795d594fSAndroid Build Coastguard Worker 
GetAndroidExpandOrError()79*795d594fSAndroid Build Coastguard Worker Result<std::string> GetAndroidExpandOrError() {
80*795d594fSAndroid Build Coastguard Worker   std::string error_msg;
81*795d594fSAndroid Build Coastguard Worker   std::string result = GetAndroidExpandSafe(&error_msg);
82*795d594fSAndroid Build Coastguard Worker   if (!error_msg.empty()) {
83*795d594fSAndroid Build Coastguard Worker     return Error() << error_msg;
84*795d594fSAndroid Build Coastguard Worker   }
85*795d594fSAndroid Build Coastguard Worker   return result;
86*795d594fSAndroid Build Coastguard Worker }
87*795d594fSAndroid Build Coastguard Worker 
GetArtRootOrError()88*795d594fSAndroid Build Coastguard Worker Result<std::string> GetArtRootOrError() {
89*795d594fSAndroid Build Coastguard Worker   std::string error_msg;
90*795d594fSAndroid Build Coastguard Worker   std::string result = GetArtRootSafe(&error_msg);
91*795d594fSAndroid Build Coastguard Worker   if (!error_msg.empty()) {
92*795d594fSAndroid Build Coastguard Worker     return Error() << error_msg;
93*795d594fSAndroid Build Coastguard Worker   }
94*795d594fSAndroid Build Coastguard Worker   return result;
95*795d594fSAndroid Build Coastguard Worker }
96*795d594fSAndroid Build Coastguard Worker 
ListManagedFiles(const std::string & android_data,const std::string & android_expand)97*795d594fSAndroid Build Coastguard Worker std::vector<std::string> ListManagedFiles(const std::string& android_data,
98*795d594fSAndroid Build Coastguard Worker                                           const std::string& android_expand) {
99*795d594fSAndroid Build Coastguard Worker   // See `art::tools::Glob` for the syntax.
100*795d594fSAndroid Build Coastguard Worker   std::vector<std::string> patterns = {
101*795d594fSAndroid Build Coastguard Worker       // Profiles for primary dex files.
102*795d594fSAndroid Build Coastguard Worker       android_data + "/misc/profiles/**",
103*795d594fSAndroid Build Coastguard Worker       // Artifacts for primary dex files.
104*795d594fSAndroid Build Coastguard Worker       android_data + "/dalvik-cache/**",
105*795d594fSAndroid Build Coastguard Worker   };
106*795d594fSAndroid Build Coastguard Worker 
107*795d594fSAndroid Build Coastguard Worker   for (const std::string& data_root : {android_data, android_expand + "/*"}) {
108*795d594fSAndroid Build Coastguard Worker     // Artifacts for primary dex files.
109*795d594fSAndroid Build Coastguard Worker     patterns.push_back(data_root + "/app/*/*/oat/**");
110*795d594fSAndroid Build Coastguard Worker 
111*795d594fSAndroid Build Coastguard Worker     for (const char* user_dir : {"/user", "/user_de"}) {
112*795d594fSAndroid Build Coastguard Worker       std::string data_dir = data_root + user_dir + "/*/*";
113*795d594fSAndroid Build Coastguard Worker       // Profiles and artifacts for secondary dex files. Those files are in app data directories, so
114*795d594fSAndroid Build Coastguard Worker       // we use more granular patterns to avoid accidentally deleting apps' files.
115*795d594fSAndroid Build Coastguard Worker       std::string secondary_oat_dir = data_dir + "/**/oat";
116*795d594fSAndroid Build Coastguard Worker       for (const char* suffix : {"", ".*.tmp", kPreRebootSuffix}) {
117*795d594fSAndroid Build Coastguard Worker         patterns.push_back(secondary_oat_dir + "/*" + ArtConstants::PROFILE_FILE_EXT + suffix);
118*795d594fSAndroid Build Coastguard Worker         patterns.push_back(secondary_oat_dir + "/*/*" + kOdexExtension + suffix);
119*795d594fSAndroid Build Coastguard Worker         patterns.push_back(secondary_oat_dir + "/*/*" + kVdexExtension + suffix);
120*795d594fSAndroid Build Coastguard Worker         patterns.push_back(secondary_oat_dir + "/*/*" + kArtExtension + suffix);
121*795d594fSAndroid Build Coastguard Worker       }
122*795d594fSAndroid Build Coastguard Worker       // Runtime image files.
123*795d594fSAndroid Build Coastguard Worker       patterns.push_back(RuntimeImage::GetRuntimeImageDir(data_dir) + "**");
124*795d594fSAndroid Build Coastguard Worker     }
125*795d594fSAndroid Build Coastguard Worker   }
126*795d594fSAndroid Build Coastguard Worker 
127*795d594fSAndroid Build Coastguard Worker   return tools::Glob(patterns, gListRootDir);
128*795d594fSAndroid Build Coastguard Worker }
129*795d594fSAndroid Build Coastguard Worker 
ListRuntimeArtifactsFiles(const std::string & android_data,const std::string & android_expand,const RuntimeArtifactsPath & runtime_artifacts_path)130*795d594fSAndroid Build Coastguard Worker std::vector<std::string> ListRuntimeArtifactsFiles(
131*795d594fSAndroid Build Coastguard Worker     const std::string& android_data,
132*795d594fSAndroid Build Coastguard Worker     const std::string& android_expand,
133*795d594fSAndroid Build Coastguard Worker     const RuntimeArtifactsPath& runtime_artifacts_path) {
134*795d594fSAndroid Build Coastguard Worker   // See `art::tools::Glob` for the syntax.
135*795d594fSAndroid Build Coastguard Worker   std::vector<std::string> patterns;
136*795d594fSAndroid Build Coastguard Worker 
137*795d594fSAndroid Build Coastguard Worker   for (const std::string& data_root : {android_data, android_expand + "/*"}) {
138*795d594fSAndroid Build Coastguard Worker     for (const char* user_dir : {"/user", "/user_de"}) {
139*795d594fSAndroid Build Coastguard Worker       std::string data_dir =
140*795d594fSAndroid Build Coastguard Worker           data_root + user_dir + "/*/" + tools::EscapeGlob(runtime_artifacts_path.packageName);
141*795d594fSAndroid Build Coastguard Worker       patterns.push_back(
142*795d594fSAndroid Build Coastguard Worker           RuntimeImage::GetRuntimeImagePath(data_dir,
143*795d594fSAndroid Build Coastguard Worker                                             tools::EscapeGlob(runtime_artifacts_path.dexPath),
144*795d594fSAndroid Build Coastguard Worker                                             tools::EscapeGlob(runtime_artifacts_path.isa)));
145*795d594fSAndroid Build Coastguard Worker     }
146*795d594fSAndroid Build Coastguard Worker   }
147*795d594fSAndroid Build Coastguard Worker 
148*795d594fSAndroid Build Coastguard Worker   return tools::Glob(patterns, gListRootDir);
149*795d594fSAndroid Build Coastguard Worker }
150*795d594fSAndroid Build Coastguard Worker 
ValidateRuntimeArtifactsPath(const RuntimeArtifactsPath & runtime_artifacts_path)151*795d594fSAndroid Build Coastguard Worker Result<void> ValidateRuntimeArtifactsPath(const RuntimeArtifactsPath& runtime_artifacts_path) {
152*795d594fSAndroid Build Coastguard Worker   OR_RETURN(ValidatePathElement(runtime_artifacts_path.packageName, "packageName"));
153*795d594fSAndroid Build Coastguard Worker   OR_RETURN(ValidatePathElement(runtime_artifacts_path.isa, "isa"));
154*795d594fSAndroid Build Coastguard Worker   OR_RETURN(ValidateDexPath(runtime_artifacts_path.dexPath));
155*795d594fSAndroid Build Coastguard Worker   return {};
156*795d594fSAndroid Build Coastguard Worker }
157*795d594fSAndroid Build Coastguard Worker 
BuildArtBinPath(const std::string & binary_name)158*795d594fSAndroid Build Coastguard Worker Result<std::string> BuildArtBinPath(const std::string& binary_name) {
159*795d594fSAndroid Build Coastguard Worker   return ART_FORMAT("{}/bin/{}", OR_RETURN(GetArtRootOrError()), binary_name);
160*795d594fSAndroid Build Coastguard Worker }
161*795d594fSAndroid Build Coastguard Worker 
BuildArtifactsPath(const ArtifactsPath & artifacts_path)162*795d594fSAndroid Build Coastguard Worker Result<RawArtifactsPath> BuildArtifactsPath(const ArtifactsPath& artifacts_path) {
163*795d594fSAndroid Build Coastguard Worker   OR_RETURN(ValidateDexPath(artifacts_path.dexPath));
164*795d594fSAndroid Build Coastguard Worker 
165*795d594fSAndroid Build Coastguard Worker   InstructionSet isa = GetInstructionSetFromString(artifacts_path.isa.c_str());
166*795d594fSAndroid Build Coastguard Worker   if (isa == InstructionSet::kNone) {
167*795d594fSAndroid Build Coastguard Worker     return Errorf("Instruction set '{}' is invalid", artifacts_path.isa);
168*795d594fSAndroid Build Coastguard Worker   }
169*795d594fSAndroid Build Coastguard Worker 
170*795d594fSAndroid Build Coastguard Worker   std::string error_msg;
171*795d594fSAndroid Build Coastguard Worker   RawArtifactsPath path;
172*795d594fSAndroid Build Coastguard Worker   if (artifacts_path.isInDalvikCache) {
173*795d594fSAndroid Build Coastguard Worker     // Apps' OAT files are never in ART APEX data.
174*795d594fSAndroid Build Coastguard Worker     if (!OatFileAssistant::DexLocationToOatFilename(artifacts_path.dexPath,
175*795d594fSAndroid Build Coastguard Worker                                                     isa,
176*795d594fSAndroid Build Coastguard Worker                                                     /*deny_art_apex_data_files=*/true,
177*795d594fSAndroid Build Coastguard Worker                                                     &path.oat_path,
178*795d594fSAndroid Build Coastguard Worker                                                     &error_msg)) {
179*795d594fSAndroid Build Coastguard Worker       return Error() << error_msg;
180*795d594fSAndroid Build Coastguard Worker     }
181*795d594fSAndroid Build Coastguard Worker   } else {
182*795d594fSAndroid Build Coastguard Worker     if (!OatFileAssistant::DexLocationToOdexFilename(
183*795d594fSAndroid Build Coastguard Worker             artifacts_path.dexPath, isa, &path.oat_path, &error_msg)) {
184*795d594fSAndroid Build Coastguard Worker       return Error() << error_msg;
185*795d594fSAndroid Build Coastguard Worker     }
186*795d594fSAndroid Build Coastguard Worker   }
187*795d594fSAndroid Build Coastguard Worker 
188*795d594fSAndroid Build Coastguard Worker   path.vdex_path = ReplaceFileExtension(path.oat_path, kVdexExtension);
189*795d594fSAndroid Build Coastguard Worker   path.art_path = ReplaceFileExtension(path.oat_path, kArtExtension);
190*795d594fSAndroid Build Coastguard Worker 
191*795d594fSAndroid Build Coastguard Worker   if (artifacts_path.isPreReboot) {
192*795d594fSAndroid Build Coastguard Worker     path.oat_path += kPreRebootSuffix;
193*795d594fSAndroid Build Coastguard Worker     path.vdex_path += kPreRebootSuffix;
194*795d594fSAndroid Build Coastguard Worker     path.art_path += kPreRebootSuffix;
195*795d594fSAndroid Build Coastguard Worker   }
196*795d594fSAndroid Build Coastguard Worker 
197*795d594fSAndroid Build Coastguard Worker   return path;
198*795d594fSAndroid Build Coastguard Worker }
199*795d594fSAndroid Build Coastguard Worker 
BuildPrimaryRefProfilePath(const PrimaryRefProfilePath & primary_ref_profile_path)200*795d594fSAndroid Build Coastguard Worker Result<std::string> BuildPrimaryRefProfilePath(
201*795d594fSAndroid Build Coastguard Worker     const PrimaryRefProfilePath& primary_ref_profile_path) {
202*795d594fSAndroid Build Coastguard Worker   OR_RETURN(ValidatePathElement(primary_ref_profile_path.packageName, "packageName"));
203*795d594fSAndroid Build Coastguard Worker   OR_RETURN(ValidatePathElementSubstring(primary_ref_profile_path.profileName, "profileName"));
204*795d594fSAndroid Build Coastguard Worker   return ART_FORMAT("{}/misc/profiles/ref/{}/{}{}{}",
205*795d594fSAndroid Build Coastguard Worker                     OR_RETURN(GetAndroidDataOrError()),
206*795d594fSAndroid Build Coastguard Worker                     primary_ref_profile_path.packageName,
207*795d594fSAndroid Build Coastguard Worker                     primary_ref_profile_path.profileName,
208*795d594fSAndroid Build Coastguard Worker                     ArtConstants::PROFILE_FILE_EXT,
209*795d594fSAndroid Build Coastguard Worker                     primary_ref_profile_path.isPreReboot ? kPreRebootSuffix : "");
210*795d594fSAndroid Build Coastguard Worker }
211*795d594fSAndroid Build Coastguard Worker 
BuildPrebuiltProfilePath(const PrebuiltProfilePath & prebuilt_profile_path)212*795d594fSAndroid Build Coastguard Worker Result<std::string> BuildPrebuiltProfilePath(const PrebuiltProfilePath& prebuilt_profile_path) {
213*795d594fSAndroid Build Coastguard Worker   OR_RETURN(ValidateDexPath(prebuilt_profile_path.dexPath));
214*795d594fSAndroid Build Coastguard Worker   return prebuilt_profile_path.dexPath + ArtConstants::PROFILE_FILE_EXT;
215*795d594fSAndroid Build Coastguard Worker }
216*795d594fSAndroid Build Coastguard Worker 
BuildPrimaryCurProfilePath(const PrimaryCurProfilePath & primary_cur_profile_path)217*795d594fSAndroid Build Coastguard Worker Result<std::string> BuildPrimaryCurProfilePath(
218*795d594fSAndroid Build Coastguard Worker     const PrimaryCurProfilePath& primary_cur_profile_path) {
219*795d594fSAndroid Build Coastguard Worker   OR_RETURN(ValidatePathElement(primary_cur_profile_path.packageName, "packageName"));
220*795d594fSAndroid Build Coastguard Worker   OR_RETURN(ValidatePathElementSubstring(primary_cur_profile_path.profileName, "profileName"));
221*795d594fSAndroid Build Coastguard Worker   return ART_FORMAT("{}/misc/profiles/cur/{}/{}/{}{}",
222*795d594fSAndroid Build Coastguard Worker                     OR_RETURN(GetAndroidDataOrError()),
223*795d594fSAndroid Build Coastguard Worker                     primary_cur_profile_path.userId,
224*795d594fSAndroid Build Coastguard Worker                     primary_cur_profile_path.packageName,
225*795d594fSAndroid Build Coastguard Worker                     primary_cur_profile_path.profileName,
226*795d594fSAndroid Build Coastguard Worker                     ArtConstants::PROFILE_FILE_EXT);
227*795d594fSAndroid Build Coastguard Worker }
228*795d594fSAndroid Build Coastguard Worker 
BuildSecondaryRefProfilePath(const SecondaryRefProfilePath & secondary_ref_profile_path)229*795d594fSAndroid Build Coastguard Worker Result<std::string> BuildSecondaryRefProfilePath(
230*795d594fSAndroid Build Coastguard Worker     const SecondaryRefProfilePath& secondary_ref_profile_path) {
231*795d594fSAndroid Build Coastguard Worker   OR_RETURN(ValidateDexPath(secondary_ref_profile_path.dexPath));
232*795d594fSAndroid Build Coastguard Worker   std::filesystem::path dex_path(secondary_ref_profile_path.dexPath);
233*795d594fSAndroid Build Coastguard Worker   return ART_FORMAT("{}/oat/{}{}{}",
234*795d594fSAndroid Build Coastguard Worker                     dex_path.parent_path().string(),
235*795d594fSAndroid Build Coastguard Worker                     dex_path.filename().string(),
236*795d594fSAndroid Build Coastguard Worker                     ArtConstants::PROFILE_FILE_EXT,
237*795d594fSAndroid Build Coastguard Worker                     secondary_ref_profile_path.isPreReboot ? kPreRebootSuffix : "");
238*795d594fSAndroid Build Coastguard Worker }
239*795d594fSAndroid Build Coastguard Worker 
BuildSecondaryCurProfilePath(const SecondaryCurProfilePath & secondary_cur_profile_path)240*795d594fSAndroid Build Coastguard Worker Result<std::string> BuildSecondaryCurProfilePath(
241*795d594fSAndroid Build Coastguard Worker     const SecondaryCurProfilePath& secondary_cur_profile_path) {
242*795d594fSAndroid Build Coastguard Worker   OR_RETURN(ValidateDexPath(secondary_cur_profile_path.dexPath));
243*795d594fSAndroid Build Coastguard Worker   std::filesystem::path dex_path(secondary_cur_profile_path.dexPath);
244*795d594fSAndroid Build Coastguard Worker   return ART_FORMAT("{}/oat/{}.cur{}",
245*795d594fSAndroid Build Coastguard Worker                     dex_path.parent_path().string(),
246*795d594fSAndroid Build Coastguard Worker                     dex_path.filename().string(),
247*795d594fSAndroid Build Coastguard Worker                     ArtConstants::PROFILE_FILE_EXT);
248*795d594fSAndroid Build Coastguard Worker }
249*795d594fSAndroid Build Coastguard Worker 
BuildWritableProfilePath(const WritableProfilePath & profile_path)250*795d594fSAndroid Build Coastguard Worker Result<std::string> BuildWritableProfilePath(const WritableProfilePath& profile_path) {
251*795d594fSAndroid Build Coastguard Worker   switch (profile_path.getTag()) {
252*795d594fSAndroid Build Coastguard Worker     case WritableProfilePath::forPrimary:
253*795d594fSAndroid Build Coastguard Worker       return BuildPrimaryRefProfilePath(profile_path.get<WritableProfilePath::forPrimary>());
254*795d594fSAndroid Build Coastguard Worker     case WritableProfilePath::forSecondary:
255*795d594fSAndroid Build Coastguard Worker       return BuildSecondaryRefProfilePath(profile_path.get<WritableProfilePath::forSecondary>());
256*795d594fSAndroid Build Coastguard Worker       // No default. All cases should be explicitly handled, or the compilation will fail.
257*795d594fSAndroid Build Coastguard Worker   }
258*795d594fSAndroid Build Coastguard Worker   // This should never happen. Just in case we get a non-enumerator value.
259*795d594fSAndroid Build Coastguard Worker   LOG(FATAL) << ART_FORMAT("Unexpected writable profile path type {}",
260*795d594fSAndroid Build Coastguard Worker                            fmt::underlying(profile_path.getTag()));
261*795d594fSAndroid Build Coastguard Worker }
262*795d594fSAndroid Build Coastguard Worker 
BuildFinalProfilePath(const TmpProfilePath & tmp_profile_path)263*795d594fSAndroid Build Coastguard Worker Result<std::string> BuildFinalProfilePath(const TmpProfilePath& tmp_profile_path) {
264*795d594fSAndroid Build Coastguard Worker   return BuildWritableProfilePath(tmp_profile_path.finalPath);
265*795d594fSAndroid Build Coastguard Worker }
266*795d594fSAndroid Build Coastguard Worker 
BuildTmpProfilePath(const TmpProfilePath & tmp_profile_path)267*795d594fSAndroid Build Coastguard Worker Result<std::string> BuildTmpProfilePath(const TmpProfilePath& tmp_profile_path) {
268*795d594fSAndroid Build Coastguard Worker   OR_RETURN(ValidatePathElementSubstring(tmp_profile_path.id, "id"));
269*795d594fSAndroid Build Coastguard Worker   return NewFile::BuildTempPath(OR_RETURN(BuildFinalProfilePath(tmp_profile_path)),
270*795d594fSAndroid Build Coastguard Worker                                 tmp_profile_path.id);
271*795d594fSAndroid Build Coastguard Worker }
272*795d594fSAndroid Build Coastguard Worker 
BuildDexMetadataPath(const DexMetadataPath & dex_metadata_path)273*795d594fSAndroid Build Coastguard Worker Result<std::string> BuildDexMetadataPath(const DexMetadataPath& dex_metadata_path) {
274*795d594fSAndroid Build Coastguard Worker   OR_RETURN(ValidateDexPath(dex_metadata_path.dexPath));
275*795d594fSAndroid Build Coastguard Worker   return ReplaceFileExtension(dex_metadata_path.dexPath, kDmExtension);
276*795d594fSAndroid Build Coastguard Worker }
277*795d594fSAndroid Build Coastguard Worker 
BuildProfileOrDmPath(const ProfilePath & profile_path)278*795d594fSAndroid Build Coastguard Worker Result<std::string> BuildProfileOrDmPath(const ProfilePath& profile_path) {
279*795d594fSAndroid Build Coastguard Worker   switch (profile_path.getTag()) {
280*795d594fSAndroid Build Coastguard Worker     case ProfilePath::primaryRefProfilePath:
281*795d594fSAndroid Build Coastguard Worker       return BuildPrimaryRefProfilePath(profile_path.get<ProfilePath::primaryRefProfilePath>());
282*795d594fSAndroid Build Coastguard Worker     case ProfilePath::prebuiltProfilePath:
283*795d594fSAndroid Build Coastguard Worker       return BuildPrebuiltProfilePath(profile_path.get<ProfilePath::prebuiltProfilePath>());
284*795d594fSAndroid Build Coastguard Worker     case ProfilePath::primaryCurProfilePath:
285*795d594fSAndroid Build Coastguard Worker       return BuildPrimaryCurProfilePath(profile_path.get<ProfilePath::primaryCurProfilePath>());
286*795d594fSAndroid Build Coastguard Worker     case ProfilePath::secondaryRefProfilePath:
287*795d594fSAndroid Build Coastguard Worker       return BuildSecondaryRefProfilePath(profile_path.get<ProfilePath::secondaryRefProfilePath>());
288*795d594fSAndroid Build Coastguard Worker     case ProfilePath::secondaryCurProfilePath:
289*795d594fSAndroid Build Coastguard Worker       return BuildSecondaryCurProfilePath(profile_path.get<ProfilePath::secondaryCurProfilePath>());
290*795d594fSAndroid Build Coastguard Worker     case ProfilePath::tmpProfilePath:
291*795d594fSAndroid Build Coastguard Worker       return BuildTmpProfilePath(profile_path.get<ProfilePath::tmpProfilePath>());
292*795d594fSAndroid Build Coastguard Worker     case ProfilePath::dexMetadataPath:
293*795d594fSAndroid Build Coastguard Worker       return BuildDexMetadataPath(profile_path.get<ProfilePath::dexMetadataPath>());
294*795d594fSAndroid Build Coastguard Worker       // No default. All cases should be explicitly handled, or the compilation will fail.
295*795d594fSAndroid Build Coastguard Worker   }
296*795d594fSAndroid Build Coastguard Worker   // This should never happen. Just in case we get a non-enumerator value.
297*795d594fSAndroid Build Coastguard Worker   LOG(FATAL) << ART_FORMAT("Unexpected profile path type {}",
298*795d594fSAndroid Build Coastguard Worker                            fmt::underlying(profile_path.getTag()));
299*795d594fSAndroid Build Coastguard Worker }
300*795d594fSAndroid Build Coastguard Worker 
BuildVdexPath(const VdexPath & vdex_path)301*795d594fSAndroid Build Coastguard Worker Result<std::string> BuildVdexPath(const VdexPath& vdex_path) {
302*795d594fSAndroid Build Coastguard Worker   DCHECK(vdex_path.getTag() == VdexPath::artifactsPath);
303*795d594fSAndroid Build Coastguard Worker   return OR_RETURN(BuildArtifactsPath(vdex_path.get<VdexPath::artifactsPath>())).vdex_path;
304*795d594fSAndroid Build Coastguard Worker }
305*795d594fSAndroid Build Coastguard Worker 
PreRebootFlag(const ProfilePath & profile_path)306*795d594fSAndroid Build Coastguard Worker bool PreRebootFlag(const ProfilePath& profile_path) {
307*795d594fSAndroid Build Coastguard Worker   switch (profile_path.getTag()) {
308*795d594fSAndroid Build Coastguard Worker     case ProfilePath::primaryRefProfilePath:
309*795d594fSAndroid Build Coastguard Worker       return profile_path.get<ProfilePath::primaryRefProfilePath>().isPreReboot;
310*795d594fSAndroid Build Coastguard Worker     case ProfilePath::secondaryRefProfilePath:
311*795d594fSAndroid Build Coastguard Worker       return profile_path.get<ProfilePath::secondaryRefProfilePath>().isPreReboot;
312*795d594fSAndroid Build Coastguard Worker     case ProfilePath::tmpProfilePath:
313*795d594fSAndroid Build Coastguard Worker       return PreRebootFlag(profile_path.get<ProfilePath::tmpProfilePath>());
314*795d594fSAndroid Build Coastguard Worker     case ProfilePath::prebuiltProfilePath:
315*795d594fSAndroid Build Coastguard Worker     case ProfilePath::primaryCurProfilePath:
316*795d594fSAndroid Build Coastguard Worker     case ProfilePath::secondaryCurProfilePath:
317*795d594fSAndroid Build Coastguard Worker     case ProfilePath::dexMetadataPath:
318*795d594fSAndroid Build Coastguard Worker       return false;
319*795d594fSAndroid Build Coastguard Worker       // No default. All cases should be explicitly handled, or the compilation will fail.
320*795d594fSAndroid Build Coastguard Worker   }
321*795d594fSAndroid Build Coastguard Worker   // This should never happen. Just in case we get a non-enumerator value.
322*795d594fSAndroid Build Coastguard Worker   LOG(FATAL) << ART_FORMAT("Unexpected profile path type {}",
323*795d594fSAndroid Build Coastguard Worker                            fmt::underlying(profile_path.getTag()));
324*795d594fSAndroid Build Coastguard Worker }
325*795d594fSAndroid Build Coastguard Worker 
PreRebootFlag(const TmpProfilePath & tmp_profile_path)326*795d594fSAndroid Build Coastguard Worker bool PreRebootFlag(const TmpProfilePath& tmp_profile_path) {
327*795d594fSAndroid Build Coastguard Worker   return PreRebootFlag(tmp_profile_path.finalPath);
328*795d594fSAndroid Build Coastguard Worker }
329*795d594fSAndroid Build Coastguard Worker 
PreRebootFlag(const OutputProfile & profile)330*795d594fSAndroid Build Coastguard Worker bool PreRebootFlag(const OutputProfile& profile) { return PreRebootFlag(profile.profilePath); }
331*795d594fSAndroid Build Coastguard Worker 
PreRebootFlag(const ArtifactsPath & artifacts_path)332*795d594fSAndroid Build Coastguard Worker bool PreRebootFlag(const ArtifactsPath& artifacts_path) { return artifacts_path.isPreReboot; }
333*795d594fSAndroid Build Coastguard Worker 
PreRebootFlag(const OutputArtifacts & artifacts)334*795d594fSAndroid Build Coastguard Worker bool PreRebootFlag(const OutputArtifacts& artifacts) {
335*795d594fSAndroid Build Coastguard Worker   return PreRebootFlag(artifacts.artifactsPath);
336*795d594fSAndroid Build Coastguard Worker }
337*795d594fSAndroid Build Coastguard Worker 
PreRebootFlag(const VdexPath & vdex_path)338*795d594fSAndroid Build Coastguard Worker bool PreRebootFlag(const VdexPath& vdex_path) {
339*795d594fSAndroid Build Coastguard Worker   return PreRebootFlag(vdex_path.get<VdexPath::artifactsPath>());
340*795d594fSAndroid Build Coastguard Worker }
341*795d594fSAndroid Build Coastguard Worker 
IsPreRebootStagedFile(std::string_view filename)342*795d594fSAndroid Build Coastguard Worker bool IsPreRebootStagedFile(std::string_view filename) {
343*795d594fSAndroid Build Coastguard Worker   return filename.ends_with(kPreRebootSuffix);
344*795d594fSAndroid Build Coastguard Worker }
345*795d594fSAndroid Build Coastguard Worker 
TestOnlySetListRootDir(std::string_view root_dir)346*795d594fSAndroid Build Coastguard Worker void TestOnlySetListRootDir(std::string_view root_dir) { gListRootDir = root_dir; }
347*795d594fSAndroid Build Coastguard Worker 
348*795d594fSAndroid Build Coastguard Worker }  // namespace artd
349*795d594fSAndroid Build Coastguard Worker }  // namespace art
350