Andreas Gampe | 02d0de5 | 2015-11-11 20:43:16 -0800 | [diff] [blame] | 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 | |
Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 33 | #define MEASURE_DEBUG 0 |
Jeff Sharkey | e12d596 | 2017-04-03 16:41:02 -0600 | [diff] [blame] | 34 | #define FIXUP_DEBUG 0 |
Samiul Islam | b9e96bf | 2022-02-03 12:45:51 +0000 | [diff] [blame^] | 35 | #define SDK_DEBUG 1 |
Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 36 | |
Jeff Sharkey | d712f0c | 2017-05-03 11:36:42 -0600 | [diff] [blame] | 37 | #define BYPASS_QUOTA 0 |
| 38 | #define BYPASS_SDCARDFS 0 |
| 39 | |
Andreas Gampe | 02d0de5 | 2015-11-11 20:43:16 -0800 | [diff] [blame] | 40 | namespace android { |
| 41 | namespace installd { |
| 42 | |
Jeff Sharkey | 9a998f4 | 2016-07-14 18:16:22 -0600 | [diff] [blame] | 43 | constexpr const char* kXattrInodeCache = "user.inode_cache"; |
| 44 | constexpr const char* kXattrInodeCodeCache = "user.inode_code_cache"; |
Jeff Sharkey | ed909ae | 2017-03-22 21:27:40 -0600 | [diff] [blame] | 45 | constexpr const char* kXattrCacheGroup = "user.cache_group"; |
Jeff Sharkey | 871a8f2 | 2017-02-21 18:30:28 -0700 | [diff] [blame] | 46 | constexpr const char* kXattrCacheTombstone = "user.cache_tombstone"; |
Jeff Sharkey | 9a998f4 | 2016-07-14 18:16:22 -0600 | [diff] [blame] | 47 | |
Andreas Gampe | 02d0de5 | 2015-11-11 20:43:16 -0800 | [diff] [blame] | 48 | std::string create_data_path(const char* volume_uuid); |
| 49 | |
| 50 | std::string create_data_app_path(const char* volume_uuid); |
Andreas Gampe | 02d0de5 | 2015-11-11 20:43:16 -0800 | [diff] [blame] | 51 | |
Jeff Sharkey | 2f720f7 | 2016-04-10 20:51:40 -0600 | [diff] [blame] | 52 | std::string create_data_user_ce_path(const char* volume_uuid, userid_t userid); |
Andreas Gampe | 02d0de5 | 2015-11-11 20:43:16 -0800 | [diff] [blame] | 53 | std::string create_data_user_de_path(const char* volume_uuid, userid_t userid); |
| 54 | |
Jeff Sharkey | 2f720f7 | 2016-04-10 20:51:40 -0600 | [diff] [blame] | 55 | std::string create_data_user_ce_package_path(const char* volume_uuid, |
Andreas Gampe | 02d0de5 | 2015-11-11 20:43:16 -0800 | [diff] [blame] | 56 | userid_t user, const char* package_name); |
Jeff Sharkey | 2f720f7 | 2016-04-10 20:51:40 -0600 | [diff] [blame] | 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); |
Andreas Gampe | 02d0de5 | 2015-11-11 20:43:16 -0800 | [diff] [blame] | 59 | std::string create_data_user_de_package_path(const char* volume_uuid, |
| 60 | userid_t user, const char* package_name); |
Calin Juravle | 7d76546 | 2017-09-04 15:57:10 -0700 | [diff] [blame] | 61 | std::string create_data_user_ce_package_path_as_user_link( |
| 62 | const char* volume_uuid, userid_t userid, const char* package_name); |
Andreas Gampe | 02d0de5 | 2015-11-11 20:43:16 -0800 | [diff] [blame] | 63 | |
Nikita Ioffe | 9c3e8f5 | 2022-02-21 19:02:05 +0000 | [diff] [blame] | 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, |
Samiul Islam | b911bc6 | 2022-01-14 16:24:48 +0000 | [diff] [blame] | 67 | userid_t userid, const char* package_name); |
Nikita Ioffe | 9c3e8f5 | 2022-02-21 19:02:05 +0000 | [diff] [blame] | 68 | std::string create_data_misc_sdk_sandbox_shared_path(const char* volume_uuid, bool isCeData, |
| 69 | userid_t userid, const char* package_name); |
Samiul Islam | b9e96bf | 2022-02-03 12:45:51 +0000 | [diff] [blame^] | 70 | std::string create_data_misc_sdk_sandbox_sdk_path(const char* volume_uuid, bool isCeData, |
| 71 | userid_t userid, const char* package_name, |
| 72 | const char* sdk_name, const char* randomSuffix); |
Samiul Islam | b911bc6 | 2022-01-14 16:24:48 +0000 | [diff] [blame] | 73 | |
Nikita Ioffe | b68f0d1 | 2019-02-04 11:06:37 +0000 | [diff] [blame] | 74 | std::string create_data_misc_ce_rollback_base_path(const char* volume_uuid, userid_t user); |
| 75 | std::string create_data_misc_de_rollback_base_path(const char* volume_uuid, userid_t user); |
| 76 | std::string create_data_misc_ce_rollback_path(const char* volume_uuid, userid_t user, |
| 77 | int32_t snapshot_id); |
| 78 | std::string create_data_misc_de_rollback_path(const char* volume_uuid, userid_t user, |
| 79 | int32_t snapshot_id); |
Narayan Kamath | e63ca7d | 2019-01-14 15:21:52 +0000 | [diff] [blame] | 80 | std::string create_data_misc_ce_rollback_package_path(const char* volume_uuid, |
Nikita Ioffe | b68f0d1 | 2019-02-04 11:06:37 +0000 | [diff] [blame] | 81 | userid_t user, int32_t snapshot_id, const char* package_name); |
Nikita Ioffe | 77be2ed | 2019-01-25 13:54:43 +0000 | [diff] [blame] | 82 | std::string create_data_misc_ce_rollback_package_path(const char* volume_uuid, |
Nikita Ioffe | b68f0d1 | 2019-02-04 11:06:37 +0000 | [diff] [blame] | 83 | userid_t user, int32_t snapshot_id, const char* package_name, ino_t ce_rollback_inode); |
Narayan Kamath | e63ca7d | 2019-01-14 15:21:52 +0000 | [diff] [blame] | 84 | std::string create_data_misc_de_rollback_package_path(const char* volume_uuid, |
Nikita Ioffe | b68f0d1 | 2019-02-04 11:06:37 +0000 | [diff] [blame] | 85 | userid_t user, int32_t snapshot_id, const char* package_name); |
Narayan Kamath | e63ca7d | 2019-01-14 15:21:52 +0000 | [diff] [blame] | 86 | |
Andreas Gampe | 02d0de5 | 2015-11-11 20:43:16 -0800 | [diff] [blame] | 87 | std::string create_data_media_path(const char* volume_uuid, userid_t userid); |
Jeff Sharkey | 3dfae0c | 2016-12-12 17:32:56 -0700 | [diff] [blame] | 88 | std::string create_data_media_package_path(const char* volume_uuid, userid_t userid, |
| 89 | const char* data_type, const char* package_name); |
Andreas Gampe | 02d0de5 | 2015-11-11 20:43:16 -0800 | [diff] [blame] | 90 | |
Jeff Sharkey | 379a12b | 2016-04-14 20:45:06 -0600 | [diff] [blame] | 91 | std::string create_data_misc_legacy_path(userid_t userid); |
| 92 | |
Jeff Sharkey | 3dfae0c | 2016-12-12 17:32:56 -0700 | [diff] [blame] | 93 | std::string create_data_dalvik_cache_path(); |
Jeff Sharkey | 3dfae0c | 2016-12-12 17:32:56 -0700 | [diff] [blame] | 94 | |
Felka Chang | 2a0a246 | 2019-11-20 14:20:40 +0800 | [diff] [blame] | 95 | std::string create_system_user_ce_path(userid_t userId); |
| 96 | |
| 97 | std::string create_system_user_ce_package_path(userid_t userId, const char* package_name); |
| 98 | |
Calin Juravle | 114f081 | 2017-03-08 19:05:07 -0800 | [diff] [blame] | 99 | std::string create_primary_cur_profile_dir_path(userid_t userid); |
| 100 | std::string create_primary_current_profile_package_dir_path( |
| 101 | userid_t user, const std::string& package_name); |
| 102 | |
| 103 | std::string create_primary_ref_profile_dir_path(); |
| 104 | std::string create_primary_reference_profile_package_dir_path(const std::string& package_name); |
| 105 | |
| 106 | std::string create_current_profile_path( |
Calin Juravle | cfcd6aa | 2018-01-18 20:23:17 -0800 | [diff] [blame] | 107 | userid_t user, |
| 108 | const std::string& package_name, |
| 109 | const std::string& location, |
| 110 | bool is_secondary_dex); |
Calin Juravle | 114f081 | 2017-03-08 19:05:07 -0800 | [diff] [blame] | 111 | std::string create_reference_profile_path( |
Calin Juravle | cfcd6aa | 2018-01-18 20:23:17 -0800 | [diff] [blame] | 112 | const std::string& package_name, |
| 113 | const std::string& location, |
| 114 | bool is_secondary_dex); |
| 115 | std::string create_snapshot_profile_path( |
| 116 | const std::string& package, |
| 117 | const std::string& profile_name); |
Jeff Sharkey | 90aff26 | 2016-12-12 14:28:24 -0700 | [diff] [blame] | 118 | |
Andreas Gampe | 02d0de5 | 2015-11-11 20:43:16 -0800 | [diff] [blame] | 119 | std::vector<userid_t> get_known_users(const char* volume_uuid); |
| 120 | |
Jeff Sharkey | 3dfae0c | 2016-12-12 17:32:56 -0700 | [diff] [blame] | 121 | int calculate_tree_size(const std::string& path, int64_t* size, |
Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 122 | int32_t include_gid = -1, int32_t exclude_gid = -1, bool exclude_apps = false); |
Jeff Sharkey | 3dfae0c | 2016-12-12 17:32:56 -0700 | [diff] [blame] | 123 | |
Andreas Gampe | 02d0de5 | 2015-11-11 20:43:16 -0800 | [diff] [blame] | 124 | int create_user_config_path(char path[PKG_PATH_MAX], userid_t userid); |
| 125 | |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 126 | bool is_valid_filename(const std::string& name); |
| 127 | bool is_valid_package_name(const std::string& packageName); |
Andreas Gampe | 02d0de5 | 2015-11-11 20:43:16 -0800 | [diff] [blame] | 128 | |
Nikita Ioffe | b68f0d1 | 2019-02-04 11:06:37 +0000 | [diff] [blame] | 129 | int create_dir_if_needed(const std::string& pathname, mode_t mode); |
| 130 | |
Calin Juravle | b06f98a | 2016-03-28 15:11:01 +0100 | [diff] [blame] | 131 | int delete_dir_contents(const std::string& pathname, bool ignore_if_missing = false); |
| 132 | int delete_dir_contents_and_dir(const std::string& pathname, bool ignore_if_missing = false); |
Andreas Gampe | 02d0de5 | 2015-11-11 20:43:16 -0800 | [diff] [blame] | 133 | |
Alex Buynytskyy | 4ab5d53 | 2022-02-17 21:20:10 +0000 | [diff] [blame] | 134 | bool is_renamed_deleted_dir(const std::string& path); |
Alex Buynytskyy | 5cb2526 | 2022-02-15 08:59:36 -0800 | [diff] [blame] | 135 | int rename_delete_dir_contents_and_dir(const std::string& pathname, bool ignore_if_missing = true); |
Alex Buynytskyy | 58a7309 | 2022-02-14 13:15:53 -0800 | [diff] [blame] | 136 | |
Samiul Islam | b9e96bf | 2022-02-03 12:45:51 +0000 | [diff] [blame^] | 137 | int foreach_subdir(const std::string& pathname, std::function<void(const std::string&)> fn); |
| 138 | |
Alex Buynytskyy | 58a7309 | 2022-02-14 13:15:53 -0800 | [diff] [blame] | 139 | void cleanup_invalid_package_dirs_under_path(const std::string& pathname); |
Alex Buynytskyy | 038a19b | 2022-02-09 19:51:52 -0800 | [diff] [blame] | 140 | |
Andreas Gampe | 02d0de5 | 2015-11-11 20:43:16 -0800 | [diff] [blame] | 141 | int delete_dir_contents(const char *pathname, |
| 142 | int also_delete_dir, |
Calin Juravle | b06f98a | 2016-03-28 15:11:01 +0100 | [diff] [blame] | 143 | int (*exclusion_predicate)(const char *name, const int is_dir), |
| 144 | bool ignore_if_missing = false); |
Andreas Gampe | 02d0de5 | 2015-11-11 20:43:16 -0800 | [diff] [blame] | 145 | |
| 146 | int delete_dir_contents_fd(int dfd, const char *name); |
| 147 | |
Oleksandr Peletskyi | d2be632 | 2019-01-24 17:59:45 +0100 | [diff] [blame] | 148 | int rm_package_dir(const std::string& package_dir); |
| 149 | |
Andreas Gampe | 02d0de5 | 2015-11-11 20:43:16 -0800 | [diff] [blame] | 150 | int copy_dir_files(const char *srcname, const char *dstname, uid_t owner, gid_t group); |
| 151 | |
Andreas Gampe | 02d0de5 | 2015-11-11 20:43:16 -0800 | [diff] [blame] | 152 | int64_t data_disk_free(const std::string& data_path); |
| 153 | |
Jeff Sharkey | 9a998f4 | 2016-07-14 18:16:22 -0600 | [diff] [blame] | 154 | int get_path_inode(const std::string& path, ino_t *inode); |
| 155 | |
| 156 | int write_path_inode(const std::string& parent, const char* name, const char* inode_xattr); |
| 157 | std::string read_path_inode(const std::string& parent, const char* name, const char* inode_xattr); |
Ryuki Nakamura | c7342f8 | 2017-09-30 11:57:00 +0900 | [diff] [blame] | 158 | void remove_path_xattr(const std::string& path, const char* inode_xattr); |
Jeff Sharkey | 9a998f4 | 2016-07-14 18:16:22 -0600 | [diff] [blame] | 159 | |
Andreas Gampe | 02d0de5 | 2015-11-11 20:43:16 -0800 | [diff] [blame] | 160 | int validate_system_app_path(const char* path); |
Calin Juravle | 114f081 | 2017-03-08 19:05:07 -0800 | [diff] [blame] | 161 | bool validate_secondary_dex_path(const std::string& pkgname, const std::string& dex_path, |
Calin Juravle | 7d76546 | 2017-09-04 15:57:10 -0700 | [diff] [blame] | 162 | const char* volume_uuid, int uid, int storage_flag); |
Andreas Gampe | 02d0de5 | 2015-11-11 20:43:16 -0800 | [diff] [blame] | 163 | |
Andreas Gampe | 02d0de5 | 2015-11-11 20:43:16 -0800 | [diff] [blame] | 164 | int validate_apk_path(const char *path); |
| 165 | int validate_apk_path_subdirs(const char *path); |
| 166 | |
Andreas Gampe | 02d0de5 | 2015-11-11 20:43:16 -0800 | [diff] [blame] | 167 | int ensure_config_user_dirs(userid_t userid); |
| 168 | |
Jiakai Zhang | 0a7603c | 2022-02-21 19:38:14 +0000 | [diff] [blame] | 169 | // Waits for a child process, or kills it if it times out. Returns the exit code. |
| 170 | int wait_child_with_timeout(pid_t pid, int timeout_ms); |
Andreas Gampe | 02d0de5 | 2015-11-11 20:43:16 -0800 | [diff] [blame] | 171 | |
Calin Juravle | 42451c0 | 2017-01-17 14:43:25 -0800 | [diff] [blame] | 172 | int prepare_app_cache_dir(const std::string& parent, const char* name, mode_t target_mode, |
| 173 | uid_t uid, gid_t gid); |
| 174 | |
Martijn Coenen | 771cc34 | 2020-02-19 23:26:56 +0100 | [diff] [blame] | 175 | bool supports_sdcardfs(); |
| 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 | |
Calin Juravle | e61189e | 2018-01-23 19:54:11 -0800 | [diff] [blame] | 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 | |
Eric Holk | 0ebbe0f | 2019-01-09 18:17:27 -0800 | [diff] [blame] | 185 | void drop_capabilities(uid_t uid); |
| 186 | |
Jiakai Zhang | 0a7603c | 2022-02-21 19:38:14 +0000 | [diff] [blame] | 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 | |
Andreas Gampe | 02d0de5 | 2015-11-11 20:43:16 -0800 | [diff] [blame] | 191 | } // namespace installd |
| 192 | } // namespace android |
| 193 | |
| 194 | #endif // UTILS_H_ |