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_LIBARTBASE_BASE_FILE_UTILS_H_
18 #define ART_LIBARTBASE_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 static constexpr const char* kAndroidArtApexDefaultPath = "/apex/com.android.art";
31 static constexpr const char* kAndroidConscryptApexDefaultPath = "/apex/com.android.conscrypt";
32 static constexpr const char* kAndroidI18nApexDefaultPath = "/apex/com.android.i18n";
33 
34 // These methods return the Android Root, which is the historical location of
35 // the Android "system" directory, containing the built Android artifacts. On
36 // target, this is normally "/system". On host this is usually a directory under
37 // the build tree, e.g. "$ANDROID_BUILD_TOP/out/host/linux-x86". The location of
38 // the Android Root can be overriden using the ANDROID_ROOT environment
39 // variable.
40 //
41 // Find $ANDROID_ROOT, /system, or abort.
42 std::string GetAndroidRoot();
43 // Find $ANDROID_ROOT, /system, or return an empty string.
44 std::string GetAndroidRootSafe(/*out*/ std::string* error_msg);
45 
46 // These methods return the ART Root, which is the location of the (activated)
47 // ART APEX module. On target, this is normally "/apex/com.android.art". On
48 // host, this is usually a subdirectory of the Android Root, e.g.
49 // "$ANDROID_BUILD_TOP/out/host/linux-x86/com.android.art". The location of the
50 // ART root can be overridden using the ANDROID_ART_ROOT environment variable.
51 //
52 // Find $ANDROID_ART_ROOT, /apex/com.android.art, or abort.
53 std::string GetArtRoot();
54 // Find $ANDROID_ART_ROOT, /apex/com.android.art, or return an empty string.
55 std::string GetArtRootSafe(/*out*/ std::string* error_msg);
56 
57 // Return the path to the directory containing the ART binaries.
58 std::string GetArtBinDir();
59 
60 // Find $ANDROID_DATA, /data, or abort.
61 std::string GetAndroidData();
62 // Find $ANDROID_DATA, /data, or return an empty string.
63 std::string GetAndroidDataSafe(/*out*/ std::string* error_msg);
64 
65 // Returns the default boot image location (ANDROID_ROOT/framework/boot.art).
66 // Returns an empty string if ANDROID_ROOT is not set.
67 std::string GetDefaultBootImageLocation(std::string* error_msg);
68 
69 // Returns the default boot image location, based on the passed android root.
70 std::string GetDefaultBootImageLocation(const std::string& android_root);
71 
72 // Returns the dalvik-cache location, with subdir appended. Returns the empty string if the cache
73 // could not be found.
74 std::string GetDalvikCache(const char* subdir);
75 
76 // Return true if we found the dalvik cache and stored it in the dalvik_cache argument.
77 // have_android_data will be set to true if we have an ANDROID_DATA that exists,
78 // dalvik_cache_exists will be true if there is a dalvik-cache directory that is present.
79 // The flag is_global_cache tells whether this cache is /data/dalvik-cache.
80 void GetDalvikCache(const char* subdir, bool create_if_absent, std::string* dalvik_cache,
81                     bool* have_android_data, bool* dalvik_cache_exists, bool* is_global_cache);
82 
83 // Returns the absolute dalvik-cache path for a DexFile or OatFile. The path returned will be
84 // rooted at cache_location.
85 bool GetDalvikCacheFilename(const char* file_location, const char* cache_location,
86                             std::string* filename, std::string* error_msg);
87 
88 // Returns the system location for an image
89 std::string GetSystemImageFilename(const char* location, InstructionSet isa);
90 
91 // Returns the vdex filename for the given oat filename.
92 std::string GetVdexFilename(const std::string& oat_filename);
93 
94 // Returns `filename` with the text after the last occurrence of '.' replaced with
95 // `extension`. If `filename` does not contain a period, returns a string containing `filename`,
96 // a period, and `new_extension`.
97 // Example: ReplaceFileExtension("foo.bar", "abc") == "foo.abc"
98 //          ReplaceFileExtension("foo", "abc") == "foo.abc"
99 std::string ReplaceFileExtension(const std::string& filename, const std::string& new_extension);
100 
101 // Return whether the location is on /apex/com.android.art
102 bool LocationIsOnArtModule(const char* location);
103 
104 // Return whether the location is on /apex/com.android.conscrypt
105 bool LocationIsOnConscryptModule(const char* location);
106 
107 // Return whether the location is on /apex/com.android.i18n
108 bool LocationIsOnI18nModule(const char* location);
109 
110 // Return whether the location is on system (i.e. android root).
111 bool LocationIsOnSystem(const char* location);
112 
113 // Return whether the location is on system/framework (i.e. android_root/framework).
114 bool LocationIsOnSystemFramework(const char* location);
115 
116 // Return whether the location is on system_ext/framework
117 bool LocationIsOnSystemExtFramework(const char* location);
118 
119 // Return whether the location is on /apex/.
120 bool LocationIsOnApex(const char* location);
121 
122 // Compare the ART module root against android root. Returns true if they are
123 // both known and distinct. This is meant to be a proxy for 'running with apex'.
124 bool ArtModuleRootDistinctFromAndroidRoot();
125 
126 // dup(2), except setting the O_CLOEXEC flag atomically, when possible.
127 int DupCloexec(int fd);
128 
129 // Returns true if `path` begins with a slash.
IsAbsoluteLocation(const std::string & path)130 inline bool IsAbsoluteLocation(const std::string& path) { return !path.empty() && path[0] == '/'; }
131 
132 }  // namespace art
133 
134 #endif  // ART_LIBARTBASE_BASE_FILE_UTILS_H_
135