utils.cpp revision ed909ae8db2f44ce7fe7003c6fee457f13669702
194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood/* 294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood** Copyright 2008, The Android Open Source Project 394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood** 4d93707342a61e66bc3eb2145628158452f577f42Dave Allison** Licensed under the Apache License, Version 2.0 (the "License"); 5d93707342a61e66bc3eb2145628158452f577f42Dave Allison** you may not use this file except in compliance with the License. 6d93707342a61e66bc3eb2145628158452f577f42Dave Allison** You may obtain a copy of the License at 794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood** 8d93707342a61e66bc3eb2145628158452f577f42Dave Allison** http://www.apache.org/licenses/LICENSE-2.0 994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood** 10d93707342a61e66bc3eb2145628158452f577f42Dave Allison** Unless required by applicable law or agreed to in writing, software 11d93707342a61e66bc3eb2145628158452f577f42Dave Allison** distributed under the License is distributed on an "AS IS" BASIS, 12d93707342a61e66bc3eb2145628158452f577f42Dave Allison** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13d93707342a61e66bc3eb2145628158452f577f42Dave Allison** See the License for the specific language governing permissions and 1494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood** limitations under the License. 1594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood*/ 1694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 1702d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe#include "utils.h" 1802d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe 1902d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe#include <errno.h> 2002d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe#include <fcntl.h> 213dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey#include <fts.h> 2202d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe#include <stdlib.h> 2302d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe#include <sys/stat.h> 2402d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe#include <sys/wait.h> 259a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey#include <sys/xattr.h> 26ed909ae8db2f44ce7fe7003c6fee457f13669702Jeff Sharkey#include <sys/statvfs.h> 2794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 28e4ec9eb7b4c452493589983970ba5ccc501728d1Elliott Hughes#include <android-base/logging.h> 2902d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe#include <android-base/stringprintf.h> 3002d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe#include <cutils/fs.h> 31871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey#include <cutils/properties.h> 327823e124e00576e20e47ec717cbe8bc89f0f2bf2Mark Salyzyn#include <log/log.h> 3302d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe#include <private/android_filesystem_config.h> 3402d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe 3502d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe#include "globals.h" // extern variables. 3694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 3702d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe#ifndef LOG_TAG 3802d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe#define LOG_TAG "installd" 3902d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe#endif 409a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey 41c03de09173f98506e73e7cf7df21fe11795d4b24Jeff Sharkey#define CACHE_NOISY(x) //x 429a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey#define DEBUG_XATTRS 0 4394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 44c03de09173f98506e73e7cf7df21fe11795d4b24Jeff Sharkeyusing android::base::StringPrintf; 4594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 4602d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampenamespace android { 4702d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampenamespace installd { 4802d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe 4994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood/** 50c03de09173f98506e73e7cf7df21fe11795d4b24Jeff Sharkey * Check that given string is valid filename, and that it attempts no 51c03de09173f98506e73e7cf7df21fe11795d4b24Jeff Sharkey * parent or child directory traversal. 5294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood */ 53423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkeybool is_valid_filename(const std::string& name) { 54c03de09173f98506e73e7cf7df21fe11795d4b24Jeff Sharkey if (name.empty() || (name == ".") || (name == "..") 55c03de09173f98506e73e7cf7df21fe11795d4b24Jeff Sharkey || (name.find('/') != std::string::npos)) { 56c03de09173f98506e73e7cf7df21fe11795d4b24Jeff Sharkey return false; 5794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } else { 58c03de09173f98506e73e7cf7df21fe11795d4b24Jeff Sharkey return true; 5994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 60c03de09173f98506e73e7cf7df21fe11795d4b24Jeff Sharkey} 6194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 626a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravlestatic void check_package_name(const char* package_name) { 636a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle CHECK(is_valid_filename(package_name)); 64423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey CHECK(is_valid_package_name(package_name)); 656a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle} 666a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle 67c03de09173f98506e73e7cf7df21fe11795d4b24Jeff Sharkey/** 68d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey * Create the path name where package app contents should be stored for 69d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey * the given volume UUID and package name. An empty UUID is assumed to 70d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey * be internal storage. 71d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey */ 72d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkeystd::string create_data_app_package_path(const char* volume_uuid, 73d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey const char* package_name) { 746a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle check_package_name(package_name); 75d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey return StringPrintf("%s/%s", 76d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey create_data_app_path(volume_uuid).c_str(), package_name); 77d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey} 78d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey 79d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey/** 80c03de09173f98506e73e7cf7df21fe11795d4b24Jeff Sharkey * Create the path name where package data should be stored for the given 81c03de09173f98506e73e7cf7df21fe11795d4b24Jeff Sharkey * volume UUID, package name, and user ID. An empty UUID is assumed to be 82c03de09173f98506e73e7cf7df21fe11795d4b24Jeff Sharkey * internal storage. 83c03de09173f98506e73e7cf7df21fe11795d4b24Jeff Sharkey */ 842f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkeystd::string create_data_user_ce_package_path(const char* volume_uuid, 85d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey userid_t user, const char* package_name) { 866a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle check_package_name(package_name); 87d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey return StringPrintf("%s/%s", 882f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey create_data_user_ce_path(volume_uuid, user).c_str(), package_name); 892f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey} 902f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey 912f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkeystd::string create_data_user_ce_package_path(const char* volume_uuid, userid_t user, 922f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey const char* package_name, ino_t ce_data_inode) { 932f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey // For testing purposes, rely on the inode when defined; this could be 942f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey // optimized to use access() in the future. 952f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey auto fallback = create_data_user_ce_package_path(volume_uuid, user, package_name); 962f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey if (ce_data_inode != 0) { 972f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey auto user_path = create_data_user_ce_path(volume_uuid, user); 982f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey DIR* dir = opendir(user_path.c_str()); 992f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey if (dir == nullptr) { 1002f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey PLOG(ERROR) << "Failed to opendir " << user_path; 1012f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey return fallback; 1022f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey } 1032f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey 1042f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey struct dirent* ent; 1052f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey while ((ent = readdir(dir))) { 1062f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey if (ent->d_ino == ce_data_inode) { 1071d992f9f7886f98e46f98430d6c1d061cc31fdb3Jeff Sharkey auto resolved = StringPrintf("%s/%s", user_path.c_str(), ent->d_name); 1089a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey#if DEBUG_XATTRS 1091d992f9f7886f98e46f98430d6c1d061cc31fdb3Jeff Sharkey if (resolved != fallback) { 1101d992f9f7886f98e46f98430d6c1d061cc31fdb3Jeff Sharkey LOG(DEBUG) << "Resolved path " << resolved << " for inode " << ce_data_inode 1111d992f9f7886f98e46f98430d6c1d061cc31fdb3Jeff Sharkey << " instead of " << fallback; 1121d992f9f7886f98e46f98430d6c1d061cc31fdb3Jeff Sharkey } 1139a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey#endif 1142f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey closedir(dir); 1151d992f9f7886f98e46f98430d6c1d061cc31fdb3Jeff Sharkey return resolved; 1162f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey } 1172f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey } 1181d992f9f7886f98e46f98430d6c1d061cc31fdb3Jeff Sharkey LOG(WARNING) << "Failed to resolve inode " << ce_data_inode << "; using " << fallback; 1192f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey closedir(dir); 1202f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey return fallback; 1212f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey } else { 1222f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey return fallback; 1232f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey } 124c03de09173f98506e73e7cf7df21fe11795d4b24Jeff Sharkey} 12594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 12663ec2d64196144b2d15d2baffedccfa011d6494fJeff Sharkeystd::string create_data_user_de_package_path(const char* volume_uuid, 12763ec2d64196144b2d15d2baffedccfa011d6494fJeff Sharkey userid_t user, const char* package_name) { 1286a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle check_package_name(package_name); 12963ec2d64196144b2d15d2baffedccfa011d6494fJeff Sharkey return StringPrintf("%s/%s", 13063ec2d64196144b2d15d2baffedccfa011d6494fJeff Sharkey create_data_user_de_path(volume_uuid, user).c_str(), package_name); 13163ec2d64196144b2d15d2baffedccfa011d6494fJeff Sharkey} 13263ec2d64196144b2d15d2baffedccfa011d6494fJeff Sharkey 133c03de09173f98506e73e7cf7df21fe11795d4b24Jeff Sharkeyint create_pkg_path(char path[PKG_PATH_MAX], const char *pkgname, 134c03de09173f98506e73e7cf7df21fe11795d4b24Jeff Sharkey const char *postfix, userid_t userid) { 135423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey if (!is_valid_package_name(pkgname)) { 136c03de09173f98506e73e7cf7df21fe11795d4b24Jeff Sharkey path[0] = '\0'; 13794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood return -1; 13894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 13994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 1402f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey std::string _tmp(create_data_user_ce_package_path(nullptr, userid, pkgname) + postfix); 141c03de09173f98506e73e7cf7df21fe11795d4b24Jeff Sharkey const char* tmp = _tmp.c_str(); 142c03de09173f98506e73e7cf7df21fe11795d4b24Jeff Sharkey if (strlen(tmp) >= PKG_PATH_MAX) { 143c03de09173f98506e73e7cf7df21fe11795d4b24Jeff Sharkey path[0] = '\0'; 144c03de09173f98506e73e7cf7df21fe11795d4b24Jeff Sharkey return -1; 145c03de09173f98506e73e7cf7df21fe11795d4b24Jeff Sharkey } else { 146c03de09173f98506e73e7cf7df21fe11795d4b24Jeff Sharkey strcpy(path, tmp); 147c03de09173f98506e73e7cf7df21fe11795d4b24Jeff Sharkey return 0; 14894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 14994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood} 15094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 15141ea424413c0381ef2aa15fc5bd5d4b88abd23c4Jeff Sharkeystd::string create_data_path(const char* volume_uuid) { 15241ea424413c0381ef2aa15fc5bd5d4b88abd23c4Jeff Sharkey if (volume_uuid == nullptr) { 15341ea424413c0381ef2aa15fc5bd5d4b88abd23c4Jeff Sharkey return "/data"; 154871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey } else if (!strcmp(volume_uuid, "TEST")) { 155871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey CHECK(property_get_bool("ro.debuggable", false)); 156871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey return "/data/local/tmp"; 15794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } else { 15841ea424413c0381ef2aa15fc5bd5d4b88abd23c4Jeff Sharkey CHECK(is_valid_filename(volume_uuid)); 15941ea424413c0381ef2aa15fc5bd5d4b88abd23c4Jeff Sharkey return StringPrintf("/mnt/expand/%s", volume_uuid); 16094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 16141ea424413c0381ef2aa15fc5bd5d4b88abd23c4Jeff Sharkey} 16294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 16341ea424413c0381ef2aa15fc5bd5d4b88abd23c4Jeff Sharkey/** 164d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey * Create the path name for app data. 165d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey */ 166d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkeystd::string create_data_app_path(const char* volume_uuid) { 167d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey return StringPrintf("%s/app", create_data_path(volume_uuid).c_str()); 168d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey} 169d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey 170d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey/** 17141ea424413c0381ef2aa15fc5bd5d4b88abd23c4Jeff Sharkey * Create the path name for user data for a certain userid. 17241ea424413c0381ef2aa15fc5bd5d4b88abd23c4Jeff Sharkey */ 1732f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkeystd::string create_data_user_ce_path(const char* volume_uuid, userid_t userid) { 17441ea424413c0381ef2aa15fc5bd5d4b88abd23c4Jeff Sharkey std::string data(create_data_path(volume_uuid)); 17541ea424413c0381ef2aa15fc5bd5d4b88abd23c4Jeff Sharkey if (volume_uuid == nullptr) { 17641ea424413c0381ef2aa15fc5bd5d4b88abd23c4Jeff Sharkey if (userid == 0) { 17741ea424413c0381ef2aa15fc5bd5d4b88abd23c4Jeff Sharkey return StringPrintf("%s/data", data.c_str()); 17841ea424413c0381ef2aa15fc5bd5d4b88abd23c4Jeff Sharkey } else { 17941ea424413c0381ef2aa15fc5bd5d4b88abd23c4Jeff Sharkey return StringPrintf("%s/user/%u", data.c_str(), userid); 18094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 18141ea424413c0381ef2aa15fc5bd5d4b88abd23c4Jeff Sharkey } else { 18241ea424413c0381ef2aa15fc5bd5d4b88abd23c4Jeff Sharkey return StringPrintf("%s/user/%u", data.c_str(), userid); 18394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 18494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood} 18594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 18694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood/** 18763ec2d64196144b2d15d2baffedccfa011d6494fJeff Sharkey * Create the path name for device encrypted user data for a certain userid. 18863ec2d64196144b2d15d2baffedccfa011d6494fJeff Sharkey */ 18963ec2d64196144b2d15d2baffedccfa011d6494fJeff Sharkeystd::string create_data_user_de_path(const char* volume_uuid, userid_t userid) { 19063ec2d64196144b2d15d2baffedccfa011d6494fJeff Sharkey std::string data(create_data_path(volume_uuid)); 19163ec2d64196144b2d15d2baffedccfa011d6494fJeff Sharkey return StringPrintf("%s/user_de/%u", data.c_str(), userid); 19263ec2d64196144b2d15d2baffedccfa011d6494fJeff Sharkey} 19363ec2d64196144b2d15d2baffedccfa011d6494fJeff Sharkey 19463ec2d64196144b2d15d2baffedccfa011d6494fJeff Sharkey/** 195abe4fe5b46157ecd2a52d28abf938c816c3ce878Jeff Sharkey * Create the path name for media for a certain userid. 19694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood */ 19741ea424413c0381ef2aa15fc5bd5d4b88abd23c4Jeff Sharkeystd::string create_data_media_path(const char* volume_uuid, userid_t userid) { 19841ea424413c0381ef2aa15fc5bd5d4b88abd23c4Jeff Sharkey return StringPrintf("%s/media/%u", create_data_path(volume_uuid).c_str(), userid); 19994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood} 20094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 201df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkeystd::string create_data_media_obb_path(const char* volume_uuid, const char* package_name) { 202df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey return StringPrintf("%s/media/obb/%s", create_data_path(volume_uuid).c_str(), package_name); 203df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey} 204df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey 2053dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkeystd::string create_data_media_package_path(const char* volume_uuid, userid_t userid, 2063dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey const char* data_type, const char* package_name) { 2073dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey return StringPrintf("%s/Android/%s/%s", create_data_media_path(volume_uuid, userid).c_str(), 2083dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey data_type, package_name); 2093dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey} 2103dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey 211379a12b0072b322c7f86e690a8e8a220e500861cJeff Sharkeystd::string create_data_misc_legacy_path(userid_t userid) { 212379a12b0072b322c7f86e690a8e8a220e500861cJeff Sharkey return StringPrintf("%s/misc/user/%u", create_data_path(nullptr).c_str(), userid); 213379a12b0072b322c7f86e690a8e8a220e500861cJeff Sharkey} 214379a12b0072b322c7f86e690a8e8a220e500861cJeff Sharkey 215114f08107be384a3f9cc954bdec2b6b7619354aeCalin Juravlestd::string create_primary_cur_profile_dir_path(userid_t userid) { 2166a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle return StringPrintf("%s/cur/%u", android_profiles_dir.path, userid); 2176a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle} 2186a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle 219114f08107be384a3f9cc954bdec2b6b7619354aeCalin Juravlestd::string create_primary_current_profile_package_dir_path(userid_t user, 220114f08107be384a3f9cc954bdec2b6b7619354aeCalin Juravle const std::string& package_name) { 22176268c56febde9a77183387fbd4baabe6694e6b5Calin Juravle check_package_name(package_name.c_str()); 222114f08107be384a3f9cc954bdec2b6b7619354aeCalin Juravle return StringPrintf("%s/%s", 223114f08107be384a3f9cc954bdec2b6b7619354aeCalin Juravle create_primary_cur_profile_dir_path(user).c_str(), package_name.c_str()); 224df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey} 225df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey 226114f08107be384a3f9cc954bdec2b6b7619354aeCalin Juravlestd::string create_primary_ref_profile_dir_path() { 227df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey return StringPrintf("%s/ref", android_profiles_dir.path); 2286a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle} 2296a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle 230114f08107be384a3f9cc954bdec2b6b7619354aeCalin Juravlestd::string create_primary_reference_profile_package_dir_path(const std::string& package_name) { 23176268c56febde9a77183387fbd4baabe6694e6b5Calin Juravle check_package_name(package_name.c_str()); 23276268c56febde9a77183387fbd4baabe6694e6b5Calin Juravle return StringPrintf("%s/ref/%s", android_profiles_dir.path, package_name.c_str()); 2336a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle} 2346a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle 2353dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkeystd::string create_data_dalvik_cache_path() { 2363dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey return "/data/dalvik-cache"; 2373dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey} 2383dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey 239114f08107be384a3f9cc954bdec2b6b7619354aeCalin Juravle// Keep profile paths in sync with ActivityThread and LoadedApk. 240114f08107be384a3f9cc954bdec2b6b7619354aeCalin Juravleconst std::string PROFILE_EXT = ".prof"; 241114f08107be384a3f9cc954bdec2b6b7619354aeCalin Juravleconst std::string PRIMARY_PROFILE_NAME = "primary" + PROFILE_EXT; 24290aff26f0135379db19432ae90c40c0831ba5954Jeff Sharkey 243114f08107be384a3f9cc954bdec2b6b7619354aeCalin Juravlestd::string create_current_profile_path(userid_t user, const std::string& location, 244114f08107be384a3f9cc954bdec2b6b7619354aeCalin Juravle bool is_secondary_dex) { 245114f08107be384a3f9cc954bdec2b6b7619354aeCalin Juravle if (is_secondary_dex) { 246114f08107be384a3f9cc954bdec2b6b7619354aeCalin Juravle // Secondary dex profiles are stored next to the dex files using .prof extension. 247114f08107be384a3f9cc954bdec2b6b7619354aeCalin Juravle return StringPrintf("%s%s", location.c_str(), PROFILE_EXT.c_str()); 248114f08107be384a3f9cc954bdec2b6b7619354aeCalin Juravle } else { 249114f08107be384a3f9cc954bdec2b6b7619354aeCalin Juravle // Profiles for primary apks are under /data/misc/profiles/cur. 250114f08107be384a3f9cc954bdec2b6b7619354aeCalin Juravle std::string profile_dir = create_primary_current_profile_package_dir_path(user, location); 251114f08107be384a3f9cc954bdec2b6b7619354aeCalin Juravle return StringPrintf("%s/%s", profile_dir.c_str(), PRIMARY_PROFILE_NAME.c_str()); 252114f08107be384a3f9cc954bdec2b6b7619354aeCalin Juravle } 253114f08107be384a3f9cc954bdec2b6b7619354aeCalin Juravle} 254114f08107be384a3f9cc954bdec2b6b7619354aeCalin Juravle 255114f08107be384a3f9cc954bdec2b6b7619354aeCalin Juravlestd::string create_reference_profile_path(const std::string& location, bool is_secondary_dex) { 256114f08107be384a3f9cc954bdec2b6b7619354aeCalin Juravle if (is_secondary_dex) { 257114f08107be384a3f9cc954bdec2b6b7619354aeCalin Juravle // Secondary dex reference profiles are stored next to the dex files under the oat folder. 258114f08107be384a3f9cc954bdec2b6b7619354aeCalin Juravle size_t dirIndex = location.rfind('/'); 259114f08107be384a3f9cc954bdec2b6b7619354aeCalin Juravle CHECK(dirIndex != std::string::npos) 260114f08107be384a3f9cc954bdec2b6b7619354aeCalin Juravle << "Unexpected dir structure for secondary dex " << location; 261114f08107be384a3f9cc954bdec2b6b7619354aeCalin Juravle 262114f08107be384a3f9cc954bdec2b6b7619354aeCalin Juravle std::string dex_dir = location.substr(0, dirIndex); 263114f08107be384a3f9cc954bdec2b6b7619354aeCalin Juravle std::string dex_name = location.substr(dirIndex +1); 264114f08107be384a3f9cc954bdec2b6b7619354aeCalin Juravle return StringPrintf("%s/oat/%s%s", 265114f08107be384a3f9cc954bdec2b6b7619354aeCalin Juravle dex_dir.c_str(), dex_name.c_str(), PROFILE_EXT.c_str()); 266114f08107be384a3f9cc954bdec2b6b7619354aeCalin Juravle } else { 267114f08107be384a3f9cc954bdec2b6b7619354aeCalin Juravle // Reference profiles for primary apks are stored in /data/misc/profile/ref. 268114f08107be384a3f9cc954bdec2b6b7619354aeCalin Juravle std::string profile_dir = create_primary_reference_profile_package_dir_path(location); 269114f08107be384a3f9cc954bdec2b6b7619354aeCalin Juravle return StringPrintf("%s/%s", profile_dir.c_str(), PRIMARY_PROFILE_NAME.c_str()); 270114f08107be384a3f9cc954bdec2b6b7619354aeCalin Juravle } 27190aff26f0135379db19432ae90c40c0831ba5954Jeff Sharkey} 27290aff26f0135379db19432ae90c40c0831ba5954Jeff Sharkey 273e36372423000a906bafae68844ebc6c42d09335aJeff Sharkeystd::vector<userid_t> get_known_users(const char* volume_uuid) { 274e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey std::vector<userid_t> users; 275e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey 276e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey // We always have an owner 277e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey users.push_back(0); 278e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey 279e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey std::string path(create_data_path(volume_uuid) + "/" + SECONDARY_USER_PREFIX); 280e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey DIR* dir = opendir(path.c_str()); 281e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey if (dir == NULL) { 282e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey // Unable to discover other users, but at least return owner 283e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey PLOG(ERROR) << "Failed to opendir " << path; 284e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey return users; 285e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey } 286e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey 287e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey struct dirent* ent; 288e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey while ((ent = readdir(dir))) { 289e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey if (ent->d_type != DT_DIR) { 290e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey continue; 291e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey } 292e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey 293e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey char* end; 294e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey userid_t user = strtol(ent->d_name, &end, 10); 295e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey if (*end == '\0' && user != 0) { 296e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey LOG(DEBUG) << "Found valid user " << user; 297e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey users.push_back(user); 298e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey } 299e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey } 300e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey closedir(dir); 301e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey 302e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey return users; 303e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey} 304e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey 3053dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkeyint calculate_tree_size(const std::string& path, int64_t* size, 306df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey int32_t include_gid, int32_t exclude_gid, bool exclude_apps) { 3073dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey FTS *fts; 3083dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey FTSENT *p; 309df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey int64_t matchedSize = 0; 3103dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey char *argv[] = { (char*) path.c_str(), nullptr }; 311b26786d647b624498c11405075e5223d1853f30aJeff Sharkey if (!(fts = fts_open(argv, FTS_PHYSICAL | FTS_NOCHDIR | FTS_XDEV, NULL))) { 3123dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey if (errno != ENOENT) { 3133dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey PLOG(ERROR) << "Failed to fts_open " << path; 3143dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey } 3153dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey return -1; 3163dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey } 3173dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey while ((p = fts_read(fts)) != NULL) { 3183dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey switch (p->fts_info) { 3193dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey case FTS_D: 3203dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey case FTS_DEFAULT: 3213dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey case FTS_F: 3223dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey case FTS_SL: 3233dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey case FTS_SLNONE: 324df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey int32_t uid = p->fts_statp->st_uid; 325df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey int32_t gid = p->fts_statp->st_gid; 326df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey int32_t user_uid = multiuser_get_app_id(uid); 327df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey int32_t user_gid = multiuser_get_app_id(gid); 328df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey if (exclude_apps && ((user_uid >= AID_APP_START && user_uid <= AID_APP_END) 329df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey || (user_gid >= AID_CACHE_GID_START && user_gid <= AID_CACHE_GID_END) 330df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey || (user_gid >= AID_SHARED_GID_START && user_gid <= AID_SHARED_GID_END))) { 331df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey // Don't traverse inside or measure 332df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey fts_set(fts, p, FTS_SKIP); 3333dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey break; 3343dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey } 335df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey if (include_gid != -1 && gid != include_gid) { 3363dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey break; 3373dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey } 338df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey if (exclude_gid != -1 && gid == exclude_gid) { 339df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey break; 340df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey } 341df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey matchedSize += (p->fts_statp->st_blocks * 512); 3423dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey break; 3433dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey } 3443dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey } 3453dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey fts_close(fts); 346df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey#if MEASURE_DEBUG 347df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey if ((include_gid == -1) && (exclude_gid == -1)) { 348df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey LOG(DEBUG) << "Measured " << path << " size " << matchedSize; 349df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey } else { 350df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey LOG(DEBUG) << "Measured " << path << " size " << matchedSize << "; include " << include_gid 351df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey << " exclude " << exclude_gid; 352df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey } 353df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey#endif 354df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey *size += matchedSize; 3553dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey return 0; 3563dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey} 3573dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey 35894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwoodint create_move_path(char path[PKG_PATH_MAX], 35994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood const char* pkgname, 36094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood const char* leaf, 36102d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe userid_t userid ATTRIBUTE_UNUSED) 36294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood{ 36394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if ((android_data_dir.len + strlen(PRIMARY_USER_PREFIX) + strlen(pkgname) + strlen(leaf) + 1) 36494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood >= PKG_PATH_MAX) { 36594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood return -1; 36694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 36794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 36894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood sprintf(path, "%s%s%s/%s", android_data_dir.path, PRIMARY_USER_PREFIX, pkgname, leaf); 36994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood return 0; 37094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood} 37194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 37294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood/** 37394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood * Checks whether the package name is valid. Returns -1 on error and 37494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood * 0 on success. 37594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood */ 376423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkeybool is_valid_package_name(const std::string& packageName) { 377367ace2f77105f7a09b34a50bc875fd4fa591177Jeff Sharkey // This logic is borrowed from PackageParser.java 378367ace2f77105f7a09b34a50bc875fd4fa591177Jeff Sharkey bool hasSep = false; 379367ace2f77105f7a09b34a50bc875fd4fa591177Jeff Sharkey bool front = true; 380367ace2f77105f7a09b34a50bc875fd4fa591177Jeff Sharkey 381367ace2f77105f7a09b34a50bc875fd4fa591177Jeff Sharkey auto it = packageName.begin(); 382367ace2f77105f7a09b34a50bc875fd4fa591177Jeff Sharkey for (; it != packageName.end() && *it != '-'; it++) { 383367ace2f77105f7a09b34a50bc875fd4fa591177Jeff Sharkey char c = *it; 384367ace2f77105f7a09b34a50bc875fd4fa591177Jeff Sharkey if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) { 385367ace2f77105f7a09b34a50bc875fd4fa591177Jeff Sharkey front = false; 386367ace2f77105f7a09b34a50bc875fd4fa591177Jeff Sharkey continue; 387367ace2f77105f7a09b34a50bc875fd4fa591177Jeff Sharkey } 388367ace2f77105f7a09b34a50bc875fd4fa591177Jeff Sharkey if (!front) { 389367ace2f77105f7a09b34a50bc875fd4fa591177Jeff Sharkey if ((c >= '0' && c <= '9') || c == '_') { 390367ace2f77105f7a09b34a50bc875fd4fa591177Jeff Sharkey continue; 39194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 39294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 393367ace2f77105f7a09b34a50bc875fd4fa591177Jeff Sharkey if (c == '.') { 394367ace2f77105f7a09b34a50bc875fd4fa591177Jeff Sharkey hasSep = true; 395367ace2f77105f7a09b34a50bc875fd4fa591177Jeff Sharkey front = true; 396367ace2f77105f7a09b34a50bc875fd4fa591177Jeff Sharkey continue; 397367ace2f77105f7a09b34a50bc875fd4fa591177Jeff Sharkey } 398367ace2f77105f7a09b34a50bc875fd4fa591177Jeff Sharkey LOG(WARNING) << "Bad package character " << c << " in " << packageName; 399367ace2f77105f7a09b34a50bc875fd4fa591177Jeff Sharkey return false; 400367ace2f77105f7a09b34a50bc875fd4fa591177Jeff Sharkey } 40194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 402ab7ac8d5a04bd3f38b85ce20ae5bb382f2a26585Jeff Sharkey if (front) { 403367ace2f77105f7a09b34a50bc875fd4fa591177Jeff Sharkey LOG(WARNING) << "Missing separator in " << packageName; 404367ace2f77105f7a09b34a50bc875fd4fa591177Jeff Sharkey return false; 40594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 40694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 407367ace2f77105f7a09b34a50bc875fd4fa591177Jeff Sharkey for (; it != packageName.end(); it++) { 408367ace2f77105f7a09b34a50bc875fd4fa591177Jeff Sharkey char c = *it; 409367ace2f77105f7a09b34a50bc875fd4fa591177Jeff Sharkey if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) continue; 410367ace2f77105f7a09b34a50bc875fd4fa591177Jeff Sharkey if ((c >= '0' && c <= '9') || c == '_' || c == '-' || c == '=') continue; 411367ace2f77105f7a09b34a50bc875fd4fa591177Jeff Sharkey LOG(WARNING) << "Bad suffix character " << c << " in " << packageName; 412367ace2f77105f7a09b34a50bc875fd4fa591177Jeff Sharkey return false; 41394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 41494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 415423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return true; 41694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood} 41794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 4183aee2c5c749dc2589f001b26fae1ec958ec89524Narayan Kamathstatic int _delete_dir_contents(DIR *d, 4193aee2c5c749dc2589f001b26fae1ec958ec89524Narayan Kamath int (*exclusion_predicate)(const char *name, const int is_dir)) 42094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood{ 42194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood int result = 0; 42294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood struct dirent *de; 42394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood int dfd; 42494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 42594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood dfd = dirfd(d); 42694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 42794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (dfd < 0) return -1; 42894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 42994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood while ((de = readdir(d))) { 43094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood const char *name = de->d_name; 43194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 4323aee2c5c749dc2589f001b26fae1ec958ec89524Narayan Kamath /* check using the exclusion predicate, if provided */ 4333aee2c5c749dc2589f001b26fae1ec958ec89524Narayan Kamath if (exclusion_predicate && exclusion_predicate(name, (de->d_type == DT_DIR))) { 4343aee2c5c749dc2589f001b26fae1ec958ec89524Narayan Kamath continue; 4353aee2c5c749dc2589f001b26fae1ec958ec89524Narayan Kamath } 43694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 43794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (de->d_type == DT_DIR) { 43899d9fb15b4a1eb534261ae81b2cf25aec4447bd0Chih-Hung Hsieh int subfd; 43994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood DIR *subdir; 44094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 44194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood /* always skip "." and ".." */ 44294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (name[0] == '.') { 44394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (name[1] == 0) continue; 44494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if ((name[1] == '.') && (name[2] == 0)) continue; 44594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 44694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 4478b7acacc93930df9fa9e1ebea4a4394195b2332eNick Kralevich subfd = openat(dfd, name, O_RDONLY | O_DIRECTORY | O_NOFOLLOW | O_CLOEXEC); 44894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (subfd < 0) { 44994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood ALOGE("Couldn't openat %s: %s\n", name, strerror(errno)); 45094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood result = -1; 45194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood continue; 45294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 45394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood subdir = fdopendir(subfd); 45494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (subdir == NULL) { 45594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood ALOGE("Couldn't fdopendir %s: %s\n", name, strerror(errno)); 45694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood close(subfd); 45794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood result = -1; 45894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood continue; 45994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 4603aee2c5c749dc2589f001b26fae1ec958ec89524Narayan Kamath if (_delete_dir_contents(subdir, exclusion_predicate)) { 46194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood result = -1; 46294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 46394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood closedir(subdir); 46494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (unlinkat(dfd, name, AT_REMOVEDIR) < 0) { 46594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood ALOGE("Couldn't unlinkat %s: %s\n", name, strerror(errno)); 46694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood result = -1; 46794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 46894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } else { 46994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (unlinkat(dfd, name, 0) < 0) { 47094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood ALOGE("Couldn't unlinkat %s: %s\n", name, strerror(errno)); 47194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood result = -1; 47294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 47394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 47494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 47594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 47694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood return result; 47794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood} 47894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 479b06f98aabc5381fd6366526d9b31b5d0345481b6Calin Juravleint delete_dir_contents(const std::string& pathname, bool ignore_if_missing) { 480b06f98aabc5381fd6366526d9b31b5d0345481b6Calin Juravle return delete_dir_contents(pathname.c_str(), 0, NULL, ignore_if_missing); 481ebf728fd43ab5c7d11a1f9e5fdc775d6740fae0aJeff Sharkey} 482ebf728fd43ab5c7d11a1f9e5fdc775d6740fae0aJeff Sharkey 483b06f98aabc5381fd6366526d9b31b5d0345481b6Calin Juravleint delete_dir_contents_and_dir(const std::string& pathname, bool ignore_if_missing) { 484b06f98aabc5381fd6366526d9b31b5d0345481b6Calin Juravle return delete_dir_contents(pathname.c_str(), 1, NULL, ignore_if_missing); 485ebf728fd43ab5c7d11a1f9e5fdc775d6740fae0aJeff Sharkey} 486ebf728fd43ab5c7d11a1f9e5fdc775d6740fae0aJeff Sharkey 48794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwoodint delete_dir_contents(const char *pathname, 48894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood int also_delete_dir, 489b06f98aabc5381fd6366526d9b31b5d0345481b6Calin Juravle int (*exclusion_predicate)(const char*, const int), 490b06f98aabc5381fd6366526d9b31b5d0345481b6Calin Juravle bool ignore_if_missing) 49194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood{ 49294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood int res = 0; 49394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood DIR *d; 49494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 49594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood d = opendir(pathname); 49694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (d == NULL) { 497b06f98aabc5381fd6366526d9b31b5d0345481b6Calin Juravle if (ignore_if_missing && (errno == ENOENT)) { 498b06f98aabc5381fd6366526d9b31b5d0345481b6Calin Juravle return 0; 499b06f98aabc5381fd6366526d9b31b5d0345481b6Calin Juravle } 50094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood ALOGE("Couldn't opendir %s: %s\n", pathname, strerror(errno)); 50194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood return -errno; 50294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 5033aee2c5c749dc2589f001b26fae1ec958ec89524Narayan Kamath res = _delete_dir_contents(d, exclusion_predicate); 50494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood closedir(d); 50594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (also_delete_dir) { 50694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (rmdir(pathname)) { 50794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood ALOGE("Couldn't rmdir %s: %s\n", pathname, strerror(errno)); 50894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood res = -1; 50994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 51094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 51194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood return res; 51294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood} 51394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 51494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwoodint delete_dir_contents_fd(int dfd, const char *name) 51594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood{ 51694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood int fd, res; 51794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood DIR *d; 51894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 5198b7acacc93930df9fa9e1ebea4a4394195b2332eNick Kralevich fd = openat(dfd, name, O_RDONLY | O_DIRECTORY | O_NOFOLLOW | O_CLOEXEC); 52094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (fd < 0) { 52194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood ALOGE("Couldn't openat %s: %s\n", name, strerror(errno)); 52294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood return -1; 52394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 52494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood d = fdopendir(fd); 52594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (d == NULL) { 52694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood ALOGE("Couldn't fdopendir %s: %s\n", name, strerror(errno)); 52794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood close(fd); 52894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood return -1; 52994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 53094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood res = _delete_dir_contents(d, 0); 53194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood closedir(d); 53294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood return res; 53394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood} 53494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 53560fd3feecab4336d964ca8e31c7c3220e1afb558Robin Leestatic int _copy_owner_permissions(int srcfd, int dstfd) 53660fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee{ 53760fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee struct stat st; 53860fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee if (fstat(srcfd, &st) != 0) { 53960fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee return -1; 54060fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee } 54160fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee if (fchmod(dstfd, st.st_mode) != 0) { 54260fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee return -1; 54360fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee } 54460fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee return 0; 54560fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee} 54660fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee 54760fd3feecab4336d964ca8e31c7c3220e1afb558Robin Leestatic int _copy_dir_files(int sdfd, int ddfd, uid_t owner, gid_t group) 54860fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee{ 54960fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee int result = 0; 55060fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee if (_copy_owner_permissions(sdfd, ddfd) != 0) { 55160fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee ALOGE("_copy_dir_files failed to copy dir permissions\n"); 55260fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee } 55360fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee if (fchown(ddfd, owner, group) != 0) { 55460fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee ALOGE("_copy_dir_files failed to change dir owner\n"); 55560fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee } 55660fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee 55760fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee DIR *ds = fdopendir(sdfd); 55860fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee if (ds == NULL) { 55960fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee ALOGE("Couldn't fdopendir: %s\n", strerror(errno)); 56060fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee return -1; 56160fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee } 56260fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee struct dirent *de; 56360fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee while ((de = readdir(ds))) { 56460fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee if (de->d_type != DT_REG) { 56560fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee continue; 56660fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee } 56760fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee 56860fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee const char *name = de->d_name; 56960fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee int fsfd = openat(sdfd, name, O_RDONLY | O_NOFOLLOW | O_CLOEXEC); 57060fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee int fdfd = openat(ddfd, name, O_WRONLY | O_NOFOLLOW | O_CLOEXEC | O_CREAT, 0600); 57160fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee if (fsfd == -1 || fdfd == -1) { 57260fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee ALOGW("Couldn't copy %s: %s\n", name, strerror(errno)); 57360fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee } else { 57460fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee if (_copy_owner_permissions(fsfd, fdfd) != 0) { 57560fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee ALOGE("Failed to change file permissions\n"); 57660fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee } 57760fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee if (fchown(fdfd, owner, group) != 0) { 57860fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee ALOGE("Failed to change file owner\n"); 57960fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee } 58060fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee 58160fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee char buf[8192]; 58260fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee ssize_t size; 58360fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee while ((size = read(fsfd, buf, sizeof(buf))) > 0) { 58460fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee write(fdfd, buf, size); 58560fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee } 58660fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee if (size < 0) { 58760fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee ALOGW("Couldn't copy %s: %s\n", name, strerror(errno)); 58860fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee result = -1; 58960fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee } 59060fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee } 59160fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee close(fdfd); 59260fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee close(fsfd); 59360fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee } 59460fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee 59560fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee return result; 59660fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee} 59760fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee 59860fd3feecab4336d964ca8e31c7c3220e1afb558Robin Leeint copy_dir_files(const char *srcname, 59960fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee const char *dstname, 60060fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee uid_t owner, 60160fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee uid_t group) 60260fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee{ 60360fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee int res = 0; 60460fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee DIR *ds = NULL; 60560fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee DIR *dd = NULL; 60660fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee 60760fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee ds = opendir(srcname); 60860fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee if (ds == NULL) { 60960fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee ALOGE("Couldn't opendir %s: %s\n", srcname, strerror(errno)); 61060fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee return -errno; 61160fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee } 61260fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee 61360fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee mkdir(dstname, 0600); 61460fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee dd = opendir(dstname); 61560fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee if (dd == NULL) { 61660fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee ALOGE("Couldn't opendir %s: %s\n", dstname, strerror(errno)); 61760fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee closedir(ds); 61860fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee return -errno; 61960fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee } 62060fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee 62160fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee int sdfd = dirfd(ds); 62260fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee int ddfd = dirfd(dd); 62360fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee if (sdfd != -1 && ddfd != -1) { 62460fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee res = _copy_dir_files(sdfd, ddfd, owner, group); 62560fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee } else { 62660fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee res = -errno; 62760fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee } 62860fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee closedir(dd); 62960fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee closedir(ds); 63060fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee return res; 63160fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee} 63260fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee 63341ea424413c0381ef2aa15fc5bd5d4b88abd23c4Jeff Sharkeyint64_t data_disk_free(const std::string& data_path) 63494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood{ 635ed909ae8db2f44ce7fe7003c6fee457f13669702Jeff Sharkey struct statvfs sfs; 636ed909ae8db2f44ce7fe7003c6fee457f13669702Jeff Sharkey if (statvfs(data_path.c_str(), &sfs) == 0) { 63794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood return sfs.f_bavail * sfs.f_bsize; 63894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } else { 639ed909ae8db2f44ce7fe7003c6fee457f13669702Jeff Sharkey PLOG(ERROR) << "Couldn't statvfs " << data_path; 64094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood return -1; 64194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 64294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood} 64394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 64494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwoodcache_t* start_cache_collection() 64594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood{ 64694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood cache_t* cache = (cache_t*)calloc(1, sizeof(cache_t)); 64794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood return cache; 64894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood} 64994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 65094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood#define CACHE_BLOCK_SIZE (512*1024) 65194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 65294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwoodstatic void* _cache_malloc(cache_t* cache, size_t len) 65394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood{ 65494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood len = (len+3)&~3; 65594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (len > (CACHE_BLOCK_SIZE/2)) { 65694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood // It doesn't make sense to try to put this allocation into one 65794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood // of our blocks, because it is so big. Instead, make a new dedicated 65894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood // block for it. 65994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood int8_t* res = (int8_t*)malloc(len+sizeof(void*)); 66094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (res == NULL) { 66194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood return NULL; 66294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 6639a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey CACHE_NOISY(ALOGI("Allocated large cache mem block: %p size %zu", res, len)); 66494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood // Link it into our list of blocks, not disrupting the current one. 66594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (cache->memBlocks == NULL) { 66694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood *(void**)res = NULL; 66794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood cache->memBlocks = res; 66894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } else { 66994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood *(void**)res = *(void**)cache->memBlocks; 67094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood *(void**)cache->memBlocks = res; 67194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 67294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood return res + sizeof(void*); 67394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 67494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood int8_t* res = cache->curMemBlockAvail; 67594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood int8_t* nextPos = res + len; 67694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (cache->memBlocks == NULL || nextPos > cache->curMemBlockEnd) { 67719803807cd7ae01868fcfa50305f4a7dd13765e2Jeff Sharkey int8_t* newBlock = (int8_t*) malloc(CACHE_BLOCK_SIZE); 67894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (newBlock == NULL) { 67994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood return NULL; 68094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 68194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood CACHE_NOISY(ALOGI("Allocated new cache mem block: %p", newBlock)); 68294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood *(void**)newBlock = cache->memBlocks; 68394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood cache->memBlocks = newBlock; 68494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood res = cache->curMemBlockAvail = newBlock + sizeof(void*); 68594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood cache->curMemBlockEnd = newBlock + CACHE_BLOCK_SIZE; 68694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood nextPos = res + len; 68794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 6889a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey CACHE_NOISY(ALOGI("cache_malloc: ret %p size %zu, block=%p, nextPos=%p", 68994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood res, len, cache->memBlocks, nextPos)); 69094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood cache->curMemBlockAvail = nextPos; 69194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood return res; 69294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood} 69394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 69494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwoodstatic void* _cache_realloc(cache_t* cache, void* cur, size_t origLen, size_t len) 69594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood{ 69694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood // This isn't really a realloc, but it is good enough for our purposes here. 69794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood void* alloc = _cache_malloc(cache, len); 69894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (alloc != NULL && cur != NULL) { 69994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood memcpy(alloc, cur, origLen < len ? origLen : len); 70094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 70194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood return alloc; 70294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood} 70394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 70494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwoodstatic void _inc_num_cache_collected(cache_t* cache) 70594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood{ 70694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood cache->numCollected++; 70794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if ((cache->numCollected%20000) == 0) { 70892dc3fc52cf097bd105460cf377779bdcf146d62Mark Salyzyn ALOGI("Collected cache so far: %zd directories, %zd files", 70994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood cache->numDirs, cache->numFiles); 71094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 71194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood} 71294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 71394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwoodstatic cache_dir_t* _add_cache_dir_t(cache_t* cache, cache_dir_t* parent, const char *name) 71494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood{ 71594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood size_t nameLen = strlen(name); 71694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood cache_dir_t* dir = (cache_dir_t*)_cache_malloc(cache, sizeof(cache_dir_t)+nameLen+1); 71794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (dir != NULL) { 71894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood dir->parent = parent; 71994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood dir->childCount = 0; 72094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood dir->hiddenCount = 0; 72194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood dir->deleted = 0; 72294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood strcpy(dir->name, name); 72394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (cache->numDirs >= cache->availDirs) { 72494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood size_t newAvail = cache->availDirs < 1000 ? 1000 : cache->availDirs*2; 72594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood cache_dir_t** newDirs = (cache_dir_t**)_cache_realloc(cache, cache->dirs, 72694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood cache->availDirs*sizeof(cache_dir_t*), newAvail*sizeof(cache_dir_t*)); 72794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (newDirs == NULL) { 72894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood ALOGE("Failure growing cache dirs array for %s\n", name); 72994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood return NULL; 73094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 73194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood cache->availDirs = newAvail; 73294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood cache->dirs = newDirs; 73394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 73494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood cache->dirs[cache->numDirs] = dir; 73594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood cache->numDirs++; 73694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (parent != NULL) { 73794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood parent->childCount++; 73894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 73994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood _inc_num_cache_collected(cache); 74094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } else { 74194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood ALOGE("Failure allocating cache_dir_t for %s\n", name); 74294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 74394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood return dir; 74494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood} 74594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 74694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwoodstatic cache_file_t* _add_cache_file_t(cache_t* cache, cache_dir_t* dir, time_t modTime, 74794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood const char *name) 74894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood{ 74994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood size_t nameLen = strlen(name); 75094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood cache_file_t* file = (cache_file_t*)_cache_malloc(cache, sizeof(cache_file_t)+nameLen+1); 75194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (file != NULL) { 75294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood file->dir = dir; 75394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood file->modTime = modTime; 75494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood strcpy(file->name, name); 75594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (cache->numFiles >= cache->availFiles) { 75694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood size_t newAvail = cache->availFiles < 1000 ? 1000 : cache->availFiles*2; 75794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood cache_file_t** newFiles = (cache_file_t**)_cache_realloc(cache, cache->files, 75894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood cache->availFiles*sizeof(cache_file_t*), newAvail*sizeof(cache_file_t*)); 75994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (newFiles == NULL) { 76094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood ALOGE("Failure growing cache file array for %s\n", name); 76194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood return NULL; 76294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 76394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood cache->availFiles = newAvail; 76494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood cache->files = newFiles; 76594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 7669a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey CACHE_NOISY(ALOGI("Setting file %p at position %zd in array %p", file, 76794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood cache->numFiles, cache->files)); 76894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood cache->files[cache->numFiles] = file; 76994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood cache->numFiles++; 77094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood dir->childCount++; 77194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood _inc_num_cache_collected(cache); 77294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } else { 77394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood ALOGE("Failure allocating cache_file_t for %s\n", name); 77494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 77594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood return file; 77694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood} 77794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 77894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwoodstatic int _add_cache_files(cache_t *cache, cache_dir_t *parentDir, const char *dirName, 77994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood DIR* dir, char *pathBase, char *pathPos, size_t pathAvailLen) 78094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood{ 78194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood struct dirent *de; 78294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood cache_dir_t* cacheDir = NULL; 78394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood int dfd; 78494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 78594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood CACHE_NOISY(ALOGI("_add_cache_files: parent=%p dirName=%s dir=%p pathBase=%s", 78694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood parentDir, dirName, dir, pathBase)); 78794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 78894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood dfd = dirfd(dir); 78994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 79094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (dfd < 0) return 0; 79194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 79294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood // Sub-directories always get added to the data structure, so if they 79394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood // are empty we will know about them to delete them later. 79494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood cacheDir = _add_cache_dir_t(cache, parentDir, dirName); 79594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 79694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood while ((de = readdir(dir))) { 79794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood const char *name = de->d_name; 79894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 79994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (de->d_type == DT_DIR) { 80094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood int subfd; 80194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood DIR *subdir; 80294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 80394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood /* always skip "." and ".." */ 80494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (name[0] == '.') { 80594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (name[1] == 0) continue; 80694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if ((name[1] == '.') && (name[2] == 0)) continue; 80794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 80894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 8098b7acacc93930df9fa9e1ebea4a4394195b2332eNick Kralevich subfd = openat(dfd, name, O_RDONLY | O_DIRECTORY | O_NOFOLLOW | O_CLOEXEC); 81094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (subfd < 0) { 81194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood ALOGE("Couldn't openat %s: %s\n", name, strerror(errno)); 81294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood continue; 81394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 81494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood subdir = fdopendir(subfd); 81594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (subdir == NULL) { 81694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood ALOGE("Couldn't fdopendir %s: %s\n", name, strerror(errno)); 81794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood close(subfd); 81894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood continue; 81994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 82094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (cacheDir == NULL) { 82194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood cacheDir = _add_cache_dir_t(cache, parentDir, dirName); 82294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 82394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (cacheDir != NULL) { 82494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood // Update pathBase for the new path... this may change dirName 82594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood // if that is also pointing to the path, but we are done with it 82694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood // now. 82794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood size_t finallen = snprintf(pathPos, pathAvailLen, "/%s", name); 82894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood CACHE_NOISY(ALOGI("Collecting dir %s\n", pathBase)); 82994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (finallen < pathAvailLen) { 83094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood _add_cache_files(cache, cacheDir, name, subdir, pathBase, 83194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood pathPos+finallen, pathAvailLen-finallen); 83294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } else { 83394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood // Whoops, the final path is too long! We'll just delete 83494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood // this directory. 83594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood ALOGW("Cache dir %s truncated in path %s; deleting dir\n", 83694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood name, pathBase); 83794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood _delete_dir_contents(subdir, NULL); 83894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (unlinkat(dfd, name, AT_REMOVEDIR) < 0) { 83994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood ALOGE("Couldn't unlinkat %s: %s\n", name, strerror(errno)); 84094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 84194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 84294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 84394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood closedir(subdir); 84494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } else if (de->d_type == DT_REG) { 84594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood // Skip files that start with '.'; they will be deleted if 84694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood // their entire directory is deleted. This allows for metadata 84794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood // like ".nomedia" to remain in the directory until the entire 84894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood // directory is deleted. 84994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (cacheDir == NULL) { 85094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood cacheDir = _add_cache_dir_t(cache, parentDir, dirName); 85194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 85294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (name[0] == '.') { 85394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood cacheDir->hiddenCount++; 85494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood continue; 85594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 85694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (cacheDir != NULL) { 85794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood // Build final full path for file... this may change dirName 85894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood // if that is also pointing to the path, but we are done with it 85994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood // now. 86094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood size_t finallen = snprintf(pathPos, pathAvailLen, "/%s", name); 86194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood CACHE_NOISY(ALOGI("Collecting file %s\n", pathBase)); 86294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (finallen < pathAvailLen) { 86394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood struct stat s; 86494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (stat(pathBase, &s) >= 0) { 86594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood _add_cache_file_t(cache, cacheDir, s.st_mtime, name); 86694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } else { 86794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood ALOGW("Unable to stat cache file %s; deleting\n", pathBase); 86894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (unlink(pathBase) < 0) { 86994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood ALOGE("Couldn't unlink %s: %s\n", pathBase, strerror(errno)); 87094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 87194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 87294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } else { 87394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood // Whoops, the final path is too long! We'll just delete 87494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood // this file. 87594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood ALOGW("Cache file %s truncated in path %s; deleting\n", 87694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood name, pathBase); 87794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (unlinkat(dfd, name, 0) < 0) { 87894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood *pathPos = 0; 87994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood ALOGE("Couldn't unlinkat %s in %s: %s\n", name, pathBase, 88094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood strerror(errno)); 88194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 88294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 88394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 88494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } else { 88594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood cacheDir->hiddenCount++; 88694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 88794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 88894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood return 0; 88994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood} 89094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 8919a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkeyint get_path_inode(const std::string& path, ino_t *inode) { 8929a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey struct stat buf; 8939a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey memset(&buf, 0, sizeof(buf)); 8949a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey if (stat(path.c_str(), &buf) != 0) { 8959a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey PLOG(WARNING) << "Failed to stat " << path; 8969a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey return -1; 8979a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey } else { 8989a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey *inode = buf.st_ino; 8999a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey return 0; 9009a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey } 9019a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey} 9029a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey 9039a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey/** 9049a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey * Write the inode of a specific child file into the given xattr on the 9059a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey * parent directory. This allows you to find the child later, even if its 9069a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey * name is encrypted. 9079a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey */ 9089a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkeyint write_path_inode(const std::string& parent, const char* name, const char* inode_xattr) { 9099a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey ino_t inode = 0; 9109a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey uint64_t inode_raw = 0; 9119a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey auto path = StringPrintf("%s/%s", parent.c_str(), name); 9129a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey 9139a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey if (get_path_inode(path, &inode) != 0) { 9149a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey // Path probably doesn't exist yet; ignore 9159a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey return 0; 9169a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey } 9179a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey 9189a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey // Check to see if already set correctly 9199a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey if (getxattr(parent.c_str(), inode_xattr, &inode_raw, sizeof(inode_raw)) == sizeof(inode_raw)) { 9209a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey if (inode_raw == inode) { 9219a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey // Already set correctly; skip writing 9229a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey return 0; 9239a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey } else { 9249a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey PLOG(WARNING) << "Mismatched inode value; found " << inode 9259a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey << " on disk but marked value was " << inode_raw << "; overwriting"; 9269a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey } 9279a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey } 9289a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey 9299a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey inode_raw = inode; 9304ed6507cfb4f0fae8567b42037e74a07f7dd28baJeff Sharkey if (setxattr(parent.c_str(), inode_xattr, &inode_raw, sizeof(inode_raw), 0) != 0 && errno != EOPNOTSUPP) { 9319a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey PLOG(ERROR) << "Failed to write xattr " << inode_xattr << " at " << parent; 9329a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey return -1; 9339a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey } else { 9349a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey return 0; 9359a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey } 9369a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey} 9379a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey 9389a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey/** 9399a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey * Read the inode of a specific child file from the given xattr on the 9409a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey * parent directory. Returns a currently valid path for that child, which 9419a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey * might have an encrypted name. 9429a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey */ 9439a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkeystd::string read_path_inode(const std::string& parent, const char* name, const char* inode_xattr) { 9449a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey ino_t inode = 0; 9459a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey uint64_t inode_raw = 0; 9469a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey auto fallback = StringPrintf("%s/%s", parent.c_str(), name); 9479a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey 9489a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey // Lookup the inode value written earlier 9499a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey if (getxattr(parent.c_str(), inode_xattr, &inode_raw, sizeof(inode_raw)) == sizeof(inode_raw)) { 9509a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey inode = inode_raw; 9519a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey } 9529a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey 9539a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey // For testing purposes, rely on the inode when defined; this could be 9549a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey // optimized to use access() in the future. 9559a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey if (inode != 0) { 9569a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey DIR* dir = opendir(parent.c_str()); 9579a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey if (dir == nullptr) { 9589a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey PLOG(ERROR) << "Failed to opendir " << parent; 9599a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey return fallback; 9609a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey } 9619a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey 9629a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey struct dirent* ent; 9639a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey while ((ent = readdir(dir))) { 9649a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey if (ent->d_ino == inode) { 9659a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey auto resolved = StringPrintf("%s/%s", parent.c_str(), ent->d_name); 9669a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey#if DEBUG_XATTRS 9679a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey if (resolved != fallback) { 9689a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey LOG(DEBUG) << "Resolved path " << resolved << " for inode " << inode 9699a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey << " instead of " << fallback; 9709a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey } 9719a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey#endif 9729a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey closedir(dir); 9739a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey return resolved; 9749a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey } 9759a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey } 9769a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey LOG(WARNING) << "Failed to resolve inode " << inode << "; using " << fallback; 9779a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey closedir(dir); 9789a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey return fallback; 9799a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey } else { 9809a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey return fallback; 9819a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey } 9829a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey} 9839a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey 98454e292e1ea87b504b552393d2e9bc800458bd0c1Jeff Sharkeyvoid add_cache_files(cache_t* cache, const std::string& data_path) { 98594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood DIR *d; 98694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood struct dirent *de; 98794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood char dirname[PATH_MAX]; 98894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 98954e292e1ea87b504b552393d2e9bc800458bd0c1Jeff Sharkey const char* basepath = data_path.c_str(); 99054e292e1ea87b504b552393d2e9bc800458bd0c1Jeff Sharkey CACHE_NOISY(ALOGI("add_cache_files: basepath=%s\n", basepath)); 99194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 99294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood d = opendir(basepath); 99394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (d == NULL) { 99494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood return; 99594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 99694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 99794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood while ((de = readdir(d))) { 99894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (de->d_type == DT_DIR) { 99994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood DIR* subdir; 100094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood const char *name = de->d_name; 100194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 100294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood /* always skip "." and ".." */ 100394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (name[0] == '.') { 100494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (name[1] == 0) continue; 100594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if ((name[1] == '.') && (name[2] == 0)) continue; 100694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 100794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 10089a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey auto parent = StringPrintf("%s/%s", basepath, name); 10099a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey auto resolved = read_path_inode(parent, "cache", kXattrInodeCache); 10109a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey strcpy(dirname, resolved.c_str()); 101194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood CACHE_NOISY(ALOGI("Adding cache files from dir: %s\n", dirname)); 101254e292e1ea87b504b552393d2e9bc800458bd0c1Jeff Sharkey 101394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood subdir = opendir(dirname); 101494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (subdir != NULL) { 101594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood size_t dirnameLen = strlen(dirname); 101694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood _add_cache_files(cache, NULL, dirname, subdir, dirname, dirname+dirnameLen, 101794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood PATH_MAX - dirnameLen); 101894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood closedir(subdir); 101994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 102094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 102194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 102294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 102394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood closedir(d); 102494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood} 102594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 1026f3124d8fab2377d6b5ec2c7cbc775e2ab20df3efFyodor Kupolovvoid add_preloads_file_cache(cache_t* cache, const char* volume_uuid) { 1027f3124d8fab2377d6b5ec2c7cbc775e2ab20df3efFyodor Kupolov char dirname[PATH_MAX]; 1028f3124d8fab2377d6b5ec2c7cbc775e2ab20df3efFyodor Kupolov DIR* subdir; 1029f3124d8fab2377d6b5ec2c7cbc775e2ab20df3efFyodor Kupolov auto cache_path = StringPrintf("%s/preloads/file_cache", create_data_path(volume_uuid).c_str()); 1030f3124d8fab2377d6b5ec2c7cbc775e2ab20df3efFyodor Kupolov strcpy(dirname, cache_path.c_str()); 1031f3124d8fab2377d6b5ec2c7cbc775e2ab20df3efFyodor Kupolov CACHE_NOISY(ALOGI("add_preloads_file_cache: dirname=%s\n", dirname)); 1032f3124d8fab2377d6b5ec2c7cbc775e2ab20df3efFyodor Kupolov subdir = opendir(dirname); 1033f3124d8fab2377d6b5ec2c7cbc775e2ab20df3efFyodor Kupolov if (subdir != NULL) { 1034f3124d8fab2377d6b5ec2c7cbc775e2ab20df3efFyodor Kupolov size_t dirnameLen = strlen(dirname); 1035f3124d8fab2377d6b5ec2c7cbc775e2ab20df3efFyodor Kupolov _add_cache_files(cache, NULL, dirname, subdir, dirname, dirname + dirnameLen, 1036f3124d8fab2377d6b5ec2c7cbc775e2ab20df3efFyodor Kupolov PATH_MAX - dirnameLen); 1037f3124d8fab2377d6b5ec2c7cbc775e2ab20df3efFyodor Kupolov closedir(subdir); 1038f3124d8fab2377d6b5ec2c7cbc775e2ab20df3efFyodor Kupolov } 1039f3124d8fab2377d6b5ec2c7cbc775e2ab20df3efFyodor Kupolov} 1040f3124d8fab2377d6b5ec2c7cbc775e2ab20df3efFyodor Kupolov 104194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwoodstatic char *create_dir_path(char path[PATH_MAX], cache_dir_t* dir) 104294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood{ 104394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood char *pos = path; 104494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (dir->parent != NULL) { 104594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood pos = create_dir_path(path, dir->parent); 104694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 104794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood // Note that we don't need to worry about going beyond the buffer, 104894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood // since when we were constructing the cache entries our maximum 104994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood // buffer size for full paths was PATH_MAX. 105094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood strcpy(pos, dir->name); 105194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood pos += strlen(pos); 105294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood *pos = '/'; 105394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood pos++; 105494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood *pos = 0; 105594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood return pos; 105694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood} 105794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 105894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwoodstatic void delete_cache_dir(char path[PATH_MAX], cache_dir_t* dir) 105994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood{ 106094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (dir->parent != NULL) { 106194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood create_dir_path(path, dir); 106294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood ALOGI("DEL DIR %s\n", path); 106394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (dir->hiddenCount <= 0) { 106494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (rmdir(path)) { 106594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood ALOGE("Couldn't rmdir %s: %s\n", path, strerror(errno)); 106694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood return; 106794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 106894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } else { 106994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood // The directory contains hidden files so we need to delete 107094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood // them along with the directory itself. 107194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (delete_dir_contents(path, 1, NULL)) { 107294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood return; 107394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 107494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 107594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood dir->parent->childCount--; 107694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood dir->deleted = 1; 107794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (dir->parent->childCount <= 0) { 107894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood delete_cache_dir(path, dir->parent); 107994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 108094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } else if (dir->hiddenCount > 0) { 108194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood // This is a root directory, but it has hidden files. Get rid of 108294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood // all of those files, but not the directory itself. 108394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood create_dir_path(path, dir); 108494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood ALOGI("DEL CONTENTS %s\n", path); 108594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood delete_dir_contents(path, 0, NULL); 108694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 108794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood} 108894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 108994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwoodstatic int cache_modtime_sort(const void *lhsP, const void *rhsP) 109094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood{ 109194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood const cache_file_t *lhs = *(const cache_file_t**)lhsP; 109294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood const cache_file_t *rhs = *(const cache_file_t**)rhsP; 109394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood return lhs->modTime < rhs->modTime ? -1 : (lhs->modTime > rhs->modTime ? 1 : 0); 109494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood} 109594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 109641ea424413c0381ef2aa15fc5bd5d4b88abd23c4Jeff Sharkeyvoid clear_cache_files(const std::string& data_path, cache_t* cache, int64_t free_size) 109794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood{ 109894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood size_t i; 109994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood int skip = 0; 110094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood char path[PATH_MAX]; 110194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 110292dc3fc52cf097bd105460cf377779bdcf146d62Mark Salyzyn ALOGI("Collected cache files: %zd directories, %zd files", 110394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood cache->numDirs, cache->numFiles); 110494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 110594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood CACHE_NOISY(ALOGI("Sorting files...")); 110694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood qsort(cache->files, cache->numFiles, sizeof(cache_file_t*), 110794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood cache_modtime_sort); 110894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 110994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood CACHE_NOISY(ALOGI("Cleaning empty directories...")); 111094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood for (i=cache->numDirs; i>0; i--) { 111194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood cache_dir_t* dir = cache->dirs[i-1]; 111294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (dir->childCount <= 0 && !dir->deleted) { 111394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood delete_cache_dir(path, dir); 111494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 111594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 111694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 111794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood CACHE_NOISY(ALOGI("Trimming files...")); 111894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood for (i=0; i<cache->numFiles; i++) { 111994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood skip++; 112094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (skip > 10) { 112141ea424413c0381ef2aa15fc5bd5d4b88abd23c4Jeff Sharkey if (data_disk_free(data_path) > free_size) { 112294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood return; 112394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 112494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood skip = 0; 112594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 112694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood cache_file_t* file = cache->files[i]; 112794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood strcpy(create_dir_path(path, file->dir), file->name); 112894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood ALOGI("DEL (mod %d) %s\n", (int)file->modTime, path); 112994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (unlink(path) < 0) { 113094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood ALOGE("Couldn't unlink %s: %s\n", path, strerror(errno)); 113194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 113294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood file->dir->childCount--; 113394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (file->dir->childCount <= 0) { 113494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood delete_cache_dir(path, file->dir); 113594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 113694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 113794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood} 113894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 113994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwoodvoid finish_cache_collection(cache_t* cache) 114094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood{ 114199d9fb15b4a1eb534261ae81b2cf25aec4447bd0Chih-Hung Hsieh CACHE_NOISY(size_t i;) 114294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 11439a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey CACHE_NOISY(ALOGI("clear_cache_files: %zu dirs, %zu files\n", cache->numDirs, cache->numFiles)); 114494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood CACHE_NOISY( 114594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood for (i=0; i<cache->numDirs; i++) { 114694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood cache_dir_t* dir = cache->dirs[i]; 11479a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey ALOGI("dir #%zu: %p %s parent=%p\n", i, dir, dir->name, dir->parent); 114894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood }) 114994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood CACHE_NOISY( 115094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood for (i=0; i<cache->numFiles; i++) { 115194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood cache_file_t* file = cache->files[i]; 11529a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey ALOGI("file #%zu: %p %s time=%d dir=%p\n", i, file, file->name, 115394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood (int)file->modTime, file->dir); 115494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood }) 115594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood void* block = cache->memBlocks; 115694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood while (block != NULL) { 115794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood void* nextBlock = *(void**)block; 115894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood CACHE_NOISY(ALOGI("Freeing cache mem block: %p", block)); 115994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood free(block); 116094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood block = nextBlock; 116194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 116294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood free(cache); 116394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood} 116494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 116594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood/** 1166c597b6dd895dbb2b28c757ce7a2651b3cdc9b00cCalin Juravle * Validate that the path is valid in the context of the provided directory. 1167c597b6dd895dbb2b28c757ce7a2651b3cdc9b00cCalin Juravle * The path is allowed to have at most one subdirectory and no indirections 1168c597b6dd895dbb2b28c757ce7a2651b3cdc9b00cCalin Juravle * to top level directories (i.e. have ".."). 1169c597b6dd895dbb2b28c757ce7a2651b3cdc9b00cCalin Juravle */ 1170e23a13299a4f6f2488935b4786cdbb46f46e3d3cJeff Sharkeystatic int validate_path(const dir_rec_t* dir, const char* path, int maxSubdirs) { 1171c597b6dd895dbb2b28c757ce7a2651b3cdc9b00cCalin Juravle size_t dir_len = dir->len; 1172c597b6dd895dbb2b28c757ce7a2651b3cdc9b00cCalin Juravle const char* subdir = strchr(path + dir_len, '/'); 1173c597b6dd895dbb2b28c757ce7a2651b3cdc9b00cCalin Juravle 1174c597b6dd895dbb2b28c757ce7a2651b3cdc9b00cCalin Juravle // Only allow the path to have at most one subdirectory. 1175c597b6dd895dbb2b28c757ce7a2651b3cdc9b00cCalin Juravle if (subdir != NULL) { 1176c597b6dd895dbb2b28c757ce7a2651b3cdc9b00cCalin Juravle ++subdir; 1177e23a13299a4f6f2488935b4786cdbb46f46e3d3cJeff Sharkey if ((--maxSubdirs == 0) && strchr(subdir, '/') != NULL) { 1178c597b6dd895dbb2b28c757ce7a2651b3cdc9b00cCalin Juravle ALOGE("invalid apk path '%s' (subdir?)\n", path); 1179c597b6dd895dbb2b28c757ce7a2651b3cdc9b00cCalin Juravle return -1; 1180c597b6dd895dbb2b28c757ce7a2651b3cdc9b00cCalin Juravle } 1181c597b6dd895dbb2b28c757ce7a2651b3cdc9b00cCalin Juravle } 1182c597b6dd895dbb2b28c757ce7a2651b3cdc9b00cCalin Juravle 1183c597b6dd895dbb2b28c757ce7a2651b3cdc9b00cCalin Juravle // Directories can't have a period directly after the directory markers to prevent "..". 1184c597b6dd895dbb2b28c757ce7a2651b3cdc9b00cCalin Juravle if ((path[dir_len] == '.') || ((subdir != NULL) && (*subdir == '.'))) { 1185c597b6dd895dbb2b28c757ce7a2651b3cdc9b00cCalin Juravle ALOGE("invalid apk path '%s' (trickery)\n", path); 1186c597b6dd895dbb2b28c757ce7a2651b3cdc9b00cCalin Juravle return -1; 1187c597b6dd895dbb2b28c757ce7a2651b3cdc9b00cCalin Juravle } 1188c597b6dd895dbb2b28c757ce7a2651b3cdc9b00cCalin Juravle 1189c597b6dd895dbb2b28c757ce7a2651b3cdc9b00cCalin Juravle return 0; 1190c597b6dd895dbb2b28c757ce7a2651b3cdc9b00cCalin Juravle} 1191c597b6dd895dbb2b28c757ce7a2651b3cdc9b00cCalin Juravle 1192c597b6dd895dbb2b28c757ce7a2651b3cdc9b00cCalin Juravle/** 119394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood * Checks whether a path points to a system app (.apk file). Returns 0 119494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood * if it is a system app or -1 if it is not. 119594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood */ 119694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwoodint validate_system_app_path(const char* path) { 119794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood size_t i; 119894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 119994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood for (i = 0; i < android_system_dirs.count; i++) { 120094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood const size_t dir_len = android_system_dirs.dirs[i].len; 120194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (!strncmp(path, android_system_dirs.dirs[i].path, dir_len)) { 1202e23a13299a4f6f2488935b4786cdbb46f46e3d3cJeff Sharkey return validate_path(android_system_dirs.dirs + i, path, 1); 120394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 120494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 120594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 120694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood return -1; 120794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood} 120894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 1209114f08107be384a3f9cc954bdec2b6b7619354aeCalin Juravlebool validate_secondary_dex_path(const std::string& pkgname, const std::string& dex_path, 121042451c029b0e87990e5833daea2286bb12c21df5Calin Juravle const char* volume_uuid, int uid, int storage_flag) { 121142451c029b0e87990e5833daea2286bb12c21df5Calin Juravle CHECK(storage_flag == FLAG_STORAGE_CE || storage_flag == FLAG_STORAGE_DE); 121242451c029b0e87990e5833daea2286bb12c21df5Calin Juravle 121342451c029b0e87990e5833daea2286bb12c21df5Calin Juravle std::string app_private_dir = storage_flag == FLAG_STORAGE_CE 1214114f08107be384a3f9cc954bdec2b6b7619354aeCalin Juravle ? create_data_user_ce_package_path( 1215114f08107be384a3f9cc954bdec2b6b7619354aeCalin Juravle volume_uuid, multiuser_get_user_id(uid), pkgname.c_str()) 1216114f08107be384a3f9cc954bdec2b6b7619354aeCalin Juravle : create_data_user_de_package_path( 1217114f08107be384a3f9cc954bdec2b6b7619354aeCalin Juravle volume_uuid, multiuser_get_user_id(uid), pkgname.c_str()); 121842451c029b0e87990e5833daea2286bb12c21df5Calin Juravle dir_rec_t dir; 121942451c029b0e87990e5833daea2286bb12c21df5Calin Juravle if (get_path_from_string(&dir, app_private_dir.c_str()) != 0) { 122042451c029b0e87990e5833daea2286bb12c21df5Calin Juravle LOG(WARNING) << "Could not get dir rec for " << app_private_dir; 122142451c029b0e87990e5833daea2286bb12c21df5Calin Juravle return false; 122242451c029b0e87990e5833daea2286bb12c21df5Calin Juravle } 122342451c029b0e87990e5833daea2286bb12c21df5Calin Juravle // Usually secondary dex files have a nested directory structure. 122442451c029b0e87990e5833daea2286bb12c21df5Calin Juravle // Pick at most 10 subdirectories when validating (arbitrary value). 122542451c029b0e87990e5833daea2286bb12c21df5Calin Juravle // If the secondary dex file is >10 directory nested then validation will 122642451c029b0e87990e5833daea2286bb12c21df5Calin Juravle // fail and the file will not be compiled. 1227114f08107be384a3f9cc954bdec2b6b7619354aeCalin Juravle return validate_path(&dir, dex_path.c_str(), /*max_subdirs*/ 10) == 0; 122842451c029b0e87990e5833daea2286bb12c21df5Calin Juravle} 122942451c029b0e87990e5833daea2286bb12c21df5Calin Juravle 123094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood/** 123194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood * Get the contents of a environment variable that contains a path. Caller 123294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood * owns the string that is inserted into the directory record. Returns 123394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood * 0 on success and -1 on error. 123494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood */ 123594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwoodint get_path_from_env(dir_rec_t* rec, const char* var) { 123694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood const char* path = getenv(var); 123794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood int ret = get_path_from_string(rec, path); 123894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (ret < 0) { 123994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood ALOGW("Problem finding value for environment variable %s\n", var); 124094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 124194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood return ret; 124294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood} 124394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 124494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood/** 124594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood * Puts the string into the record as a directory. Appends '/' to the end 124694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood * of all paths. Caller owns the string that is inserted into the directory 124794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood * record. A null value will result in an error. 124894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood * 124994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood * Returns 0 on success and -1 on error. 125094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood */ 125194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwoodint get_path_from_string(dir_rec_t* rec, const char* path) { 125294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (path == NULL) { 125394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood return -1; 125494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } else { 125594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood const size_t path_len = strlen(path); 125694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (path_len <= 0) { 125794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood return -1; 125894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 125994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 126094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood // Make sure path is absolute. 126194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (path[0] != '/') { 126294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood return -1; 126394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 126494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 126594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (path[path_len - 1] == '/') { 126694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood // Path ends with a forward slash. Make our own copy. 126794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 126894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood rec->path = strdup(path); 126994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (rec->path == NULL) { 127094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood return -1; 127194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 127294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 127394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood rec->len = path_len; 127494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } else { 127594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood // Path does not end with a slash. Generate a new string. 127694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood char *dst; 127794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 127894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood // Add space for slash and terminating null. 127994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood size_t dst_size = path_len + 2; 128094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 128119803807cd7ae01868fcfa50305f4a7dd13765e2Jeff Sharkey rec->path = (char*) malloc(dst_size); 128294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (rec->path == NULL) { 128394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood return -1; 128494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 128594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 128694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood dst = rec->path; 128794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 128894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (append_and_increment(&dst, path, &dst_size) < 0 128994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood || append_and_increment(&dst, "/", &dst_size)) { 129094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood ALOGE("Error canonicalizing path"); 129194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood return -1; 129294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 129394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 129494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood rec->len = dst - rec->path; 129594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 129694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 129794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood return 0; 129894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood} 129994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 130094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwoodint copy_and_append(dir_rec_t* dst, const dir_rec_t* src, const char* suffix) { 130194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood dst->len = src->len + strlen(suffix); 130294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood const size_t dstSize = dst->len + 1; 130394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood dst->path = (char*) malloc(dstSize); 130494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 130594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (dst->path == NULL 130694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood || snprintf(dst->path, dstSize, "%s%s", src->path, suffix) 130794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood != (ssize_t) dst->len) { 130894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood ALOGE("Could not allocate memory to hold appended path; aborting\n"); 130994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood return -1; 131094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 131194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 131294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood return 0; 131394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood} 131494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 131594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood/** 1316d845c96128a40ca5802c0840ae190fa0af7d4735Narayan Kamath * Check whether path points to a valid path for an APK file. The path must 1317d845c96128a40ca5802c0840ae190fa0af7d4735Narayan Kamath * begin with a whitelisted prefix path and must be no deeper than |maxSubdirs| within 1318d845c96128a40ca5802c0840ae190fa0af7d4735Narayan Kamath * that path. Returns -1 when an invalid path is encountered and 0 when a valid path 1319d845c96128a40ca5802c0840ae190fa0af7d4735Narayan Kamath * is encountered. 132094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood */ 1321d845c96128a40ca5802c0840ae190fa0af7d4735Narayan Kamathstatic int validate_apk_path_internal(const char *path, int maxSubdirs) { 1322c597b6dd895dbb2b28c757ce7a2651b3cdc9b00cCalin Juravle const dir_rec_t* dir = NULL; 132394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (!strncmp(path, android_app_dir.path, android_app_dir.len)) { 1324c597b6dd895dbb2b28c757ce7a2651b3cdc9b00cCalin Juravle dir = &android_app_dir; 132594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } else if (!strncmp(path, android_app_private_dir.path, android_app_private_dir.len)) { 1326c597b6dd895dbb2b28c757ce7a2651b3cdc9b00cCalin Juravle dir = &android_app_private_dir; 13275c1a910e6a2d2c42002dc6ed88ff770336afcb3fTodd Kennedy } else if (!strncmp(path, android_app_ephemeral_dir.path, android_app_ephemeral_dir.len)) { 13285c1a910e6a2d2c42002dc6ed88ff770336afcb3fTodd Kennedy dir = &android_app_ephemeral_dir; 132994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } else if (!strncmp(path, android_asec_dir.path, android_asec_dir.len)) { 1330c597b6dd895dbb2b28c757ce7a2651b3cdc9b00cCalin Juravle dir = &android_asec_dir; 1331e23a13299a4f6f2488935b4786cdbb46f46e3d3cJeff Sharkey } else if (!strncmp(path, android_mnt_expand_dir.path, android_mnt_expand_dir.len)) { 1332e23a13299a4f6f2488935b4786cdbb46f46e3d3cJeff Sharkey dir = &android_mnt_expand_dir; 1333d845c96128a40ca5802c0840ae190fa0af7d4735Narayan Kamath if (maxSubdirs < 2) { 1334d845c96128a40ca5802c0840ae190fa0af7d4735Narayan Kamath maxSubdirs = 2; 1335d845c96128a40ca5802c0840ae190fa0af7d4735Narayan Kamath } 133694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } else { 133794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood return -1; 133894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 133994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 1340e23a13299a4f6f2488935b4786cdbb46f46e3d3cJeff Sharkey return validate_path(dir, path, maxSubdirs); 134194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood} 134294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 1343d845c96128a40ca5802c0840ae190fa0af7d4735Narayan Kamathint validate_apk_path(const char* path) { 1344d845c96128a40ca5802c0840ae190fa0af7d4735Narayan Kamath return validate_apk_path_internal(path, 1 /* maxSubdirs */); 1345d845c96128a40ca5802c0840ae190fa0af7d4735Narayan Kamath} 1346d845c96128a40ca5802c0840ae190fa0af7d4735Narayan Kamath 1347d845c96128a40ca5802c0840ae190fa0af7d4735Narayan Kamathint validate_apk_path_subdirs(const char* path) { 1348d845c96128a40ca5802c0840ae190fa0af7d4735Narayan Kamath return validate_apk_path_internal(path, 3 /* maxSubdirs */); 1349d845c96128a40ca5802c0840ae190fa0af7d4735Narayan Kamath} 1350d845c96128a40ca5802c0840ae190fa0af7d4735Narayan Kamath 135194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwoodint append_and_increment(char** dst, const char* src, size_t* dst_size) { 135294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood ssize_t ret = strlcpy(*dst, src, *dst_size); 135394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (ret < 0 || (size_t) ret >= *dst_size) { 135494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood return -1; 135594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 135694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood *dst += ret; 135794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood *dst_size -= ret; 135894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood return 0; 135994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood} 136094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 136119803807cd7ae01868fcfa50305f4a7dd13765e2Jeff Sharkeychar *build_string2(const char *s1, const char *s2) { 136294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (s1 == NULL || s2 == NULL) return NULL; 136394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 136494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood int len_s1 = strlen(s1); 136594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood int len_s2 = strlen(s2); 136694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood int len = len_s1 + len_s2 + 1; 136719803807cd7ae01868fcfa50305f4a7dd13765e2Jeff Sharkey char *result = (char *) malloc(len); 136894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (result == NULL) return NULL; 136994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 137094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood strcpy(result, s1); 137194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood strcpy(result + len_s1, s2); 137294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 137394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood return result; 137494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood} 137594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 137619803807cd7ae01868fcfa50305f4a7dd13765e2Jeff Sharkeychar *build_string3(const char *s1, const char *s2, const char *s3) { 137794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (s1 == NULL || s2 == NULL || s3 == NULL) return NULL; 137894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 137994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood int len_s1 = strlen(s1); 138094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood int len_s2 = strlen(s2); 138194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood int len_s3 = strlen(s3); 138294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood int len = len_s1 + len_s2 + len_s3 + 1; 138319803807cd7ae01868fcfa50305f4a7dd13765e2Jeff Sharkey char *result = (char *) malloc(len); 138494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (result == NULL) return NULL; 138594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 138694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood strcpy(result, s1); 138794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood strcpy(result + len_s1, s2); 138894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood strcpy(result + len_s1 + len_s2, s3); 138994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 139094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood return result; 139194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood} 139294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 1393095c763dd9aa26a206d10ab7c1d7e1c569298fb3Robin Leeint ensure_config_user_dirs(userid_t userid) { 1394095c763dd9aa26a206d10ab7c1d7e1c569298fb3Robin Lee // writable by system, readable by any app within the same user 139560fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee const int uid = multiuser_get_uid(userid, AID_SYSTEM); 139660fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee const int gid = multiuser_get_uid(userid, AID_EVERYBODY); 1397095c763dd9aa26a206d10ab7c1d7e1c569298fb3Robin Lee 1398095c763dd9aa26a206d10ab7c1d7e1c569298fb3Robin Lee // Ensure /data/misc/user/<userid> exists 1399379a12b0072b322c7f86e690a8e8a220e500861cJeff Sharkey auto path = create_data_misc_legacy_path(userid); 1400379a12b0072b322c7f86e690a8e8a220e500861cJeff Sharkey return fs_prepare_dir(path.c_str(), 0750, uid, gid); 1401095c763dd9aa26a206d10ab7c1d7e1c569298fb3Robin Lee} 140202d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe 140302d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampeint wait_child(pid_t pid) 140402d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe{ 140502d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe int status; 140602d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe pid_t got_pid; 140702d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe 140802d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe while (1) { 140902d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe got_pid = waitpid(pid, &status, 0); 141002d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe if (got_pid == -1 && errno == EINTR) { 141102d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe printf("waitpid interrupted, retrying\n"); 141202d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe } else { 141302d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe break; 141402d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe } 141502d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe } 141602d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe if (got_pid != pid) { 141702d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe ALOGW("waitpid failed: wanted %d, got %d: %s\n", 141802d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe (int) pid, (int) got_pid, strerror(errno)); 141902d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe return 1; 142002d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe } 142102d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe 142202d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe if (WIFEXITED(status) && WEXITSTATUS(status) == 0) { 142302d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe return 0; 142402d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe } else { 142502d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe return status; /* always nonzero */ 142602d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe } 142702d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe} 142802d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe 142942451c029b0e87990e5833daea2286bb12c21df5Calin Juravle/** 143042451c029b0e87990e5833daea2286bb12c21df5Calin Juravle * Prepare an app cache directory, which offers to fix-up the GID and 143142451c029b0e87990e5833daea2286bb12c21df5Calin Juravle * directory mode flags during a platform upgrade. 143242451c029b0e87990e5833daea2286bb12c21df5Calin Juravle * The app cache directory path will be 'parent'/'name'. 143342451c029b0e87990e5833daea2286bb12c21df5Calin Juravle */ 143442451c029b0e87990e5833daea2286bb12c21df5Calin Juravleint prepare_app_cache_dir(const std::string& parent, const char* name, mode_t target_mode, 143542451c029b0e87990e5833daea2286bb12c21df5Calin Juravle uid_t uid, gid_t gid) { 143642451c029b0e87990e5833daea2286bb12c21df5Calin Juravle auto path = StringPrintf("%s/%s", parent.c_str(), name); 143742451c029b0e87990e5833daea2286bb12c21df5Calin Juravle struct stat st; 143842451c029b0e87990e5833daea2286bb12c21df5Calin Juravle if (stat(path.c_str(), &st) != 0) { 143942451c029b0e87990e5833daea2286bb12c21df5Calin Juravle if (errno == ENOENT) { 144042451c029b0e87990e5833daea2286bb12c21df5Calin Juravle // This is fine, just create it 144142451c029b0e87990e5833daea2286bb12c21df5Calin Juravle if (fs_prepare_dir_strict(path.c_str(), target_mode, uid, gid) != 0) { 144242451c029b0e87990e5833daea2286bb12c21df5Calin Juravle PLOG(ERROR) << "Failed to prepare " << path; 144342451c029b0e87990e5833daea2286bb12c21df5Calin Juravle return -1; 144442451c029b0e87990e5833daea2286bb12c21df5Calin Juravle } else { 144542451c029b0e87990e5833daea2286bb12c21df5Calin Juravle return 0; 144642451c029b0e87990e5833daea2286bb12c21df5Calin Juravle } 144742451c029b0e87990e5833daea2286bb12c21df5Calin Juravle } else { 144842451c029b0e87990e5833daea2286bb12c21df5Calin Juravle PLOG(ERROR) << "Failed to stat " << path; 144942451c029b0e87990e5833daea2286bb12c21df5Calin Juravle return -1; 145042451c029b0e87990e5833daea2286bb12c21df5Calin Juravle } 145142451c029b0e87990e5833daea2286bb12c21df5Calin Juravle } 145242451c029b0e87990e5833daea2286bb12c21df5Calin Juravle 145342451c029b0e87990e5833daea2286bb12c21df5Calin Juravle mode_t actual_mode = st.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO | S_ISGID); 145442451c029b0e87990e5833daea2286bb12c21df5Calin Juravle if (st.st_uid != uid) { 145542451c029b0e87990e5833daea2286bb12c21df5Calin Juravle // Mismatched UID is real trouble; we can't recover 145642451c029b0e87990e5833daea2286bb12c21df5Calin Juravle LOG(ERROR) << "Mismatched UID at " << path << ": found " << st.st_uid 145742451c029b0e87990e5833daea2286bb12c21df5Calin Juravle << " but expected " << uid; 145842451c029b0e87990e5833daea2286bb12c21df5Calin Juravle return -1; 145942451c029b0e87990e5833daea2286bb12c21df5Calin Juravle } else if (st.st_gid == gid && actual_mode == target_mode) { 146042451c029b0e87990e5833daea2286bb12c21df5Calin Juravle // Everything looks good! 146142451c029b0e87990e5833daea2286bb12c21df5Calin Juravle return 0; 146242451c029b0e87990e5833daea2286bb12c21df5Calin Juravle } 146342451c029b0e87990e5833daea2286bb12c21df5Calin Juravle 146442451c029b0e87990e5833daea2286bb12c21df5Calin Juravle // Directory is owned correctly, but GID or mode mismatch means it's 146542451c029b0e87990e5833daea2286bb12c21df5Calin Juravle // probably a platform upgrade so we need to fix them 146642451c029b0e87990e5833daea2286bb12c21df5Calin Juravle FTS *fts; 146742451c029b0e87990e5833daea2286bb12c21df5Calin Juravle FTSENT *p; 146842451c029b0e87990e5833daea2286bb12c21df5Calin Juravle char *argv[] = { (char*) path.c_str(), nullptr }; 1469b26786d647b624498c11405075e5223d1853f30aJeff Sharkey if (!(fts = fts_open(argv, FTS_PHYSICAL | FTS_NOCHDIR | FTS_XDEV, NULL))) { 147042451c029b0e87990e5833daea2286bb12c21df5Calin Juravle PLOG(ERROR) << "Failed to fts_open " << path; 147142451c029b0e87990e5833daea2286bb12c21df5Calin Juravle return -1; 147242451c029b0e87990e5833daea2286bb12c21df5Calin Juravle } 147342451c029b0e87990e5833daea2286bb12c21df5Calin Juravle while ((p = fts_read(fts)) != NULL) { 147442451c029b0e87990e5833daea2286bb12c21df5Calin Juravle switch (p->fts_info) { 147542451c029b0e87990e5833daea2286bb12c21df5Calin Juravle case FTS_DP: 147642451c029b0e87990e5833daea2286bb12c21df5Calin Juravle if (chmod(p->fts_accpath, target_mode) != 0) { 147742451c029b0e87990e5833daea2286bb12c21df5Calin Juravle PLOG(WARNING) << "Failed to chmod " << p->fts_path; 147842451c029b0e87990e5833daea2286bb12c21df5Calin Juravle } 147942451c029b0e87990e5833daea2286bb12c21df5Calin Juravle // Intentional fall through to also set GID 148042451c029b0e87990e5833daea2286bb12c21df5Calin Juravle case FTS_F: 148142451c029b0e87990e5833daea2286bb12c21df5Calin Juravle if (chown(p->fts_accpath, -1, gid) != 0) { 148242451c029b0e87990e5833daea2286bb12c21df5Calin Juravle PLOG(WARNING) << "Failed to chown " << p->fts_path; 148342451c029b0e87990e5833daea2286bb12c21df5Calin Juravle } 148442451c029b0e87990e5833daea2286bb12c21df5Calin Juravle break; 148542451c029b0e87990e5833daea2286bb12c21df5Calin Juravle case FTS_SL: 148642451c029b0e87990e5833daea2286bb12c21df5Calin Juravle case FTS_SLNONE: 148742451c029b0e87990e5833daea2286bb12c21df5Calin Juravle if (lchown(p->fts_accpath, -1, gid) != 0) { 148842451c029b0e87990e5833daea2286bb12c21df5Calin Juravle PLOG(WARNING) << "Failed to chown " << p->fts_path; 148942451c029b0e87990e5833daea2286bb12c21df5Calin Juravle } 149042451c029b0e87990e5833daea2286bb12c21df5Calin Juravle break; 149142451c029b0e87990e5833daea2286bb12c21df5Calin Juravle } 149242451c029b0e87990e5833daea2286bb12c21df5Calin Juravle } 149342451c029b0e87990e5833daea2286bb12c21df5Calin Juravle fts_close(fts); 149442451c029b0e87990e5833daea2286bb12c21df5Calin Juravle return 0; 149542451c029b0e87990e5833daea2286bb12c21df5Calin Juravle} 149642451c029b0e87990e5833daea2286bb12c21df5Calin Juravle 149702d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe} // namespace installd 149802d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe} // namespace android 1499