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 /* Directory records that are used in execution of commands. */
34 dir_rec_t android_app_dir;
35 dir_rec_t android_app_ephemeral_dir;
36 dir_rec_t android_app_lib_dir;
37 dir_rec_t android_app_private_dir;
38 dir_rec_t android_asec_dir;
39 dir_rec_t android_data_dir;
40 dir_rec_t android_media_dir;
41 dir_rec_t android_mnt_expand_dir;
42 dir_rec_t android_profiles_dir;
43 
44 dir_rec_array_t android_system_dirs;
45 
46 /**
47  * Initialize all the global variables that are used elsewhere. Returns 0 upon
48  * success and -1 on error.
49  */
free_globals()50 void free_globals() {
51     size_t i;
52 
53     for (i = 0; i < android_system_dirs.count; i++) {
54         if (android_system_dirs.dirs[i].path != NULL) {
55             free(android_system_dirs.dirs[i].path);
56         }
57     }
58 
59     free(android_system_dirs.dirs);
60 }
61 
init_globals_from_data_and_root(const char * data,const char * root)62 bool init_globals_from_data_and_root(const char* data, const char* root) {
63     // Get the android data directory.
64     if (get_path_from_string(&android_data_dir, data) < 0) {
65         return false;
66     }
67 
68     // Get the android app directory.
69     if (copy_and_append(&android_app_dir, &android_data_dir, APP_SUBDIR) < 0) {
70         return false;
71     }
72 
73     // Get the android protected app directory.
74     if (copy_and_append(&android_app_private_dir, &android_data_dir, PRIVATE_APP_SUBDIR) < 0) {
75         return false;
76     }
77 
78     // Get the android ephemeral app directory.
79     if (copy_and_append(&android_app_ephemeral_dir, &android_data_dir, EPHEMERAL_APP_SUBDIR) < 0) {
80         return -1;
81     }
82 
83     // Get the android app native library directory.
84     if (copy_and_append(&android_app_lib_dir, &android_data_dir, APP_LIB_SUBDIR) < 0) {
85         return false;
86     }
87 
88     // Get the sd-card ASEC mount point.
89     if (get_path_from_env(&android_asec_dir, "ASEC_MOUNTPOINT") < 0) {
90         return false;
91     }
92 
93     // Get the android media directory.
94     if (copy_and_append(&android_media_dir, &android_data_dir, MEDIA_SUBDIR) < 0) {
95         return false;
96     }
97 
98     // Get the android external app directory.
99     if (get_path_from_string(&android_mnt_expand_dir, "/mnt/expand/") < 0) {
100         return false;
101     }
102 
103     // Get the android profiles directory.
104     if (copy_and_append(&android_profiles_dir, &android_data_dir, PROFILES_SUBDIR) < 0) {
105         return false;
106     }
107 
108     // Take note of the system and vendor directories.
109     android_system_dirs.count = 4;
110 
111     android_system_dirs.dirs = (dir_rec_t*) calloc(android_system_dirs.count, sizeof(dir_rec_t));
112     if (android_system_dirs.dirs == NULL) {
113         ALOGE("Couldn't allocate array for dirs; aborting\n");
114         return false;
115     }
116 
117     dir_rec_t android_root_dir;
118     if (get_path_from_string(&android_root_dir, root) < 0) {
119         return false;
120     }
121 
122     android_system_dirs.dirs[0].path = build_string2(android_root_dir.path, APP_SUBDIR);
123     android_system_dirs.dirs[0].len = strlen(android_system_dirs.dirs[0].path);
124 
125     android_system_dirs.dirs[1].path = build_string2(android_root_dir.path, PRIV_APP_SUBDIR);
126     android_system_dirs.dirs[1].len = strlen(android_system_dirs.dirs[1].path);
127 
128     android_system_dirs.dirs[2].path = strdup("/vendor/app/");
129     android_system_dirs.dirs[2].len = strlen(android_system_dirs.dirs[2].path);
130 
131     android_system_dirs.dirs[3].path = strdup("/oem/app/");
132     android_system_dirs.dirs[3].len = strlen(android_system_dirs.dirs[3].path);
133 
134     return true;
135 }
136 
137 }  // namespace installd
138 }  // namespace android
139