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