• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #define SDK_DEBUG 1
36 
37 #define BYPASS_QUOTA 0
38 #define BYPASS_SDCARDFS 0
39 
40 namespace android {
41 namespace installd {
42 
43 constexpr const char* kXattrInodeCache = "user.inode_cache";
44 constexpr const char* kXattrInodeCodeCache = "user.inode_code_cache";
45 constexpr const char* kXattrCacheGroup = "user.cache_group";
46 constexpr const char* kXattrCacheTombstone = "user.cache_tombstone";
47 
48 std::string create_data_path(const char* volume_uuid);
49 
50 std::string create_data_app_path(const char* volume_uuid);
51 
52 std::string create_data_user_ce_path(const char* volume_uuid, userid_t userid);
53 std::string create_data_user_de_path(const char* volume_uuid, userid_t userid);
54 
55 std::string create_data_user_ce_package_path(const char* volume_uuid,
56         userid_t user, const char* package_name);
57 std::string create_data_user_ce_package_path(const char* volume_uuid,
58         userid_t user, const char* package_name, ino_t ce_data_inode);
59 std::string create_data_user_de_package_path(const char* volume_uuid,
60         userid_t user, const char* package_name);
61 std::string create_data_user_ce_package_path_as_user_link(
62         const char* volume_uuid, userid_t userid, const char* package_name);
63 
64 std::string create_data_misc_sdk_sandbox_path(const char* volume_uuid, bool isCeData,
65                                               userid_t userid);
66 std::string create_data_misc_sdk_sandbox_package_path(const char* volume_uuid, bool isCeData,
67                                                       userid_t userid, const char* package_name);
68 std::string create_data_misc_sdk_sandbox_sdk_path(const char* volume_uuid, bool isCeData,
69                                                   userid_t userid, const char* package_name,
70                                                   const char* sub_dir_name);
71 
72 std::string create_data_misc_ce_rollback_base_path(const char* volume_uuid, userid_t user);
73 std::string create_data_misc_de_rollback_base_path(const char* volume_uuid, userid_t user);
74 std::string create_data_misc_ce_rollback_path(const char* volume_uuid, userid_t user,
75         int32_t snapshot_id);
76 std::string create_data_misc_de_rollback_path(const char* volume_uuid, userid_t user,
77         int32_t snapshot_id);
78 std::string create_data_misc_ce_rollback_package_path(const char* volume_uuid,
79         userid_t user, int32_t snapshot_id, const char* package_name);
80 std::string create_data_misc_ce_rollback_package_path(const char* volume_uuid,
81         userid_t user, int32_t snapshot_id, const char* package_name, ino_t ce_rollback_inode);
82 std::string create_data_misc_de_rollback_package_path(const char* volume_uuid,
83         userid_t user, int32_t snapshot_id, const char* package_name);
84 
85 std::string create_data_media_path(const char* volume_uuid, userid_t userid);
86 std::string create_data_media_package_path(const char* volume_uuid, userid_t userid,
87         const char* data_type, const char* package_name);
88 
89 std::string create_data_misc_legacy_path(userid_t userid);
90 
91 std::string create_data_dalvik_cache_path();
92 
93 std::string create_system_user_ce_path(userid_t userId);
94 
95 std::string create_system_user_ce_package_path(userid_t userId, const char* package_name);
96 
97 std::string create_primary_cur_profile_dir_path(userid_t userid);
98 std::string create_primary_current_profile_package_dir_path(
99         userid_t user, const std::string& package_name);
100 
101 std::string create_primary_ref_profile_dir_path();
102 std::string create_primary_reference_profile_package_dir_path(const std::string& package_name);
103 
104 std::string create_current_profile_path(
105         userid_t user,
106         const std::string& package_name,
107         const std::string& location,
108         bool is_secondary_dex);
109 std::string create_reference_profile_path(
110         const std::string& package_name,
111         const std::string& location,
112         bool is_secondary_dex);
113 std::string create_snapshot_profile_path(
114         const std::string& package,
115         const std::string& profile_name);
116 
117 std::vector<userid_t> get_known_users(const char* volume_uuid);
118 
119 int calculate_tree_size(const std::string& path, int64_t* size,
120         int32_t include_gid = -1, int32_t exclude_gid = -1, bool exclude_apps = false);
121 
122 int create_user_config_path(char path[PKG_PATH_MAX], userid_t userid);
123 
124 bool is_valid_filename(const std::string& name);
125 bool is_valid_package_name(const std::string& packageName);
126 
127 int create_dir_if_needed(const std::string& pathname, mode_t mode);
128 
129 int delete_dir_contents(const std::string& pathname, bool ignore_if_missing = false);
130 int delete_dir_contents_and_dir(const std::string& pathname, bool ignore_if_missing = false);
131 
132 bool is_renamed_deleted_dir(const std::string& path);
133 int rename_delete_dir_contents_and_dir(const std::string& pathname, bool ignore_if_missing = true);
134 
135 int foreach_subdir(const std::string& pathname, std::function<void(const std::string&)> fn);
136 
137 void cleanup_invalid_package_dirs_under_path(const std::string& pathname);
138 
139 int delete_dir_contents(const char *pathname,
140                         int also_delete_dir,
141                         int (*exclusion_predicate)(const char *name, const int is_dir),
142                         bool ignore_if_missing = false);
143 
144 int delete_dir_contents_fd(int dfd, const char *name);
145 
146 int rm_package_dir(const std::string& package_dir);
147 
148 int copy_dir_files(const char *srcname, const char *dstname, uid_t owner, gid_t group);
149 
150 int64_t data_disk_free(const std::string& data_path);
151 
152 int get_path_inode(const std::string& path, ino_t *inode);
153 
154 int write_path_inode(const std::string& parent, const char* name, const char* inode_xattr);
155 std::string read_path_inode(const std::string& parent, const char* name, const char* inode_xattr);
156 void remove_path_xattr(const std::string& path, const char* inode_xattr);
157 
158 int validate_system_app_path(const char* path);
159 bool validate_secondary_dex_path(const std::string& pkgname, const std::string& dex_path,
160         const char* volume_uuid, int uid, int storage_flag);
161 
162 int validate_apk_path(const char *path);
163 int validate_apk_path_subdirs(const char *path);
164 
165 int ensure_config_user_dirs(userid_t userid);
166 
167 // Waits for a child process, or kills it if it times out. Returns the exit code.
168 int wait_child_with_timeout(pid_t pid, int timeout_ms);
169 
170 int prepare_app_cache_dir(const std::string& parent, const char* name, mode_t target_mode,
171         uid_t uid, gid_t gid);
172 
173 bool supports_sdcardfs();
174 long get_project_id(uid_t uid, long start_project_id_range);
175 int set_quota_project_id(const std::string& path, long project_id, bool set_inherit);
176 int64_t get_occupied_app_space_external(const std::string& uuid, int32_t userId, int32_t appId);
177 int64_t get_occupied_app_cache_space_external(const std::string& uuid, int32_t userId, int32_t appId);
178 
179 // Collect all non empty profiles from the global profile directory and
180 // put then into profile_paths. The profiles are identified based on PROFILE_EXT extension.
181 // If a subdirectory or profile file cannot be opened the method logs a warning and moves on.
182 // It returns true if there were no errors at all, and false otherwise.
183 bool collect_profiles(std::vector<std::string>* profiles_paths);
184 
185 void drop_capabilities(uid_t uid);
186 
187 // Removes a file specified by a file descriptor. Returns true on success. Reports the file path to
188 // `path` if present.
189 bool remove_file_at_fd(int fd, /*out*/ std::string* path = nullptr);
190 
191 }  // namespace installd
192 }  // namespace android
193 
194 #endif  // UTILS_H_
195