utils.cpp revision ebf728fd43ab5c7d11a1f9e5fdc775d6740fae0a
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
1794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood#include "installd.h"
1894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
19c03de09173f98506e73e7cf7df21fe11795d4b24Jeff Sharkey#include <base/stringprintf.h>
20c03de09173f98506e73e7cf7df21fe11795d4b24Jeff Sharkey#include <base/logging.h>
2194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
22c03de09173f98506e73e7cf7df21fe11795d4b24Jeff Sharkey#define CACHE_NOISY(x) //x
2394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
24c03de09173f98506e73e7cf7df21fe11795d4b24Jeff Sharkeyusing android::base::StringPrintf;
2594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
2694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood/**
27c03de09173f98506e73e7cf7df21fe11795d4b24Jeff Sharkey * Check that given string is valid filename, and that it attempts no
28c03de09173f98506e73e7cf7df21fe11795d4b24Jeff Sharkey * parent or child directory traversal.
2994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood */
30c03de09173f98506e73e7cf7df21fe11795d4b24Jeff Sharkeystatic bool is_valid_filename(const std::string& name) {
31c03de09173f98506e73e7cf7df21fe11795d4b24Jeff Sharkey    if (name.empty() || (name == ".") || (name == "..")
32c03de09173f98506e73e7cf7df21fe11795d4b24Jeff Sharkey            || (name.find('/') != std::string::npos)) {
33c03de09173f98506e73e7cf7df21fe11795d4b24Jeff Sharkey        return false;
3494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    } else {
35c03de09173f98506e73e7cf7df21fe11795d4b24Jeff Sharkey        return true;
3694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
37c03de09173f98506e73e7cf7df21fe11795d4b24Jeff Sharkey}
3894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
39c03de09173f98506e73e7cf7df21fe11795d4b24Jeff Sharkey/**
40d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey * Create the path name where package app contents should be stored for
41d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey * the given volume UUID and package name.  An empty UUID is assumed to
42d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey * be internal storage.
43d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey */
44d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkeystd::string create_data_app_package_path(const char* volume_uuid,
45d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey        const char* package_name) {
46d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey    CHECK(is_valid_filename(package_name));
47d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey    CHECK(is_valid_package_name(package_name) == 0);
48d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey
49d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey    return StringPrintf("%s/%s",
50d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey            create_data_app_path(volume_uuid).c_str(), package_name);
51d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey}
52d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey
53d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey/**
54c03de09173f98506e73e7cf7df21fe11795d4b24Jeff Sharkey * Create the path name where package data should be stored for the given
55c03de09173f98506e73e7cf7df21fe11795d4b24Jeff Sharkey * volume UUID, package name, and user ID. An empty UUID is assumed to be
56c03de09173f98506e73e7cf7df21fe11795d4b24Jeff Sharkey * internal storage.
57c03de09173f98506e73e7cf7df21fe11795d4b24Jeff Sharkey */
58d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkeystd::string create_data_user_package_path(const char* volume_uuid,
59d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey        userid_t user, const char* package_name) {
60c03de09173f98506e73e7cf7df21fe11795d4b24Jeff Sharkey    CHECK(is_valid_filename(package_name));
61c03de09173f98506e73e7cf7df21fe11795d4b24Jeff Sharkey    CHECK(is_valid_package_name(package_name) == 0);
62c03de09173f98506e73e7cf7df21fe11795d4b24Jeff Sharkey
63d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey    return StringPrintf("%s/%s",
64d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey            create_data_user_path(volume_uuid, user).c_str(), package_name);
65c03de09173f98506e73e7cf7df21fe11795d4b24Jeff Sharkey}
6694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
6763ec2d64196144b2d15d2baffedccfa011d6494fJeff Sharkeystd::string create_data_user_de_package_path(const char* volume_uuid,
6863ec2d64196144b2d15d2baffedccfa011d6494fJeff Sharkey        userid_t user, const char* package_name) {
6963ec2d64196144b2d15d2baffedccfa011d6494fJeff Sharkey    CHECK(is_valid_filename(package_name));
7063ec2d64196144b2d15d2baffedccfa011d6494fJeff Sharkey    CHECK(is_valid_package_name(package_name) == 0);
7163ec2d64196144b2d15d2baffedccfa011d6494fJeff Sharkey
7263ec2d64196144b2d15d2baffedccfa011d6494fJeff Sharkey    return StringPrintf("%s/%s",
7363ec2d64196144b2d15d2baffedccfa011d6494fJeff Sharkey            create_data_user_de_path(volume_uuid, user).c_str(), package_name);
7463ec2d64196144b2d15d2baffedccfa011d6494fJeff Sharkey}
7563ec2d64196144b2d15d2baffedccfa011d6494fJeff Sharkey
76c03de09173f98506e73e7cf7df21fe11795d4b24Jeff Sharkeyint create_pkg_path(char path[PKG_PATH_MAX], const char *pkgname,
77c03de09173f98506e73e7cf7df21fe11795d4b24Jeff Sharkey        const char *postfix, userid_t userid) {
78c03de09173f98506e73e7cf7df21fe11795d4b24Jeff Sharkey    if (is_valid_package_name(pkgname) != 0) {
79c03de09173f98506e73e7cf7df21fe11795d4b24Jeff Sharkey        path[0] = '\0';
8094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        return -1;
8194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
8294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
83d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey    std::string _tmp(create_data_user_package_path(nullptr, userid, pkgname) + postfix);
84c03de09173f98506e73e7cf7df21fe11795d4b24Jeff Sharkey    const char* tmp = _tmp.c_str();
85c03de09173f98506e73e7cf7df21fe11795d4b24Jeff Sharkey    if (strlen(tmp) >= PKG_PATH_MAX) {
86c03de09173f98506e73e7cf7df21fe11795d4b24Jeff Sharkey        path[0] = '\0';
87c03de09173f98506e73e7cf7df21fe11795d4b24Jeff Sharkey        return -1;
88c03de09173f98506e73e7cf7df21fe11795d4b24Jeff Sharkey    } else {
89c03de09173f98506e73e7cf7df21fe11795d4b24Jeff Sharkey        strcpy(path, tmp);
90c03de09173f98506e73e7cf7df21fe11795d4b24Jeff Sharkey        return 0;
9194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
9294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
9394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
9441ea424413c0381ef2aa15fc5bd5d4b88abd23c4Jeff Sharkeystd::string create_data_path(const char* volume_uuid) {
9541ea424413c0381ef2aa15fc5bd5d4b88abd23c4Jeff Sharkey    if (volume_uuid == nullptr) {
9641ea424413c0381ef2aa15fc5bd5d4b88abd23c4Jeff Sharkey        return "/data";
9794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    } else {
9841ea424413c0381ef2aa15fc5bd5d4b88abd23c4Jeff Sharkey        CHECK(is_valid_filename(volume_uuid));
9941ea424413c0381ef2aa15fc5bd5d4b88abd23c4Jeff Sharkey        return StringPrintf("/mnt/expand/%s", volume_uuid);
10094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
10141ea424413c0381ef2aa15fc5bd5d4b88abd23c4Jeff Sharkey}
10294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
10341ea424413c0381ef2aa15fc5bd5d4b88abd23c4Jeff Sharkey/**
104d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey * Create the path name for app data.
105d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey */
106d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkeystd::string create_data_app_path(const char* volume_uuid) {
107d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey    return StringPrintf("%s/app", create_data_path(volume_uuid).c_str());
108d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey}
109d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey
110d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey/**
11141ea424413c0381ef2aa15fc5bd5d4b88abd23c4Jeff Sharkey * Create the path name for user data for a certain userid.
11241ea424413c0381ef2aa15fc5bd5d4b88abd23c4Jeff Sharkey */
11341ea424413c0381ef2aa15fc5bd5d4b88abd23c4Jeff Sharkeystd::string create_data_user_path(const char* volume_uuid, userid_t userid) {
11441ea424413c0381ef2aa15fc5bd5d4b88abd23c4Jeff Sharkey    std::string data(create_data_path(volume_uuid));
11541ea424413c0381ef2aa15fc5bd5d4b88abd23c4Jeff Sharkey    if (volume_uuid == nullptr) {
11641ea424413c0381ef2aa15fc5bd5d4b88abd23c4Jeff Sharkey        if (userid == 0) {
11741ea424413c0381ef2aa15fc5bd5d4b88abd23c4Jeff Sharkey            return StringPrintf("%s/data", data.c_str());
11841ea424413c0381ef2aa15fc5bd5d4b88abd23c4Jeff Sharkey        } else {
11941ea424413c0381ef2aa15fc5bd5d4b88abd23c4Jeff Sharkey            return StringPrintf("%s/user/%u", data.c_str(), userid);
12094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        }
12141ea424413c0381ef2aa15fc5bd5d4b88abd23c4Jeff Sharkey    } else {
12241ea424413c0381ef2aa15fc5bd5d4b88abd23c4Jeff Sharkey        return StringPrintf("%s/user/%u", data.c_str(), userid);
12394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
12494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
12594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
12694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood/**
12763ec2d64196144b2d15d2baffedccfa011d6494fJeff Sharkey * Create the path name for device encrypted user data for a certain userid.
12863ec2d64196144b2d15d2baffedccfa011d6494fJeff Sharkey */
12963ec2d64196144b2d15d2baffedccfa011d6494fJeff Sharkeystd::string create_data_user_de_path(const char* volume_uuid, userid_t userid) {
13063ec2d64196144b2d15d2baffedccfa011d6494fJeff Sharkey    std::string data(create_data_path(volume_uuid));
13163ec2d64196144b2d15d2baffedccfa011d6494fJeff Sharkey    return StringPrintf("%s/user_de/%u", data.c_str(), userid);
13263ec2d64196144b2d15d2baffedccfa011d6494fJeff Sharkey}
13363ec2d64196144b2d15d2baffedccfa011d6494fJeff Sharkey
13463ec2d64196144b2d15d2baffedccfa011d6494fJeff Sharkey/**
135abe4fe5b46157ecd2a52d28abf938c816c3ce878Jeff Sharkey * Create the path name for media for a certain userid.
13694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood */
13741ea424413c0381ef2aa15fc5bd5d4b88abd23c4Jeff Sharkeystd::string create_data_media_path(const char* volume_uuid, userid_t userid) {
13841ea424413c0381ef2aa15fc5bd5d4b88abd23c4Jeff Sharkey    return StringPrintf("%s/media/%u", create_data_path(volume_uuid).c_str(), userid);
13994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
14094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
141e36372423000a906bafae68844ebc6c42d09335aJeff Sharkeystd::vector<userid_t> get_known_users(const char* volume_uuid) {
142e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey    std::vector<userid_t> users;
143e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey
144e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey    // We always have an owner
145e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey    users.push_back(0);
146e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey
147e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey    std::string path(create_data_path(volume_uuid) + "/" + SECONDARY_USER_PREFIX);
148e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey    DIR* dir = opendir(path.c_str());
149e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey    if (dir == NULL) {
150e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey        // Unable to discover other users, but at least return owner
151e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey        PLOG(ERROR) << "Failed to opendir " << path;
152e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey        return users;
153e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey    }
154e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey
155e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey    struct dirent* ent;
156e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey    while ((ent = readdir(dir))) {
157e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey        if (ent->d_type != DT_DIR) {
158e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey            continue;
159e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey        }
160e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey
161e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey        char* end;
162e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey        userid_t user = strtol(ent->d_name, &end, 10);
163e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey        if (*end == '\0' && user != 0) {
164e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey            LOG(DEBUG) << "Found valid user " << user;
165e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey            users.push_back(user);
166e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey        }
167e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey    }
168e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey    closedir(dir);
169e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey
170e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey    return users;
171e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey}
172e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey
173095c763dd9aa26a206d10ab7c1d7e1c569298fb3Robin Lee/**
174095c763dd9aa26a206d10ab7c1d7e1c569298fb3Robin Lee * Create the path name for config for a certain userid.
175095c763dd9aa26a206d10ab7c1d7e1c569298fb3Robin Lee * Returns 0 on success, and -1 on failure.
176095c763dd9aa26a206d10ab7c1d7e1c569298fb3Robin Lee */
177095c763dd9aa26a206d10ab7c1d7e1c569298fb3Robin Leeint create_user_config_path(char path[PATH_MAX], userid_t userid) {
178095c763dd9aa26a206d10ab7c1d7e1c569298fb3Robin Lee    if (snprintf(path, PATH_MAX, "%s%d", "/data/misc/user/", userid) > PATH_MAX) {
179095c763dd9aa26a206d10ab7c1d7e1c569298fb3Robin Lee        return -1;
180095c763dd9aa26a206d10ab7c1d7e1c569298fb3Robin Lee    }
181095c763dd9aa26a206d10ab7c1d7e1c569298fb3Robin Lee    return 0;
182095c763dd9aa26a206d10ab7c1d7e1c569298fb3Robin Lee}
183095c763dd9aa26a206d10ab7c1d7e1c569298fb3Robin Lee
18494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwoodint create_move_path(char path[PKG_PATH_MAX],
18594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    const char* pkgname,
18694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    const char* leaf,
18799d9fb15b4a1eb534261ae81b2cf25aec4447bd0Chih-Hung Hsieh    userid_t userid __unused)
18894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood{
18994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    if ((android_data_dir.len + strlen(PRIMARY_USER_PREFIX) + strlen(pkgname) + strlen(leaf) + 1)
19094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            >= PKG_PATH_MAX) {
19194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        return -1;
19294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
19394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
19494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    sprintf(path, "%s%s%s/%s", android_data_dir.path, PRIMARY_USER_PREFIX, pkgname, leaf);
19594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    return 0;
19694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
19794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
19894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood/**
19994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood * Checks whether the package name is valid. Returns -1 on error and
20094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood * 0 on success.
20194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood */
20294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwoodint is_valid_package_name(const char* pkgname) {
20394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    const char *x = pkgname;
20494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    int alpha = -1;
20594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
206c03de09173f98506e73e7cf7df21fe11795d4b24Jeff Sharkey    if (strlen(pkgname) > PKG_NAME_MAX) {
207c03de09173f98506e73e7cf7df21fe11795d4b24Jeff Sharkey        return -1;
208c03de09173f98506e73e7cf7df21fe11795d4b24Jeff Sharkey    }
209c03de09173f98506e73e7cf7df21fe11795d4b24Jeff Sharkey
21094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    while (*x) {
21194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        if (isalnum(*x) || (*x == '_')) {
21294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                /* alphanumeric or underscore are fine */
21394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        } else if (*x == '.') {
21494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            if ((x == pkgname) || (x[1] == '.') || (x[1] == 0)) {
21594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                    /* periods must not be first, last, or doubled */
21694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                ALOGE("invalid package name '%s'\n", pkgname);
21794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                return -1;
21894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            }
21994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        } else if (*x == '-') {
22094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            /* Suffix -X is fine to let versioning of packages.
22194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood               But whatever follows should be alphanumeric.*/
22294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            alpha = 1;
22394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        } else {
22494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                /* anything not A-Z, a-z, 0-9, _, or . is invalid */
22594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            ALOGE("invalid package name '%s'\n", pkgname);
22694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            return -1;
22794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        }
22894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
22994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        x++;
23094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
23194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
23294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    if (alpha == 1) {
23394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        // Skip current character
23494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        x++;
23594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        while (*x) {
23694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            if (!isalnum(*x)) {
23794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                ALOGE("invalid package name '%s' should include only numbers after -\n", pkgname);
23894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                return -1;
23994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            }
24094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            x++;
24194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        }
24294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
24394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
24494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    return 0;
24594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
24694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
2473aee2c5c749dc2589f001b26fae1ec958ec89524Narayan Kamathstatic int _delete_dir_contents(DIR *d,
2483aee2c5c749dc2589f001b26fae1ec958ec89524Narayan Kamath                                int (*exclusion_predicate)(const char *name, const int is_dir))
24994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood{
25094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    int result = 0;
25194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    struct dirent *de;
25294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    int dfd;
25394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
25494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    dfd = dirfd(d);
25594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
25694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    if (dfd < 0) return -1;
25794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
25894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    while ((de = readdir(d))) {
25994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        const char *name = de->d_name;
26094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
2613aee2c5c749dc2589f001b26fae1ec958ec89524Narayan Kamath            /* check using the exclusion predicate, if provided */
2623aee2c5c749dc2589f001b26fae1ec958ec89524Narayan Kamath        if (exclusion_predicate && exclusion_predicate(name, (de->d_type == DT_DIR))) {
2633aee2c5c749dc2589f001b26fae1ec958ec89524Narayan Kamath            continue;
2643aee2c5c749dc2589f001b26fae1ec958ec89524Narayan Kamath        }
26594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
26694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        if (de->d_type == DT_DIR) {
26799d9fb15b4a1eb534261ae81b2cf25aec4447bd0Chih-Hung Hsieh            int subfd;
26894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            DIR *subdir;
26994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
27094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                /* always skip "." and ".." */
27194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            if (name[0] == '.') {
27294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                if (name[1] == 0) continue;
27394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                if ((name[1] == '.') && (name[2] == 0)) continue;
27494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            }
27594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
2768b7acacc93930df9fa9e1ebea4a4394195b2332eNick Kralevich            subfd = openat(dfd, name, O_RDONLY | O_DIRECTORY | O_NOFOLLOW | O_CLOEXEC);
27794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            if (subfd < 0) {
27894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                ALOGE("Couldn't openat %s: %s\n", name, strerror(errno));
27994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                result = -1;
28094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                continue;
28194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            }
28294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            subdir = fdopendir(subfd);
28394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            if (subdir == NULL) {
28494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                ALOGE("Couldn't fdopendir %s: %s\n", name, strerror(errno));
28594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                close(subfd);
28694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                result = -1;
28794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                continue;
28894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            }
2893aee2c5c749dc2589f001b26fae1ec958ec89524Narayan Kamath            if (_delete_dir_contents(subdir, exclusion_predicate)) {
29094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                result = -1;
29194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            }
29294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            closedir(subdir);
29394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            if (unlinkat(dfd, name, AT_REMOVEDIR) < 0) {
29494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                ALOGE("Couldn't unlinkat %s: %s\n", name, strerror(errno));
29594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                result = -1;
29694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            }
29794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        } else {
29894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            if (unlinkat(dfd, name, 0) < 0) {
29994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                ALOGE("Couldn't unlinkat %s: %s\n", name, strerror(errno));
30094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                result = -1;
30194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            }
30294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        }
30394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
30494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
30594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    return result;
30694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
30794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
308ebf728fd43ab5c7d11a1f9e5fdc775d6740fae0aJeff Sharkeyint delete_dir_contents(const std::string& pathname) {
309ebf728fd43ab5c7d11a1f9e5fdc775d6740fae0aJeff Sharkey    return delete_dir_contents(pathname.c_str(), 0, NULL);
310ebf728fd43ab5c7d11a1f9e5fdc775d6740fae0aJeff Sharkey}
311ebf728fd43ab5c7d11a1f9e5fdc775d6740fae0aJeff Sharkey
312ebf728fd43ab5c7d11a1f9e5fdc775d6740fae0aJeff Sharkeyint delete_dir_contents_and_dir(const std::string& pathname) {
313ebf728fd43ab5c7d11a1f9e5fdc775d6740fae0aJeff Sharkey    return delete_dir_contents(pathname.c_str(), 1, NULL);
314ebf728fd43ab5c7d11a1f9e5fdc775d6740fae0aJeff Sharkey}
315ebf728fd43ab5c7d11a1f9e5fdc775d6740fae0aJeff Sharkey
31694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwoodint delete_dir_contents(const char *pathname,
31794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                        int also_delete_dir,
3183aee2c5c749dc2589f001b26fae1ec958ec89524Narayan Kamath                        int (*exclusion_predicate)(const char*, const int))
31994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood{
32094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    int res = 0;
32194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    DIR *d;
32294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
32394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    d = opendir(pathname);
32494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    if (d == NULL) {
32594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        ALOGE("Couldn't opendir %s: %s\n", pathname, strerror(errno));
32694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        return -errno;
32794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
3283aee2c5c749dc2589f001b26fae1ec958ec89524Narayan Kamath    res = _delete_dir_contents(d, exclusion_predicate);
32994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    closedir(d);
33094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    if (also_delete_dir) {
33194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        if (rmdir(pathname)) {
33294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            ALOGE("Couldn't rmdir %s: %s\n", pathname, strerror(errno));
33394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            res = -1;
33494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        }
33594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
33694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    return res;
33794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
33894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
33994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwoodint delete_dir_contents_fd(int dfd, const char *name)
34094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood{
34194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    int fd, res;
34294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    DIR *d;
34394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
3448b7acacc93930df9fa9e1ebea4a4394195b2332eNick Kralevich    fd = openat(dfd, name, O_RDONLY | O_DIRECTORY | O_NOFOLLOW | O_CLOEXEC);
34594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    if (fd < 0) {
34694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        ALOGE("Couldn't openat %s: %s\n", name, strerror(errno));
34794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        return -1;
34894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
34994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    d = fdopendir(fd);
35094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    if (d == NULL) {
35194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        ALOGE("Couldn't fdopendir %s: %s\n", name, strerror(errno));
35294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        close(fd);
35394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        return -1;
35494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
35594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    res = _delete_dir_contents(d, 0);
35694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    closedir(d);
35794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    return res;
35894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
35994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
36060fd3feecab4336d964ca8e31c7c3220e1afb558Robin Leestatic int _copy_owner_permissions(int srcfd, int dstfd)
36160fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee{
36260fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee    struct stat st;
36360fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee    if (fstat(srcfd, &st) != 0) {
36460fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee        return -1;
36560fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee    }
36660fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee    if (fchmod(dstfd, st.st_mode) != 0) {
36760fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee        return -1;
36860fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee    }
36960fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee    return 0;
37060fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee}
37160fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee
37260fd3feecab4336d964ca8e31c7c3220e1afb558Robin Leestatic int _copy_dir_files(int sdfd, int ddfd, uid_t owner, gid_t group)
37360fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee{
37460fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee    int result = 0;
37560fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee    if (_copy_owner_permissions(sdfd, ddfd) != 0) {
37660fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee        ALOGE("_copy_dir_files failed to copy dir permissions\n");
37760fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee    }
37860fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee    if (fchown(ddfd, owner, group) != 0) {
37960fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee        ALOGE("_copy_dir_files failed to change dir owner\n");
38060fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee    }
38160fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee
38260fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee    DIR *ds = fdopendir(sdfd);
38360fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee    if (ds == NULL) {
38460fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee        ALOGE("Couldn't fdopendir: %s\n", strerror(errno));
38560fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee        return -1;
38660fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee    }
38760fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee    struct dirent *de;
38860fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee    while ((de = readdir(ds))) {
38960fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee        if (de->d_type != DT_REG) {
39060fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee            continue;
39160fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee        }
39260fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee
39360fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee        const char *name = de->d_name;
39460fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee        int fsfd = openat(sdfd, name, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
39560fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee        int fdfd = openat(ddfd, name, O_WRONLY | O_NOFOLLOW | O_CLOEXEC | O_CREAT, 0600);
39660fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee        if (fsfd == -1 || fdfd == -1) {
39760fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee            ALOGW("Couldn't copy %s: %s\n", name, strerror(errno));
39860fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee        } else {
39960fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee            if (_copy_owner_permissions(fsfd, fdfd) != 0) {
40060fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee                ALOGE("Failed to change file permissions\n");
40160fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee            }
40260fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee            if (fchown(fdfd, owner, group) != 0) {
40360fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee                ALOGE("Failed to change file owner\n");
40460fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee            }
40560fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee
40660fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee            char buf[8192];
40760fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee            ssize_t size;
40860fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee            while ((size = read(fsfd, buf, sizeof(buf))) > 0) {
40960fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee                write(fdfd, buf, size);
41060fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee            }
41160fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee            if (size < 0) {
41260fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee                ALOGW("Couldn't copy %s: %s\n", name, strerror(errno));
41360fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee                result = -1;
41460fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee            }
41560fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee        }
41660fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee        close(fdfd);
41760fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee        close(fsfd);
41860fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee    }
41960fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee
42060fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee    return result;
42160fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee}
42260fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee
42360fd3feecab4336d964ca8e31c7c3220e1afb558Robin Leeint copy_dir_files(const char *srcname,
42460fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee                   const char *dstname,
42560fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee                   uid_t owner,
42660fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee                   uid_t group)
42760fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee{
42860fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee    int res = 0;
42960fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee    DIR *ds = NULL;
43060fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee    DIR *dd = NULL;
43160fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee
43260fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee    ds = opendir(srcname);
43360fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee    if (ds == NULL) {
43460fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee        ALOGE("Couldn't opendir %s: %s\n", srcname, strerror(errno));
43560fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee        return -errno;
43660fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee    }
43760fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee
43860fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee    mkdir(dstname, 0600);
43960fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee    dd = opendir(dstname);
44060fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee    if (dd == NULL) {
44160fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee        ALOGE("Couldn't opendir %s: %s\n", dstname, strerror(errno));
44260fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee        closedir(ds);
44360fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee        return -errno;
44460fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee    }
44560fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee
44660fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee    int sdfd = dirfd(ds);
44760fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee    int ddfd = dirfd(dd);
44860fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee    if (sdfd != -1 && ddfd != -1) {
44960fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee        res = _copy_dir_files(sdfd, ddfd, owner, group);
45060fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee    } else {
45160fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee        res = -errno;
45260fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee    }
45360fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee    closedir(dd);
45460fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee    closedir(ds);
45560fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee    return res;
45660fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee}
45760fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee
45894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwoodint lookup_media_dir(char basepath[PATH_MAX], const char *dir)
45994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood{
46094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    DIR *d;
46194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    struct dirent *de;
46294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    struct stat s;
46394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    char* dirpos = basepath + strlen(basepath);
46494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
46594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    if ((*(dirpos-1)) != '/') {
46694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        *dirpos = '/';
46794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        dirpos++;
46894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
46994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
47094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    CACHE_NOISY(ALOGI("Looking up %s in %s\n", dir, basepath));
47194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    // Verify the path won't extend beyond our buffer, to avoid
47294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    // repeated checking later.
47394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    if ((dirpos-basepath+strlen(dir)) >= (PATH_MAX-1)) {
47494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        ALOGW("Path exceeds limit: %s%s", basepath, dir);
47594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        return -1;
47694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
47794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
47894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    // First, can we find this directory with the case that is given?
47994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    strcpy(dirpos, dir);
48094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    if (stat(basepath, &s) >= 0) {
48194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        CACHE_NOISY(ALOGI("Found direct: %s\n", basepath));
48294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        return 0;
48394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
48494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
48594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    // Not found with that case...  search through all entries to find
48694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    // one that matches regardless of case.
48794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    *dirpos = 0;
48894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
48994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    d = opendir(basepath);
49094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    if (d == NULL) {
49194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        return -1;
49294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
49394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
49494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    while ((de = readdir(d))) {
49594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        if (strcasecmp(de->d_name, dir) == 0) {
49694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            strcpy(dirpos, de->d_name);
49794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            closedir(d);
49894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            CACHE_NOISY(ALOGI("Found search: %s\n", basepath));
49994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            return 0;
50094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        }
50194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
50294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
50394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    ALOGW("Couldn't find %s in %s", dir, basepath);
50494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    closedir(d);
50594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    return -1;
50694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
50794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
50841ea424413c0381ef2aa15fc5bd5d4b88abd23c4Jeff Sharkeyint64_t data_disk_free(const std::string& data_path)
50994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood{
51094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    struct statfs sfs;
51141ea424413c0381ef2aa15fc5bd5d4b88abd23c4Jeff Sharkey    if (statfs(data_path.c_str(), &sfs) == 0) {
51294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        return sfs.f_bavail * sfs.f_bsize;
51394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    } else {
51441ea424413c0381ef2aa15fc5bd5d4b88abd23c4Jeff Sharkey        PLOG(ERROR) << "Couldn't statfs " << data_path;
51594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        return -1;
51694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
51794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
51894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
51994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwoodcache_t* start_cache_collection()
52094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood{
52194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    cache_t* cache = (cache_t*)calloc(1, sizeof(cache_t));
52294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    return cache;
52394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
52494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
52594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood#define CACHE_BLOCK_SIZE (512*1024)
52694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
52794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwoodstatic void* _cache_malloc(cache_t* cache, size_t len)
52894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood{
52994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    len = (len+3)&~3;
53094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    if (len > (CACHE_BLOCK_SIZE/2)) {
53194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        // It doesn't make sense to try to put this allocation into one
53294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        // of our blocks, because it is so big.  Instead, make a new dedicated
53394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        // block for it.
53494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        int8_t* res = (int8_t*)malloc(len+sizeof(void*));
53594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        if (res == NULL) {
53694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            return NULL;
53794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        }
53894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        CACHE_NOISY(ALOGI("Allocated large cache mem block: %p size %d", res, len));
53994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        // Link it into our list of blocks, not disrupting the current one.
54094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        if (cache->memBlocks == NULL) {
54194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            *(void**)res = NULL;
54294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            cache->memBlocks = res;
54394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        } else {
54494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            *(void**)res = *(void**)cache->memBlocks;
54594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            *(void**)cache->memBlocks = res;
54694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        }
54794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        return res + sizeof(void*);
54894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
54994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    int8_t* res = cache->curMemBlockAvail;
55094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    int8_t* nextPos = res + len;
55194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    if (cache->memBlocks == NULL || nextPos > cache->curMemBlockEnd) {
55219803807cd7ae01868fcfa50305f4a7dd13765e2Jeff Sharkey        int8_t* newBlock = (int8_t*) malloc(CACHE_BLOCK_SIZE);
55394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        if (newBlock == NULL) {
55494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            return NULL;
55594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        }
55694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        CACHE_NOISY(ALOGI("Allocated new cache mem block: %p", newBlock));
55794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        *(void**)newBlock = cache->memBlocks;
55894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        cache->memBlocks = newBlock;
55994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        res = cache->curMemBlockAvail = newBlock + sizeof(void*);
56094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        cache->curMemBlockEnd = newBlock + CACHE_BLOCK_SIZE;
56194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        nextPos = res + len;
56294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
56394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    CACHE_NOISY(ALOGI("cache_malloc: ret %p size %d, block=%p, nextPos=%p",
56494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            res, len, cache->memBlocks, nextPos));
56594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    cache->curMemBlockAvail = nextPos;
56694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    return res;
56794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
56894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
56994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwoodstatic void* _cache_realloc(cache_t* cache, void* cur, size_t origLen, size_t len)
57094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood{
57194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    // This isn't really a realloc, but it is good enough for our purposes here.
57294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    void* alloc = _cache_malloc(cache, len);
57394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    if (alloc != NULL && cur != NULL) {
57494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        memcpy(alloc, cur, origLen < len ? origLen : len);
57594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
57694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    return alloc;
57794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
57894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
57994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwoodstatic void _inc_num_cache_collected(cache_t* cache)
58094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood{
58194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    cache->numCollected++;
58294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    if ((cache->numCollected%20000) == 0) {
58392dc3fc52cf097bd105460cf377779bdcf146d62Mark Salyzyn        ALOGI("Collected cache so far: %zd directories, %zd files",
58494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            cache->numDirs, cache->numFiles);
58594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
58694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
58794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
58894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwoodstatic cache_dir_t* _add_cache_dir_t(cache_t* cache, cache_dir_t* parent, const char *name)
58994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood{
59094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    size_t nameLen = strlen(name);
59194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    cache_dir_t* dir = (cache_dir_t*)_cache_malloc(cache, sizeof(cache_dir_t)+nameLen+1);
59294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    if (dir != NULL) {
59394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        dir->parent = parent;
59494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        dir->childCount = 0;
59594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        dir->hiddenCount = 0;
59694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        dir->deleted = 0;
59794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        strcpy(dir->name, name);
59894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        if (cache->numDirs >= cache->availDirs) {
59994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            size_t newAvail = cache->availDirs < 1000 ? 1000 : cache->availDirs*2;
60094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            cache_dir_t** newDirs = (cache_dir_t**)_cache_realloc(cache, cache->dirs,
60194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                    cache->availDirs*sizeof(cache_dir_t*), newAvail*sizeof(cache_dir_t*));
60294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            if (newDirs == NULL) {
60394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                ALOGE("Failure growing cache dirs array for %s\n", name);
60494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                return NULL;
60594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            }
60694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            cache->availDirs = newAvail;
60794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            cache->dirs = newDirs;
60894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        }
60994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        cache->dirs[cache->numDirs] = dir;
61094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        cache->numDirs++;
61194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        if (parent != NULL) {
61294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            parent->childCount++;
61394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        }
61494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        _inc_num_cache_collected(cache);
61594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    } else {
61694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        ALOGE("Failure allocating cache_dir_t for %s\n", name);
61794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
61894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    return dir;
61994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
62094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
62194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwoodstatic cache_file_t* _add_cache_file_t(cache_t* cache, cache_dir_t* dir, time_t modTime,
62294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        const char *name)
62394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood{
62494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    size_t nameLen = strlen(name);
62594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    cache_file_t* file = (cache_file_t*)_cache_malloc(cache, sizeof(cache_file_t)+nameLen+1);
62694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    if (file != NULL) {
62794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        file->dir = dir;
62894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        file->modTime = modTime;
62994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        strcpy(file->name, name);
63094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        if (cache->numFiles >= cache->availFiles) {
63194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            size_t newAvail = cache->availFiles < 1000 ? 1000 : cache->availFiles*2;
63294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            cache_file_t** newFiles = (cache_file_t**)_cache_realloc(cache, cache->files,
63394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                    cache->availFiles*sizeof(cache_file_t*), newAvail*sizeof(cache_file_t*));
63494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            if (newFiles == NULL) {
63594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                ALOGE("Failure growing cache file array for %s\n", name);
63694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                return NULL;
63794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            }
63894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            cache->availFiles = newAvail;
63994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            cache->files = newFiles;
64094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        }
64194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        CACHE_NOISY(ALOGI("Setting file %p at position %d in array %p", file,
64294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                cache->numFiles, cache->files));
64394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        cache->files[cache->numFiles] = file;
64494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        cache->numFiles++;
64594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        dir->childCount++;
64694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        _inc_num_cache_collected(cache);
64794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    } else {
64894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        ALOGE("Failure allocating cache_file_t for %s\n", name);
64994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
65094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    return file;
65194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
65294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
65394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwoodstatic int _add_cache_files(cache_t *cache, cache_dir_t *parentDir, const char *dirName,
65494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        DIR* dir, char *pathBase, char *pathPos, size_t pathAvailLen)
65594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood{
65694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    struct dirent *de;
65794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    cache_dir_t* cacheDir = NULL;
65894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    int dfd;
65994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
66094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    CACHE_NOISY(ALOGI("_add_cache_files: parent=%p dirName=%s dir=%p pathBase=%s",
66194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            parentDir, dirName, dir, pathBase));
66294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
66394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    dfd = dirfd(dir);
66494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
66594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    if (dfd < 0) return 0;
66694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
66794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    // Sub-directories always get added to the data structure, so if they
66894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    // are empty we will know about them to delete them later.
66994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    cacheDir = _add_cache_dir_t(cache, parentDir, dirName);
67094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
67194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    while ((de = readdir(dir))) {
67294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        const char *name = de->d_name;
67394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
67494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        if (de->d_type == DT_DIR) {
67594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            int subfd;
67694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            DIR *subdir;
67794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
67894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                /* always skip "." and ".." */
67994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            if (name[0] == '.') {
68094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                if (name[1] == 0) continue;
68194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                if ((name[1] == '.') && (name[2] == 0)) continue;
68294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            }
68394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
6848b7acacc93930df9fa9e1ebea4a4394195b2332eNick Kralevich            subfd = openat(dfd, name, O_RDONLY | O_DIRECTORY | O_NOFOLLOW | O_CLOEXEC);
68594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            if (subfd < 0) {
68694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                ALOGE("Couldn't openat %s: %s\n", name, strerror(errno));
68794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                continue;
68894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            }
68994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            subdir = fdopendir(subfd);
69094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            if (subdir == NULL) {
69194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                ALOGE("Couldn't fdopendir %s: %s\n", name, strerror(errno));
69294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                close(subfd);
69394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                continue;
69494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            }
69594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            if (cacheDir == NULL) {
69694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                cacheDir = _add_cache_dir_t(cache, parentDir, dirName);
69794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            }
69894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            if (cacheDir != NULL) {
69994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                // Update pathBase for the new path...  this may change dirName
70094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                // if that is also pointing to the path, but we are done with it
70194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                // now.
70294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                size_t finallen = snprintf(pathPos, pathAvailLen, "/%s", name);
70394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                CACHE_NOISY(ALOGI("Collecting dir %s\n", pathBase));
70494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                if (finallen < pathAvailLen) {
70594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                    _add_cache_files(cache, cacheDir, name, subdir, pathBase,
70694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                            pathPos+finallen, pathAvailLen-finallen);
70794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                } else {
70894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                    // Whoops, the final path is too long!  We'll just delete
70994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                    // this directory.
71094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                    ALOGW("Cache dir %s truncated in path %s; deleting dir\n",
71194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                            name, pathBase);
71294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                    _delete_dir_contents(subdir, NULL);
71394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                    if (unlinkat(dfd, name, AT_REMOVEDIR) < 0) {
71494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                        ALOGE("Couldn't unlinkat %s: %s\n", name, strerror(errno));
71594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                    }
71694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                }
71794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            }
71894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            closedir(subdir);
71994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        } else if (de->d_type == DT_REG) {
72094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            // Skip files that start with '.'; they will be deleted if
72194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            // their entire directory is deleted.  This allows for metadata
72294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            // like ".nomedia" to remain in the directory until the entire
72394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            // directory is deleted.
72494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            if (cacheDir == NULL) {
72594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                cacheDir = _add_cache_dir_t(cache, parentDir, dirName);
72694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            }
72794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            if (name[0] == '.') {
72894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                cacheDir->hiddenCount++;
72994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                continue;
73094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            }
73194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            if (cacheDir != NULL) {
73294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                // Build final full path for file...  this may change dirName
73394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                // if that is also pointing to the path, but we are done with it
73494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                // now.
73594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                size_t finallen = snprintf(pathPos, pathAvailLen, "/%s", name);
73694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                CACHE_NOISY(ALOGI("Collecting file %s\n", pathBase));
73794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                if (finallen < pathAvailLen) {
73894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                    struct stat s;
73994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                    if (stat(pathBase, &s) >= 0) {
74094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                        _add_cache_file_t(cache, cacheDir, s.st_mtime, name);
74194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                    } else {
74294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                        ALOGW("Unable to stat cache file %s; deleting\n", pathBase);
74394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                        if (unlink(pathBase) < 0) {
74494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                            ALOGE("Couldn't unlink %s: %s\n", pathBase, strerror(errno));
74594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                        }
74694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                    }
74794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                } else {
74894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                    // Whoops, the final path is too long!  We'll just delete
74994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                    // this file.
75094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                    ALOGW("Cache file %s truncated in path %s; deleting\n",
75194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                            name, pathBase);
75294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                    if (unlinkat(dfd, name, 0) < 0) {
75394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                        *pathPos = 0;
75494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                        ALOGE("Couldn't unlinkat %s in %s: %s\n", name, pathBase,
75594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                                strerror(errno));
75694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                    }
75794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                }
75894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            }
75994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        } else {
76094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            cacheDir->hiddenCount++;
76194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        }
76294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
76394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    return 0;
76494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
76594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
76694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwoodvoid add_cache_files(cache_t* cache, const char *basepath, const char *cachedir)
76794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood{
76894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    DIR *d;
76994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    struct dirent *de;
77094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    char dirname[PATH_MAX];
77194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
77294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    CACHE_NOISY(ALOGI("add_cache_files: base=%s cachedir=%s\n", basepath, cachedir));
77394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
77494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    d = opendir(basepath);
77594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    if (d == NULL) {
77694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        return;
77794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
77894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
77994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    while ((de = readdir(d))) {
78094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        if (de->d_type == DT_DIR) {
78194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            DIR* subdir;
78294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            const char *name = de->d_name;
78394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            char* pathpos;
78494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
78594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                /* always skip "." and ".." */
78694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            if (name[0] == '.') {
78794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                if (name[1] == 0) continue;
78894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                if ((name[1] == '.') && (name[2] == 0)) continue;
78994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            }
79094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
79194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            strcpy(dirname, basepath);
79294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            pathpos = dirname + strlen(dirname);
79394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            if ((*(pathpos-1)) != '/') {
79494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                *pathpos = '/';
79594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                pathpos++;
79694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                *pathpos = 0;
79794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            }
79894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            if (cachedir != NULL) {
79994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                snprintf(pathpos, sizeof(dirname)-(pathpos-dirname), "%s/%s", name, cachedir);
80094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            } else {
80194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                snprintf(pathpos, sizeof(dirname)-(pathpos-dirname), "%s", name);
80294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            }
80394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            CACHE_NOISY(ALOGI("Adding cache files from dir: %s\n", dirname));
80494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            subdir = opendir(dirname);
80594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            if (subdir != NULL) {
80694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                size_t dirnameLen = strlen(dirname);
80794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                _add_cache_files(cache, NULL, dirname, subdir, dirname, dirname+dirnameLen,
80894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                        PATH_MAX - dirnameLen);
80994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                closedir(subdir);
81094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            }
81194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        }
81294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
81394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
81494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    closedir(d);
81594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
81694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
81794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwoodstatic char *create_dir_path(char path[PATH_MAX], cache_dir_t* dir)
81894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood{
81994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    char *pos = path;
82094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    if (dir->parent != NULL) {
82194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        pos = create_dir_path(path, dir->parent);
82294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
82394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    // Note that we don't need to worry about going beyond the buffer,
82494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    // since when we were constructing the cache entries our maximum
82594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    // buffer size for full paths was PATH_MAX.
82694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    strcpy(pos, dir->name);
82794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    pos += strlen(pos);
82894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    *pos = '/';
82994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    pos++;
83094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    *pos = 0;
83194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    return pos;
83294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
83394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
83494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwoodstatic void delete_cache_dir(char path[PATH_MAX], cache_dir_t* dir)
83594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood{
83694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    if (dir->parent != NULL) {
83794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        create_dir_path(path, dir);
83894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        ALOGI("DEL DIR %s\n", path);
83994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        if (dir->hiddenCount <= 0) {
84094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            if (rmdir(path)) {
84194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                ALOGE("Couldn't rmdir %s: %s\n", path, strerror(errno));
84294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                return;
84394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            }
84494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        } else {
84594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            // The directory contains hidden files so we need to delete
84694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            // them along with the directory itself.
84794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            if (delete_dir_contents(path, 1, NULL)) {
84894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                return;
84994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            }
85094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        }
85194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        dir->parent->childCount--;
85294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        dir->deleted = 1;
85394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        if (dir->parent->childCount <= 0) {
85494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            delete_cache_dir(path, dir->parent);
85594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        }
85694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    } else if (dir->hiddenCount > 0) {
85794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        // This is a root directory, but it has hidden files.  Get rid of
85894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        // all of those files, but not the directory itself.
85994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        create_dir_path(path, dir);
86094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        ALOGI("DEL CONTENTS %s\n", path);
86194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        delete_dir_contents(path, 0, NULL);
86294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
86394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
86494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
86594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwoodstatic int cache_modtime_sort(const void *lhsP, const void *rhsP)
86694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood{
86794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    const cache_file_t *lhs = *(const cache_file_t**)lhsP;
86894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    const cache_file_t *rhs = *(const cache_file_t**)rhsP;
86994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    return lhs->modTime < rhs->modTime ? -1 : (lhs->modTime > rhs->modTime ? 1 : 0);
87094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
87194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
87241ea424413c0381ef2aa15fc5bd5d4b88abd23c4Jeff Sharkeyvoid clear_cache_files(const std::string& data_path, cache_t* cache, int64_t free_size)
87394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood{
87494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    size_t i;
87594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    int skip = 0;
87694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    char path[PATH_MAX];
87794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
87892dc3fc52cf097bd105460cf377779bdcf146d62Mark Salyzyn    ALOGI("Collected cache files: %zd directories, %zd files",
87994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        cache->numDirs, cache->numFiles);
88094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
88194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    CACHE_NOISY(ALOGI("Sorting files..."));
88294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    qsort(cache->files, cache->numFiles, sizeof(cache_file_t*),
88394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            cache_modtime_sort);
88494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
88594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    CACHE_NOISY(ALOGI("Cleaning empty directories..."));
88694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    for (i=cache->numDirs; i>0; i--) {
88794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        cache_dir_t* dir = cache->dirs[i-1];
88894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        if (dir->childCount <= 0 && !dir->deleted) {
88994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            delete_cache_dir(path, dir);
89094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        }
89194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
89294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
89394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    CACHE_NOISY(ALOGI("Trimming files..."));
89494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    for (i=0; i<cache->numFiles; i++) {
89594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        skip++;
89694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        if (skip > 10) {
89741ea424413c0381ef2aa15fc5bd5d4b88abd23c4Jeff Sharkey            if (data_disk_free(data_path) > free_size) {
89894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                return;
89994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            }
90094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            skip = 0;
90194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        }
90294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        cache_file_t* file = cache->files[i];
90394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        strcpy(create_dir_path(path, file->dir), file->name);
90494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        ALOGI("DEL (mod %d) %s\n", (int)file->modTime, path);
90594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        if (unlink(path) < 0) {
90694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            ALOGE("Couldn't unlink %s: %s\n", path, strerror(errno));
90794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        }
90894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        file->dir->childCount--;
90994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        if (file->dir->childCount <= 0) {
91094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            delete_cache_dir(path, file->dir);
91194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        }
91294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
91394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
91494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
91594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwoodvoid finish_cache_collection(cache_t* cache)
91694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood{
91799d9fb15b4a1eb534261ae81b2cf25aec4447bd0Chih-Hung Hsieh    CACHE_NOISY(size_t i;)
91894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
91994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    CACHE_NOISY(ALOGI("clear_cache_files: %d dirs, %d files\n", cache->numDirs, cache->numFiles));
92094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    CACHE_NOISY(
92194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        for (i=0; i<cache->numDirs; i++) {
92294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            cache_dir_t* dir = cache->dirs[i];
92394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            ALOGI("dir #%d: %p %s parent=%p\n", i, dir, dir->name, dir->parent);
92494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        })
92594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    CACHE_NOISY(
92694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        for (i=0; i<cache->numFiles; i++) {
92794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            cache_file_t* file = cache->files[i];
92894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            ALOGI("file #%d: %p %s time=%d dir=%p\n", i, file, file->name,
92994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                    (int)file->modTime, file->dir);
93094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        })
93194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    void* block = cache->memBlocks;
93294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    while (block != NULL) {
93394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        void* nextBlock = *(void**)block;
93494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        CACHE_NOISY(ALOGI("Freeing cache mem block: %p", block));
93594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        free(block);
93694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        block = nextBlock;
93794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
93894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    free(cache);
93994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
94094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
94194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood/**
942c597b6dd895dbb2b28c757ce7a2651b3cdc9b00cCalin Juravle * Validate that the path is valid in the context of the provided directory.
943c597b6dd895dbb2b28c757ce7a2651b3cdc9b00cCalin Juravle * The path is allowed to have at most one subdirectory and no indirections
944c597b6dd895dbb2b28c757ce7a2651b3cdc9b00cCalin Juravle * to top level directories (i.e. have "..").
945c597b6dd895dbb2b28c757ce7a2651b3cdc9b00cCalin Juravle */
946e23a13299a4f6f2488935b4786cdbb46f46e3d3cJeff Sharkeystatic int validate_path(const dir_rec_t* dir, const char* path, int maxSubdirs) {
947c597b6dd895dbb2b28c757ce7a2651b3cdc9b00cCalin Juravle    size_t dir_len = dir->len;
948c597b6dd895dbb2b28c757ce7a2651b3cdc9b00cCalin Juravle    const char* subdir = strchr(path + dir_len, '/');
949c597b6dd895dbb2b28c757ce7a2651b3cdc9b00cCalin Juravle
950c597b6dd895dbb2b28c757ce7a2651b3cdc9b00cCalin Juravle    // Only allow the path to have at most one subdirectory.
951c597b6dd895dbb2b28c757ce7a2651b3cdc9b00cCalin Juravle    if (subdir != NULL) {
952c597b6dd895dbb2b28c757ce7a2651b3cdc9b00cCalin Juravle        ++subdir;
953e23a13299a4f6f2488935b4786cdbb46f46e3d3cJeff Sharkey        if ((--maxSubdirs == 0) && strchr(subdir, '/') != NULL) {
954c597b6dd895dbb2b28c757ce7a2651b3cdc9b00cCalin Juravle            ALOGE("invalid apk path '%s' (subdir?)\n", path);
955c597b6dd895dbb2b28c757ce7a2651b3cdc9b00cCalin Juravle            return -1;
956c597b6dd895dbb2b28c757ce7a2651b3cdc9b00cCalin Juravle        }
957c597b6dd895dbb2b28c757ce7a2651b3cdc9b00cCalin Juravle    }
958c597b6dd895dbb2b28c757ce7a2651b3cdc9b00cCalin Juravle
959c597b6dd895dbb2b28c757ce7a2651b3cdc9b00cCalin Juravle    // Directories can't have a period directly after the directory markers to prevent "..".
960c597b6dd895dbb2b28c757ce7a2651b3cdc9b00cCalin Juravle    if ((path[dir_len] == '.') || ((subdir != NULL) && (*subdir == '.'))) {
961c597b6dd895dbb2b28c757ce7a2651b3cdc9b00cCalin Juravle        ALOGE("invalid apk path '%s' (trickery)\n", path);
962c597b6dd895dbb2b28c757ce7a2651b3cdc9b00cCalin Juravle        return -1;
963c597b6dd895dbb2b28c757ce7a2651b3cdc9b00cCalin Juravle    }
964c597b6dd895dbb2b28c757ce7a2651b3cdc9b00cCalin Juravle
965c597b6dd895dbb2b28c757ce7a2651b3cdc9b00cCalin Juravle    return 0;
966c597b6dd895dbb2b28c757ce7a2651b3cdc9b00cCalin Juravle}
967c597b6dd895dbb2b28c757ce7a2651b3cdc9b00cCalin Juravle
968c597b6dd895dbb2b28c757ce7a2651b3cdc9b00cCalin Juravle/**
96994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood * Checks whether a path points to a system app (.apk file). Returns 0
97094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood * if it is a system app or -1 if it is not.
97194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood */
97294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwoodint validate_system_app_path(const char* path) {
97394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    size_t i;
97494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
97594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    for (i = 0; i < android_system_dirs.count; i++) {
97694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        const size_t dir_len = android_system_dirs.dirs[i].len;
97794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        if (!strncmp(path, android_system_dirs.dirs[i].path, dir_len)) {
978e23a13299a4f6f2488935b4786cdbb46f46e3d3cJeff Sharkey            return validate_path(android_system_dirs.dirs + i, path, 1);
97994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        }
98094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
98194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
98294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    return -1;
98394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
98494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
98594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood/**
98694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood * Get the contents of a environment variable that contains a path. Caller
98794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood * owns the string that is inserted into the directory record. Returns
98894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood * 0 on success and -1 on error.
98994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood */
99094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwoodint get_path_from_env(dir_rec_t* rec, const char* var) {
99194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    const char* path = getenv(var);
99294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    int ret = get_path_from_string(rec, path);
99394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    if (ret < 0) {
99494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        ALOGW("Problem finding value for environment variable %s\n", var);
99594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
99694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    return ret;
99794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
99894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
99994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood/**
100094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood * Puts the string into the record as a directory. Appends '/' to the end
100194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood * of all paths. Caller owns the string that is inserted into the directory
100294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood * record. A null value will result in an error.
100394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood *
100494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood * Returns 0 on success and -1 on error.
100594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood */
100694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwoodint get_path_from_string(dir_rec_t* rec, const char* path) {
100794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    if (path == NULL) {
100894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        return -1;
100994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    } else {
101094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        const size_t path_len = strlen(path);
101194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        if (path_len <= 0) {
101294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            return -1;
101394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        }
101494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
101594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        // Make sure path is absolute.
101694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        if (path[0] != '/') {
101794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            return -1;
101894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        }
101994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
102094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        if (path[path_len - 1] == '/') {
102194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            // Path ends with a forward slash. Make our own copy.
102294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
102394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            rec->path = strdup(path);
102494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            if (rec->path == NULL) {
102594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                return -1;
102694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            }
102794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
102894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            rec->len = path_len;
102994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        } else {
103094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            // Path does not end with a slash. Generate a new string.
103194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            char *dst;
103294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
103394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            // Add space for slash and terminating null.
103494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            size_t dst_size = path_len + 2;
103594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
103619803807cd7ae01868fcfa50305f4a7dd13765e2Jeff Sharkey            rec->path = (char*) malloc(dst_size);
103794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            if (rec->path == NULL) {
103894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                return -1;
103994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            }
104094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
104194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            dst = rec->path;
104294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
104394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            if (append_and_increment(&dst, path, &dst_size) < 0
104494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                    || append_and_increment(&dst, "/", &dst_size)) {
104594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                ALOGE("Error canonicalizing path");
104694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                return -1;
104794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            }
104894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
104994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            rec->len = dst - rec->path;
105094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        }
105194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
105294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    return 0;
105394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
105494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
105594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwoodint copy_and_append(dir_rec_t* dst, const dir_rec_t* src, const char* suffix) {
105694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    dst->len = src->len + strlen(suffix);
105794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    const size_t dstSize = dst->len + 1;
105894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    dst->path = (char*) malloc(dstSize);
105994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
106094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    if (dst->path == NULL
106194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            || snprintf(dst->path, dstSize, "%s%s", src->path, suffix)
106294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                    != (ssize_t) dst->len) {
106394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        ALOGE("Could not allocate memory to hold appended path; aborting\n");
106494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        return -1;
106594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
106694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
106794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    return 0;
106894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
106994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
107094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood/**
1071d845c96128a40ca5802c0840ae190fa0af7d4735Narayan Kamath * Check whether path points to a valid path for an APK file. The path must
1072d845c96128a40ca5802c0840ae190fa0af7d4735Narayan Kamath * begin with a whitelisted prefix path and must be no deeper than |maxSubdirs| within
1073d845c96128a40ca5802c0840ae190fa0af7d4735Narayan Kamath * that path. Returns -1 when an invalid path is encountered and 0 when a valid path
1074d845c96128a40ca5802c0840ae190fa0af7d4735Narayan Kamath * is encountered.
107594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood */
1076d845c96128a40ca5802c0840ae190fa0af7d4735Narayan Kamathstatic int validate_apk_path_internal(const char *path, int maxSubdirs) {
1077c597b6dd895dbb2b28c757ce7a2651b3cdc9b00cCalin Juravle    const dir_rec_t* dir = NULL;
107894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    if (!strncmp(path, android_app_dir.path, android_app_dir.len)) {
1079c597b6dd895dbb2b28c757ce7a2651b3cdc9b00cCalin Juravle        dir = &android_app_dir;
108094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    } else if (!strncmp(path, android_app_private_dir.path, android_app_private_dir.len)) {
1081c597b6dd895dbb2b28c757ce7a2651b3cdc9b00cCalin Juravle        dir = &android_app_private_dir;
108294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    } else if (!strncmp(path, android_asec_dir.path, android_asec_dir.len)) {
1083c597b6dd895dbb2b28c757ce7a2651b3cdc9b00cCalin Juravle        dir = &android_asec_dir;
1084e23a13299a4f6f2488935b4786cdbb46f46e3d3cJeff Sharkey    } else if (!strncmp(path, android_mnt_expand_dir.path, android_mnt_expand_dir.len)) {
1085e23a13299a4f6f2488935b4786cdbb46f46e3d3cJeff Sharkey        dir = &android_mnt_expand_dir;
1086d845c96128a40ca5802c0840ae190fa0af7d4735Narayan Kamath        if (maxSubdirs < 2) {
1087d845c96128a40ca5802c0840ae190fa0af7d4735Narayan Kamath            maxSubdirs = 2;
1088d845c96128a40ca5802c0840ae190fa0af7d4735Narayan Kamath        }
108994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    } else {
109094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        return -1;
109194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
109294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
1093e23a13299a4f6f2488935b4786cdbb46f46e3d3cJeff Sharkey    return validate_path(dir, path, maxSubdirs);
109494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
109594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
1096d845c96128a40ca5802c0840ae190fa0af7d4735Narayan Kamathint validate_apk_path(const char* path) {
1097d845c96128a40ca5802c0840ae190fa0af7d4735Narayan Kamath    return validate_apk_path_internal(path, 1 /* maxSubdirs */);
1098d845c96128a40ca5802c0840ae190fa0af7d4735Narayan Kamath}
1099d845c96128a40ca5802c0840ae190fa0af7d4735Narayan Kamath
1100d845c96128a40ca5802c0840ae190fa0af7d4735Narayan Kamathint validate_apk_path_subdirs(const char* path) {
1101d845c96128a40ca5802c0840ae190fa0af7d4735Narayan Kamath    return validate_apk_path_internal(path, 3 /* maxSubdirs */);
1102d845c96128a40ca5802c0840ae190fa0af7d4735Narayan Kamath}
1103d845c96128a40ca5802c0840ae190fa0af7d4735Narayan Kamath
110494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwoodint append_and_increment(char** dst, const char* src, size_t* dst_size) {
110594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    ssize_t ret = strlcpy(*dst, src, *dst_size);
110694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    if (ret < 0 || (size_t) ret >= *dst_size) {
110794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        return -1;
110894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
110994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    *dst += ret;
111094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    *dst_size -= ret;
111194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    return 0;
111294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
111394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
111419803807cd7ae01868fcfa50305f4a7dd13765e2Jeff Sharkeychar *build_string2(const char *s1, const char *s2) {
111594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    if (s1 == NULL || s2 == NULL) return NULL;
111694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
111794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    int len_s1 = strlen(s1);
111894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    int len_s2 = strlen(s2);
111994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    int len = len_s1 + len_s2 + 1;
112019803807cd7ae01868fcfa50305f4a7dd13765e2Jeff Sharkey    char *result = (char *) malloc(len);
112194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    if (result == NULL) return NULL;
112294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
112394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    strcpy(result, s1);
112494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    strcpy(result + len_s1, s2);
112594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
112694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    return result;
112794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
112894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
112919803807cd7ae01868fcfa50305f4a7dd13765e2Jeff Sharkeychar *build_string3(const char *s1, const char *s2, const char *s3) {
113094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    if (s1 == NULL || s2 == NULL || s3 == NULL) return NULL;
113194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
113294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    int len_s1 = strlen(s1);
113394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    int len_s2 = strlen(s2);
113494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    int len_s3 = strlen(s3);
113594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    int len = len_s1 + len_s2 + len_s3 + 1;
113619803807cd7ae01868fcfa50305f4a7dd13765e2Jeff Sharkey    char *result = (char *) malloc(len);
113794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    if (result == NULL) return NULL;
113894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
113994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    strcpy(result, s1);
114094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    strcpy(result + len_s1, s2);
114194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    strcpy(result + len_s1 + len_s2, s3);
114294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
114394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    return result;
114494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
114594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
114694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood/* Ensure that /data/media directories are prepared for given user. */
114741ea424413c0381ef2aa15fc5bd5d4b88abd23c4Jeff Sharkeyint ensure_media_user_dirs(const char* uuid, userid_t userid) {
114841ea424413c0381ef2aa15fc5bd5d4b88abd23c4Jeff Sharkey    std::string media_user_path(create_data_media_path(uuid, userid));
114941ea424413c0381ef2aa15fc5bd5d4b88abd23c4Jeff Sharkey    if (fs_prepare_dir(media_user_path.c_str(), 0770, AID_MEDIA_RW, AID_MEDIA_RW) == -1) {
115094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        return -1;
115194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
115294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
115394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    return 0;
115494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
1155d93707342a61e66bc3eb2145628158452f577f42Dave Allison
1156095c763dd9aa26a206d10ab7c1d7e1c569298fb3Robin Leeint ensure_config_user_dirs(userid_t userid) {
1157095c763dd9aa26a206d10ab7c1d7e1c569298fb3Robin Lee    char config_user_path[PATH_MAX];
1158095c763dd9aa26a206d10ab7c1d7e1c569298fb3Robin Lee
1159095c763dd9aa26a206d10ab7c1d7e1c569298fb3Robin Lee    // writable by system, readable by any app within the same user
116060fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee    const int uid = multiuser_get_uid(userid, AID_SYSTEM);
116160fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee    const int gid = multiuser_get_uid(userid, AID_EVERYBODY);
1162095c763dd9aa26a206d10ab7c1d7e1c569298fb3Robin Lee
1163095c763dd9aa26a206d10ab7c1d7e1c569298fb3Robin Lee    // Ensure /data/misc/user/<userid> exists
1164095c763dd9aa26a206d10ab7c1d7e1c569298fb3Robin Lee    create_user_config_path(config_user_path, userid);
1165095c763dd9aa26a206d10ab7c1d7e1c569298fb3Robin Lee    if (fs_prepare_dir(config_user_path, 0750, uid, gid) == -1) {
1166095c763dd9aa26a206d10ab7c1d7e1c569298fb3Robin Lee        return -1;
1167095c763dd9aa26a206d10ab7c1d7e1c569298fb3Robin Lee    }
1168095c763dd9aa26a206d10ab7c1d7e1c569298fb3Robin Lee
1169095c763dd9aa26a206d10ab7c1d7e1c569298fb3Robin Lee   return 0;
1170095c763dd9aa26a206d10ab7c1d7e1c569298fb3Robin Lee}
1171