blob: a1d8443d7d35eded1a3a9ac20ad940690f6f2a4d [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
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 Sharkeydf2d7542017-01-07 09:19:35 -070033#define MEASURE_DEBUG 0
Jeff Sharkeye12d5962017-04-03 16:41:02 -060034#define FIXUP_DEBUG 0
Jeff Sharkeydf2d7542017-01-07 09:19:35 -070035
Jeff Sharkeyd712f0c2017-05-03 11:36:42 -060036#define BYPASS_QUOTA 0
37#define BYPASS_SDCARDFS 0
38
Jeff Sharkey4f7be172017-08-11 15:13:31 -060039#define APPLY_HARD_QUOTAS 1
40
Andreas Gampe02d0de52015-11-11 20:43:16 -080041namespace android {
42namespace installd {
43
44struct dir_rec_t;
45
Jeff Sharkey9a998f42016-07-14 18:16:22 -060046constexpr const char* kXattrInodeCache = "user.inode_cache";
47constexpr const char* kXattrInodeCodeCache = "user.inode_code_cache";
Jeff Sharkeyed909ae2017-03-22 21:27:40 -060048constexpr const char* kXattrCacheGroup = "user.cache_group";
Jeff Sharkey871a8f22017-02-21 18:30:28 -070049constexpr const char* kXattrCacheTombstone = "user.cache_tombstone";
Jeff Sharkey9a998f42016-07-14 18:16:22 -060050
Andreas Gampe02d0de52015-11-11 20:43:16 -080051int create_pkg_path(char path[PKG_PATH_MAX],
52 const char *pkgname,
53 const char *postfix,
54 userid_t userid);
55
56std::string create_data_path(const char* volume_uuid);
57
58std::string create_data_app_path(const char* volume_uuid);
Andreas Gampe02d0de52015-11-11 20:43:16 -080059std::string create_data_app_package_path(const char* volume_uuid, const char* package_name);
60
Jeff Sharkey2f720f72016-04-10 20:51:40 -060061std::string create_data_user_ce_path(const char* volume_uuid, userid_t userid);
Andreas Gampe02d0de52015-11-11 20:43:16 -080062std::string create_data_user_de_path(const char* volume_uuid, userid_t userid);
63
Jeff Sharkey2f720f72016-04-10 20:51:40 -060064std::string create_data_user_ce_package_path(const char* volume_uuid,
Andreas Gampe02d0de52015-11-11 20:43:16 -080065 userid_t user, const char* package_name);
Jeff Sharkey2f720f72016-04-10 20:51:40 -060066std::string create_data_user_ce_package_path(const char* volume_uuid,
67 userid_t user, const char* package_name, ino_t ce_data_inode);
Andreas Gampe02d0de52015-11-11 20:43:16 -080068std::string create_data_user_de_package_path(const char* volume_uuid,
69 userid_t user, const char* package_name);
70
71std::string create_data_media_path(const char* volume_uuid, userid_t userid);
Jeff Sharkeydf2d7542017-01-07 09:19:35 -070072std::string create_data_media_obb_path(const char* volume_uuid, const char* package_name);
Jeff Sharkey3dfae0c2016-12-12 17:32:56 -070073std::string create_data_media_package_path(const char* volume_uuid, userid_t userid,
74 const char* data_type, const char* package_name);
Andreas Gampe02d0de52015-11-11 20:43:16 -080075
Jeff Sharkey379a12b2016-04-14 20:45:06 -060076std::string create_data_misc_legacy_path(userid_t userid);
77
Jeff Sharkey3dfae0c2016-12-12 17:32:56 -070078std::string create_data_dalvik_cache_path();
Jeff Sharkey3dfae0c2016-12-12 17:32:56 -070079
Calin Juravle114f0812017-03-08 19:05:07 -080080std::string create_primary_cur_profile_dir_path(userid_t userid);
81std::string create_primary_current_profile_package_dir_path(
82 userid_t user, const std::string& package_name);
83
84std::string create_primary_ref_profile_dir_path();
85std::string create_primary_reference_profile_package_dir_path(const std::string& package_name);
86
87std::string create_current_profile_path(
88 userid_t user, const std::string& package_name, bool is_secondary_dex);
89std::string create_reference_profile_path(
90 const std::string& package_name, bool is_secondary_dex);
Jeff Sharkey90aff262016-12-12 14:28:24 -070091
Andreas Gampe02d0de52015-11-11 20:43:16 -080092std::vector<userid_t> get_known_users(const char* volume_uuid);
93
Jeff Sharkey3dfae0c2016-12-12 17:32:56 -070094int calculate_tree_size(const std::string& path, int64_t* size,
Jeff Sharkeydf2d7542017-01-07 09:19:35 -070095 int32_t include_gid = -1, int32_t exclude_gid = -1, bool exclude_apps = false);
Jeff Sharkey3dfae0c2016-12-12 17:32:56 -070096
Andreas Gampe02d0de52015-11-11 20:43:16 -080097int create_user_config_path(char path[PKG_PATH_MAX], userid_t userid);
98
99int create_move_path(char path[PKG_PATH_MAX],
100 const char* pkgname,
101 const char* leaf,
102 userid_t userid);
103
Jeff Sharkey423e7462016-12-09 18:18:43 -0700104bool is_valid_filename(const std::string& name);
105bool is_valid_package_name(const std::string& packageName);
Andreas Gampe02d0de52015-11-11 20:43:16 -0800106
Calin Juravleb06f98a2016-03-28 15:11:01 +0100107int delete_dir_contents(const std::string& pathname, bool ignore_if_missing = false);
108int delete_dir_contents_and_dir(const std::string& pathname, bool ignore_if_missing = false);
Andreas Gampe02d0de52015-11-11 20:43:16 -0800109
110int delete_dir_contents(const char *pathname,
111 int also_delete_dir,
Calin Juravleb06f98a2016-03-28 15:11:01 +0100112 int (*exclusion_predicate)(const char *name, const int is_dir),
113 bool ignore_if_missing = false);
Andreas Gampe02d0de52015-11-11 20:43:16 -0800114
115int delete_dir_contents_fd(int dfd, const char *name);
116
117int copy_dir_files(const char *srcname, const char *dstname, uid_t owner, gid_t group);
118
Andreas Gampe02d0de52015-11-11 20:43:16 -0800119int64_t data_disk_free(const std::string& data_path);
120
Jeff Sharkey9a998f42016-07-14 18:16:22 -0600121int get_path_inode(const std::string& path, ino_t *inode);
122
123int write_path_inode(const std::string& parent, const char* name, const char* inode_xattr);
124std::string read_path_inode(const std::string& parent, const char* name, const char* inode_xattr);
Ryuki Nakamurac7342f82017-09-30 11:57:00 +0900125void remove_path_xattr(const std::string& path, const char* inode_xattr);
Jeff Sharkey9a998f42016-07-14 18:16:22 -0600126
Andreas Gampe02d0de52015-11-11 20:43:16 -0800127int validate_system_app_path(const char* path);
Calin Juravle114f0812017-03-08 19:05:07 -0800128bool validate_secondary_dex_path(const std::string& pkgname, const std::string& dex_path,
Calin Juravledd42e272017-09-11 11:50:36 -0700129 const char* volume_uuid, int uid, int storage_flag, bool validate_package_path = true);
Andreas Gampe02d0de52015-11-11 20:43:16 -0800130
131int get_path_from_env(dir_rec_t* rec, const char* var);
132
133int get_path_from_string(dir_rec_t* rec, const char* path);
134
135int copy_and_append(dir_rec_t* dst, const dir_rec_t* src, const char* suffix);
136
137int validate_apk_path(const char *path);
138int validate_apk_path_subdirs(const char *path);
139
140int append_and_increment(char** dst, const char* src, size_t* dst_size);
141
142char *build_string2(const char *s1, const char *s2);
143char *build_string3(const char *s1, const char *s2, const char *s3);
144
Andreas Gampe02d0de52015-11-11 20:43:16 -0800145int ensure_config_user_dirs(userid_t userid);
146
147int wait_child(pid_t pid);
148
Calin Juravle42451c02017-01-17 14:43:25 -0800149int prepare_app_cache_dir(const std::string& parent, const char* name, mode_t target_mode,
150 uid_t uid, gid_t gid);
151
Andreas Gampe02d0de52015-11-11 20:43:16 -0800152} // namespace installd
153} // namespace android
154
155#endif // UTILS_H_