utils.cpp revision 1d992f9f7886f98e46f98430d6c1d061cc31fdb3
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>
2102d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe#include <stdlib.h>
2202d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe#include <sys/stat.h>
2302d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe#include <sys/wait.h>
2402d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe
2502d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe#if defined(__APPLE__)
2602d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe#include <sys/mount.h>
2702d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe#else
2802d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe#include <sys/statfs.h>
2902d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe#endif
3094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
31e4ec9eb7b4c452493589983970ba5ccc501728d1Elliott Hughes#include <android-base/logging.h>
3202d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe#include <android-base/stringprintf.h>
3302d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe#include <cutils/fs.h>
3402d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe#include <cutils/log.h>
3502d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe#include <private/android_filesystem_config.h>
3602d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe
3702d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe#include "globals.h"  // extern variables.
3894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
3902d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe#ifndef LOG_TAG
4002d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe#define LOG_TAG "installd"
4102d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe#endif
42c03de09173f98506e73e7cf7df21fe11795d4b24Jeff Sharkey#define CACHE_NOISY(x) //x
4394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
44c03de09173f98506e73e7cf7df21fe11795d4b24Jeff Sharkeyusing android::base::StringPrintf;
4594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
4602d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampenamespace android {
4702d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampenamespace installd {
4802d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe
4994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood/**
50c03de09173f98506e73e7cf7df21fe11795d4b24Jeff Sharkey * Check that given string is valid filename, and that it attempts no
51c03de09173f98506e73e7cf7df21fe11795d4b24Jeff Sharkey * parent or child directory traversal.
5294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood */
53c03de09173f98506e73e7cf7df21fe11795d4b24Jeff Sharkeystatic bool is_valid_filename(const std::string& name) {
54c03de09173f98506e73e7cf7df21fe11795d4b24Jeff Sharkey    if (name.empty() || (name == ".") || (name == "..")
55c03de09173f98506e73e7cf7df21fe11795d4b24Jeff Sharkey            || (name.find('/') != std::string::npos)) {
56c03de09173f98506e73e7cf7df21fe11795d4b24Jeff Sharkey        return false;
5794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    } else {
58c03de09173f98506e73e7cf7df21fe11795d4b24Jeff Sharkey        return true;
5994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
60c03de09173f98506e73e7cf7df21fe11795d4b24Jeff Sharkey}
6194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
626a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravlestatic void check_package_name(const char* package_name) {
636a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle    CHECK(is_valid_filename(package_name));
646a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle    CHECK(is_valid_package_name(package_name) == 0);
656a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle}
666a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle
67c03de09173f98506e73e7cf7df21fe11795d4b24Jeff Sharkey/**
68d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey * Create the path name where package app contents should be stored for
69d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey * the given volume UUID and package name.  An empty UUID is assumed to
70d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey * be internal storage.
71d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey */
72d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkeystd::string create_data_app_package_path(const char* volume_uuid,
73d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey        const char* package_name) {
746a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle    check_package_name(package_name);
75d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey    return StringPrintf("%s/%s",
76d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey            create_data_app_path(volume_uuid).c_str(), package_name);
77d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey}
78d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey
79d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey/**
80c03de09173f98506e73e7cf7df21fe11795d4b24Jeff Sharkey * Create the path name where package data should be stored for the given
81c03de09173f98506e73e7cf7df21fe11795d4b24Jeff Sharkey * volume UUID, package name, and user ID. An empty UUID is assumed to be
82c03de09173f98506e73e7cf7df21fe11795d4b24Jeff Sharkey * internal storage.
83c03de09173f98506e73e7cf7df21fe11795d4b24Jeff Sharkey */
842f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkeystd::string create_data_user_ce_package_path(const char* volume_uuid,
85d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey        userid_t user, const char* package_name) {
866a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle    check_package_name(package_name);
87d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey    return StringPrintf("%s/%s",
882f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey            create_data_user_ce_path(volume_uuid, user).c_str(), package_name);
892f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey}
902f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey
912f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkeystd::string create_data_user_ce_package_path(const char* volume_uuid, userid_t user,
922f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey        const char* package_name, ino_t ce_data_inode) {
932f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey    // For testing purposes, rely on the inode when defined; this could be
942f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey    // optimized to use access() in the future.
952f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey    auto fallback = create_data_user_ce_package_path(volume_uuid, user, package_name);
962f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey    if (ce_data_inode != 0) {
972f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey        auto user_path = create_data_user_ce_path(volume_uuid, user);
982f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey        DIR* dir = opendir(user_path.c_str());
992f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey        if (dir == nullptr) {
1002f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey            PLOG(ERROR) << "Failed to opendir " << user_path;
1012f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey            return fallback;
1022f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey        }
1032f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey
1042f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey        struct dirent* ent;
1052f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey        while ((ent = readdir(dir))) {
1062f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey            if (ent->d_ino == ce_data_inode) {
1071d992f9f7886f98e46f98430d6c1d061cc31fdb3Jeff Sharkey                auto resolved = StringPrintf("%s/%s", user_path.c_str(), ent->d_name);
1081d992f9f7886f98e46f98430d6c1d061cc31fdb3Jeff Sharkey                if (resolved != fallback) {
1091d992f9f7886f98e46f98430d6c1d061cc31fdb3Jeff Sharkey                    LOG(DEBUG) << "Resolved path " << resolved << " for inode " << ce_data_inode
1101d992f9f7886f98e46f98430d6c1d061cc31fdb3Jeff Sharkey                            << " instead of " << fallback;
1111d992f9f7886f98e46f98430d6c1d061cc31fdb3Jeff Sharkey                }
1122f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey                closedir(dir);
1131d992f9f7886f98e46f98430d6c1d061cc31fdb3Jeff Sharkey                return resolved;
1142f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey            }
1152f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey        }
1161d992f9f7886f98e46f98430d6c1d061cc31fdb3Jeff Sharkey        LOG(WARNING) << "Failed to resolve inode " << ce_data_inode << "; using " << fallback;
1172f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey        closedir(dir);
1182f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey        return fallback;
1192f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey    } else {
1202f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey        return fallback;
1212f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey    }
122c03de09173f98506e73e7cf7df21fe11795d4b24Jeff Sharkey}
12394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
12463ec2d64196144b2d15d2baffedccfa011d6494fJeff Sharkeystd::string create_data_user_de_package_path(const char* volume_uuid,
12563ec2d64196144b2d15d2baffedccfa011d6494fJeff Sharkey        userid_t user, const char* package_name) {
1266a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle    check_package_name(package_name);
12763ec2d64196144b2d15d2baffedccfa011d6494fJeff Sharkey    return StringPrintf("%s/%s",
12863ec2d64196144b2d15d2baffedccfa011d6494fJeff Sharkey            create_data_user_de_path(volume_uuid, user).c_str(), package_name);
12963ec2d64196144b2d15d2baffedccfa011d6494fJeff Sharkey}
13063ec2d64196144b2d15d2baffedccfa011d6494fJeff Sharkey
131c03de09173f98506e73e7cf7df21fe11795d4b24Jeff Sharkeyint create_pkg_path(char path[PKG_PATH_MAX], const char *pkgname,
132c03de09173f98506e73e7cf7df21fe11795d4b24Jeff Sharkey        const char *postfix, userid_t userid) {
133c03de09173f98506e73e7cf7df21fe11795d4b24Jeff Sharkey    if (is_valid_package_name(pkgname) != 0) {
134c03de09173f98506e73e7cf7df21fe11795d4b24Jeff Sharkey        path[0] = '\0';
13594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        return -1;
13694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
13794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
1382f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey    std::string _tmp(create_data_user_ce_package_path(nullptr, userid, pkgname) + postfix);
139c03de09173f98506e73e7cf7df21fe11795d4b24Jeff Sharkey    const char* tmp = _tmp.c_str();
140c03de09173f98506e73e7cf7df21fe11795d4b24Jeff Sharkey    if (strlen(tmp) >= PKG_PATH_MAX) {
141c03de09173f98506e73e7cf7df21fe11795d4b24Jeff Sharkey        path[0] = '\0';
142c03de09173f98506e73e7cf7df21fe11795d4b24Jeff Sharkey        return -1;
143c03de09173f98506e73e7cf7df21fe11795d4b24Jeff Sharkey    } else {
144c03de09173f98506e73e7cf7df21fe11795d4b24Jeff Sharkey        strcpy(path, tmp);
145c03de09173f98506e73e7cf7df21fe11795d4b24Jeff Sharkey        return 0;
14694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
14794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
14894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
14941ea424413c0381ef2aa15fc5bd5d4b88abd23c4Jeff Sharkeystd::string create_data_path(const char* volume_uuid) {
15041ea424413c0381ef2aa15fc5bd5d4b88abd23c4Jeff Sharkey    if (volume_uuid == nullptr) {
15141ea424413c0381ef2aa15fc5bd5d4b88abd23c4Jeff Sharkey        return "/data";
15294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    } else {
15341ea424413c0381ef2aa15fc5bd5d4b88abd23c4Jeff Sharkey        CHECK(is_valid_filename(volume_uuid));
15441ea424413c0381ef2aa15fc5bd5d4b88abd23c4Jeff Sharkey        return StringPrintf("/mnt/expand/%s", volume_uuid);
15594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
15641ea424413c0381ef2aa15fc5bd5d4b88abd23c4Jeff Sharkey}
15794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
15841ea424413c0381ef2aa15fc5bd5d4b88abd23c4Jeff Sharkey/**
159d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey * Create the path name for app data.
160d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey */
161d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkeystd::string create_data_app_path(const char* volume_uuid) {
162d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey    return StringPrintf("%s/app", create_data_path(volume_uuid).c_str());
163d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey}
164d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey
165d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey/**
16641ea424413c0381ef2aa15fc5bd5d4b88abd23c4Jeff Sharkey * Create the path name for user data for a certain userid.
16741ea424413c0381ef2aa15fc5bd5d4b88abd23c4Jeff Sharkey */
1682f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkeystd::string create_data_user_ce_path(const char* volume_uuid, userid_t userid) {
16941ea424413c0381ef2aa15fc5bd5d4b88abd23c4Jeff Sharkey    std::string data(create_data_path(volume_uuid));
17041ea424413c0381ef2aa15fc5bd5d4b88abd23c4Jeff Sharkey    if (volume_uuid == nullptr) {
17141ea424413c0381ef2aa15fc5bd5d4b88abd23c4Jeff Sharkey        if (userid == 0) {
17241ea424413c0381ef2aa15fc5bd5d4b88abd23c4Jeff Sharkey            return StringPrintf("%s/data", data.c_str());
17341ea424413c0381ef2aa15fc5bd5d4b88abd23c4Jeff Sharkey        } else {
17441ea424413c0381ef2aa15fc5bd5d4b88abd23c4Jeff Sharkey            return StringPrintf("%s/user/%u", data.c_str(), userid);
17594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        }
17641ea424413c0381ef2aa15fc5bd5d4b88abd23c4Jeff Sharkey    } else {
17741ea424413c0381ef2aa15fc5bd5d4b88abd23c4Jeff Sharkey        return StringPrintf("%s/user/%u", data.c_str(), userid);
17894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
17994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
18094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
18194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood/**
18263ec2d64196144b2d15d2baffedccfa011d6494fJeff Sharkey * Create the path name for device encrypted user data for a certain userid.
18363ec2d64196144b2d15d2baffedccfa011d6494fJeff Sharkey */
18463ec2d64196144b2d15d2baffedccfa011d6494fJeff Sharkeystd::string create_data_user_de_path(const char* volume_uuid, userid_t userid) {
18563ec2d64196144b2d15d2baffedccfa011d6494fJeff Sharkey    std::string data(create_data_path(volume_uuid));
18663ec2d64196144b2d15d2baffedccfa011d6494fJeff Sharkey    return StringPrintf("%s/user_de/%u", data.c_str(), userid);
18763ec2d64196144b2d15d2baffedccfa011d6494fJeff Sharkey}
18863ec2d64196144b2d15d2baffedccfa011d6494fJeff Sharkey
18963ec2d64196144b2d15d2baffedccfa011d6494fJeff Sharkey/**
190abe4fe5b46157ecd2a52d28abf938c816c3ce878Jeff Sharkey * Create the path name for media for a certain userid.
19194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood */
19241ea424413c0381ef2aa15fc5bd5d4b88abd23c4Jeff Sharkeystd::string create_data_media_path(const char* volume_uuid, userid_t userid) {
19341ea424413c0381ef2aa15fc5bd5d4b88abd23c4Jeff Sharkey    return StringPrintf("%s/media/%u", create_data_path(volume_uuid).c_str(), userid);
19494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
19594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
1966a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravlestd::string create_data_user_profiles_path(userid_t userid) {
1976a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle    return StringPrintf("%s/cur/%u", android_profiles_dir.path, userid);
1986a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle}
1996a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle
2006a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravlestd::string create_data_user_profile_package_path(userid_t user, const char* package_name) {
2016a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle    check_package_name(package_name);
2026a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle    return StringPrintf("%s/%s",create_data_user_profiles_path(user).c_str(), package_name);
2036a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle}
2046a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle
2056a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravlestd::string create_data_ref_profile_package_path(const char* package_name) {
2066a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle    check_package_name(package_name);
2076a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle    return StringPrintf("%s/ref/%s", android_profiles_dir.path, package_name);
2086a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle}
2096a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle
210e36372423000a906bafae68844ebc6c42d09335aJeff Sharkeystd::vector<userid_t> get_known_users(const char* volume_uuid) {
211e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey    std::vector<userid_t> users;
212e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey
213e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey    // We always have an owner
214e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey    users.push_back(0);
215e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey
216e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey    std::string path(create_data_path(volume_uuid) + "/" + SECONDARY_USER_PREFIX);
217e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey    DIR* dir = opendir(path.c_str());
218e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey    if (dir == NULL) {
219e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey        // Unable to discover other users, but at least return owner
220e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey        PLOG(ERROR) << "Failed to opendir " << path;
221e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey        return users;
222e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey    }
223e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey
224e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey    struct dirent* ent;
225e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey    while ((ent = readdir(dir))) {
226e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey        if (ent->d_type != DT_DIR) {
227e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey            continue;
228e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey        }
229e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey
230e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey        char* end;
231e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey        userid_t user = strtol(ent->d_name, &end, 10);
232e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey        if (*end == '\0' && user != 0) {
233e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey            LOG(DEBUG) << "Found valid user " << user;
234e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey            users.push_back(user);
235e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey        }
236e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey    }
237e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey    closedir(dir);
238e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey
239e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey    return users;
240e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey}
241e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey
242095c763dd9aa26a206d10ab7c1d7e1c569298fb3Robin Lee/**
243095c763dd9aa26a206d10ab7c1d7e1c569298fb3Robin Lee * Create the path name for config for a certain userid.
244095c763dd9aa26a206d10ab7c1d7e1c569298fb3Robin Lee * Returns 0 on success, and -1 on failure.
245095c763dd9aa26a206d10ab7c1d7e1c569298fb3Robin Lee */
246095c763dd9aa26a206d10ab7c1d7e1c569298fb3Robin Leeint create_user_config_path(char path[PATH_MAX], userid_t userid) {
247095c763dd9aa26a206d10ab7c1d7e1c569298fb3Robin Lee    if (snprintf(path, PATH_MAX, "%s%d", "/data/misc/user/", userid) > PATH_MAX) {
248095c763dd9aa26a206d10ab7c1d7e1c569298fb3Robin Lee        return -1;
249095c763dd9aa26a206d10ab7c1d7e1c569298fb3Robin Lee    }
250095c763dd9aa26a206d10ab7c1d7e1c569298fb3Robin Lee    return 0;
251095c763dd9aa26a206d10ab7c1d7e1c569298fb3Robin Lee}
252095c763dd9aa26a206d10ab7c1d7e1c569298fb3Robin Lee
25394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwoodint create_move_path(char path[PKG_PATH_MAX],
25494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    const char* pkgname,
25594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    const char* leaf,
25602d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe    userid_t userid ATTRIBUTE_UNUSED)
25794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood{
25894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    if ((android_data_dir.len + strlen(PRIMARY_USER_PREFIX) + strlen(pkgname) + strlen(leaf) + 1)
25994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            >= PKG_PATH_MAX) {
26094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        return -1;
26194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
26294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
26394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    sprintf(path, "%s%s%s/%s", android_data_dir.path, PRIMARY_USER_PREFIX, pkgname, leaf);
26494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    return 0;
26594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
26694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
26794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood/**
26894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood * Checks whether the package name is valid. Returns -1 on error and
26994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood * 0 on success.
27094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood */
27194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwoodint is_valid_package_name(const char* pkgname) {
27294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    const char *x = pkgname;
27394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    int alpha = -1;
27494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
275c03de09173f98506e73e7cf7df21fe11795d4b24Jeff Sharkey    if (strlen(pkgname) > PKG_NAME_MAX) {
276c03de09173f98506e73e7cf7df21fe11795d4b24Jeff Sharkey        return -1;
277c03de09173f98506e73e7cf7df21fe11795d4b24Jeff Sharkey    }
278c03de09173f98506e73e7cf7df21fe11795d4b24Jeff Sharkey
27994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    while (*x) {
28094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        if (isalnum(*x) || (*x == '_')) {
28194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                /* alphanumeric or underscore are fine */
28294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        } else if (*x == '.') {
28394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            if ((x == pkgname) || (x[1] == '.') || (x[1] == 0)) {
28494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                    /* periods must not be first, last, or doubled */
28594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                ALOGE("invalid package name '%s'\n", pkgname);
28694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                return -1;
28794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            }
28894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        } else if (*x == '-') {
28994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            /* Suffix -X is fine to let versioning of packages.
29094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood               But whatever follows should be alphanumeric.*/
29194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            alpha = 1;
29294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        } else {
29394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                /* anything not A-Z, a-z, 0-9, _, or . is invalid */
29494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            ALOGE("invalid package name '%s'\n", pkgname);
29594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            return -1;
29694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        }
29794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
29894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        x++;
29994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
30094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
30194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    if (alpha == 1) {
30294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        // Skip current character
30394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        x++;
30494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        while (*x) {
30594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            if (!isalnum(*x)) {
30694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                ALOGE("invalid package name '%s' should include only numbers after -\n", pkgname);
30794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                return -1;
30894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            }
30994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            x++;
31094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        }
31194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
31294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
31394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    return 0;
31494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
31594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
3163aee2c5c749dc2589f001b26fae1ec958ec89524Narayan Kamathstatic int _delete_dir_contents(DIR *d,
3173aee2c5c749dc2589f001b26fae1ec958ec89524Narayan Kamath                                int (*exclusion_predicate)(const char *name, const int is_dir))
31894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood{
31994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    int result = 0;
32094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    struct dirent *de;
32194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    int dfd;
32294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
32394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    dfd = dirfd(d);
32494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
32594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    if (dfd < 0) return -1;
32694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
32794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    while ((de = readdir(d))) {
32894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        const char *name = de->d_name;
32994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
3303aee2c5c749dc2589f001b26fae1ec958ec89524Narayan Kamath            /* check using the exclusion predicate, if provided */
3313aee2c5c749dc2589f001b26fae1ec958ec89524Narayan Kamath        if (exclusion_predicate && exclusion_predicate(name, (de->d_type == DT_DIR))) {
3323aee2c5c749dc2589f001b26fae1ec958ec89524Narayan Kamath            continue;
3333aee2c5c749dc2589f001b26fae1ec958ec89524Narayan Kamath        }
33494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
33594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        if (de->d_type == DT_DIR) {
33699d9fb15b4a1eb534261ae81b2cf25aec4447bd0Chih-Hung Hsieh            int subfd;
33794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            DIR *subdir;
33894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
33994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                /* always skip "." and ".." */
34094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            if (name[0] == '.') {
34194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                if (name[1] == 0) continue;
34294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                if ((name[1] == '.') && (name[2] == 0)) continue;
34394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            }
34494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
3458b7acacc93930df9fa9e1ebea4a4394195b2332eNick Kralevich            subfd = openat(dfd, name, O_RDONLY | O_DIRECTORY | O_NOFOLLOW | O_CLOEXEC);
34694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            if (subfd < 0) {
34794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                ALOGE("Couldn't openat %s: %s\n", name, strerror(errno));
34894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                result = -1;
34994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                continue;
35094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            }
35194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            subdir = fdopendir(subfd);
35294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            if (subdir == NULL) {
35394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                ALOGE("Couldn't fdopendir %s: %s\n", name, strerror(errno));
35494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                close(subfd);
35594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                result = -1;
35694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                continue;
35794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            }
3583aee2c5c749dc2589f001b26fae1ec958ec89524Narayan Kamath            if (_delete_dir_contents(subdir, exclusion_predicate)) {
35994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                result = -1;
36094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            }
36194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            closedir(subdir);
36294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            if (unlinkat(dfd, name, AT_REMOVEDIR) < 0) {
36394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                ALOGE("Couldn't unlinkat %s: %s\n", name, strerror(errno));
36494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                result = -1;
36594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            }
36694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        } else {
36794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            if (unlinkat(dfd, name, 0) < 0) {
36894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                ALOGE("Couldn't unlinkat %s: %s\n", name, strerror(errno));
36994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                result = -1;
37094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            }
37194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        }
37294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
37394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
37494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    return result;
37594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
37694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
377b06f98aabc5381fd6366526d9b31b5d0345481b6Calin Juravleint delete_dir_contents(const std::string& pathname, bool ignore_if_missing) {
378b06f98aabc5381fd6366526d9b31b5d0345481b6Calin Juravle    return delete_dir_contents(pathname.c_str(), 0, NULL, ignore_if_missing);
379ebf728fd43ab5c7d11a1f9e5fdc775d6740fae0aJeff Sharkey}
380ebf728fd43ab5c7d11a1f9e5fdc775d6740fae0aJeff Sharkey
381b06f98aabc5381fd6366526d9b31b5d0345481b6Calin Juravleint delete_dir_contents_and_dir(const std::string& pathname, bool ignore_if_missing) {
382b06f98aabc5381fd6366526d9b31b5d0345481b6Calin Juravle    return delete_dir_contents(pathname.c_str(), 1, NULL, ignore_if_missing);
383ebf728fd43ab5c7d11a1f9e5fdc775d6740fae0aJeff Sharkey}
384ebf728fd43ab5c7d11a1f9e5fdc775d6740fae0aJeff Sharkey
38594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwoodint delete_dir_contents(const char *pathname,
38694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                        int also_delete_dir,
387b06f98aabc5381fd6366526d9b31b5d0345481b6Calin Juravle                        int (*exclusion_predicate)(const char*, const int),
388b06f98aabc5381fd6366526d9b31b5d0345481b6Calin Juravle                        bool ignore_if_missing)
38994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood{
39094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    int res = 0;
39194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    DIR *d;
39294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
39394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    d = opendir(pathname);
39494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    if (d == NULL) {
395b06f98aabc5381fd6366526d9b31b5d0345481b6Calin Juravle        if (ignore_if_missing && (errno == ENOENT)) {
396b06f98aabc5381fd6366526d9b31b5d0345481b6Calin Juravle            return 0;
397b06f98aabc5381fd6366526d9b31b5d0345481b6Calin Juravle        }
39894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        ALOGE("Couldn't opendir %s: %s\n", pathname, strerror(errno));
39994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        return -errno;
40094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
4013aee2c5c749dc2589f001b26fae1ec958ec89524Narayan Kamath    res = _delete_dir_contents(d, exclusion_predicate);
40294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    closedir(d);
40394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    if (also_delete_dir) {
40494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        if (rmdir(pathname)) {
40594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            ALOGE("Couldn't rmdir %s: %s\n", pathname, strerror(errno));
40694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            res = -1;
40794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        }
40894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
40994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    return res;
41094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
41194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
41294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwoodint delete_dir_contents_fd(int dfd, const char *name)
41394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood{
41494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    int fd, res;
41594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    DIR *d;
41694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
4178b7acacc93930df9fa9e1ebea4a4394195b2332eNick Kralevich    fd = openat(dfd, name, O_RDONLY | O_DIRECTORY | O_NOFOLLOW | O_CLOEXEC);
41894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    if (fd < 0) {
41994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        ALOGE("Couldn't openat %s: %s\n", name, strerror(errno));
42094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        return -1;
42194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
42294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    d = fdopendir(fd);
42394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    if (d == NULL) {
42494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        ALOGE("Couldn't fdopendir %s: %s\n", name, strerror(errno));
42594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        close(fd);
42694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        return -1;
42794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
42894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    res = _delete_dir_contents(d, 0);
42994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    closedir(d);
43094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    return res;
43194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
43294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
43360fd3feecab4336d964ca8e31c7c3220e1afb558Robin Leestatic int _copy_owner_permissions(int srcfd, int dstfd)
43460fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee{
43560fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee    struct stat st;
43660fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee    if (fstat(srcfd, &st) != 0) {
43760fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee        return -1;
43860fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee    }
43960fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee    if (fchmod(dstfd, st.st_mode) != 0) {
44060fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee        return -1;
44160fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee    }
44260fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee    return 0;
44360fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee}
44460fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee
44560fd3feecab4336d964ca8e31c7c3220e1afb558Robin Leestatic int _copy_dir_files(int sdfd, int ddfd, uid_t owner, gid_t group)
44660fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee{
44760fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee    int result = 0;
44860fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee    if (_copy_owner_permissions(sdfd, ddfd) != 0) {
44960fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee        ALOGE("_copy_dir_files failed to copy dir permissions\n");
45060fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee    }
45160fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee    if (fchown(ddfd, owner, group) != 0) {
45260fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee        ALOGE("_copy_dir_files failed to change dir owner\n");
45360fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee    }
45460fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee
45560fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee    DIR *ds = fdopendir(sdfd);
45660fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee    if (ds == NULL) {
45760fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee        ALOGE("Couldn't fdopendir: %s\n", strerror(errno));
45860fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee        return -1;
45960fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee    }
46060fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee    struct dirent *de;
46160fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee    while ((de = readdir(ds))) {
46260fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee        if (de->d_type != DT_REG) {
46360fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee            continue;
46460fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee        }
46560fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee
46660fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee        const char *name = de->d_name;
46760fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee        int fsfd = openat(sdfd, name, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
46860fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee        int fdfd = openat(ddfd, name, O_WRONLY | O_NOFOLLOW | O_CLOEXEC | O_CREAT, 0600);
46960fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee        if (fsfd == -1 || fdfd == -1) {
47060fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee            ALOGW("Couldn't copy %s: %s\n", name, strerror(errno));
47160fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee        } else {
47260fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee            if (_copy_owner_permissions(fsfd, fdfd) != 0) {
47360fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee                ALOGE("Failed to change file permissions\n");
47460fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee            }
47560fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee            if (fchown(fdfd, owner, group) != 0) {
47660fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee                ALOGE("Failed to change file owner\n");
47760fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee            }
47860fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee
47960fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee            char buf[8192];
48060fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee            ssize_t size;
48160fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee            while ((size = read(fsfd, buf, sizeof(buf))) > 0) {
48260fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee                write(fdfd, buf, size);
48360fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee            }
48460fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee            if (size < 0) {
48560fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee                ALOGW("Couldn't copy %s: %s\n", name, strerror(errno));
48660fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee                result = -1;
48760fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee            }
48860fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee        }
48960fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee        close(fdfd);
49060fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee        close(fsfd);
49160fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee    }
49260fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee
49360fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee    return result;
49460fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee}
49560fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee
49660fd3feecab4336d964ca8e31c7c3220e1afb558Robin Leeint copy_dir_files(const char *srcname,
49760fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee                   const char *dstname,
49860fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee                   uid_t owner,
49960fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee                   uid_t group)
50060fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee{
50160fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee    int res = 0;
50260fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee    DIR *ds = NULL;
50360fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee    DIR *dd = NULL;
50460fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee
50560fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee    ds = opendir(srcname);
50660fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee    if (ds == NULL) {
50760fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee        ALOGE("Couldn't opendir %s: %s\n", srcname, strerror(errno));
50860fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee        return -errno;
50960fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee    }
51060fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee
51160fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee    mkdir(dstname, 0600);
51260fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee    dd = opendir(dstname);
51360fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee    if (dd == NULL) {
51460fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee        ALOGE("Couldn't opendir %s: %s\n", dstname, strerror(errno));
51560fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee        closedir(ds);
51660fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee        return -errno;
51760fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee    }
51860fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee
51960fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee    int sdfd = dirfd(ds);
52060fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee    int ddfd = dirfd(dd);
52160fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee    if (sdfd != -1 && ddfd != -1) {
52260fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee        res = _copy_dir_files(sdfd, ddfd, owner, group);
52360fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee    } else {
52460fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee        res = -errno;
52560fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee    }
52660fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee    closedir(dd);
52760fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee    closedir(ds);
52860fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee    return res;
52960fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee}
53060fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee
53194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwoodint lookup_media_dir(char basepath[PATH_MAX], const char *dir)
53294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood{
53394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    DIR *d;
53494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    struct dirent *de;
53594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    struct stat s;
53694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    char* dirpos = basepath + strlen(basepath);
53794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
53894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    if ((*(dirpos-1)) != '/') {
53994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        *dirpos = '/';
54094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        dirpos++;
54194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
54294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
54394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    CACHE_NOISY(ALOGI("Looking up %s in %s\n", dir, basepath));
54494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    // Verify the path won't extend beyond our buffer, to avoid
54594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    // repeated checking later.
54694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    if ((dirpos-basepath+strlen(dir)) >= (PATH_MAX-1)) {
54794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        ALOGW("Path exceeds limit: %s%s", basepath, dir);
54894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        return -1;
54994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
55094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
55194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    // First, can we find this directory with the case that is given?
55294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    strcpy(dirpos, dir);
55394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    if (stat(basepath, &s) >= 0) {
55494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        CACHE_NOISY(ALOGI("Found direct: %s\n", basepath));
55594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        return 0;
55694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
55794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
55894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    // Not found with that case...  search through all entries to find
55994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    // one that matches regardless of case.
56094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    *dirpos = 0;
56194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
56294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    d = opendir(basepath);
56394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    if (d == NULL) {
56494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        return -1;
56594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
56694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
56794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    while ((de = readdir(d))) {
56894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        if (strcasecmp(de->d_name, dir) == 0) {
56994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            strcpy(dirpos, de->d_name);
57094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            closedir(d);
57194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            CACHE_NOISY(ALOGI("Found search: %s\n", basepath));
57294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            return 0;
57394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        }
57494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
57594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
57694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    ALOGW("Couldn't find %s in %s", dir, basepath);
57794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    closedir(d);
57894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    return -1;
57994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
58094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
58141ea424413c0381ef2aa15fc5bd5d4b88abd23c4Jeff Sharkeyint64_t data_disk_free(const std::string& data_path)
58294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood{
58394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    struct statfs sfs;
58441ea424413c0381ef2aa15fc5bd5d4b88abd23c4Jeff Sharkey    if (statfs(data_path.c_str(), &sfs) == 0) {
58594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        return sfs.f_bavail * sfs.f_bsize;
58694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    } else {
58741ea424413c0381ef2aa15fc5bd5d4b88abd23c4Jeff Sharkey        PLOG(ERROR) << "Couldn't statfs " << data_path;
58894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        return -1;
58994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
59094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
59194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
59294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwoodcache_t* start_cache_collection()
59394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood{
59494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    cache_t* cache = (cache_t*)calloc(1, sizeof(cache_t));
59594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    return cache;
59694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
59794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
59894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood#define CACHE_BLOCK_SIZE (512*1024)
59994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
60094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwoodstatic void* _cache_malloc(cache_t* cache, size_t len)
60194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood{
60294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    len = (len+3)&~3;
60394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    if (len > (CACHE_BLOCK_SIZE/2)) {
60494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        // It doesn't make sense to try to put this allocation into one
60594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        // of our blocks, because it is so big.  Instead, make a new dedicated
60694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        // block for it.
60794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        int8_t* res = (int8_t*)malloc(len+sizeof(void*));
60894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        if (res == NULL) {
60994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            return NULL;
61094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        }
61194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        CACHE_NOISY(ALOGI("Allocated large cache mem block: %p size %d", res, len));
61294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        // Link it into our list of blocks, not disrupting the current one.
61394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        if (cache->memBlocks == NULL) {
61494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            *(void**)res = NULL;
61594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            cache->memBlocks = res;
61694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        } else {
61794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            *(void**)res = *(void**)cache->memBlocks;
61894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            *(void**)cache->memBlocks = res;
61994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        }
62094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        return res + sizeof(void*);
62194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
62294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    int8_t* res = cache->curMemBlockAvail;
62394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    int8_t* nextPos = res + len;
62494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    if (cache->memBlocks == NULL || nextPos > cache->curMemBlockEnd) {
62519803807cd7ae01868fcfa50305f4a7dd13765e2Jeff Sharkey        int8_t* newBlock = (int8_t*) malloc(CACHE_BLOCK_SIZE);
62694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        if (newBlock == NULL) {
62794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            return NULL;
62894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        }
62994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        CACHE_NOISY(ALOGI("Allocated new cache mem block: %p", newBlock));
63094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        *(void**)newBlock = cache->memBlocks;
63194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        cache->memBlocks = newBlock;
63294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        res = cache->curMemBlockAvail = newBlock + sizeof(void*);
63394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        cache->curMemBlockEnd = newBlock + CACHE_BLOCK_SIZE;
63494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        nextPos = res + len;
63594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
63694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    CACHE_NOISY(ALOGI("cache_malloc: ret %p size %d, block=%p, nextPos=%p",
63794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            res, len, cache->memBlocks, nextPos));
63894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    cache->curMemBlockAvail = nextPos;
63994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    return res;
64094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
64194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
64294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwoodstatic void* _cache_realloc(cache_t* cache, void* cur, size_t origLen, size_t len)
64394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood{
64494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    // This isn't really a realloc, but it is good enough for our purposes here.
64594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    void* alloc = _cache_malloc(cache, len);
64694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    if (alloc != NULL && cur != NULL) {
64794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        memcpy(alloc, cur, origLen < len ? origLen : len);
64894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
64994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    return alloc;
65094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
65194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
65294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwoodstatic void _inc_num_cache_collected(cache_t* cache)
65394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood{
65494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    cache->numCollected++;
65594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    if ((cache->numCollected%20000) == 0) {
65692dc3fc52cf097bd105460cf377779bdcf146d62Mark Salyzyn        ALOGI("Collected cache so far: %zd directories, %zd files",
65794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            cache->numDirs, cache->numFiles);
65894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
65994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
66094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
66194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwoodstatic cache_dir_t* _add_cache_dir_t(cache_t* cache, cache_dir_t* parent, const char *name)
66294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood{
66394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    size_t nameLen = strlen(name);
66494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    cache_dir_t* dir = (cache_dir_t*)_cache_malloc(cache, sizeof(cache_dir_t)+nameLen+1);
66594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    if (dir != NULL) {
66694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        dir->parent = parent;
66794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        dir->childCount = 0;
66894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        dir->hiddenCount = 0;
66994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        dir->deleted = 0;
67094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        strcpy(dir->name, name);
67194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        if (cache->numDirs >= cache->availDirs) {
67294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            size_t newAvail = cache->availDirs < 1000 ? 1000 : cache->availDirs*2;
67394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            cache_dir_t** newDirs = (cache_dir_t**)_cache_realloc(cache, cache->dirs,
67494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                    cache->availDirs*sizeof(cache_dir_t*), newAvail*sizeof(cache_dir_t*));
67594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            if (newDirs == NULL) {
67694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                ALOGE("Failure growing cache dirs array for %s\n", name);
67794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                return NULL;
67894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            }
67994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            cache->availDirs = newAvail;
68094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            cache->dirs = newDirs;
68194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        }
68294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        cache->dirs[cache->numDirs] = dir;
68394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        cache->numDirs++;
68494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        if (parent != NULL) {
68594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            parent->childCount++;
68694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        }
68794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        _inc_num_cache_collected(cache);
68894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    } else {
68994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        ALOGE("Failure allocating cache_dir_t for %s\n", name);
69094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
69194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    return dir;
69294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
69394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
69494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwoodstatic cache_file_t* _add_cache_file_t(cache_t* cache, cache_dir_t* dir, time_t modTime,
69594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        const char *name)
69694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood{
69794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    size_t nameLen = strlen(name);
69894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    cache_file_t* file = (cache_file_t*)_cache_malloc(cache, sizeof(cache_file_t)+nameLen+1);
69994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    if (file != NULL) {
70094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        file->dir = dir;
70194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        file->modTime = modTime;
70294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        strcpy(file->name, name);
70394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        if (cache->numFiles >= cache->availFiles) {
70494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            size_t newAvail = cache->availFiles < 1000 ? 1000 : cache->availFiles*2;
70594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            cache_file_t** newFiles = (cache_file_t**)_cache_realloc(cache, cache->files,
70694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                    cache->availFiles*sizeof(cache_file_t*), newAvail*sizeof(cache_file_t*));
70794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            if (newFiles == NULL) {
70894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                ALOGE("Failure growing cache file array for %s\n", name);
70994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                return NULL;
71094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            }
71194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            cache->availFiles = newAvail;
71294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            cache->files = newFiles;
71394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        }
71494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        CACHE_NOISY(ALOGI("Setting file %p at position %d in array %p", file,
71594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                cache->numFiles, cache->files));
71694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        cache->files[cache->numFiles] = file;
71794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        cache->numFiles++;
71894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        dir->childCount++;
71994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        _inc_num_cache_collected(cache);
72094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    } else {
72194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        ALOGE("Failure allocating cache_file_t for %s\n", name);
72294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
72394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    return file;
72494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
72594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
72694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwoodstatic int _add_cache_files(cache_t *cache, cache_dir_t *parentDir, const char *dirName,
72794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        DIR* dir, char *pathBase, char *pathPos, size_t pathAvailLen)
72894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood{
72994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    struct dirent *de;
73094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    cache_dir_t* cacheDir = NULL;
73194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    int dfd;
73294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
73394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    CACHE_NOISY(ALOGI("_add_cache_files: parent=%p dirName=%s dir=%p pathBase=%s",
73494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            parentDir, dirName, dir, pathBase));
73594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
73694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    dfd = dirfd(dir);
73794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
73894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    if (dfd < 0) return 0;
73994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
74094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    // Sub-directories always get added to the data structure, so if they
74194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    // are empty we will know about them to delete them later.
74294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    cacheDir = _add_cache_dir_t(cache, parentDir, dirName);
74394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
74494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    while ((de = readdir(dir))) {
74594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        const char *name = de->d_name;
74694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
74794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        if (de->d_type == DT_DIR) {
74894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            int subfd;
74994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            DIR *subdir;
75094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
75194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                /* always skip "." and ".." */
75294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            if (name[0] == '.') {
75394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                if (name[1] == 0) continue;
75494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                if ((name[1] == '.') && (name[2] == 0)) continue;
75594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            }
75694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
7578b7acacc93930df9fa9e1ebea4a4394195b2332eNick Kralevich            subfd = openat(dfd, name, O_RDONLY | O_DIRECTORY | O_NOFOLLOW | O_CLOEXEC);
75894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            if (subfd < 0) {
75994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                ALOGE("Couldn't openat %s: %s\n", name, strerror(errno));
76094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                continue;
76194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            }
76294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            subdir = fdopendir(subfd);
76394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            if (subdir == NULL) {
76494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                ALOGE("Couldn't fdopendir %s: %s\n", name, strerror(errno));
76594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                close(subfd);
76694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                continue;
76794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            }
76894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            if (cacheDir == NULL) {
76994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                cacheDir = _add_cache_dir_t(cache, parentDir, dirName);
77094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            }
77194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            if (cacheDir != NULL) {
77294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                // Update pathBase for the new path...  this may change dirName
77394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                // if that is also pointing to the path, but we are done with it
77494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                // now.
77594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                size_t finallen = snprintf(pathPos, pathAvailLen, "/%s", name);
77694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                CACHE_NOISY(ALOGI("Collecting dir %s\n", pathBase));
77794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                if (finallen < pathAvailLen) {
77894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                    _add_cache_files(cache, cacheDir, name, subdir, pathBase,
77994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                            pathPos+finallen, pathAvailLen-finallen);
78094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                } else {
78194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                    // Whoops, the final path is too long!  We'll just delete
78294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                    // this directory.
78394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                    ALOGW("Cache dir %s truncated in path %s; deleting dir\n",
78494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                            name, pathBase);
78594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                    _delete_dir_contents(subdir, NULL);
78694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                    if (unlinkat(dfd, name, AT_REMOVEDIR) < 0) {
78794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                        ALOGE("Couldn't unlinkat %s: %s\n", name, strerror(errno));
78894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                    }
78994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                }
79094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            }
79194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            closedir(subdir);
79294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        } else if (de->d_type == DT_REG) {
79394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            // Skip files that start with '.'; they will be deleted if
79494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            // their entire directory is deleted.  This allows for metadata
79594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            // like ".nomedia" to remain in the directory until the entire
79694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            // directory is deleted.
79794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            if (cacheDir == NULL) {
79894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                cacheDir = _add_cache_dir_t(cache, parentDir, dirName);
79994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            }
80094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            if (name[0] == '.') {
80194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                cacheDir->hiddenCount++;
80294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                continue;
80394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            }
80494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            if (cacheDir != NULL) {
80594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                // Build final full path for file...  this may change dirName
80694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                // if that is also pointing to the path, but we are done with it
80794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                // now.
80894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                size_t finallen = snprintf(pathPos, pathAvailLen, "/%s", name);
80994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                CACHE_NOISY(ALOGI("Collecting file %s\n", pathBase));
81094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                if (finallen < pathAvailLen) {
81194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                    struct stat s;
81294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                    if (stat(pathBase, &s) >= 0) {
81394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                        _add_cache_file_t(cache, cacheDir, s.st_mtime, name);
81494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                    } else {
81594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                        ALOGW("Unable to stat cache file %s; deleting\n", pathBase);
81694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                        if (unlink(pathBase) < 0) {
81794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                            ALOGE("Couldn't unlink %s: %s\n", pathBase, strerror(errno));
81894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                        }
81994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                    }
82094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                } else {
82194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                    // Whoops, the final path is too long!  We'll just delete
82294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                    // this file.
82394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                    ALOGW("Cache file %s truncated in path %s; deleting\n",
82494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                            name, pathBase);
82594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                    if (unlinkat(dfd, name, 0) < 0) {
82694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                        *pathPos = 0;
82794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                        ALOGE("Couldn't unlinkat %s in %s: %s\n", name, pathBase,
82894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                                strerror(errno));
82994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                    }
83094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                }
83194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            }
83294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        } else {
83394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            cacheDir->hiddenCount++;
83494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        }
83594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
83694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    return 0;
83794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
83894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
83994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwoodvoid add_cache_files(cache_t* cache, const char *basepath, const char *cachedir)
84094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood{
84194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    DIR *d;
84294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    struct dirent *de;
84394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    char dirname[PATH_MAX];
84494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
84594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    CACHE_NOISY(ALOGI("add_cache_files: base=%s cachedir=%s\n", basepath, cachedir));
84694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
84794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    d = opendir(basepath);
84894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    if (d == NULL) {
84994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        return;
85094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
85194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
85294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    while ((de = readdir(d))) {
85394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        if (de->d_type == DT_DIR) {
85494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            DIR* subdir;
85594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            const char *name = de->d_name;
85694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            char* pathpos;
85794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
85894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                /* always skip "." and ".." */
85994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            if (name[0] == '.') {
86094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                if (name[1] == 0) continue;
86194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                if ((name[1] == '.') && (name[2] == 0)) continue;
86294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            }
86394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
86494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            strcpy(dirname, basepath);
86594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            pathpos = dirname + strlen(dirname);
86694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            if ((*(pathpos-1)) != '/') {
86794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                *pathpos = '/';
86894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                pathpos++;
86994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                *pathpos = 0;
87094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            }
87194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            if (cachedir != NULL) {
87294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                snprintf(pathpos, sizeof(dirname)-(pathpos-dirname), "%s/%s", name, cachedir);
87394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            } else {
87494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                snprintf(pathpos, sizeof(dirname)-(pathpos-dirname), "%s", name);
87594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            }
87694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            CACHE_NOISY(ALOGI("Adding cache files from dir: %s\n", dirname));
87794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            subdir = opendir(dirname);
87894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            if (subdir != NULL) {
87994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                size_t dirnameLen = strlen(dirname);
88094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                _add_cache_files(cache, NULL, dirname, subdir, dirname, dirname+dirnameLen,
88194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                        PATH_MAX - dirnameLen);
88294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                closedir(subdir);
88394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            }
88494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        }
88594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
88694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
88794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    closedir(d);
88894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
88994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
89094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwoodstatic char *create_dir_path(char path[PATH_MAX], cache_dir_t* dir)
89194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood{
89294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    char *pos = path;
89394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    if (dir->parent != NULL) {
89494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        pos = create_dir_path(path, dir->parent);
89594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
89694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    // Note that we don't need to worry about going beyond the buffer,
89794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    // since when we were constructing the cache entries our maximum
89894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    // buffer size for full paths was PATH_MAX.
89994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    strcpy(pos, dir->name);
90094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    pos += strlen(pos);
90194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    *pos = '/';
90294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    pos++;
90394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    *pos = 0;
90494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    return pos;
90594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
90694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
90794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwoodstatic void delete_cache_dir(char path[PATH_MAX], cache_dir_t* dir)
90894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood{
90994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    if (dir->parent != NULL) {
91094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        create_dir_path(path, dir);
91194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        ALOGI("DEL DIR %s\n", path);
91294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        if (dir->hiddenCount <= 0) {
91394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            if (rmdir(path)) {
91494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                ALOGE("Couldn't rmdir %s: %s\n", path, strerror(errno));
91594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                return;
91694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            }
91794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        } else {
91894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            // The directory contains hidden files so we need to delete
91994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            // them along with the directory itself.
92094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            if (delete_dir_contents(path, 1, NULL)) {
92194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                return;
92294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            }
92394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        }
92494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        dir->parent->childCount--;
92594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        dir->deleted = 1;
92694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        if (dir->parent->childCount <= 0) {
92794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            delete_cache_dir(path, dir->parent);
92894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        }
92994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    } else if (dir->hiddenCount > 0) {
93094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        // This is a root directory, but it has hidden files.  Get rid of
93194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        // all of those files, but not the directory itself.
93294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        create_dir_path(path, dir);
93394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        ALOGI("DEL CONTENTS %s\n", path);
93494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        delete_dir_contents(path, 0, NULL);
93594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
93694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
93794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
93894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwoodstatic int cache_modtime_sort(const void *lhsP, const void *rhsP)
93994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood{
94094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    const cache_file_t *lhs = *(const cache_file_t**)lhsP;
94194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    const cache_file_t *rhs = *(const cache_file_t**)rhsP;
94294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    return lhs->modTime < rhs->modTime ? -1 : (lhs->modTime > rhs->modTime ? 1 : 0);
94394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
94494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
94541ea424413c0381ef2aa15fc5bd5d4b88abd23c4Jeff Sharkeyvoid clear_cache_files(const std::string& data_path, cache_t* cache, int64_t free_size)
94694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood{
94794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    size_t i;
94894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    int skip = 0;
94994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    char path[PATH_MAX];
95094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
95192dc3fc52cf097bd105460cf377779bdcf146d62Mark Salyzyn    ALOGI("Collected cache files: %zd directories, %zd files",
95294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        cache->numDirs, cache->numFiles);
95394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
95494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    CACHE_NOISY(ALOGI("Sorting files..."));
95594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    qsort(cache->files, cache->numFiles, sizeof(cache_file_t*),
95694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            cache_modtime_sort);
95794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
95894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    CACHE_NOISY(ALOGI("Cleaning empty directories..."));
95994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    for (i=cache->numDirs; i>0; i--) {
96094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        cache_dir_t* dir = cache->dirs[i-1];
96194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        if (dir->childCount <= 0 && !dir->deleted) {
96294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            delete_cache_dir(path, dir);
96394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        }
96494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
96594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
96694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    CACHE_NOISY(ALOGI("Trimming files..."));
96794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    for (i=0; i<cache->numFiles; i++) {
96894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        skip++;
96994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        if (skip > 10) {
97041ea424413c0381ef2aa15fc5bd5d4b88abd23c4Jeff Sharkey            if (data_disk_free(data_path) > free_size) {
97194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                return;
97294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            }
97394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            skip = 0;
97494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        }
97594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        cache_file_t* file = cache->files[i];
97694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        strcpy(create_dir_path(path, file->dir), file->name);
97794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        ALOGI("DEL (mod %d) %s\n", (int)file->modTime, path);
97894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        if (unlink(path) < 0) {
97994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            ALOGE("Couldn't unlink %s: %s\n", path, strerror(errno));
98094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        }
98194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        file->dir->childCount--;
98294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        if (file->dir->childCount <= 0) {
98394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            delete_cache_dir(path, file->dir);
98494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        }
98594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
98694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
98794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
98894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwoodvoid finish_cache_collection(cache_t* cache)
98994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood{
99099d9fb15b4a1eb534261ae81b2cf25aec4447bd0Chih-Hung Hsieh    CACHE_NOISY(size_t i;)
99194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
99294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    CACHE_NOISY(ALOGI("clear_cache_files: %d dirs, %d files\n", cache->numDirs, cache->numFiles));
99394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    CACHE_NOISY(
99494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        for (i=0; i<cache->numDirs; i++) {
99594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            cache_dir_t* dir = cache->dirs[i];
99694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            ALOGI("dir #%d: %p %s parent=%p\n", i, dir, dir->name, dir->parent);
99794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        })
99894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    CACHE_NOISY(
99994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        for (i=0; i<cache->numFiles; i++) {
100094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            cache_file_t* file = cache->files[i];
100194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            ALOGI("file #%d: %p %s time=%d dir=%p\n", i, file, file->name,
100294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                    (int)file->modTime, file->dir);
100394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        })
100494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    void* block = cache->memBlocks;
100594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    while (block != NULL) {
100694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        void* nextBlock = *(void**)block;
100794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        CACHE_NOISY(ALOGI("Freeing cache mem block: %p", block));
100894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        free(block);
100994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        block = nextBlock;
101094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
101194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    free(cache);
101294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
101394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
101494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood/**
1015c597b6dd895dbb2b28c757ce7a2651b3cdc9b00cCalin Juravle * Validate that the path is valid in the context of the provided directory.
1016c597b6dd895dbb2b28c757ce7a2651b3cdc9b00cCalin Juravle * The path is allowed to have at most one subdirectory and no indirections
1017c597b6dd895dbb2b28c757ce7a2651b3cdc9b00cCalin Juravle * to top level directories (i.e. have "..").
1018c597b6dd895dbb2b28c757ce7a2651b3cdc9b00cCalin Juravle */
1019e23a13299a4f6f2488935b4786cdbb46f46e3d3cJeff Sharkeystatic int validate_path(const dir_rec_t* dir, const char* path, int maxSubdirs) {
1020c597b6dd895dbb2b28c757ce7a2651b3cdc9b00cCalin Juravle    size_t dir_len = dir->len;
1021c597b6dd895dbb2b28c757ce7a2651b3cdc9b00cCalin Juravle    const char* subdir = strchr(path + dir_len, '/');
1022c597b6dd895dbb2b28c757ce7a2651b3cdc9b00cCalin Juravle
1023c597b6dd895dbb2b28c757ce7a2651b3cdc9b00cCalin Juravle    // Only allow the path to have at most one subdirectory.
1024c597b6dd895dbb2b28c757ce7a2651b3cdc9b00cCalin Juravle    if (subdir != NULL) {
1025c597b6dd895dbb2b28c757ce7a2651b3cdc9b00cCalin Juravle        ++subdir;
1026e23a13299a4f6f2488935b4786cdbb46f46e3d3cJeff Sharkey        if ((--maxSubdirs == 0) && strchr(subdir, '/') != NULL) {
1027c597b6dd895dbb2b28c757ce7a2651b3cdc9b00cCalin Juravle            ALOGE("invalid apk path '%s' (subdir?)\n", path);
1028c597b6dd895dbb2b28c757ce7a2651b3cdc9b00cCalin Juravle            return -1;
1029c597b6dd895dbb2b28c757ce7a2651b3cdc9b00cCalin Juravle        }
1030c597b6dd895dbb2b28c757ce7a2651b3cdc9b00cCalin Juravle    }
1031c597b6dd895dbb2b28c757ce7a2651b3cdc9b00cCalin Juravle
1032c597b6dd895dbb2b28c757ce7a2651b3cdc9b00cCalin Juravle    // Directories can't have a period directly after the directory markers to prevent "..".
1033c597b6dd895dbb2b28c757ce7a2651b3cdc9b00cCalin Juravle    if ((path[dir_len] == '.') || ((subdir != NULL) && (*subdir == '.'))) {
1034c597b6dd895dbb2b28c757ce7a2651b3cdc9b00cCalin Juravle        ALOGE("invalid apk path '%s' (trickery)\n", path);
1035c597b6dd895dbb2b28c757ce7a2651b3cdc9b00cCalin Juravle        return -1;
1036c597b6dd895dbb2b28c757ce7a2651b3cdc9b00cCalin Juravle    }
1037c597b6dd895dbb2b28c757ce7a2651b3cdc9b00cCalin Juravle
1038c597b6dd895dbb2b28c757ce7a2651b3cdc9b00cCalin Juravle    return 0;
1039c597b6dd895dbb2b28c757ce7a2651b3cdc9b00cCalin Juravle}
1040c597b6dd895dbb2b28c757ce7a2651b3cdc9b00cCalin Juravle
1041c597b6dd895dbb2b28c757ce7a2651b3cdc9b00cCalin Juravle/**
104294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood * Checks whether a path points to a system app (.apk file). Returns 0
104394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood * if it is a system app or -1 if it is not.
104494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood */
104594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwoodint validate_system_app_path(const char* path) {
104694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    size_t i;
104794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
104894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    for (i = 0; i < android_system_dirs.count; i++) {
104994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        const size_t dir_len = android_system_dirs.dirs[i].len;
105094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        if (!strncmp(path, android_system_dirs.dirs[i].path, dir_len)) {
1051e23a13299a4f6f2488935b4786cdbb46f46e3d3cJeff Sharkey            return validate_path(android_system_dirs.dirs + i, path, 1);
105294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        }
105394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
105494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
105594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    return -1;
105694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
105794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
105894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood/**
105994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood * Get the contents of a environment variable that contains a path. Caller
106094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood * owns the string that is inserted into the directory record. Returns
106194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood * 0 on success and -1 on error.
106294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood */
106394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwoodint get_path_from_env(dir_rec_t* rec, const char* var) {
106494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    const char* path = getenv(var);
106594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    int ret = get_path_from_string(rec, path);
106694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    if (ret < 0) {
106794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        ALOGW("Problem finding value for environment variable %s\n", var);
106894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
106994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    return ret;
107094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
107194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
107294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood/**
107394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood * Puts the string into the record as a directory. Appends '/' to the end
107494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood * of all paths. Caller owns the string that is inserted into the directory
107594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood * record. A null value will result in an error.
107694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood *
107794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood * Returns 0 on success and -1 on error.
107894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood */
107994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwoodint get_path_from_string(dir_rec_t* rec, const char* path) {
108094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    if (path == NULL) {
108194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        return -1;
108294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    } else {
108394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        const size_t path_len = strlen(path);
108494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        if (path_len <= 0) {
108594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            return -1;
108694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        }
108794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
108894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        // Make sure path is absolute.
108994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        if (path[0] != '/') {
109094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            return -1;
109194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        }
109294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
109394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        if (path[path_len - 1] == '/') {
109494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            // Path ends with a forward slash. Make our own copy.
109594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
109694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            rec->path = strdup(path);
109794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            if (rec->path == NULL) {
109894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                return -1;
109994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            }
110094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
110194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            rec->len = path_len;
110294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        } else {
110394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            // Path does not end with a slash. Generate a new string.
110494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            char *dst;
110594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
110694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            // Add space for slash and terminating null.
110794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            size_t dst_size = path_len + 2;
110894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
110919803807cd7ae01868fcfa50305f4a7dd13765e2Jeff Sharkey            rec->path = (char*) malloc(dst_size);
111094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            if (rec->path == NULL) {
111194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                return -1;
111294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            }
111394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
111494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            dst = rec->path;
111594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
111694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            if (append_and_increment(&dst, path, &dst_size) < 0
111794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                    || append_and_increment(&dst, "/", &dst_size)) {
111894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                ALOGE("Error canonicalizing path");
111994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                return -1;
112094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            }
112194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
112294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            rec->len = dst - rec->path;
112394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        }
112494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
112594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    return 0;
112694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
112794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
112894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwoodint copy_and_append(dir_rec_t* dst, const dir_rec_t* src, const char* suffix) {
112994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    dst->len = src->len + strlen(suffix);
113094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    const size_t dstSize = dst->len + 1;
113194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    dst->path = (char*) malloc(dstSize);
113294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
113394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    if (dst->path == NULL
113494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            || snprintf(dst->path, dstSize, "%s%s", src->path, suffix)
113594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                    != (ssize_t) dst->len) {
113694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        ALOGE("Could not allocate memory to hold appended path; aborting\n");
113794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        return -1;
113894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
113994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
114094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    return 0;
114194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
114294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
114394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood/**
1144d845c96128a40ca5802c0840ae190fa0af7d4735Narayan Kamath * Check whether path points to a valid path for an APK file. The path must
1145d845c96128a40ca5802c0840ae190fa0af7d4735Narayan Kamath * begin with a whitelisted prefix path and must be no deeper than |maxSubdirs| within
1146d845c96128a40ca5802c0840ae190fa0af7d4735Narayan Kamath * that path. Returns -1 when an invalid path is encountered and 0 when a valid path
1147d845c96128a40ca5802c0840ae190fa0af7d4735Narayan Kamath * is encountered.
114894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood */
1149d845c96128a40ca5802c0840ae190fa0af7d4735Narayan Kamathstatic int validate_apk_path_internal(const char *path, int maxSubdirs) {
1150c597b6dd895dbb2b28c757ce7a2651b3cdc9b00cCalin Juravle    const dir_rec_t* dir = NULL;
115194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    if (!strncmp(path, android_app_dir.path, android_app_dir.len)) {
1152c597b6dd895dbb2b28c757ce7a2651b3cdc9b00cCalin Juravle        dir = &android_app_dir;
115394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    } else if (!strncmp(path, android_app_private_dir.path, android_app_private_dir.len)) {
1154c597b6dd895dbb2b28c757ce7a2651b3cdc9b00cCalin Juravle        dir = &android_app_private_dir;
11555c1a910e6a2d2c42002dc6ed88ff770336afcb3fTodd Kennedy    } else if (!strncmp(path, android_app_ephemeral_dir.path, android_app_ephemeral_dir.len)) {
11565c1a910e6a2d2c42002dc6ed88ff770336afcb3fTodd Kennedy        dir = &android_app_ephemeral_dir;
115794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    } else if (!strncmp(path, android_asec_dir.path, android_asec_dir.len)) {
1158c597b6dd895dbb2b28c757ce7a2651b3cdc9b00cCalin Juravle        dir = &android_asec_dir;
1159e23a13299a4f6f2488935b4786cdbb46f46e3d3cJeff Sharkey    } else if (!strncmp(path, android_mnt_expand_dir.path, android_mnt_expand_dir.len)) {
1160e23a13299a4f6f2488935b4786cdbb46f46e3d3cJeff Sharkey        dir = &android_mnt_expand_dir;
1161d845c96128a40ca5802c0840ae190fa0af7d4735Narayan Kamath        if (maxSubdirs < 2) {
1162d845c96128a40ca5802c0840ae190fa0af7d4735Narayan Kamath            maxSubdirs = 2;
1163d845c96128a40ca5802c0840ae190fa0af7d4735Narayan Kamath        }
116494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    } else {
116594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        return -1;
116694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
116794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
1168e23a13299a4f6f2488935b4786cdbb46f46e3d3cJeff Sharkey    return validate_path(dir, path, maxSubdirs);
116994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
117094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
1171d845c96128a40ca5802c0840ae190fa0af7d4735Narayan Kamathint validate_apk_path(const char* path) {
1172d845c96128a40ca5802c0840ae190fa0af7d4735Narayan Kamath    return validate_apk_path_internal(path, 1 /* maxSubdirs */);
1173d845c96128a40ca5802c0840ae190fa0af7d4735Narayan Kamath}
1174d845c96128a40ca5802c0840ae190fa0af7d4735Narayan Kamath
1175d845c96128a40ca5802c0840ae190fa0af7d4735Narayan Kamathint validate_apk_path_subdirs(const char* path) {
1176d845c96128a40ca5802c0840ae190fa0af7d4735Narayan Kamath    return validate_apk_path_internal(path, 3 /* maxSubdirs */);
1177d845c96128a40ca5802c0840ae190fa0af7d4735Narayan Kamath}
1178d845c96128a40ca5802c0840ae190fa0af7d4735Narayan Kamath
117994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwoodint append_and_increment(char** dst, const char* src, size_t* dst_size) {
118094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    ssize_t ret = strlcpy(*dst, src, *dst_size);
118194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    if (ret < 0 || (size_t) ret >= *dst_size) {
118294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        return -1;
118394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
118494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    *dst += ret;
118594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    *dst_size -= ret;
118694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    return 0;
118794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
118894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
118919803807cd7ae01868fcfa50305f4a7dd13765e2Jeff Sharkeychar *build_string2(const char *s1, const char *s2) {
119094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    if (s1 == NULL || s2 == NULL) return NULL;
119194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
119294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    int len_s1 = strlen(s1);
119394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    int len_s2 = strlen(s2);
119494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    int len = len_s1 + len_s2 + 1;
119519803807cd7ae01868fcfa50305f4a7dd13765e2Jeff Sharkey    char *result = (char *) malloc(len);
119694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    if (result == NULL) return NULL;
119794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
119894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    strcpy(result, s1);
119994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    strcpy(result + len_s1, s2);
120094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
120194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    return result;
120294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
120394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
120419803807cd7ae01868fcfa50305f4a7dd13765e2Jeff Sharkeychar *build_string3(const char *s1, const char *s2, const char *s3) {
120594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    if (s1 == NULL || s2 == NULL || s3 == NULL) return NULL;
120694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
120794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    int len_s1 = strlen(s1);
120894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    int len_s2 = strlen(s2);
120994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    int len_s3 = strlen(s3);
121094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    int len = len_s1 + len_s2 + len_s3 + 1;
121119803807cd7ae01868fcfa50305f4a7dd13765e2Jeff Sharkey    char *result = (char *) malloc(len);
121294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    if (result == NULL) return NULL;
121394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
121494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    strcpy(result, s1);
121594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    strcpy(result + len_s1, s2);
121694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    strcpy(result + len_s1 + len_s2, s3);
121794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
121894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    return result;
121994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
122094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
1221095c763dd9aa26a206d10ab7c1d7e1c569298fb3Robin Leeint ensure_config_user_dirs(userid_t userid) {
1222095c763dd9aa26a206d10ab7c1d7e1c569298fb3Robin Lee    char config_user_path[PATH_MAX];
1223095c763dd9aa26a206d10ab7c1d7e1c569298fb3Robin Lee
1224095c763dd9aa26a206d10ab7c1d7e1c569298fb3Robin Lee    // writable by system, readable by any app within the same user
122560fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee    const int uid = multiuser_get_uid(userid, AID_SYSTEM);
122660fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee    const int gid = multiuser_get_uid(userid, AID_EVERYBODY);
1227095c763dd9aa26a206d10ab7c1d7e1c569298fb3Robin Lee
1228095c763dd9aa26a206d10ab7c1d7e1c569298fb3Robin Lee    // Ensure /data/misc/user/<userid> exists
1229095c763dd9aa26a206d10ab7c1d7e1c569298fb3Robin Lee    create_user_config_path(config_user_path, userid);
1230095c763dd9aa26a206d10ab7c1d7e1c569298fb3Robin Lee    if (fs_prepare_dir(config_user_path, 0750, uid, gid) == -1) {
1231095c763dd9aa26a206d10ab7c1d7e1c569298fb3Robin Lee        return -1;
1232095c763dd9aa26a206d10ab7c1d7e1c569298fb3Robin Lee    }
1233095c763dd9aa26a206d10ab7c1d7e1c569298fb3Robin Lee
1234095c763dd9aa26a206d10ab7c1d7e1c569298fb3Robin Lee   return 0;
1235095c763dd9aa26a206d10ab7c1d7e1c569298fb3Robin Lee}
123602d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe
123702d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampeint wait_child(pid_t pid)
123802d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe{
123902d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe    int status;
124002d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe    pid_t got_pid;
124102d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe
124202d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe    while (1) {
124302d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe        got_pid = waitpid(pid, &status, 0);
124402d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe        if (got_pid == -1 && errno == EINTR) {
124502d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe            printf("waitpid interrupted, retrying\n");
124602d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe        } else {
124702d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe            break;
124802d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe        }
124902d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe    }
125002d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe    if (got_pid != pid) {
125102d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe        ALOGW("waitpid failed: wanted %d, got %d: %s\n",
125202d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe            (int) pid, (int) got_pid, strerror(errno));
125302d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe        return 1;
125402d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe    }
125502d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe
125602d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe    if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
125702d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe        return 0;
125802d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe    } else {
125902d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe        return status;      /* always nonzero */
126002d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe    }
126102d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe}
126202d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe
126302d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe}  // namespace installd
126402d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe}  // namespace android
1265