blob: c43fdbd54798086bda9129606c4f20859ec2237b [file] [log] [blame]
Andreas Gampe02d0de52015-11-11 20:43:16 -08001/*
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
Ryan Prichard644a5b72023-07-20 21:39:48 -070021#include <functional>
Andreas Gampe02d0de52015-11-11 20:43:16 -080022#include <string>
23#include <vector>
24
25#include <dirent.h>
26#include <inttypes.h>
27#include <unistd.h>
28#include <utime.h>
29
30#include <cutils/multiuser.h>
31
32#include <installd_constants.h>
33
Jeff Sharkeydf2d7542017-01-07 09:19:35 -070034#define MEASURE_DEBUG 0
Jeff Sharkeye12d5962017-04-03 16:41:02 -060035#define FIXUP_DEBUG 0
Samiul Islamb9e96bf2022-02-03 12:45:51 +000036#define SDK_DEBUG 1
Jeff Sharkeydf2d7542017-01-07 09:19:35 -070037
Jeff Sharkeyd712f0c2017-05-03 11:36:42 -060038#define BYPASS_QUOTA 0
39#define BYPASS_SDCARDFS 0
40
Andreas Gampe02d0de52015-11-11 20:43:16 -080041namespace android {
42namespace installd {
43
Jeff Sharkey9a998f42016-07-14 18:16:22 -060044constexpr const char* kXattrInodeCache = "user.inode_cache";
45constexpr const char* kXattrInodeCodeCache = "user.inode_code_cache";
Jeff Sharkeyed909ae2017-03-22 21:27:40 -060046constexpr const char* kXattrCacheGroup = "user.cache_group";
Jeff Sharkey871a8f22017-02-21 18:30:28 -070047constexpr const char* kXattrCacheTombstone = "user.cache_tombstone";
Jeff Sharkey9a998f42016-07-14 18:16:22 -060048
Andreas Gampe02d0de52015-11-11 20:43:16 -080049std::string create_data_path(const char* volume_uuid);
50
51std::string create_data_app_path(const char* volume_uuid);
Andreas Gampe02d0de52015-11-11 20:43:16 -080052
Jeff Sharkey2f720f72016-04-10 20:51:40 -060053std::string create_data_user_ce_path(const char* volume_uuid, userid_t userid);
Andreas Gampe02d0de52015-11-11 20:43:16 -080054std::string create_data_user_de_path(const char* volume_uuid, userid_t userid);
55
Jeff Sharkey2f720f72016-04-10 20:51:40 -060056std::string create_data_user_ce_package_path(const char* volume_uuid,
Andreas Gampe02d0de52015-11-11 20:43:16 -080057 userid_t user, const char* package_name);
Jeff Sharkey2f720f72016-04-10 20:51:40 -060058std::string create_data_user_ce_package_path(const char* volume_uuid,
59 userid_t user, const char* package_name, ino_t ce_data_inode);
Andreas Gampe02d0de52015-11-11 20:43:16 -080060std::string create_data_user_de_package_path(const char* volume_uuid,
61 userid_t user, const char* package_name);
Calin Juravle7d765462017-09-04 15:57:10 -070062std::string create_data_user_ce_package_path_as_user_link(
63 const char* volume_uuid, userid_t userid, const char* package_name);
Andreas Gampe02d0de52015-11-11 20:43:16 -080064
Nikita Ioffe9c3e8f52022-02-21 19:02:05 +000065std::string create_data_misc_sdk_sandbox_path(const char* volume_uuid, bool isCeData,
66 userid_t userid);
67std::string create_data_misc_sdk_sandbox_package_path(const char* volume_uuid, bool isCeData,
Samiul Islamb911bc62022-01-14 16:24:48 +000068 userid_t userid, const char* package_name);
Samiul Islamb9e96bf2022-02-03 12:45:51 +000069std::string create_data_misc_sdk_sandbox_sdk_path(const char* volume_uuid, bool isCeData,
70 userid_t userid, const char* package_name,
Mohammad Samiul Islam04302792022-03-09 17:04:38 +000071 const char* sub_dir_name);
Samiul Islamb911bc62022-01-14 16:24:48 +000072
Nikita Ioffeb68f0d12019-02-04 11:06:37 +000073std::string create_data_misc_ce_rollback_base_path(const char* volume_uuid, userid_t user);
74std::string create_data_misc_de_rollback_base_path(const char* volume_uuid, userid_t user);
75std::string create_data_misc_ce_rollback_path(const char* volume_uuid, userid_t user,
76 int32_t snapshot_id);
77std::string create_data_misc_de_rollback_path(const char* volume_uuid, userid_t user,
78 int32_t snapshot_id);
Narayan Kamathe63ca7d2019-01-14 15:21:52 +000079std::string create_data_misc_ce_rollback_package_path(const char* volume_uuid,
Nikita Ioffeb68f0d12019-02-04 11:06:37 +000080 userid_t user, int32_t snapshot_id, const char* package_name);
Nikita Ioffe77be2ed2019-01-25 13:54:43 +000081std::string create_data_misc_ce_rollback_package_path(const char* volume_uuid,
Nikita Ioffeb68f0d12019-02-04 11:06:37 +000082 userid_t user, int32_t snapshot_id, const char* package_name, ino_t ce_rollback_inode);
Narayan Kamathe63ca7d2019-01-14 15:21:52 +000083std::string create_data_misc_de_rollback_package_path(const char* volume_uuid,
Nikita Ioffeb68f0d12019-02-04 11:06:37 +000084 userid_t user, int32_t snapshot_id, const char* package_name);
Narayan Kamathe63ca7d2019-01-14 15:21:52 +000085
Andreas Gampe02d0de52015-11-11 20:43:16 -080086std::string create_data_media_path(const char* volume_uuid, userid_t userid);
Jeff Sharkey3dfae0c2016-12-12 17:32:56 -070087std::string create_data_media_package_path(const char* volume_uuid, userid_t userid,
88 const char* data_type, const char* package_name);
Andreas Gampe02d0de52015-11-11 20:43:16 -080089
Jeff Sharkey379a12b2016-04-14 20:45:06 -060090std::string create_data_misc_legacy_path(userid_t userid);
91
Jeff Sharkey3dfae0c2016-12-12 17:32:56 -070092std::string create_data_dalvik_cache_path();
Jeff Sharkey3dfae0c2016-12-12 17:32:56 -070093
Felka Chang2a0a2462019-11-20 14:20:40 +080094std::string create_system_user_ce_path(userid_t userId);
95
96std::string create_system_user_ce_package_path(userid_t userId, const char* package_name);
97
Calin Juravle114f0812017-03-08 19:05:07 -080098std::string create_primary_cur_profile_dir_path(userid_t userid);
99std::string create_primary_current_profile_package_dir_path(
100 userid_t user, const std::string& package_name);
101
102std::string create_primary_ref_profile_dir_path();
103std::string create_primary_reference_profile_package_dir_path(const std::string& package_name);
104
105std::string create_current_profile_path(
Calin Juravlecfcd6aa2018-01-18 20:23:17 -0800106 userid_t user,
107 const std::string& package_name,
108 const std::string& location,
109 bool is_secondary_dex);
Calin Juravle114f0812017-03-08 19:05:07 -0800110std::string create_reference_profile_path(
Calin Juravlecfcd6aa2018-01-18 20:23:17 -0800111 const std::string& package_name,
112 const std::string& location,
113 bool is_secondary_dex);
114std::string create_snapshot_profile_path(
115 const std::string& package,
116 const std::string& profile_name);
Jeff Sharkey90aff262016-12-12 14:28:24 -0700117
Andreas Gampe02d0de52015-11-11 20:43:16 -0800118std::vector<userid_t> get_known_users(const char* volume_uuid);
119
Jeff Sharkey3dfae0c2016-12-12 17:32:56 -0700120int calculate_tree_size(const std::string& path, int64_t* size,
Jeff Sharkeydf2d7542017-01-07 09:19:35 -0700121 int32_t include_gid = -1, int32_t exclude_gid = -1, bool exclude_apps = false);
Jeff Sharkey3dfae0c2016-12-12 17:32:56 -0700122
Andreas Gampe02d0de52015-11-11 20:43:16 -0800123int create_user_config_path(char path[PKG_PATH_MAX], userid_t userid);
124
Jeff Sharkey423e7462016-12-09 18:18:43 -0700125bool is_valid_filename(const std::string& name);
126bool is_valid_package_name(const std::string& packageName);
Andreas Gampe02d0de52015-11-11 20:43:16 -0800127
Nikita Ioffeb68f0d12019-02-04 11:06:37 +0000128int create_dir_if_needed(const std::string& pathname, mode_t mode);
129
Calin Juravleb06f98a2016-03-28 15:11:01 +0100130int delete_dir_contents(const std::string& pathname, bool ignore_if_missing = false);
131int delete_dir_contents_and_dir(const std::string& pathname, bool ignore_if_missing = false);
Andreas Gampe02d0de52015-11-11 20:43:16 -0800132
Alex Buynytskyy4ab5d532022-02-17 21:20:10 +0000133bool is_renamed_deleted_dir(const std::string& path);
Alex Buynytskyy5cb25262022-02-15 08:59:36 -0800134int rename_delete_dir_contents_and_dir(const std::string& pathname, bool ignore_if_missing = true);
Alex Buynytskyy58a73092022-02-14 13:15:53 -0800135
Samiul Islamb9e96bf2022-02-03 12:45:51 +0000136int foreach_subdir(const std::string& pathname, std::function<void(const std::string&)> fn);
137
Alex Buynytskyy58a73092022-02-14 13:15:53 -0800138void cleanup_invalid_package_dirs_under_path(const std::string& pathname);
Alex Buynytskyy038a19b2022-02-09 19:51:52 -0800139
Andreas Gampe02d0de52015-11-11 20:43:16 -0800140int delete_dir_contents(const char *pathname,
141 int also_delete_dir,
Calin Juravleb06f98a2016-03-28 15:11:01 +0100142 int (*exclusion_predicate)(const char *name, const int is_dir),
143 bool ignore_if_missing = false);
Andreas Gampe02d0de52015-11-11 20:43:16 -0800144
145int delete_dir_contents_fd(int dfd, const char *name);
146
Oleksandr Peletskyid2be6322019-01-24 17:59:45 +0100147int rm_package_dir(const std::string& package_dir);
148
Andreas Gampe02d0de52015-11-11 20:43:16 -0800149int copy_dir_files(const char *srcname, const char *dstname, uid_t owner, gid_t group);
150
Andreas Gampe02d0de52015-11-11 20:43:16 -0800151int64_t data_disk_free(const std::string& data_path);
152
Jeff Sharkey9a998f42016-07-14 18:16:22 -0600153int get_path_inode(const std::string& path, ino_t *inode);
154
155int write_path_inode(const std::string& parent, const char* name, const char* inode_xattr);
156std::string read_path_inode(const std::string& parent, const char* name, const char* inode_xattr);
Ryuki Nakamurac7342f82017-09-30 11:57:00 +0900157void remove_path_xattr(const std::string& path, const char* inode_xattr);
Jeff Sharkey9a998f42016-07-14 18:16:22 -0600158
Andreas Gampe02d0de52015-11-11 20:43:16 -0800159int validate_system_app_path(const char* path);
Calin Juravle114f0812017-03-08 19:05:07 -0800160bool validate_secondary_dex_path(const std::string& pkgname, const std::string& dex_path,
Calin Juravle7d765462017-09-04 15:57:10 -0700161 const char* volume_uuid, int uid, int storage_flag);
Andreas Gampe02d0de52015-11-11 20:43:16 -0800162
Andreas Gampe02d0de52015-11-11 20:43:16 -0800163int validate_apk_path(const char *path);
164int validate_apk_path_subdirs(const char *path);
165
Andreas Gampe02d0de52015-11-11 20:43:16 -0800166int ensure_config_user_dirs(userid_t userid);
167
Jiakai Zhang0a7603c2022-02-21 19:38:14 +0000168// Waits for a child process, or kills it if it times out. Returns the exit code.
169int wait_child_with_timeout(pid_t pid, int timeout_ms);
Andreas Gampe02d0de52015-11-11 20:43:16 -0800170
Calin Juravle42451c02017-01-17 14:43:25 -0800171int prepare_app_cache_dir(const std::string& parent, const char* name, mode_t target_mode,
172 uid_t uid, gid_t gid);
173
Martijn Coenen771cc342020-02-19 23:26:56 +0100174bool supports_sdcardfs();
Shikha Malhotra7fa4f882022-01-12 21:05:31 +0000175long get_project_id(uid_t uid, long start_project_id_range);
176int set_quota_project_id(const std::string& path, long project_id, bool set_inherit);
Martijn Coenen771cc342020-02-19 23:26:56 +0100177int64_t get_occupied_app_space_external(const std::string& uuid, int32_t userId, int32_t appId);
178int64_t get_occupied_app_cache_space_external(const std::string& uuid, int32_t userId, int32_t appId);
179
Calin Juravlee61189e2018-01-23 19:54:11 -0800180// Collect all non empty profiles from the global profile directory and
181// put then into profile_paths. The profiles are identified based on PROFILE_EXT extension.
182// If a subdirectory or profile file cannot be opened the method logs a warning and moves on.
183// It returns true if there were no errors at all, and false otherwise.
184bool collect_profiles(std::vector<std::string>* profiles_paths);
185
Eric Holk0ebbe0f2019-01-09 18:17:27 -0800186void drop_capabilities(uid_t uid);
187
Jiakai Zhang0a7603c2022-02-21 19:38:14 +0000188// Removes a file specified by a file descriptor. Returns true on success. Reports the file path to
189// `path` if present.
190bool remove_file_at_fd(int fd, /*out*/ std::string* path = nullptr);
191
Andreas Gampe02d0de52015-11-11 20:43:16 -0800192} // namespace installd
193} // namespace android
194
195#endif // UTILS_H_