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> |
| 23 | |
Andreas Gampe | 5794381 | 2017-12-06 21:39:13 -0800 | [diff] [blame] | 24 | #include <android-base/logging.h> |
| 25 | |
David Sehr | 891a50e | 2017-10-27 17:01:07 -0700 | [diff] [blame] | 26 | #include "arch/instruction_set.h" |
David Sehr | 891a50e | 2017-10-27 17:01:07 -0700 | [diff] [blame] | 27 | |
| 28 | namespace art { |
| 29 | |
Roland Levillain | 50eec3d | 2019-04-05 18:53:58 +0100 | [diff] [blame] | 30 | // These methods return the Android Root, which is the historical location of |
| 31 | // the Android "system" directory, containing the built Android artifacts. On |
| 32 | // target, this is normally "/system". On host this is usually a directory under |
| 33 | // the build tree, e.g. "$ANDROID_BUILD_TOP/out/host/linux-x86". The location of |
| 34 | // the Android Root can be overriden using the ANDROID_ROOT environment |
| 35 | // variable. |
| 36 | // |
David Sehr | 891a50e | 2017-10-27 17:01:07 -0700 | [diff] [blame] | 37 | // Find $ANDROID_ROOT, /system, or abort. |
| 38 | std::string GetAndroidRoot(); |
| 39 | // Find $ANDROID_ROOT, /system, or return an empty string. |
Roland Levillain | 9ff900d | 2019-03-29 18:50:01 +0000 | [diff] [blame] | 40 | std::string GetAndroidRootSafe(/*out*/ std::string* error_msg); |
David Sehr | 891a50e | 2017-10-27 17:01:07 -0700 | [diff] [blame] | 41 | |
Roland Levillain | 50eec3d | 2019-04-05 18:53:58 +0100 | [diff] [blame] | 42 | // These methods return the Android Runtime Root, which is the location of the |
| 43 | // (activated) Android Runtime APEX module. On target, this is normally |
Martin Stjernholm | ad909af | 2019-07-16 17:02:44 +0100 | [diff] [blame] | 44 | // "/apex/com.android.art". On host, this is usually a subdirectory of the |
Roland Levillain | 50eec3d | 2019-04-05 18:53:58 +0100 | [diff] [blame] | 45 | // Android Root, e.g. |
Martin Stjernholm | ad909af | 2019-07-16 17:02:44 +0100 | [diff] [blame] | 46 | // "$ANDROID_BUILD_TOP/out/host/linux-x86/com.android.art". The location of |
Roland Levillain | 50eec3d | 2019-04-05 18:53:58 +0100 | [diff] [blame] | 47 | // the Android Runtime Root can be overriden using the ANDROID_RUNTIME_ROOT |
| 48 | // environment variable. |
| 49 | // |
Martin Stjernholm | ad909af | 2019-07-16 17:02:44 +0100 | [diff] [blame] | 50 | // Find $ANDROID_RUNTIME_ROOT, /apex/com.android.art, or abort. |
Roland Levillain | 1ea8a62 | 2019-03-29 19:08:56 +0000 | [diff] [blame] | 51 | std::string GetAndroidRuntimeRoot(); |
Martin Stjernholm | ad909af | 2019-07-16 17:02:44 +0100 | [diff] [blame] | 52 | // Find $ANDROID_RUNTIME_ROOT, /apex/com.android.art, or return an empty string. |
Roland Levillain | 1ea8a62 | 2019-03-29 19:08:56 +0000 | [diff] [blame] | 53 | std::string GetAndroidRuntimeRootSafe(/*out*/ std::string* error_msg); |
| 54 | |
Roland Levillain | fb6a5c0 | 2019-03-29 20:20:16 +0000 | [diff] [blame] | 55 | // Return the path to the directory containing the Android Runtime binaries. |
| 56 | std::string GetAndroidRuntimeBinDir(); |
| 57 | |
David Sehr | 891a50e | 2017-10-27 17:01:07 -0700 | [diff] [blame] | 58 | // Find $ANDROID_DATA, /data, or abort. |
Roland Levillain | 2e3cb54 | 2019-04-05 18:00:04 +0100 | [diff] [blame] | 59 | std::string GetAndroidData(); |
| 60 | // Find $ANDROID_DATA, /data, or return an empty string. |
| 61 | std::string GetAndroidDataSafe(/*out*/ std::string* error_msg); |
David Sehr | 891a50e | 2017-10-27 17:01:07 -0700 | [diff] [blame] | 62 | |
| 63 | // Returns the default boot image location (ANDROID_ROOT/framework/boot.art). |
| 64 | // Returns an empty string if ANDROID_ROOT is not set. |
| 65 | std::string GetDefaultBootImageLocation(std::string* error_msg); |
| 66 | |
Nicolas Geoffray | de1b2a2 | 2019-02-27 09:10:57 +0000 | [diff] [blame] | 67 | // Returns the default boot image location, based on the passed android root. |
| 68 | std::string GetDefaultBootImageLocation(const std::string& android_root); |
| 69 | |
David Sehr | 891a50e | 2017-10-27 17:01:07 -0700 | [diff] [blame] | 70 | // Returns the dalvik-cache location, with subdir appended. Returns the empty string if the cache |
| 71 | // could not be found. |
| 72 | std::string GetDalvikCache(const char* subdir); |
David Sehr | c3e1895 | 2018-05-11 16:59:31 -0700 | [diff] [blame] | 73 | |
David Sehr | 891a50e | 2017-10-27 17:01:07 -0700 | [diff] [blame] | 74 | // Return true if we found the dalvik cache and stored it in the dalvik_cache argument. |
| 75 | // have_android_data will be set to true if we have an ANDROID_DATA that exists, |
| 76 | // dalvik_cache_exists will be true if there is a dalvik-cache directory that is present. |
| 77 | // The flag is_global_cache tells whether this cache is /data/dalvik-cache. |
| 78 | void GetDalvikCache(const char* subdir, bool create_if_absent, std::string* dalvik_cache, |
| 79 | bool* have_android_data, bool* dalvik_cache_exists, bool* is_global_cache); |
| 80 | |
| 81 | // Returns the absolute dalvik-cache path for a DexFile or OatFile. The path returned will be |
| 82 | // rooted at cache_location. |
| 83 | bool GetDalvikCacheFilename(const char* file_location, const char* cache_location, |
| 84 | std::string* filename, std::string* error_msg); |
| 85 | |
| 86 | // Returns the system location for an image |
| 87 | std::string GetSystemImageFilename(const char* location, InstructionSet isa); |
| 88 | |
| 89 | // Returns the vdex filename for the given oat filename. |
| 90 | std::string GetVdexFilename(const std::string& oat_filename); |
| 91 | |
David Sehr | 891a50e | 2017-10-27 17:01:07 -0700 | [diff] [blame] | 92 | // Returns `filename` with the text after the last occurrence of '.' replaced with |
| 93 | // `extension`. If `filename` does not contain a period, returns a string containing `filename`, |
| 94 | // a period, and `new_extension`. |
| 95 | // Example: ReplaceFileExtension("foo.bar", "abc") == "foo.abc" |
| 96 | // ReplaceFileExtension("foo", "abc") == "foo.abc" |
| 97 | std::string ReplaceFileExtension(const std::string& filename, const std::string& new_extension); |
| 98 | |
Martin Stjernholm | ad909af | 2019-07-16 17:02:44 +0100 | [diff] [blame] | 99 | // Return whether the location is on /apex/com.android.art |
Orion Hodson | 12162de | 2019-01-21 16:01:30 +0000 | [diff] [blame] | 100 | bool LocationIsOnRuntimeModule(const char* location); |
| 101 | |
David Brazdil | 3c83921 | 2019-03-04 14:29:50 +0000 | [diff] [blame] | 102 | // Return whether the location is on /apex/com.android.conscrypt |
| 103 | bool LocationIsOnConscryptModule(const char* location); |
| 104 | |
Nicolas Geoffray | 2974260 | 2017-12-14 10:09:03 +0000 | [diff] [blame] | 105 | // Return whether the location is on system (i.e. android root). |
| 106 | bool LocationIsOnSystem(const char* location); |
| 107 | |
David Brazdil | 8e1a7cb | 2018-03-27 08:14:25 +0000 | [diff] [blame] | 108 | // Return whether the location is on system/framework (i.e. android_root/framework). |
| 109 | bool LocationIsOnSystemFramework(const char* location); |
| 110 | |
David Brazdil | e7e26d1 | 2019-02-28 15:04:14 +0000 | [diff] [blame] | 111 | // Return whether the location is on /apex/. |
| 112 | bool LocationIsOnApex(const char* location); |
| 113 | |
David Brazdil | bfaba28 | 2019-03-15 11:35:51 +0000 | [diff] [blame] | 114 | // Compare the runtime module root against android root. Returns true if they are |
| 115 | // both known and distinct. This is meant to be a proxy for 'running with apex'. |
| 116 | bool RuntimeModuleRootDistinctFromAndroidRoot(); |
| 117 | |
Josh Gao | 35696a0 | 2018-08-30 17:24:16 -0700 | [diff] [blame] | 118 | // dup(2), except setting the O_CLOEXEC flag atomically, when possible. |
| 119 | int DupCloexec(int fd); |
| 120 | |
David Brazdil | 3e8aae0 | 2019-03-26 18:48:02 +0000 | [diff] [blame] | 121 | // Returns true if `path` begins with a slash. |
| 122 | inline bool IsAbsoluteLocation(const std::string& path) { return !path.empty() && path[0] == '/'; } |
| 123 | |
David Sehr | 891a50e | 2017-10-27 17:01:07 -0700 | [diff] [blame] | 124 | } // namespace art |
| 125 | |
David Sehr | c3e1895 | 2018-05-11 16:59:31 -0700 | [diff] [blame] | 126 | #endif // ART_LIBARTBASE_BASE_FILE_UTILS_H_ |