1/*
2** Copyright 2015, 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#define LOG_TAG "installd"
18
19#include <globals.h>
20#include <installd_constants.h>
21#include <utils.h>
22
23#include <android-base/logging.h>
24
25#include <stdlib.h>
26#include <string.h>
27
28namespace android {
29namespace installd {
30
31static constexpr const char* APP_SUBDIR = "app/"; // sub-directory under ANDROID_DATA
32
33static constexpr const char* PRIV_APP_SUBDIR = "priv-app/"; // sub-directory under ANDROID_DATA
34
35static constexpr const char* EPHEMERAL_APP_SUBDIR = "app-ephemeral/"; // sub-directory under
36                                                                      // ANDROID_DATA
37
38static constexpr const char* APP_LIB_SUBDIR = "app-lib/"; // sub-directory under ANDROID_DATA
39
40static constexpr const char* MEDIA_SUBDIR = "media/"; // sub-directory under ANDROID_DATA
41
42static constexpr const char* PROFILES_SUBDIR = "misc/profiles"; // sub-directory under ANDROID_DATA
43
44static constexpr const char* PRIVATE_APP_SUBDIR = "app-private/"; // sub-directory under
45                                                                  // ANDROID_DATA
46
47std::string android_app_dir;
48std::string android_app_ephemeral_dir;
49std::string android_app_lib_dir;
50std::string android_app_private_dir;
51std::string android_asec_dir;
52std::string android_data_dir;
53std::string android_media_dir;
54std::string android_mnt_expand_dir;
55std::string android_profiles_dir;
56std::string android_root_dir;
57
58std::vector<std::string> android_system_dirs;
59
60bool init_globals_from_data_and_root() {
61    const char* data_path = getenv("ANDROID_DATA");
62    if (data_path == nullptr) {
63        LOG(ERROR) << "Could not find ANDROID_DATA";
64        return false;
65    }
66    const char* root_path = getenv("ANDROID_ROOT");
67    if (root_path == nullptr) {
68        LOG(ERROR) << "Could not find ANDROID_ROOT";
69        return false;
70    }
71    return init_globals_from_data_and_root(data_path, root_path);
72}
73
74static std::string ensure_trailing_slash(const std::string& path) {
75    if (path.rfind('/') != path.size() - 1) {
76        return path + '/';
77    } else {
78        return path;
79    }
80}
81
82bool init_globals_from_data_and_root(const char* data, const char* root) {
83    // Get the android data directory.
84    android_data_dir = ensure_trailing_slash(data);
85
86    // Get the android root directory.
87    android_root_dir = ensure_trailing_slash(root);
88
89    // Get the android app directory.
90    android_app_dir = android_data_dir + APP_SUBDIR;
91
92    // Get the android protected app directory.
93    android_app_private_dir = android_data_dir + PRIVATE_APP_SUBDIR;
94
95    // Get the android ephemeral app directory.
96    android_app_ephemeral_dir = android_data_dir + EPHEMERAL_APP_SUBDIR;
97
98    // Get the android app native library directory.
99    android_app_lib_dir = android_data_dir + APP_LIB_SUBDIR;
100
101    // Get the sd-card ASEC mount point.
102    android_asec_dir = ensure_trailing_slash(getenv(ASEC_MOUNTPOINT_ENV_NAME));
103
104    // Get the android media directory.
105    android_media_dir = android_data_dir + MEDIA_SUBDIR;
106
107    // Get the android external app directory.
108    android_mnt_expand_dir = "/mnt/expand/";
109
110    // Get the android profiles directory.
111    android_profiles_dir = android_data_dir + PROFILES_SUBDIR;
112
113    // Take note of the system and vendor directories.
114    android_system_dirs.clear();
115    android_system_dirs.push_back(android_root_dir + APP_SUBDIR);
116    android_system_dirs.push_back(android_root_dir + PRIV_APP_SUBDIR);
117    android_system_dirs.push_back("/vendor/app/");
118    android_system_dirs.push_back("/oem/app/");
119
120    return true;
121}
122
123}  // namespace installd
124}  // namespace android
125