blob: f3d94597e8552e0ed14f494c7f2ccccd6234affc [file] [log] [blame]
Mike Lockwood94afecf2012-10-24 10:45:23 -07001/*
2** Copyright 2008, The Android Open Source Project
3**
Dave Allisond9370732014-01-30 14:19:23 -08004** 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 Lockwood94afecf2012-10-24 10:45:23 -07007**
Dave Allisond9370732014-01-30 14:19:23 -08008** http://www.apache.org/licenses/LICENSE-2.0
Mike Lockwood94afecf2012-10-24 10:45:23 -07009**
Dave Allisond9370732014-01-30 14:19:23 -080010** 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 Lockwood94afecf2012-10-24 10:45:23 -070014** limitations under the License.
15*/
16
Andreas Gampe02d0de52015-11-11 20:43:16 -080017#include "utils.h"
Mike Lockwood94afecf2012-10-24 10:45:23 -070018
Andreas Gampe02d0de52015-11-11 20:43:16 -080019#include <errno.h>
20#include <fcntl.h>
Jeff Sharkey3dfae0c2016-12-12 17:32:56 -070021#include <fts.h>
Andreas Gampe02d0de52015-11-11 20:43:16 -080022#include <stdlib.h>
23#include <sys/stat.h>
24#include <sys/wait.h>
Jeff Sharkey9a998f42016-07-14 18:16:22 -060025#include <sys/xattr.h>
Jeff Sharkeyed909ae2017-03-22 21:27:40 -060026#include <sys/statvfs.h>
Andreas Gampe02d0de52015-11-11 20:43:16 -080027
Elliott Hughese4ec9eb2015-12-04 15:39:32 -080028#include <android-base/logging.h>
Calin Juravlecfcd6aa2018-01-18 20:23:17 -080029#include <android-base/strings.h>
Andreas Gampe02d0de52015-11-11 20:43:16 -080030#include <android-base/stringprintf.h>
31#include <cutils/fs.h>
Jeff Sharkey871a8f22017-02-21 18:30:28 -070032#include <cutils/properties.h>
Mark Salyzyn7823e122016-09-29 08:08:05 -070033#include <log/log.h>
Andreas Gampe02d0de52015-11-11 20:43:16 -080034#include <private/android_filesystem_config.h>
Jeff Sharkeyc03de092015-04-07 18:14:05 -070035
Andreas Gampe02d0de52015-11-11 20:43:16 -080036#include "globals.h" // extern variables.
37
38#ifndef LOG_TAG
39#define LOG_TAG "installd"
40#endif
Jeff Sharkey9a998f42016-07-14 18:16:22 -060041
Jeff Sharkey9a998f42016-07-14 18:16:22 -060042#define DEBUG_XATTRS 0
Mike Lockwood94afecf2012-10-24 10:45:23 -070043
Jeff Sharkeyc03de092015-04-07 18:14:05 -070044using android::base::StringPrintf;
Mike Lockwood94afecf2012-10-24 10:45:23 -070045
Andreas Gampe02d0de52015-11-11 20:43:16 -080046namespace android {
47namespace installd {
48
Jeff Sharkeyc03de092015-04-07 18:14:05 -070049/**
50 * Check that given string is valid filename, and that it attempts no
51 * parent or child directory traversal.
52 */
Jeff Sharkey423e7462016-12-09 18:18:43 -070053bool is_valid_filename(const std::string& name) {
Jeff Sharkeyc03de092015-04-07 18:14:05 -070054 if (name.empty() || (name == ".") || (name == "..")
55 || (name.find('/') != std::string::npos)) {
56 return false;
57 } else {
58 return true;
59 }
Mike Lockwood94afecf2012-10-24 10:45:23 -070060}
61
Calin Juravle6a1648e2016-02-01 12:12:16 +000062static void check_package_name(const char* package_name) {
63 CHECK(is_valid_filename(package_name));
Jeff Sharkey423e7462016-12-09 18:18:43 -070064 CHECK(is_valid_package_name(package_name));
Calin Juravle6a1648e2016-02-01 12:12:16 +000065}
66
Mike Lockwood94afecf2012-10-24 10:45:23 -070067/**
Jeff Sharkeyd7921182015-04-30 15:58:19 -070068 * Create the path name where package app contents should be stored for
69 * the given volume UUID and package name. An empty UUID is assumed to
70 * be internal storage.
71 */
72std::string create_data_app_package_path(const char* volume_uuid,
73 const char* package_name) {
Calin Juravle6a1648e2016-02-01 12:12:16 +000074 check_package_name(package_name);
Jeff Sharkeyd7921182015-04-30 15:58:19 -070075 return StringPrintf("%s/%s",
76 create_data_app_path(volume_uuid).c_str(), package_name);
77}
78
79/**
Jeff Sharkeyc03de092015-04-07 18:14:05 -070080 * Create the path name where package data should be stored for the given
81 * volume UUID, package name, and user ID. An empty UUID is assumed to be
82 * internal storage.
Mike Lockwood94afecf2012-10-24 10:45:23 -070083 */
Jeff Sharkey2f720f72016-04-10 20:51:40 -060084std::string create_data_user_ce_package_path(const char* volume_uuid,
Jeff Sharkeyd7921182015-04-30 15:58:19 -070085 userid_t user, const char* package_name) {
Calin Juravle6a1648e2016-02-01 12:12:16 +000086 check_package_name(package_name);
Jeff Sharkeyd7921182015-04-30 15:58:19 -070087 return StringPrintf("%s/%s",
Jeff Sharkey2f720f72016-04-10 20:51:40 -060088 create_data_user_ce_path(volume_uuid, user).c_str(), package_name);
89}
90
Calin Juravle7d765462017-09-04 15:57:10 -070091/**
92 * Create the path name where package data should be stored for the given
93 * volume UUID, package name, and user ID. An empty UUID is assumed to be
94 * internal storage.
95 * Compared to create_data_user_ce_package_path this method always return the
96 * ".../user/..." directory.
97 */
98std::string create_data_user_ce_package_path_as_user_link(
99 const char* volume_uuid, userid_t userid, const char* package_name) {
100 check_package_name(package_name);
101 std::string data(create_data_path(volume_uuid));
102 return StringPrintf("%s/user/%u/%s", data.c_str(), userid, package_name);
103}
104
Jeff Sharkey2f720f72016-04-10 20:51:40 -0600105std::string create_data_user_ce_package_path(const char* volume_uuid, userid_t user,
106 const char* package_name, ino_t ce_data_inode) {
107 // For testing purposes, rely on the inode when defined; this could be
108 // optimized to use access() in the future.
109 auto fallback = create_data_user_ce_package_path(volume_uuid, user, package_name);
110 if (ce_data_inode != 0) {
111 auto user_path = create_data_user_ce_path(volume_uuid, user);
112 DIR* dir = opendir(user_path.c_str());
113 if (dir == nullptr) {
114 PLOG(ERROR) << "Failed to opendir " << user_path;
115 return fallback;
116 }
117
118 struct dirent* ent;
119 while ((ent = readdir(dir))) {
120 if (ent->d_ino == ce_data_inode) {
Jeff Sharkey1d992f92016-04-13 13:45:47 -0600121 auto resolved = StringPrintf("%s/%s", user_path.c_str(), ent->d_name);
Jeff Sharkey9a998f42016-07-14 18:16:22 -0600122#if DEBUG_XATTRS
Jeff Sharkey1d992f92016-04-13 13:45:47 -0600123 if (resolved != fallback) {
124 LOG(DEBUG) << "Resolved path " << resolved << " for inode " << ce_data_inode
125 << " instead of " << fallback;
126 }
Jeff Sharkey9a998f42016-07-14 18:16:22 -0600127#endif
Jeff Sharkey2f720f72016-04-10 20:51:40 -0600128 closedir(dir);
Jeff Sharkey1d992f92016-04-13 13:45:47 -0600129 return resolved;
Jeff Sharkey2f720f72016-04-10 20:51:40 -0600130 }
131 }
Jeff Sharkey1d992f92016-04-13 13:45:47 -0600132 LOG(WARNING) << "Failed to resolve inode " << ce_data_inode << "; using " << fallback;
Jeff Sharkey2f720f72016-04-10 20:51:40 -0600133 closedir(dir);
134 return fallback;
135 } else {
136 return fallback;
137 }
Jeff Sharkeyc03de092015-04-07 18:14:05 -0700138}
Mike Lockwood94afecf2012-10-24 10:45:23 -0700139
Jeff Sharkey63ec2d62015-11-09 13:10:36 -0800140std::string create_data_user_de_package_path(const char* volume_uuid,
141 userid_t user, const char* package_name) {
Calin Juravle6a1648e2016-02-01 12:12:16 +0000142 check_package_name(package_name);
Jeff Sharkey63ec2d62015-11-09 13:10:36 -0800143 return StringPrintf("%s/%s",
144 create_data_user_de_path(volume_uuid, user).c_str(), package_name);
145}
146
Jeff Sharkey41ea4242015-04-09 11:34:03 -0700147std::string create_data_path(const char* volume_uuid) {
148 if (volume_uuid == nullptr) {
149 return "/data";
Jeff Sharkey871a8f22017-02-21 18:30:28 -0700150 } else if (!strcmp(volume_uuid, "TEST")) {
151 CHECK(property_get_bool("ro.debuggable", false));
152 return "/data/local/tmp";
Jeff Sharkey41ea4242015-04-09 11:34:03 -0700153 } else {
154 CHECK(is_valid_filename(volume_uuid));
155 return StringPrintf("/mnt/expand/%s", volume_uuid);
156 }
157}
158
Mike Lockwood94afecf2012-10-24 10:45:23 -0700159/**
Jeff Sharkeyd7921182015-04-30 15:58:19 -0700160 * Create the path name for app data.
161 */
162std::string create_data_app_path(const char* volume_uuid) {
163 return StringPrintf("%s/app", create_data_path(volume_uuid).c_str());
164}
165
166/**
Jeff Sharkeyabe4fe52013-07-10 16:55:46 -0700167 * Create the path name for user data for a certain userid.
cjbao75d4e572017-04-12 00:12:24 +0800168 * Keep same implementation as vold to minimize path walking overhead
Mike Lockwood94afecf2012-10-24 10:45:23 -0700169 */
Jeff Sharkey2f720f72016-04-10 20:51:40 -0600170std::string create_data_user_ce_path(const char* volume_uuid, userid_t userid) {
Jeff Sharkey41ea4242015-04-09 11:34:03 -0700171 std::string data(create_data_path(volume_uuid));
cjbao75d4e572017-04-12 00:12:24 +0800172 if (volume_uuid == nullptr && userid == 0) {
173 std::string legacy = StringPrintf("%s/data", data.c_str());
174 struct stat sb;
175 if (lstat(legacy.c_str(), &sb) == 0 && S_ISDIR(sb.st_mode)) {
176 /* /data/data is dir, return /data/data for legacy system */
177 return legacy;
Jeff Sharkey41ea4242015-04-09 11:34:03 -0700178 }
Mike Lockwood94afecf2012-10-24 10:45:23 -0700179 }
cjbao75d4e572017-04-12 00:12:24 +0800180 return StringPrintf("%s/user/%u", data.c_str(), userid);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700181}
182
183/**
Jeff Sharkey63ec2d62015-11-09 13:10:36 -0800184 * Create the path name for device encrypted user data for a certain userid.
185 */
186std::string create_data_user_de_path(const char* volume_uuid, userid_t userid) {
187 std::string data(create_data_path(volume_uuid));
188 return StringPrintf("%s/user_de/%u", data.c_str(), userid);
189}
190
191/**
Jeff Sharkeyabe4fe52013-07-10 16:55:46 -0700192 * Create the path name for media for a certain userid.
Mike Lockwood94afecf2012-10-24 10:45:23 -0700193 */
Jeff Sharkey41ea4242015-04-09 11:34:03 -0700194std::string create_data_media_path(const char* volume_uuid, userid_t userid) {
195 return StringPrintf("%s/media/%u", create_data_path(volume_uuid).c_str(), userid);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700196}
197
Jeff Sharkeydf2d7542017-01-07 09:19:35 -0700198std::string create_data_media_obb_path(const char* volume_uuid, const char* package_name) {
199 return StringPrintf("%s/media/obb/%s", create_data_path(volume_uuid).c_str(), package_name);
200}
201
Jeff Sharkey3dfae0c2016-12-12 17:32:56 -0700202std::string create_data_media_package_path(const char* volume_uuid, userid_t userid,
203 const char* data_type, const char* package_name) {
204 return StringPrintf("%s/Android/%s/%s", create_data_media_path(volume_uuid, userid).c_str(),
205 data_type, package_name);
206}
207
Jeff Sharkey379a12b2016-04-14 20:45:06 -0600208std::string create_data_misc_legacy_path(userid_t userid) {
209 return StringPrintf("%s/misc/user/%u", create_data_path(nullptr).c_str(), userid);
210}
211
Calin Juravle114f0812017-03-08 19:05:07 -0800212std::string create_primary_cur_profile_dir_path(userid_t userid) {
Jeff Sharkeyc1149c92017-09-21 14:51:09 -0600213 return StringPrintf("%s/cur/%u", android_profiles_dir.c_str(), userid);
Calin Juravle6a1648e2016-02-01 12:12:16 +0000214}
215
Calin Juravle114f0812017-03-08 19:05:07 -0800216std::string create_primary_current_profile_package_dir_path(userid_t user,
217 const std::string& package_name) {
Calin Juravle76268c52017-03-09 13:19:42 -0800218 check_package_name(package_name.c_str());
Calin Juravle114f0812017-03-08 19:05:07 -0800219 return StringPrintf("%s/%s",
220 create_primary_cur_profile_dir_path(user).c_str(), package_name.c_str());
Jeff Sharkeydf2d7542017-01-07 09:19:35 -0700221}
222
Calin Juravle114f0812017-03-08 19:05:07 -0800223std::string create_primary_ref_profile_dir_path() {
Jeff Sharkeyc1149c92017-09-21 14:51:09 -0600224 return StringPrintf("%s/ref", android_profiles_dir.c_str());
Calin Juravle6a1648e2016-02-01 12:12:16 +0000225}
226
Calin Juravle114f0812017-03-08 19:05:07 -0800227std::string create_primary_reference_profile_package_dir_path(const std::string& package_name) {
Calin Juravle76268c52017-03-09 13:19:42 -0800228 check_package_name(package_name.c_str());
Jeff Sharkeyc1149c92017-09-21 14:51:09 -0600229 return StringPrintf("%s/ref/%s", android_profiles_dir.c_str(), package_name.c_str());
Calin Juravle6a1648e2016-02-01 12:12:16 +0000230}
231
Jeff Sharkey3dfae0c2016-12-12 17:32:56 -0700232std::string create_data_dalvik_cache_path() {
233 return "/data/dalvik-cache";
234}
235
Calin Juravle114f0812017-03-08 19:05:07 -0800236// Keep profile paths in sync with ActivityThread and LoadedApk.
237const std::string PROFILE_EXT = ".prof";
Calin Juravle3760ad32017-07-27 16:31:55 -0700238const std::string CURRENT_PROFILE_EXT = ".cur";
Calin Juravle29591732017-11-20 17:46:19 -0800239const std::string SNAPSHOT_PROFILE_EXT = ".snapshot";
Jeff Sharkey90aff262016-12-12 14:28:24 -0700240
Calin Juravle3760ad32017-07-27 16:31:55 -0700241// Gets the parent directory and the file name for the given secondary dex path.
242// Returns true on success, false on failure (if the dex_path does not have the expected
243// structure).
244static bool get_secondary_dex_location(const std::string& dex_path,
245 std::string* out_dir_name, std::string* out_file_name) {
246 size_t dirIndex = dex_path.rfind('/');
247 if (dirIndex == std::string::npos) {
248 return false;
249 }
250 if (dirIndex == dex_path.size() - 1) {
251 return false;
252 }
253 *out_dir_name = dex_path.substr(0, dirIndex);
254 *out_file_name = dex_path.substr(dirIndex + 1);
255
256 return true;
257}
258
Calin Juravlecfcd6aa2018-01-18 20:23:17 -0800259std::string create_current_profile_path(userid_t user, const std::string& package_name,
260 const std::string& location, bool is_secondary_dex) {
Calin Juravle114f0812017-03-08 19:05:07 -0800261 if (is_secondary_dex) {
Calin Juravle3760ad32017-07-27 16:31:55 -0700262 // Secondary dex current profiles are stored next to the dex files under the oat folder.
263 std::string dex_dir;
264 std::string dex_name;
265 CHECK(get_secondary_dex_location(location, &dex_dir, &dex_name))
266 << "Unexpected dir structure for secondary dex " << location;
267 return StringPrintf("%s/oat/%s%s%s",
268 dex_dir.c_str(), dex_name.c_str(), CURRENT_PROFILE_EXT.c_str(),
269 PROFILE_EXT.c_str());
Calin Juravle114f0812017-03-08 19:05:07 -0800270 } else {
271 // Profiles for primary apks are under /data/misc/profiles/cur.
Calin Juravlecfcd6aa2018-01-18 20:23:17 -0800272 std::string profile_dir = create_primary_current_profile_package_dir_path(
273 user, package_name);
274 return StringPrintf("%s/%s", profile_dir.c_str(), location.c_str());
Calin Juravle114f0812017-03-08 19:05:07 -0800275 }
276}
277
Calin Juravlecfcd6aa2018-01-18 20:23:17 -0800278std::string create_reference_profile_path(const std::string& package_name,
279 const std::string& location, bool is_secondary_dex) {
Calin Juravle114f0812017-03-08 19:05:07 -0800280 if (is_secondary_dex) {
281 // Secondary dex reference profiles are stored next to the dex files under the oat folder.
Calin Juravle3760ad32017-07-27 16:31:55 -0700282 std::string dex_dir;
283 std::string dex_name;
284 CHECK(get_secondary_dex_location(location, &dex_dir, &dex_name))
Calin Juravle114f0812017-03-08 19:05:07 -0800285 << "Unexpected dir structure for secondary dex " << location;
Calin Juravle114f0812017-03-08 19:05:07 -0800286 return StringPrintf("%s/oat/%s%s",
287 dex_dir.c_str(), dex_name.c_str(), PROFILE_EXT.c_str());
288 } else {
289 // Reference profiles for primary apks are stored in /data/misc/profile/ref.
Calin Juravlecfcd6aa2018-01-18 20:23:17 -0800290 std::string profile_dir = create_primary_reference_profile_package_dir_path(package_name);
291 return StringPrintf("%s/%s", profile_dir.c_str(), location.c_str());
Calin Juravle114f0812017-03-08 19:05:07 -0800292 }
Jeff Sharkey90aff262016-12-12 14:28:24 -0700293}
294
Calin Juravle29591732017-11-20 17:46:19 -0800295std::string create_snapshot_profile_path(const std::string& package,
Calin Juravlecfcd6aa2018-01-18 20:23:17 -0800296 const std::string& profile_name) {
297 std::string ref_profile = create_reference_profile_path(package, profile_name,
298 /*is_secondary_dex*/ false);
Calin Juravle29591732017-11-20 17:46:19 -0800299 return ref_profile + SNAPSHOT_PROFILE_EXT;
300}
301
Jeff Sharkeye3637242015-04-08 20:56:42 -0700302std::vector<userid_t> get_known_users(const char* volume_uuid) {
303 std::vector<userid_t> users;
304
305 // We always have an owner
306 users.push_back(0);
307
308 std::string path(create_data_path(volume_uuid) + "/" + SECONDARY_USER_PREFIX);
309 DIR* dir = opendir(path.c_str());
310 if (dir == NULL) {
311 // Unable to discover other users, but at least return owner
312 PLOG(ERROR) << "Failed to opendir " << path;
313 return users;
314 }
315
316 struct dirent* ent;
317 while ((ent = readdir(dir))) {
318 if (ent->d_type != DT_DIR) {
319 continue;
320 }
321
322 char* end;
323 userid_t user = strtol(ent->d_name, &end, 10);
324 if (*end == '\0' && user != 0) {
325 LOG(DEBUG) << "Found valid user " << user;
326 users.push_back(user);
327 }
328 }
329 closedir(dir);
330
331 return users;
332}
333
Jeff Sharkey3dfae0c2016-12-12 17:32:56 -0700334int calculate_tree_size(const std::string& path, int64_t* size,
Jeff Sharkeydf2d7542017-01-07 09:19:35 -0700335 int32_t include_gid, int32_t exclude_gid, bool exclude_apps) {
Jeff Sharkey3dfae0c2016-12-12 17:32:56 -0700336 FTS *fts;
337 FTSENT *p;
Jeff Sharkeydf2d7542017-01-07 09:19:35 -0700338 int64_t matchedSize = 0;
Jeff Sharkey3dfae0c2016-12-12 17:32:56 -0700339 char *argv[] = { (char*) path.c_str(), nullptr };
Jeff Sharkeyb26786d2017-03-11 19:40:29 -0700340 if (!(fts = fts_open(argv, FTS_PHYSICAL | FTS_NOCHDIR | FTS_XDEV, NULL))) {
Jeff Sharkey3dfae0c2016-12-12 17:32:56 -0700341 if (errno != ENOENT) {
342 PLOG(ERROR) << "Failed to fts_open " << path;
343 }
344 return -1;
345 }
346 while ((p = fts_read(fts)) != NULL) {
347 switch (p->fts_info) {
348 case FTS_D:
349 case FTS_DEFAULT:
350 case FTS_F:
351 case FTS_SL:
352 case FTS_SLNONE:
Jeff Sharkeydf2d7542017-01-07 09:19:35 -0700353 int32_t uid = p->fts_statp->st_uid;
354 int32_t gid = p->fts_statp->st_gid;
355 int32_t user_uid = multiuser_get_app_id(uid);
356 int32_t user_gid = multiuser_get_app_id(gid);
357 if (exclude_apps && ((user_uid >= AID_APP_START && user_uid <= AID_APP_END)
358 || (user_gid >= AID_CACHE_GID_START && user_gid <= AID_CACHE_GID_END)
359 || (user_gid >= AID_SHARED_GID_START && user_gid <= AID_SHARED_GID_END))) {
360 // Don't traverse inside or measure
361 fts_set(fts, p, FTS_SKIP);
Jeff Sharkey3dfae0c2016-12-12 17:32:56 -0700362 break;
363 }
Jeff Sharkeydf2d7542017-01-07 09:19:35 -0700364 if (include_gid != -1 && gid != include_gid) {
Jeff Sharkey3dfae0c2016-12-12 17:32:56 -0700365 break;
366 }
Jeff Sharkeydf2d7542017-01-07 09:19:35 -0700367 if (exclude_gid != -1 && gid == exclude_gid) {
368 break;
369 }
370 matchedSize += (p->fts_statp->st_blocks * 512);
Jeff Sharkey3dfae0c2016-12-12 17:32:56 -0700371 break;
372 }
373 }
374 fts_close(fts);
Jeff Sharkeydf2d7542017-01-07 09:19:35 -0700375#if MEASURE_DEBUG
376 if ((include_gid == -1) && (exclude_gid == -1)) {
377 LOG(DEBUG) << "Measured " << path << " size " << matchedSize;
378 } else {
379 LOG(DEBUG) << "Measured " << path << " size " << matchedSize << "; include " << include_gid
380 << " exclude " << exclude_gid;
381 }
382#endif
383 *size += matchedSize;
Jeff Sharkey3dfae0c2016-12-12 17:32:56 -0700384 return 0;
385}
386
Mike Lockwood94afecf2012-10-24 10:45:23 -0700387/**
388 * Checks whether the package name is valid. Returns -1 on error and
389 * 0 on success.
390 */
Jeff Sharkey423e7462016-12-09 18:18:43 -0700391bool is_valid_package_name(const std::string& packageName) {
Jeff Sharkey367ace22017-03-07 22:12:03 -0700392 // This logic is borrowed from PackageParser.java
393 bool hasSep = false;
394 bool front = true;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700395
Jeff Sharkey367ace22017-03-07 22:12:03 -0700396 auto it = packageName.begin();
397 for (; it != packageName.end() && *it != '-'; it++) {
398 char c = *it;
399 if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) {
400 front = false;
401 continue;
402 }
403 if (!front) {
404 if ((c >= '0' && c <= '9') || c == '_') {
405 continue;
406 }
407 }
408 if (c == '.') {
409 hasSep = true;
410 front = true;
411 continue;
412 }
413 LOG(WARNING) << "Bad package character " << c << " in " << packageName;
Jeff Sharkey423e7462016-12-09 18:18:43 -0700414 return false;
Jeff Sharkeyc03de092015-04-07 18:14:05 -0700415 }
416
Jeff Sharkeyab7ac8d2017-03-08 12:39:46 -0700417 if (front) {
Jeff Sharkey367ace22017-03-07 22:12:03 -0700418 LOG(WARNING) << "Missing separator in " << packageName;
419 return false;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700420 }
421
Jeff Sharkey367ace22017-03-07 22:12:03 -0700422 for (; it != packageName.end(); it++) {
423 char c = *it;
424 if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) continue;
425 if ((c >= '0' && c <= '9') || c == '_' || c == '-' || c == '=') continue;
426 LOG(WARNING) << "Bad suffix character " << c << " in " << packageName;
427 return false;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700428 }
429
Jeff Sharkey423e7462016-12-09 18:18:43 -0700430 return true;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700431}
432
Narayan Kamath3aee2c52014-06-10 13:16:47 +0100433static int _delete_dir_contents(DIR *d,
434 int (*exclusion_predicate)(const char *name, const int is_dir))
Mike Lockwood94afecf2012-10-24 10:45:23 -0700435{
436 int result = 0;
437 struct dirent *de;
438 int dfd;
439
440 dfd = dirfd(d);
441
442 if (dfd < 0) return -1;
443
444 while ((de = readdir(d))) {
445 const char *name = de->d_name;
446
Narayan Kamath3aee2c52014-06-10 13:16:47 +0100447 /* check using the exclusion predicate, if provided */
448 if (exclusion_predicate && exclusion_predicate(name, (de->d_type == DT_DIR))) {
449 continue;
450 }
Mike Lockwood94afecf2012-10-24 10:45:23 -0700451
452 if (de->d_type == DT_DIR) {
Chih-Hung Hsieh99d9fb12014-09-11 14:44:46 -0700453 int subfd;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700454 DIR *subdir;
455
456 /* always skip "." and ".." */
457 if (name[0] == '.') {
458 if (name[1] == 0) continue;
459 if ((name[1] == '.') && (name[2] == 0)) continue;
460 }
461
Nick Kralevich8b7acac2015-08-10 13:43:00 -0700462 subfd = openat(dfd, name, O_RDONLY | O_DIRECTORY | O_NOFOLLOW | O_CLOEXEC);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700463 if (subfd < 0) {
464 ALOGE("Couldn't openat %s: %s\n", name, strerror(errno));
465 result = -1;
466 continue;
467 }
468 subdir = fdopendir(subfd);
469 if (subdir == NULL) {
470 ALOGE("Couldn't fdopendir %s: %s\n", name, strerror(errno));
471 close(subfd);
472 result = -1;
473 continue;
474 }
Narayan Kamath3aee2c52014-06-10 13:16:47 +0100475 if (_delete_dir_contents(subdir, exclusion_predicate)) {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700476 result = -1;
477 }
478 closedir(subdir);
479 if (unlinkat(dfd, name, AT_REMOVEDIR) < 0) {
480 ALOGE("Couldn't unlinkat %s: %s\n", name, strerror(errno));
481 result = -1;
482 }
483 } else {
484 if (unlinkat(dfd, name, 0) < 0) {
485 ALOGE("Couldn't unlinkat %s: %s\n", name, strerror(errno));
486 result = -1;
487 }
488 }
489 }
490
491 return result;
492}
493
Calin Juravleb06f98a2016-03-28 15:11:01 +0100494int delete_dir_contents(const std::string& pathname, bool ignore_if_missing) {
495 return delete_dir_contents(pathname.c_str(), 0, NULL, ignore_if_missing);
Jeff Sharkeyebf728f2015-11-18 14:15:17 -0700496}
497
Calin Juravleb06f98a2016-03-28 15:11:01 +0100498int delete_dir_contents_and_dir(const std::string& pathname, bool ignore_if_missing) {
499 return delete_dir_contents(pathname.c_str(), 1, NULL, ignore_if_missing);
Jeff Sharkeyebf728f2015-11-18 14:15:17 -0700500}
501
Mike Lockwood94afecf2012-10-24 10:45:23 -0700502int delete_dir_contents(const char *pathname,
503 int also_delete_dir,
Calin Juravleb06f98a2016-03-28 15:11:01 +0100504 int (*exclusion_predicate)(const char*, const int),
505 bool ignore_if_missing)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700506{
507 int res = 0;
508 DIR *d;
509
510 d = opendir(pathname);
511 if (d == NULL) {
Calin Juravleb06f98a2016-03-28 15:11:01 +0100512 if (ignore_if_missing && (errno == ENOENT)) {
513 return 0;
514 }
Mike Lockwood94afecf2012-10-24 10:45:23 -0700515 ALOGE("Couldn't opendir %s: %s\n", pathname, strerror(errno));
516 return -errno;
517 }
Narayan Kamath3aee2c52014-06-10 13:16:47 +0100518 res = _delete_dir_contents(d, exclusion_predicate);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700519 closedir(d);
520 if (also_delete_dir) {
521 if (rmdir(pathname)) {
522 ALOGE("Couldn't rmdir %s: %s\n", pathname, strerror(errno));
523 res = -1;
524 }
525 }
526 return res;
527}
528
529int delete_dir_contents_fd(int dfd, const char *name)
530{
531 int fd, res;
532 DIR *d;
533
Nick Kralevich8b7acac2015-08-10 13:43:00 -0700534 fd = openat(dfd, name, O_RDONLY | O_DIRECTORY | O_NOFOLLOW | O_CLOEXEC);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700535 if (fd < 0) {
536 ALOGE("Couldn't openat %s: %s\n", name, strerror(errno));
537 return -1;
538 }
539 d = fdopendir(fd);
540 if (d == NULL) {
541 ALOGE("Couldn't fdopendir %s: %s\n", name, strerror(errno));
542 close(fd);
543 return -1;
544 }
545 res = _delete_dir_contents(d, 0);
546 closedir(d);
547 return res;
548}
549
Robin Lee60fd3fe2014-10-07 16:55:02 +0100550static int _copy_owner_permissions(int srcfd, int dstfd)
551{
552 struct stat st;
553 if (fstat(srcfd, &st) != 0) {
554 return -1;
555 }
556 if (fchmod(dstfd, st.st_mode) != 0) {
557 return -1;
558 }
559 return 0;
560}
561
562static int _copy_dir_files(int sdfd, int ddfd, uid_t owner, gid_t group)
563{
564 int result = 0;
565 if (_copy_owner_permissions(sdfd, ddfd) != 0) {
566 ALOGE("_copy_dir_files failed to copy dir permissions\n");
567 }
568 if (fchown(ddfd, owner, group) != 0) {
569 ALOGE("_copy_dir_files failed to change dir owner\n");
570 }
571
572 DIR *ds = fdopendir(sdfd);
573 if (ds == NULL) {
574 ALOGE("Couldn't fdopendir: %s\n", strerror(errno));
575 return -1;
576 }
577 struct dirent *de;
578 while ((de = readdir(ds))) {
579 if (de->d_type != DT_REG) {
580 continue;
581 }
582
583 const char *name = de->d_name;
584 int fsfd = openat(sdfd, name, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
585 int fdfd = openat(ddfd, name, O_WRONLY | O_NOFOLLOW | O_CLOEXEC | O_CREAT, 0600);
586 if (fsfd == -1 || fdfd == -1) {
587 ALOGW("Couldn't copy %s: %s\n", name, strerror(errno));
588 } else {
589 if (_copy_owner_permissions(fsfd, fdfd) != 0) {
590 ALOGE("Failed to change file permissions\n");
591 }
592 if (fchown(fdfd, owner, group) != 0) {
593 ALOGE("Failed to change file owner\n");
594 }
595
596 char buf[8192];
597 ssize_t size;
598 while ((size = read(fsfd, buf, sizeof(buf))) > 0) {
599 write(fdfd, buf, size);
600 }
601 if (size < 0) {
602 ALOGW("Couldn't copy %s: %s\n", name, strerror(errno));
603 result = -1;
604 }
605 }
606 close(fdfd);
607 close(fsfd);
608 }
609
610 return result;
611}
612
613int copy_dir_files(const char *srcname,
614 const char *dstname,
615 uid_t owner,
616 uid_t group)
617{
618 int res = 0;
619 DIR *ds = NULL;
620 DIR *dd = NULL;
621
622 ds = opendir(srcname);
623 if (ds == NULL) {
624 ALOGE("Couldn't opendir %s: %s\n", srcname, strerror(errno));
625 return -errno;
626 }
627
628 mkdir(dstname, 0600);
629 dd = opendir(dstname);
630 if (dd == NULL) {
631 ALOGE("Couldn't opendir %s: %s\n", dstname, strerror(errno));
632 closedir(ds);
633 return -errno;
634 }
635
636 int sdfd = dirfd(ds);
637 int ddfd = dirfd(dd);
638 if (sdfd != -1 && ddfd != -1) {
639 res = _copy_dir_files(sdfd, ddfd, owner, group);
640 } else {
641 res = -errno;
642 }
643 closedir(dd);
644 closedir(ds);
645 return res;
646}
647
Jeff Sharkeya836c472017-04-02 23:29:30 -0600648int64_t data_disk_free(const std::string& data_path) {
Jeff Sharkeyed909ae2017-03-22 21:27:40 -0600649 struct statvfs sfs;
650 if (statvfs(data_path.c_str(), &sfs) == 0) {
Jeff Sharkey4f7be172017-08-11 15:13:31 -0600651 return static_cast<int64_t>(sfs.f_bavail) * sfs.f_frsize;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700652 } else {
Jeff Sharkeyed909ae2017-03-22 21:27:40 -0600653 PLOG(ERROR) << "Couldn't statvfs " << data_path;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700654 return -1;
655 }
656}
657
Jeff Sharkey9a998f42016-07-14 18:16:22 -0600658int get_path_inode(const std::string& path, ino_t *inode) {
659 struct stat buf;
660 memset(&buf, 0, sizeof(buf));
661 if (stat(path.c_str(), &buf) != 0) {
662 PLOG(WARNING) << "Failed to stat " << path;
663 return -1;
664 } else {
665 *inode = buf.st_ino;
666 return 0;
667 }
668}
669
670/**
671 * Write the inode of a specific child file into the given xattr on the
672 * parent directory. This allows you to find the child later, even if its
673 * name is encrypted.
674 */
675int write_path_inode(const std::string& parent, const char* name, const char* inode_xattr) {
676 ino_t inode = 0;
677 uint64_t inode_raw = 0;
678 auto path = StringPrintf("%s/%s", parent.c_str(), name);
679
680 if (get_path_inode(path, &inode) != 0) {
681 // Path probably doesn't exist yet; ignore
682 return 0;
683 }
684
685 // Check to see if already set correctly
686 if (getxattr(parent.c_str(), inode_xattr, &inode_raw, sizeof(inode_raw)) == sizeof(inode_raw)) {
687 if (inode_raw == inode) {
688 // Already set correctly; skip writing
689 return 0;
690 } else {
691 PLOG(WARNING) << "Mismatched inode value; found " << inode
692 << " on disk but marked value was " << inode_raw << "; overwriting";
693 }
694 }
695
696 inode_raw = inode;
Jeff Sharkey4ed65072016-07-22 11:38:54 -0600697 if (setxattr(parent.c_str(), inode_xattr, &inode_raw, sizeof(inode_raw), 0) != 0 && errno != EOPNOTSUPP) {
Jeff Sharkey9a998f42016-07-14 18:16:22 -0600698 PLOG(ERROR) << "Failed to write xattr " << inode_xattr << " at " << parent;
699 return -1;
700 } else {
701 return 0;
702 }
703}
704
705/**
706 * Read the inode of a specific child file from the given xattr on the
707 * parent directory. Returns a currently valid path for that child, which
708 * might have an encrypted name.
709 */
710std::string read_path_inode(const std::string& parent, const char* name, const char* inode_xattr) {
711 ino_t inode = 0;
712 uint64_t inode_raw = 0;
713 auto fallback = StringPrintf("%s/%s", parent.c_str(), name);
714
715 // Lookup the inode value written earlier
716 if (getxattr(parent.c_str(), inode_xattr, &inode_raw, sizeof(inode_raw)) == sizeof(inode_raw)) {
717 inode = inode_raw;
718 }
719
720 // For testing purposes, rely on the inode when defined; this could be
721 // optimized to use access() in the future.
722 if (inode != 0) {
723 DIR* dir = opendir(parent.c_str());
724 if (dir == nullptr) {
725 PLOG(ERROR) << "Failed to opendir " << parent;
726 return fallback;
727 }
728
729 struct dirent* ent;
730 while ((ent = readdir(dir))) {
731 if (ent->d_ino == inode) {
732 auto resolved = StringPrintf("%s/%s", parent.c_str(), ent->d_name);
733#if DEBUG_XATTRS
734 if (resolved != fallback) {
735 LOG(DEBUG) << "Resolved path " << resolved << " for inode " << inode
736 << " instead of " << fallback;
737 }
738#endif
739 closedir(dir);
740 return resolved;
741 }
742 }
743 LOG(WARNING) << "Failed to resolve inode " << inode << "; using " << fallback;
744 closedir(dir);
745 return fallback;
746 } else {
747 return fallback;
748 }
749}
750
Ryuki Nakamurac7342f82017-09-30 11:57:00 +0900751void remove_path_xattr(const std::string& path, const char* inode_xattr) {
752 if (removexattr(path.c_str(), inode_xattr) && errno != ENODATA) {
753 PLOG(ERROR) << "Failed to remove xattr " << inode_xattr << " at " << path;
754 }
755}
756
Mike Lockwood94afecf2012-10-24 10:45:23 -0700757/**
Calin Juravlec597b6d2014-08-19 17:43:05 +0100758 * Validate that the path is valid in the context of the provided directory.
759 * The path is allowed to have at most one subdirectory and no indirections
760 * to top level directories (i.e. have "..").
761 */
Jeff Sharkeyc1149c92017-09-21 14:51:09 -0600762static int validate_path(const std::string& dir, const std::string& path, int maxSubdirs) {
763 // Argument sanity checking
764 if (dir.find('/') != 0 || dir.rfind('/') != dir.size() - 1
765 || dir.find("..") != std::string::npos) {
766 LOG(ERROR) << "Invalid directory " << dir;
767 return -1;
768 }
769 if (path.find("..") != std::string::npos) {
770 LOG(ERROR) << "Invalid path " << path;
771 return -1;
Calin Juravlec597b6d2014-08-19 17:43:05 +0100772 }
773
Jeff Sharkeyc1149c92017-09-21 14:51:09 -0600774 if (path.compare(0, dir.size(), dir) != 0) {
775 // Common case, path isn't under directory
776 return -1;
777 }
778
779 // Count number of subdirectories
780 auto pos = path.find('/', dir.size());
781 int count = 0;
782 while (pos != std::string::npos) {
Jeff Sharkey172fac02017-10-06 13:09:46 -0600783 auto next = path.find('/', pos + 1);
784 if (next > pos + 1) {
785 count++;
786 }
787 pos = next;
Jeff Sharkeyc1149c92017-09-21 14:51:09 -0600788 }
789
790 if (count > maxSubdirs) {
791 LOG(ERROR) << "Invalid path depth " << path << " when tested against " << dir;
Calin Juravlec597b6d2014-08-19 17:43:05 +0100792 return -1;
793 }
794
795 return 0;
796}
797
798/**
Mike Lockwood94afecf2012-10-24 10:45:23 -0700799 * Checks whether a path points to a system app (.apk file). Returns 0
800 * if it is a system app or -1 if it is not.
801 */
802int validate_system_app_path(const char* path) {
Jeff Sharkeyc1149c92017-09-21 14:51:09 -0600803 std::string path_ = path;
804 for (const auto& dir : android_system_dirs) {
805 if (validate_path(dir, path, 1) == 0) {
806 return 0;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700807 }
808 }
Mike Lockwood94afecf2012-10-24 10:45:23 -0700809 return -1;
810}
811
Calin Juravle114f0812017-03-08 19:05:07 -0800812bool validate_secondary_dex_path(const std::string& pkgname, const std::string& dex_path,
Calin Juravle7d765462017-09-04 15:57:10 -0700813 const char* volume_uuid, int uid, int storage_flag) {
Calin Juravle42451c02017-01-17 14:43:25 -0800814 CHECK(storage_flag == FLAG_STORAGE_CE || storage_flag == FLAG_STORAGE_DE);
815
Calin Juravle3760ad32017-07-27 16:31:55 -0700816 // Empty paths are not allowed.
817 if (dex_path.empty()) { return false; }
818 // First character should always be '/'. No relative paths.
819 if (dex_path[0] != '/') { return false; }
820 // The last character should not be '/'.
821 if (dex_path[dex_path.size() - 1] == '/') { return false; }
822 // There should be no '.' after the directory marker.
823 if (dex_path.find("/.") != std::string::npos) { return false; }
824 // The path should be at most PKG_PATH_MAX long.
825 if (dex_path.size() > PKG_PATH_MAX) { return false; }
826
Calin Juravle7d765462017-09-04 15:57:10 -0700827 // The dex_path should be under the app data directory.
828 std::string app_private_dir = storage_flag == FLAG_STORAGE_CE
Calin Juravledd42e272017-09-11 11:50:36 -0700829 ? create_data_user_ce_package_path(
830 volume_uuid, multiuser_get_user_id(uid), pkgname.c_str())
831 : create_data_user_de_package_path(
832 volume_uuid, multiuser_get_user_id(uid), pkgname.c_str());
Calin Juravle3760ad32017-07-27 16:31:55 -0700833
Calin Juravle7d765462017-09-04 15:57:10 -0700834 if (strncmp(dex_path.c_str(), app_private_dir.c_str(), app_private_dir.size()) != 0) {
835 // The check above might fail if the dex file is accessed via the /data/user/0 symlink.
836 // If that's the case, attempt to validate against the user data link.
837 std::string app_private_dir_symlink = create_data_user_ce_package_path_as_user_link(
838 volume_uuid, multiuser_get_user_id(uid), pkgname.c_str());
839 if (strncmp(dex_path.c_str(), app_private_dir_symlink.c_str(),
840 app_private_dir_symlink.size()) != 0) {
Calin Juravledd42e272017-09-11 11:50:36 -0700841 return false;
842 }
Calin Juravle42451c02017-01-17 14:43:25 -0800843 }
Calin Juravle3760ad32017-07-27 16:31:55 -0700844
845 // If we got here we have a valid path.
846 return true;
Calin Juravle42451c02017-01-17 14:43:25 -0800847}
848
Mike Lockwood94afecf2012-10-24 10:45:23 -0700849/**
Narayan Kamathd845c962015-06-04 13:20:27 +0100850 * Check whether path points to a valid path for an APK file. The path must
851 * begin with a whitelisted prefix path and must be no deeper than |maxSubdirs| within
852 * that path. Returns -1 when an invalid path is encountered and 0 when a valid path
853 * is encountered.
Mike Lockwood94afecf2012-10-24 10:45:23 -0700854 */
Narayan Kamathd845c962015-06-04 13:20:27 +0100855static int validate_apk_path_internal(const char *path, int maxSubdirs) {
Jeff Sharkeyc1149c92017-09-21 14:51:09 -0600856 std::string path_ = path;
857 if (validate_path(android_app_dir, path_, maxSubdirs) == 0) {
858 return 0;
859 } else if (validate_path(android_app_private_dir, path_, maxSubdirs) == 0) {
860 return 0;
861 } else if (validate_path(android_app_ephemeral_dir, path_, maxSubdirs) == 0) {
862 return 0;
863 } else if (validate_path(android_asec_dir, path_, maxSubdirs) == 0) {
864 return 0;
865 } else if (validate_path(android_mnt_expand_dir, path_, std::max(maxSubdirs, 2)) == 0) {
866 return 0;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700867 } else {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700868 return -1;
869 }
Mike Lockwood94afecf2012-10-24 10:45:23 -0700870}
871
Narayan Kamathd845c962015-06-04 13:20:27 +0100872int validate_apk_path(const char* path) {
873 return validate_apk_path_internal(path, 1 /* maxSubdirs */);
874}
875
876int validate_apk_path_subdirs(const char* path) {
877 return validate_apk_path_internal(path, 3 /* maxSubdirs */);
878}
879
Robin Lee095c7632014-04-25 15:05:19 +0100880int ensure_config_user_dirs(userid_t userid) {
Robin Lee095c7632014-04-25 15:05:19 +0100881 // writable by system, readable by any app within the same user
Robin Lee60fd3fe2014-10-07 16:55:02 +0100882 const int uid = multiuser_get_uid(userid, AID_SYSTEM);
883 const int gid = multiuser_get_uid(userid, AID_EVERYBODY);
Robin Lee095c7632014-04-25 15:05:19 +0100884
885 // Ensure /data/misc/user/<userid> exists
Jeff Sharkey379a12b2016-04-14 20:45:06 -0600886 auto path = create_data_misc_legacy_path(userid);
887 return fs_prepare_dir(path.c_str(), 0750, uid, gid);
Robin Lee095c7632014-04-25 15:05:19 +0100888}
Andreas Gampe02d0de52015-11-11 20:43:16 -0800889
890int wait_child(pid_t pid)
891{
892 int status;
893 pid_t got_pid;
894
895 while (1) {
896 got_pid = waitpid(pid, &status, 0);
897 if (got_pid == -1 && errno == EINTR) {
898 printf("waitpid interrupted, retrying\n");
899 } else {
900 break;
901 }
902 }
903 if (got_pid != pid) {
904 ALOGW("waitpid failed: wanted %d, got %d: %s\n",
905 (int) pid, (int) got_pid, strerror(errno));
906 return 1;
907 }
908
909 if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
910 return 0;
911 } else {
912 return status; /* always nonzero */
913 }
914}
915
Calin Juravle42451c02017-01-17 14:43:25 -0800916/**
917 * Prepare an app cache directory, which offers to fix-up the GID and
918 * directory mode flags during a platform upgrade.
919 * The app cache directory path will be 'parent'/'name'.
920 */
921int prepare_app_cache_dir(const std::string& parent, const char* name, mode_t target_mode,
922 uid_t uid, gid_t gid) {
923 auto path = StringPrintf("%s/%s", parent.c_str(), name);
924 struct stat st;
925 if (stat(path.c_str(), &st) != 0) {
926 if (errno == ENOENT) {
927 // This is fine, just create it
928 if (fs_prepare_dir_strict(path.c_str(), target_mode, uid, gid) != 0) {
929 PLOG(ERROR) << "Failed to prepare " << path;
930 return -1;
931 } else {
932 return 0;
933 }
934 } else {
935 PLOG(ERROR) << "Failed to stat " << path;
936 return -1;
937 }
938 }
939
940 mode_t actual_mode = st.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO | S_ISGID);
941 if (st.st_uid != uid) {
942 // Mismatched UID is real trouble; we can't recover
943 LOG(ERROR) << "Mismatched UID at " << path << ": found " << st.st_uid
944 << " but expected " << uid;
945 return -1;
946 } else if (st.st_gid == gid && actual_mode == target_mode) {
947 // Everything looks good!
948 return 0;
Jeff Sharkeye59c85c2017-04-02 21:53:14 -0600949 } else {
950 // Mismatched GID/mode is recoverable; fall through to update
951 LOG(DEBUG) << "Mismatched cache GID/mode at " << path << ": found " << st.st_gid
Shubham Ajmeraec0afbf2017-09-14 11:07:33 -0700952 << "/" << actual_mode << " but expected " << gid << "/" << target_mode;
Calin Juravle42451c02017-01-17 14:43:25 -0800953 }
954
955 // Directory is owned correctly, but GID or mode mismatch means it's
956 // probably a platform upgrade so we need to fix them
957 FTS *fts;
958 FTSENT *p;
959 char *argv[] = { (char*) path.c_str(), nullptr };
Jeff Sharkeyb26786d2017-03-11 19:40:29 -0700960 if (!(fts = fts_open(argv, FTS_PHYSICAL | FTS_NOCHDIR | FTS_XDEV, NULL))) {
Calin Juravle42451c02017-01-17 14:43:25 -0800961 PLOG(ERROR) << "Failed to fts_open " << path;
962 return -1;
963 }
964 while ((p = fts_read(fts)) != NULL) {
965 switch (p->fts_info) {
966 case FTS_DP:
Jeff Sharkeye12d5962017-04-03 16:41:02 -0600967 if (chmod(p->fts_path, target_mode) != 0) {
Calin Juravle42451c02017-01-17 14:43:25 -0800968 PLOG(WARNING) << "Failed to chmod " << p->fts_path;
969 }
970 // Intentional fall through to also set GID
971 case FTS_F:
Jeff Sharkeye12d5962017-04-03 16:41:02 -0600972 if (chown(p->fts_path, -1, gid) != 0) {
Calin Juravle42451c02017-01-17 14:43:25 -0800973 PLOG(WARNING) << "Failed to chown " << p->fts_path;
974 }
975 break;
976 case FTS_SL:
977 case FTS_SLNONE:
Jeff Sharkeye12d5962017-04-03 16:41:02 -0600978 if (lchown(p->fts_path, -1, gid) != 0) {
Calin Juravle42451c02017-01-17 14:43:25 -0800979 PLOG(WARNING) << "Failed to chown " << p->fts_path;
980 }
981 break;
982 }
983 }
984 fts_close(fts);
985 return 0;
986}
987
Andreas Gampe02d0de52015-11-11 20:43:16 -0800988} // namespace installd
989} // namespace android