blob: a4eb1f66b97aff03f7ede79b3c13e19aa7a9af65 [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 Sharkeydf2d7542017-01-07 09:19:35 -070034
Andreas Gampe02d0de52015-11-11 20:43:16 -080035namespace android {
36namespace installd {
37
38struct dir_rec_t;
39
40typedef struct cache_dir_struct {
41 struct cache_dir_struct* parent;
42 int32_t childCount;
43 int32_t hiddenCount;
44 int32_t deleted;
45 char name[];
46} cache_dir_t;
47
48typedef struct {
49 cache_dir_t* dir;
50 time_t modTime;
51 char name[];
52} cache_file_t;
53
54typedef struct {
55 size_t numDirs;
56 size_t availDirs;
57 cache_dir_t** dirs;
58 size_t numFiles;
59 size_t availFiles;
60 cache_file_t** files;
61 size_t numCollected;
62 void* memBlocks;
63 int8_t* curMemBlockAvail;
64 int8_t* curMemBlockEnd;
65} cache_t;
66
Jeff Sharkey9a998f42016-07-14 18:16:22 -060067constexpr const char* kXattrInodeCache = "user.inode_cache";
68constexpr const char* kXattrInodeCodeCache = "user.inode_code_cache";
Jeff Sharkeyed909ae2017-03-22 21:27:40 -060069constexpr const char* kXattrCacheGroup = "user.cache_group";
Jeff Sharkey871a8f22017-02-21 18:30:28 -070070constexpr const char* kXattrCacheTombstone = "user.cache_tombstone";
Jeff Sharkey9a998f42016-07-14 18:16:22 -060071
Andreas Gampe02d0de52015-11-11 20:43:16 -080072int create_pkg_path(char path[PKG_PATH_MAX],
73 const char *pkgname,
74 const char *postfix,
75 userid_t userid);
76
77std::string create_data_path(const char* volume_uuid);
78
79std::string create_data_app_path(const char* volume_uuid);
Andreas Gampe02d0de52015-11-11 20:43:16 -080080std::string create_data_app_package_path(const char* volume_uuid, const char* package_name);
81
Jeff Sharkey2f720f72016-04-10 20:51:40 -060082std::string create_data_user_ce_path(const char* volume_uuid, userid_t userid);
Andreas Gampe02d0de52015-11-11 20:43:16 -080083std::string create_data_user_de_path(const char* volume_uuid, userid_t userid);
84
Jeff Sharkey2f720f72016-04-10 20:51:40 -060085std::string create_data_user_ce_package_path(const char* volume_uuid,
Andreas Gampe02d0de52015-11-11 20:43:16 -080086 userid_t user, const char* package_name);
Jeff Sharkey2f720f72016-04-10 20:51:40 -060087std::string create_data_user_ce_package_path(const char* volume_uuid,
88 userid_t user, const char* package_name, ino_t ce_data_inode);
Andreas Gampe02d0de52015-11-11 20:43:16 -080089std::string create_data_user_de_package_path(const char* volume_uuid,
90 userid_t user, const char* package_name);
91
92std::string create_data_media_path(const char* volume_uuid, userid_t userid);
Jeff Sharkeydf2d7542017-01-07 09:19:35 -070093std::string create_data_media_obb_path(const char* volume_uuid, const char* package_name);
Jeff Sharkey3dfae0c2016-12-12 17:32:56 -070094std::string create_data_media_package_path(const char* volume_uuid, userid_t userid,
95 const char* data_type, const char* package_name);
Andreas Gampe02d0de52015-11-11 20:43:16 -080096
Jeff Sharkey379a12b2016-04-14 20:45:06 -060097std::string create_data_misc_legacy_path(userid_t userid);
98
Jeff Sharkey3dfae0c2016-12-12 17:32:56 -070099std::string create_data_dalvik_cache_path();
Jeff Sharkey3dfae0c2016-12-12 17:32:56 -0700100
Calin Juravle114f0812017-03-08 19:05:07 -0800101std::string create_primary_cur_profile_dir_path(userid_t userid);
102std::string create_primary_current_profile_package_dir_path(
103 userid_t user, const std::string& package_name);
104
105std::string create_primary_ref_profile_dir_path();
106std::string create_primary_reference_profile_package_dir_path(const std::string& package_name);
107
108std::string create_current_profile_path(
109 userid_t user, const std::string& package_name, bool is_secondary_dex);
110std::string create_reference_profile_path(
111 const std::string& package_name, bool is_secondary_dex);
Jeff Sharkey90aff262016-12-12 14:28:24 -0700112
Andreas Gampe02d0de52015-11-11 20:43:16 -0800113std::vector<userid_t> get_known_users(const char* volume_uuid);
114
Jeff Sharkey3dfae0c2016-12-12 17:32:56 -0700115int calculate_tree_size(const std::string& path, int64_t* size,
Jeff Sharkeydf2d7542017-01-07 09:19:35 -0700116 int32_t include_gid = -1, int32_t exclude_gid = -1, bool exclude_apps = false);
Jeff Sharkey3dfae0c2016-12-12 17:32:56 -0700117
Andreas Gampe02d0de52015-11-11 20:43:16 -0800118int create_user_config_path(char path[PKG_PATH_MAX], userid_t userid);
119
120int create_move_path(char path[PKG_PATH_MAX],
121 const char* pkgname,
122 const char* leaf,
123 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
Calin Juravleb06f98a2016-03-28 15:11:01 +0100128int delete_dir_contents(const std::string& pathname, bool ignore_if_missing = false);
129int delete_dir_contents_and_dir(const std::string& pathname, bool ignore_if_missing = false);
Andreas Gampe02d0de52015-11-11 20:43:16 -0800130
131int delete_dir_contents(const char *pathname,
132 int also_delete_dir,
Calin Juravleb06f98a2016-03-28 15:11:01 +0100133 int (*exclusion_predicate)(const char *name, const int is_dir),
134 bool ignore_if_missing = false);
Andreas Gampe02d0de52015-11-11 20:43:16 -0800135
136int delete_dir_contents_fd(int dfd, const char *name);
137
138int copy_dir_files(const char *srcname, const char *dstname, uid_t owner, gid_t group);
139
Andreas Gampe02d0de52015-11-11 20:43:16 -0800140int64_t data_disk_free(const std::string& data_path);
141
142cache_t* start_cache_collection();
143
Jeff Sharkey9a998f42016-07-14 18:16:22 -0600144int get_path_inode(const std::string& path, ino_t *inode);
145
146int write_path_inode(const std::string& parent, const char* name, const char* inode_xattr);
147std::string read_path_inode(const std::string& parent, const char* name, const char* inode_xattr);
148
Jeff Sharkey54e292e2016-05-10 17:21:13 -0600149void add_cache_files(cache_t* cache, const std::string& data_path);
Andreas Gampe02d0de52015-11-11 20:43:16 -0800150
Fyodor Kupolovf3124d82016-09-07 13:07:45 -0700151void add_preloads_file_cache(cache_t* cache, const char* volume_uuid);
152
Andreas Gampe02d0de52015-11-11 20:43:16 -0800153void clear_cache_files(const std::string& data_path, cache_t* cache, int64_t free_size);
154
155void finish_cache_collection(cache_t* cache);
156
157int validate_system_app_path(const char* path);
Calin Juravle114f0812017-03-08 19:05:07 -0800158bool validate_secondary_dex_path(const std::string& pkgname, const std::string& dex_path,
Calin Juravle42451c02017-01-17 14:43:25 -0800159 const char* volume_uuid, int uid, int storage_flag);
Andreas Gampe02d0de52015-11-11 20:43:16 -0800160
161int get_path_from_env(dir_rec_t* rec, const char* var);
162
163int get_path_from_string(dir_rec_t* rec, const char* path);
164
165int copy_and_append(dir_rec_t* dst, const dir_rec_t* src, const char* suffix);
166
167int validate_apk_path(const char *path);
168int validate_apk_path_subdirs(const char *path);
169
170int append_and_increment(char** dst, const char* src, size_t* dst_size);
171
172char *build_string2(const char *s1, const char *s2);
173char *build_string3(const char *s1, const char *s2, const char *s3);
174
Andreas Gampe02d0de52015-11-11 20:43:16 -0800175int ensure_config_user_dirs(userid_t userid);
176
177int wait_child(pid_t pid);
178
Calin Juravle42451c02017-01-17 14:43:25 -0800179int prepare_app_cache_dir(const std::string& parent, const char* name, mode_t target_mode,
180 uid_t uid, gid_t gid);
181
Andreas Gampe02d0de52015-11-11 20:43:16 -0800182} // namespace installd
183} // namespace android
184
185#endif // UTILS_H_