• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 namespace android {
34 namespace installd {
35 
36 struct dir_rec_t;
37 
38 typedef struct cache_dir_struct {
39     struct cache_dir_struct* parent;
40     int32_t childCount;
41     int32_t hiddenCount;
42     int32_t deleted;
43     char name[];
44 } cache_dir_t;
45 
46 typedef struct {
47     cache_dir_t* dir;
48     time_t modTime;
49     char name[];
50 } cache_file_t;
51 
52 typedef struct {
53     size_t numDirs;
54     size_t availDirs;
55     cache_dir_t** dirs;
56     size_t numFiles;
57     size_t availFiles;
58     cache_file_t** files;
59     size_t numCollected;
60     void* memBlocks;
61     int8_t* curMemBlockAvail;
62     int8_t* curMemBlockEnd;
63 } cache_t;
64 
65 constexpr const char* kXattrInodeCache = "user.inode_cache";
66 constexpr const char* kXattrInodeCodeCache = "user.inode_code_cache";
67 
68 int create_pkg_path(char path[PKG_PATH_MAX],
69                     const char *pkgname,
70                     const char *postfix,
71                     userid_t userid);
72 
73 std::string create_data_path(const char* volume_uuid);
74 
75 std::string create_data_app_path(const char* volume_uuid);
76 
77 std::string create_data_app_package_path(const char* volume_uuid, const char* package_name);
78 
79 std::string create_data_user_ce_path(const char* volume_uuid, userid_t userid);
80 std::string create_data_user_de_path(const char* volume_uuid, userid_t userid);
81 
82 std::string create_data_user_ce_package_path(const char* volume_uuid,
83         userid_t user, const char* package_name);
84 std::string create_data_user_ce_package_path(const char* volume_uuid,
85         userid_t user, const char* package_name, ino_t ce_data_inode);
86 std::string create_data_user_de_package_path(const char* volume_uuid,
87         userid_t user, const char* package_name);
88 
89 std::string create_data_media_path(const char* volume_uuid, userid_t userid);
90 
91 std::string create_data_misc_legacy_path(userid_t userid);
92 
93 std::string create_data_user_profiles_path(userid_t userid);
94 std::string create_data_user_profile_package_path(userid_t user, const char* package_name);
95 std::string create_data_ref_profile_package_path(const char* package_name);
96 
97 std::vector<userid_t> get_known_users(const char* volume_uuid);
98 
99 int create_user_config_path(char path[PKG_PATH_MAX], userid_t userid);
100 
101 int create_move_path(char path[PKG_PATH_MAX],
102                      const char* pkgname,
103                      const char* leaf,
104                      userid_t userid);
105 
106 int is_valid_package_name(const char* pkgname);
107 
108 int delete_dir_contents(const std::string& pathname, bool ignore_if_missing = false);
109 int delete_dir_contents_and_dir(const std::string& pathname, bool ignore_if_missing = false);
110 
111 int delete_dir_contents(const char *pathname,
112                         int also_delete_dir,
113                         int (*exclusion_predicate)(const char *name, const int is_dir),
114                         bool ignore_if_missing = false);
115 
116 int delete_dir_contents_fd(int dfd, const char *name);
117 
118 int copy_dir_files(const char *srcname, const char *dstname, uid_t owner, gid_t group);
119 
120 int64_t data_disk_free(const std::string& data_path);
121 
122 cache_t* start_cache_collection();
123 
124 int get_path_inode(const std::string& path, ino_t *inode);
125 
126 int write_path_inode(const std::string& parent, const char* name, const char* inode_xattr);
127 std::string read_path_inode(const std::string& parent, const char* name, const char* inode_xattr);
128 
129 void add_cache_files(cache_t* cache, const std::string& data_path);
130 
131 void clear_cache_files(const std::string& data_path, cache_t* cache, int64_t free_size);
132 
133 void finish_cache_collection(cache_t* cache);
134 
135 int validate_system_app_path(const char* path);
136 
137 int get_path_from_env(dir_rec_t* rec, const char* var);
138 
139 int get_path_from_string(dir_rec_t* rec, const char* path);
140 
141 int copy_and_append(dir_rec_t* dst, const dir_rec_t* src, const char* suffix);
142 
143 int validate_apk_path(const char *path);
144 int validate_apk_path_subdirs(const char *path);
145 
146 int append_and_increment(char** dst, const char* src, size_t* dst_size);
147 
148 char *build_string2(const char *s1, const char *s2);
149 char *build_string3(const char *s1, const char *s2, const char *s3);
150 
151 int ensure_config_user_dirs(userid_t userid);
152 
153 int wait_child(pid_t pid);
154 
155 }  // namespace installd
156 }  // namespace android
157 
158 #endif  // UTILS_H_
159