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 17 #ifndef ART_RUNTIME_BASE_FILE_UTILS_H_ 18 #define ART_RUNTIME_BASE_FILE_UTILS_H_ 19 20 #include <stdlib.h> 21 22 #include <string> 23 24 #include <android-base/logging.h> 25 26 #include "arch/instruction_set.h" 27 28 namespace art { 29 30 bool ReadFileToString(const std::string& file_name, std::string* result); 31 bool PrintFileToLog(const std::string& file_name, android::base::LogSeverity level); 32 33 // Find $ANDROID_ROOT, /system, or abort. 34 std::string GetAndroidRoot(); 35 // Find $ANDROID_ROOT, /system, or return an empty string. 36 std::string GetAndroidRootSafe(std::string* error_msg); 37 38 // Find $ANDROID_DATA, /data, or abort. 39 const char* GetAndroidData(); 40 // Find $ANDROID_DATA, /data, or return null. 41 const char* GetAndroidDataSafe(std::string* error_msg); 42 43 // Returns the default boot image location (ANDROID_ROOT/framework/boot.art). 44 // Returns an empty string if ANDROID_ROOT is not set. 45 std::string GetDefaultBootImageLocation(std::string* error_msg); 46 47 // Returns the dalvik-cache location, with subdir appended. Returns the empty string if the cache 48 // could not be found. 49 std::string GetDalvikCache(const char* subdir); 50 // Return true if we found the dalvik cache and stored it in the dalvik_cache argument. 51 // have_android_data will be set to true if we have an ANDROID_DATA that exists, 52 // dalvik_cache_exists will be true if there is a dalvik-cache directory that is present. 53 // The flag is_global_cache tells whether this cache is /data/dalvik-cache. 54 void GetDalvikCache(const char* subdir, bool create_if_absent, std::string* dalvik_cache, 55 bool* have_android_data, bool* dalvik_cache_exists, bool* is_global_cache); 56 57 // Returns the absolute dalvik-cache path for a DexFile or OatFile. The path returned will be 58 // rooted at cache_location. 59 bool GetDalvikCacheFilename(const char* file_location, const char* cache_location, 60 std::string* filename, std::string* error_msg); 61 62 // Returns the system location for an image 63 std::string GetSystemImageFilename(const char* location, InstructionSet isa); 64 65 // Returns the vdex filename for the given oat filename. 66 std::string GetVdexFilename(const std::string& oat_filename); 67 68 // Returns `filename` with the text after the last occurrence of '.' replaced with 69 // `extension`. If `filename` does not contain a period, returns a string containing `filename`, 70 // a period, and `new_extension`. 71 // Example: ReplaceFileExtension("foo.bar", "abc") == "foo.abc" 72 // ReplaceFileExtension("foo", "abc") == "foo.abc" 73 std::string ReplaceFileExtension(const std::string& filename, const std::string& new_extension); 74 75 // Return whether the location is on system (i.e. android root). 76 bool LocationIsOnSystem(const char* location); 77 78 // Return whether the location is on system/framework (i.e. android_root/framework). 79 bool LocationIsOnSystemFramework(const char* location); 80 81 } // namespace art 82 83 #endif // ART_RUNTIME_BASE_FILE_UTILS_H_ 84