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