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