David Sehr | 891a50e | 2017-10-27 17:01:07 -0700 | [diff] [blame] | 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 | |
David Sehr | c3e1895 | 2018-05-11 16:59:31 -0700 | [diff] [blame] | 17 | #ifndef ART_LIBARTBASE_BASE_FILE_UTILS_H_ |
| 18 | #define ART_LIBARTBASE_BASE_FILE_UTILS_H_ |
David Sehr | 891a50e | 2017-10-27 17:01:07 -0700 | [diff] [blame] | 19 | |
| 20 | #include <stdlib.h> |
| 21 | |
| 22 | #include <string> |
Orion Hodson | 4c3ade6 | 2021-02-10 14:07:10 +0000 | [diff] [blame^] | 23 | #include <string_view> |
David Sehr | 891a50e | 2017-10-27 17:01:07 -0700 | [diff] [blame] | 24 | |
Andreas Gampe | 5794381 | 2017-12-06 21:39:13 -0800 | [diff] [blame] | 25 | #include <android-base/logging.h> |
| 26 | |
David Sehr | 891a50e | 2017-10-27 17:01:07 -0700 | [diff] [blame] | 27 | #include "arch/instruction_set.h" |
David Sehr | 891a50e | 2017-10-27 17:01:07 -0700 | [diff] [blame] | 28 | |
| 29 | namespace art { |
| 30 | |
David Srbecky | 0c0f302 | 2020-02-13 15:53:01 +0000 | [diff] [blame] | 31 | static constexpr const char* kAndroidArtApexDefaultPath = "/apex/com.android.art"; |
Orion Hodson | d6e00a7 | 2021-02-10 13:52:40 +0000 | [diff] [blame] | 32 | static constexpr const char* kArtApexDataDefaultPath = "/data/misc/apexdata/com.android.art"; |
David Srbecky | 0c0f302 | 2020-02-13 15:53:01 +0000 | [diff] [blame] | 33 | static constexpr const char* kAndroidConscryptApexDefaultPath = "/apex/com.android.conscrypt"; |
Victor Chang | d20e51d | 2020-05-05 16:01:19 +0100 | [diff] [blame] | 34 | static constexpr const char* kAndroidI18nApexDefaultPath = "/apex/com.android.i18n"; |
David Srbecky | 0c0f302 | 2020-02-13 15:53:01 +0000 | [diff] [blame] | 35 | |
Orion Hodson | d6e00a7 | 2021-02-10 13:52:40 +0000 | [diff] [blame] | 36 | static constexpr const char* kArtImageExtension = "art"; |
| 37 | |
Roland Levillain | 50eec3d | 2019-04-05 18:53:58 +0100 | [diff] [blame] | 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 | // |
David Sehr | 891a50e | 2017-10-27 17:01:07 -0700 | [diff] [blame] | 45 | // Find $ANDROID_ROOT, /system, or abort. |
| 46 | std::string GetAndroidRoot(); |
| 47 | // Find $ANDROID_ROOT, /system, or return an empty string. |
Roland Levillain | 9ff900d | 2019-03-29 18:50:01 +0000 | [diff] [blame] | 48 | std::string GetAndroidRootSafe(/*out*/ std::string* error_msg); |
David Sehr | 891a50e | 2017-10-27 17:01:07 -0700 | [diff] [blame] | 49 | |
Martin Stjernholm | e58624f | 2019-09-20 15:53:40 +0100 | [diff] [blame] | 50 | // 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 Levillain | 50eec3d | 2019-04-05 18:53:58 +0100 | [diff] [blame] | 55 | // |
Martin Stjernholm | e58624f | 2019-09-20 15:53:40 +0100 | [diff] [blame] | 56 | // Find $ANDROID_ART_ROOT, /apex/com.android.art, or abort. |
| 57 | std::string GetArtRoot(); |
| 58 | // Find $ANDROID_ART_ROOT, /apex/com.android.art, or return an empty string. |
| 59 | std::string GetArtRootSafe(/*out*/ std::string* error_msg); |
Roland Levillain | 1ea8a62 | 2019-03-29 19:08:56 +0000 | [diff] [blame] | 60 | |
Martin Stjernholm | e58624f | 2019-09-20 15:53:40 +0100 | [diff] [blame] | 61 | // Return the path to the directory containing the ART binaries. |
| 62 | std::string GetArtBinDir(); |
Roland Levillain | fb6a5c0 | 2019-03-29 20:20:16 +0000 | [diff] [blame] | 63 | |
David Sehr | 891a50e | 2017-10-27 17:01:07 -0700 | [diff] [blame] | 64 | // Find $ANDROID_DATA, /data, or abort. |
Roland Levillain | 2e3cb54 | 2019-04-05 18:00:04 +0100 | [diff] [blame] | 65 | std::string GetAndroidData(); |
| 66 | // Find $ANDROID_DATA, /data, or return an empty string. |
| 67 | std::string GetAndroidDataSafe(/*out*/ std::string* error_msg); |
David Sehr | 891a50e | 2017-10-27 17:01:07 -0700 | [diff] [blame] | 68 | |
Orion Hodson | d6e00a7 | 2021-02-10 13:52:40 +0000 | [diff] [blame] | 69 | // Find $ART_APEX_DATA, /data/misc/apexdata/com.android.art, or abort. |
| 70 | std::string GetArtApexData(); |
| 71 | |
David Sehr | 891a50e | 2017-10-27 17:01:07 -0700 | [diff] [blame] | 72 | // Returns the default boot image location (ANDROID_ROOT/framework/boot.art). |
| 73 | // Returns an empty string if ANDROID_ROOT is not set. |
| 74 | std::string GetDefaultBootImageLocation(std::string* error_msg); |
| 75 | |
Orion Hodson | d6e00a7 | 2021-02-10 13:52:40 +0000 | [diff] [blame] | 76 | // Returns the default boot image location, based on the passed `android_root`. |
Nicolas Geoffray | de1b2a2 | 2019-02-27 09:10:57 +0000 | [diff] [blame] | 77 | std::string GetDefaultBootImageLocation(const std::string& android_root); |
| 78 | |
David Sehr | 891a50e | 2017-10-27 17:01:07 -0700 | [diff] [blame] | 79 | // Return true if we found the dalvik cache and stored it in the dalvik_cache argument. |
Orion Hodson | d6e00a7 | 2021-02-10 13:52:40 +0000 | [diff] [blame] | 80 | // `have_android_data` will be set to true if we have an ANDROID_DATA that exists, |
| 81 | // `dalvik_cache_exists` will be true if there is a dalvik-cache directory that is present. |
| 82 | // The flag `is_global_cache` tells whether this cache is /data/dalvik-cache. |
David Sehr | 891a50e | 2017-10-27 17:01:07 -0700 | [diff] [blame] | 83 | void GetDalvikCache(const char* subdir, bool create_if_absent, std::string* dalvik_cache, |
| 84 | bool* have_android_data, bool* dalvik_cache_exists, bool* is_global_cache); |
| 85 | |
| 86 | // Returns the absolute dalvik-cache path for a DexFile or OatFile. The path returned will be |
Orion Hodson | d6e00a7 | 2021-02-10 13:52:40 +0000 | [diff] [blame] | 87 | // rooted at `cache_location`. |
| 88 | bool GetDalvikCacheFilename(const char* location, const char* cache_location, |
David Sehr | 891a50e | 2017-10-27 17:01:07 -0700 | [diff] [blame] | 89 | std::string* filename, std::string* error_msg); |
| 90 | |
Orion Hodson | d6e00a7 | 2021-02-10 13:52:40 +0000 | [diff] [blame] | 91 | // Gets the oat location in the ART APEX data directory for a DEX file installed anywhere other |
| 92 | // than in an APEX. Returns the oat filename if `location` is valid, empty string otherwise. |
| 93 | std::string GetApexDataOatFilename(std::string_view location, InstructionSet isa); |
| 94 | |
| 95 | // Gets the odex location in the ART APEX data directory for a DEX file installed anywhere other |
| 96 | // than in an APEX. Returns the odex filename if `location` is valid, empty string otherwise. |
| 97 | std::string GetApexDataOdexFilename(std::string_view location, InstructionSet isa); |
| 98 | |
| 99 | // Gets the boot image in the ART APEX data directory for a DEX file installed anywhere other |
| 100 | // than in an APEX. Returns the image location if `dex_location` is valid, empty string otherwise. |
| 101 | std::string GetApexDataBootImage(std::string_view dex_location); |
| 102 | |
| 103 | // Gets the image in the ART APEX data directory for a DEX file installed installed anywhere other |
| 104 | // than in an APEX. Returns the image location if `dex_location` is valid, empty string otherwise. |
| 105 | std::string GetApexDataImage(std::string_view dex_location); |
| 106 | |
| 107 | // Gets the name of a file in the ART APEX directory dalvik-cache. This method assumes the |
| 108 | // `dex_location` is for an application and that the `dex_location` is not within an APEX. |
| 109 | // Returns the location of the file in the dalvik-cache |
| 110 | std::string GetApexDataDalvikCacheFilename(std::string_view dex_location, |
| 111 | InstructionSet isa, |
| 112 | std::string_view file_extension); |
| 113 | |
| 114 | // Returns the system location for an image. This method inserts the `isa` between the |
| 115 | // dirname and basename of `location`. |
David Sehr | 891a50e | 2017-10-27 17:01:07 -0700 | [diff] [blame] | 116 | std::string GetSystemImageFilename(const char* location, InstructionSet isa); |
| 117 | |
| 118 | // Returns the vdex filename for the given oat filename. |
| 119 | std::string GetVdexFilename(const std::string& oat_filename); |
| 120 | |
David Sehr | 891a50e | 2017-10-27 17:01:07 -0700 | [diff] [blame] | 121 | // Returns `filename` with the text after the last occurrence of '.' replaced with |
| 122 | // `extension`. If `filename` does not contain a period, returns a string containing `filename`, |
| 123 | // a period, and `new_extension`. |
| 124 | // Example: ReplaceFileExtension("foo.bar", "abc") == "foo.abc" |
| 125 | // ReplaceFileExtension("foo", "abc") == "foo.abc" |
Orion Hodson | d6e00a7 | 2021-02-10 13:52:40 +0000 | [diff] [blame] | 126 | std::string ReplaceFileExtension(std::string_view filename, std::string_view new_extension); |
David Sehr | 891a50e | 2017-10-27 17:01:07 -0700 | [diff] [blame] | 127 | |
Martin Stjernholm | ad909af | 2019-07-16 17:02:44 +0100 | [diff] [blame] | 128 | // Return whether the location is on /apex/com.android.art |
Orion Hodson | d6e00a7 | 2021-02-10 13:52:40 +0000 | [diff] [blame] | 129 | bool LocationIsOnArtModule(std::string_view location); |
| 130 | |
| 131 | // Return whether the location is on /data/misc/apexdata/com.android.art/. |
| 132 | bool LocationIsOnArtApexData(std::string_view location); |
Orion Hodson | 12162de | 2019-01-21 16:01:30 +0000 | [diff] [blame] | 133 | |
David Brazdil | 3c83921 | 2019-03-04 14:29:50 +0000 | [diff] [blame] | 134 | // Return whether the location is on /apex/com.android.conscrypt |
Orion Hodson | d6e00a7 | 2021-02-10 13:52:40 +0000 | [diff] [blame] | 135 | bool LocationIsOnConscryptModule(std::string_view location); |
David Brazdil | 3c83921 | 2019-03-04 14:29:50 +0000 | [diff] [blame] | 136 | |
Victor Chang | d20e51d | 2020-05-05 16:01:19 +0100 | [diff] [blame] | 137 | // Return whether the location is on /apex/com.android.i18n |
Orion Hodson | d6e00a7 | 2021-02-10 13:52:40 +0000 | [diff] [blame] | 138 | bool LocationIsOnI18nModule(std::string_view location); |
Victor Chang | d20e51d | 2020-05-05 16:01:19 +0100 | [diff] [blame] | 139 | |
Nicolas Geoffray | 2974260 | 2017-12-14 10:09:03 +0000 | [diff] [blame] | 140 | // Return whether the location is on system (i.e. android root). |
| 141 | bool LocationIsOnSystem(const char* location); |
| 142 | |
Orion Hodson | d6e00a7 | 2021-02-10 13:52:40 +0000 | [diff] [blame] | 143 | // Return whether the location is on system/framework (i.e. $ANDROID_ROOT/framework). |
| 144 | bool LocationIsOnSystemFramework(std::string_view location); |
David Brazdil | 8e1a7cb | 2018-03-27 08:14:25 +0000 | [diff] [blame] | 145 | |
Chris Gross | 5477b8e | 2020-04-24 09:36:45 -0700 | [diff] [blame] | 146 | // Return whether the location is on system_ext/framework |
Orion Hodson | d6e00a7 | 2021-02-10 13:52:40 +0000 | [diff] [blame] | 147 | bool LocationIsOnSystemExtFramework(std::string_view location); |
Chris Gross | 5477b8e | 2020-04-24 09:36:45 -0700 | [diff] [blame] | 148 | |
David Brazdil | e7e26d1 | 2019-02-28 15:04:14 +0000 | [diff] [blame] | 149 | // Return whether the location is on /apex/. |
Orion Hodson | d6e00a7 | 2021-02-10 13:52:40 +0000 | [diff] [blame] | 150 | bool LocationIsOnApex(std::string_view location); |
David Brazdil | e7e26d1 | 2019-02-28 15:04:14 +0000 | [diff] [blame] | 151 | |
Martin Stjernholm | e58624f | 2019-09-20 15:53:40 +0100 | [diff] [blame] | 152 | // Compare the ART module root against android root. Returns true if they are |
David Brazdil | bfaba28 | 2019-03-15 11:35:51 +0000 | [diff] [blame] | 153 | // both known and distinct. This is meant to be a proxy for 'running with apex'. |
Martin Stjernholm | e58624f | 2019-09-20 15:53:40 +0100 | [diff] [blame] | 154 | bool ArtModuleRootDistinctFromAndroidRoot(); |
David Brazdil | bfaba28 | 2019-03-15 11:35:51 +0000 | [diff] [blame] | 155 | |
Josh Gao | 35696a0 | 2018-08-30 17:24:16 -0700 | [diff] [blame] | 156 | // dup(2), except setting the O_CLOEXEC flag atomically, when possible. |
| 157 | int DupCloexec(int fd); |
| 158 | |
David Brazdil | 3e8aae0 | 2019-03-26 18:48:02 +0000 | [diff] [blame] | 159 | // Returns true if `path` begins with a slash. |
| 160 | inline bool IsAbsoluteLocation(const std::string& path) { return !path.empty() && path[0] == '/'; } |
| 161 | |
David Sehr | 891a50e | 2017-10-27 17:01:07 -0700 | [diff] [blame] | 162 | } // namespace art |
| 163 | |
David Sehr | c3e1895 | 2018-05-11 16:59:31 -0700 | [diff] [blame] | 164 | #endif // ART_LIBARTBASE_BASE_FILE_UTILS_H_ |