utils.cpp revision 367ace2f77105f7a09b34a50bc875fd4fa591177
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> 2602d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe 2702d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe#if defined(__APPLE__) 2802d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe#include <sys/mount.h> 2902d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe#else 3002d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe#include <sys/statfs.h> 3102d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe#endif 3294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 33e4ec9eb7b4c452493589983970ba5ccc501728d1Elliott Hughes#include <android-base/logging.h> 3402d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe#include <android-base/stringprintf.h> 3502d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe#include <cutils/fs.h> 36871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey#include <cutils/properties.h> 377823e124e00576e20e47ec717cbe8bc89f0f2bf2Mark Salyzyn#include <log/log.h> 3802d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe#include <private/android_filesystem_config.h> 3902d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe 4002d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe#include "globals.h" // extern variables. 4194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 4202d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe#ifndef LOG_TAG 4302d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe#define LOG_TAG "installd" 4402d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe#endif 459a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey 46c03de09173f98506e73e7cf7df21fe11795d4b24Jeff Sharkey#define CACHE_NOISY(x) //x 479a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey#define DEBUG_XATTRS 0 4894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 49c03de09173f98506e73e7cf7df21fe11795d4b24Jeff Sharkeyusing android::base::StringPrintf; 5094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 5102d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampenamespace android { 5202d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampenamespace installd { 5302d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe 5494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood/** 55c03de09173f98506e73e7cf7df21fe11795d4b24Jeff Sharkey * Check that given string is valid filename, and that it attempts no 56c03de09173f98506e73e7cf7df21fe11795d4b24Jeff Sharkey * parent or child directory traversal. 5794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood */ 58423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkeybool is_valid_filename(const std::string& name) { 59c03de09173f98506e73e7cf7df21fe11795d4b24Jeff Sharkey if (name.empty() || (name == ".") || (name == "..") 60c03de09173f98506e73e7cf7df21fe11795d4b24Jeff Sharkey || (name.find('/') != std::string::npos)) { 61c03de09173f98506e73e7cf7df21fe11795d4b24Jeff Sharkey return false; 6294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } else { 63c03de09173f98506e73e7cf7df21fe11795d4b24Jeff Sharkey return true; 6494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 65c03de09173f98506e73e7cf7df21fe11795d4b24Jeff Sharkey} 6694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 676a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravlestatic void check_package_name(const char* package_name) { 686a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle CHECK(is_valid_filename(package_name)); 69423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey CHECK(is_valid_package_name(package_name)); 706a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle} 716a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle 72c03de09173f98506e73e7cf7df21fe11795d4b24Jeff Sharkey/** 73d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey * Create the path name where package app contents should be stored for 74d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey * the given volume UUID and package name. An empty UUID is assumed to 75d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey * be internal storage. 76d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey */ 77d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkeystd::string create_data_app_package_path(const char* volume_uuid, 78d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey const char* package_name) { 796a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle check_package_name(package_name); 80d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey return StringPrintf("%s/%s", 81d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey create_data_app_path(volume_uuid).c_str(), package_name); 82d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey} 83d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey 84d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey/** 85c03de09173f98506e73e7cf7df21fe11795d4b24Jeff Sharkey * Create the path name where package data should be stored for the given 86c03de09173f98506e73e7cf7df21fe11795d4b24Jeff Sharkey * volume UUID, package name, and user ID. An empty UUID is assumed to be 87c03de09173f98506e73e7cf7df21fe11795d4b24Jeff Sharkey * internal storage. 88c03de09173f98506e73e7cf7df21fe11795d4b24Jeff Sharkey */ 892f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkeystd::string create_data_user_ce_package_path(const char* volume_uuid, 90d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey userid_t user, const char* package_name) { 916a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle check_package_name(package_name); 92d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey return StringPrintf("%s/%s", 932f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey create_data_user_ce_path(volume_uuid, user).c_str(), package_name); 942f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey} 952f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey 962f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkeystd::string create_data_user_ce_package_path(const char* volume_uuid, userid_t user, 972f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey const char* package_name, ino_t ce_data_inode) { 982f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey // For testing purposes, rely on the inode when defined; this could be 992f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey // optimized to use access() in the future. 1002f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey auto fallback = create_data_user_ce_package_path(volume_uuid, user, package_name); 1012f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey if (ce_data_inode != 0) { 1022f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey auto user_path = create_data_user_ce_path(volume_uuid, user); 1032f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey DIR* dir = opendir(user_path.c_str()); 1042f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey if (dir == nullptr) { 1052f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey PLOG(ERROR) << "Failed to opendir " << user_path; 1062f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey return fallback; 1072f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey } 1082f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey 1092f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey struct dirent* ent; 1102f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey while ((ent = readdir(dir))) { 1112f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey if (ent->d_ino == ce_data_inode) { 1121d992f9f7886f98e46f98430d6c1d061cc31fdb3Jeff Sharkey auto resolved = StringPrintf("%s/%s", user_path.c_str(), ent->d_name); 1139a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey#if DEBUG_XATTRS 1141d992f9f7886f98e46f98430d6c1d061cc31fdb3Jeff Sharkey if (resolved != fallback) { 1151d992f9f7886f98e46f98430d6c1d061cc31fdb3Jeff Sharkey LOG(DEBUG) << "Resolved path " << resolved << " for inode " << ce_data_inode 1161d992f9f7886f98e46f98430d6c1d061cc31fdb3Jeff Sharkey << " instead of " << fallback; 1171d992f9f7886f98e46f98430d6c1d061cc31fdb3Jeff Sharkey } 1189a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey#endif 1192f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey closedir(dir); 1201d992f9f7886f98e46f98430d6c1d061cc31fdb3Jeff Sharkey return resolved; 1212f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey } 1222f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey } 1231d992f9f7886f98e46f98430d6c1d061cc31fdb3Jeff Sharkey LOG(WARNING) << "Failed to resolve inode " << ce_data_inode << "; using " << fallback; 1242f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey closedir(dir); 1252f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey return fallback; 1262f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey } else { 1272f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey return fallback; 1282f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey } 129c03de09173f98506e73e7cf7df21fe11795d4b24Jeff Sharkey} 13094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 13163ec2d64196144b2d15d2baffedccfa011d6494fJeff Sharkeystd::string create_data_user_de_package_path(const char* volume_uuid, 13263ec2d64196144b2d15d2baffedccfa011d6494fJeff Sharkey userid_t user, const char* package_name) { 1336a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle check_package_name(package_name); 13463ec2d64196144b2d15d2baffedccfa011d6494fJeff Sharkey return StringPrintf("%s/%s", 13563ec2d64196144b2d15d2baffedccfa011d6494fJeff Sharkey create_data_user_de_path(volume_uuid, user).c_str(), package_name); 13663ec2d64196144b2d15d2baffedccfa011d6494fJeff Sharkey} 13763ec2d64196144b2d15d2baffedccfa011d6494fJeff Sharkey 138c03de09173f98506e73e7cf7df21fe11795d4b24Jeff Sharkeyint create_pkg_path(char path[PKG_PATH_MAX], const char *pkgname, 139c03de09173f98506e73e7cf7df21fe11795d4b24Jeff Sharkey const char *postfix, userid_t userid) { 140423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey if (!is_valid_package_name(pkgname)) { 141c03de09173f98506e73e7cf7df21fe11795d4b24Jeff Sharkey path[0] = '\0'; 14294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood return -1; 14394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 14494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 1452f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey std::string _tmp(create_data_user_ce_package_path(nullptr, userid, pkgname) + postfix); 146c03de09173f98506e73e7cf7df21fe11795d4b24Jeff Sharkey const char* tmp = _tmp.c_str(); 147c03de09173f98506e73e7cf7df21fe11795d4b24Jeff Sharkey if (strlen(tmp) >= PKG_PATH_MAX) { 148c03de09173f98506e73e7cf7df21fe11795d4b24Jeff Sharkey path[0] = '\0'; 149c03de09173f98506e73e7cf7df21fe11795d4b24Jeff Sharkey return -1; 150c03de09173f98506e73e7cf7df21fe11795d4b24Jeff Sharkey } else { 151c03de09173f98506e73e7cf7df21fe11795d4b24Jeff Sharkey strcpy(path, tmp); 152c03de09173f98506e73e7cf7df21fe11795d4b24Jeff Sharkey return 0; 15394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 15494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood} 15594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 15641ea424413c0381ef2aa15fc5bd5d4b88abd23c4Jeff Sharkeystd::string create_data_path(const char* volume_uuid) { 15741ea424413c0381ef2aa15fc5bd5d4b88abd23c4Jeff Sharkey if (volume_uuid == nullptr) { 15841ea424413c0381ef2aa15fc5bd5d4b88abd23c4Jeff Sharkey return "/data"; 159871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey } else if (!strcmp(volume_uuid, "TEST")) { 160871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey CHECK(property_get_bool("ro.debuggable", false)); 161871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey return "/data/local/tmp"; 16294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } else { 16341ea424413c0381ef2aa15fc5bd5d4b88abd23c4Jeff Sharkey CHECK(is_valid_filename(volume_uuid)); 16441ea424413c0381ef2aa15fc5bd5d4b88abd23c4Jeff Sharkey return StringPrintf("/mnt/expand/%s", volume_uuid); 16594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 16641ea424413c0381ef2aa15fc5bd5d4b88abd23c4Jeff Sharkey} 16794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 16841ea424413c0381ef2aa15fc5bd5d4b88abd23c4Jeff Sharkey/** 169d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey * Create the path name for app data. 170d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey */ 171d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkeystd::string create_data_app_path(const char* volume_uuid) { 172d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey return StringPrintf("%s/app", create_data_path(volume_uuid).c_str()); 173d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey} 174d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey 175d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey/** 17641ea424413c0381ef2aa15fc5bd5d4b88abd23c4Jeff Sharkey * Create the path name for user data for a certain userid. 17741ea424413c0381ef2aa15fc5bd5d4b88abd23c4Jeff Sharkey */ 1782f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkeystd::string create_data_user_ce_path(const char* volume_uuid, userid_t userid) { 17941ea424413c0381ef2aa15fc5bd5d4b88abd23c4Jeff Sharkey std::string data(create_data_path(volume_uuid)); 18041ea424413c0381ef2aa15fc5bd5d4b88abd23c4Jeff Sharkey if (volume_uuid == nullptr) { 18141ea424413c0381ef2aa15fc5bd5d4b88abd23c4Jeff Sharkey if (userid == 0) { 18241ea424413c0381ef2aa15fc5bd5d4b88abd23c4Jeff Sharkey return StringPrintf("%s/data", data.c_str()); 18341ea424413c0381ef2aa15fc5bd5d4b88abd23c4Jeff Sharkey } else { 18441ea424413c0381ef2aa15fc5bd5d4b88abd23c4Jeff Sharkey return StringPrintf("%s/user/%u", data.c_str(), userid); 18594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 18641ea424413c0381ef2aa15fc5bd5d4b88abd23c4Jeff Sharkey } else { 18741ea424413c0381ef2aa15fc5bd5d4b88abd23c4Jeff Sharkey return StringPrintf("%s/user/%u", data.c_str(), userid); 18894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 18994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood} 19094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 19194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood/** 19263ec2d64196144b2d15d2baffedccfa011d6494fJeff Sharkey * Create the path name for device encrypted user data for a certain userid. 19363ec2d64196144b2d15d2baffedccfa011d6494fJeff Sharkey */ 19463ec2d64196144b2d15d2baffedccfa011d6494fJeff Sharkeystd::string create_data_user_de_path(const char* volume_uuid, userid_t userid) { 19563ec2d64196144b2d15d2baffedccfa011d6494fJeff Sharkey std::string data(create_data_path(volume_uuid)); 19663ec2d64196144b2d15d2baffedccfa011d6494fJeff Sharkey return StringPrintf("%s/user_de/%u", data.c_str(), userid); 19763ec2d64196144b2d15d2baffedccfa011d6494fJeff Sharkey} 19863ec2d64196144b2d15d2baffedccfa011d6494fJeff Sharkey 19963ec2d64196144b2d15d2baffedccfa011d6494fJeff Sharkey/** 200abe4fe5b46157ecd2a52d28abf938c816c3ce878Jeff Sharkey * Create the path name for media for a certain userid. 20194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood */ 20241ea424413c0381ef2aa15fc5bd5d4b88abd23c4Jeff Sharkeystd::string create_data_media_path(const char* volume_uuid, userid_t userid) { 20341ea424413c0381ef2aa15fc5bd5d4b88abd23c4Jeff Sharkey return StringPrintf("%s/media/%u", create_data_path(volume_uuid).c_str(), userid); 20494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood} 20594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 206df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkeystd::string create_data_media_obb_path(const char* volume_uuid, const char* package_name) { 207df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey return StringPrintf("%s/media/obb/%s", create_data_path(volume_uuid).c_str(), package_name); 208df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey} 209df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey 2103dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkeystd::string create_data_media_package_path(const char* volume_uuid, userid_t userid, 2113dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey const char* data_type, const char* package_name) { 2123dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey return StringPrintf("%s/Android/%s/%s", create_data_media_path(volume_uuid, userid).c_str(), 2133dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey data_type, package_name); 2143dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey} 2153dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey 216379a12b0072b322c7f86e690a8e8a220e500861cJeff Sharkeystd::string create_data_misc_legacy_path(userid_t userid) { 217379a12b0072b322c7f86e690a8e8a220e500861cJeff Sharkey return StringPrintf("%s/misc/user/%u", create_data_path(nullptr).c_str(), userid); 218379a12b0072b322c7f86e690a8e8a220e500861cJeff Sharkey} 219379a12b0072b322c7f86e690a8e8a220e500861cJeff Sharkey 220df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkeystd::string create_data_user_profile_path(userid_t userid) { 2216a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle return StringPrintf("%s/cur/%u", android_profiles_dir.path, userid); 2226a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle} 2236a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle 2246a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravlestd::string create_data_user_profile_package_path(userid_t user, const char* package_name) { 2256a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle check_package_name(package_name); 226df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey return StringPrintf("%s/%s",create_data_user_profile_path(user).c_str(), package_name); 227df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey} 228df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey 229df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkeystd::string create_data_ref_profile_path() { 230df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey return StringPrintf("%s/ref", android_profiles_dir.path); 2316a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle} 2326a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle 2336a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravlestd::string create_data_ref_profile_package_path(const char* package_name) { 2346a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle check_package_name(package_name); 2356a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle return StringPrintf("%s/ref/%s", android_profiles_dir.path, package_name); 2366a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle} 2376a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle 2383dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkeystd::string create_data_dalvik_cache_path() { 2393dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey return "/data/dalvik-cache"; 2403dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey} 2413dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey 2423dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkeystd::string create_data_misc_foreign_dex_path(userid_t userid) { 2433dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey return StringPrintf("/data/misc/profiles/cur/%d/foreign-dex", userid); 2443dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey} 2453dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey 24690aff26f0135379db19432ae90c40c0831ba5954Jeff Sharkey// Keep profile paths in sync with ActivityThread. 24790aff26f0135379db19432ae90c40c0831ba5954Jeff Sharkeyconstexpr const char* PRIMARY_PROFILE_NAME = "primary.prof"; 24890aff26f0135379db19432ae90c40c0831ba5954Jeff Sharkey 24990aff26f0135379db19432ae90c40c0831ba5954Jeff Sharkeystd::string create_primary_profile(const std::string& profile_dir) { 25090aff26f0135379db19432ae90c40c0831ba5954Jeff Sharkey return StringPrintf("%s/%s", profile_dir.c_str(), PRIMARY_PROFILE_NAME); 25190aff26f0135379db19432ae90c40c0831ba5954Jeff Sharkey} 25290aff26f0135379db19432ae90c40c0831ba5954Jeff Sharkey 253e36372423000a906bafae68844ebc6c42d09335aJeff Sharkeystd::vector<userid_t> get_known_users(const char* volume_uuid) { 254e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey std::vector<userid_t> users; 255e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey 256e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey // We always have an owner 257e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey users.push_back(0); 258e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey 259e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey std::string path(create_data_path(volume_uuid) + "/" + SECONDARY_USER_PREFIX); 260e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey DIR* dir = opendir(path.c_str()); 261e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey if (dir == NULL) { 262e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey // Unable to discover other users, but at least return owner 263e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey PLOG(ERROR) << "Failed to opendir " << path; 264e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey return users; 265e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey } 266e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey 267e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey struct dirent* ent; 268e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey while ((ent = readdir(dir))) { 269e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey if (ent->d_type != DT_DIR) { 270e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey continue; 271e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey } 272e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey 273e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey char* end; 274e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey userid_t user = strtol(ent->d_name, &end, 10); 275e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey if (*end == '\0' && user != 0) { 276e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey LOG(DEBUG) << "Found valid user " << user; 277e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey users.push_back(user); 278e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey } 279e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey } 280e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey closedir(dir); 281e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey 282e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey return users; 283e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey} 284e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey 2853dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkeyint calculate_tree_size(const std::string& path, int64_t* size, 286df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey int32_t include_gid, int32_t exclude_gid, bool exclude_apps) { 2873dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey FTS *fts; 2883dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey FTSENT *p; 289df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey int64_t matchedSize = 0; 2903dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey char *argv[] = { (char*) path.c_str(), nullptr }; 2913dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey if (!(fts = fts_open(argv, FTS_PHYSICAL | FTS_XDEV, NULL))) { 2923dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey if (errno != ENOENT) { 2933dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey PLOG(ERROR) << "Failed to fts_open " << path; 2943dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey } 2953dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey return -1; 2963dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey } 2973dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey while ((p = fts_read(fts)) != NULL) { 2983dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey switch (p->fts_info) { 2993dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey case FTS_D: 3003dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey case FTS_DEFAULT: 3013dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey case FTS_F: 3023dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey case FTS_SL: 3033dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey case FTS_SLNONE: 304df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey int32_t uid = p->fts_statp->st_uid; 305df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey int32_t gid = p->fts_statp->st_gid; 306df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey int32_t user_uid = multiuser_get_app_id(uid); 307df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey int32_t user_gid = multiuser_get_app_id(gid); 308df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey if (exclude_apps && ((user_uid >= AID_APP_START && user_uid <= AID_APP_END) 309df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey || (user_gid >= AID_CACHE_GID_START && user_gid <= AID_CACHE_GID_END) 310df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey || (user_gid >= AID_SHARED_GID_START && user_gid <= AID_SHARED_GID_END))) { 311df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey // Don't traverse inside or measure 312df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey fts_set(fts, p, FTS_SKIP); 3133dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey break; 3143dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey } 315df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey if (include_gid != -1 && gid != include_gid) { 3163dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey break; 3173dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey } 318df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey if (exclude_gid != -1 && gid == exclude_gid) { 319df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey break; 320df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey } 321df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey matchedSize += (p->fts_statp->st_blocks * 512); 3223dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey break; 3233dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey } 3243dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey } 3253dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey fts_close(fts); 326df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey#if MEASURE_DEBUG 327df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey if ((include_gid == -1) && (exclude_gid == -1)) { 328df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey LOG(DEBUG) << "Measured " << path << " size " << matchedSize; 329df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey } else { 330df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey LOG(DEBUG) << "Measured " << path << " size " << matchedSize << "; include " << include_gid 331df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey << " exclude " << exclude_gid; 332df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey } 333df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey#endif 334df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey *size += matchedSize; 3353dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey return 0; 3363dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey} 3373dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey 33894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwoodint create_move_path(char path[PKG_PATH_MAX], 33994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood const char* pkgname, 34094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood const char* leaf, 34102d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe userid_t userid ATTRIBUTE_UNUSED) 34294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood{ 34394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if ((android_data_dir.len + strlen(PRIMARY_USER_PREFIX) + strlen(pkgname) + strlen(leaf) + 1) 34494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood >= PKG_PATH_MAX) { 34594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood return -1; 34694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 34794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 34894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood sprintf(path, "%s%s%s/%s", android_data_dir.path, PRIMARY_USER_PREFIX, pkgname, leaf); 34994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood return 0; 35094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood} 35194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 35294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood/** 35394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood * Checks whether the package name is valid. Returns -1 on error and 35494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood * 0 on success. 35594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood */ 356423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkeybool is_valid_package_name(const std::string& packageName) { 357367ace2f77105f7a09b34a50bc875fd4fa591177Jeff Sharkey // This logic is borrowed from PackageParser.java 358367ace2f77105f7a09b34a50bc875fd4fa591177Jeff Sharkey bool hasSep = false; 359367ace2f77105f7a09b34a50bc875fd4fa591177Jeff Sharkey bool front = true; 360367ace2f77105f7a09b34a50bc875fd4fa591177Jeff Sharkey 361367ace2f77105f7a09b34a50bc875fd4fa591177Jeff Sharkey auto it = packageName.begin(); 362367ace2f77105f7a09b34a50bc875fd4fa591177Jeff Sharkey for (; it != packageName.end() && *it != '-'; it++) { 363367ace2f77105f7a09b34a50bc875fd4fa591177Jeff Sharkey char c = *it; 364367ace2f77105f7a09b34a50bc875fd4fa591177Jeff Sharkey if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) { 365367ace2f77105f7a09b34a50bc875fd4fa591177Jeff Sharkey front = false; 366367ace2f77105f7a09b34a50bc875fd4fa591177Jeff Sharkey continue; 367367ace2f77105f7a09b34a50bc875fd4fa591177Jeff Sharkey } 368367ace2f77105f7a09b34a50bc875fd4fa591177Jeff Sharkey if (!front) { 369367ace2f77105f7a09b34a50bc875fd4fa591177Jeff Sharkey if ((c >= '0' && c <= '9') || c == '_') { 370367ace2f77105f7a09b34a50bc875fd4fa591177Jeff Sharkey continue; 37194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 37294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 373367ace2f77105f7a09b34a50bc875fd4fa591177Jeff Sharkey if (c == '.') { 374367ace2f77105f7a09b34a50bc875fd4fa591177Jeff Sharkey hasSep = true; 375367ace2f77105f7a09b34a50bc875fd4fa591177Jeff Sharkey front = true; 376367ace2f77105f7a09b34a50bc875fd4fa591177Jeff Sharkey continue; 377367ace2f77105f7a09b34a50bc875fd4fa591177Jeff Sharkey } 378367ace2f77105f7a09b34a50bc875fd4fa591177Jeff Sharkey LOG(WARNING) << "Bad package character " << c << " in " << packageName; 379367ace2f77105f7a09b34a50bc875fd4fa591177Jeff Sharkey return false; 380367ace2f77105f7a09b34a50bc875fd4fa591177Jeff Sharkey } 38194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 382367ace2f77105f7a09b34a50bc875fd4fa591177Jeff Sharkey if (!hasSep || front) { 383367ace2f77105f7a09b34a50bc875fd4fa591177Jeff Sharkey LOG(WARNING) << "Missing separator in " << packageName; 384367ace2f77105f7a09b34a50bc875fd4fa591177Jeff Sharkey return false; 38594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 38694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 387367ace2f77105f7a09b34a50bc875fd4fa591177Jeff Sharkey for (; it != packageName.end(); it++) { 388367ace2f77105f7a09b34a50bc875fd4fa591177Jeff Sharkey char c = *it; 389367ace2f77105f7a09b34a50bc875fd4fa591177Jeff Sharkey if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) continue; 390367ace2f77105f7a09b34a50bc875fd4fa591177Jeff Sharkey if ((c >= '0' && c <= '9') || c == '_' || c == '-' || c == '=') continue; 391367ace2f77105f7a09b34a50bc875fd4fa591177Jeff Sharkey LOG(WARNING) << "Bad suffix character " << c << " in " << packageName; 392367ace2f77105f7a09b34a50bc875fd4fa591177Jeff Sharkey return false; 39394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 39494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 395423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return true; 39694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood} 39794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 3983aee2c5c749dc2589f001b26fae1ec958ec89524Narayan Kamathstatic int _delete_dir_contents(DIR *d, 3993aee2c5c749dc2589f001b26fae1ec958ec89524Narayan Kamath int (*exclusion_predicate)(const char *name, const int is_dir)) 40094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood{ 40194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood int result = 0; 40294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood struct dirent *de; 40394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood int dfd; 40494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 40594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood dfd = dirfd(d); 40694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 40794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (dfd < 0) return -1; 40894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 40994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood while ((de = readdir(d))) { 41094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood const char *name = de->d_name; 41194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 4123aee2c5c749dc2589f001b26fae1ec958ec89524Narayan Kamath /* check using the exclusion predicate, if provided */ 4133aee2c5c749dc2589f001b26fae1ec958ec89524Narayan Kamath if (exclusion_predicate && exclusion_predicate(name, (de->d_type == DT_DIR))) { 4143aee2c5c749dc2589f001b26fae1ec958ec89524Narayan Kamath continue; 4153aee2c5c749dc2589f001b26fae1ec958ec89524Narayan Kamath } 41694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 41794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (de->d_type == DT_DIR) { 41899d9fb15b4a1eb534261ae81b2cf25aec4447bd0Chih-Hung Hsieh int subfd; 41994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood DIR *subdir; 42094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 42194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood /* always skip "." and ".." */ 42294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (name[0] == '.') { 42394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (name[1] == 0) continue; 42494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if ((name[1] == '.') && (name[2] == 0)) continue; 42594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 42694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 4278b7acacc93930df9fa9e1ebea4a4394195b2332eNick Kralevich subfd = openat(dfd, name, O_RDONLY | O_DIRECTORY | O_NOFOLLOW | O_CLOEXEC); 42894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (subfd < 0) { 42994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood ALOGE("Couldn't openat %s: %s\n", name, strerror(errno)); 43094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood result = -1; 43194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood continue; 43294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 43394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood subdir = fdopendir(subfd); 43494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (subdir == NULL) { 43594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood ALOGE("Couldn't fdopendir %s: %s\n", name, strerror(errno)); 43694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood close(subfd); 43794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood result = -1; 43894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood continue; 43994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 4403aee2c5c749dc2589f001b26fae1ec958ec89524Narayan Kamath if (_delete_dir_contents(subdir, exclusion_predicate)) { 44194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood result = -1; 44294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 44394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood closedir(subdir); 44494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (unlinkat(dfd, name, AT_REMOVEDIR) < 0) { 44594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood ALOGE("Couldn't unlinkat %s: %s\n", name, strerror(errno)); 44694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood result = -1; 44794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 44894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } else { 44994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (unlinkat(dfd, name, 0) < 0) { 45094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood ALOGE("Couldn't unlinkat %s: %s\n", name, strerror(errno)); 45194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood result = -1; 45294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 45394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 45494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 45594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 45694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood return result; 45794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood} 45894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 459b06f98aabc5381fd6366526d9b31b5d0345481b6Calin Juravleint delete_dir_contents(const std::string& pathname, bool ignore_if_missing) { 460b06f98aabc5381fd6366526d9b31b5d0345481b6Calin Juravle return delete_dir_contents(pathname.c_str(), 0, NULL, ignore_if_missing); 461ebf728fd43ab5c7d11a1f9e5fdc775d6740fae0aJeff Sharkey} 462ebf728fd43ab5c7d11a1f9e5fdc775d6740fae0aJeff Sharkey 463b06f98aabc5381fd6366526d9b31b5d0345481b6Calin Juravleint delete_dir_contents_and_dir(const std::string& pathname, bool ignore_if_missing) { 464b06f98aabc5381fd6366526d9b31b5d0345481b6Calin Juravle return delete_dir_contents(pathname.c_str(), 1, NULL, ignore_if_missing); 465ebf728fd43ab5c7d11a1f9e5fdc775d6740fae0aJeff Sharkey} 466ebf728fd43ab5c7d11a1f9e5fdc775d6740fae0aJeff Sharkey 46794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwoodint delete_dir_contents(const char *pathname, 46894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood int also_delete_dir, 469b06f98aabc5381fd6366526d9b31b5d0345481b6Calin Juravle int (*exclusion_predicate)(const char*, const int), 470b06f98aabc5381fd6366526d9b31b5d0345481b6Calin Juravle bool ignore_if_missing) 47194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood{ 47294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood int res = 0; 47394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood DIR *d; 47494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 47594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood d = opendir(pathname); 47694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (d == NULL) { 477b06f98aabc5381fd6366526d9b31b5d0345481b6Calin Juravle if (ignore_if_missing && (errno == ENOENT)) { 478b06f98aabc5381fd6366526d9b31b5d0345481b6Calin Juravle return 0; 479b06f98aabc5381fd6366526d9b31b5d0345481b6Calin Juravle } 48094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood ALOGE("Couldn't opendir %s: %s\n", pathname, strerror(errno)); 48194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood return -errno; 48294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 4833aee2c5c749dc2589f001b26fae1ec958ec89524Narayan Kamath res = _delete_dir_contents(d, exclusion_predicate); 48494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood closedir(d); 48594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (also_delete_dir) { 48694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (rmdir(pathname)) { 48794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood ALOGE("Couldn't rmdir %s: %s\n", pathname, strerror(errno)); 48894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood res = -1; 48994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 49094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 49194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood return res; 49294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood} 49394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 49494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwoodint delete_dir_contents_fd(int dfd, const char *name) 49594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood{ 49694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood int fd, res; 49794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood DIR *d; 49894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 4998b7acacc93930df9fa9e1ebea4a4394195b2332eNick Kralevich fd = openat(dfd, name, O_RDONLY | O_DIRECTORY | O_NOFOLLOW | O_CLOEXEC); 50094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (fd < 0) { 50194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood ALOGE("Couldn't openat %s: %s\n", name, strerror(errno)); 50294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood return -1; 50394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 50494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood d = fdopendir(fd); 50594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (d == NULL) { 50694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood ALOGE("Couldn't fdopendir %s: %s\n", name, strerror(errno)); 50794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood close(fd); 50894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood return -1; 50994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 51094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood res = _delete_dir_contents(d, 0); 51194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood closedir(d); 51294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood return res; 51394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood} 51494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 51560fd3feecab4336d964ca8e31c7c3220e1afb558Robin Leestatic int _copy_owner_permissions(int srcfd, int dstfd) 51660fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee{ 51760fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee struct stat st; 51860fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee if (fstat(srcfd, &st) != 0) { 51960fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee return -1; 52060fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee } 52160fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee if (fchmod(dstfd, st.st_mode) != 0) { 52260fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee return -1; 52360fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee } 52460fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee return 0; 52560fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee} 52660fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee 52760fd3feecab4336d964ca8e31c7c3220e1afb558Robin Leestatic int _copy_dir_files(int sdfd, int ddfd, uid_t owner, gid_t group) 52860fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee{ 52960fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee int result = 0; 53060fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee if (_copy_owner_permissions(sdfd, ddfd) != 0) { 53160fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee ALOGE("_copy_dir_files failed to copy dir permissions\n"); 53260fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee } 53360fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee if (fchown(ddfd, owner, group) != 0) { 53460fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee ALOGE("_copy_dir_files failed to change dir owner\n"); 53560fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee } 53660fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee 53760fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee DIR *ds = fdopendir(sdfd); 53860fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee if (ds == NULL) { 53960fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee ALOGE("Couldn't fdopendir: %s\n", strerror(errno)); 54060fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee return -1; 54160fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee } 54260fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee struct dirent *de; 54360fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee while ((de = readdir(ds))) { 54460fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee if (de->d_type != DT_REG) { 54560fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee continue; 54660fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee } 54760fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee 54860fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee const char *name = de->d_name; 54960fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee int fsfd = openat(sdfd, name, O_RDONLY | O_NOFOLLOW | O_CLOEXEC); 55060fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee int fdfd = openat(ddfd, name, O_WRONLY | O_NOFOLLOW | O_CLOEXEC | O_CREAT, 0600); 55160fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee if (fsfd == -1 || fdfd == -1) { 55260fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee ALOGW("Couldn't copy %s: %s\n", name, strerror(errno)); 55360fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee } else { 55460fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee if (_copy_owner_permissions(fsfd, fdfd) != 0) { 55560fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee ALOGE("Failed to change file permissions\n"); 55660fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee } 55760fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee if (fchown(fdfd, owner, group) != 0) { 55860fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee ALOGE("Failed to change file owner\n"); 55960fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee } 56060fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee 56160fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee char buf[8192]; 56260fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee ssize_t size; 56360fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee while ((size = read(fsfd, buf, sizeof(buf))) > 0) { 56460fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee write(fdfd, buf, size); 56560fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee } 56660fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee if (size < 0) { 56760fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee ALOGW("Couldn't copy %s: %s\n", name, strerror(errno)); 56860fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee result = -1; 56960fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee } 57060fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee } 57160fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee close(fdfd); 57260fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee close(fsfd); 57360fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee } 57460fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee 57560fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee return result; 57660fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee} 57760fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee 57860fd3feecab4336d964ca8e31c7c3220e1afb558Robin Leeint copy_dir_files(const char *srcname, 57960fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee const char *dstname, 58060fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee uid_t owner, 58160fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee uid_t group) 58260fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee{ 58360fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee int res = 0; 58460fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee DIR *ds = NULL; 58560fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee DIR *dd = NULL; 58660fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee 58760fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee ds = opendir(srcname); 58860fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee if (ds == NULL) { 58960fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee ALOGE("Couldn't opendir %s: %s\n", srcname, strerror(errno)); 59060fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee return -errno; 59160fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee } 59260fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee 59360fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee mkdir(dstname, 0600); 59460fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee dd = opendir(dstname); 59560fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee if (dd == NULL) { 59660fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee ALOGE("Couldn't opendir %s: %s\n", dstname, strerror(errno)); 59760fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee closedir(ds); 59860fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee return -errno; 59960fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee } 60060fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee 60160fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee int sdfd = dirfd(ds); 60260fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee int ddfd = dirfd(dd); 60360fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee if (sdfd != -1 && ddfd != -1) { 60460fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee res = _copy_dir_files(sdfd, ddfd, owner, group); 60560fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee } else { 60660fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee res = -errno; 60760fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee } 60860fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee closedir(dd); 60960fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee closedir(ds); 61060fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee return res; 61160fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee} 61260fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee 61341ea424413c0381ef2aa15fc5bd5d4b88abd23c4Jeff Sharkeyint64_t data_disk_free(const std::string& data_path) 61494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood{ 61594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood struct statfs sfs; 61641ea424413c0381ef2aa15fc5bd5d4b88abd23c4Jeff Sharkey if (statfs(data_path.c_str(), &sfs) == 0) { 61794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood return sfs.f_bavail * sfs.f_bsize; 61894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } else { 61941ea424413c0381ef2aa15fc5bd5d4b88abd23c4Jeff Sharkey PLOG(ERROR) << "Couldn't statfs " << data_path; 62094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood return -1; 62194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 62294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood} 62394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 62494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwoodcache_t* start_cache_collection() 62594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood{ 62694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood cache_t* cache = (cache_t*)calloc(1, sizeof(cache_t)); 62794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood return cache; 62894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood} 62994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 63094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood#define CACHE_BLOCK_SIZE (512*1024) 63194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 63294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwoodstatic void* _cache_malloc(cache_t* cache, size_t len) 63394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood{ 63494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood len = (len+3)&~3; 63594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (len > (CACHE_BLOCK_SIZE/2)) { 63694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood // It doesn't make sense to try to put this allocation into one 63794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood // of our blocks, because it is so big. Instead, make a new dedicated 63894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood // block for it. 63994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood int8_t* res = (int8_t*)malloc(len+sizeof(void*)); 64094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (res == NULL) { 64194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood return NULL; 64294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 6439a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey CACHE_NOISY(ALOGI("Allocated large cache mem block: %p size %zu", res, len)); 64494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood // Link it into our list of blocks, not disrupting the current one. 64594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (cache->memBlocks == NULL) { 64694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood *(void**)res = NULL; 64794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood cache->memBlocks = res; 64894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } else { 64994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood *(void**)res = *(void**)cache->memBlocks; 65094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood *(void**)cache->memBlocks = res; 65194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 65294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood return res + sizeof(void*); 65394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 65494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood int8_t* res = cache->curMemBlockAvail; 65594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood int8_t* nextPos = res + len; 65694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (cache->memBlocks == NULL || nextPos > cache->curMemBlockEnd) { 65719803807cd7ae01868fcfa50305f4a7dd13765e2Jeff Sharkey int8_t* newBlock = (int8_t*) malloc(CACHE_BLOCK_SIZE); 65894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (newBlock == NULL) { 65994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood return NULL; 66094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 66194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood CACHE_NOISY(ALOGI("Allocated new cache mem block: %p", newBlock)); 66294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood *(void**)newBlock = cache->memBlocks; 66394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood cache->memBlocks = newBlock; 66494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood res = cache->curMemBlockAvail = newBlock + sizeof(void*); 66594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood cache->curMemBlockEnd = newBlock + CACHE_BLOCK_SIZE; 66694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood nextPos = res + len; 66794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 6689a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey CACHE_NOISY(ALOGI("cache_malloc: ret %p size %zu, block=%p, nextPos=%p", 66994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood res, len, cache->memBlocks, nextPos)); 67094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood cache->curMemBlockAvail = nextPos; 67194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood return res; 67294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood} 67394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 67494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwoodstatic void* _cache_realloc(cache_t* cache, void* cur, size_t origLen, size_t len) 67594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood{ 67694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood // This isn't really a realloc, but it is good enough for our purposes here. 67794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood void* alloc = _cache_malloc(cache, len); 67894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (alloc != NULL && cur != NULL) { 67994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood memcpy(alloc, cur, origLen < len ? origLen : len); 68094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 68194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood return alloc; 68294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood} 68394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 68494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwoodstatic void _inc_num_cache_collected(cache_t* cache) 68594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood{ 68694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood cache->numCollected++; 68794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if ((cache->numCollected%20000) == 0) { 68892dc3fc52cf097bd105460cf377779bdcf146d62Mark Salyzyn ALOGI("Collected cache so far: %zd directories, %zd files", 68994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood cache->numDirs, cache->numFiles); 69094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 69194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood} 69294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 69394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwoodstatic cache_dir_t* _add_cache_dir_t(cache_t* cache, cache_dir_t* parent, const char *name) 69494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood{ 69594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood size_t nameLen = strlen(name); 69694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood cache_dir_t* dir = (cache_dir_t*)_cache_malloc(cache, sizeof(cache_dir_t)+nameLen+1); 69794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (dir != NULL) { 69894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood dir->parent = parent; 69994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood dir->childCount = 0; 70094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood dir->hiddenCount = 0; 70194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood dir->deleted = 0; 70294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood strcpy(dir->name, name); 70394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (cache->numDirs >= cache->availDirs) { 70494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood size_t newAvail = cache->availDirs < 1000 ? 1000 : cache->availDirs*2; 70594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood cache_dir_t** newDirs = (cache_dir_t**)_cache_realloc(cache, cache->dirs, 70694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood cache->availDirs*sizeof(cache_dir_t*), newAvail*sizeof(cache_dir_t*)); 70794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (newDirs == NULL) { 70894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood ALOGE("Failure growing cache dirs array for %s\n", name); 70994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood return NULL; 71094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 71194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood cache->availDirs = newAvail; 71294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood cache->dirs = newDirs; 71394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 71494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood cache->dirs[cache->numDirs] = dir; 71594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood cache->numDirs++; 71694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (parent != NULL) { 71794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood parent->childCount++; 71894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 71994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood _inc_num_cache_collected(cache); 72094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } else { 72194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood ALOGE("Failure allocating cache_dir_t for %s\n", name); 72294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 72394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood return dir; 72494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood} 72594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 72694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwoodstatic cache_file_t* _add_cache_file_t(cache_t* cache, cache_dir_t* dir, time_t modTime, 72794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood const char *name) 72894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood{ 72994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood size_t nameLen = strlen(name); 73094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood cache_file_t* file = (cache_file_t*)_cache_malloc(cache, sizeof(cache_file_t)+nameLen+1); 73194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (file != NULL) { 73294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood file->dir = dir; 73394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood file->modTime = modTime; 73494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood strcpy(file->name, name); 73594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (cache->numFiles >= cache->availFiles) { 73694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood size_t newAvail = cache->availFiles < 1000 ? 1000 : cache->availFiles*2; 73794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood cache_file_t** newFiles = (cache_file_t**)_cache_realloc(cache, cache->files, 73894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood cache->availFiles*sizeof(cache_file_t*), newAvail*sizeof(cache_file_t*)); 73994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (newFiles == NULL) { 74094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood ALOGE("Failure growing cache file array for %s\n", name); 74194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood return NULL; 74294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 74394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood cache->availFiles = newAvail; 74494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood cache->files = newFiles; 74594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 7469a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey CACHE_NOISY(ALOGI("Setting file %p at position %zd in array %p", file, 74794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood cache->numFiles, cache->files)); 74894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood cache->files[cache->numFiles] = file; 74994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood cache->numFiles++; 75094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood dir->childCount++; 75194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood _inc_num_cache_collected(cache); 75294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } else { 75394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood ALOGE("Failure allocating cache_file_t for %s\n", name); 75494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 75594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood return file; 75694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood} 75794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 75894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwoodstatic int _add_cache_files(cache_t *cache, cache_dir_t *parentDir, const char *dirName, 75994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood DIR* dir, char *pathBase, char *pathPos, size_t pathAvailLen) 76094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood{ 76194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood struct dirent *de; 76294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood cache_dir_t* cacheDir = NULL; 76394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood int dfd; 76494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 76594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood CACHE_NOISY(ALOGI("_add_cache_files: parent=%p dirName=%s dir=%p pathBase=%s", 76694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood parentDir, dirName, dir, pathBase)); 76794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 76894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood dfd = dirfd(dir); 76994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 77094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (dfd < 0) return 0; 77194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 77294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood // Sub-directories always get added to the data structure, so if they 77394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood // are empty we will know about them to delete them later. 77494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood cacheDir = _add_cache_dir_t(cache, parentDir, dirName); 77594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 77694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood while ((de = readdir(dir))) { 77794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood const char *name = de->d_name; 77894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 77994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (de->d_type == DT_DIR) { 78094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood int subfd; 78194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood DIR *subdir; 78294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 78394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood /* always skip "." and ".." */ 78494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (name[0] == '.') { 78594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (name[1] == 0) continue; 78694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if ((name[1] == '.') && (name[2] == 0)) continue; 78794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 78894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 7898b7acacc93930df9fa9e1ebea4a4394195b2332eNick Kralevich subfd = openat(dfd, name, O_RDONLY | O_DIRECTORY | O_NOFOLLOW | O_CLOEXEC); 79094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (subfd < 0) { 79194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood ALOGE("Couldn't openat %s: %s\n", name, strerror(errno)); 79294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood continue; 79394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 79494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood subdir = fdopendir(subfd); 79594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (subdir == NULL) { 79694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood ALOGE("Couldn't fdopendir %s: %s\n", name, strerror(errno)); 79794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood close(subfd); 79894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood continue; 79994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 80094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (cacheDir == NULL) { 80194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood cacheDir = _add_cache_dir_t(cache, parentDir, dirName); 80294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 80394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (cacheDir != NULL) { 80494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood // Update pathBase for the new path... this may change dirName 80594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood // if that is also pointing to the path, but we are done with it 80694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood // now. 80794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood size_t finallen = snprintf(pathPos, pathAvailLen, "/%s", name); 80894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood CACHE_NOISY(ALOGI("Collecting dir %s\n", pathBase)); 80994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (finallen < pathAvailLen) { 81094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood _add_cache_files(cache, cacheDir, name, subdir, pathBase, 81194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood pathPos+finallen, pathAvailLen-finallen); 81294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } else { 81394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood // Whoops, the final path is too long! We'll just delete 81494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood // this directory. 81594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood ALOGW("Cache dir %s truncated in path %s; deleting dir\n", 81694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood name, pathBase); 81794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood _delete_dir_contents(subdir, NULL); 81894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (unlinkat(dfd, name, AT_REMOVEDIR) < 0) { 81994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood ALOGE("Couldn't unlinkat %s: %s\n", name, strerror(errno)); 82094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 82194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 82294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 82394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood closedir(subdir); 82494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } else if (de->d_type == DT_REG) { 82594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood // Skip files that start with '.'; they will be deleted if 82694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood // their entire directory is deleted. This allows for metadata 82794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood // like ".nomedia" to remain in the directory until the entire 82894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood // directory is deleted. 82994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (cacheDir == NULL) { 83094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood cacheDir = _add_cache_dir_t(cache, parentDir, dirName); 83194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 83294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (name[0] == '.') { 83394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood cacheDir->hiddenCount++; 83494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood continue; 83594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 83694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (cacheDir != NULL) { 83794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood // Build final full path for file... this may change dirName 83894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood // if that is also pointing to the path, but we are done with it 83994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood // now. 84094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood size_t finallen = snprintf(pathPos, pathAvailLen, "/%s", name); 84194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood CACHE_NOISY(ALOGI("Collecting file %s\n", pathBase)); 84294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (finallen < pathAvailLen) { 84394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood struct stat s; 84494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (stat(pathBase, &s) >= 0) { 84594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood _add_cache_file_t(cache, cacheDir, s.st_mtime, name); 84694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } else { 84794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood ALOGW("Unable to stat cache file %s; deleting\n", pathBase); 84894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (unlink(pathBase) < 0) { 84994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood ALOGE("Couldn't unlink %s: %s\n", pathBase, strerror(errno)); 85094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 85194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 85294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } else { 85394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood // Whoops, the final path is too long! We'll just delete 85494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood // this file. 85594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood ALOGW("Cache file %s truncated in path %s; deleting\n", 85694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood name, pathBase); 85794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (unlinkat(dfd, name, 0) < 0) { 85894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood *pathPos = 0; 85994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood ALOGE("Couldn't unlinkat %s in %s: %s\n", name, pathBase, 86094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood strerror(errno)); 86194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 86294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 86394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 86494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } else { 86594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood cacheDir->hiddenCount++; 86694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 86794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 86894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood return 0; 86994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood} 87094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 8719a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkeyint get_path_inode(const std::string& path, ino_t *inode) { 8729a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey struct stat buf; 8739a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey memset(&buf, 0, sizeof(buf)); 8749a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey if (stat(path.c_str(), &buf) != 0) { 8759a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey PLOG(WARNING) << "Failed to stat " << path; 8769a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey return -1; 8779a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey } else { 8789a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey *inode = buf.st_ino; 8799a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey return 0; 8809a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey } 8819a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey} 8829a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey 8839a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey/** 8849a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey * Write the inode of a specific child file into the given xattr on the 8859a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey * parent directory. This allows you to find the child later, even if its 8869a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey * name is encrypted. 8879a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey */ 8889a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkeyint write_path_inode(const std::string& parent, const char* name, const char* inode_xattr) { 8899a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey ino_t inode = 0; 8909a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey uint64_t inode_raw = 0; 8919a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey auto path = StringPrintf("%s/%s", parent.c_str(), name); 8929a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey 8939a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey if (get_path_inode(path, &inode) != 0) { 8949a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey // Path probably doesn't exist yet; ignore 8959a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey return 0; 8969a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey } 8979a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey 8989a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey // Check to see if already set correctly 8999a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey if (getxattr(parent.c_str(), inode_xattr, &inode_raw, sizeof(inode_raw)) == sizeof(inode_raw)) { 9009a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey if (inode_raw == inode) { 9019a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey // Already set correctly; skip writing 9029a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey return 0; 9039a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey } else { 9049a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey PLOG(WARNING) << "Mismatched inode value; found " << inode 9059a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey << " on disk but marked value was " << inode_raw << "; overwriting"; 9069a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey } 9079a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey } 9089a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey 9099a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey inode_raw = inode; 9104ed6507cfb4f0fae8567b42037e74a07f7dd28baJeff Sharkey if (setxattr(parent.c_str(), inode_xattr, &inode_raw, sizeof(inode_raw), 0) != 0 && errno != EOPNOTSUPP) { 9119a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey PLOG(ERROR) << "Failed to write xattr " << inode_xattr << " at " << parent; 9129a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey return -1; 9139a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey } else { 9149a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey return 0; 9159a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey } 9169a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey} 9179a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey 9189a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey/** 9199a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey * Read the inode of a specific child file from the given xattr on the 9209a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey * parent directory. Returns a currently valid path for that child, which 9219a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey * might have an encrypted name. 9229a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey */ 9239a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkeystd::string read_path_inode(const std::string& parent, const char* name, const char* inode_xattr) { 9249a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey ino_t inode = 0; 9259a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey uint64_t inode_raw = 0; 9269a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey auto fallback = StringPrintf("%s/%s", parent.c_str(), name); 9279a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey 9289a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey // Lookup the inode value written earlier 9299a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey if (getxattr(parent.c_str(), inode_xattr, &inode_raw, sizeof(inode_raw)) == sizeof(inode_raw)) { 9309a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey inode = inode_raw; 9319a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey } 9329a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey 9339a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey // For testing purposes, rely on the inode when defined; this could be 9349a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey // optimized to use access() in the future. 9359a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey if (inode != 0) { 9369a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey DIR* dir = opendir(parent.c_str()); 9379a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey if (dir == nullptr) { 9389a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey PLOG(ERROR) << "Failed to opendir " << parent; 9399a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey return fallback; 9409a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey } 9419a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey 9429a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey struct dirent* ent; 9439a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey while ((ent = readdir(dir))) { 9449a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey if (ent->d_ino == inode) { 9459a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey auto resolved = StringPrintf("%s/%s", parent.c_str(), ent->d_name); 9469a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey#if DEBUG_XATTRS 9479a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey if (resolved != fallback) { 9489a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey LOG(DEBUG) << "Resolved path " << resolved << " for inode " << inode 9499a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey << " instead of " << fallback; 9509a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey } 9519a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey#endif 9529a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey closedir(dir); 9539a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey return resolved; 9549a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey } 9559a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey } 9569a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey LOG(WARNING) << "Failed to resolve inode " << inode << "; using " << fallback; 9579a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey closedir(dir); 9589a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey return fallback; 9599a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey } else { 9609a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey return fallback; 9619a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey } 9629a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey} 9639a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey 96454e292e1ea87b504b552393d2e9bc800458bd0c1Jeff Sharkeyvoid add_cache_files(cache_t* cache, const std::string& data_path) { 96594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood DIR *d; 96694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood struct dirent *de; 96794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood char dirname[PATH_MAX]; 96894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 96954e292e1ea87b504b552393d2e9bc800458bd0c1Jeff Sharkey const char* basepath = data_path.c_str(); 97054e292e1ea87b504b552393d2e9bc800458bd0c1Jeff Sharkey CACHE_NOISY(ALOGI("add_cache_files: basepath=%s\n", basepath)); 97194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 97294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood d = opendir(basepath); 97394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (d == NULL) { 97494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood return; 97594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 97694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 97794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood while ((de = readdir(d))) { 97894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (de->d_type == DT_DIR) { 97994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood DIR* subdir; 98094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood const char *name = de->d_name; 98194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 98294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood /* always skip "." and ".." */ 98394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (name[0] == '.') { 98494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (name[1] == 0) continue; 98594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if ((name[1] == '.') && (name[2] == 0)) continue; 98694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 98794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 9889a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey auto parent = StringPrintf("%s/%s", basepath, name); 9899a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey auto resolved = read_path_inode(parent, "cache", kXattrInodeCache); 9909a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey strcpy(dirname, resolved.c_str()); 99194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood CACHE_NOISY(ALOGI("Adding cache files from dir: %s\n", dirname)); 99254e292e1ea87b504b552393d2e9bc800458bd0c1Jeff Sharkey 99394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood subdir = opendir(dirname); 99494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (subdir != NULL) { 99594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood size_t dirnameLen = strlen(dirname); 99694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood _add_cache_files(cache, NULL, dirname, subdir, dirname, dirname+dirnameLen, 99794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood PATH_MAX - dirnameLen); 99894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood closedir(subdir); 99994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 100094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 100194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 100294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 100394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood closedir(d); 100494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood} 100594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 1006f3124d8fab2377d6b5ec2c7cbc775e2ab20df3efFyodor Kupolovvoid add_preloads_file_cache(cache_t* cache, const char* volume_uuid) { 1007f3124d8fab2377d6b5ec2c7cbc775e2ab20df3efFyodor Kupolov char dirname[PATH_MAX]; 1008f3124d8fab2377d6b5ec2c7cbc775e2ab20df3efFyodor Kupolov DIR* subdir; 1009f3124d8fab2377d6b5ec2c7cbc775e2ab20df3efFyodor Kupolov auto cache_path = StringPrintf("%s/preloads/file_cache", create_data_path(volume_uuid).c_str()); 1010f3124d8fab2377d6b5ec2c7cbc775e2ab20df3efFyodor Kupolov strcpy(dirname, cache_path.c_str()); 1011f3124d8fab2377d6b5ec2c7cbc775e2ab20df3efFyodor Kupolov CACHE_NOISY(ALOGI("add_preloads_file_cache: dirname=%s\n", dirname)); 1012f3124d8fab2377d6b5ec2c7cbc775e2ab20df3efFyodor Kupolov subdir = opendir(dirname); 1013f3124d8fab2377d6b5ec2c7cbc775e2ab20df3efFyodor Kupolov if (subdir != NULL) { 1014f3124d8fab2377d6b5ec2c7cbc775e2ab20df3efFyodor Kupolov size_t dirnameLen = strlen(dirname); 1015f3124d8fab2377d6b5ec2c7cbc775e2ab20df3efFyodor Kupolov _add_cache_files(cache, NULL, dirname, subdir, dirname, dirname + dirnameLen, 1016f3124d8fab2377d6b5ec2c7cbc775e2ab20df3efFyodor Kupolov PATH_MAX - dirnameLen); 1017f3124d8fab2377d6b5ec2c7cbc775e2ab20df3efFyodor Kupolov closedir(subdir); 1018f3124d8fab2377d6b5ec2c7cbc775e2ab20df3efFyodor Kupolov } 1019f3124d8fab2377d6b5ec2c7cbc775e2ab20df3efFyodor Kupolov} 1020f3124d8fab2377d6b5ec2c7cbc775e2ab20df3efFyodor Kupolov 102194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwoodstatic char *create_dir_path(char path[PATH_MAX], cache_dir_t* dir) 102294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood{ 102394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood char *pos = path; 102494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (dir->parent != NULL) { 102594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood pos = create_dir_path(path, dir->parent); 102694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 102794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood // Note that we don't need to worry about going beyond the buffer, 102894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood // since when we were constructing the cache entries our maximum 102994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood // buffer size for full paths was PATH_MAX. 103094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood strcpy(pos, dir->name); 103194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood pos += strlen(pos); 103294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood *pos = '/'; 103394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood pos++; 103494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood *pos = 0; 103594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood return pos; 103694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood} 103794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 103894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwoodstatic void delete_cache_dir(char path[PATH_MAX], cache_dir_t* dir) 103994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood{ 104094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (dir->parent != NULL) { 104194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood create_dir_path(path, dir); 104294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood ALOGI("DEL DIR %s\n", path); 104394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (dir->hiddenCount <= 0) { 104494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (rmdir(path)) { 104594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood ALOGE("Couldn't rmdir %s: %s\n", path, strerror(errno)); 104694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood return; 104794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 104894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } else { 104994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood // The directory contains hidden files so we need to delete 105094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood // them along with the directory itself. 105194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (delete_dir_contents(path, 1, NULL)) { 105294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood return; 105394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 105494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 105594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood dir->parent->childCount--; 105694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood dir->deleted = 1; 105794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (dir->parent->childCount <= 0) { 105894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood delete_cache_dir(path, dir->parent); 105994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 106094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } else if (dir->hiddenCount > 0) { 106194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood // This is a root directory, but it has hidden files. Get rid of 106294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood // all of those files, but not the directory itself. 106394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood create_dir_path(path, dir); 106494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood ALOGI("DEL CONTENTS %s\n", path); 106594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood delete_dir_contents(path, 0, NULL); 106694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 106794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood} 106894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 106994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwoodstatic int cache_modtime_sort(const void *lhsP, const void *rhsP) 107094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood{ 107194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood const cache_file_t *lhs = *(const cache_file_t**)lhsP; 107294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood const cache_file_t *rhs = *(const cache_file_t**)rhsP; 107394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood return lhs->modTime < rhs->modTime ? -1 : (lhs->modTime > rhs->modTime ? 1 : 0); 107494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood} 107594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 107641ea424413c0381ef2aa15fc5bd5d4b88abd23c4Jeff Sharkeyvoid clear_cache_files(const std::string& data_path, cache_t* cache, int64_t free_size) 107794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood{ 107894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood size_t i; 107994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood int skip = 0; 108094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood char path[PATH_MAX]; 108194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 108292dc3fc52cf097bd105460cf377779bdcf146d62Mark Salyzyn ALOGI("Collected cache files: %zd directories, %zd files", 108394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood cache->numDirs, cache->numFiles); 108494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 108594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood CACHE_NOISY(ALOGI("Sorting files...")); 108694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood qsort(cache->files, cache->numFiles, sizeof(cache_file_t*), 108794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood cache_modtime_sort); 108894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 108994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood CACHE_NOISY(ALOGI("Cleaning empty directories...")); 109094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood for (i=cache->numDirs; i>0; i--) { 109194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood cache_dir_t* dir = cache->dirs[i-1]; 109294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (dir->childCount <= 0 && !dir->deleted) { 109394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood delete_cache_dir(path, dir); 109494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 109594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 109694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 109794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood CACHE_NOISY(ALOGI("Trimming files...")); 109894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood for (i=0; i<cache->numFiles; i++) { 109994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood skip++; 110094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (skip > 10) { 110141ea424413c0381ef2aa15fc5bd5d4b88abd23c4Jeff Sharkey if (data_disk_free(data_path) > free_size) { 110294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood return; 110394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 110494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood skip = 0; 110594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 110694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood cache_file_t* file = cache->files[i]; 110794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood strcpy(create_dir_path(path, file->dir), file->name); 110894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood ALOGI("DEL (mod %d) %s\n", (int)file->modTime, path); 110994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (unlink(path) < 0) { 111094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood ALOGE("Couldn't unlink %s: %s\n", path, strerror(errno)); 111194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 111294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood file->dir->childCount--; 111394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (file->dir->childCount <= 0) { 111494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood delete_cache_dir(path, file->dir); 111594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 111694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 111794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood} 111894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 111994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwoodvoid finish_cache_collection(cache_t* cache) 112094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood{ 112199d9fb15b4a1eb534261ae81b2cf25aec4447bd0Chih-Hung Hsieh CACHE_NOISY(size_t i;) 112294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 11239a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey CACHE_NOISY(ALOGI("clear_cache_files: %zu dirs, %zu files\n", cache->numDirs, cache->numFiles)); 112494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood CACHE_NOISY( 112594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood for (i=0; i<cache->numDirs; i++) { 112694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood cache_dir_t* dir = cache->dirs[i]; 11279a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey ALOGI("dir #%zu: %p %s parent=%p\n", i, dir, dir->name, dir->parent); 112894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood }) 112994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood CACHE_NOISY( 113094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood for (i=0; i<cache->numFiles; i++) { 113194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood cache_file_t* file = cache->files[i]; 11329a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey ALOGI("file #%zu: %p %s time=%d dir=%p\n", i, file, file->name, 113394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood (int)file->modTime, file->dir); 113494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood }) 113594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood void* block = cache->memBlocks; 113694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood while (block != NULL) { 113794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood void* nextBlock = *(void**)block; 113894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood CACHE_NOISY(ALOGI("Freeing cache mem block: %p", block)); 113994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood free(block); 114094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood block = nextBlock; 114194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 114294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood free(cache); 114394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood} 114494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 114594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood/** 1146c597b6dd895dbb2b28c757ce7a2651b3cdc9b00cCalin Juravle * Validate that the path is valid in the context of the provided directory. 1147c597b6dd895dbb2b28c757ce7a2651b3cdc9b00cCalin Juravle * The path is allowed to have at most one subdirectory and no indirections 1148c597b6dd895dbb2b28c757ce7a2651b3cdc9b00cCalin Juravle * to top level directories (i.e. have ".."). 1149c597b6dd895dbb2b28c757ce7a2651b3cdc9b00cCalin Juravle */ 1150e23a13299a4f6f2488935b4786cdbb46f46e3d3cJeff Sharkeystatic int validate_path(const dir_rec_t* dir, const char* path, int maxSubdirs) { 1151c597b6dd895dbb2b28c757ce7a2651b3cdc9b00cCalin Juravle size_t dir_len = dir->len; 1152c597b6dd895dbb2b28c757ce7a2651b3cdc9b00cCalin Juravle const char* subdir = strchr(path + dir_len, '/'); 1153c597b6dd895dbb2b28c757ce7a2651b3cdc9b00cCalin Juravle 1154c597b6dd895dbb2b28c757ce7a2651b3cdc9b00cCalin Juravle // Only allow the path to have at most one subdirectory. 1155c597b6dd895dbb2b28c757ce7a2651b3cdc9b00cCalin Juravle if (subdir != NULL) { 1156c597b6dd895dbb2b28c757ce7a2651b3cdc9b00cCalin Juravle ++subdir; 1157e23a13299a4f6f2488935b4786cdbb46f46e3d3cJeff Sharkey if ((--maxSubdirs == 0) && strchr(subdir, '/') != NULL) { 1158c597b6dd895dbb2b28c757ce7a2651b3cdc9b00cCalin Juravle ALOGE("invalid apk path '%s' (subdir?)\n", path); 1159c597b6dd895dbb2b28c757ce7a2651b3cdc9b00cCalin Juravle return -1; 1160c597b6dd895dbb2b28c757ce7a2651b3cdc9b00cCalin Juravle } 1161c597b6dd895dbb2b28c757ce7a2651b3cdc9b00cCalin Juravle } 1162c597b6dd895dbb2b28c757ce7a2651b3cdc9b00cCalin Juravle 1163c597b6dd895dbb2b28c757ce7a2651b3cdc9b00cCalin Juravle // Directories can't have a period directly after the directory markers to prevent "..". 1164c597b6dd895dbb2b28c757ce7a2651b3cdc9b00cCalin Juravle if ((path[dir_len] == '.') || ((subdir != NULL) && (*subdir == '.'))) { 1165c597b6dd895dbb2b28c757ce7a2651b3cdc9b00cCalin Juravle ALOGE("invalid apk path '%s' (trickery)\n", path); 1166c597b6dd895dbb2b28c757ce7a2651b3cdc9b00cCalin Juravle return -1; 1167c597b6dd895dbb2b28c757ce7a2651b3cdc9b00cCalin Juravle } 1168c597b6dd895dbb2b28c757ce7a2651b3cdc9b00cCalin Juravle 1169c597b6dd895dbb2b28c757ce7a2651b3cdc9b00cCalin Juravle return 0; 1170c597b6dd895dbb2b28c757ce7a2651b3cdc9b00cCalin Juravle} 1171c597b6dd895dbb2b28c757ce7a2651b3cdc9b00cCalin Juravle 1172c597b6dd895dbb2b28c757ce7a2651b3cdc9b00cCalin Juravle/** 117394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood * Checks whether a path points to a system app (.apk file). Returns 0 117494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood * if it is a system app or -1 if it is not. 117594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood */ 117694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwoodint validate_system_app_path(const char* path) { 117794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood size_t i; 117894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 117994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood for (i = 0; i < android_system_dirs.count; i++) { 118094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood const size_t dir_len = android_system_dirs.dirs[i].len; 118194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (!strncmp(path, android_system_dirs.dirs[i].path, dir_len)) { 1182e23a13299a4f6f2488935b4786cdbb46f46e3d3cJeff Sharkey return validate_path(android_system_dirs.dirs + i, path, 1); 118394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 118494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 118594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 118694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood return -1; 118794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood} 118894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 118942451c029b0e87990e5833daea2286bb12c21df5Calin Juravlebool validate_secondary_dex_path(const char* pkgname, const char* path, 119042451c029b0e87990e5833daea2286bb12c21df5Calin Juravle const char* volume_uuid, int uid, int storage_flag) { 119142451c029b0e87990e5833daea2286bb12c21df5Calin Juravle CHECK(storage_flag == FLAG_STORAGE_CE || storage_flag == FLAG_STORAGE_DE); 119242451c029b0e87990e5833daea2286bb12c21df5Calin Juravle 119342451c029b0e87990e5833daea2286bb12c21df5Calin Juravle std::string app_private_dir = storage_flag == FLAG_STORAGE_CE 119442451c029b0e87990e5833daea2286bb12c21df5Calin Juravle ? create_data_user_ce_package_path(volume_uuid, multiuser_get_user_id(uid), pkgname) 119542451c029b0e87990e5833daea2286bb12c21df5Calin Juravle : create_data_user_de_package_path(volume_uuid, multiuser_get_user_id(uid), pkgname); 119642451c029b0e87990e5833daea2286bb12c21df5Calin Juravle dir_rec_t dir; 119742451c029b0e87990e5833daea2286bb12c21df5Calin Juravle if (get_path_from_string(&dir, app_private_dir.c_str()) != 0) { 119842451c029b0e87990e5833daea2286bb12c21df5Calin Juravle LOG(WARNING) << "Could not get dir rec for " << app_private_dir; 119942451c029b0e87990e5833daea2286bb12c21df5Calin Juravle return false; 120042451c029b0e87990e5833daea2286bb12c21df5Calin Juravle } 120142451c029b0e87990e5833daea2286bb12c21df5Calin Juravle // Usually secondary dex files have a nested directory structure. 120242451c029b0e87990e5833daea2286bb12c21df5Calin Juravle // Pick at most 10 subdirectories when validating (arbitrary value). 120342451c029b0e87990e5833daea2286bb12c21df5Calin Juravle // If the secondary dex file is >10 directory nested then validation will 120442451c029b0e87990e5833daea2286bb12c21df5Calin Juravle // fail and the file will not be compiled. 120542451c029b0e87990e5833daea2286bb12c21df5Calin Juravle return validate_path(&dir, path, /*max_subdirs*/ 10) == 0; 120642451c029b0e87990e5833daea2286bb12c21df5Calin Juravle} 120742451c029b0e87990e5833daea2286bb12c21df5Calin Juravle 120894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood/** 120994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood * Get the contents of a environment variable that contains a path. Caller 121094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood * owns the string that is inserted into the directory record. Returns 121194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood * 0 on success and -1 on error. 121294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood */ 121394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwoodint get_path_from_env(dir_rec_t* rec, const char* var) { 121494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood const char* path = getenv(var); 121594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood int ret = get_path_from_string(rec, path); 121694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (ret < 0) { 121794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood ALOGW("Problem finding value for environment variable %s\n", var); 121894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 121994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood return ret; 122094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood} 122194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 122294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood/** 122394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood * Puts the string into the record as a directory. Appends '/' to the end 122494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood * of all paths. Caller owns the string that is inserted into the directory 122594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood * record. A null value will result in an error. 122694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood * 122794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood * Returns 0 on success and -1 on error. 122894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood */ 122994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwoodint get_path_from_string(dir_rec_t* rec, const char* path) { 123094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (path == NULL) { 123194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood return -1; 123294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } else { 123394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood const size_t path_len = strlen(path); 123494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (path_len <= 0) { 123594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood return -1; 123694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 123794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 123894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood // Make sure path is absolute. 123994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (path[0] != '/') { 124094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood return -1; 124194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 124294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 124394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (path[path_len - 1] == '/') { 124494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood // Path ends with a forward slash. Make our own copy. 124594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 124694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood rec->path = strdup(path); 124794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (rec->path == NULL) { 124894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood return -1; 124994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 125094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 125194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood rec->len = path_len; 125294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } else { 125394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood // Path does not end with a slash. Generate a new string. 125494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood char *dst; 125594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 125694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood // Add space for slash and terminating null. 125794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood size_t dst_size = path_len + 2; 125894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 125919803807cd7ae01868fcfa50305f4a7dd13765e2Jeff Sharkey rec->path = (char*) malloc(dst_size); 126094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (rec->path == NULL) { 126194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood return -1; 126294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 126394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 126494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood dst = rec->path; 126594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 126694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (append_and_increment(&dst, path, &dst_size) < 0 126794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood || append_and_increment(&dst, "/", &dst_size)) { 126894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood ALOGE("Error canonicalizing path"); 126994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood return -1; 127094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 127194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 127294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood rec->len = dst - rec->path; 127394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 127494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 127594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood return 0; 127694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood} 127794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 127894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwoodint copy_and_append(dir_rec_t* dst, const dir_rec_t* src, const char* suffix) { 127994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood dst->len = src->len + strlen(suffix); 128094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood const size_t dstSize = dst->len + 1; 128194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood dst->path = (char*) malloc(dstSize); 128294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 128394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (dst->path == NULL 128494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood || snprintf(dst->path, dstSize, "%s%s", src->path, suffix) 128594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood != (ssize_t) dst->len) { 128694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood ALOGE("Could not allocate memory to hold appended path; aborting\n"); 128794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood return -1; 128894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 128994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 129094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood return 0; 129194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood} 129294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 129394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood/** 1294d845c96128a40ca5802c0840ae190fa0af7d4735Narayan Kamath * Check whether path points to a valid path for an APK file. The path must 1295d845c96128a40ca5802c0840ae190fa0af7d4735Narayan Kamath * begin with a whitelisted prefix path and must be no deeper than |maxSubdirs| within 1296d845c96128a40ca5802c0840ae190fa0af7d4735Narayan Kamath * that path. Returns -1 when an invalid path is encountered and 0 when a valid path 1297d845c96128a40ca5802c0840ae190fa0af7d4735Narayan Kamath * is encountered. 129894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood */ 1299d845c96128a40ca5802c0840ae190fa0af7d4735Narayan Kamathstatic int validate_apk_path_internal(const char *path, int maxSubdirs) { 1300c597b6dd895dbb2b28c757ce7a2651b3cdc9b00cCalin Juravle const dir_rec_t* dir = NULL; 130194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (!strncmp(path, android_app_dir.path, android_app_dir.len)) { 1302c597b6dd895dbb2b28c757ce7a2651b3cdc9b00cCalin Juravle dir = &android_app_dir; 130394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } else if (!strncmp(path, android_app_private_dir.path, android_app_private_dir.len)) { 1304c597b6dd895dbb2b28c757ce7a2651b3cdc9b00cCalin Juravle dir = &android_app_private_dir; 13055c1a910e6a2d2c42002dc6ed88ff770336afcb3fTodd Kennedy } else if (!strncmp(path, android_app_ephemeral_dir.path, android_app_ephemeral_dir.len)) { 13065c1a910e6a2d2c42002dc6ed88ff770336afcb3fTodd Kennedy dir = &android_app_ephemeral_dir; 130794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } else if (!strncmp(path, android_asec_dir.path, android_asec_dir.len)) { 1308c597b6dd895dbb2b28c757ce7a2651b3cdc9b00cCalin Juravle dir = &android_asec_dir; 1309e23a13299a4f6f2488935b4786cdbb46f46e3d3cJeff Sharkey } else if (!strncmp(path, android_mnt_expand_dir.path, android_mnt_expand_dir.len)) { 1310e23a13299a4f6f2488935b4786cdbb46f46e3d3cJeff Sharkey dir = &android_mnt_expand_dir; 1311d845c96128a40ca5802c0840ae190fa0af7d4735Narayan Kamath if (maxSubdirs < 2) { 1312d845c96128a40ca5802c0840ae190fa0af7d4735Narayan Kamath maxSubdirs = 2; 1313d845c96128a40ca5802c0840ae190fa0af7d4735Narayan Kamath } 131494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } else { 131594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood return -1; 131694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 131794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 1318e23a13299a4f6f2488935b4786cdbb46f46e3d3cJeff Sharkey return validate_path(dir, path, maxSubdirs); 131994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood} 132094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 1321d845c96128a40ca5802c0840ae190fa0af7d4735Narayan Kamathint validate_apk_path(const char* path) { 1322d845c96128a40ca5802c0840ae190fa0af7d4735Narayan Kamath return validate_apk_path_internal(path, 1 /* maxSubdirs */); 1323d845c96128a40ca5802c0840ae190fa0af7d4735Narayan Kamath} 1324d845c96128a40ca5802c0840ae190fa0af7d4735Narayan Kamath 1325d845c96128a40ca5802c0840ae190fa0af7d4735Narayan Kamathint validate_apk_path_subdirs(const char* path) { 1326d845c96128a40ca5802c0840ae190fa0af7d4735Narayan Kamath return validate_apk_path_internal(path, 3 /* maxSubdirs */); 1327d845c96128a40ca5802c0840ae190fa0af7d4735Narayan Kamath} 1328d845c96128a40ca5802c0840ae190fa0af7d4735Narayan Kamath 132994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwoodint append_and_increment(char** dst, const char* src, size_t* dst_size) { 133094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood ssize_t ret = strlcpy(*dst, src, *dst_size); 133194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (ret < 0 || (size_t) ret >= *dst_size) { 133294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood return -1; 133394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 133494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood *dst += ret; 133594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood *dst_size -= ret; 133694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood return 0; 133794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood} 133894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 133919803807cd7ae01868fcfa50305f4a7dd13765e2Jeff Sharkeychar *build_string2(const char *s1, const char *s2) { 134094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (s1 == NULL || s2 == NULL) return NULL; 134194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 134294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood int len_s1 = strlen(s1); 134394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood int len_s2 = strlen(s2); 134494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood int len = len_s1 + len_s2 + 1; 134519803807cd7ae01868fcfa50305f4a7dd13765e2Jeff Sharkey char *result = (char *) malloc(len); 134694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (result == NULL) return NULL; 134794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 134894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood strcpy(result, s1); 134994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood strcpy(result + len_s1, s2); 135094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 135194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood return result; 135294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood} 135394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 135419803807cd7ae01868fcfa50305f4a7dd13765e2Jeff Sharkeychar *build_string3(const char *s1, const char *s2, const char *s3) { 135594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (s1 == NULL || s2 == NULL || s3 == NULL) return NULL; 135694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 135794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood int len_s1 = strlen(s1); 135894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood int len_s2 = strlen(s2); 135994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood int len_s3 = strlen(s3); 136094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood int len = len_s1 + len_s2 + len_s3 + 1; 136119803807cd7ae01868fcfa50305f4a7dd13765e2Jeff Sharkey char *result = (char *) malloc(len); 136294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (result == NULL) return NULL; 136394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 136494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood strcpy(result, s1); 136594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood strcpy(result + len_s1, s2); 136694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood strcpy(result + len_s1 + len_s2, s3); 136794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 136894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood return result; 136994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood} 137094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 1371095c763dd9aa26a206d10ab7c1d7e1c569298fb3Robin Leeint ensure_config_user_dirs(userid_t userid) { 1372095c763dd9aa26a206d10ab7c1d7e1c569298fb3Robin Lee // writable by system, readable by any app within the same user 137360fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee const int uid = multiuser_get_uid(userid, AID_SYSTEM); 137460fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee const int gid = multiuser_get_uid(userid, AID_EVERYBODY); 1375095c763dd9aa26a206d10ab7c1d7e1c569298fb3Robin Lee 1376095c763dd9aa26a206d10ab7c1d7e1c569298fb3Robin Lee // Ensure /data/misc/user/<userid> exists 1377379a12b0072b322c7f86e690a8e8a220e500861cJeff Sharkey auto path = create_data_misc_legacy_path(userid); 1378379a12b0072b322c7f86e690a8e8a220e500861cJeff Sharkey return fs_prepare_dir(path.c_str(), 0750, uid, gid); 1379095c763dd9aa26a206d10ab7c1d7e1c569298fb3Robin Lee} 138002d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe 138102d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampeint wait_child(pid_t pid) 138202d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe{ 138302d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe int status; 138402d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe pid_t got_pid; 138502d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe 138602d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe while (1) { 138702d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe got_pid = waitpid(pid, &status, 0); 138802d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe if (got_pid == -1 && errno == EINTR) { 138902d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe printf("waitpid interrupted, retrying\n"); 139002d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe } else { 139102d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe break; 139202d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe } 139302d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe } 139402d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe if (got_pid != pid) { 139502d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe ALOGW("waitpid failed: wanted %d, got %d: %s\n", 139602d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe (int) pid, (int) got_pid, strerror(errno)); 139702d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe return 1; 139802d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe } 139902d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe 140002d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe if (WIFEXITED(status) && WEXITSTATUS(status) == 0) { 140102d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe return 0; 140202d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe } else { 140302d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe return status; /* always nonzero */ 140402d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe } 140502d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe} 140602d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe 140742451c029b0e87990e5833daea2286bb12c21df5Calin Juravle/** 140842451c029b0e87990e5833daea2286bb12c21df5Calin Juravle * Prepare an app cache directory, which offers to fix-up the GID and 140942451c029b0e87990e5833daea2286bb12c21df5Calin Juravle * directory mode flags during a platform upgrade. 141042451c029b0e87990e5833daea2286bb12c21df5Calin Juravle * The app cache directory path will be 'parent'/'name'. 141142451c029b0e87990e5833daea2286bb12c21df5Calin Juravle */ 141242451c029b0e87990e5833daea2286bb12c21df5Calin Juravleint prepare_app_cache_dir(const std::string& parent, const char* name, mode_t target_mode, 141342451c029b0e87990e5833daea2286bb12c21df5Calin Juravle uid_t uid, gid_t gid) { 141442451c029b0e87990e5833daea2286bb12c21df5Calin Juravle auto path = StringPrintf("%s/%s", parent.c_str(), name); 141542451c029b0e87990e5833daea2286bb12c21df5Calin Juravle struct stat st; 141642451c029b0e87990e5833daea2286bb12c21df5Calin Juravle if (stat(path.c_str(), &st) != 0) { 141742451c029b0e87990e5833daea2286bb12c21df5Calin Juravle if (errno == ENOENT) { 141842451c029b0e87990e5833daea2286bb12c21df5Calin Juravle // This is fine, just create it 141942451c029b0e87990e5833daea2286bb12c21df5Calin Juravle if (fs_prepare_dir_strict(path.c_str(), target_mode, uid, gid) != 0) { 142042451c029b0e87990e5833daea2286bb12c21df5Calin Juravle PLOG(ERROR) << "Failed to prepare " << path; 142142451c029b0e87990e5833daea2286bb12c21df5Calin Juravle return -1; 142242451c029b0e87990e5833daea2286bb12c21df5Calin Juravle } else { 142342451c029b0e87990e5833daea2286bb12c21df5Calin Juravle return 0; 142442451c029b0e87990e5833daea2286bb12c21df5Calin Juravle } 142542451c029b0e87990e5833daea2286bb12c21df5Calin Juravle } else { 142642451c029b0e87990e5833daea2286bb12c21df5Calin Juravle PLOG(ERROR) << "Failed to stat " << path; 142742451c029b0e87990e5833daea2286bb12c21df5Calin Juravle return -1; 142842451c029b0e87990e5833daea2286bb12c21df5Calin Juravle } 142942451c029b0e87990e5833daea2286bb12c21df5Calin Juravle } 143042451c029b0e87990e5833daea2286bb12c21df5Calin Juravle 143142451c029b0e87990e5833daea2286bb12c21df5Calin Juravle mode_t actual_mode = st.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO | S_ISGID); 143242451c029b0e87990e5833daea2286bb12c21df5Calin Juravle if (st.st_uid != uid) { 143342451c029b0e87990e5833daea2286bb12c21df5Calin Juravle // Mismatched UID is real trouble; we can't recover 143442451c029b0e87990e5833daea2286bb12c21df5Calin Juravle LOG(ERROR) << "Mismatched UID at " << path << ": found " << st.st_uid 143542451c029b0e87990e5833daea2286bb12c21df5Calin Juravle << " but expected " << uid; 143642451c029b0e87990e5833daea2286bb12c21df5Calin Juravle return -1; 143742451c029b0e87990e5833daea2286bb12c21df5Calin Juravle } else if (st.st_gid == gid && actual_mode == target_mode) { 143842451c029b0e87990e5833daea2286bb12c21df5Calin Juravle // Everything looks good! 143942451c029b0e87990e5833daea2286bb12c21df5Calin Juravle return 0; 144042451c029b0e87990e5833daea2286bb12c21df5Calin Juravle } 144142451c029b0e87990e5833daea2286bb12c21df5Calin Juravle 144242451c029b0e87990e5833daea2286bb12c21df5Calin Juravle // Directory is owned correctly, but GID or mode mismatch means it's 144342451c029b0e87990e5833daea2286bb12c21df5Calin Juravle // probably a platform upgrade so we need to fix them 144442451c029b0e87990e5833daea2286bb12c21df5Calin Juravle FTS *fts; 144542451c029b0e87990e5833daea2286bb12c21df5Calin Juravle FTSENT *p; 144642451c029b0e87990e5833daea2286bb12c21df5Calin Juravle char *argv[] = { (char*) path.c_str(), nullptr }; 144742451c029b0e87990e5833daea2286bb12c21df5Calin Juravle if (!(fts = fts_open(argv, FTS_PHYSICAL | FTS_XDEV, NULL))) { 144842451c029b0e87990e5833daea2286bb12c21df5Calin Juravle PLOG(ERROR) << "Failed to fts_open " << path; 144942451c029b0e87990e5833daea2286bb12c21df5Calin Juravle return -1; 145042451c029b0e87990e5833daea2286bb12c21df5Calin Juravle } 145142451c029b0e87990e5833daea2286bb12c21df5Calin Juravle while ((p = fts_read(fts)) != NULL) { 145242451c029b0e87990e5833daea2286bb12c21df5Calin Juravle switch (p->fts_info) { 145342451c029b0e87990e5833daea2286bb12c21df5Calin Juravle case FTS_DP: 145442451c029b0e87990e5833daea2286bb12c21df5Calin Juravle if (chmod(p->fts_accpath, target_mode) != 0) { 145542451c029b0e87990e5833daea2286bb12c21df5Calin Juravle PLOG(WARNING) << "Failed to chmod " << p->fts_path; 145642451c029b0e87990e5833daea2286bb12c21df5Calin Juravle } 145742451c029b0e87990e5833daea2286bb12c21df5Calin Juravle // Intentional fall through to also set GID 145842451c029b0e87990e5833daea2286bb12c21df5Calin Juravle case FTS_F: 145942451c029b0e87990e5833daea2286bb12c21df5Calin Juravle if (chown(p->fts_accpath, -1, gid) != 0) { 146042451c029b0e87990e5833daea2286bb12c21df5Calin Juravle PLOG(WARNING) << "Failed to chown " << p->fts_path; 146142451c029b0e87990e5833daea2286bb12c21df5Calin Juravle } 146242451c029b0e87990e5833daea2286bb12c21df5Calin Juravle break; 146342451c029b0e87990e5833daea2286bb12c21df5Calin Juravle case FTS_SL: 146442451c029b0e87990e5833daea2286bb12c21df5Calin Juravle case FTS_SLNONE: 146542451c029b0e87990e5833daea2286bb12c21df5Calin Juravle if (lchown(p->fts_accpath, -1, gid) != 0) { 146642451c029b0e87990e5833daea2286bb12c21df5Calin Juravle PLOG(WARNING) << "Failed to chown " << p->fts_path; 146742451c029b0e87990e5833daea2286bb12c21df5Calin Juravle } 146842451c029b0e87990e5833daea2286bb12c21df5Calin Juravle break; 146942451c029b0e87990e5833daea2286bb12c21df5Calin Juravle } 147042451c029b0e87990e5833daea2286bb12c21df5Calin Juravle } 147142451c029b0e87990e5833daea2286bb12c21df5Calin Juravle fts_close(fts); 147242451c029b0e87990e5833daea2286bb12c21df5Calin Juravle return 0; 147342451c029b0e87990e5833daea2286bb12c21df5Calin Juravle} 147442451c029b0e87990e5833daea2286bb12c21df5Calin Juravle 147502d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe} // namespace installd 147602d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe} // namespace android 1477