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 #include <string_view>
24 
25 #include <android-base/logging.h>
26 
27 #include "arch/instruction_set.h"
28 
29 namespace art {
30 
31 static constexpr const char kAndroidArtApexDefaultPath[] = "/apex/com.android.art";
32 static constexpr const char kArtApexDataDefaultPath[] = "/data/misc/apexdata/com.android.art";
33 static constexpr const char kAndroidConscryptApexDefaultPath[] = "/apex/com.android.conscrypt";
34 static constexpr const char kAndroidI18nApexDefaultPath[] = "/apex/com.android.i18n";
35 
36 static constexpr const char kArtImageExtension[] = "art";
37 
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 //
45 // Find $ANDROID_ROOT, /system, or abort.
46 std::string GetAndroidRoot();
47 // Find $ANDROID_ROOT, /system, or return an empty string.
48 std::string GetAndroidRootSafe(/*out*/ std::string* error_msg);
49 
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.
55 //
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);
60 
61 // Return the path to the directory containing the ART binaries.
62 std::string GetArtBinDir();
63 
64 // Find $ANDROID_DATA, /data, or abort.
65 std::string GetAndroidData();
66 // Find $ANDROID_DATA, /data, or return an empty string.
67 std::string GetAndroidDataSafe(/*out*/ std::string* error_msg);
68 
69 // Find $ART_APEX_DATA, /data/misc/apexdata/com.android.art, or abort.
70 std::string GetArtApexData();
71 
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 
76 // Returns the default boot image location, based on the passed `android_root`.
77 std::string GetDefaultBootImageLocation(const std::string& android_root,
78                                         bool deny_art_apex_data_files);
79 
80 // Return true if we found the dalvik cache and stored it in the dalvik_cache argument.
81 // `have_android_data` will be set to true if we have an ANDROID_DATA that exists,
82 // `dalvik_cache_exists` will be true if there is a dalvik-cache directory that is present.
83 // The flag `is_global_cache` tells whether this cache is /data/dalvik-cache.
84 void GetDalvikCache(const char* subdir, bool create_if_absent, std::string* dalvik_cache,
85                     bool* have_android_data, bool* dalvik_cache_exists, bool* is_global_cache);
86 
87 // Returns the absolute dalvik-cache path for a DexFile or OatFile. The path returned will be
88 // rooted at `cache_location`.
89 bool GetDalvikCacheFilename(const char* location, const char* cache_location,
90                             std::string* filename, std::string* error_msg);
91 
92 // Gets the oat location in the ART APEX data directory for a DEX file installed anywhere other
93 // than in an APEX. Returns the oat filename if `location` is valid, empty string otherwise.
94 std::string GetApexDataOatFilename(std::string_view location, InstructionSet isa);
95 
96 // Gets the odex location in the ART APEX data directory for a DEX file installed anywhere other
97 // than in an APEX. Returns the odex filename if `location` is valid, empty string otherwise.
98 std::string GetApexDataOdexFilename(std::string_view location, InstructionSet isa);
99 
100 // Gets the boot image in the ART APEX data directory for a DEX file installed anywhere other
101 // than in an APEX. Returns the image location if `dex_location` is valid, empty string otherwise.
102 std::string GetApexDataBootImage(std::string_view dex_location);
103 
104 // Gets the image in the ART APEX data directory for a DEX file installed installed anywhere other
105 // than in an APEX. Returns the image location if `dex_location` is valid, empty string otherwise.
106 std::string GetApexDataImage(std::string_view dex_location);
107 
108 // Gets the name of a file in the ART APEX directory dalvik-cache. This method assumes the
109 // `dex_location` is for an application and that the `dex_location` is not within an APEX.
110 // Returns the location of the file in the dalvik-cache
111 std::string GetApexDataDalvikCacheFilename(std::string_view dex_location,
112                                            InstructionSet isa,
113                                            std::string_view file_extension);
114 
115 // Returns the system location for an image. This method inserts the `isa` between the
116 // dirname and basename of `location`.
117 std::string GetSystemImageFilename(const char* location, InstructionSet isa);
118 
119 // Returns the vdex filename for the given oat filename.
120 std::string GetVdexFilename(const std::string& oat_filename);
121 
122 // Returns `filename` with the text after the last occurrence of '.' replaced with
123 // `extension`. If `filename` does not contain a period, returns a string containing `filename`,
124 // a period, and `new_extension`.
125 // Example: ReplaceFileExtension("foo.bar", "abc") == "foo.abc"
126 //          ReplaceFileExtension("foo", "abc") == "foo.abc"
127 std::string ReplaceFileExtension(std::string_view filename, std::string_view new_extension);
128 
129 // Return whether the location is on /apex/com.android.art
130 bool LocationIsOnArtModule(std::string_view location);
131 
132 // Return whether the location is on /data/misc/apexdata/com.android.art/.
133 bool LocationIsOnArtApexData(std::string_view location);
134 
135 // Return whether the location is on /apex/com.android.conscrypt
136 bool LocationIsOnConscryptModule(std::string_view location);
137 
138 // Return whether the location is on /apex/com.android.i18n
139 bool LocationIsOnI18nModule(std::string_view location);
140 
141 // Return whether the location is on system (i.e. android root).
142 bool LocationIsOnSystem(const std::string& location);
143 
144 // Return whether the location is on system/framework (i.e. $ANDROID_ROOT/framework).
145 bool LocationIsOnSystemFramework(std::string_view location);
146 
147 // Return whether the location is on system_ext/framework
148 bool LocationIsOnSystemExtFramework(std::string_view location);
149 
150 // Return whether the location is on /apex/.
151 bool LocationIsOnApex(std::string_view location);
152 
153 // Returns whether the location is trusted for loading oat files. Trusted locations are protected
154 // by dm-verity or fs-verity. The recognized locations are on /system or
155 // /data/misc/apexdata/com.android.art.
156 bool LocationIsTrusted(const std::string& location, bool trust_art_apex_data_files);
157 
158 // Compare the ART module root against android root. Returns true if they are
159 // both known and distinct. This is meant to be a proxy for 'running with apex'.
160 bool ArtModuleRootDistinctFromAndroidRoot();
161 
162 // dup(2), except setting the O_CLOEXEC flag atomically, when possible.
163 int DupCloexec(int fd);
164 
165 // Returns true if `path` begins with a slash.
IsAbsoluteLocation(const std::string & path)166 inline bool IsAbsoluteLocation(const std::string& path) { return !path.empty() && path[0] == '/'; }
167 
168 }  // namespace art
169 
170 #endif  // ART_LIBARTBASE_BASE_FILE_UTILS_H_
171