Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 1 | /* |
| 2 | ** Copyright 2008, The Android Open Source Project |
| 3 | ** |
Dave Allison | d937073 | 2014-01-30 14:19:23 -0800 | [diff] [blame] | 4 | ** Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | ** you may not use this file except in compliance with the License. |
| 6 | ** You may obtain a copy of the License at |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 7 | ** |
Dave Allison | d937073 | 2014-01-30 14:19:23 -0800 | [diff] [blame] | 8 | ** http://www.apache.org/licenses/LICENSE-2.0 |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 9 | ** |
Dave Allison | d937073 | 2014-01-30 14:19:23 -0800 | [diff] [blame] | 10 | ** Unless required by applicable law or agreed to in writing, software |
| 11 | ** distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | ** See the License for the specific language governing permissions and |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 14 | ** limitations under the License. |
| 15 | */ |
| 16 | |
Andreas Gampe | 02d0de5 | 2015-11-11 20:43:16 -0800 | [diff] [blame] | 17 | #include "utils.h" |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 18 | |
Andreas Gampe | 02d0de5 | 2015-11-11 20:43:16 -0800 | [diff] [blame] | 19 | #include <errno.h> |
| 20 | #include <fcntl.h> |
Jeff Sharkey | 3dfae0c | 2016-12-12 17:32:56 -0700 | [diff] [blame] | 21 | #include <fts.h> |
Andreas Gampe | 02d0de5 | 2015-11-11 20:43:16 -0800 | [diff] [blame] | 22 | #include <stdlib.h> |
| 23 | #include <sys/stat.h> |
| 24 | #include <sys/wait.h> |
Jeff Sharkey | 9a998f4 | 2016-07-14 18:16:22 -0600 | [diff] [blame] | 25 | #include <sys/xattr.h> |
Jeff Sharkey | ed909ae | 2017-03-22 21:27:40 -0600 | [diff] [blame] | 26 | #include <sys/statvfs.h> |
Andreas Gampe | 02d0de5 | 2015-11-11 20:43:16 -0800 | [diff] [blame] | 27 | |
Elliott Hughes | e4ec9eb | 2015-12-04 15:39:32 -0800 | [diff] [blame] | 28 | #include <android-base/logging.h> |
Calin Juravle | cfcd6aa | 2018-01-18 20:23:17 -0800 | [diff] [blame] | 29 | #include <android-base/strings.h> |
Andreas Gampe | 02d0de5 | 2015-11-11 20:43:16 -0800 | [diff] [blame] | 30 | #include <android-base/stringprintf.h> |
Calin Juravle | e61189e | 2018-01-23 19:54:11 -0800 | [diff] [blame] | 31 | #include <android-base/unique_fd.h> |
Andreas Gampe | 02d0de5 | 2015-11-11 20:43:16 -0800 | [diff] [blame] | 32 | #include <cutils/fs.h> |
Jeff Sharkey | 871a8f2 | 2017-02-21 18:30:28 -0700 | [diff] [blame] | 33 | #include <cutils/properties.h> |
Mark Salyzyn | 7823e12 | 2016-09-29 08:08:05 -0700 | [diff] [blame] | 34 | #include <log/log.h> |
Andreas Gampe | 02d0de5 | 2015-11-11 20:43:16 -0800 | [diff] [blame] | 35 | #include <private/android_filesystem_config.h> |
Jeff Sharkey | c03de09 | 2015-04-07 18:14:05 -0700 | [diff] [blame] | 36 | |
Andreas Gampe | 02d0de5 | 2015-11-11 20:43:16 -0800 | [diff] [blame] | 37 | #include "globals.h" // extern variables. |
| 38 | |
| 39 | #ifndef LOG_TAG |
| 40 | #define LOG_TAG "installd" |
| 41 | #endif |
Jeff Sharkey | 9a998f4 | 2016-07-14 18:16:22 -0600 | [diff] [blame] | 42 | |
Jeff Sharkey | 9a998f4 | 2016-07-14 18:16:22 -0600 | [diff] [blame] | 43 | #define DEBUG_XATTRS 0 |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 44 | |
Calin Juravle | e61189e | 2018-01-23 19:54:11 -0800 | [diff] [blame] | 45 | using android::base::EndsWith; |
Jeff Sharkey | c03de09 | 2015-04-07 18:14:05 -0700 | [diff] [blame] | 46 | using android::base::StringPrintf; |
Calin Juravle | e61189e | 2018-01-23 19:54:11 -0800 | [diff] [blame] | 47 | using android::base::unique_fd; |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 48 | |
Andreas Gampe | 02d0de5 | 2015-11-11 20:43:16 -0800 | [diff] [blame] | 49 | namespace android { |
| 50 | namespace installd { |
| 51 | |
Jeff Sharkey | c03de09 | 2015-04-07 18:14:05 -0700 | [diff] [blame] | 52 | /** |
| 53 | * Check that given string is valid filename, and that it attempts no |
| 54 | * parent or child directory traversal. |
| 55 | */ |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 56 | bool is_valid_filename(const std::string& name) { |
Jeff Sharkey | c03de09 | 2015-04-07 18:14:05 -0700 | [diff] [blame] | 57 | if (name.empty() || (name == ".") || (name == "..") |
| 58 | || (name.find('/') != std::string::npos)) { |
| 59 | return false; |
| 60 | } else { |
| 61 | return true; |
| 62 | } |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 63 | } |
| 64 | |
Calin Juravle | 6a1648e | 2016-02-01 12:12:16 +0000 | [diff] [blame] | 65 | static void check_package_name(const char* package_name) { |
| 66 | CHECK(is_valid_filename(package_name)); |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 67 | CHECK(is_valid_package_name(package_name)); |
Calin Juravle | 6a1648e | 2016-02-01 12:12:16 +0000 | [diff] [blame] | 68 | } |
| 69 | |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 70 | /** |
Jeff Sharkey | d792118 | 2015-04-30 15:58:19 -0700 | [diff] [blame] | 71 | * Create the path name where package app contents should be stored for |
| 72 | * the given volume UUID and package name. An empty UUID is assumed to |
| 73 | * be internal storage. |
| 74 | */ |
| 75 | std::string create_data_app_package_path(const char* volume_uuid, |
| 76 | const char* package_name) { |
Calin Juravle | 6a1648e | 2016-02-01 12:12:16 +0000 | [diff] [blame] | 77 | check_package_name(package_name); |
Jeff Sharkey | d792118 | 2015-04-30 15:58:19 -0700 | [diff] [blame] | 78 | return StringPrintf("%s/%s", |
| 79 | create_data_app_path(volume_uuid).c_str(), package_name); |
| 80 | } |
| 81 | |
| 82 | /** |
Jeff Sharkey | c03de09 | 2015-04-07 18:14:05 -0700 | [diff] [blame] | 83 | * Create the path name where package data should be stored for the given |
| 84 | * volume UUID, package name, and user ID. An empty UUID is assumed to be |
| 85 | * internal storage. |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 86 | */ |
Jeff Sharkey | 2f720f7 | 2016-04-10 20:51:40 -0600 | [diff] [blame] | 87 | std::string create_data_user_ce_package_path(const char* volume_uuid, |
Jeff Sharkey | d792118 | 2015-04-30 15:58:19 -0700 | [diff] [blame] | 88 | userid_t user, const char* package_name) { |
Calin Juravle | 6a1648e | 2016-02-01 12:12:16 +0000 | [diff] [blame] | 89 | check_package_name(package_name); |
Jeff Sharkey | d792118 | 2015-04-30 15:58:19 -0700 | [diff] [blame] | 90 | return StringPrintf("%s/%s", |
Jeff Sharkey | 2f720f7 | 2016-04-10 20:51:40 -0600 | [diff] [blame] | 91 | create_data_user_ce_path(volume_uuid, user).c_str(), package_name); |
| 92 | } |
| 93 | |
Calin Juravle | 7d76546 | 2017-09-04 15:57:10 -0700 | [diff] [blame] | 94 | /** |
| 95 | * Create the path name where package data should be stored for the given |
| 96 | * volume UUID, package name, and user ID. An empty UUID is assumed to be |
| 97 | * internal storage. |
| 98 | * Compared to create_data_user_ce_package_path this method always return the |
| 99 | * ".../user/..." directory. |
| 100 | */ |
| 101 | std::string create_data_user_ce_package_path_as_user_link( |
| 102 | const char* volume_uuid, userid_t userid, const char* package_name) { |
| 103 | check_package_name(package_name); |
| 104 | std::string data(create_data_path(volume_uuid)); |
| 105 | return StringPrintf("%s/user/%u/%s", data.c_str(), userid, package_name); |
| 106 | } |
| 107 | |
Jeff Sharkey | 2f720f7 | 2016-04-10 20:51:40 -0600 | [diff] [blame] | 108 | std::string create_data_user_ce_package_path(const char* volume_uuid, userid_t user, |
| 109 | const char* package_name, ino_t ce_data_inode) { |
| 110 | // For testing purposes, rely on the inode when defined; this could be |
| 111 | // optimized to use access() in the future. |
| 112 | auto fallback = create_data_user_ce_package_path(volume_uuid, user, package_name); |
| 113 | if (ce_data_inode != 0) { |
| 114 | auto user_path = create_data_user_ce_path(volume_uuid, user); |
| 115 | DIR* dir = opendir(user_path.c_str()); |
| 116 | if (dir == nullptr) { |
| 117 | PLOG(ERROR) << "Failed to opendir " << user_path; |
| 118 | return fallback; |
| 119 | } |
| 120 | |
| 121 | struct dirent* ent; |
| 122 | while ((ent = readdir(dir))) { |
| 123 | if (ent->d_ino == ce_data_inode) { |
Jeff Sharkey | 1d992f9 | 2016-04-13 13:45:47 -0600 | [diff] [blame] | 124 | auto resolved = StringPrintf("%s/%s", user_path.c_str(), ent->d_name); |
Jeff Sharkey | 9a998f4 | 2016-07-14 18:16:22 -0600 | [diff] [blame] | 125 | #if DEBUG_XATTRS |
Jeff Sharkey | 1d992f9 | 2016-04-13 13:45:47 -0600 | [diff] [blame] | 126 | if (resolved != fallback) { |
| 127 | LOG(DEBUG) << "Resolved path " << resolved << " for inode " << ce_data_inode |
| 128 | << " instead of " << fallback; |
| 129 | } |
Jeff Sharkey | 9a998f4 | 2016-07-14 18:16:22 -0600 | [diff] [blame] | 130 | #endif |
Jeff Sharkey | 2f720f7 | 2016-04-10 20:51:40 -0600 | [diff] [blame] | 131 | closedir(dir); |
Jeff Sharkey | 1d992f9 | 2016-04-13 13:45:47 -0600 | [diff] [blame] | 132 | return resolved; |
Jeff Sharkey | 2f720f7 | 2016-04-10 20:51:40 -0600 | [diff] [blame] | 133 | } |
| 134 | } |
Jeff Sharkey | 1d992f9 | 2016-04-13 13:45:47 -0600 | [diff] [blame] | 135 | LOG(WARNING) << "Failed to resolve inode " << ce_data_inode << "; using " << fallback; |
Jeff Sharkey | 2f720f7 | 2016-04-10 20:51:40 -0600 | [diff] [blame] | 136 | closedir(dir); |
| 137 | return fallback; |
| 138 | } else { |
| 139 | return fallback; |
| 140 | } |
Jeff Sharkey | c03de09 | 2015-04-07 18:14:05 -0700 | [diff] [blame] | 141 | } |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 142 | |
Jeff Sharkey | 63ec2d6 | 2015-11-09 13:10:36 -0800 | [diff] [blame] | 143 | std::string create_data_user_de_package_path(const char* volume_uuid, |
| 144 | userid_t user, const char* package_name) { |
Calin Juravle | 6a1648e | 2016-02-01 12:12:16 +0000 | [diff] [blame] | 145 | check_package_name(package_name); |
Jeff Sharkey | 63ec2d6 | 2015-11-09 13:10:36 -0800 | [diff] [blame] | 146 | return StringPrintf("%s/%s", |
| 147 | create_data_user_de_path(volume_uuid, user).c_str(), package_name); |
| 148 | } |
| 149 | |
Jeff Sharkey | 41ea424 | 2015-04-09 11:34:03 -0700 | [diff] [blame] | 150 | std::string create_data_path(const char* volume_uuid) { |
| 151 | if (volume_uuid == nullptr) { |
| 152 | return "/data"; |
Jeff Sharkey | 871a8f2 | 2017-02-21 18:30:28 -0700 | [diff] [blame] | 153 | } else if (!strcmp(volume_uuid, "TEST")) { |
| 154 | CHECK(property_get_bool("ro.debuggable", false)); |
| 155 | return "/data/local/tmp"; |
Jeff Sharkey | 41ea424 | 2015-04-09 11:34:03 -0700 | [diff] [blame] | 156 | } else { |
| 157 | CHECK(is_valid_filename(volume_uuid)); |
| 158 | return StringPrintf("/mnt/expand/%s", volume_uuid); |
| 159 | } |
| 160 | } |
| 161 | |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 162 | /** |
Jeff Sharkey | d792118 | 2015-04-30 15:58:19 -0700 | [diff] [blame] | 163 | * Create the path name for app data. |
| 164 | */ |
| 165 | std::string create_data_app_path(const char* volume_uuid) { |
| 166 | return StringPrintf("%s/app", create_data_path(volume_uuid).c_str()); |
| 167 | } |
| 168 | |
| 169 | /** |
Jeff Sharkey | abe4fe5 | 2013-07-10 16:55:46 -0700 | [diff] [blame] | 170 | * Create the path name for user data for a certain userid. |
cjbao | 75d4e57 | 2017-04-12 00:12:24 +0800 | [diff] [blame] | 171 | * Keep same implementation as vold to minimize path walking overhead |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 172 | */ |
Jeff Sharkey | 2f720f7 | 2016-04-10 20:51:40 -0600 | [diff] [blame] | 173 | std::string create_data_user_ce_path(const char* volume_uuid, userid_t userid) { |
Jeff Sharkey | 41ea424 | 2015-04-09 11:34:03 -0700 | [diff] [blame] | 174 | std::string data(create_data_path(volume_uuid)); |
cjbao | 75d4e57 | 2017-04-12 00:12:24 +0800 | [diff] [blame] | 175 | if (volume_uuid == nullptr && userid == 0) { |
| 176 | std::string legacy = StringPrintf("%s/data", data.c_str()); |
| 177 | struct stat sb; |
| 178 | if (lstat(legacy.c_str(), &sb) == 0 && S_ISDIR(sb.st_mode)) { |
| 179 | /* /data/data is dir, return /data/data for legacy system */ |
| 180 | return legacy; |
Jeff Sharkey | 41ea424 | 2015-04-09 11:34:03 -0700 | [diff] [blame] | 181 | } |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 182 | } |
cjbao | 75d4e57 | 2017-04-12 00:12:24 +0800 | [diff] [blame] | 183 | return StringPrintf("%s/user/%u", data.c_str(), userid); |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | /** |
Jeff Sharkey | 63ec2d6 | 2015-11-09 13:10:36 -0800 | [diff] [blame] | 187 | * Create the path name for device encrypted user data for a certain userid. |
| 188 | */ |
| 189 | std::string create_data_user_de_path(const char* volume_uuid, userid_t userid) { |
| 190 | std::string data(create_data_path(volume_uuid)); |
| 191 | return StringPrintf("%s/user_de/%u", data.c_str(), userid); |
| 192 | } |
| 193 | |
| 194 | /** |
Jeff Sharkey | abe4fe5 | 2013-07-10 16:55:46 -0700 | [diff] [blame] | 195 | * Create the path name for media for a certain userid. |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 196 | */ |
Jeff Sharkey | 41ea424 | 2015-04-09 11:34:03 -0700 | [diff] [blame] | 197 | std::string create_data_media_path(const char* volume_uuid, userid_t userid) { |
| 198 | return StringPrintf("%s/media/%u", create_data_path(volume_uuid).c_str(), userid); |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 199 | } |
| 200 | |
Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 201 | std::string create_data_media_obb_path(const char* volume_uuid, const char* package_name) { |
| 202 | return StringPrintf("%s/media/obb/%s", create_data_path(volume_uuid).c_str(), package_name); |
| 203 | } |
| 204 | |
Jeff Sharkey | 3dfae0c | 2016-12-12 17:32:56 -0700 | [diff] [blame] | 205 | std::string create_data_media_package_path(const char* volume_uuid, userid_t userid, |
| 206 | const char* data_type, const char* package_name) { |
| 207 | return StringPrintf("%s/Android/%s/%s", create_data_media_path(volume_uuid, userid).c_str(), |
| 208 | data_type, package_name); |
| 209 | } |
| 210 | |
Jeff Sharkey | 379a12b | 2016-04-14 20:45:06 -0600 | [diff] [blame] | 211 | std::string create_data_misc_legacy_path(userid_t userid) { |
| 212 | return StringPrintf("%s/misc/user/%u", create_data_path(nullptr).c_str(), userid); |
| 213 | } |
| 214 | |
Calin Juravle | 114f081 | 2017-03-08 19:05:07 -0800 | [diff] [blame] | 215 | std::string create_primary_cur_profile_dir_path(userid_t userid) { |
Jeff Sharkey | c1149c9 | 2017-09-21 14:51:09 -0600 | [diff] [blame] | 216 | return StringPrintf("%s/cur/%u", android_profiles_dir.c_str(), userid); |
Calin Juravle | 6a1648e | 2016-02-01 12:12:16 +0000 | [diff] [blame] | 217 | } |
| 218 | |
Calin Juravle | 114f081 | 2017-03-08 19:05:07 -0800 | [diff] [blame] | 219 | std::string create_primary_current_profile_package_dir_path(userid_t user, |
| 220 | const std::string& package_name) { |
Calin Juravle | 76268c5 | 2017-03-09 13:19:42 -0800 | [diff] [blame] | 221 | check_package_name(package_name.c_str()); |
Calin Juravle | 114f081 | 2017-03-08 19:05:07 -0800 | [diff] [blame] | 222 | return StringPrintf("%s/%s", |
| 223 | create_primary_cur_profile_dir_path(user).c_str(), package_name.c_str()); |
Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 224 | } |
| 225 | |
Calin Juravle | 114f081 | 2017-03-08 19:05:07 -0800 | [diff] [blame] | 226 | std::string create_primary_ref_profile_dir_path() { |
Jeff Sharkey | c1149c9 | 2017-09-21 14:51:09 -0600 | [diff] [blame] | 227 | return StringPrintf("%s/ref", android_profiles_dir.c_str()); |
Calin Juravle | 6a1648e | 2016-02-01 12:12:16 +0000 | [diff] [blame] | 228 | } |
| 229 | |
Calin Juravle | 114f081 | 2017-03-08 19:05:07 -0800 | [diff] [blame] | 230 | std::string create_primary_reference_profile_package_dir_path(const std::string& package_name) { |
Calin Juravle | 76268c5 | 2017-03-09 13:19:42 -0800 | [diff] [blame] | 231 | check_package_name(package_name.c_str()); |
Jeff Sharkey | c1149c9 | 2017-09-21 14:51:09 -0600 | [diff] [blame] | 232 | return StringPrintf("%s/ref/%s", android_profiles_dir.c_str(), package_name.c_str()); |
Calin Juravle | 6a1648e | 2016-02-01 12:12:16 +0000 | [diff] [blame] | 233 | } |
| 234 | |
Jeff Sharkey | 3dfae0c | 2016-12-12 17:32:56 -0700 | [diff] [blame] | 235 | std::string create_data_dalvik_cache_path() { |
| 236 | return "/data/dalvik-cache"; |
| 237 | } |
| 238 | |
Calin Juravle | 114f081 | 2017-03-08 19:05:07 -0800 | [diff] [blame] | 239 | // Keep profile paths in sync with ActivityThread and LoadedApk. |
| 240 | const std::string PROFILE_EXT = ".prof"; |
Calin Juravle | 3760ad3 | 2017-07-27 16:31:55 -0700 | [diff] [blame] | 241 | const std::string CURRENT_PROFILE_EXT = ".cur"; |
Calin Juravle | 2959173 | 2017-11-20 17:46:19 -0800 | [diff] [blame] | 242 | const std::string SNAPSHOT_PROFILE_EXT = ".snapshot"; |
Jeff Sharkey | 90aff26 | 2016-12-12 14:28:24 -0700 | [diff] [blame] | 243 | |
Calin Juravle | 3760ad3 | 2017-07-27 16:31:55 -0700 | [diff] [blame] | 244 | // Gets the parent directory and the file name for the given secondary dex path. |
| 245 | // Returns true on success, false on failure (if the dex_path does not have the expected |
| 246 | // structure). |
| 247 | static bool get_secondary_dex_location(const std::string& dex_path, |
| 248 | std::string* out_dir_name, std::string* out_file_name) { |
| 249 | size_t dirIndex = dex_path.rfind('/'); |
| 250 | if (dirIndex == std::string::npos) { |
| 251 | return false; |
| 252 | } |
| 253 | if (dirIndex == dex_path.size() - 1) { |
| 254 | return false; |
| 255 | } |
| 256 | *out_dir_name = dex_path.substr(0, dirIndex); |
| 257 | *out_file_name = dex_path.substr(dirIndex + 1); |
| 258 | |
| 259 | return true; |
| 260 | } |
| 261 | |
Calin Juravle | cfcd6aa | 2018-01-18 20:23:17 -0800 | [diff] [blame] | 262 | std::string create_current_profile_path(userid_t user, const std::string& package_name, |
| 263 | const std::string& location, bool is_secondary_dex) { |
Calin Juravle | 114f081 | 2017-03-08 19:05:07 -0800 | [diff] [blame] | 264 | if (is_secondary_dex) { |
Calin Juravle | 3760ad3 | 2017-07-27 16:31:55 -0700 | [diff] [blame] | 265 | // Secondary dex current profiles are stored next to the dex files under the oat folder. |
| 266 | std::string dex_dir; |
| 267 | std::string dex_name; |
| 268 | CHECK(get_secondary_dex_location(location, &dex_dir, &dex_name)) |
| 269 | << "Unexpected dir structure for secondary dex " << location; |
| 270 | return StringPrintf("%s/oat/%s%s%s", |
| 271 | dex_dir.c_str(), dex_name.c_str(), CURRENT_PROFILE_EXT.c_str(), |
| 272 | PROFILE_EXT.c_str()); |
Calin Juravle | 114f081 | 2017-03-08 19:05:07 -0800 | [diff] [blame] | 273 | } else { |
| 274 | // Profiles for primary apks are under /data/misc/profiles/cur. |
Calin Juravle | cfcd6aa | 2018-01-18 20:23:17 -0800 | [diff] [blame] | 275 | std::string profile_dir = create_primary_current_profile_package_dir_path( |
| 276 | user, package_name); |
| 277 | return StringPrintf("%s/%s", profile_dir.c_str(), location.c_str()); |
Calin Juravle | 114f081 | 2017-03-08 19:05:07 -0800 | [diff] [blame] | 278 | } |
| 279 | } |
| 280 | |
Calin Juravle | cfcd6aa | 2018-01-18 20:23:17 -0800 | [diff] [blame] | 281 | std::string create_reference_profile_path(const std::string& package_name, |
| 282 | const std::string& location, bool is_secondary_dex) { |
Calin Juravle | 114f081 | 2017-03-08 19:05:07 -0800 | [diff] [blame] | 283 | if (is_secondary_dex) { |
| 284 | // Secondary dex reference profiles are stored next to the dex files under the oat folder. |
Calin Juravle | 3760ad3 | 2017-07-27 16:31:55 -0700 | [diff] [blame] | 285 | std::string dex_dir; |
| 286 | std::string dex_name; |
| 287 | CHECK(get_secondary_dex_location(location, &dex_dir, &dex_name)) |
Calin Juravle | 114f081 | 2017-03-08 19:05:07 -0800 | [diff] [blame] | 288 | << "Unexpected dir structure for secondary dex " << location; |
Calin Juravle | 114f081 | 2017-03-08 19:05:07 -0800 | [diff] [blame] | 289 | return StringPrintf("%s/oat/%s%s", |
| 290 | dex_dir.c_str(), dex_name.c_str(), PROFILE_EXT.c_str()); |
| 291 | } else { |
| 292 | // Reference profiles for primary apks are stored in /data/misc/profile/ref. |
Calin Juravle | cfcd6aa | 2018-01-18 20:23:17 -0800 | [diff] [blame] | 293 | std::string profile_dir = create_primary_reference_profile_package_dir_path(package_name); |
| 294 | return StringPrintf("%s/%s", profile_dir.c_str(), location.c_str()); |
Calin Juravle | 114f081 | 2017-03-08 19:05:07 -0800 | [diff] [blame] | 295 | } |
Jeff Sharkey | 90aff26 | 2016-12-12 14:28:24 -0700 | [diff] [blame] | 296 | } |
| 297 | |
Calin Juravle | 2959173 | 2017-11-20 17:46:19 -0800 | [diff] [blame] | 298 | std::string create_snapshot_profile_path(const std::string& package, |
Calin Juravle | cfcd6aa | 2018-01-18 20:23:17 -0800 | [diff] [blame] | 299 | const std::string& profile_name) { |
| 300 | std::string ref_profile = create_reference_profile_path(package, profile_name, |
| 301 | /*is_secondary_dex*/ false); |
Calin Juravle | 2959173 | 2017-11-20 17:46:19 -0800 | [diff] [blame] | 302 | return ref_profile + SNAPSHOT_PROFILE_EXT; |
| 303 | } |
| 304 | |
Jeff Sharkey | e363724 | 2015-04-08 20:56:42 -0700 | [diff] [blame] | 305 | std::vector<userid_t> get_known_users(const char* volume_uuid) { |
| 306 | std::vector<userid_t> users; |
| 307 | |
| 308 | // We always have an owner |
| 309 | users.push_back(0); |
| 310 | |
| 311 | std::string path(create_data_path(volume_uuid) + "/" + SECONDARY_USER_PREFIX); |
| 312 | DIR* dir = opendir(path.c_str()); |
| 313 | if (dir == NULL) { |
| 314 | // Unable to discover other users, but at least return owner |
| 315 | PLOG(ERROR) << "Failed to opendir " << path; |
| 316 | return users; |
| 317 | } |
| 318 | |
| 319 | struct dirent* ent; |
| 320 | while ((ent = readdir(dir))) { |
| 321 | if (ent->d_type != DT_DIR) { |
| 322 | continue; |
| 323 | } |
| 324 | |
| 325 | char* end; |
| 326 | userid_t user = strtol(ent->d_name, &end, 10); |
| 327 | if (*end == '\0' && user != 0) { |
| 328 | LOG(DEBUG) << "Found valid user " << user; |
| 329 | users.push_back(user); |
| 330 | } |
| 331 | } |
| 332 | closedir(dir); |
| 333 | |
| 334 | return users; |
| 335 | } |
| 336 | |
Jeff Sharkey | 3dfae0c | 2016-12-12 17:32:56 -0700 | [diff] [blame] | 337 | int calculate_tree_size(const std::string& path, int64_t* size, |
Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 338 | int32_t include_gid, int32_t exclude_gid, bool exclude_apps) { |
Jeff Sharkey | 3dfae0c | 2016-12-12 17:32:56 -0700 | [diff] [blame] | 339 | FTS *fts; |
| 340 | FTSENT *p; |
Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 341 | int64_t matchedSize = 0; |
Jeff Sharkey | 3dfae0c | 2016-12-12 17:32:56 -0700 | [diff] [blame] | 342 | char *argv[] = { (char*) path.c_str(), nullptr }; |
Jeff Sharkey | b26786d | 2017-03-11 19:40:29 -0700 | [diff] [blame] | 343 | if (!(fts = fts_open(argv, FTS_PHYSICAL | FTS_NOCHDIR | FTS_XDEV, NULL))) { |
Jeff Sharkey | 3dfae0c | 2016-12-12 17:32:56 -0700 | [diff] [blame] | 344 | if (errno != ENOENT) { |
| 345 | PLOG(ERROR) << "Failed to fts_open " << path; |
| 346 | } |
| 347 | return -1; |
| 348 | } |
| 349 | while ((p = fts_read(fts)) != NULL) { |
| 350 | switch (p->fts_info) { |
| 351 | case FTS_D: |
| 352 | case FTS_DEFAULT: |
| 353 | case FTS_F: |
| 354 | case FTS_SL: |
| 355 | case FTS_SLNONE: |
Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 356 | int32_t uid = p->fts_statp->st_uid; |
| 357 | int32_t gid = p->fts_statp->st_gid; |
| 358 | int32_t user_uid = multiuser_get_app_id(uid); |
| 359 | int32_t user_gid = multiuser_get_app_id(gid); |
| 360 | if (exclude_apps && ((user_uid >= AID_APP_START && user_uid <= AID_APP_END) |
| 361 | || (user_gid >= AID_CACHE_GID_START && user_gid <= AID_CACHE_GID_END) |
| 362 | || (user_gid >= AID_SHARED_GID_START && user_gid <= AID_SHARED_GID_END))) { |
| 363 | // Don't traverse inside or measure |
| 364 | fts_set(fts, p, FTS_SKIP); |
Jeff Sharkey | 3dfae0c | 2016-12-12 17:32:56 -0700 | [diff] [blame] | 365 | break; |
| 366 | } |
Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 367 | if (include_gid != -1 && gid != include_gid) { |
Jeff Sharkey | 3dfae0c | 2016-12-12 17:32:56 -0700 | [diff] [blame] | 368 | break; |
| 369 | } |
Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 370 | if (exclude_gid != -1 && gid == exclude_gid) { |
| 371 | break; |
| 372 | } |
| 373 | matchedSize += (p->fts_statp->st_blocks * 512); |
Jeff Sharkey | 3dfae0c | 2016-12-12 17:32:56 -0700 | [diff] [blame] | 374 | break; |
| 375 | } |
| 376 | } |
| 377 | fts_close(fts); |
Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 378 | #if MEASURE_DEBUG |
| 379 | if ((include_gid == -1) && (exclude_gid == -1)) { |
| 380 | LOG(DEBUG) << "Measured " << path << " size " << matchedSize; |
| 381 | } else { |
| 382 | LOG(DEBUG) << "Measured " << path << " size " << matchedSize << "; include " << include_gid |
| 383 | << " exclude " << exclude_gid; |
| 384 | } |
| 385 | #endif |
| 386 | *size += matchedSize; |
Jeff Sharkey | 3dfae0c | 2016-12-12 17:32:56 -0700 | [diff] [blame] | 387 | return 0; |
| 388 | } |
| 389 | |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 390 | /** |
| 391 | * Checks whether the package name is valid. Returns -1 on error and |
| 392 | * 0 on success. |
| 393 | */ |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 394 | bool is_valid_package_name(const std::string& packageName) { |
Jeff Sharkey | 367ace2 | 2017-03-07 22:12:03 -0700 | [diff] [blame] | 395 | // This logic is borrowed from PackageParser.java |
| 396 | bool hasSep = false; |
| 397 | bool front = true; |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 398 | |
Jeff Sharkey | 367ace2 | 2017-03-07 22:12:03 -0700 | [diff] [blame] | 399 | auto it = packageName.begin(); |
| 400 | for (; it != packageName.end() && *it != '-'; it++) { |
| 401 | char c = *it; |
| 402 | if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) { |
| 403 | front = false; |
| 404 | continue; |
| 405 | } |
| 406 | if (!front) { |
| 407 | if ((c >= '0' && c <= '9') || c == '_') { |
| 408 | continue; |
| 409 | } |
| 410 | } |
| 411 | if (c == '.') { |
| 412 | hasSep = true; |
| 413 | front = true; |
| 414 | continue; |
| 415 | } |
| 416 | LOG(WARNING) << "Bad package character " << c << " in " << packageName; |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 417 | return false; |
Jeff Sharkey | c03de09 | 2015-04-07 18:14:05 -0700 | [diff] [blame] | 418 | } |
| 419 | |
Jeff Sharkey | ab7ac8d | 2017-03-08 12:39:46 -0700 | [diff] [blame] | 420 | if (front) { |
Jeff Sharkey | 367ace2 | 2017-03-07 22:12:03 -0700 | [diff] [blame] | 421 | LOG(WARNING) << "Missing separator in " << packageName; |
| 422 | return false; |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 423 | } |
| 424 | |
Jeff Sharkey | 367ace2 | 2017-03-07 22:12:03 -0700 | [diff] [blame] | 425 | for (; it != packageName.end(); it++) { |
| 426 | char c = *it; |
| 427 | if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) continue; |
| 428 | if ((c >= '0' && c <= '9') || c == '_' || c == '-' || c == '=') continue; |
| 429 | LOG(WARNING) << "Bad suffix character " << c << " in " << packageName; |
| 430 | return false; |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 431 | } |
| 432 | |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 433 | return true; |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 434 | } |
| 435 | |
Narayan Kamath | 3aee2c5 | 2014-06-10 13:16:47 +0100 | [diff] [blame] | 436 | static int _delete_dir_contents(DIR *d, |
| 437 | int (*exclusion_predicate)(const char *name, const int is_dir)) |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 438 | { |
| 439 | int result = 0; |
| 440 | struct dirent *de; |
| 441 | int dfd; |
| 442 | |
| 443 | dfd = dirfd(d); |
| 444 | |
| 445 | if (dfd < 0) return -1; |
| 446 | |
| 447 | while ((de = readdir(d))) { |
| 448 | const char *name = de->d_name; |
| 449 | |
Narayan Kamath | 3aee2c5 | 2014-06-10 13:16:47 +0100 | [diff] [blame] | 450 | /* check using the exclusion predicate, if provided */ |
| 451 | if (exclusion_predicate && exclusion_predicate(name, (de->d_type == DT_DIR))) { |
| 452 | continue; |
| 453 | } |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 454 | |
| 455 | if (de->d_type == DT_DIR) { |
Chih-Hung Hsieh | 99d9fb1 | 2014-09-11 14:44:46 -0700 | [diff] [blame] | 456 | int subfd; |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 457 | DIR *subdir; |
| 458 | |
| 459 | /* always skip "." and ".." */ |
| 460 | if (name[0] == '.') { |
| 461 | if (name[1] == 0) continue; |
| 462 | if ((name[1] == '.') && (name[2] == 0)) continue; |
| 463 | } |
| 464 | |
Nick Kralevich | 8b7acac | 2015-08-10 13:43:00 -0700 | [diff] [blame] | 465 | subfd = openat(dfd, name, O_RDONLY | O_DIRECTORY | O_NOFOLLOW | O_CLOEXEC); |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 466 | if (subfd < 0) { |
| 467 | ALOGE("Couldn't openat %s: %s\n", name, strerror(errno)); |
| 468 | result = -1; |
| 469 | continue; |
| 470 | } |
| 471 | subdir = fdopendir(subfd); |
| 472 | if (subdir == NULL) { |
| 473 | ALOGE("Couldn't fdopendir %s: %s\n", name, strerror(errno)); |
| 474 | close(subfd); |
| 475 | result = -1; |
| 476 | continue; |
| 477 | } |
Narayan Kamath | 3aee2c5 | 2014-06-10 13:16:47 +0100 | [diff] [blame] | 478 | if (_delete_dir_contents(subdir, exclusion_predicate)) { |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 479 | result = -1; |
| 480 | } |
| 481 | closedir(subdir); |
| 482 | if (unlinkat(dfd, name, AT_REMOVEDIR) < 0) { |
| 483 | ALOGE("Couldn't unlinkat %s: %s\n", name, strerror(errno)); |
| 484 | result = -1; |
| 485 | } |
| 486 | } else { |
| 487 | if (unlinkat(dfd, name, 0) < 0) { |
| 488 | ALOGE("Couldn't unlinkat %s: %s\n", name, strerror(errno)); |
| 489 | result = -1; |
| 490 | } |
| 491 | } |
| 492 | } |
| 493 | |
| 494 | return result; |
| 495 | } |
| 496 | |
Calin Juravle | b06f98a | 2016-03-28 15:11:01 +0100 | [diff] [blame] | 497 | int delete_dir_contents(const std::string& pathname, bool ignore_if_missing) { |
| 498 | return delete_dir_contents(pathname.c_str(), 0, NULL, ignore_if_missing); |
Jeff Sharkey | ebf728f | 2015-11-18 14:15:17 -0700 | [diff] [blame] | 499 | } |
| 500 | |
Calin Juravle | b06f98a | 2016-03-28 15:11:01 +0100 | [diff] [blame] | 501 | int delete_dir_contents_and_dir(const std::string& pathname, bool ignore_if_missing) { |
| 502 | return delete_dir_contents(pathname.c_str(), 1, NULL, ignore_if_missing); |
Jeff Sharkey | ebf728f | 2015-11-18 14:15:17 -0700 | [diff] [blame] | 503 | } |
| 504 | |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 505 | int delete_dir_contents(const char *pathname, |
| 506 | int also_delete_dir, |
Calin Juravle | b06f98a | 2016-03-28 15:11:01 +0100 | [diff] [blame] | 507 | int (*exclusion_predicate)(const char*, const int), |
| 508 | bool ignore_if_missing) |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 509 | { |
| 510 | int res = 0; |
| 511 | DIR *d; |
| 512 | |
| 513 | d = opendir(pathname); |
| 514 | if (d == NULL) { |
Calin Juravle | b06f98a | 2016-03-28 15:11:01 +0100 | [diff] [blame] | 515 | if (ignore_if_missing && (errno == ENOENT)) { |
| 516 | return 0; |
| 517 | } |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 518 | ALOGE("Couldn't opendir %s: %s\n", pathname, strerror(errno)); |
| 519 | return -errno; |
| 520 | } |
Narayan Kamath | 3aee2c5 | 2014-06-10 13:16:47 +0100 | [diff] [blame] | 521 | res = _delete_dir_contents(d, exclusion_predicate); |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 522 | closedir(d); |
| 523 | if (also_delete_dir) { |
| 524 | if (rmdir(pathname)) { |
| 525 | ALOGE("Couldn't rmdir %s: %s\n", pathname, strerror(errno)); |
| 526 | res = -1; |
| 527 | } |
| 528 | } |
| 529 | return res; |
| 530 | } |
| 531 | |
| 532 | int delete_dir_contents_fd(int dfd, const char *name) |
| 533 | { |
| 534 | int fd, res; |
| 535 | DIR *d; |
| 536 | |
Nick Kralevich | 8b7acac | 2015-08-10 13:43:00 -0700 | [diff] [blame] | 537 | fd = openat(dfd, name, O_RDONLY | O_DIRECTORY | O_NOFOLLOW | O_CLOEXEC); |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 538 | if (fd < 0) { |
| 539 | ALOGE("Couldn't openat %s: %s\n", name, strerror(errno)); |
| 540 | return -1; |
| 541 | } |
| 542 | d = fdopendir(fd); |
| 543 | if (d == NULL) { |
| 544 | ALOGE("Couldn't fdopendir %s: %s\n", name, strerror(errno)); |
| 545 | close(fd); |
| 546 | return -1; |
| 547 | } |
| 548 | res = _delete_dir_contents(d, 0); |
| 549 | closedir(d); |
| 550 | return res; |
| 551 | } |
| 552 | |
Robin Lee | 60fd3fe | 2014-10-07 16:55:02 +0100 | [diff] [blame] | 553 | static int _copy_owner_permissions(int srcfd, int dstfd) |
| 554 | { |
| 555 | struct stat st; |
| 556 | if (fstat(srcfd, &st) != 0) { |
| 557 | return -1; |
| 558 | } |
| 559 | if (fchmod(dstfd, st.st_mode) != 0) { |
| 560 | return -1; |
| 561 | } |
| 562 | return 0; |
| 563 | } |
| 564 | |
| 565 | static int _copy_dir_files(int sdfd, int ddfd, uid_t owner, gid_t group) |
| 566 | { |
| 567 | int result = 0; |
| 568 | if (_copy_owner_permissions(sdfd, ddfd) != 0) { |
| 569 | ALOGE("_copy_dir_files failed to copy dir permissions\n"); |
| 570 | } |
| 571 | if (fchown(ddfd, owner, group) != 0) { |
| 572 | ALOGE("_copy_dir_files failed to change dir owner\n"); |
| 573 | } |
| 574 | |
| 575 | DIR *ds = fdopendir(sdfd); |
| 576 | if (ds == NULL) { |
| 577 | ALOGE("Couldn't fdopendir: %s\n", strerror(errno)); |
| 578 | return -1; |
| 579 | } |
| 580 | struct dirent *de; |
| 581 | while ((de = readdir(ds))) { |
| 582 | if (de->d_type != DT_REG) { |
| 583 | continue; |
| 584 | } |
| 585 | |
| 586 | const char *name = de->d_name; |
| 587 | int fsfd = openat(sdfd, name, O_RDONLY | O_NOFOLLOW | O_CLOEXEC); |
| 588 | int fdfd = openat(ddfd, name, O_WRONLY | O_NOFOLLOW | O_CLOEXEC | O_CREAT, 0600); |
| 589 | if (fsfd == -1 || fdfd == -1) { |
| 590 | ALOGW("Couldn't copy %s: %s\n", name, strerror(errno)); |
| 591 | } else { |
| 592 | if (_copy_owner_permissions(fsfd, fdfd) != 0) { |
| 593 | ALOGE("Failed to change file permissions\n"); |
| 594 | } |
| 595 | if (fchown(fdfd, owner, group) != 0) { |
| 596 | ALOGE("Failed to change file owner\n"); |
| 597 | } |
| 598 | |
| 599 | char buf[8192]; |
| 600 | ssize_t size; |
| 601 | while ((size = read(fsfd, buf, sizeof(buf))) > 0) { |
| 602 | write(fdfd, buf, size); |
| 603 | } |
| 604 | if (size < 0) { |
| 605 | ALOGW("Couldn't copy %s: %s\n", name, strerror(errno)); |
| 606 | result = -1; |
| 607 | } |
| 608 | } |
| 609 | close(fdfd); |
| 610 | close(fsfd); |
| 611 | } |
| 612 | |
| 613 | return result; |
| 614 | } |
| 615 | |
| 616 | int copy_dir_files(const char *srcname, |
| 617 | const char *dstname, |
| 618 | uid_t owner, |
| 619 | uid_t group) |
| 620 | { |
| 621 | int res = 0; |
| 622 | DIR *ds = NULL; |
| 623 | DIR *dd = NULL; |
| 624 | |
| 625 | ds = opendir(srcname); |
| 626 | if (ds == NULL) { |
| 627 | ALOGE("Couldn't opendir %s: %s\n", srcname, strerror(errno)); |
| 628 | return -errno; |
| 629 | } |
| 630 | |
| 631 | mkdir(dstname, 0600); |
| 632 | dd = opendir(dstname); |
| 633 | if (dd == NULL) { |
| 634 | ALOGE("Couldn't opendir %s: %s\n", dstname, strerror(errno)); |
| 635 | closedir(ds); |
| 636 | return -errno; |
| 637 | } |
| 638 | |
| 639 | int sdfd = dirfd(ds); |
| 640 | int ddfd = dirfd(dd); |
| 641 | if (sdfd != -1 && ddfd != -1) { |
| 642 | res = _copy_dir_files(sdfd, ddfd, owner, group); |
| 643 | } else { |
| 644 | res = -errno; |
| 645 | } |
| 646 | closedir(dd); |
| 647 | closedir(ds); |
| 648 | return res; |
| 649 | } |
| 650 | |
Jeff Sharkey | a836c47 | 2017-04-02 23:29:30 -0600 | [diff] [blame] | 651 | int64_t data_disk_free(const std::string& data_path) { |
Jeff Sharkey | ed909ae | 2017-03-22 21:27:40 -0600 | [diff] [blame] | 652 | struct statvfs sfs; |
| 653 | if (statvfs(data_path.c_str(), &sfs) == 0) { |
Jeff Sharkey | 4f7be17 | 2017-08-11 15:13:31 -0600 | [diff] [blame] | 654 | return static_cast<int64_t>(sfs.f_bavail) * sfs.f_frsize; |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 655 | } else { |
Jeff Sharkey | ed909ae | 2017-03-22 21:27:40 -0600 | [diff] [blame] | 656 | PLOG(ERROR) << "Couldn't statvfs " << data_path; |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 657 | return -1; |
| 658 | } |
| 659 | } |
| 660 | |
Jeff Sharkey | 9a998f4 | 2016-07-14 18:16:22 -0600 | [diff] [blame] | 661 | int get_path_inode(const std::string& path, ino_t *inode) { |
| 662 | struct stat buf; |
| 663 | memset(&buf, 0, sizeof(buf)); |
| 664 | if (stat(path.c_str(), &buf) != 0) { |
| 665 | PLOG(WARNING) << "Failed to stat " << path; |
| 666 | return -1; |
| 667 | } else { |
| 668 | *inode = buf.st_ino; |
| 669 | return 0; |
| 670 | } |
| 671 | } |
| 672 | |
| 673 | /** |
| 674 | * Write the inode of a specific child file into the given xattr on the |
| 675 | * parent directory. This allows you to find the child later, even if its |
| 676 | * name is encrypted. |
| 677 | */ |
| 678 | int write_path_inode(const std::string& parent, const char* name, const char* inode_xattr) { |
| 679 | ino_t inode = 0; |
| 680 | uint64_t inode_raw = 0; |
| 681 | auto path = StringPrintf("%s/%s", parent.c_str(), name); |
| 682 | |
| 683 | if (get_path_inode(path, &inode) != 0) { |
| 684 | // Path probably doesn't exist yet; ignore |
| 685 | return 0; |
| 686 | } |
| 687 | |
| 688 | // Check to see if already set correctly |
| 689 | if (getxattr(parent.c_str(), inode_xattr, &inode_raw, sizeof(inode_raw)) == sizeof(inode_raw)) { |
| 690 | if (inode_raw == inode) { |
| 691 | // Already set correctly; skip writing |
| 692 | return 0; |
| 693 | } else { |
| 694 | PLOG(WARNING) << "Mismatched inode value; found " << inode |
| 695 | << " on disk but marked value was " << inode_raw << "; overwriting"; |
| 696 | } |
| 697 | } |
| 698 | |
| 699 | inode_raw = inode; |
Jeff Sharkey | 4ed6507 | 2016-07-22 11:38:54 -0600 | [diff] [blame] | 700 | if (setxattr(parent.c_str(), inode_xattr, &inode_raw, sizeof(inode_raw), 0) != 0 && errno != EOPNOTSUPP) { |
Jeff Sharkey | 9a998f4 | 2016-07-14 18:16:22 -0600 | [diff] [blame] | 701 | PLOG(ERROR) << "Failed to write xattr " << inode_xattr << " at " << parent; |
| 702 | return -1; |
| 703 | } else { |
| 704 | return 0; |
| 705 | } |
| 706 | } |
| 707 | |
| 708 | /** |
| 709 | * Read the inode of a specific child file from the given xattr on the |
| 710 | * parent directory. Returns a currently valid path for that child, which |
| 711 | * might have an encrypted name. |
| 712 | */ |
| 713 | std::string read_path_inode(const std::string& parent, const char* name, const char* inode_xattr) { |
| 714 | ino_t inode = 0; |
| 715 | uint64_t inode_raw = 0; |
| 716 | auto fallback = StringPrintf("%s/%s", parent.c_str(), name); |
| 717 | |
| 718 | // Lookup the inode value written earlier |
| 719 | if (getxattr(parent.c_str(), inode_xattr, &inode_raw, sizeof(inode_raw)) == sizeof(inode_raw)) { |
| 720 | inode = inode_raw; |
| 721 | } |
| 722 | |
| 723 | // For testing purposes, rely on the inode when defined; this could be |
| 724 | // optimized to use access() in the future. |
| 725 | if (inode != 0) { |
| 726 | DIR* dir = opendir(parent.c_str()); |
| 727 | if (dir == nullptr) { |
| 728 | PLOG(ERROR) << "Failed to opendir " << parent; |
| 729 | return fallback; |
| 730 | } |
| 731 | |
| 732 | struct dirent* ent; |
| 733 | while ((ent = readdir(dir))) { |
| 734 | if (ent->d_ino == inode) { |
| 735 | auto resolved = StringPrintf("%s/%s", parent.c_str(), ent->d_name); |
| 736 | #if DEBUG_XATTRS |
| 737 | if (resolved != fallback) { |
| 738 | LOG(DEBUG) << "Resolved path " << resolved << " for inode " << inode |
| 739 | << " instead of " << fallback; |
| 740 | } |
| 741 | #endif |
| 742 | closedir(dir); |
| 743 | return resolved; |
| 744 | } |
| 745 | } |
| 746 | LOG(WARNING) << "Failed to resolve inode " << inode << "; using " << fallback; |
| 747 | closedir(dir); |
| 748 | return fallback; |
| 749 | } else { |
| 750 | return fallback; |
| 751 | } |
| 752 | } |
| 753 | |
Ryuki Nakamura | c7342f8 | 2017-09-30 11:57:00 +0900 | [diff] [blame] | 754 | void remove_path_xattr(const std::string& path, const char* inode_xattr) { |
| 755 | if (removexattr(path.c_str(), inode_xattr) && errno != ENODATA) { |
| 756 | PLOG(ERROR) << "Failed to remove xattr " << inode_xattr << " at " << path; |
| 757 | } |
| 758 | } |
| 759 | |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 760 | /** |
Calin Juravle | c597b6d | 2014-08-19 17:43:05 +0100 | [diff] [blame] | 761 | * Validate that the path is valid in the context of the provided directory. |
| 762 | * The path is allowed to have at most one subdirectory and no indirections |
| 763 | * to top level directories (i.e. have ".."). |
| 764 | */ |
Jeff Sharkey | c1149c9 | 2017-09-21 14:51:09 -0600 | [diff] [blame] | 765 | static int validate_path(const std::string& dir, const std::string& path, int maxSubdirs) { |
| 766 | // Argument sanity checking |
| 767 | if (dir.find('/') != 0 || dir.rfind('/') != dir.size() - 1 |
| 768 | || dir.find("..") != std::string::npos) { |
| 769 | LOG(ERROR) << "Invalid directory " << dir; |
| 770 | return -1; |
| 771 | } |
| 772 | if (path.find("..") != std::string::npos) { |
| 773 | LOG(ERROR) << "Invalid path " << path; |
| 774 | return -1; |
Calin Juravle | c597b6d | 2014-08-19 17:43:05 +0100 | [diff] [blame] | 775 | } |
| 776 | |
Jeff Sharkey | c1149c9 | 2017-09-21 14:51:09 -0600 | [diff] [blame] | 777 | if (path.compare(0, dir.size(), dir) != 0) { |
| 778 | // Common case, path isn't under directory |
| 779 | return -1; |
| 780 | } |
| 781 | |
| 782 | // Count number of subdirectories |
| 783 | auto pos = path.find('/', dir.size()); |
| 784 | int count = 0; |
| 785 | while (pos != std::string::npos) { |
Jeff Sharkey | 172fac0 | 2017-10-06 13:09:46 -0600 | [diff] [blame] | 786 | auto next = path.find('/', pos + 1); |
| 787 | if (next > pos + 1) { |
| 788 | count++; |
| 789 | } |
| 790 | pos = next; |
Jeff Sharkey | c1149c9 | 2017-09-21 14:51:09 -0600 | [diff] [blame] | 791 | } |
| 792 | |
| 793 | if (count > maxSubdirs) { |
| 794 | LOG(ERROR) << "Invalid path depth " << path << " when tested against " << dir; |
Calin Juravle | c597b6d | 2014-08-19 17:43:05 +0100 | [diff] [blame] | 795 | return -1; |
| 796 | } |
| 797 | |
| 798 | return 0; |
| 799 | } |
| 800 | |
| 801 | /** |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 802 | * Checks whether a path points to a system app (.apk file). Returns 0 |
| 803 | * if it is a system app or -1 if it is not. |
| 804 | */ |
| 805 | int validate_system_app_path(const char* path) { |
Jeff Sharkey | c1149c9 | 2017-09-21 14:51:09 -0600 | [diff] [blame] | 806 | std::string path_ = path; |
| 807 | for (const auto& dir : android_system_dirs) { |
| 808 | if (validate_path(dir, path, 1) == 0) { |
| 809 | return 0; |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 810 | } |
| 811 | } |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 812 | return -1; |
| 813 | } |
| 814 | |
Calin Juravle | 114f081 | 2017-03-08 19:05:07 -0800 | [diff] [blame] | 815 | bool validate_secondary_dex_path(const std::string& pkgname, const std::string& dex_path, |
Calin Juravle | 7d76546 | 2017-09-04 15:57:10 -0700 | [diff] [blame] | 816 | const char* volume_uuid, int uid, int storage_flag) { |
Calin Juravle | 42451c0 | 2017-01-17 14:43:25 -0800 | [diff] [blame] | 817 | CHECK(storage_flag == FLAG_STORAGE_CE || storage_flag == FLAG_STORAGE_DE); |
| 818 | |
Calin Juravle | 3760ad3 | 2017-07-27 16:31:55 -0700 | [diff] [blame] | 819 | // Empty paths are not allowed. |
| 820 | if (dex_path.empty()) { return false; } |
| 821 | // First character should always be '/'. No relative paths. |
| 822 | if (dex_path[0] != '/') { return false; } |
| 823 | // The last character should not be '/'. |
| 824 | if (dex_path[dex_path.size() - 1] == '/') { return false; } |
| 825 | // There should be no '.' after the directory marker. |
| 826 | if (dex_path.find("/.") != std::string::npos) { return false; } |
| 827 | // The path should be at most PKG_PATH_MAX long. |
| 828 | if (dex_path.size() > PKG_PATH_MAX) { return false; } |
| 829 | |
Calin Juravle | 7d76546 | 2017-09-04 15:57:10 -0700 | [diff] [blame] | 830 | // The dex_path should be under the app data directory. |
| 831 | std::string app_private_dir = storage_flag == FLAG_STORAGE_CE |
Calin Juravle | dd42e27 | 2017-09-11 11:50:36 -0700 | [diff] [blame] | 832 | ? create_data_user_ce_package_path( |
| 833 | volume_uuid, multiuser_get_user_id(uid), pkgname.c_str()) |
| 834 | : create_data_user_de_package_path( |
| 835 | volume_uuid, multiuser_get_user_id(uid), pkgname.c_str()); |
Calin Juravle | 3760ad3 | 2017-07-27 16:31:55 -0700 | [diff] [blame] | 836 | |
Calin Juravle | 7d76546 | 2017-09-04 15:57:10 -0700 | [diff] [blame] | 837 | if (strncmp(dex_path.c_str(), app_private_dir.c_str(), app_private_dir.size()) != 0) { |
| 838 | // The check above might fail if the dex file is accessed via the /data/user/0 symlink. |
| 839 | // If that's the case, attempt to validate against the user data link. |
| 840 | std::string app_private_dir_symlink = create_data_user_ce_package_path_as_user_link( |
| 841 | volume_uuid, multiuser_get_user_id(uid), pkgname.c_str()); |
| 842 | if (strncmp(dex_path.c_str(), app_private_dir_symlink.c_str(), |
| 843 | app_private_dir_symlink.size()) != 0) { |
Calin Juravle | dd42e27 | 2017-09-11 11:50:36 -0700 | [diff] [blame] | 844 | return false; |
| 845 | } |
Calin Juravle | 42451c0 | 2017-01-17 14:43:25 -0800 | [diff] [blame] | 846 | } |
Calin Juravle | 3760ad3 | 2017-07-27 16:31:55 -0700 | [diff] [blame] | 847 | |
| 848 | // If we got here we have a valid path. |
| 849 | return true; |
Calin Juravle | 42451c0 | 2017-01-17 14:43:25 -0800 | [diff] [blame] | 850 | } |
| 851 | |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 852 | /** |
Narayan Kamath | d845c96 | 2015-06-04 13:20:27 +0100 | [diff] [blame] | 853 | * Check whether path points to a valid path for an APK file. The path must |
| 854 | * begin with a whitelisted prefix path and must be no deeper than |maxSubdirs| within |
| 855 | * that path. Returns -1 when an invalid path is encountered and 0 when a valid path |
| 856 | * is encountered. |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 857 | */ |
Jeff Sharkey | 8fa803a | 2018-04-09 18:46:45 -0600 | [diff] [blame^] | 858 | static int validate_apk_path_internal(const std::string& path, int maxSubdirs) { |
| 859 | if (validate_path(android_app_dir, path, maxSubdirs) == 0) { |
Jeff Sharkey | c1149c9 | 2017-09-21 14:51:09 -0600 | [diff] [blame] | 860 | return 0; |
Jeff Sharkey | 8fa803a | 2018-04-09 18:46:45 -0600 | [diff] [blame^] | 861 | } else if (validate_path(android_app_private_dir, path, maxSubdirs) == 0) { |
Jeff Sharkey | c1149c9 | 2017-09-21 14:51:09 -0600 | [diff] [blame] | 862 | return 0; |
Jeff Sharkey | 8fa803a | 2018-04-09 18:46:45 -0600 | [diff] [blame^] | 863 | } else if (validate_path(android_app_ephemeral_dir, path, maxSubdirs) == 0) { |
Jeff Sharkey | c1149c9 | 2017-09-21 14:51:09 -0600 | [diff] [blame] | 864 | return 0; |
Jeff Sharkey | 8fa803a | 2018-04-09 18:46:45 -0600 | [diff] [blame^] | 865 | } else if (validate_path(android_asec_dir, path, maxSubdirs) == 0) { |
Jeff Sharkey | c1149c9 | 2017-09-21 14:51:09 -0600 | [diff] [blame] | 866 | return 0; |
Jeff Sharkey | 8fa803a | 2018-04-09 18:46:45 -0600 | [diff] [blame^] | 867 | } else if (android::base::StartsWith(path, android_mnt_expand_dir)) { |
| 868 | // Rewrite the path as if it were on internal storage, and test that |
| 869 | size_t end = path.find('/', android_mnt_expand_dir.size() + 1); |
| 870 | if (end != std::string::npos) { |
| 871 | auto modified = path; |
| 872 | modified.replace(0, end + 1, android_data_dir); |
| 873 | return validate_apk_path_internal(modified, maxSubdirs); |
| 874 | } |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 875 | } |
Jeff Sharkey | 8fa803a | 2018-04-09 18:46:45 -0600 | [diff] [blame^] | 876 | return -1; |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 877 | } |
| 878 | |
Narayan Kamath | d845c96 | 2015-06-04 13:20:27 +0100 | [diff] [blame] | 879 | int validate_apk_path(const char* path) { |
| 880 | return validate_apk_path_internal(path, 1 /* maxSubdirs */); |
| 881 | } |
| 882 | |
| 883 | int validate_apk_path_subdirs(const char* path) { |
| 884 | return validate_apk_path_internal(path, 3 /* maxSubdirs */); |
| 885 | } |
| 886 | |
Robin Lee | 095c763 | 2014-04-25 15:05:19 +0100 | [diff] [blame] | 887 | int ensure_config_user_dirs(userid_t userid) { |
Robin Lee | 095c763 | 2014-04-25 15:05:19 +0100 | [diff] [blame] | 888 | // writable by system, readable by any app within the same user |
Robin Lee | 60fd3fe | 2014-10-07 16:55:02 +0100 | [diff] [blame] | 889 | const int uid = multiuser_get_uid(userid, AID_SYSTEM); |
| 890 | const int gid = multiuser_get_uid(userid, AID_EVERYBODY); |
Robin Lee | 095c763 | 2014-04-25 15:05:19 +0100 | [diff] [blame] | 891 | |
| 892 | // Ensure /data/misc/user/<userid> exists |
Jeff Sharkey | 379a12b | 2016-04-14 20:45:06 -0600 | [diff] [blame] | 893 | auto path = create_data_misc_legacy_path(userid); |
| 894 | return fs_prepare_dir(path.c_str(), 0750, uid, gid); |
Robin Lee | 095c763 | 2014-04-25 15:05:19 +0100 | [diff] [blame] | 895 | } |
Andreas Gampe | 02d0de5 | 2015-11-11 20:43:16 -0800 | [diff] [blame] | 896 | |
| 897 | int wait_child(pid_t pid) |
| 898 | { |
| 899 | int status; |
| 900 | pid_t got_pid; |
| 901 | |
| 902 | while (1) { |
| 903 | got_pid = waitpid(pid, &status, 0); |
| 904 | if (got_pid == -1 && errno == EINTR) { |
| 905 | printf("waitpid interrupted, retrying\n"); |
| 906 | } else { |
| 907 | break; |
| 908 | } |
| 909 | } |
| 910 | if (got_pid != pid) { |
| 911 | ALOGW("waitpid failed: wanted %d, got %d: %s\n", |
| 912 | (int) pid, (int) got_pid, strerror(errno)); |
| 913 | return 1; |
| 914 | } |
| 915 | |
| 916 | if (WIFEXITED(status) && WEXITSTATUS(status) == 0) { |
| 917 | return 0; |
| 918 | } else { |
| 919 | return status; /* always nonzero */ |
| 920 | } |
| 921 | } |
| 922 | |
Calin Juravle | 42451c0 | 2017-01-17 14:43:25 -0800 | [diff] [blame] | 923 | /** |
| 924 | * Prepare an app cache directory, which offers to fix-up the GID and |
| 925 | * directory mode flags during a platform upgrade. |
| 926 | * The app cache directory path will be 'parent'/'name'. |
| 927 | */ |
| 928 | int prepare_app_cache_dir(const std::string& parent, const char* name, mode_t target_mode, |
| 929 | uid_t uid, gid_t gid) { |
| 930 | auto path = StringPrintf("%s/%s", parent.c_str(), name); |
| 931 | struct stat st; |
| 932 | if (stat(path.c_str(), &st) != 0) { |
| 933 | if (errno == ENOENT) { |
| 934 | // This is fine, just create it |
| 935 | if (fs_prepare_dir_strict(path.c_str(), target_mode, uid, gid) != 0) { |
| 936 | PLOG(ERROR) << "Failed to prepare " << path; |
| 937 | return -1; |
| 938 | } else { |
| 939 | return 0; |
| 940 | } |
| 941 | } else { |
| 942 | PLOG(ERROR) << "Failed to stat " << path; |
| 943 | return -1; |
| 944 | } |
| 945 | } |
| 946 | |
| 947 | mode_t actual_mode = st.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO | S_ISGID); |
| 948 | if (st.st_uid != uid) { |
| 949 | // Mismatched UID is real trouble; we can't recover |
| 950 | LOG(ERROR) << "Mismatched UID at " << path << ": found " << st.st_uid |
| 951 | << " but expected " << uid; |
| 952 | return -1; |
| 953 | } else if (st.st_gid == gid && actual_mode == target_mode) { |
| 954 | // Everything looks good! |
| 955 | return 0; |
Jeff Sharkey | e59c85c | 2017-04-02 21:53:14 -0600 | [diff] [blame] | 956 | } else { |
| 957 | // Mismatched GID/mode is recoverable; fall through to update |
| 958 | LOG(DEBUG) << "Mismatched cache GID/mode at " << path << ": found " << st.st_gid |
Shubham Ajmera | ec0afbf | 2017-09-14 11:07:33 -0700 | [diff] [blame] | 959 | << "/" << actual_mode << " but expected " << gid << "/" << target_mode; |
Calin Juravle | 42451c0 | 2017-01-17 14:43:25 -0800 | [diff] [blame] | 960 | } |
| 961 | |
| 962 | // Directory is owned correctly, but GID or mode mismatch means it's |
| 963 | // probably a platform upgrade so we need to fix them |
| 964 | FTS *fts; |
| 965 | FTSENT *p; |
| 966 | char *argv[] = { (char*) path.c_str(), nullptr }; |
Jeff Sharkey | b26786d | 2017-03-11 19:40:29 -0700 | [diff] [blame] | 967 | if (!(fts = fts_open(argv, FTS_PHYSICAL | FTS_NOCHDIR | FTS_XDEV, NULL))) { |
Calin Juravle | 42451c0 | 2017-01-17 14:43:25 -0800 | [diff] [blame] | 968 | PLOG(ERROR) << "Failed to fts_open " << path; |
| 969 | return -1; |
| 970 | } |
| 971 | while ((p = fts_read(fts)) != NULL) { |
| 972 | switch (p->fts_info) { |
| 973 | case FTS_DP: |
Jeff Sharkey | e12d596 | 2017-04-03 16:41:02 -0600 | [diff] [blame] | 974 | if (chmod(p->fts_path, target_mode) != 0) { |
Calin Juravle | 42451c0 | 2017-01-17 14:43:25 -0800 | [diff] [blame] | 975 | PLOG(WARNING) << "Failed to chmod " << p->fts_path; |
| 976 | } |
| 977 | // Intentional fall through to also set GID |
| 978 | case FTS_F: |
Jeff Sharkey | e12d596 | 2017-04-03 16:41:02 -0600 | [diff] [blame] | 979 | if (chown(p->fts_path, -1, gid) != 0) { |
Calin Juravle | 42451c0 | 2017-01-17 14:43:25 -0800 | [diff] [blame] | 980 | PLOG(WARNING) << "Failed to chown " << p->fts_path; |
| 981 | } |
| 982 | break; |
| 983 | case FTS_SL: |
| 984 | case FTS_SLNONE: |
Jeff Sharkey | e12d596 | 2017-04-03 16:41:02 -0600 | [diff] [blame] | 985 | if (lchown(p->fts_path, -1, gid) != 0) { |
Calin Juravle | 42451c0 | 2017-01-17 14:43:25 -0800 | [diff] [blame] | 986 | PLOG(WARNING) << "Failed to chown " << p->fts_path; |
| 987 | } |
| 988 | break; |
| 989 | } |
| 990 | } |
| 991 | fts_close(fts); |
| 992 | return 0; |
| 993 | } |
| 994 | |
Calin Juravle | e61189e | 2018-01-23 19:54:11 -0800 | [diff] [blame] | 995 | // Collect all non empty profiles from the given directory and puts then into profile_paths. |
| 996 | // The profiles are identified based on PROFILE_EXT extension. |
| 997 | // If a subdirectory or profile file cannot be opened the method logs a warning and moves on. |
| 998 | // It returns true if there were no errors at all, and false otherwise. |
| 999 | static bool collect_profiles(DIR* d, |
| 1000 | const std::string& current_path, |
| 1001 | std::vector<std::string>* profiles_paths) { |
| 1002 | int32_t dir_fd = dirfd(d); |
| 1003 | if (dir_fd < 0) { |
| 1004 | return false; |
| 1005 | } |
| 1006 | |
| 1007 | bool result = true; |
| 1008 | struct dirent* dir_entry; |
| 1009 | while ((dir_entry = readdir(d))) { |
| 1010 | std::string name = dir_entry->d_name; |
| 1011 | std::string local_path = current_path + "/" + name; |
| 1012 | |
| 1013 | if (dir_entry->d_type == DT_REG) { |
| 1014 | // Check if this is a non empty profile file. |
| 1015 | if (EndsWith(name, PROFILE_EXT)) { |
| 1016 | struct stat st; |
| 1017 | if (stat(local_path.c_str(), &st) != 0) { |
| 1018 | PLOG(WARNING) << "Cannot stat local path " << local_path; |
| 1019 | result = false; |
| 1020 | continue; |
| 1021 | } else if (st.st_size > 0) { |
| 1022 | profiles_paths->push_back(local_path); |
| 1023 | } |
| 1024 | } |
| 1025 | } else if (dir_entry->d_type == DT_DIR) { |
| 1026 | // always skip "." and ".." |
| 1027 | if (name == "." || name == "..") { |
| 1028 | continue; |
| 1029 | } |
| 1030 | |
| 1031 | unique_fd subdir_fd(openat(dir_fd, name.c_str(), |
| 1032 | O_RDONLY | O_DIRECTORY | O_NOFOLLOW | O_CLOEXEC)); |
| 1033 | if (subdir_fd < 0) { |
| 1034 | PLOG(WARNING) << "Could not open dir path " << local_path; |
| 1035 | result = false; |
| 1036 | continue; |
| 1037 | } |
| 1038 | |
| 1039 | DIR* subdir = fdopendir(subdir_fd); |
| 1040 | if (subdir == NULL) { |
| 1041 | PLOG(WARNING) << "Could not open dir path " << local_path; |
| 1042 | result = false; |
| 1043 | continue; |
| 1044 | } |
| 1045 | bool new_result = collect_profiles(subdir, local_path, profiles_paths); |
| 1046 | result = result && new_result; |
| 1047 | if (closedir(subdir) != 0) { |
| 1048 | PLOG(WARNING) << "Could not close dir path " << local_path; |
| 1049 | } |
| 1050 | } |
| 1051 | } |
| 1052 | |
| 1053 | return result; |
| 1054 | } |
| 1055 | |
| 1056 | bool collect_profiles(std::vector<std::string>* profiles_paths) { |
| 1057 | DIR* d = opendir(android_profiles_dir.c_str()); |
| 1058 | if (d == NULL) { |
| 1059 | return false; |
| 1060 | } else { |
| 1061 | return collect_profiles(d, android_profiles_dir, profiles_paths); |
| 1062 | } |
| 1063 | } |
| 1064 | |
Andreas Gampe | 02d0de5 | 2015-11-11 20:43:16 -0800 | [diff] [blame] | 1065 | } // namespace installd |
| 1066 | } // namespace android |