diff options
Diffstat (limited to 'cmds/installd/globals.cpp')
-rw-r--r-- | cmds/installd/globals.cpp | 134 |
1 files changed, 53 insertions, 81 deletions
diff --git a/cmds/installd/globals.cpp b/cmds/installd/globals.cpp index edcdb6a1e2..b3a6dafa9a 100644 --- a/cmds/installd/globals.cpp +++ b/cmds/installd/globals.cpp @@ -16,15 +16,15 @@ #define LOG_TAG "installd" -#include <stdlib.h> -#include <string.h> - -#include <log/log.h> // TODO: Move everything to base::logging. - #include <globals.h> #include <installd_constants.h> #include <utils.h> +#include <android-base/logging.h> + +#include <stdlib.h> +#include <string.h> + namespace android { namespace installd { @@ -44,106 +44,78 @@ static constexpr const char* PROFILES_SUBDIR = "misc/profiles"; // sub-directory static constexpr const char* PRIVATE_APP_SUBDIR = "app-private/"; // sub-directory under // ANDROID_DATA -/* Directory records that are used in execution of commands. */ -dir_rec_t android_app_dir; -dir_rec_t android_app_ephemeral_dir; -dir_rec_t android_app_lib_dir; -dir_rec_t android_app_private_dir; -dir_rec_t android_asec_dir; -dir_rec_t android_data_dir; -dir_rec_t android_media_dir; -dir_rec_t android_mnt_expand_dir; -dir_rec_t android_profiles_dir; - -dir_rec_array_t android_system_dirs; - -/** - * Initialize all the global variables that are used elsewhere. Returns 0 upon - * success and -1 on error. - */ -void free_globals() { - size_t i; - - for (i = 0; i < android_system_dirs.count; i++) { - if (android_system_dirs.dirs[i].path != NULL) { - free(android_system_dirs.dirs[i].path); - } +std::string android_app_dir; +std::string android_app_ephemeral_dir; +std::string android_app_lib_dir; +std::string android_app_private_dir; +std::string android_asec_dir; +std::string android_data_dir; +std::string android_media_dir; +std::string android_mnt_expand_dir; +std::string android_profiles_dir; +std::string android_root_dir; + +std::vector<std::string> android_system_dirs; + +bool init_globals_from_data_and_root() { + const char* data_path = getenv("ANDROID_DATA"); + if (data_path == nullptr) { + LOG(ERROR) << "Could not find ANDROID_DATA"; + return false; + } + const char* root_path = getenv("ANDROID_ROOT"); + if (root_path == nullptr) { + LOG(ERROR) << "Could not find ANDROID_ROOT"; + return false; } + return init_globals_from_data_and_root(data_path, root_path); +} - free(android_system_dirs.dirs); +static std::string ensure_trailing_slash(const std::string& path) { + if (path.rfind('/') != path.size() - 1) { + return path + '/'; + } else { + return path; + } } bool init_globals_from_data_and_root(const char* data, const char* root) { // Get the android data directory. - if (get_path_from_string(&android_data_dir, data) < 0) { - return false; - } + android_data_dir = ensure_trailing_slash(data); + + // Get the android root directory. + android_root_dir = ensure_trailing_slash(root); // Get the android app directory. - if (copy_and_append(&android_app_dir, &android_data_dir, APP_SUBDIR) < 0) { - return false; - } + android_app_dir = android_data_dir + APP_SUBDIR; // Get the android protected app directory. - if (copy_and_append(&android_app_private_dir, &android_data_dir, PRIVATE_APP_SUBDIR) < 0) { - return false; - } + android_app_private_dir = android_data_dir + PRIVATE_APP_SUBDIR; // Get the android ephemeral app directory. - if (copy_and_append(&android_app_ephemeral_dir, &android_data_dir, EPHEMERAL_APP_SUBDIR) < 0) { - return false; - } + android_app_ephemeral_dir = android_data_dir + EPHEMERAL_APP_SUBDIR; // Get the android app native library directory. - if (copy_and_append(&android_app_lib_dir, &android_data_dir, APP_LIB_SUBDIR) < 0) { - return false; - } + android_app_lib_dir = android_data_dir + APP_LIB_SUBDIR; // Get the sd-card ASEC mount point. - if (get_path_from_env(&android_asec_dir, ASEC_MOUNTPOINT_ENV_NAME) < 0) { - return false; - } + android_asec_dir = ensure_trailing_slash(getenv(ASEC_MOUNTPOINT_ENV_NAME)); // Get the android media directory. - if (copy_and_append(&android_media_dir, &android_data_dir, MEDIA_SUBDIR) < 0) { - return false; - } + android_media_dir = android_data_dir + MEDIA_SUBDIR; // Get the android external app directory. - if (get_path_from_string(&android_mnt_expand_dir, "/mnt/expand/") < 0) { - return false; - } + android_mnt_expand_dir = "/mnt/expand/"; // Get the android profiles directory. - if (copy_and_append(&android_profiles_dir, &android_data_dir, PROFILES_SUBDIR) < 0) { - return false; - } + android_profiles_dir = android_data_dir + PROFILES_SUBDIR; // Take note of the system and vendor directories. - android_system_dirs.count = 4; - - android_system_dirs.dirs = (dir_rec_t*) calloc(android_system_dirs.count, sizeof(dir_rec_t)); - if (android_system_dirs.dirs == NULL) { - ALOGE("Couldn't allocate array for dirs; aborting\n"); - return false; - } - - dir_rec_t android_root_dir; - if (get_path_from_string(&android_root_dir, root) < 0) { - return false; - } - - android_system_dirs.dirs[0].path = build_string2(android_root_dir.path, APP_SUBDIR); - android_system_dirs.dirs[0].len = strlen(android_system_dirs.dirs[0].path); - - android_system_dirs.dirs[1].path = build_string2(android_root_dir.path, PRIV_APP_SUBDIR); - android_system_dirs.dirs[1].len = strlen(android_system_dirs.dirs[1].path); - - android_system_dirs.dirs[2].path = strdup("/vendor/app/"); - android_system_dirs.dirs[2].len = strlen(android_system_dirs.dirs[2].path); - - android_system_dirs.dirs[3].path = strdup("/oem/app/"); - android_system_dirs.dirs[3].len = strlen(android_system_dirs.dirs[3].path); + android_system_dirs.clear(); + android_system_dirs.push_back(android_root_dir + APP_SUBDIR); + android_system_dirs.push_back(android_root_dir + PRIV_APP_SUBDIR); + android_system_dirs.push_back("/vendor/app/"); + android_system_dirs.push_back("/oem/app/"); return true; } |