blob: e4555ad3cb1ddd78a90c8f7633a77535e132670a [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
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
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);
Andreas Gampe57943812017-12-06 21:39:13 -080031bool PrintFileToLog(const std::string& file_name, android::base::LogSeverity level);
David Sehr891a50e2017-10-27 17:01:07 -070032
33// Find $ANDROID_ROOT, /system, or abort.
34std::string GetAndroidRoot();
35// Find $ANDROID_ROOT, /system, or return an empty string.
36std::string GetAndroidRootSafe(std::string* error_msg);
37
38// Find $ANDROID_DATA, /data, or abort.
39const char* GetAndroidData();
40// Find $ANDROID_DATA, /data, or return null.
41const 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.
45std::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.
49std::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.
54void 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.
59bool 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
63std::string GetSystemImageFilename(const char* location, InstructionSet isa);
64
65// Returns the vdex filename for the given oat filename.
66std::string GetVdexFilename(const std::string& oat_filename);
67
68// Returns true if the file exists.
69bool FileExists(const std::string& filename);
70bool FileExistsAndNotEmpty(const std::string& filename);
71
72// Returns `filename` with the text after the last occurrence of '.' replaced with
73// `extension`. If `filename` does not contain a period, returns a string containing `filename`,
74// a period, and `new_extension`.
75// Example: ReplaceFileExtension("foo.bar", "abc") == "foo.abc"
76// ReplaceFileExtension("foo", "abc") == "foo.abc"
77std::string ReplaceFileExtension(const std::string& filename, const std::string& new_extension);
78
79// Return the file size in bytes or -1 if the file does not exists.
80int64_t GetFileSizeBytes(const std::string& filename);
81
82// Madvise the largest page aligned region within begin and end.
83int MadviseLargestPageAlignedRegion(const uint8_t* begin, const uint8_t* end, int advice);
84
85} // namespace art
86
87#endif // ART_RUNTIME_BASE_FILE_UTILS_H_