xref: /aosp_15_r20/art/libartbase/base/file_utils.h (revision 795d594fd825385562da6b089ea9b2033f3abf5a)
1*795d594fSAndroid Build Coastguard Worker /*
2*795d594fSAndroid Build Coastguard Worker  * Copyright (C) 2011 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_LIBARTBASE_BASE_FILE_UTILS_H_
18*795d594fSAndroid Build Coastguard Worker #define ART_LIBARTBASE_BASE_FILE_UTILS_H_
19*795d594fSAndroid Build Coastguard Worker 
20*795d594fSAndroid Build Coastguard Worker #include <stdlib.h>
21*795d594fSAndroid Build Coastguard Worker 
22*795d594fSAndroid Build Coastguard Worker #include <string>
23*795d594fSAndroid Build Coastguard Worker #include <string_view>
24*795d594fSAndroid Build Coastguard Worker 
25*795d594fSAndroid Build Coastguard Worker #include <android-base/logging.h>
26*795d594fSAndroid Build Coastguard Worker 
27*795d594fSAndroid Build Coastguard Worker #include "arch/instruction_set.h"
28*795d594fSAndroid Build Coastguard Worker 
29*795d594fSAndroid Build Coastguard Worker namespace art {
30*795d594fSAndroid Build Coastguard Worker 
31*795d594fSAndroid Build Coastguard Worker static constexpr const char kAndroidArtApexDefaultPath[] = "/apex/com.android.art";
32*795d594fSAndroid Build Coastguard Worker static constexpr const char kArtApexDataDefaultPath[] = "/data/misc/apexdata/com.android.art";
33*795d594fSAndroid Build Coastguard Worker static constexpr const char kAndroidConscryptApexDefaultPath[] = "/apex/com.android.conscrypt";
34*795d594fSAndroid Build Coastguard Worker static constexpr const char kAndroidI18nApexDefaultPath[] = "/apex/com.android.i18n";
35*795d594fSAndroid Build Coastguard Worker 
36*795d594fSAndroid Build Coastguard Worker static constexpr const char* kOatExtension = ".oat";
37*795d594fSAndroid Build Coastguard Worker static constexpr const char* kOdexExtension = ".odex";
38*795d594fSAndroid Build Coastguard Worker static constexpr const char* kVdexExtension = ".vdex";
39*795d594fSAndroid Build Coastguard Worker static constexpr const char* kArtExtension = ".art";
40*795d594fSAndroid Build Coastguard Worker static constexpr const char* kDmExtension = ".dm";
41*795d594fSAndroid Build Coastguard Worker static constexpr const char* kSdmExtension = ".sdm";
42*795d594fSAndroid Build Coastguard Worker 
43*795d594fSAndroid Build Coastguard Worker // These methods return the Android Root, which is the historical location of
44*795d594fSAndroid Build Coastguard Worker // the Android "system" directory, containing the built Android artifacts. On
45*795d594fSAndroid Build Coastguard Worker // target, this is normally "/system". On host this is usually a directory under
46*795d594fSAndroid Build Coastguard Worker // the build tree, e.g. "$ANDROID_BUILD_TOP/out/host/linux-x86". The location of
47*795d594fSAndroid Build Coastguard Worker // the Android Root can be overriden using the ANDROID_ROOT environment
48*795d594fSAndroid Build Coastguard Worker // variable.
49*795d594fSAndroid Build Coastguard Worker //
50*795d594fSAndroid Build Coastguard Worker // Find $ANDROID_ROOT, /system, or abort.
51*795d594fSAndroid Build Coastguard Worker std::string GetAndroidRoot();
52*795d594fSAndroid Build Coastguard Worker // Find $ANDROID_ROOT, /system, or return an empty string.
53*795d594fSAndroid Build Coastguard Worker std::string GetAndroidRootSafe(/*out*/ std::string* error_msg);
54*795d594fSAndroid Build Coastguard Worker 
55*795d594fSAndroid Build Coastguard Worker // Find $SYSTEM_EXT_ROOT, /system_ext, or abort.
56*795d594fSAndroid Build Coastguard Worker std::string GetSystemExtRoot();
57*795d594fSAndroid Build Coastguard Worker // Find $SYSTEM_EXT_ROOT, /system_ext, or return an empty string.
58*795d594fSAndroid Build Coastguard Worker std::string GetSystemExtRootSafe(/*out*/ std::string* error_msg);
59*795d594fSAndroid Build Coastguard Worker 
60*795d594fSAndroid Build Coastguard Worker // These methods return the ART Root, which is the location of the (activated)
61*795d594fSAndroid Build Coastguard Worker // ART APEX module. On target, this is normally "/apex/com.android.art". On
62*795d594fSAndroid Build Coastguard Worker // host, this is usually a subdirectory of the Android Root, e.g.
63*795d594fSAndroid Build Coastguard Worker // "$ANDROID_BUILD_TOP/out/host/linux-x86/com.android.art". The location of the
64*795d594fSAndroid Build Coastguard Worker // ART root can be overridden using the ANDROID_ART_ROOT environment variable.
65*795d594fSAndroid Build Coastguard Worker //
66*795d594fSAndroid Build Coastguard Worker // Find $ANDROID_ART_ROOT, /apex/com.android.art, or abort.
67*795d594fSAndroid Build Coastguard Worker std::string GetArtRoot();
68*795d594fSAndroid Build Coastguard Worker // Find $ANDROID_ART_ROOT, /apex/com.android.art, or return an empty string.
69*795d594fSAndroid Build Coastguard Worker std::string GetArtRootSafe(/*out*/ std::string* error_msg);
70*795d594fSAndroid Build Coastguard Worker 
71*795d594fSAndroid Build Coastguard Worker // Return the path to the directory containing the ART binaries.
72*795d594fSAndroid Build Coastguard Worker std::string GetArtBinDir();
73*795d594fSAndroid Build Coastguard Worker 
74*795d594fSAndroid Build Coastguard Worker // Find $ANDROID_DATA, /data, or abort.
75*795d594fSAndroid Build Coastguard Worker std::string GetAndroidData();
76*795d594fSAndroid Build Coastguard Worker // Find $ANDROID_DATA, /data, or return an empty string.
77*795d594fSAndroid Build Coastguard Worker std::string GetAndroidDataSafe(/*out*/ std::string* error_msg);
78*795d594fSAndroid Build Coastguard Worker 
79*795d594fSAndroid Build Coastguard Worker // Find $ANDROID_EXPAND, /mnt/expand, or abort.
80*795d594fSAndroid Build Coastguard Worker std::string GetAndroidExpand();
81*795d594fSAndroid Build Coastguard Worker // Find $ANDROID_EXPAND, /mnt/expand, or return an empty string.
82*795d594fSAndroid Build Coastguard Worker std::string GetAndroidExpandSafe(/*out*/ std::string* error_msg);
83*795d594fSAndroid Build Coastguard Worker 
84*795d594fSAndroid Build Coastguard Worker // Find $ART_APEX_DATA, /data/misc/apexdata/com.android.art, or abort.
85*795d594fSAndroid Build Coastguard Worker std::string GetArtApexData();
86*795d594fSAndroid Build Coastguard Worker 
87*795d594fSAndroid Build Coastguard Worker // Returns the directory that contains the prebuilt version of the primary boot image (i.e., the one
88*795d594fSAndroid Build Coastguard Worker // generated at build time).
89*795d594fSAndroid Build Coastguard Worker std::string GetPrebuiltPrimaryBootImageDir();
90*795d594fSAndroid Build Coastguard Worker 
91*795d594fSAndroid Build Coastguard Worker // Returns the filename of the first mainline framework library.
92*795d594fSAndroid Build Coastguard Worker std::string GetFirstMainlineFrameworkLibraryFilename(std::string* error_msg);
93*795d594fSAndroid Build Coastguard Worker 
94*795d594fSAndroid Build Coastguard Worker // Returns the default boot image location, based on the passed `android_root`.
95*795d594fSAndroid Build Coastguard Worker // Returns an empty string if an error occurs.
96*795d594fSAndroid Build Coastguard Worker // The default boot image location can only be used with the default bootclasspath (the value of the
97*795d594fSAndroid Build Coastguard Worker // BOOTCLASSPATH environment variable).
98*795d594fSAndroid Build Coastguard Worker std::string GetDefaultBootImageLocationSafe(const std::string& android_root,
99*795d594fSAndroid Build Coastguard Worker                                             bool deny_art_apex_data_files,
100*795d594fSAndroid Build Coastguard Worker                                             std::string* error_msg);
101*795d594fSAndroid Build Coastguard Worker 
102*795d594fSAndroid Build Coastguard Worker // Same as above, but fails if an error occurs.
103*795d594fSAndroid Build Coastguard Worker std::string GetDefaultBootImageLocation(const std::string& android_root,
104*795d594fSAndroid Build Coastguard Worker                                         bool deny_art_apex_data_files);
105*795d594fSAndroid Build Coastguard Worker 
106*795d594fSAndroid Build Coastguard Worker // Returns the boot image location that forces the runtime to run in JIT Zygote mode.
107*795d594fSAndroid Build Coastguard Worker std::string GetJitZygoteBootImageLocation();
108*795d594fSAndroid Build Coastguard Worker 
109*795d594fSAndroid Build Coastguard Worker // A helper function to pick the most appropriate boot image based on the given options.
110*795d594fSAndroid Build Coastguard Worker // The boot image location can only be used with the default bootclasspath (the value of the
111*795d594fSAndroid Build Coastguard Worker // BOOTCLASSPATH environment variable).
112*795d594fSAndroid Build Coastguard Worker std::string GetBootImageLocationForDefaultBcp(bool no_boot_image,
113*795d594fSAndroid Build Coastguard Worker                                               std::string user_defined_boot_image,
114*795d594fSAndroid Build Coastguard Worker                                               bool deny_art_apex_data_files,
115*795d594fSAndroid Build Coastguard Worker                                               std::string* error_msg);
116*795d594fSAndroid Build Coastguard Worker 
117*795d594fSAndroid Build Coastguard Worker // A helper function to pick the most appropriate boot image based on system properties.
118*795d594fSAndroid Build Coastguard Worker // The boot image location can only be used with the default bootclasspath (the value of the
119*795d594fSAndroid Build Coastguard Worker // BOOTCLASSPATH environment variable).
120*795d594fSAndroid Build Coastguard Worker std::string GetBootImageLocationForDefaultBcpRespectingSysProps(std::string* error_msg);
121*795d594fSAndroid Build Coastguard Worker 
122*795d594fSAndroid Build Coastguard Worker // Allows the name to be used for the dalvik cache directory (normally "dalvik-cache") to be
123*795d594fSAndroid Build Coastguard Worker // overridden with a new value.
124*795d594fSAndroid Build Coastguard Worker void OverrideDalvikCacheSubDirectory(std::string sub_dir);
125*795d594fSAndroid Build Coastguard Worker 
126*795d594fSAndroid Build Coastguard Worker // Return true if we found the dalvik cache and stored it in the dalvik_cache argument.
127*795d594fSAndroid Build Coastguard Worker // `have_android_data` will be set to true if we have an ANDROID_DATA that exists,
128*795d594fSAndroid Build Coastguard Worker // `dalvik_cache_exists` will be true if there is a dalvik-cache directory that is present.
129*795d594fSAndroid Build Coastguard Worker // The flag `is_global_cache` tells whether this cache is /data/dalvik-cache.
130*795d594fSAndroid Build Coastguard Worker void GetDalvikCache(const char* subdir, bool create_if_absent, std::string* dalvik_cache,
131*795d594fSAndroid Build Coastguard Worker                     bool* have_android_data, bool* dalvik_cache_exists, bool* is_global_cache);
132*795d594fSAndroid Build Coastguard Worker 
133*795d594fSAndroid Build Coastguard Worker // Returns the absolute dalvik-cache path for a DexFile or OatFile. The path returned will be
134*795d594fSAndroid Build Coastguard Worker // rooted at `cache_location`.
135*795d594fSAndroid Build Coastguard Worker bool GetDalvikCacheFilename(std::string_view location,
136*795d594fSAndroid Build Coastguard Worker                             std::string_view cache_location,
137*795d594fSAndroid Build Coastguard Worker                             std::string* filename,
138*795d594fSAndroid Build Coastguard Worker                             std::string* error_msg);
139*795d594fSAndroid Build Coastguard Worker 
140*795d594fSAndroid Build Coastguard Worker // Returns the absolute dalvik-cache path. The path may include the instruction set sub-directory
141*795d594fSAndroid Build Coastguard Worker // if specified.
142*795d594fSAndroid Build Coastguard Worker std::string GetApexDataDalvikCacheDirectory(InstructionSet isa);
143*795d594fSAndroid Build Coastguard Worker 
144*795d594fSAndroid Build Coastguard Worker // Gets the oat location in the ART APEX data directory for a DEX file installed anywhere other
145*795d594fSAndroid Build Coastguard Worker // than in an APEX. Returns the oat filename if `location` is valid, empty string otherwise.
146*795d594fSAndroid Build Coastguard Worker std::string GetApexDataOatFilename(std::string_view location, InstructionSet isa);
147*795d594fSAndroid Build Coastguard Worker 
148*795d594fSAndroid Build Coastguard Worker // Gets the odex location in the ART APEX data directory for a DEX file. Returns the odex filename
149*795d594fSAndroid Build Coastguard Worker // if `location` is valid, empty string otherwise.
150*795d594fSAndroid Build Coastguard Worker std::string GetApexDataOdexFilename(std::string_view location, InstructionSet isa);
151*795d594fSAndroid Build Coastguard Worker 
152*795d594fSAndroid Build Coastguard Worker // Gets the boot image in the ART APEX data directory for a DEX file installed anywhere other
153*795d594fSAndroid Build Coastguard Worker // than in an APEX. Returns the image location if `dex_location` is valid, empty string otherwise.
154*795d594fSAndroid Build Coastguard Worker std::string GetApexDataBootImage(std::string_view dex_location);
155*795d594fSAndroid Build Coastguard Worker 
156*795d594fSAndroid Build Coastguard Worker // Gets the image in the ART APEX data directory for a DEX file. Returns the image location if
157*795d594fSAndroid Build Coastguard Worker // `dex_location` is valid, empty string otherwise.
158*795d594fSAndroid Build Coastguard Worker std::string GetApexDataImage(std::string_view dex_location);
159*795d594fSAndroid Build Coastguard Worker 
160*795d594fSAndroid Build Coastguard Worker // Gets the name of a file in the ART APEX directory dalvik-cache. This method assumes the
161*795d594fSAndroid Build Coastguard Worker // `dex_location` is for an application.
162*795d594fSAndroid Build Coastguard Worker // Returns the location of the file in the dalvik-cache
163*795d594fSAndroid Build Coastguard Worker std::string GetApexDataDalvikCacheFilename(std::string_view dex_location,
164*795d594fSAndroid Build Coastguard Worker                                            InstructionSet isa,
165*795d594fSAndroid Build Coastguard Worker                                            std::string_view file_extension);
166*795d594fSAndroid Build Coastguard Worker 
167*795d594fSAndroid Build Coastguard Worker // Returns the system location for an image. This method inserts the `isa` between the
168*795d594fSAndroid Build Coastguard Worker // dirname and basename of `location`.
169*795d594fSAndroid Build Coastguard Worker std::string GetSystemImageFilename(const char* location, InstructionSet isa);
170*795d594fSAndroid Build Coastguard Worker 
171*795d594fSAndroid Build Coastguard Worker // Returns the vdex filename for the given oat filename.
172*795d594fSAndroid Build Coastguard Worker std::string GetVdexFilename(const std::string& oat_filename);
173*795d594fSAndroid Build Coastguard Worker 
174*795d594fSAndroid Build Coastguard Worker // Returns the dm filename for the given dex location.
175*795d594fSAndroid Build Coastguard Worker std::string GetDmFilename(const std::string& dex_location);
176*795d594fSAndroid Build Coastguard Worker 
177*795d594fSAndroid Build Coastguard Worker // Returns the odex location on /system for a DEX file on /apex. The caller must make sure that
178*795d594fSAndroid Build Coastguard Worker // `location` is on /apex.
179*795d594fSAndroid Build Coastguard Worker std::string GetSystemOdexFilenameForApex(std::string_view location, InstructionSet isa);
180*795d594fSAndroid Build Coastguard Worker 
181*795d594fSAndroid Build Coastguard Worker // Returns `filename` with the text after the last occurrence of '.' replaced with
182*795d594fSAndroid Build Coastguard Worker // `extension`. If `filename` does not contain a period, returns a string containing `filename`,
183*795d594fSAndroid Build Coastguard Worker // a period, and `new_extension`.
184*795d594fSAndroid Build Coastguard Worker // A leading period in `new_extension`, if exists, is ignored.
185*795d594fSAndroid Build Coastguard Worker // Example: ReplaceFileExtension("foo.bar", "abc") == "foo.abc"
186*795d594fSAndroid Build Coastguard Worker //          ReplaceFileExtension("foo", "abc") == "foo.abc"
187*795d594fSAndroid Build Coastguard Worker //          ReplaceFileExtension("foo.bar", ".abc") == "foo.abc"
188*795d594fSAndroid Build Coastguard Worker //          ReplaceFileExtension("foo", ".abc") == "foo.abc"
189*795d594fSAndroid Build Coastguard Worker std::string ReplaceFileExtension(std::string_view filename, std::string_view new_extension);
190*795d594fSAndroid Build Coastguard Worker 
191*795d594fSAndroid Build Coastguard Worker // Return whether the location is on /apex/com.android.art
192*795d594fSAndroid Build Coastguard Worker bool LocationIsOnArtModule(std::string_view location);
193*795d594fSAndroid Build Coastguard Worker 
194*795d594fSAndroid Build Coastguard Worker // Return whether the location is on /data/misc/apexdata/com.android.art/.
195*795d594fSAndroid Build Coastguard Worker bool LocationIsOnArtApexData(std::string_view location);
196*795d594fSAndroid Build Coastguard Worker 
197*795d594fSAndroid Build Coastguard Worker // Return whether the location is on /apex/com.android.conscrypt
198*795d594fSAndroid Build Coastguard Worker bool LocationIsOnConscryptModule(std::string_view location);
199*795d594fSAndroid Build Coastguard Worker 
200*795d594fSAndroid Build Coastguard Worker // Return whether the location is on /apex/com.android.i18n
201*795d594fSAndroid Build Coastguard Worker bool LocationIsOnI18nModule(std::string_view location);
202*795d594fSAndroid Build Coastguard Worker 
203*795d594fSAndroid Build Coastguard Worker // Return whether the location is on system (i.e. android root).
204*795d594fSAndroid Build Coastguard Worker bool LocationIsOnSystem(const std::string& location);
205*795d594fSAndroid Build Coastguard Worker 
206*795d594fSAndroid Build Coastguard Worker // Return whether the location is on system_ext
207*795d594fSAndroid Build Coastguard Worker bool LocationIsOnSystemExt(const std::string& location);
208*795d594fSAndroid Build Coastguard Worker 
209*795d594fSAndroid Build Coastguard Worker // Return whether the location is on system/framework (i.e. $ANDROID_ROOT/framework).
210*795d594fSAndroid Build Coastguard Worker bool LocationIsOnSystemFramework(std::string_view location);
211*795d594fSAndroid Build Coastguard Worker 
212*795d594fSAndroid Build Coastguard Worker // Return whether the location is on system_ext/framework
213*795d594fSAndroid Build Coastguard Worker bool LocationIsOnSystemExtFramework(std::string_view location);
214*795d594fSAndroid Build Coastguard Worker 
215*795d594fSAndroid Build Coastguard Worker // Return whether the location is on /apex/.
216*795d594fSAndroid Build Coastguard Worker bool LocationIsOnApex(std::string_view location);
217*795d594fSAndroid Build Coastguard Worker 
218*795d594fSAndroid Build Coastguard Worker // If the given location is /apex/<apexname>/..., return <apexname>, otherwise return an empty
219*795d594fSAndroid Build Coastguard Worker // string. Note that the result is a view into full_path and is valid only as long as it is.
220*795d594fSAndroid Build Coastguard Worker std::string_view ApexNameFromLocation(std::string_view full_path);
221*795d594fSAndroid Build Coastguard Worker 
222*795d594fSAndroid Build Coastguard Worker // Returns whether the location is trusted for loading oat files. Trusted locations are protected
223*795d594fSAndroid Build Coastguard Worker // by dm-verity or fs-verity. The recognized locations are on /system or
224*795d594fSAndroid Build Coastguard Worker // /data/misc/apexdata/com.android.art.
225*795d594fSAndroid Build Coastguard Worker bool LocationIsTrusted(const std::string& location, bool trust_art_apex_data_files);
226*795d594fSAndroid Build Coastguard Worker 
227*795d594fSAndroid Build Coastguard Worker // Compare the ART module root against android root. Returns true if they are
228*795d594fSAndroid Build Coastguard Worker // both known and distinct. This is meant to be a proxy for 'running with apex'.
229*795d594fSAndroid Build Coastguard Worker bool ArtModuleRootDistinctFromAndroidRoot();
230*795d594fSAndroid Build Coastguard Worker 
231*795d594fSAndroid Build Coastguard Worker // dup(2), except setting the O_CLOEXEC flag atomically, when possible.
232*795d594fSAndroid Build Coastguard Worker int DupCloexec(int fd);
233*795d594fSAndroid Build Coastguard Worker 
234*795d594fSAndroid Build Coastguard Worker // Returns true if `path` begins with a slash.
IsAbsoluteLocation(const std::string & path)235*795d594fSAndroid Build Coastguard Worker inline bool IsAbsoluteLocation(const std::string& path) { return !path.empty() && path[0] == '/'; }
236*795d594fSAndroid Build Coastguard Worker 
237*795d594fSAndroid Build Coastguard Worker }  // namespace art
238*795d594fSAndroid Build Coastguard Worker 
239*795d594fSAndroid Build Coastguard Worker #endif  // ART_LIBARTBASE_BASE_FILE_UTILS_H_
240