Replace more literal strings with constants in libartbase/base/file_utils.cc.
Also use more uniform names for some of these constants as well.
Test: m test-art-host-gtest-file_utils_test
Change-Id: I2bae31b242497d6e0275912c392b032ed1d623bb
diff --git a/libartbase/base/file_utils.cc b/libartbase/base/file_utils.cc
index 865c8a6..af1d2e6 100644
--- a/libartbase/base/file_utils.cc
+++ b/libartbase/base/file_utils.cc
@@ -66,10 +66,14 @@
static constexpr const char* kClassesDex = "classes.dex";
static constexpr const char* kApexDefaultPath = "/apex/";
-static constexpr const char* kRuntimeApexEnvVar = "ANDROID_RUNTIME_ROOT";
-static constexpr const char* kRuntimeApexDefaultPath = "/apex/com.android.runtime";
-static constexpr const char* kConscryptApexEnvVar = "ANDROID_CONSCRYPT_ROOT";
-static constexpr const char* kConscryptApexDefaultPath = "/apex/com.android.conscrypt";
+static constexpr const char* kAndroidRootEnvVar = "ANDROID_ROOT";
+static constexpr const char* kAndroidRootDefaultPath = "/system";
+static constexpr const char* kAndroidDataEnvVar = "ANDROID_DATA";
+static constexpr const char* kAndroidDataDefaultPath = "/data";
+static constexpr const char* kAndroidRuntimeRootEnvVar = "ANDROID_RUNTIME_ROOT";
+static constexpr const char* kAndroidRuntimeApexDefaultPath = "/apex/com.android.runtime";
+static constexpr const char* kAndroidConscryptRootEnvVar = "ANDROID_CONSCRYPT_ROOT";
+static constexpr const char* kAndroidConscryptApexDefaultPath = "/apex/com.android.conscrypt";
bool ReadFileToString(const std::string& file_name, std::string* result) {
File file(file_name, O_RDONLY, false);
@@ -92,17 +96,18 @@
std::string GetAndroidRootSafe(std::string* error_msg) {
#ifdef _WIN32
+ UNUSED(kAndroidRootEnvVar, kAndroidRootDefaultPath);
*error_msg = "GetAndroidRootSafe unsupported for Windows.";
return "";
#else
// Prefer ANDROID_ROOT if it's set.
- const char* android_dir = getenv("ANDROID_ROOT");
- if (android_dir != nullptr) {
- if (!OS::DirectoryExists(android_dir)) {
- *error_msg = StringPrintf("Failed to find ANDROID_ROOT directory %s", android_dir);
+ const char* android_root_from_env = getenv(kAndroidRootEnvVar);
+ if (android_root_from_env != nullptr) {
+ if (!OS::DirectoryExists(android_root_from_env)) {
+ *error_msg = StringPrintf("Failed to find ANDROID_ROOT directory %s", android_root_from_env);
return "";
}
- return android_dir;
+ return android_root_from_env;
}
// Check where libart is from, and derive from there. Only do this for non-Mac.
@@ -123,12 +128,12 @@
}
#endif
- // Try "/system".
- if (!OS::DirectoryExists("/system")) {
- *error_msg = "Failed to find ANDROID_ROOT directory /system";
+ // Try the default path.
+ if (!OS::DirectoryExists(kAndroidRootDefaultPath)) {
+ *error_msg = StringPrintf("Failed to find directory %s", kAndroidRootDefaultPath);
return "";
}
- return "/system";
+ return kAndroidRootDefaultPath;
#endif
}
@@ -174,11 +179,11 @@
}
const char* GetAndroidData() {
- return GetAndroidDir("ANDROID_DATA", "/data");
+ return GetAndroidDir(kAndroidDataEnvVar, kAndroidDataDefaultPath);
}
const char* GetAndroidDataSafe(std::string* error_msg) {
- return GetAndroidDirSafe("ANDROID_DATA", "/data", error_msg);
+ return GetAndroidDirSafe(kAndroidDataEnvVar, kAndroidDataDefaultPath, error_msg);
}
std::string GetDefaultBootImageLocation(const std::string& android_root) {
@@ -205,8 +210,8 @@
LOG(FATAL) << "GetDalvikCache unsupported on Windows.";
#else
CHECK(subdir != nullptr);
- std::string error_msg;
- const char* android_data = GetAndroidDataSafe(&error_msg);
+ std::string unused_error_msg;
+ const char* android_data = GetAndroidDataSafe(&unused_error_msg);
if (android_data == nullptr) {
*have_android_data = false;
*dalvik_cache_exists = false;
@@ -218,7 +223,7 @@
const std::string dalvik_cache_root(StringPrintf("%s/dalvik-cache/", android_data));
*dalvik_cache = dalvik_cache_root + subdir;
*dalvik_cache_exists = OS::DirectoryExists(dalvik_cache->c_str());
- *is_global_cache = strcmp(android_data, "/data") == 0;
+ *is_global_cache = strcmp(android_data, kAndroidDataDefaultPath) == 0;
if (create_if_absent && !*dalvik_cache_exists && !*is_global_cache) {
// Don't create the system's /data/dalvik-cache/... because it needs special permissions.
*dalvik_cache_exists = ((mkdir(dalvik_cache_root.c_str(), 0700) == 0 || errno == EEXIST) &&
@@ -290,8 +295,8 @@
static bool IsLocationOnModule(const char* full_path,
const char* env_var,
const char* default_path) {
- std::string error_msg;
- const char* module_path = GetAndroidDirSafe(env_var, default_path, &error_msg);
+ std::string unused_error_msg;
+ const char* module_path = GetAndroidDirSafe(env_var, default_path, &unused_error_msg);
if (module_path == nullptr) {
return false;
}
@@ -299,11 +304,12 @@
}
bool LocationIsOnRuntimeModule(const char* full_path) {
- return IsLocationOnModule(full_path, kRuntimeApexEnvVar, kRuntimeApexDefaultPath);
+ return IsLocationOnModule(full_path, kAndroidRuntimeRootEnvVar, kAndroidRuntimeApexDefaultPath);
}
bool LocationIsOnConscryptModule(const char* full_path) {
- return IsLocationOnModule(full_path, kConscryptApexEnvVar, kConscryptApexDefaultPath);
+ return IsLocationOnModule(
+ full_path, kAndroidConscryptRootEnvVar, kAndroidConscryptApexDefaultPath);
}
bool LocationIsOnApex(const char* full_path) {
@@ -323,8 +329,8 @@
}
bool LocationIsOnSystemFramework(const char* full_path) {
- std::string error_msg;
- std::string root_path = GetAndroidRootSafe(&error_msg);
+ std::string unused_error_msg;
+ std::string root_path = GetAndroidRootSafe(&unused_error_msg);
if (root_path.empty()) {
// Could not find Android root.
// TODO(dbrazdil): change to stricter GetAndroidRoot() once b/76452688 is resolved.
@@ -338,7 +344,7 @@
std::string error_msg;
std::string android_root = GetAndroidRootSafe(&error_msg);
const char* runtime_root =
- GetAndroidDirSafe(kRuntimeApexEnvVar, kRuntimeApexDefaultPath, &error_msg);
+ GetAndroidDirSafe(kAndroidRuntimeRootEnvVar, kAndroidRuntimeApexDefaultPath, &error_msg);
return !android_root.empty()
&& (runtime_root != nullptr)
&& (android_root != std::string_view(runtime_root));