blob: f2f0cbb030332e1ce79b6562a34fda5b2128b17e [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
34#define MEASURE_EXTERNAL 0
35
Andreas Gampe02d0de52015-11-11 20:43:16 -080036namespace android {
37namespace installd {
38
39struct dir_rec_t;
40
41typedef struct cache_dir_struct {
42 struct cache_dir_struct* parent;
43 int32_t childCount;
44 int32_t hiddenCount;
45 int32_t deleted;
46 char name[];
47} cache_dir_t;
48
49typedef struct {
50 cache_dir_t* dir;
51 time_t modTime;
52 char name[];
53} cache_file_t;
54
55typedef struct {
56 size_t numDirs;
57 size_t availDirs;
58 cache_dir_t** dirs;
59 size_t numFiles;
60 size_t availFiles;
61 cache_file_t** files;
62 size_t numCollected;
63 void* memBlocks;
64 int8_t* curMemBlockAvail;
65 int8_t* curMemBlockEnd;
66} cache_t;
67
Jeff Sharkey9a998f42016-07-14 18:16:22 -060068constexpr const char* kXattrInodeCache = "user.inode_cache";
69constexpr const char* kXattrInodeCodeCache = "user.inode_code_cache";
70
Andreas Gampe02d0de52015-11-11 20:43:16 -080071int create_pkg_path(char path[PKG_PATH_MAX],
72 const char *pkgname,
73 const char *postfix,
74 userid_t userid);
75
76std::string create_data_path(const char* volume_uuid);
77
78std::string create_data_app_path(const char* volume_uuid);
Andreas Gampe02d0de52015-11-11 20:43:16 -080079std::string create_data_app_package_path(const char* volume_uuid, const char* package_name);
80
Jeff Sharkey2f720f72016-04-10 20:51:40 -060081std::string create_data_user_ce_path(const char* volume_uuid, userid_t userid);
Andreas Gampe02d0de52015-11-11 20:43:16 -080082std::string create_data_user_de_path(const char* volume_uuid, userid_t userid);
83
Jeff Sharkey2f720f72016-04-10 20:51:40 -060084std::string create_data_user_ce_package_path(const char* volume_uuid,
Andreas Gampe02d0de52015-11-11 20:43:16 -080085 userid_t user, const char* package_name);
Jeff Sharkey2f720f72016-04-10 20:51:40 -060086std::string create_data_user_ce_package_path(const char* volume_uuid,
87 userid_t user, const char* package_name, ino_t ce_data_inode);
Andreas Gampe02d0de52015-11-11 20:43:16 -080088std::string create_data_user_de_package_path(const char* volume_uuid,
89 userid_t user, const char* package_name);
90
91std::string create_data_media_path(const char* volume_uuid, userid_t userid);
Jeff Sharkeydf2d7542017-01-07 09:19:35 -070092std::string create_data_media_obb_path(const char* volume_uuid, const char* package_name);
Jeff Sharkey3dfae0c2016-12-12 17:32:56 -070093std::string create_data_media_package_path(const char* volume_uuid, userid_t userid,
94 const char* data_type, const char* package_name);
Andreas Gampe02d0de52015-11-11 20:43:16 -080095
Jeff Sharkey379a12b2016-04-14 20:45:06 -060096std::string create_data_misc_legacy_path(userid_t userid);
97
Jeff Sharkeydf2d7542017-01-07 09:19:35 -070098std::string create_data_user_profile_path(userid_t userid);
Calin Juravle6a1648e2016-02-01 12:12:16 +000099std::string create_data_user_profile_package_path(userid_t user, const char* package_name);
Jeff Sharkeydf2d7542017-01-07 09:19:35 -0700100
101std::string create_data_ref_profile_path();
Calin Juravle6a1648e2016-02-01 12:12:16 +0000102std::string create_data_ref_profile_package_path(const char* package_name);
103
Jeff Sharkey3dfae0c2016-12-12 17:32:56 -0700104std::string create_data_dalvik_cache_path();
105std::string create_data_misc_foreign_dex_path(userid_t userid);
106
Jeff Sharkey90aff262016-12-12 14:28:24 -0700107std::string create_primary_profile(const std::string& profile_dir);
108
Andreas Gampe02d0de52015-11-11 20:43:16 -0800109std::vector<userid_t> get_known_users(const char* volume_uuid);
110
Jeff Sharkey3dfae0c2016-12-12 17:32:56 -0700111int calculate_tree_size(const std::string& path, int64_t* size,
Jeff Sharkeydf2d7542017-01-07 09:19:35 -0700112 int32_t include_gid = -1, int32_t exclude_gid = -1, bool exclude_apps = false);
Jeff Sharkey3dfae0c2016-12-12 17:32:56 -0700113
Andreas Gampe02d0de52015-11-11 20:43:16 -0800114int create_user_config_path(char path[PKG_PATH_MAX], userid_t userid);
115
116int create_move_path(char path[PKG_PATH_MAX],
117 const char* pkgname,
118 const char* leaf,
119 userid_t userid);
120
Jeff Sharkey423e7462016-12-09 18:18:43 -0700121bool is_valid_filename(const std::string& name);
122bool is_valid_package_name(const std::string& packageName);
Andreas Gampe02d0de52015-11-11 20:43:16 -0800123
Calin Juravleb06f98a2016-03-28 15:11:01 +0100124int delete_dir_contents(const std::string& pathname, bool ignore_if_missing = false);
125int delete_dir_contents_and_dir(const std::string& pathname, bool ignore_if_missing = false);
Andreas Gampe02d0de52015-11-11 20:43:16 -0800126
127int delete_dir_contents(const char *pathname,
128 int also_delete_dir,
Calin Juravleb06f98a2016-03-28 15:11:01 +0100129 int (*exclusion_predicate)(const char *name, const int is_dir),
130 bool ignore_if_missing = false);
Andreas Gampe02d0de52015-11-11 20:43:16 -0800131
132int delete_dir_contents_fd(int dfd, const char *name);
133
134int copy_dir_files(const char *srcname, const char *dstname, uid_t owner, gid_t group);
135
Andreas Gampe02d0de52015-11-11 20:43:16 -0800136int64_t data_disk_free(const std::string& data_path);
137
138cache_t* start_cache_collection();
139
Jeff Sharkey9a998f42016-07-14 18:16:22 -0600140int get_path_inode(const std::string& path, ino_t *inode);
141
142int write_path_inode(const std::string& parent, const char* name, const char* inode_xattr);
143std::string read_path_inode(const std::string& parent, const char* name, const char* inode_xattr);
144
Jeff Sharkey54e292e2016-05-10 17:21:13 -0600145void add_cache_files(cache_t* cache, const std::string& data_path);
Andreas Gampe02d0de52015-11-11 20:43:16 -0800146
147void clear_cache_files(const std::string& data_path, cache_t* cache, int64_t free_size);
148
149void finish_cache_collection(cache_t* cache);
150
151int validate_system_app_path(const char* path);
152
153int get_path_from_env(dir_rec_t* rec, const char* var);
154
155int get_path_from_string(dir_rec_t* rec, const char* path);
156
157int copy_and_append(dir_rec_t* dst, const dir_rec_t* src, const char* suffix);
158
159int validate_apk_path(const char *path);
160int validate_apk_path_subdirs(const char *path);
161
162int append_and_increment(char** dst, const char* src, size_t* dst_size);
163
164char *build_string2(const char *s1, const char *s2);
165char *build_string3(const char *s1, const char *s2, const char *s3);
166
Andreas Gampe02d0de52015-11-11 20:43:16 -0800167int ensure_config_user_dirs(userid_t userid);
168
169int wait_child(pid_t pid);
170
171} // namespace installd
172} // namespace android
173
174#endif // UTILS_H_