blob: 007f3b443d2bb29f1557a30a6e25a2dc77cd2c6f [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
24#include "arch/instruction_set.h"
25#include "base/logging.h"
26
27namespace art {
28
29bool ReadFileToString(const std::string& file_name, std::string* result);
30bool PrintFileToLog(const std::string& file_name, LogSeverity level);
31
32// Find $ANDROID_ROOT, /system, or abort.
33std::string GetAndroidRoot();
34// Find $ANDROID_ROOT, /system, or return an empty string.
35std::string GetAndroidRootSafe(std::string* error_msg);
36
37// Find $ANDROID_DATA, /data, or abort.
38const char* GetAndroidData();
39// Find $ANDROID_DATA, /data, or return null.
40const char* GetAndroidDataSafe(std::string* error_msg);
41
42// Returns the default boot image location (ANDROID_ROOT/framework/boot.art).
43// Returns an empty string if ANDROID_ROOT is not set.
44std::string GetDefaultBootImageLocation(std::string* error_msg);
45
46// Returns the dalvik-cache location, with subdir appended. Returns the empty string if the cache
47// could not be found.
48std::string GetDalvikCache(const char* subdir);
49// Return true if we found the dalvik cache and stored it in the dalvik_cache argument.
50// have_android_data will be set to true if we have an ANDROID_DATA that exists,
51// dalvik_cache_exists will be true if there is a dalvik-cache directory that is present.
52// The flag is_global_cache tells whether this cache is /data/dalvik-cache.
53void GetDalvikCache(const char* subdir, bool create_if_absent, std::string* dalvik_cache,
54 bool* have_android_data, bool* dalvik_cache_exists, bool* is_global_cache);
55
56// Returns the absolute dalvik-cache path for a DexFile or OatFile. The path returned will be
57// rooted at cache_location.
58bool GetDalvikCacheFilename(const char* file_location, const char* cache_location,
59 std::string* filename, std::string* error_msg);
60
61// Returns the system location for an image
62std::string GetSystemImageFilename(const char* location, InstructionSet isa);
63
64// Returns the vdex filename for the given oat filename.
65std::string GetVdexFilename(const std::string& oat_filename);
66
67// Returns true if the file exists.
68bool FileExists(const std::string& filename);
69bool FileExistsAndNotEmpty(const std::string& filename);
70
71// Returns `filename` with the text after the last occurrence of '.' replaced with
72// `extension`. If `filename` does not contain a period, returns a string containing `filename`,
73// a period, and `new_extension`.
74// Example: ReplaceFileExtension("foo.bar", "abc") == "foo.abc"
75// ReplaceFileExtension("foo", "abc") == "foo.abc"
76std::string ReplaceFileExtension(const std::string& filename, const std::string& new_extension);
77
78// Return the file size in bytes or -1 if the file does not exists.
79int64_t GetFileSizeBytes(const std::string& filename);
80
81// Madvise the largest page aligned region within begin and end.
82int MadviseLargestPageAlignedRegion(const uint8_t* begin, const uint8_t* end, int advice);
83
84} // namespace art
85
86#endif // ART_RUNTIME_BASE_FILE_UTILS_H_