• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include <stdlib.h>
18 #include <string.h>
19 
20 #include <cutils/log.h>               // TODO: Move everything to base::logging.
21 
22 #include <globals.h>
23 #include <installd_constants.h>
24 #include <utils.h>
25 
26 #ifndef LOG_TAG
27 #define LOG_TAG "installd"
28 #endif
29 
30 namespace android {
31 namespace installd {
32 
33 static constexpr const char* APP_SUBDIR = "app/"; // sub-directory under ANDROID_DATA
34 
35 static constexpr const char* PRIV_APP_SUBDIR = "priv-app/"; // sub-directory under ANDROID_DATA
36 
37 static constexpr const char* EPHEMERAL_APP_SUBDIR = "app-ephemeral/"; // sub-directory under
38                                                                       // ANDROID_DATA
39 
40 static constexpr const char* APP_LIB_SUBDIR = "app-lib/"; // sub-directory under ANDROID_DATA
41 
42 static constexpr const char* MEDIA_SUBDIR = "media/"; // sub-directory under ANDROID_DATA
43 
44 static constexpr const char* PROFILES_SUBDIR = "misc/profiles"; // sub-directory under ANDROID_DATA
45 
46 static constexpr const char* PRIVATE_APP_SUBDIR = "app-private/"; // sub-directory under
47                                                                   // ANDROID_DATA
48 
49 /* Directory records that are used in execution of commands. */
50 dir_rec_t android_app_dir;
51 dir_rec_t android_app_ephemeral_dir;
52 dir_rec_t android_app_lib_dir;
53 dir_rec_t android_app_private_dir;
54 dir_rec_t android_asec_dir;
55 dir_rec_t android_data_dir;
56 dir_rec_t android_media_dir;
57 dir_rec_t android_mnt_expand_dir;
58 dir_rec_t android_profiles_dir;
59 
60 dir_rec_array_t android_system_dirs;
61 
62 /**
63  * Initialize all the global variables that are used elsewhere. Returns 0 upon
64  * success and -1 on error.
65  */
free_globals()66 void free_globals() {
67     size_t i;
68 
69     for (i = 0; i < android_system_dirs.count; i++) {
70         if (android_system_dirs.dirs[i].path != NULL) {
71             free(android_system_dirs.dirs[i].path);
72         }
73     }
74 
75     free(android_system_dirs.dirs);
76 }
77 
init_globals_from_data_and_root(const char * data,const char * root)78 bool init_globals_from_data_and_root(const char* data, const char* root) {
79     // Get the android data directory.
80     if (get_path_from_string(&android_data_dir, data) < 0) {
81         return false;
82     }
83 
84     // Get the android app directory.
85     if (copy_and_append(&android_app_dir, &android_data_dir, APP_SUBDIR) < 0) {
86         return false;
87     }
88 
89     // Get the android protected app directory.
90     if (copy_and_append(&android_app_private_dir, &android_data_dir, PRIVATE_APP_SUBDIR) < 0) {
91         return false;
92     }
93 
94     // Get the android ephemeral app directory.
95     if (copy_and_append(&android_app_ephemeral_dir, &android_data_dir, EPHEMERAL_APP_SUBDIR) < 0) {
96         return false;
97     }
98 
99     // Get the android app native library directory.
100     if (copy_and_append(&android_app_lib_dir, &android_data_dir, APP_LIB_SUBDIR) < 0) {
101         return false;
102     }
103 
104     // Get the sd-card ASEC mount point.
105     if (get_path_from_env(&android_asec_dir, ASEC_MOUNTPOINT_ENV_NAME) < 0) {
106         return false;
107     }
108 
109     // Get the android media directory.
110     if (copy_and_append(&android_media_dir, &android_data_dir, MEDIA_SUBDIR) < 0) {
111         return false;
112     }
113 
114     // Get the android external app directory.
115     if (get_path_from_string(&android_mnt_expand_dir, "/mnt/expand/") < 0) {
116         return false;
117     }
118 
119     // Get the android profiles directory.
120     if (copy_and_append(&android_profiles_dir, &android_data_dir, PROFILES_SUBDIR) < 0) {
121         return false;
122     }
123 
124     // Take note of the system and vendor directories.
125     android_system_dirs.count = 4;
126 
127     android_system_dirs.dirs = (dir_rec_t*) calloc(android_system_dirs.count, sizeof(dir_rec_t));
128     if (android_system_dirs.dirs == NULL) {
129         ALOGE("Couldn't allocate array for dirs; aborting\n");
130         return false;
131     }
132 
133     dir_rec_t android_root_dir;
134     if (get_path_from_string(&android_root_dir, root) < 0) {
135         return false;
136     }
137 
138     android_system_dirs.dirs[0].path = build_string2(android_root_dir.path, APP_SUBDIR);
139     android_system_dirs.dirs[0].len = strlen(android_system_dirs.dirs[0].path);
140 
141     android_system_dirs.dirs[1].path = build_string2(android_root_dir.path, PRIV_APP_SUBDIR);
142     android_system_dirs.dirs[1].len = strlen(android_system_dirs.dirs[1].path);
143 
144     android_system_dirs.dirs[2].path = strdup("/vendor/app/");
145     android_system_dirs.dirs[2].len = strlen(android_system_dirs.dirs[2].path);
146 
147     android_system_dirs.dirs[3].path = strdup("/oem/app/");
148     android_system_dirs.dirs[3].len = strlen(android_system_dirs.dirs[3].path);
149 
150     return true;
151 }
152 
153 }  // namespace installd
154 }  // namespace android
155