blob: b1d044a4053d8cac28344c78a2660135e6b6e633 [file] [log] [blame]
David Sehr891a50e2017-10-27 17:01:07 -07001/*
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 Sehrc3e18952018-05-11 16:59:31 -070017#ifndef ART_LIBARTBASE_BASE_FILE_UTILS_H_
18#define ART_LIBARTBASE_BASE_FILE_UTILS_H_
David Sehr891a50e2017-10-27 17:01:07 -070019
20#include <stdlib.h>
21
22#include <string>
23
Andreas Gampe57943812017-12-06 21:39:13 -080024#include <android-base/logging.h>
25
David Sehr891a50e2017-10-27 17:01:07 -070026#include "arch/instruction_set.h"
David Sehr891a50e2017-10-27 17:01:07 -070027
28namespace art {
29
David Srbecky0c0f3022020-02-13 15:53:01 +000030static constexpr const char* kAndroidArtApexDefaultPath = "/apex/com.android.art";
31static constexpr const char* kAndroidConscryptApexDefaultPath = "/apex/com.android.conscrypt";
32
Roland Levillain50eec3d2019-04-05 18:53:58 +010033// These methods return the Android Root, which is the historical location of
34// the Android "system" directory, containing the built Android artifacts. On
35// target, this is normally "/system". On host this is usually a directory under
36// the build tree, e.g. "$ANDROID_BUILD_TOP/out/host/linux-x86". The location of
37// the Android Root can be overriden using the ANDROID_ROOT environment
38// variable.
39//
David Sehr891a50e2017-10-27 17:01:07 -070040// Find $ANDROID_ROOT, /system, or abort.
41std::string GetAndroidRoot();
42// Find $ANDROID_ROOT, /system, or return an empty string.
Roland Levillain9ff900d2019-03-29 18:50:01 +000043std::string GetAndroidRootSafe(/*out*/ std::string* error_msg);
David Sehr891a50e2017-10-27 17:01:07 -070044
Martin Stjernholme58624f2019-09-20 15:53:40 +010045// These methods return the ART Root, which is the location of the (activated)
46// ART APEX module. On target, this is normally "/apex/com.android.art". On
47// host, this is usually a subdirectory of the Android Root, e.g.
48// "$ANDROID_BUILD_TOP/out/host/linux-x86/com.android.art". The location of the
49// ART root can be overridden using the ANDROID_ART_ROOT environment variable.
Roland Levillain50eec3d2019-04-05 18:53:58 +010050//
Martin Stjernholme58624f2019-09-20 15:53:40 +010051// Find $ANDROID_ART_ROOT, /apex/com.android.art, or abort.
52std::string GetArtRoot();
53// Find $ANDROID_ART_ROOT, /apex/com.android.art, or return an empty string.
54std::string GetArtRootSafe(/*out*/ std::string* error_msg);
Roland Levillain1ea8a622019-03-29 19:08:56 +000055
Martin Stjernholme58624f2019-09-20 15:53:40 +010056// Return the path to the directory containing the ART binaries.
57std::string GetArtBinDir();
Roland Levillainfb6a5c02019-03-29 20:20:16 +000058
David Sehr891a50e2017-10-27 17:01:07 -070059// Find $ANDROID_DATA, /data, or abort.
Roland Levillain2e3cb542019-04-05 18:00:04 +010060std::string GetAndroidData();
61// Find $ANDROID_DATA, /data, or return an empty string.
62std::string GetAndroidDataSafe(/*out*/ std::string* error_msg);
David Sehr891a50e2017-10-27 17:01:07 -070063
64// Returns the default boot image location (ANDROID_ROOT/framework/boot.art).
65// Returns an empty string if ANDROID_ROOT is not set.
66std::string GetDefaultBootImageLocation(std::string* error_msg);
67
Nicolas Geoffrayde1b2a22019-02-27 09:10:57 +000068// Returns the default boot image location, based on the passed android root.
69std::string GetDefaultBootImageLocation(const std::string& android_root);
70
David Sehr891a50e2017-10-27 17:01:07 -070071// Returns the dalvik-cache location, with subdir appended. Returns the empty string if the cache
72// could not be found.
73std::string GetDalvikCache(const char* subdir);
David Sehrc3e18952018-05-11 16:59:31 -070074
David Sehr891a50e2017-10-27 17:01:07 -070075// Return true if we found the dalvik cache and stored it in the dalvik_cache argument.
76// have_android_data will be set to true if we have an ANDROID_DATA that exists,
77// dalvik_cache_exists will be true if there is a dalvik-cache directory that is present.
78// The flag is_global_cache tells whether this cache is /data/dalvik-cache.
79void GetDalvikCache(const char* subdir, bool create_if_absent, std::string* dalvik_cache,
80 bool* have_android_data, bool* dalvik_cache_exists, bool* is_global_cache);
81
82// Returns the absolute dalvik-cache path for a DexFile or OatFile. The path returned will be
83// rooted at cache_location.
84bool GetDalvikCacheFilename(const char* file_location, const char* cache_location,
85 std::string* filename, std::string* error_msg);
86
87// Returns the system location for an image
88std::string GetSystemImageFilename(const char* location, InstructionSet isa);
89
90// Returns the vdex filename for the given oat filename.
91std::string GetVdexFilename(const std::string& oat_filename);
92
David Sehr891a50e2017-10-27 17:01:07 -070093// Returns `filename` with the text after the last occurrence of '.' replaced with
94// `extension`. If `filename` does not contain a period, returns a string containing `filename`,
95// a period, and `new_extension`.
96// Example: ReplaceFileExtension("foo.bar", "abc") == "foo.abc"
97// ReplaceFileExtension("foo", "abc") == "foo.abc"
98std::string ReplaceFileExtension(const std::string& filename, const std::string& new_extension);
99
Martin Stjernholmad909af2019-07-16 17:02:44 +0100100// Return whether the location is on /apex/com.android.art
Martin Stjernholme58624f2019-09-20 15:53:40 +0100101bool LocationIsOnArtModule(const char* location);
Orion Hodson12162de2019-01-21 16:01:30 +0000102
David Brazdil3c839212019-03-04 14:29:50 +0000103// Return whether the location is on /apex/com.android.conscrypt
104bool LocationIsOnConscryptModule(const char* location);
105
Nicolas Geoffray29742602017-12-14 10:09:03 +0000106// Return whether the location is on system (i.e. android root).
107bool LocationIsOnSystem(const char* location);
108
David Brazdil8e1a7cb2018-03-27 08:14:25 +0000109// Return whether the location is on system/framework (i.e. android_root/framework).
110bool LocationIsOnSystemFramework(const char* location);
111
David Brazdile7e26d12019-02-28 15:04:14 +0000112// Return whether the location is on /apex/.
113bool LocationIsOnApex(const char* location);
114
Martin Stjernholme58624f2019-09-20 15:53:40 +0100115// Compare the ART module root against android root. Returns true if they are
David Brazdilbfaba282019-03-15 11:35:51 +0000116// both known and distinct. This is meant to be a proxy for 'running with apex'.
Martin Stjernholme58624f2019-09-20 15:53:40 +0100117bool ArtModuleRootDistinctFromAndroidRoot();
David Brazdilbfaba282019-03-15 11:35:51 +0000118
Josh Gao35696a02018-08-30 17:24:16 -0700119// dup(2), except setting the O_CLOEXEC flag atomically, when possible.
120int DupCloexec(int fd);
121
David Brazdil3e8aae02019-03-26 18:48:02 +0000122// Returns true if `path` begins with a slash.
123inline bool IsAbsoluteLocation(const std::string& path) { return !path.empty() && path[0] == '/'; }
124
David Sehr891a50e2017-10-27 17:01:07 -0700125} // namespace art
126
David Sehrc3e18952018-05-11 16:59:31 -0700127#endif // ART_LIBARTBASE_BASE_FILE_UTILS_H_