blob: 2406336bcb3b945deda41b7fa6d4c859530a7275 [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
30bool ReadFileToString(const std::string& file_name, std::string* result);
David Sehr891a50e2017-10-27 17:01:07 -070031
32// Find $ANDROID_ROOT, /system, or abort.
33std::string GetAndroidRoot();
34// Find $ANDROID_ROOT, /system, or return an empty string.
Roland Levillain9ff900d2019-03-29 18:50:01 +000035std::string GetAndroidRootSafe(/*out*/ std::string* error_msg);
David Sehr891a50e2017-10-27 17:01:07 -070036
Roland Levillain1ea8a622019-03-29 19:08:56 +000037// Find $ANDROID_RUNTIME_ROOT, /apex/com.android.runtime, or abort.
38std::string GetAndroidRuntimeRoot();
39// Find $ANDROID_RUNTIME_ROOT, /apex/com.android.runtime, or return an empty string.
40std::string GetAndroidRuntimeRootSafe(/*out*/ std::string* error_msg);
41
David Sehr891a50e2017-10-27 17:01:07 -070042// Find $ANDROID_DATA, /data, or abort.
Roland Levillain2e3cb542019-04-05 18:00:04 +010043std::string GetAndroidData();
44// Find $ANDROID_DATA, /data, or return an empty string.
45std::string GetAndroidDataSafe(/*out*/ std::string* error_msg);
David Sehr891a50e2017-10-27 17:01:07 -070046
47// Returns the default boot image location (ANDROID_ROOT/framework/boot.art).
48// Returns an empty string if ANDROID_ROOT is not set.
49std::string GetDefaultBootImageLocation(std::string* error_msg);
50
Nicolas Geoffrayde1b2a22019-02-27 09:10:57 +000051// Returns the default boot image location, based on the passed android root.
52std::string GetDefaultBootImageLocation(const std::string& android_root);
53
David Sehr891a50e2017-10-27 17:01:07 -070054// Returns the dalvik-cache location, with subdir appended. Returns the empty string if the cache
55// could not be found.
56std::string GetDalvikCache(const char* subdir);
David Sehrc3e18952018-05-11 16:59:31 -070057
David Sehr891a50e2017-10-27 17:01:07 -070058// Return true if we found the dalvik cache and stored it in the dalvik_cache argument.
59// have_android_data will be set to true if we have an ANDROID_DATA that exists,
60// dalvik_cache_exists will be true if there is a dalvik-cache directory that is present.
61// The flag is_global_cache tells whether this cache is /data/dalvik-cache.
62void GetDalvikCache(const char* subdir, bool create_if_absent, std::string* dalvik_cache,
63 bool* have_android_data, bool* dalvik_cache_exists, bool* is_global_cache);
64
65// Returns the absolute dalvik-cache path for a DexFile or OatFile. The path returned will be
66// rooted at cache_location.
67bool GetDalvikCacheFilename(const char* file_location, const char* cache_location,
68 std::string* filename, std::string* error_msg);
69
70// Returns the system location for an image
71std::string GetSystemImageFilename(const char* location, InstructionSet isa);
72
73// Returns the vdex filename for the given oat filename.
74std::string GetVdexFilename(const std::string& oat_filename);
75
David Sehr891a50e2017-10-27 17:01:07 -070076// Returns `filename` with the text after the last occurrence of '.' replaced with
77// `extension`. If `filename` does not contain a period, returns a string containing `filename`,
78// a period, and `new_extension`.
79// Example: ReplaceFileExtension("foo.bar", "abc") == "foo.abc"
80// ReplaceFileExtension("foo", "abc") == "foo.abc"
81std::string ReplaceFileExtension(const std::string& filename, const std::string& new_extension);
82
David Brazdile7e26d12019-02-28 15:04:14 +000083// Return whether the location is on /apex/com.android.runtime
Orion Hodson12162de2019-01-21 16:01:30 +000084bool LocationIsOnRuntimeModule(const char* location);
85
David Brazdil3c839212019-03-04 14:29:50 +000086// Return whether the location is on /apex/com.android.conscrypt
87bool LocationIsOnConscryptModule(const char* location);
88
Nicolas Geoffray29742602017-12-14 10:09:03 +000089// Return whether the location is on system (i.e. android root).
90bool LocationIsOnSystem(const char* location);
91
David Brazdil8e1a7cb2018-03-27 08:14:25 +000092// Return whether the location is on system/framework (i.e. android_root/framework).
93bool LocationIsOnSystemFramework(const char* location);
94
David Brazdile7e26d12019-02-28 15:04:14 +000095// Return whether the location is on /apex/.
96bool LocationIsOnApex(const char* location);
97
David Brazdilbfaba282019-03-15 11:35:51 +000098// Compare the runtime module root against android root. Returns true if they are
99// both known and distinct. This is meant to be a proxy for 'running with apex'.
100bool RuntimeModuleRootDistinctFromAndroidRoot();
101
Josh Gao35696a02018-08-30 17:24:16 -0700102// dup(2), except setting the O_CLOEXEC flag atomically, when possible.
103int DupCloexec(int fd);
104
David Sehr891a50e2017-10-27 17:01:07 -0700105} // namespace art
106
David Sehrc3e18952018-05-11 16:59:31 -0700107#endif // ART_LIBARTBASE_BASE_FILE_UTILS_H_