1 /*
2 **
3 ** Copyright 2008, The Android Open Source Project
4 **
5 ** Licensed under the Apache License, Version 2.0 (the "License");
6 ** you may not use this file except in compliance with the License.
7 ** You may obtain a copy of the License at
8 **
9 **     http://www.apache.org/licenses/LICENSE-2.0
10 **
11 ** Unless required by applicable law or agreed to in writing, software
12 ** distributed under the License is distributed on an "AS IS" BASIS,
13 ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 ** See the License for the specific language governing permissions and
15 ** limitations under the License.
16 */
17 
18 #ifndef UTILS_H_
19 #define UTILS_H_
20 
21 #include <string>
22 #include <vector>
23 
24 #include <dirent.h>
25 #include <inttypes.h>
26 #include <unistd.h>
27 #include <utime.h>
28 
29 #include <cutils/multiuser.h>
30 
31 #include <installd_constants.h>
32 
33 #define MEASURE_DEBUG 0
34 #define FIXUP_DEBUG 0
35 
36 #define BYPASS_QUOTA 0
37 #define BYPASS_SDCARDFS 0
38 
39 namespace android {
40 namespace installd {
41 
42 constexpr const char* kXattrInodeCache = "user.inode_cache";
43 constexpr const char* kXattrInodeCodeCache = "user.inode_code_cache";
44 constexpr const char* kXattrCacheGroup = "user.cache_group";
45 constexpr const char* kXattrCacheTombstone = "user.cache_tombstone";
46 
47 std::string create_data_path(const char* volume_uuid);
48 
49 std::string create_data_app_path(const char* volume_uuid);
50 
51 std::string create_data_user_ce_path(const char* volume_uuid, userid_t userid);
52 std::string create_data_user_de_path(const char* volume_uuid, userid_t userid);
53 
54 std::string create_data_user_ce_package_path(const char* volume_uuid,
55         userid_t user, const char* package_name);
56 std::string create_data_user_ce_package_path(const char* volume_uuid,
57         userid_t user, const char* package_name, ino_t ce_data_inode);
58 std::string create_data_user_de_package_path(const char* volume_uuid,
59         userid_t user, const char* package_name);
60 std::string create_data_user_ce_package_path_as_user_link(
61         const char* volume_uuid, userid_t userid, const char* package_name);
62 
63 std::string create_data_misc_ce_rollback_base_path(const char* volume_uuid, userid_t user);
64 std::string create_data_misc_de_rollback_base_path(const char* volume_uuid, userid_t user);
65 std::string create_data_misc_ce_rollback_path(const char* volume_uuid, userid_t user,
66         int32_t snapshot_id);
67 std::string create_data_misc_de_rollback_path(const char* volume_uuid, userid_t user,
68         int32_t snapshot_id);
69 std::string create_data_misc_ce_rollback_package_path(const char* volume_uuid,
70         userid_t user, int32_t snapshot_id, const char* package_name);
71 std::string create_data_misc_ce_rollback_package_path(const char* volume_uuid,
72         userid_t user, int32_t snapshot_id, const char* package_name, ino_t ce_rollback_inode);
73 std::string create_data_misc_de_rollback_package_path(const char* volume_uuid,
74         userid_t user, int32_t snapshot_id, const char* package_name);
75 
76 std::string create_data_media_path(const char* volume_uuid, userid_t userid);
77 std::string create_data_media_package_path(const char* volume_uuid, userid_t userid,
78         const char* data_type, const char* package_name);
79 
80 std::string create_data_misc_legacy_path(userid_t userid);
81 
82 std::string create_data_dalvik_cache_path();
83 
84 std::string create_system_user_ce_path(userid_t userId);
85 
86 std::string create_system_user_ce_package_path(userid_t userId, const char* package_name);
87 
88 std::string create_primary_cur_profile_dir_path(userid_t userid);
89 std::string create_primary_current_profile_package_dir_path(
90         userid_t user, const std::string& package_name);
91 
92 std::string create_primary_ref_profile_dir_path();
93 std::string create_primary_reference_profile_package_dir_path(const std::string& package_name);
94 
95 std::string create_current_profile_path(
96         userid_t user,
97         const std::string& package_name,
98         const std::string& location,
99         bool is_secondary_dex);
100 std::string create_reference_profile_path(
101         const std::string& package_name,
102         const std::string& location,
103         bool is_secondary_dex);
104 std::string create_snapshot_profile_path(
105         const std::string& package,
106         const std::string& profile_name);
107 
108 std::vector<userid_t> get_known_users(const char* volume_uuid);
109 
110 int calculate_tree_size(const std::string& path, int64_t* size,
111         int32_t include_gid = -1, int32_t exclude_gid = -1, bool exclude_apps = false);
112 
113 int create_user_config_path(char path[PKG_PATH_MAX], userid_t userid);
114 
115 bool is_valid_filename(const std::string& name);
116 bool is_valid_package_name(const std::string& packageName);
117 
118 int create_dir_if_needed(const std::string& pathname, mode_t mode);
119 
120 int delete_dir_contents(const std::string& pathname, bool ignore_if_missing = false);
121 int delete_dir_contents_and_dir(const std::string& pathname, bool ignore_if_missing = false);
122 
123 int delete_dir_contents(const char *pathname,
124                         int also_delete_dir,
125                         int (*exclusion_predicate)(const char *name, const int is_dir),
126                         bool ignore_if_missing = false);
127 
128 int delete_dir_contents_fd(int dfd, const char *name);
129 
130 int rm_package_dir(const std::string& package_dir);
131 
132 int copy_dir_files(const char *srcname, const char *dstname, uid_t owner, gid_t group);
133 
134 int64_t data_disk_free(const std::string& data_path);
135 
136 int get_path_inode(const std::string& path, ino_t *inode);
137 
138 int write_path_inode(const std::string& parent, const char* name, const char* inode_xattr);
139 std::string read_path_inode(const std::string& parent, const char* name, const char* inode_xattr);
140 void remove_path_xattr(const std::string& path, const char* inode_xattr);
141 
142 int validate_system_app_path(const char* path);
143 bool validate_secondary_dex_path(const std::string& pkgname, const std::string& dex_path,
144         const char* volume_uuid, int uid, int storage_flag);
145 
146 int validate_apk_path(const char *path);
147 int validate_apk_path_subdirs(const char *path);
148 
149 int ensure_config_user_dirs(userid_t userid);
150 
151 int wait_child(pid_t pid);
152 
153 int prepare_app_cache_dir(const std::string& parent, const char* name, mode_t target_mode,
154         uid_t uid, gid_t gid);
155 
156 bool supports_sdcardfs();
157 int64_t get_occupied_app_space_external(const std::string& uuid, int32_t userId, int32_t appId);
158 int64_t get_occupied_app_cache_space_external(const std::string& uuid, int32_t userId, int32_t appId);
159 
160 // Collect all non empty profiles from the global profile directory and
161 // put then into profile_paths. The profiles are identified based on PROFILE_EXT extension.
162 // If a subdirectory or profile file cannot be opened the method logs a warning and moves on.
163 // It returns true if there were no errors at all, and false otherwise.
164 bool collect_profiles(std::vector<std::string>* profiles_paths);
165 
166 void drop_capabilities(uid_t uid);
167 
168 }  // namespace installd
169 }  // namespace android
170 
171 #endif  // UTILS_H_
172