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