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 |
Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 35 | |
Andreas Gampe | 02d0de5 | 2015-11-11 20:43:16 -0800 | [diff] [blame] | 36 | namespace android { |
| 37 | namespace installd { |
| 38 | |
| 39 | struct dir_rec_t; |
| 40 | |
Jeff Sharkey | 9a998f4 | 2016-07-14 18:16:22 -0600 | [diff] [blame] | 41 | constexpr const char* kXattrInodeCache = "user.inode_cache"; |
| 42 | constexpr const char* kXattrInodeCodeCache = "user.inode_code_cache"; |
Jeff Sharkey | ed909ae | 2017-03-22 21:27:40 -0600 | [diff] [blame] | 43 | constexpr const char* kXattrCacheGroup = "user.cache_group"; |
Jeff Sharkey | 871a8f2 | 2017-02-21 18:30:28 -0700 | [diff] [blame] | 44 | constexpr const char* kXattrCacheTombstone = "user.cache_tombstone"; |
Jeff Sharkey | 9a998f4 | 2016-07-14 18:16:22 -0600 | [diff] [blame] | 45 | |
Andreas Gampe | 02d0de5 | 2015-11-11 20:43:16 -0800 | [diff] [blame] | 46 | int create_pkg_path(char path[PKG_PATH_MAX], |
| 47 | const char *pkgname, |
| 48 | const char *postfix, |
| 49 | userid_t userid); |
| 50 | |
| 51 | std::string create_data_path(const char* volume_uuid); |
| 52 | |
| 53 | std::string create_data_app_path(const char* volume_uuid); |
Andreas Gampe | 02d0de5 | 2015-11-11 20:43:16 -0800 | [diff] [blame] | 54 | std::string create_data_app_package_path(const char* volume_uuid, const char* package_name); |
| 55 | |
Jeff Sharkey | 2f720f7 | 2016-04-10 20:51:40 -0600 | [diff] [blame] | 56 | 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] | 57 | std::string create_data_user_de_path(const char* volume_uuid, userid_t userid); |
| 58 | |
Jeff Sharkey | 2f720f7 | 2016-04-10 20:51:40 -0600 | [diff] [blame] | 59 | std::string create_data_user_ce_package_path(const char* volume_uuid, |
Andreas Gampe | 02d0de5 | 2015-11-11 20:43:16 -0800 | [diff] [blame] | 60 | userid_t user, const char* package_name); |
Jeff Sharkey | 2f720f7 | 2016-04-10 20:51:40 -0600 | [diff] [blame] | 61 | std::string create_data_user_ce_package_path(const char* volume_uuid, |
| 62 | userid_t user, const char* package_name, ino_t ce_data_inode); |
Andreas Gampe | 02d0de5 | 2015-11-11 20:43:16 -0800 | [diff] [blame] | 63 | std::string create_data_user_de_package_path(const char* volume_uuid, |
| 64 | userid_t user, const char* package_name); |
| 65 | |
| 66 | std::string create_data_media_path(const char* volume_uuid, userid_t userid); |
Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 67 | std::string create_data_media_obb_path(const char* volume_uuid, const char* package_name); |
Jeff Sharkey | 3dfae0c | 2016-12-12 17:32:56 -0700 | [diff] [blame] | 68 | std::string create_data_media_package_path(const char* volume_uuid, userid_t userid, |
| 69 | const char* data_type, const char* package_name); |
Andreas Gampe | 02d0de5 | 2015-11-11 20:43:16 -0800 | [diff] [blame] | 70 | |
Jeff Sharkey | 379a12b | 2016-04-14 20:45:06 -0600 | [diff] [blame] | 71 | std::string create_data_misc_legacy_path(userid_t userid); |
| 72 | |
Jeff Sharkey | 3dfae0c | 2016-12-12 17:32:56 -0700 | [diff] [blame] | 73 | std::string create_data_dalvik_cache_path(); |
Jeff Sharkey | 3dfae0c | 2016-12-12 17:32:56 -0700 | [diff] [blame] | 74 | |
Calin Juravle | 114f081 | 2017-03-08 19:05:07 -0800 | [diff] [blame] | 75 | std::string create_primary_cur_profile_dir_path(userid_t userid); |
| 76 | std::string create_primary_current_profile_package_dir_path( |
| 77 | userid_t user, const std::string& package_name); |
| 78 | |
| 79 | std::string create_primary_ref_profile_dir_path(); |
| 80 | std::string create_primary_reference_profile_package_dir_path(const std::string& package_name); |
| 81 | |
| 82 | std::string create_current_profile_path( |
| 83 | userid_t user, const std::string& package_name, bool is_secondary_dex); |
| 84 | std::string create_reference_profile_path( |
| 85 | const std::string& package_name, bool is_secondary_dex); |
Jeff Sharkey | 90aff26 | 2016-12-12 14:28:24 -0700 | [diff] [blame] | 86 | |
Andreas Gampe | 02d0de5 | 2015-11-11 20:43:16 -0800 | [diff] [blame] | 87 | std::vector<userid_t> get_known_users(const char* volume_uuid); |
| 88 | |
Jeff Sharkey | 3dfae0c | 2016-12-12 17:32:56 -0700 | [diff] [blame] | 89 | int calculate_tree_size(const std::string& path, int64_t* size, |
Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 90 | 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] | 91 | |
Andreas Gampe | 02d0de5 | 2015-11-11 20:43:16 -0800 | [diff] [blame] | 92 | int create_user_config_path(char path[PKG_PATH_MAX], userid_t userid); |
| 93 | |
| 94 | int create_move_path(char path[PKG_PATH_MAX], |
| 95 | const char* pkgname, |
| 96 | const char* leaf, |
| 97 | userid_t userid); |
| 98 | |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 99 | bool is_valid_filename(const std::string& name); |
| 100 | bool is_valid_package_name(const std::string& packageName); |
Andreas Gampe | 02d0de5 | 2015-11-11 20:43:16 -0800 | [diff] [blame] | 101 | |
Calin Juravle | b06f98a | 2016-03-28 15:11:01 +0100 | [diff] [blame] | 102 | int delete_dir_contents(const std::string& pathname, bool ignore_if_missing = false); |
| 103 | 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] | 104 | |
| 105 | int delete_dir_contents(const char *pathname, |
| 106 | int also_delete_dir, |
Calin Juravle | b06f98a | 2016-03-28 15:11:01 +0100 | [diff] [blame] | 107 | int (*exclusion_predicate)(const char *name, const int is_dir), |
| 108 | bool ignore_if_missing = false); |
Andreas Gampe | 02d0de5 | 2015-11-11 20:43:16 -0800 | [diff] [blame] | 109 | |
| 110 | int delete_dir_contents_fd(int dfd, const char *name); |
| 111 | |
| 112 | int copy_dir_files(const char *srcname, const char *dstname, uid_t owner, gid_t group); |
| 113 | |
Andreas Gampe | 02d0de5 | 2015-11-11 20:43:16 -0800 | [diff] [blame] | 114 | int64_t data_disk_free(const std::string& data_path); |
| 115 | |
Jeff Sharkey | 9a998f4 | 2016-07-14 18:16:22 -0600 | [diff] [blame] | 116 | int get_path_inode(const std::string& path, ino_t *inode); |
| 117 | |
| 118 | int write_path_inode(const std::string& parent, const char* name, const char* inode_xattr); |
| 119 | std::string read_path_inode(const std::string& parent, const char* name, const char* inode_xattr); |
| 120 | |
Andreas Gampe | 02d0de5 | 2015-11-11 20:43:16 -0800 | [diff] [blame] | 121 | int validate_system_app_path(const char* path); |
Calin Juravle | 114f081 | 2017-03-08 19:05:07 -0800 | [diff] [blame] | 122 | bool validate_secondary_dex_path(const std::string& pkgname, const std::string& dex_path, |
Calin Juravle | 42451c0 | 2017-01-17 14:43:25 -0800 | [diff] [blame] | 123 | const char* volume_uuid, int uid, int storage_flag); |
Andreas Gampe | 02d0de5 | 2015-11-11 20:43:16 -0800 | [diff] [blame] | 124 | |
| 125 | int get_path_from_env(dir_rec_t* rec, const char* var); |
| 126 | |
| 127 | int get_path_from_string(dir_rec_t* rec, const char* path); |
| 128 | |
| 129 | int copy_and_append(dir_rec_t* dst, const dir_rec_t* src, const char* suffix); |
| 130 | |
| 131 | int validate_apk_path(const char *path); |
| 132 | int validate_apk_path_subdirs(const char *path); |
| 133 | |
| 134 | int append_and_increment(char** dst, const char* src, size_t* dst_size); |
| 135 | |
| 136 | char *build_string2(const char *s1, const char *s2); |
| 137 | char *build_string3(const char *s1, const char *s2, const char *s3); |
| 138 | |
Andreas Gampe | 02d0de5 | 2015-11-11 20:43:16 -0800 | [diff] [blame] | 139 | int ensure_config_user_dirs(userid_t userid); |
| 140 | |
| 141 | int wait_child(pid_t pid); |
| 142 | |
Calin Juravle | 42451c0 | 2017-01-17 14:43:25 -0800 | [diff] [blame] | 143 | int prepare_app_cache_dir(const std::string& parent, const char* name, mode_t target_mode, |
| 144 | uid_t uid, gid_t gid); |
| 145 | |
Andreas Gampe | 02d0de5 | 2015-11-11 20:43:16 -0800 | [diff] [blame] | 146 | } // namespace installd |
| 147 | } // namespace android |
| 148 | |
| 149 | #endif // UTILS_H_ |