blob: 61c9c8ff084b8db151f472cac102260567638166 [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>
Andreas Gampe02d0de52015-11-11 20:43:16 -080029#include <android-base/stringprintf.h>
30#include <cutils/fs.h>
Jeff Sharkey871a8f22017-02-21 18:30:28 -070031#include <cutils/properties.h>
Mark Salyzyn7823e122016-09-29 08:08:05 -070032#include <log/log.h>
Andreas Gampe02d0de52015-11-11 20:43:16 -080033#include <private/android_filesystem_config.h>
Jeff Sharkeyc03de092015-04-07 18:14:05 -070034
Andreas Gampe02d0de52015-11-11 20:43:16 -080035#include "globals.h" // extern variables.
36
37#ifndef LOG_TAG
38#define LOG_TAG "installd"
39#endif
Jeff Sharkey9a998f42016-07-14 18:16:22 -060040
Jeff Sharkey9a998f42016-07-14 18:16:22 -060041#define DEBUG_XATTRS 0
Mike Lockwood94afecf2012-10-24 10:45:23 -070042
Jeff Sharkeyc03de092015-04-07 18:14:05 -070043using android::base::StringPrintf;
Mike Lockwood94afecf2012-10-24 10:45:23 -070044
Andreas Gampe02d0de52015-11-11 20:43:16 -080045namespace android {
46namespace installd {
47
Jeff Sharkeyc03de092015-04-07 18:14:05 -070048/**
49 * Check that given string is valid filename, and that it attempts no
50 * parent or child directory traversal.
51 */
Jeff Sharkey423e7462016-12-09 18:18:43 -070052bool is_valid_filename(const std::string& name) {
Jeff Sharkeyc03de092015-04-07 18:14:05 -070053 if (name.empty() || (name == ".") || (name == "..")
54 || (name.find('/') != std::string::npos)) {
55 return false;
56 } else {
57 return true;
58 }
Mike Lockwood94afecf2012-10-24 10:45:23 -070059}
60
Calin Juravle6a1648e2016-02-01 12:12:16 +000061static void check_package_name(const char* package_name) {
62 CHECK(is_valid_filename(package_name));
Jeff Sharkey423e7462016-12-09 18:18:43 -070063 CHECK(is_valid_package_name(package_name));
Calin Juravle6a1648e2016-02-01 12:12:16 +000064}
65
Mike Lockwood94afecf2012-10-24 10:45:23 -070066/**
Jeff Sharkeyd7921182015-04-30 15:58:19 -070067 * Create the path name where package app contents should be stored for
68 * the given volume UUID and package name. An empty UUID is assumed to
69 * be internal storage.
70 */
71std::string create_data_app_package_path(const char* volume_uuid,
72 const char* package_name) {
Calin Juravle6a1648e2016-02-01 12:12:16 +000073 check_package_name(package_name);
Jeff Sharkeyd7921182015-04-30 15:58:19 -070074 return StringPrintf("%s/%s",
75 create_data_app_path(volume_uuid).c_str(), package_name);
76}
77
78/**
Jeff Sharkeyc03de092015-04-07 18:14:05 -070079 * Create the path name where package data should be stored for the given
80 * volume UUID, package name, and user ID. An empty UUID is assumed to be
81 * internal storage.
Mike Lockwood94afecf2012-10-24 10:45:23 -070082 */
Jeff Sharkey2f720f72016-04-10 20:51:40 -060083std::string create_data_user_ce_package_path(const char* volume_uuid,
Jeff Sharkeyd7921182015-04-30 15:58:19 -070084 userid_t user, const char* package_name) {
Calin Juravle6a1648e2016-02-01 12:12:16 +000085 check_package_name(package_name);
Jeff Sharkeyd7921182015-04-30 15:58:19 -070086 return StringPrintf("%s/%s",
Jeff Sharkey2f720f72016-04-10 20:51:40 -060087 create_data_user_ce_path(volume_uuid, user).c_str(), package_name);
88}
89
Calin Juravle7d765462017-09-04 15:57:10 -070090/**
91 * Create the path name where package data should be stored for the given
92 * volume UUID, package name, and user ID. An empty UUID is assumed to be
93 * internal storage.
94 * Compared to create_data_user_ce_package_path this method always return the
95 * ".../user/..." directory.
96 */
97std::string create_data_user_ce_package_path_as_user_link(
98 const char* volume_uuid, userid_t userid, const char* package_name) {
99 check_package_name(package_name);
100 std::string data(create_data_path(volume_uuid));
101 return StringPrintf("%s/user/%u/%s", data.c_str(), userid, package_name);
102}
103
Jeff Sharkey2f720f72016-04-10 20:51:40 -0600104std::string create_data_user_ce_package_path(const char* volume_uuid, userid_t user,
105 const char* package_name, ino_t ce_data_inode) {
106 // For testing purposes, rely on the inode when defined; this could be
107 // optimized to use access() in the future.
108 auto fallback = create_data_user_ce_package_path(volume_uuid, user, package_name);
109 if (ce_data_inode != 0) {
110 auto user_path = create_data_user_ce_path(volume_uuid, user);
111 DIR* dir = opendir(user_path.c_str());
112 if (dir == nullptr) {
113 PLOG(ERROR) << "Failed to opendir " << user_path;
114 return fallback;
115 }
116
117 struct dirent* ent;
118 while ((ent = readdir(dir))) {
119 if (ent->d_ino == ce_data_inode) {
Jeff Sharkey1d992f92016-04-13 13:45:47 -0600120 auto resolved = StringPrintf("%s/%s", user_path.c_str(), ent->d_name);
Jeff Sharkey9a998f42016-07-14 18:16:22 -0600121#if DEBUG_XATTRS
Jeff Sharkey1d992f92016-04-13 13:45:47 -0600122 if (resolved != fallback) {
123 LOG(DEBUG) << "Resolved path " << resolved << " for inode " << ce_data_inode
124 << " instead of " << fallback;
125 }
Jeff Sharkey9a998f42016-07-14 18:16:22 -0600126#endif
Jeff Sharkey2f720f72016-04-10 20:51:40 -0600127 closedir(dir);
Jeff Sharkey1d992f92016-04-13 13:45:47 -0600128 return resolved;
Jeff Sharkey2f720f72016-04-10 20:51:40 -0600129 }
130 }
Jeff Sharkey1d992f92016-04-13 13:45:47 -0600131 LOG(WARNING) << "Failed to resolve inode " << ce_data_inode << "; using " << fallback;
Jeff Sharkey2f720f72016-04-10 20:51:40 -0600132 closedir(dir);
133 return fallback;
134 } else {
135 return fallback;
136 }
Jeff Sharkeyc03de092015-04-07 18:14:05 -0700137}
Mike Lockwood94afecf2012-10-24 10:45:23 -0700138
Jeff Sharkey63ec2d62015-11-09 13:10:36 -0800139std::string create_data_user_de_package_path(const char* volume_uuid,
140 userid_t user, const char* package_name) {
Calin Juravle6a1648e2016-02-01 12:12:16 +0000141 check_package_name(package_name);
Jeff Sharkey63ec2d62015-11-09 13:10:36 -0800142 return StringPrintf("%s/%s",
143 create_data_user_de_path(volume_uuid, user).c_str(), package_name);
144}
145
Jeff Sharkey41ea4242015-04-09 11:34:03 -0700146std::string create_data_path(const char* volume_uuid) {
147 if (volume_uuid == nullptr) {
148 return "/data";
Jeff Sharkey871a8f22017-02-21 18:30:28 -0700149 } else if (!strcmp(volume_uuid, "TEST")) {
150 CHECK(property_get_bool("ro.debuggable", false));
151 return "/data/local/tmp";
Jeff Sharkey41ea4242015-04-09 11:34:03 -0700152 } else {
153 CHECK(is_valid_filename(volume_uuid));
154 return StringPrintf("/mnt/expand/%s", volume_uuid);
155 }
156}
157
Mike Lockwood94afecf2012-10-24 10:45:23 -0700158/**
Jeff Sharkeyd7921182015-04-30 15:58:19 -0700159 * Create the path name for app data.
160 */
161std::string create_data_app_path(const char* volume_uuid) {
162 return StringPrintf("%s/app", create_data_path(volume_uuid).c_str());
163}
164
165/**
Jeff Sharkeyabe4fe52013-07-10 16:55:46 -0700166 * Create the path name for user data for a certain userid.
cjbao75d4e572017-04-12 00:12:24 +0800167 * Keep same implementation as vold to minimize path walking overhead
Mike Lockwood94afecf2012-10-24 10:45:23 -0700168 */
Jeff Sharkey2f720f72016-04-10 20:51:40 -0600169std::string create_data_user_ce_path(const char* volume_uuid, userid_t userid) {
Jeff Sharkey41ea4242015-04-09 11:34:03 -0700170 std::string data(create_data_path(volume_uuid));
cjbao75d4e572017-04-12 00:12:24 +0800171 if (volume_uuid == nullptr && userid == 0) {
172 std::string legacy = StringPrintf("%s/data", data.c_str());
173 struct stat sb;
174 if (lstat(legacy.c_str(), &sb) == 0 && S_ISDIR(sb.st_mode)) {
175 /* /data/data is dir, return /data/data for legacy system */
176 return legacy;
Jeff Sharkey41ea4242015-04-09 11:34:03 -0700177 }
Mike Lockwood94afecf2012-10-24 10:45:23 -0700178 }
cjbao75d4e572017-04-12 00:12:24 +0800179 return StringPrintf("%s/user/%u", data.c_str(), userid);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700180}
181
182/**
Jeff Sharkey63ec2d62015-11-09 13:10:36 -0800183 * Create the path name for device encrypted user data for a certain userid.
184 */
185std::string create_data_user_de_path(const char* volume_uuid, userid_t userid) {
186 std::string data(create_data_path(volume_uuid));
187 return StringPrintf("%s/user_de/%u", data.c_str(), userid);
188}
189
190/**
Jeff Sharkeyabe4fe52013-07-10 16:55:46 -0700191 * Create the path name for media for a certain userid.
Mike Lockwood94afecf2012-10-24 10:45:23 -0700192 */
Jeff Sharkey41ea4242015-04-09 11:34:03 -0700193std::string create_data_media_path(const char* volume_uuid, userid_t userid) {
194 return StringPrintf("%s/media/%u", create_data_path(volume_uuid).c_str(), userid);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700195}
196
Jeff Sharkeydf2d7542017-01-07 09:19:35 -0700197std::string create_data_media_obb_path(const char* volume_uuid, const char* package_name) {
198 return StringPrintf("%s/media/obb/%s", create_data_path(volume_uuid).c_str(), package_name);
199}
200
Jeff Sharkey3dfae0c2016-12-12 17:32:56 -0700201std::string create_data_media_package_path(const char* volume_uuid, userid_t userid,
202 const char* data_type, const char* package_name) {
203 return StringPrintf("%s/Android/%s/%s", create_data_media_path(volume_uuid, userid).c_str(),
204 data_type, package_name);
205}
206
Jeff Sharkey379a12b2016-04-14 20:45:06 -0600207std::string create_data_misc_legacy_path(userid_t userid) {
208 return StringPrintf("%s/misc/user/%u", create_data_path(nullptr).c_str(), userid);
209}
210
Calin Juravle114f0812017-03-08 19:05:07 -0800211std::string create_primary_cur_profile_dir_path(userid_t userid) {
Jeff Sharkeyc1149c92017-09-21 14:51:09 -0600212 return StringPrintf("%s/cur/%u", android_profiles_dir.c_str(), userid);
Calin Juravle6a1648e2016-02-01 12:12:16 +0000213}
214
Calin Juravle114f0812017-03-08 19:05:07 -0800215std::string create_primary_current_profile_package_dir_path(userid_t user,
216 const std::string& package_name) {
Calin Juravle76268c52017-03-09 13:19:42 -0800217 check_package_name(package_name.c_str());
Calin Juravle114f0812017-03-08 19:05:07 -0800218 return StringPrintf("%s/%s",
219 create_primary_cur_profile_dir_path(user).c_str(), package_name.c_str());
Jeff Sharkeydf2d7542017-01-07 09:19:35 -0700220}
221
Calin Juravle114f0812017-03-08 19:05:07 -0800222std::string create_primary_ref_profile_dir_path() {
Jeff Sharkeyc1149c92017-09-21 14:51:09 -0600223 return StringPrintf("%s/ref", android_profiles_dir.c_str());
Calin Juravle6a1648e2016-02-01 12:12:16 +0000224}
225
Calin Juravle114f0812017-03-08 19:05:07 -0800226std::string create_primary_reference_profile_package_dir_path(const std::string& package_name) {
Calin Juravle76268c52017-03-09 13:19:42 -0800227 check_package_name(package_name.c_str());
Jeff Sharkeyc1149c92017-09-21 14:51:09 -0600228 return StringPrintf("%s/ref/%s", android_profiles_dir.c_str(), package_name.c_str());
Calin Juravle6a1648e2016-02-01 12:12:16 +0000229}
230
Jeff Sharkey3dfae0c2016-12-12 17:32:56 -0700231std::string create_data_dalvik_cache_path() {
232 return "/data/dalvik-cache";
233}
234
Calin Juravle114f0812017-03-08 19:05:07 -0800235// Keep profile paths in sync with ActivityThread and LoadedApk.
236const std::string PROFILE_EXT = ".prof";
Calin Juravle3760ad32017-07-27 16:31:55 -0700237const std::string CURRENT_PROFILE_EXT = ".cur";
Calin Juravle114f0812017-03-08 19:05:07 -0800238const std::string PRIMARY_PROFILE_NAME = "primary" + PROFILE_EXT;
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 Juravle114f0812017-03-08 19:05:07 -0800259std::string create_current_profile_path(userid_t user, const std::string& location,
260 bool is_secondary_dex) {
261 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.
272 std::string profile_dir = create_primary_current_profile_package_dir_path(user, location);
273 return StringPrintf("%s/%s", profile_dir.c_str(), PRIMARY_PROFILE_NAME.c_str());
274 }
275}
276
277std::string create_reference_profile_path(const std::string& location, bool is_secondary_dex) {
278 if (is_secondary_dex) {
279 // Secondary dex reference profiles are stored next to the dex files under the oat folder.
Calin Juravle3760ad32017-07-27 16:31:55 -0700280 std::string dex_dir;
281 std::string dex_name;
282 CHECK(get_secondary_dex_location(location, &dex_dir, &dex_name))
Calin Juravle114f0812017-03-08 19:05:07 -0800283 << "Unexpected dir structure for secondary dex " << location;
Calin Juravle114f0812017-03-08 19:05:07 -0800284 return StringPrintf("%s/oat/%s%s",
285 dex_dir.c_str(), dex_name.c_str(), PROFILE_EXT.c_str());
286 } else {
287 // Reference profiles for primary apks are stored in /data/misc/profile/ref.
288 std::string profile_dir = create_primary_reference_profile_package_dir_path(location);
289 return StringPrintf("%s/%s", profile_dir.c_str(), PRIMARY_PROFILE_NAME.c_str());
290 }
Jeff Sharkey90aff262016-12-12 14:28:24 -0700291}
292
Calin Juravle29591732017-11-20 17:46:19 -0800293std::string create_snapshot_profile_path(const std::string& package,
294 const std::string& code_path ATTRIBUTE_UNUSED) {
295 // TODD(calin): code_path is ignored for now. It will be used when each split gets its own
296 // profile file.
297 std::string ref_profile = create_reference_profile_path(package, /*is_secondary_dex*/ false);
298 return ref_profile + SNAPSHOT_PROFILE_EXT;
299}
300
Jeff Sharkeye3637242015-04-08 20:56:42 -0700301std::vector<userid_t> get_known_users(const char* volume_uuid) {
302 std::vector<userid_t> users;
303
304 // We always have an owner
305 users.push_back(0);
306
307 std::string path(create_data_path(volume_uuid) + "/" + SECONDARY_USER_PREFIX);
308 DIR* dir = opendir(path.c_str());
309 if (dir == NULL) {
310 // Unable to discover other users, but at least return owner
311 PLOG(ERROR) << "Failed to opendir " << path;
312 return users;
313 }
314
315 struct dirent* ent;
316 while ((ent = readdir(dir))) {
317 if (ent->d_type != DT_DIR) {
318 continue;
319 }
320
321 char* end;
322 userid_t user = strtol(ent->d_name, &end, 10);
323 if (*end == '\0' && user != 0) {
324 LOG(DEBUG) << "Found valid user " << user;
325 users.push_back(user);
326 }
327 }
328 closedir(dir);
329
330 return users;
331}
332
Jeff Sharkey3dfae0c2016-12-12 17:32:56 -0700333int calculate_tree_size(const std::string& path, int64_t* size,
Jeff Sharkeydf2d7542017-01-07 09:19:35 -0700334 int32_t include_gid, int32_t exclude_gid, bool exclude_apps) {
Jeff Sharkey3dfae0c2016-12-12 17:32:56 -0700335 FTS *fts;
336 FTSENT *p;
Jeff Sharkeydf2d7542017-01-07 09:19:35 -0700337 int64_t matchedSize = 0;
Jeff Sharkey3dfae0c2016-12-12 17:32:56 -0700338 char *argv[] = { (char*) path.c_str(), nullptr };
Jeff Sharkeyb26786d2017-03-11 19:40:29 -0700339 if (!(fts = fts_open(argv, FTS_PHYSICAL | FTS_NOCHDIR | FTS_XDEV, NULL))) {
Jeff Sharkey3dfae0c2016-12-12 17:32:56 -0700340 if (errno != ENOENT) {
341 PLOG(ERROR) << "Failed to fts_open " << path;
342 }
343 return -1;
344 }
345 while ((p = fts_read(fts)) != NULL) {
346 switch (p->fts_info) {
347 case FTS_D:
348 case FTS_DEFAULT:
349 case FTS_F:
350 case FTS_SL:
351 case FTS_SLNONE:
Jeff Sharkeydf2d7542017-01-07 09:19:35 -0700352 int32_t uid = p->fts_statp->st_uid;
353 int32_t gid = p->fts_statp->st_gid;
354 int32_t user_uid = multiuser_get_app_id(uid);
355 int32_t user_gid = multiuser_get_app_id(gid);
356 if (exclude_apps && ((user_uid >= AID_APP_START && user_uid <= AID_APP_END)
357 || (user_gid >= AID_CACHE_GID_START && user_gid <= AID_CACHE_GID_END)
358 || (user_gid >= AID_SHARED_GID_START && user_gid <= AID_SHARED_GID_END))) {
359 // Don't traverse inside or measure
360 fts_set(fts, p, FTS_SKIP);
Jeff Sharkey3dfae0c2016-12-12 17:32:56 -0700361 break;
362 }
Jeff Sharkeydf2d7542017-01-07 09:19:35 -0700363 if (include_gid != -1 && gid != include_gid) {
Jeff Sharkey3dfae0c2016-12-12 17:32:56 -0700364 break;
365 }
Jeff Sharkeydf2d7542017-01-07 09:19:35 -0700366 if (exclude_gid != -1 && gid == exclude_gid) {
367 break;
368 }
369 matchedSize += (p->fts_statp->st_blocks * 512);
Jeff Sharkey3dfae0c2016-12-12 17:32:56 -0700370 break;
371 }
372 }
373 fts_close(fts);
Jeff Sharkeydf2d7542017-01-07 09:19:35 -0700374#if MEASURE_DEBUG
375 if ((include_gid == -1) && (exclude_gid == -1)) {
376 LOG(DEBUG) << "Measured " << path << " size " << matchedSize;
377 } else {
378 LOG(DEBUG) << "Measured " << path << " size " << matchedSize << "; include " << include_gid
379 << " exclude " << exclude_gid;
380 }
381#endif
382 *size += matchedSize;
Jeff Sharkey3dfae0c2016-12-12 17:32:56 -0700383 return 0;
384}
385
Mike Lockwood94afecf2012-10-24 10:45:23 -0700386/**
387 * Checks whether the package name is valid. Returns -1 on error and
388 * 0 on success.
389 */
Jeff Sharkey423e7462016-12-09 18:18:43 -0700390bool is_valid_package_name(const std::string& packageName) {
Jeff Sharkey367ace22017-03-07 22:12:03 -0700391 // This logic is borrowed from PackageParser.java
392 bool hasSep = false;
393 bool front = true;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700394
Jeff Sharkey367ace22017-03-07 22:12:03 -0700395 auto it = packageName.begin();
396 for (; it != packageName.end() && *it != '-'; it++) {
397 char c = *it;
398 if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) {
399 front = false;
400 continue;
401 }
402 if (!front) {
403 if ((c >= '0' && c <= '9') || c == '_') {
404 continue;
405 }
406 }
407 if (c == '.') {
408 hasSep = true;
409 front = true;
410 continue;
411 }
412 LOG(WARNING) << "Bad package character " << c << " in " << packageName;
Jeff Sharkey423e7462016-12-09 18:18:43 -0700413 return false;
Jeff Sharkeyc03de092015-04-07 18:14:05 -0700414 }
415
Jeff Sharkeyab7ac8d2017-03-08 12:39:46 -0700416 if (front) {
Jeff Sharkey367ace22017-03-07 22:12:03 -0700417 LOG(WARNING) << "Missing separator in " << packageName;
418 return false;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700419 }
420
Jeff Sharkey367ace22017-03-07 22:12:03 -0700421 for (; it != packageName.end(); it++) {
422 char c = *it;
423 if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) continue;
424 if ((c >= '0' && c <= '9') || c == '_' || c == '-' || c == '=') continue;
425 LOG(WARNING) << "Bad suffix character " << c << " in " << packageName;
426 return false;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700427 }
428
Jeff Sharkey423e7462016-12-09 18:18:43 -0700429 return true;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700430}
431
Narayan Kamath3aee2c52014-06-10 13:16:47 +0100432static int _delete_dir_contents(DIR *d,
433 int (*exclusion_predicate)(const char *name, const int is_dir))
Mike Lockwood94afecf2012-10-24 10:45:23 -0700434{
435 int result = 0;
436 struct dirent *de;
437 int dfd;
438
439 dfd = dirfd(d);
440
441 if (dfd < 0) return -1;
442
443 while ((de = readdir(d))) {
444 const char *name = de->d_name;
445
Narayan Kamath3aee2c52014-06-10 13:16:47 +0100446 /* check using the exclusion predicate, if provided */
447 if (exclusion_predicate && exclusion_predicate(name, (de->d_type == DT_DIR))) {
448 continue;
449 }
Mike Lockwood94afecf2012-10-24 10:45:23 -0700450
451 if (de->d_type == DT_DIR) {
Chih-Hung Hsieh99d9fb12014-09-11 14:44:46 -0700452 int subfd;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700453 DIR *subdir;
454
455 /* always skip "." and ".." */
456 if (name[0] == '.') {
457 if (name[1] == 0) continue;
458 if ((name[1] == '.') && (name[2] == 0)) continue;
459 }
460
Nick Kralevich8b7acac2015-08-10 13:43:00 -0700461 subfd = openat(dfd, name, O_RDONLY | O_DIRECTORY | O_NOFOLLOW | O_CLOEXEC);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700462 if (subfd < 0) {
463 ALOGE("Couldn't openat %s: %s\n", name, strerror(errno));
464 result = -1;
465 continue;
466 }
467 subdir = fdopendir(subfd);
468 if (subdir == NULL) {
469 ALOGE("Couldn't fdopendir %s: %s\n", name, strerror(errno));
470 close(subfd);
471 result = -1;
472 continue;
473 }
Narayan Kamath3aee2c52014-06-10 13:16:47 +0100474 if (_delete_dir_contents(subdir, exclusion_predicate)) {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700475 result = -1;
476 }
477 closedir(subdir);
478 if (unlinkat(dfd, name, AT_REMOVEDIR) < 0) {
479 ALOGE("Couldn't unlinkat %s: %s\n", name, strerror(errno));
480 result = -1;
481 }
482 } else {
483 if (unlinkat(dfd, name, 0) < 0) {
484 ALOGE("Couldn't unlinkat %s: %s\n", name, strerror(errno));
485 result = -1;
486 }
487 }
488 }
489
490 return result;
491}
492
Calin Juravleb06f98a2016-03-28 15:11:01 +0100493int delete_dir_contents(const std::string& pathname, bool ignore_if_missing) {
494 return delete_dir_contents(pathname.c_str(), 0, NULL, ignore_if_missing);
Jeff Sharkeyebf728f2015-11-18 14:15:17 -0700495}
496
Calin Juravleb06f98a2016-03-28 15:11:01 +0100497int delete_dir_contents_and_dir(const std::string& pathname, bool ignore_if_missing) {
498 return delete_dir_contents(pathname.c_str(), 1, NULL, ignore_if_missing);
Jeff Sharkeyebf728f2015-11-18 14:15:17 -0700499}
500
Mike Lockwood94afecf2012-10-24 10:45:23 -0700501int delete_dir_contents(const char *pathname,
502 int also_delete_dir,
Calin Juravleb06f98a2016-03-28 15:11:01 +0100503 int (*exclusion_predicate)(const char*, const int),
504 bool ignore_if_missing)
Mike Lockwood94afecf2012-10-24 10:45:23 -0700505{
506 int res = 0;
507 DIR *d;
508
509 d = opendir(pathname);
510 if (d == NULL) {
Calin Juravleb06f98a2016-03-28 15:11:01 +0100511 if (ignore_if_missing && (errno == ENOENT)) {
512 return 0;
513 }
Mike Lockwood94afecf2012-10-24 10:45:23 -0700514 ALOGE("Couldn't opendir %s: %s\n", pathname, strerror(errno));
515 return -errno;
516 }
Narayan Kamath3aee2c52014-06-10 13:16:47 +0100517 res = _delete_dir_contents(d, exclusion_predicate);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700518 closedir(d);
519 if (also_delete_dir) {
520 if (rmdir(pathname)) {
521 ALOGE("Couldn't rmdir %s: %s\n", pathname, strerror(errno));
522 res = -1;
523 }
524 }
525 return res;
526}
527
528int delete_dir_contents_fd(int dfd, const char *name)
529{
530 int fd, res;
531 DIR *d;
532
Nick Kralevich8b7acac2015-08-10 13:43:00 -0700533 fd = openat(dfd, name, O_RDONLY | O_DIRECTORY | O_NOFOLLOW | O_CLOEXEC);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700534 if (fd < 0) {
535 ALOGE("Couldn't openat %s: %s\n", name, strerror(errno));
536 return -1;
537 }
538 d = fdopendir(fd);
539 if (d == NULL) {
540 ALOGE("Couldn't fdopendir %s: %s\n", name, strerror(errno));
541 close(fd);
542 return -1;
543 }
544 res = _delete_dir_contents(d, 0);
545 closedir(d);
546 return res;
547}
548
Robin Lee60fd3fe2014-10-07 16:55:02 +0100549static int _copy_owner_permissions(int srcfd, int dstfd)
550{
551 struct stat st;
552 if (fstat(srcfd, &st) != 0) {
553 return -1;
554 }
555 if (fchmod(dstfd, st.st_mode) != 0) {
556 return -1;
557 }
558 return 0;
559}
560
561static int _copy_dir_files(int sdfd, int ddfd, uid_t owner, gid_t group)
562{
563 int result = 0;
564 if (_copy_owner_permissions(sdfd, ddfd) != 0) {
565 ALOGE("_copy_dir_files failed to copy dir permissions\n");
566 }
567 if (fchown(ddfd, owner, group) != 0) {
568 ALOGE("_copy_dir_files failed to change dir owner\n");
569 }
570
571 DIR *ds = fdopendir(sdfd);
572 if (ds == NULL) {
573 ALOGE("Couldn't fdopendir: %s\n", strerror(errno));
574 return -1;
575 }
576 struct dirent *de;
577 while ((de = readdir(ds))) {
578 if (de->d_type != DT_REG) {
579 continue;
580 }
581
582 const char *name = de->d_name;
583 int fsfd = openat(sdfd, name, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
584 int fdfd = openat(ddfd, name, O_WRONLY | O_NOFOLLOW | O_CLOEXEC | O_CREAT, 0600);
585 if (fsfd == -1 || fdfd == -1) {
586 ALOGW("Couldn't copy %s: %s\n", name, strerror(errno));
587 } else {
588 if (_copy_owner_permissions(fsfd, fdfd) != 0) {
589 ALOGE("Failed to change file permissions\n");
590 }
591 if (fchown(fdfd, owner, group) != 0) {
592 ALOGE("Failed to change file owner\n");
593 }
594
595 char buf[8192];
596 ssize_t size;
597 while ((size = read(fsfd, buf, sizeof(buf))) > 0) {
598 write(fdfd, buf, size);
599 }
600 if (size < 0) {
601 ALOGW("Couldn't copy %s: %s\n", name, strerror(errno));
602 result = -1;
603 }
604 }
605 close(fdfd);
606 close(fsfd);
607 }
608
609 return result;
610}
611
612int copy_dir_files(const char *srcname,
613 const char *dstname,
614 uid_t owner,
615 uid_t group)
616{
617 int res = 0;
618 DIR *ds = NULL;
619 DIR *dd = NULL;
620
621 ds = opendir(srcname);
622 if (ds == NULL) {
623 ALOGE("Couldn't opendir %s: %s\n", srcname, strerror(errno));
624 return -errno;
625 }
626
627 mkdir(dstname, 0600);
628 dd = opendir(dstname);
629 if (dd == NULL) {
630 ALOGE("Couldn't opendir %s: %s\n", dstname, strerror(errno));
631 closedir(ds);
632 return -errno;
633 }
634
635 int sdfd = dirfd(ds);
636 int ddfd = dirfd(dd);
637 if (sdfd != -1 && ddfd != -1) {
638 res = _copy_dir_files(sdfd, ddfd, owner, group);
639 } else {
640 res = -errno;
641 }
642 closedir(dd);
643 closedir(ds);
644 return res;
645}
646
Jeff Sharkeya836c472017-04-02 23:29:30 -0600647int64_t data_disk_free(const std::string& data_path) {
Jeff Sharkeyed909ae2017-03-22 21:27:40 -0600648 struct statvfs sfs;
649 if (statvfs(data_path.c_str(), &sfs) == 0) {
Jeff Sharkey4f7be172017-08-11 15:13:31 -0600650 return static_cast<int64_t>(sfs.f_bavail) * sfs.f_frsize;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700651 } else {
Jeff Sharkeyed909ae2017-03-22 21:27:40 -0600652 PLOG(ERROR) << "Couldn't statvfs " << data_path;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700653 return -1;
654 }
655}
656
Jeff Sharkey9a998f42016-07-14 18:16:22 -0600657int get_path_inode(const std::string& path, ino_t *inode) {
658 struct stat buf;
659 memset(&buf, 0, sizeof(buf));
660 if (stat(path.c_str(), &buf) != 0) {
661 PLOG(WARNING) << "Failed to stat " << path;
662 return -1;
663 } else {
664 *inode = buf.st_ino;
665 return 0;
666 }
667}
668
669/**
670 * Write the inode of a specific child file into the given xattr on the
671 * parent directory. This allows you to find the child later, even if its
672 * name is encrypted.
673 */
674int write_path_inode(const std::string& parent, const char* name, const char* inode_xattr) {
675 ino_t inode = 0;
676 uint64_t inode_raw = 0;
677 auto path = StringPrintf("%s/%s", parent.c_str(), name);
678
679 if (get_path_inode(path, &inode) != 0) {
680 // Path probably doesn't exist yet; ignore
681 return 0;
682 }
683
684 // Check to see if already set correctly
685 if (getxattr(parent.c_str(), inode_xattr, &inode_raw, sizeof(inode_raw)) == sizeof(inode_raw)) {
686 if (inode_raw == inode) {
687 // Already set correctly; skip writing
688 return 0;
689 } else {
690 PLOG(WARNING) << "Mismatched inode value; found " << inode
691 << " on disk but marked value was " << inode_raw << "; overwriting";
692 }
693 }
694
695 inode_raw = inode;
Jeff Sharkey4ed65072016-07-22 11:38:54 -0600696 if (setxattr(parent.c_str(), inode_xattr, &inode_raw, sizeof(inode_raw), 0) != 0 && errno != EOPNOTSUPP) {
Jeff Sharkey9a998f42016-07-14 18:16:22 -0600697 PLOG(ERROR) << "Failed to write xattr " << inode_xattr << " at " << parent;
698 return -1;
699 } else {
700 return 0;
701 }
702}
703
704/**
705 * Read the inode of a specific child file from the given xattr on the
706 * parent directory. Returns a currently valid path for that child, which
707 * might have an encrypted name.
708 */
709std::string read_path_inode(const std::string& parent, const char* name, const char* inode_xattr) {
710 ino_t inode = 0;
711 uint64_t inode_raw = 0;
712 auto fallback = StringPrintf("%s/%s", parent.c_str(), name);
713
714 // Lookup the inode value written earlier
715 if (getxattr(parent.c_str(), inode_xattr, &inode_raw, sizeof(inode_raw)) == sizeof(inode_raw)) {
716 inode = inode_raw;
717 }
718
719 // For testing purposes, rely on the inode when defined; this could be
720 // optimized to use access() in the future.
721 if (inode != 0) {
722 DIR* dir = opendir(parent.c_str());
723 if (dir == nullptr) {
724 PLOG(ERROR) << "Failed to opendir " << parent;
725 return fallback;
726 }
727
728 struct dirent* ent;
729 while ((ent = readdir(dir))) {
730 if (ent->d_ino == inode) {
731 auto resolved = StringPrintf("%s/%s", parent.c_str(), ent->d_name);
732#if DEBUG_XATTRS
733 if (resolved != fallback) {
734 LOG(DEBUG) << "Resolved path " << resolved << " for inode " << inode
735 << " instead of " << fallback;
736 }
737#endif
738 closedir(dir);
739 return resolved;
740 }
741 }
742 LOG(WARNING) << "Failed to resolve inode " << inode << "; using " << fallback;
743 closedir(dir);
744 return fallback;
745 } else {
746 return fallback;
747 }
748}
749
Ryuki Nakamurac7342f82017-09-30 11:57:00 +0900750void remove_path_xattr(const std::string& path, const char* inode_xattr) {
751 if (removexattr(path.c_str(), inode_xattr) && errno != ENODATA) {
752 PLOG(ERROR) << "Failed to remove xattr " << inode_xattr << " at " << path;
753 }
754}
755
Mike Lockwood94afecf2012-10-24 10:45:23 -0700756/**
Calin Juravlec597b6d2014-08-19 17:43:05 +0100757 * Validate that the path is valid in the context of the provided directory.
758 * The path is allowed to have at most one subdirectory and no indirections
759 * to top level directories (i.e. have "..").
760 */
Jeff Sharkeyc1149c92017-09-21 14:51:09 -0600761static int validate_path(const std::string& dir, const std::string& path, int maxSubdirs) {
762 // Argument sanity checking
763 if (dir.find('/') != 0 || dir.rfind('/') != dir.size() - 1
764 || dir.find("..") != std::string::npos) {
765 LOG(ERROR) << "Invalid directory " << dir;
766 return -1;
767 }
768 if (path.find("..") != std::string::npos) {
769 LOG(ERROR) << "Invalid path " << path;
770 return -1;
Calin Juravlec597b6d2014-08-19 17:43:05 +0100771 }
772
Jeff Sharkeyc1149c92017-09-21 14:51:09 -0600773 if (path.compare(0, dir.size(), dir) != 0) {
774 // Common case, path isn't under directory
775 return -1;
776 }
777
778 // Count number of subdirectories
779 auto pos = path.find('/', dir.size());
780 int count = 0;
781 while (pos != std::string::npos) {
Jeff Sharkey172fac02017-10-06 13:09:46 -0600782 auto next = path.find('/', pos + 1);
783 if (next > pos + 1) {
784 count++;
785 }
786 pos = next;
Jeff Sharkeyc1149c92017-09-21 14:51:09 -0600787 }
788
789 if (count > maxSubdirs) {
790 LOG(ERROR) << "Invalid path depth " << path << " when tested against " << dir;
Calin Juravlec597b6d2014-08-19 17:43:05 +0100791 return -1;
792 }
793
794 return 0;
795}
796
797/**
Mike Lockwood94afecf2012-10-24 10:45:23 -0700798 * Checks whether a path points to a system app (.apk file). Returns 0
799 * if it is a system app or -1 if it is not.
800 */
801int validate_system_app_path(const char* path) {
Jeff Sharkeyc1149c92017-09-21 14:51:09 -0600802 std::string path_ = path;
803 for (const auto& dir : android_system_dirs) {
804 if (validate_path(dir, path, 1) == 0) {
805 return 0;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700806 }
807 }
Mike Lockwood94afecf2012-10-24 10:45:23 -0700808 return -1;
809}
810
Calin Juravle114f0812017-03-08 19:05:07 -0800811bool validate_secondary_dex_path(const std::string& pkgname, const std::string& dex_path,
Calin Juravle7d765462017-09-04 15:57:10 -0700812 const char* volume_uuid, int uid, int storage_flag) {
Calin Juravle42451c02017-01-17 14:43:25 -0800813 CHECK(storage_flag == FLAG_STORAGE_CE || storage_flag == FLAG_STORAGE_DE);
814
Calin Juravle3760ad32017-07-27 16:31:55 -0700815 // Empty paths are not allowed.
816 if (dex_path.empty()) { return false; }
817 // First character should always be '/'. No relative paths.
818 if (dex_path[0] != '/') { return false; }
819 // The last character should not be '/'.
820 if (dex_path[dex_path.size() - 1] == '/') { return false; }
821 // There should be no '.' after the directory marker.
822 if (dex_path.find("/.") != std::string::npos) { return false; }
823 // The path should be at most PKG_PATH_MAX long.
824 if (dex_path.size() > PKG_PATH_MAX) { return false; }
825
Calin Juravle7d765462017-09-04 15:57:10 -0700826 // The dex_path should be under the app data directory.
827 std::string app_private_dir = storage_flag == FLAG_STORAGE_CE
Calin Juravledd42e272017-09-11 11:50:36 -0700828 ? create_data_user_ce_package_path(
829 volume_uuid, multiuser_get_user_id(uid), pkgname.c_str())
830 : create_data_user_de_package_path(
831 volume_uuid, multiuser_get_user_id(uid), pkgname.c_str());
Calin Juravle3760ad32017-07-27 16:31:55 -0700832
Calin Juravle7d765462017-09-04 15:57:10 -0700833 if (strncmp(dex_path.c_str(), app_private_dir.c_str(), app_private_dir.size()) != 0) {
834 // The check above might fail if the dex file is accessed via the /data/user/0 symlink.
835 // If that's the case, attempt to validate against the user data link.
836 std::string app_private_dir_symlink = create_data_user_ce_package_path_as_user_link(
837 volume_uuid, multiuser_get_user_id(uid), pkgname.c_str());
838 if (strncmp(dex_path.c_str(), app_private_dir_symlink.c_str(),
839 app_private_dir_symlink.size()) != 0) {
Calin Juravledd42e272017-09-11 11:50:36 -0700840 return false;
841 }
Calin Juravle42451c02017-01-17 14:43:25 -0800842 }
Calin Juravle3760ad32017-07-27 16:31:55 -0700843
844 // If we got here we have a valid path.
845 return true;
Calin Juravle42451c02017-01-17 14:43:25 -0800846}
847
Mike Lockwood94afecf2012-10-24 10:45:23 -0700848/**
Narayan Kamathd845c962015-06-04 13:20:27 +0100849 * Check whether path points to a valid path for an APK file. The path must
850 * begin with a whitelisted prefix path and must be no deeper than |maxSubdirs| within
851 * that path. Returns -1 when an invalid path is encountered and 0 when a valid path
852 * is encountered.
Mike Lockwood94afecf2012-10-24 10:45:23 -0700853 */
Narayan Kamathd845c962015-06-04 13:20:27 +0100854static int validate_apk_path_internal(const char *path, int maxSubdirs) {
Jeff Sharkeyc1149c92017-09-21 14:51:09 -0600855 std::string path_ = path;
856 if (validate_path(android_app_dir, path_, maxSubdirs) == 0) {
857 return 0;
858 } else if (validate_path(android_app_private_dir, path_, maxSubdirs) == 0) {
859 return 0;
860 } else if (validate_path(android_app_ephemeral_dir, path_, maxSubdirs) == 0) {
861 return 0;
862 } else if (validate_path(android_asec_dir, path_, maxSubdirs) == 0) {
863 return 0;
864 } else if (validate_path(android_mnt_expand_dir, path_, std::max(maxSubdirs, 2)) == 0) {
865 return 0;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700866 } else {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700867 return -1;
868 }
Mike Lockwood94afecf2012-10-24 10:45:23 -0700869}
870
Narayan Kamathd845c962015-06-04 13:20:27 +0100871int validate_apk_path(const char* path) {
872 return validate_apk_path_internal(path, 1 /* maxSubdirs */);
873}
874
875int validate_apk_path_subdirs(const char* path) {
876 return validate_apk_path_internal(path, 3 /* maxSubdirs */);
877}
878
Robin Lee095c7632014-04-25 15:05:19 +0100879int ensure_config_user_dirs(userid_t userid) {
Robin Lee095c7632014-04-25 15:05:19 +0100880 // writable by system, readable by any app within the same user
Robin Lee60fd3fe2014-10-07 16:55:02 +0100881 const int uid = multiuser_get_uid(userid, AID_SYSTEM);
882 const int gid = multiuser_get_uid(userid, AID_EVERYBODY);
Robin Lee095c7632014-04-25 15:05:19 +0100883
884 // Ensure /data/misc/user/<userid> exists
Jeff Sharkey379a12b2016-04-14 20:45:06 -0600885 auto path = create_data_misc_legacy_path(userid);
886 return fs_prepare_dir(path.c_str(), 0750, uid, gid);
Robin Lee095c7632014-04-25 15:05:19 +0100887}
Andreas Gampe02d0de52015-11-11 20:43:16 -0800888
889int wait_child(pid_t pid)
890{
891 int status;
892 pid_t got_pid;
893
894 while (1) {
895 got_pid = waitpid(pid, &status, 0);
896 if (got_pid == -1 && errno == EINTR) {
897 printf("waitpid interrupted, retrying\n");
898 } else {
899 break;
900 }
901 }
902 if (got_pid != pid) {
903 ALOGW("waitpid failed: wanted %d, got %d: %s\n",
904 (int) pid, (int) got_pid, strerror(errno));
905 return 1;
906 }
907
908 if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
909 return 0;
910 } else {
911 return status; /* always nonzero */
912 }
913}
914
Calin Juravle42451c02017-01-17 14:43:25 -0800915/**
916 * Prepare an app cache directory, which offers to fix-up the GID and
917 * directory mode flags during a platform upgrade.
918 * The app cache directory path will be 'parent'/'name'.
919 */
920int prepare_app_cache_dir(const std::string& parent, const char* name, mode_t target_mode,
921 uid_t uid, gid_t gid) {
922 auto path = StringPrintf("%s/%s", parent.c_str(), name);
923 struct stat st;
924 if (stat(path.c_str(), &st) != 0) {
925 if (errno == ENOENT) {
926 // This is fine, just create it
927 if (fs_prepare_dir_strict(path.c_str(), target_mode, uid, gid) != 0) {
928 PLOG(ERROR) << "Failed to prepare " << path;
929 return -1;
930 } else {
931 return 0;
932 }
933 } else {
934 PLOG(ERROR) << "Failed to stat " << path;
935 return -1;
936 }
937 }
938
939 mode_t actual_mode = st.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO | S_ISGID);
940 if (st.st_uid != uid) {
941 // Mismatched UID is real trouble; we can't recover
942 LOG(ERROR) << "Mismatched UID at " << path << ": found " << st.st_uid
943 << " but expected " << uid;
944 return -1;
945 } else if (st.st_gid == gid && actual_mode == target_mode) {
946 // Everything looks good!
947 return 0;
Jeff Sharkeye59c85c2017-04-02 21:53:14 -0600948 } else {
949 // Mismatched GID/mode is recoverable; fall through to update
950 LOG(DEBUG) << "Mismatched cache GID/mode at " << path << ": found " << st.st_gid
Shubham Ajmeraec0afbf2017-09-14 11:07:33 -0700951 << "/" << actual_mode << " but expected " << gid << "/" << target_mode;
Calin Juravle42451c02017-01-17 14:43:25 -0800952 }
953
954 // Directory is owned correctly, but GID or mode mismatch means it's
955 // probably a platform upgrade so we need to fix them
956 FTS *fts;
957 FTSENT *p;
958 char *argv[] = { (char*) path.c_str(), nullptr };
Jeff Sharkeyb26786d2017-03-11 19:40:29 -0700959 if (!(fts = fts_open(argv, FTS_PHYSICAL | FTS_NOCHDIR | FTS_XDEV, NULL))) {
Calin Juravle42451c02017-01-17 14:43:25 -0800960 PLOG(ERROR) << "Failed to fts_open " << path;
961 return -1;
962 }
963 while ((p = fts_read(fts)) != NULL) {
964 switch (p->fts_info) {
965 case FTS_DP:
Jeff Sharkeye12d5962017-04-03 16:41:02 -0600966 if (chmod(p->fts_path, target_mode) != 0) {
Calin Juravle42451c02017-01-17 14:43:25 -0800967 PLOG(WARNING) << "Failed to chmod " << p->fts_path;
968 }
969 // Intentional fall through to also set GID
970 case FTS_F:
Jeff Sharkeye12d5962017-04-03 16:41:02 -0600971 if (chown(p->fts_path, -1, gid) != 0) {
Calin Juravle42451c02017-01-17 14:43:25 -0800972 PLOG(WARNING) << "Failed to chown " << p->fts_path;
973 }
974 break;
975 case FTS_SL:
976 case FTS_SLNONE:
Jeff Sharkeye12d5962017-04-03 16:41:02 -0600977 if (lchown(p->fts_path, -1, gid) != 0) {
Calin Juravle42451c02017-01-17 14:43:25 -0800978 PLOG(WARNING) << "Failed to chown " << p->fts_path;
979 }
980 break;
981 }
982 }
983 fts_close(fts);
984 return 0;
985}
986
Andreas Gampe02d0de52015-11-11 20:43:16 -0800987} // namespace installd
988} // namespace android