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