blob: b9d999fde9c7eefc9bfee5a743d38f8bdacec0b3 [file] [log] [blame]
David Sehr891a50e2017-10-27 17:01:07 -07001/*
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
David Sehrc3e18952018-05-11 16:59:31 -070017#ifndef ART_LIBARTBASE_BASE_FILE_UTILS_H_
18#define ART_LIBARTBASE_BASE_FILE_UTILS_H_
David Sehr891a50e2017-10-27 17:01:07 -070019
20#include <stdlib.h>
21
22#include <string>
Orion Hodson4c3ade62021-02-10 14:07:10 +000023#include <string_view>
David Sehr891a50e2017-10-27 17:01:07 -070024
Andreas Gampe57943812017-12-06 21:39:13 -080025#include <android-base/logging.h>
26
David Sehr891a50e2017-10-27 17:01:07 -070027#include "arch/instruction_set.h"
David Sehr891a50e2017-10-27 17:01:07 -070028
29namespace art {
30
Martin Stjernholm93c28ed2021-05-14 14:11:42 +010031static 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";
David Srbecky0c0f3022020-02-13 15:53:01 +000035
Martin Stjernholm93c28ed2021-05-14 14:11:42 +010036static constexpr const char kArtImageExtension[] = "art";
Orion Hodsond6e00a72021-02-10 13:52:40 +000037
Roland Levillain50eec3d2019-04-05 18:53:58 +010038// 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//
David Sehr891a50e2017-10-27 17:01:07 -070045// Find $ANDROID_ROOT, /system, or abort.
46std::string GetAndroidRoot();
47// Find $ANDROID_ROOT, /system, or return an empty string.
Roland Levillain9ff900d2019-03-29 18:50:01 +000048std::string GetAndroidRootSafe(/*out*/ std::string* error_msg);
David Sehr891a50e2017-10-27 17:01:07 -070049
Martin Stjernholme58624f2019-09-20 15:53:40 +010050// These methods return the ART Root, which is the location of the (activated)
51// ART APEX module. On target, this is normally "/apex/com.android.art". On
52// host, this is usually a subdirectory of the Android Root, e.g.
53// "$ANDROID_BUILD_TOP/out/host/linux-x86/com.android.art". The location of the
54// ART root can be overridden using the ANDROID_ART_ROOT environment variable.
Roland Levillain50eec3d2019-04-05 18:53:58 +010055//
Martin Stjernholme58624f2019-09-20 15:53:40 +010056// Find $ANDROID_ART_ROOT, /apex/com.android.art, or abort.
57std::string GetArtRoot();
58// Find $ANDROID_ART_ROOT, /apex/com.android.art, or return an empty string.
59std::string GetArtRootSafe(/*out*/ std::string* error_msg);
Roland Levillain1ea8a622019-03-29 19:08:56 +000060
Martin Stjernholme58624f2019-09-20 15:53:40 +010061// Return the path to the directory containing the ART binaries.
62std::string GetArtBinDir();
Roland Levillainfb6a5c02019-03-29 20:20:16 +000063
David Sehr891a50e2017-10-27 17:01:07 -070064// Find $ANDROID_DATA, /data, or abort.
Roland Levillain2e3cb542019-04-05 18:00:04 +010065std::string GetAndroidData();
66// Find $ANDROID_DATA, /data, or return an empty string.
67std::string GetAndroidDataSafe(/*out*/ std::string* error_msg);
David Sehr891a50e2017-10-27 17:01:07 -070068
Orion Hodsond6e00a72021-02-10 13:52:40 +000069// Find $ART_APEX_DATA, /data/misc/apexdata/com.android.art, or abort.
70std::string GetArtApexData();
71
Jiakai Zhanga3293452022-01-12 20:04:11 +000072// Returns the directory that contains the prebuilt version of the primary boot image (i.e., the one
73// generated at build time).
74std::string GetPrebuiltPrimaryBootImageDir();
75
David Sehr891a50e2017-10-27 17:01:07 -070076// Returns the default boot image location (ANDROID_ROOT/framework/boot.art).
77// Returns an empty string if ANDROID_ROOT is not set.
78std::string GetDefaultBootImageLocation(std::string* error_msg);
79
Orion Hodsond6e00a72021-02-10 13:52:40 +000080// Returns the default boot image location, based on the passed `android_root`.
Orion Hodson971068d2021-06-30 21:17:53 +010081std::string GetDefaultBootImageLocation(const std::string& android_root,
82 bool deny_art_apex_data_files);
Nicolas Geoffrayde1b2a22019-02-27 09:10:57 +000083
Jiakai Zhangdc386f62022-08-06 20:54:35 +010084// Returns the boot image location that forces the runtime to run in JIT Zygote mode.
85std::string GetJitZygoteBootImageLocation();
86
Alan Stokesd4d21bf2021-10-13 11:07:25 +010087// Allows the name to be used for the dalvik cache directory (normally "dalvik-cache") to be
88// overridden with a new value.
89void OverrideDalvikCacheSubDirectory(std::string sub_dir);
90
David Sehr891a50e2017-10-27 17:01:07 -070091// Return true if we found the dalvik cache and stored it in the dalvik_cache argument.
Orion Hodsond6e00a72021-02-10 13:52:40 +000092// `have_android_data` will be set to true if we have an ANDROID_DATA that exists,
93// `dalvik_cache_exists` will be true if there is a dalvik-cache directory that is present.
94// The flag `is_global_cache` tells whether this cache is /data/dalvik-cache.
David Sehr891a50e2017-10-27 17:01:07 -070095void GetDalvikCache(const char* subdir, bool create_if_absent, std::string* dalvik_cache,
96 bool* have_android_data, bool* dalvik_cache_exists, bool* is_global_cache);
97
98// Returns the absolute dalvik-cache path for a DexFile or OatFile. The path returned will be
Orion Hodsond6e00a72021-02-10 13:52:40 +000099// rooted at `cache_location`.
100bool GetDalvikCacheFilename(const char* location, const char* cache_location,
David Sehr891a50e2017-10-27 17:01:07 -0700101 std::string* filename, std::string* error_msg);
102
Victor Hsiehfb007612021-12-01 16:51:20 -0800103// Returns the absolute dalvik-cache path. The path may include the instruction set sub-directory
104// if specified.
105std::string GetApexDataDalvikCacheDirectory(InstructionSet isa);
106
Orion Hodsond6e00a72021-02-10 13:52:40 +0000107// Gets the oat location in the ART APEX data directory for a DEX file installed anywhere other
108// than in an APEX. Returns the oat filename if `location` is valid, empty string otherwise.
109std::string GetApexDataOatFilename(std::string_view location, InstructionSet isa);
110
Jiakai Zhangd61b0212021-07-08 10:20:26 +0800111// Gets the odex location in the ART APEX data directory for a DEX file. Returns the odex filename
112// if `location` is valid, empty string otherwise.
Orion Hodsond6e00a72021-02-10 13:52:40 +0000113std::string GetApexDataOdexFilename(std::string_view location, InstructionSet isa);
114
115// Gets the boot image in the ART APEX data directory for a DEX file installed anywhere other
116// than in an APEX. Returns the image location if `dex_location` is valid, empty string otherwise.
117std::string GetApexDataBootImage(std::string_view dex_location);
118
Jiakai Zhangd61b0212021-07-08 10:20:26 +0800119// Gets the image in the ART APEX data directory for a DEX file. Returns the image location if
120// `dex_location` is valid, empty string otherwise.
Orion Hodsond6e00a72021-02-10 13:52:40 +0000121std::string GetApexDataImage(std::string_view dex_location);
122
123// Gets the name of a file in the ART APEX directory dalvik-cache. This method assumes the
Jiakai Zhangd61b0212021-07-08 10:20:26 +0800124// `dex_location` is for an application.
Orion Hodsond6e00a72021-02-10 13:52:40 +0000125// Returns the location of the file in the dalvik-cache
126std::string GetApexDataDalvikCacheFilename(std::string_view dex_location,
127 InstructionSet isa,
128 std::string_view file_extension);
129
130// Returns the system location for an image. This method inserts the `isa` between the
131// dirname and basename of `location`.
David Sehr891a50e2017-10-27 17:01:07 -0700132std::string GetSystemImageFilename(const char* location, InstructionSet isa);
133
134// Returns the vdex filename for the given oat filename.
135std::string GetVdexFilename(const std::string& oat_filename);
136
Nicolas Geoffray327cfcf2021-10-12 14:13:25 +0100137// Returns the dm filename for the given dex location.
138std::string GetDmFilename(const std::string& dex_location);
139
Jiakai Zhang7fa70862021-09-15 08:56:56 +0000140// Returns the odex location on /system for a DEX file on /apex. The caller must make sure that
141// `location` is on /apex.
142std::string GetSystemOdexFilenameForApex(std::string_view location, InstructionSet isa);
143
David Sehr891a50e2017-10-27 17:01:07 -0700144// Returns `filename` with the text after the last occurrence of '.' replaced with
145// `extension`. If `filename` does not contain a period, returns a string containing `filename`,
146// a period, and `new_extension`.
147// Example: ReplaceFileExtension("foo.bar", "abc") == "foo.abc"
148// ReplaceFileExtension("foo", "abc") == "foo.abc"
Orion Hodsond6e00a72021-02-10 13:52:40 +0000149std::string ReplaceFileExtension(std::string_view filename, std::string_view new_extension);
David Sehr891a50e2017-10-27 17:01:07 -0700150
Martin Stjernholmad909af2019-07-16 17:02:44 +0100151// Return whether the location is on /apex/com.android.art
Orion Hodsond6e00a72021-02-10 13:52:40 +0000152bool LocationIsOnArtModule(std::string_view location);
153
154// Return whether the location is on /data/misc/apexdata/com.android.art/.
155bool LocationIsOnArtApexData(std::string_view location);
Orion Hodson12162de2019-01-21 16:01:30 +0000156
David Brazdil3c839212019-03-04 14:29:50 +0000157// Return whether the location is on /apex/com.android.conscrypt
Orion Hodsond6e00a72021-02-10 13:52:40 +0000158bool LocationIsOnConscryptModule(std::string_view location);
David Brazdil3c839212019-03-04 14:29:50 +0000159
Victor Changd20e51d2020-05-05 16:01:19 +0100160// Return whether the location is on /apex/com.android.i18n
Orion Hodsond6e00a72021-02-10 13:52:40 +0000161bool LocationIsOnI18nModule(std::string_view location);
Victor Changd20e51d2020-05-05 16:01:19 +0100162
Nicolas Geoffray29742602017-12-14 10:09:03 +0000163// Return whether the location is on system (i.e. android root).
Orion Hodson094b1cf2021-06-08 09:28:28 +0100164bool LocationIsOnSystem(const std::string& location);
Nicolas Geoffray29742602017-12-14 10:09:03 +0000165
Orion Hodsond6e00a72021-02-10 13:52:40 +0000166// Return whether the location is on system/framework (i.e. $ANDROID_ROOT/framework).
167bool LocationIsOnSystemFramework(std::string_view location);
David Brazdil8e1a7cb2018-03-27 08:14:25 +0000168
Chris Gross5477b8e2020-04-24 09:36:45 -0700169// Return whether the location is on system_ext/framework
Orion Hodsond6e00a72021-02-10 13:52:40 +0000170bool LocationIsOnSystemExtFramework(std::string_view location);
Chris Gross5477b8e2020-04-24 09:36:45 -0700171
David Brazdile7e26d12019-02-28 15:04:14 +0000172// Return whether the location is on /apex/.
Orion Hodsond6e00a72021-02-10 13:52:40 +0000173bool LocationIsOnApex(std::string_view location);
David Brazdile7e26d12019-02-28 15:04:14 +0000174
Alan Stokesa2e39362022-01-06 14:09:00 +0000175// If the given location is /apex/<apexname>/..., return <apexname>, otherwise return an empty
176// string. Note that the result is a view into full_path and is valid only as long as it is.
177std::string_view ApexNameFromLocation(std::string_view full_path);
178
Orion Hodson094b1cf2021-06-08 09:28:28 +0100179// Returns whether the location is trusted for loading oat files. Trusted locations are protected
180// by dm-verity or fs-verity. The recognized locations are on /system or
181// /data/misc/apexdata/com.android.art.
Orion Hodson971068d2021-06-30 21:17:53 +0100182bool LocationIsTrusted(const std::string& location, bool trust_art_apex_data_files);
Orion Hodson094b1cf2021-06-08 09:28:28 +0100183
Martin Stjernholme58624f2019-09-20 15:53:40 +0100184// Compare the ART module root against android root. Returns true if they are
David Brazdilbfaba282019-03-15 11:35:51 +0000185// both known and distinct. This is meant to be a proxy for 'running with apex'.
Martin Stjernholme58624f2019-09-20 15:53:40 +0100186bool ArtModuleRootDistinctFromAndroidRoot();
David Brazdilbfaba282019-03-15 11:35:51 +0000187
Josh Gao35696a02018-08-30 17:24:16 -0700188// dup(2), except setting the O_CLOEXEC flag atomically, when possible.
189int DupCloexec(int fd);
190
David Brazdil3e8aae02019-03-26 18:48:02 +0000191// Returns true if `path` begins with a slash.
192inline bool IsAbsoluteLocation(const std::string& path) { return !path.empty() && path[0] == '/'; }
193
David Sehr891a50e2017-10-27 17:01:07 -0700194} // namespace art
195
David Sehrc3e18952018-05-11 16:59:31 -0700196#endif // ART_LIBARTBASE_BASE_FILE_UTILS_H_