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
33#define MEASURE_DEBUG 0
34#define FIXUP_DEBUG 0
35
36#define BYPASS_QUOTA 0
37#define BYPASS_SDCARDFS 0
38
39namespace android {
40namespace installd {
41
42struct dir_rec_t;
43
44constexpr const char* kXattrInodeCache = "user.inode_cache";
45constexpr const char* kXattrInodeCodeCache = "user.inode_code_cache";
46constexpr const char* kXattrCacheGroup = "user.cache_group";
47constexpr const char* kXattrCacheTombstone = "user.cache_tombstone";
48
49int create_pkg_path(char path[PKG_PATH_MAX],
50                    const char *pkgname,
51                    const char *postfix,
52                    userid_t userid);
53
54std::string create_data_path(const char* volume_uuid);
55
56std::string create_data_app_path(const char* volume_uuid);
57std::string create_data_app_package_path(const char* volume_uuid, const char* package_name);
58
59std::string create_data_user_ce_path(const char* volume_uuid, userid_t userid);
60std::string create_data_user_de_path(const char* volume_uuid, userid_t userid);
61
62std::string create_data_user_ce_package_path(const char* volume_uuid,
63        userid_t user, const char* package_name);
64std::string create_data_user_ce_package_path(const char* volume_uuid,
65        userid_t user, const char* package_name, ino_t ce_data_inode);
66std::string create_data_user_de_package_path(const char* volume_uuid,
67        userid_t user, const char* package_name);
68
69std::string create_data_media_path(const char* volume_uuid, userid_t userid);
70std::string create_data_media_obb_path(const char* volume_uuid, const char* package_name);
71std::string create_data_media_package_path(const char* volume_uuid, userid_t userid,
72        const char* data_type, const char* package_name);
73
74std::string create_data_misc_legacy_path(userid_t userid);
75
76std::string create_data_dalvik_cache_path();
77
78std::string create_primary_cur_profile_dir_path(userid_t userid);
79std::string create_primary_current_profile_package_dir_path(
80        userid_t user, const std::string& package_name);
81
82std::string create_primary_ref_profile_dir_path();
83std::string create_primary_reference_profile_package_dir_path(const std::string& package_name);
84
85std::string create_current_profile_path(
86        userid_t user, const std::string& package_name, bool is_secondary_dex);
87std::string create_reference_profile_path(
88        const std::string& package_name, bool is_secondary_dex);
89
90std::vector<userid_t> get_known_users(const char* volume_uuid);
91
92int calculate_tree_size(const std::string& path, int64_t* size,
93        int32_t include_gid = -1, int32_t exclude_gid = -1, bool exclude_apps = false);
94
95int create_user_config_path(char path[PKG_PATH_MAX], userid_t userid);
96
97int create_move_path(char path[PKG_PATH_MAX],
98                     const char* pkgname,
99                     const char* leaf,
100                     userid_t userid);
101
102bool is_valid_filename(const std::string& name);
103bool is_valid_package_name(const std::string& packageName);
104
105int delete_dir_contents(const std::string& pathname, bool ignore_if_missing = false);
106int delete_dir_contents_and_dir(const std::string& pathname, bool ignore_if_missing = false);
107
108int delete_dir_contents(const char *pathname,
109                        int also_delete_dir,
110                        int (*exclusion_predicate)(const char *name, const int is_dir),
111                        bool ignore_if_missing = false);
112
113int delete_dir_contents_fd(int dfd, const char *name);
114
115int copy_dir_files(const char *srcname, const char *dstname, uid_t owner, gid_t group);
116
117int64_t data_disk_free(const std::string& data_path);
118
119int get_path_inode(const std::string& path, ino_t *inode);
120
121int write_path_inode(const std::string& parent, const char* name, const char* inode_xattr);
122std::string read_path_inode(const std::string& parent, const char* name, const char* inode_xattr);
123
124int validate_system_app_path(const char* path);
125bool validate_secondary_dex_path(const std::string& pkgname, const std::string& dex_path,
126        const char* volume_uuid, int uid, int storage_flag);
127
128int get_path_from_env(dir_rec_t* rec, const char* var);
129
130int get_path_from_string(dir_rec_t* rec, const char* path);
131
132int copy_and_append(dir_rec_t* dst, const dir_rec_t* src, const char* suffix);
133
134int validate_apk_path(const char *path);
135int validate_apk_path_subdirs(const char *path);
136
137int append_and_increment(char** dst, const char* src, size_t* dst_size);
138
139char *build_string2(const char *s1, const char *s2);
140char *build_string3(const char *s1, const char *s2, const char *s3);
141
142int ensure_config_user_dirs(userid_t userid);
143
144int wait_child(pid_t pid);
145
146int prepare_app_cache_dir(const std::string& parent, const char* name, mode_t target_mode,
147        uid_t uid, gid_t gid);
148
149}  // namespace installd
150}  // namespace android
151
152#endif  // UTILS_H_
153