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