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 | |
| 30 | bool ReadFileToString(const std::string& file_name, std::string* result); |
David Sehr | 891a50e | 2017-10-27 17:01:07 -0700 | [diff] [blame] | 31 | |
| 32 | // Find $ANDROID_ROOT, /system, or abort. |
| 33 | std::string GetAndroidRoot(); |
| 34 | // Find $ANDROID_ROOT, /system, or return an empty string. |
| 35 | std::string GetAndroidRootSafe(std::string* error_msg); |
| 36 | |
| 37 | // Find $ANDROID_DATA, /data, or abort. |
| 38 | const char* GetAndroidData(); |
| 39 | // Find $ANDROID_DATA, /data, or return null. |
| 40 | const char* GetAndroidDataSafe(std::string* error_msg); |
| 41 | |
| 42 | // Returns the default boot image location (ANDROID_ROOT/framework/boot.art). |
| 43 | // Returns an empty string if ANDROID_ROOT is not set. |
| 44 | std::string GetDefaultBootImageLocation(std::string* error_msg); |
| 45 | |
Nicolas Geoffray | de1b2a2 | 2019-02-27 09:10:57 +0000 | [diff] [blame] | 46 | // Returns the default boot image location, based on the passed android root. |
| 47 | std::string GetDefaultBootImageLocation(const std::string& android_root); |
| 48 | |
David Sehr | 891a50e | 2017-10-27 17:01:07 -0700 | [diff] [blame] | 49 | // Returns the dalvik-cache location, with subdir appended. Returns the empty string if the cache |
| 50 | // could not be found. |
| 51 | std::string GetDalvikCache(const char* subdir); |
David Sehr | c3e1895 | 2018-05-11 16:59:31 -0700 | [diff] [blame] | 52 | |
David Sehr | 891a50e | 2017-10-27 17:01:07 -0700 | [diff] [blame] | 53 | // Return true if we found the dalvik cache and stored it in the dalvik_cache argument. |
| 54 | // have_android_data will be set to true if we have an ANDROID_DATA that exists, |
| 55 | // dalvik_cache_exists will be true if there is a dalvik-cache directory that is present. |
| 56 | // The flag is_global_cache tells whether this cache is /data/dalvik-cache. |
| 57 | void GetDalvikCache(const char* subdir, bool create_if_absent, std::string* dalvik_cache, |
| 58 | bool* have_android_data, bool* dalvik_cache_exists, bool* is_global_cache); |
| 59 | |
| 60 | // Returns the absolute dalvik-cache path for a DexFile or OatFile. The path returned will be |
| 61 | // rooted at cache_location. |
| 62 | bool GetDalvikCacheFilename(const char* file_location, const char* cache_location, |
| 63 | std::string* filename, std::string* error_msg); |
| 64 | |
| 65 | // Returns the system location for an image |
| 66 | std::string GetSystemImageFilename(const char* location, InstructionSet isa); |
| 67 | |
| 68 | // Returns the vdex filename for the given oat filename. |
| 69 | std::string GetVdexFilename(const std::string& oat_filename); |
| 70 | |
David Sehr | 891a50e | 2017-10-27 17:01:07 -0700 | [diff] [blame] | 71 | // Returns `filename` with the text after the last occurrence of '.' replaced with |
| 72 | // `extension`. If `filename` does not contain a period, returns a string containing `filename`, |
| 73 | // a period, and `new_extension`. |
| 74 | // Example: ReplaceFileExtension("foo.bar", "abc") == "foo.abc" |
| 75 | // ReplaceFileExtension("foo", "abc") == "foo.abc" |
| 76 | std::string ReplaceFileExtension(const std::string& filename, const std::string& new_extension); |
| 77 | |
Orion Hodson | 12162de | 2019-01-21 16:01:30 +0000 | [diff] [blame] | 78 | // Return whether the location is on apex/com.android.runtime |
| 79 | bool LocationIsOnRuntimeModule(const char* location); |
| 80 | |
Nicolas Geoffray | 2974260 | 2017-12-14 10:09:03 +0000 | [diff] [blame] | 81 | // Return whether the location is on system (i.e. android root). |
| 82 | bool LocationIsOnSystem(const char* location); |
| 83 | |
David Brazdil | 8e1a7cb | 2018-03-27 08:14:25 +0000 | [diff] [blame] | 84 | // Return whether the location is on system/framework (i.e. android_root/framework). |
| 85 | bool LocationIsOnSystemFramework(const char* location); |
| 86 | |
Josh Gao | 35696a0 | 2018-08-30 17:24:16 -0700 | [diff] [blame] | 87 | // dup(2), except setting the O_CLOEXEC flag atomically, when possible. |
| 88 | int DupCloexec(int fd); |
| 89 | |
David Sehr | 891a50e | 2017-10-27 17:01:07 -0700 | [diff] [blame] | 90 | } // namespace art |
| 91 | |
David Sehr | c3e1895 | 2018-05-11 16:59:31 -0700 | [diff] [blame] | 92 | #endif // ART_LIBARTBASE_BASE_FILE_UTILS_H_ |