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 int create_pkg_path(char path[PKG_PATH_MAX],
66                     const char *pkgname,
67                     const char *postfix,
68                     userid_t userid);
69 
70 std::string create_data_path(const char* volume_uuid);
71 
72 std::string create_data_app_path(const char* volume_uuid);
73 
74 std::string create_data_app_package_path(const char* volume_uuid, const char* package_name);
75 
76 std::string create_data_user_ce_path(const char* volume_uuid, userid_t userid);
77 std::string create_data_user_de_path(const char* volume_uuid, userid_t userid);
78 
79 std::string create_data_user_ce_package_path(const char* volume_uuid,
80         userid_t user, const char* package_name);
81 std::string create_data_user_ce_package_path(const char* volume_uuid,
82         userid_t user, const char* package_name, ino_t ce_data_inode);
83 std::string create_data_user_de_package_path(const char* volume_uuid,
84         userid_t user, const char* package_name);
85 
86 std::string create_data_media_path(const char* volume_uuid, userid_t userid);
87 
88 std::string create_data_misc_legacy_path(userid_t userid);
89 
90 std::string create_data_user_profiles_path(userid_t userid);
91 std::string create_data_user_profile_package_path(userid_t user, const char* package_name);
92 std::string create_data_ref_profile_package_path(const char* package_name);
93 
94 std::vector<userid_t> get_known_users(const char* volume_uuid);
95 
96 int create_user_config_path(char path[PKG_PATH_MAX], userid_t userid);
97 
98 int create_move_path(char path[PKG_PATH_MAX],
99                      const char* pkgname,
100                      const char* leaf,
101                      userid_t userid);
102 
103 int is_valid_package_name(const char* pkgname);
104 
105 int delete_dir_contents(const std::string& pathname, bool ignore_if_missing = false);
106 int delete_dir_contents_and_dir(const std::string& pathname, bool ignore_if_missing = false);
107 
108 int 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 
113 int delete_dir_contents_fd(int dfd, const char *name);
114 
115 int copy_dir_files(const char *srcname, const char *dstname, uid_t owner, gid_t group);
116 
117 int64_t data_disk_free(const std::string& data_path);
118 
119 cache_t* start_cache_collection();
120 
121 void add_cache_files(cache_t* cache, const std::string& data_path);
122 
123 void clear_cache_files(const std::string& data_path, cache_t* cache, int64_t free_size);
124 
125 void finish_cache_collection(cache_t* cache);
126 
127 int validate_system_app_path(const char* path);
128 
129 int get_path_from_env(dir_rec_t* rec, const char* var);
130 
131 int get_path_from_string(dir_rec_t* rec, const char* path);
132 
133 int copy_and_append(dir_rec_t* dst, const dir_rec_t* src, const char* suffix);
134 
135 int validate_apk_path(const char *path);
136 int validate_apk_path_subdirs(const char *path);
137 
138 int append_and_increment(char** dst, const char* src, size_t* dst_size);
139 
140 char *build_string2(const char *s1, const char *s2);
141 char *build_string3(const char *s1, const char *s2, const char *s3);
142 
143 int ensure_config_user_dirs(userid_t userid);
144 
145 int wait_child(pid_t pid);
146 
147 }  // namespace installd
148 }  // namespace android
149 
150 #endif  // UTILS_H_
151