blob: e0847810294d650ec0f171c92d69a90922ed1d7e [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
33namespace android {
34namespace installd {
35
36struct dir_rec_t;
37
38typedef 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
46typedef struct {
47 cache_dir_t* dir;
48 time_t modTime;
49 char name[];
50} cache_file_t;
51
52typedef 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
Jeff Sharkey9a998f42016-07-14 18:16:22 -060065constexpr const char* kXattrInodeCache = "user.inode_cache";
66constexpr const char* kXattrInodeCodeCache = "user.inode_code_cache";
67
Andreas Gampe02d0de52015-11-11 20:43:16 -080068int create_pkg_path(char path[PKG_PATH_MAX],
69 const char *pkgname,
70 const char *postfix,
71 userid_t userid);
72
73std::string create_data_path(const char* volume_uuid);
74
75std::string create_data_app_path(const char* volume_uuid);
Andreas Gampe02d0de52015-11-11 20:43:16 -080076std::string create_data_app_package_path(const char* volume_uuid, const char* package_name);
77
Jeff Sharkey2f720f72016-04-10 20:51:40 -060078std::string create_data_user_ce_path(const char* volume_uuid, userid_t userid);
Andreas Gampe02d0de52015-11-11 20:43:16 -080079std::string create_data_user_de_path(const char* volume_uuid, userid_t userid);
80
Jeff Sharkey2f720f72016-04-10 20:51:40 -060081std::string create_data_user_ce_package_path(const char* volume_uuid,
Andreas Gampe02d0de52015-11-11 20:43:16 -080082 userid_t user, const char* package_name);
Jeff Sharkey2f720f72016-04-10 20:51:40 -060083std::string create_data_user_ce_package_path(const char* volume_uuid,
84 userid_t user, const char* package_name, ino_t ce_data_inode);
Andreas Gampe02d0de52015-11-11 20:43:16 -080085std::string create_data_user_de_package_path(const char* volume_uuid,
86 userid_t user, const char* package_name);
87
88std::string create_data_media_path(const char* volume_uuid, userid_t userid);
89
Jeff Sharkey379a12b2016-04-14 20:45:06 -060090std::string create_data_misc_legacy_path(userid_t userid);
91
Calin Juravle6a1648e2016-02-01 12:12:16 +000092std::string create_data_user_profiles_path(userid_t userid);
93std::string create_data_user_profile_package_path(userid_t user, const char* package_name);
94std::string create_data_ref_profile_package_path(const char* package_name);
95
Andreas Gampe02d0de52015-11-11 20:43:16 -080096std::vector<userid_t> get_known_users(const char* volume_uuid);
97
98int create_user_config_path(char path[PKG_PATH_MAX], userid_t userid);
99
100int create_move_path(char path[PKG_PATH_MAX],
101 const char* pkgname,
102 const char* leaf,
103 userid_t userid);
104
Jeff Sharkey423e7462016-12-09 18:18:43 -0700105bool is_valid_filename(const std::string& name);
106bool is_valid_package_name(const std::string& packageName);
Andreas Gampe02d0de52015-11-11 20:43:16 -0800107
Calin Juravleb06f98a2016-03-28 15:11:01 +0100108int delete_dir_contents(const std::string& pathname, bool ignore_if_missing = false);
109int delete_dir_contents_and_dir(const std::string& pathname, bool ignore_if_missing = false);
Andreas Gampe02d0de52015-11-11 20:43:16 -0800110
111int delete_dir_contents(const char *pathname,
112 int also_delete_dir,
Calin Juravleb06f98a2016-03-28 15:11:01 +0100113 int (*exclusion_predicate)(const char *name, const int is_dir),
114 bool ignore_if_missing = false);
Andreas Gampe02d0de52015-11-11 20:43:16 -0800115
116int delete_dir_contents_fd(int dfd, const char *name);
117
118int copy_dir_files(const char *srcname, const char *dstname, uid_t owner, gid_t group);
119
Andreas Gampe02d0de52015-11-11 20:43:16 -0800120int64_t data_disk_free(const std::string& data_path);
121
122cache_t* start_cache_collection();
123
Jeff Sharkey9a998f42016-07-14 18:16:22 -0600124int get_path_inode(const std::string& path, ino_t *inode);
125
126int write_path_inode(const std::string& parent, const char* name, const char* inode_xattr);
127std::string read_path_inode(const std::string& parent, const char* name, const char* inode_xattr);
128
Jeff Sharkey54e292e2016-05-10 17:21:13 -0600129void add_cache_files(cache_t* cache, const std::string& data_path);
Andreas Gampe02d0de52015-11-11 20:43:16 -0800130
131void clear_cache_files(const std::string& data_path, cache_t* cache, int64_t free_size);
132
133void finish_cache_collection(cache_t* cache);
134
135int validate_system_app_path(const char* path);
136
137int get_path_from_env(dir_rec_t* rec, const char* var);
138
139int get_path_from_string(dir_rec_t* rec, const char* path);
140
141int copy_and_append(dir_rec_t* dst, const dir_rec_t* src, const char* suffix);
142
143int validate_apk_path(const char *path);
144int validate_apk_path_subdirs(const char *path);
145
146int append_and_increment(char** dst, const char* src, size_t* dst_size);
147
148char *build_string2(const char *s1, const char *s2);
149char *build_string3(const char *s1, const char *s2, const char *s3);
150
Andreas Gampe02d0de52015-11-11 20:43:16 -0800151int ensure_config_user_dirs(userid_t userid);
152
153int wait_child(pid_t pid);
154
155} // namespace installd
156} // namespace android
157
158#endif // UTILS_H_