utils.cpp revision 41ea424413c0381ef2aa15fc5bd5d4b88abd23c4
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/**
40c03de09173f98506e73e7cf7df21fe11795d4b24Jeff Sharkey * Create the path name where package data should be stored for the given
41c03de09173f98506e73e7cf7df21fe11795d4b24Jeff Sharkey * volume UUID, package name, and user ID. An empty UUID is assumed to be
42c03de09173f98506e73e7cf7df21fe11795d4b24Jeff Sharkey * internal storage.
43c03de09173f98506e73e7cf7df21fe11795d4b24Jeff Sharkey */
44c03de09173f98506e73e7cf7df21fe11795d4b24Jeff Sharkeystd::string create_package_data_path(const char* volume_uuid,
45c03de09173f98506e73e7cf7df21fe11795d4b24Jeff Sharkey        const char* package_name, userid_t user) {
46c03de09173f98506e73e7cf7df21fe11795d4b24Jeff Sharkey    CHECK(is_valid_filename(package_name));
47c03de09173f98506e73e7cf7df21fe11795d4b24Jeff Sharkey    CHECK(is_valid_package_name(package_name) == 0);
48c03de09173f98506e73e7cf7df21fe11795d4b24Jeff Sharkey
4941ea424413c0381ef2aa15fc5bd5d4b88abd23c4Jeff Sharkey    return StringPrintf("%s/%s", create_data_user_path(volume_uuid, user).c_str(), package_name);
50c03de09173f98506e73e7cf7df21fe11795d4b24Jeff Sharkey}
5194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
52c03de09173f98506e73e7cf7df21fe11795d4b24Jeff Sharkeyint create_pkg_path(char path[PKG_PATH_MAX], const char *pkgname,
53c03de09173f98506e73e7cf7df21fe11795d4b24Jeff Sharkey        const char *postfix, userid_t userid) {
54c03de09173f98506e73e7cf7df21fe11795d4b24Jeff Sharkey    if (is_valid_package_name(pkgname) != 0) {
55c03de09173f98506e73e7cf7df21fe11795d4b24Jeff Sharkey        path[0] = '\0';
5694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        return -1;
5794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
5894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
59c03de09173f98506e73e7cf7df21fe11795d4b24Jeff Sharkey    std::string _tmp(create_package_data_path(nullptr, pkgname, userid) + postfix);
60c03de09173f98506e73e7cf7df21fe11795d4b24Jeff Sharkey    const char* tmp = _tmp.c_str();
61c03de09173f98506e73e7cf7df21fe11795d4b24Jeff Sharkey    if (strlen(tmp) >= PKG_PATH_MAX) {
62c03de09173f98506e73e7cf7df21fe11795d4b24Jeff Sharkey        path[0] = '\0';
63c03de09173f98506e73e7cf7df21fe11795d4b24Jeff Sharkey        return -1;
64c03de09173f98506e73e7cf7df21fe11795d4b24Jeff Sharkey    } else {
65c03de09173f98506e73e7cf7df21fe11795d4b24Jeff Sharkey        strcpy(path, tmp);
66c03de09173f98506e73e7cf7df21fe11795d4b24Jeff Sharkey        return 0;
6794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
6894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
6994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
7041ea424413c0381ef2aa15fc5bd5d4b88abd23c4Jeff Sharkeystd::string create_data_path(const char* volume_uuid) {
7141ea424413c0381ef2aa15fc5bd5d4b88abd23c4Jeff Sharkey    if (volume_uuid == nullptr) {
7241ea424413c0381ef2aa15fc5bd5d4b88abd23c4Jeff Sharkey        return "/data";
7394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    } else {
7441ea424413c0381ef2aa15fc5bd5d4b88abd23c4Jeff Sharkey        CHECK(is_valid_filename(volume_uuid));
7541ea424413c0381ef2aa15fc5bd5d4b88abd23c4Jeff Sharkey        return StringPrintf("/mnt/expand/%s", volume_uuid);
7694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
7741ea424413c0381ef2aa15fc5bd5d4b88abd23c4Jeff Sharkey}
7894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
7941ea424413c0381ef2aa15fc5bd5d4b88abd23c4Jeff Sharkey/**
8041ea424413c0381ef2aa15fc5bd5d4b88abd23c4Jeff Sharkey * Create the path name for user data for a certain userid.
8141ea424413c0381ef2aa15fc5bd5d4b88abd23c4Jeff Sharkey */
8241ea424413c0381ef2aa15fc5bd5d4b88abd23c4Jeff Sharkeystd::string create_data_user_path(const char* volume_uuid, userid_t userid) {
8341ea424413c0381ef2aa15fc5bd5d4b88abd23c4Jeff Sharkey    std::string data(create_data_path(volume_uuid));
8441ea424413c0381ef2aa15fc5bd5d4b88abd23c4Jeff Sharkey    if (volume_uuid == nullptr) {
8541ea424413c0381ef2aa15fc5bd5d4b88abd23c4Jeff Sharkey        if (userid == 0) {
8641ea424413c0381ef2aa15fc5bd5d4b88abd23c4Jeff Sharkey            return StringPrintf("%s/data", data.c_str());
8741ea424413c0381ef2aa15fc5bd5d4b88abd23c4Jeff Sharkey        } else {
8841ea424413c0381ef2aa15fc5bd5d4b88abd23c4Jeff Sharkey            return StringPrintf("%s/user/%u", data.c_str(), userid);
8994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        }
9041ea424413c0381ef2aa15fc5bd5d4b88abd23c4Jeff Sharkey    } else {
9141ea424413c0381ef2aa15fc5bd5d4b88abd23c4Jeff Sharkey        return StringPrintf("%s/user/%u", data.c_str(), userid);
9294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
9394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
9494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
9594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood/**
96abe4fe5b46157ecd2a52d28abf938c816c3ce878Jeff Sharkey * Create the path name for media for a certain userid.
9794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood */
9841ea424413c0381ef2aa15fc5bd5d4b88abd23c4Jeff Sharkeystd::string create_data_media_path(const char* volume_uuid, userid_t userid) {
9941ea424413c0381ef2aa15fc5bd5d4b88abd23c4Jeff Sharkey    return StringPrintf("%s/media/%u", create_data_path(volume_uuid).c_str(), userid);
10094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
10194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
102095c763dd9aa26a206d10ab7c1d7e1c569298fb3Robin Lee/**
103095c763dd9aa26a206d10ab7c1d7e1c569298fb3Robin Lee * Create the path name for config for a certain userid.
104095c763dd9aa26a206d10ab7c1d7e1c569298fb3Robin Lee * Returns 0 on success, and -1 on failure.
105095c763dd9aa26a206d10ab7c1d7e1c569298fb3Robin Lee */
106095c763dd9aa26a206d10ab7c1d7e1c569298fb3Robin Leeint create_user_config_path(char path[PATH_MAX], userid_t userid) {
107095c763dd9aa26a206d10ab7c1d7e1c569298fb3Robin Lee    if (snprintf(path, PATH_MAX, "%s%d", "/data/misc/user/", userid) > PATH_MAX) {
108095c763dd9aa26a206d10ab7c1d7e1c569298fb3Robin Lee        return -1;
109095c763dd9aa26a206d10ab7c1d7e1c569298fb3Robin Lee    }
110095c763dd9aa26a206d10ab7c1d7e1c569298fb3Robin Lee    return 0;
111095c763dd9aa26a206d10ab7c1d7e1c569298fb3Robin Lee}
112095c763dd9aa26a206d10ab7c1d7e1c569298fb3Robin Lee
11394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwoodint create_move_path(char path[PKG_PATH_MAX],
11494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    const char* pkgname,
11594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    const char* leaf,
11699d9fb15b4a1eb534261ae81b2cf25aec4447bd0Chih-Hung Hsieh    userid_t userid __unused)
11794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood{
11894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    if ((android_data_dir.len + strlen(PRIMARY_USER_PREFIX) + strlen(pkgname) + strlen(leaf) + 1)
11994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            >= PKG_PATH_MAX) {
12094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        return -1;
12194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
12294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
12394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    sprintf(path, "%s%s%s/%s", android_data_dir.path, PRIMARY_USER_PREFIX, pkgname, leaf);
12494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    return 0;
12594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
12694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
12794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood/**
12894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood * Checks whether the package name is valid. Returns -1 on error and
12994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood * 0 on success.
13094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood */
13194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwoodint is_valid_package_name(const char* pkgname) {
13294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    const char *x = pkgname;
13394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    int alpha = -1;
13494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
135c03de09173f98506e73e7cf7df21fe11795d4b24Jeff Sharkey    if (strlen(pkgname) > PKG_NAME_MAX) {
136c03de09173f98506e73e7cf7df21fe11795d4b24Jeff Sharkey        return -1;
137c03de09173f98506e73e7cf7df21fe11795d4b24Jeff Sharkey    }
138c03de09173f98506e73e7cf7df21fe11795d4b24Jeff Sharkey
13994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    while (*x) {
14094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        if (isalnum(*x) || (*x == '_')) {
14194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                /* alphanumeric or underscore are fine */
14294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        } else if (*x == '.') {
14394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            if ((x == pkgname) || (x[1] == '.') || (x[1] == 0)) {
14494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                    /* periods must not be first, last, or doubled */
14594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                ALOGE("invalid package name '%s'\n", pkgname);
14694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                return -1;
14794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            }
14894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        } else if (*x == '-') {
14994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            /* Suffix -X is fine to let versioning of packages.
15094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood               But whatever follows should be alphanumeric.*/
15194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            alpha = 1;
15294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        } else {
15394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                /* anything not A-Z, a-z, 0-9, _, or . is invalid */
15494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            ALOGE("invalid package name '%s'\n", pkgname);
15594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            return -1;
15694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        }
15794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
15894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        x++;
15994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
16094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
16194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    if (alpha == 1) {
16294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        // Skip current character
16394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        x++;
16494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        while (*x) {
16594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            if (!isalnum(*x)) {
16694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                ALOGE("invalid package name '%s' should include only numbers after -\n", pkgname);
16794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                return -1;
16894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            }
16994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            x++;
17094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        }
17194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
17294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
17394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    return 0;
17494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
17594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
1763aee2c5c749dc2589f001b26fae1ec958ec89524Narayan Kamathstatic int _delete_dir_contents(DIR *d,
1773aee2c5c749dc2589f001b26fae1ec958ec89524Narayan Kamath                                int (*exclusion_predicate)(const char *name, const int is_dir))
17894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood{
17994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    int result = 0;
18094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    struct dirent *de;
18194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    int dfd;
18294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
18394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    dfd = dirfd(d);
18494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
18594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    if (dfd < 0) return -1;
18694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
18794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    while ((de = readdir(d))) {
18894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        const char *name = de->d_name;
18994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
1903aee2c5c749dc2589f001b26fae1ec958ec89524Narayan Kamath            /* check using the exclusion predicate, if provided */
1913aee2c5c749dc2589f001b26fae1ec958ec89524Narayan Kamath        if (exclusion_predicate && exclusion_predicate(name, (de->d_type == DT_DIR))) {
1923aee2c5c749dc2589f001b26fae1ec958ec89524Narayan Kamath            continue;
1933aee2c5c749dc2589f001b26fae1ec958ec89524Narayan Kamath        }
19494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
19594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        if (de->d_type == DT_DIR) {
19699d9fb15b4a1eb534261ae81b2cf25aec4447bd0Chih-Hung Hsieh            int subfd;
19794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            DIR *subdir;
19894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
19994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                /* always skip "." and ".." */
20094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            if (name[0] == '.') {
20194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                if (name[1] == 0) continue;
20294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                if ((name[1] == '.') && (name[2] == 0)) continue;
20394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            }
20494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
20594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            subfd = openat(dfd, name, O_RDONLY | O_DIRECTORY);
20694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            if (subfd < 0) {
20794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                ALOGE("Couldn't openat %s: %s\n", name, strerror(errno));
20894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                result = -1;
20994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                continue;
21094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            }
21194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            subdir = fdopendir(subfd);
21294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            if (subdir == NULL) {
21394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                ALOGE("Couldn't fdopendir %s: %s\n", name, strerror(errno));
21494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                close(subfd);
21594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                result = -1;
21694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                continue;
21794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            }
2183aee2c5c749dc2589f001b26fae1ec958ec89524Narayan Kamath            if (_delete_dir_contents(subdir, exclusion_predicate)) {
21994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                result = -1;
22094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            }
22194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            closedir(subdir);
22294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            if (unlinkat(dfd, name, AT_REMOVEDIR) < 0) {
22394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                ALOGE("Couldn't unlinkat %s: %s\n", name, strerror(errno));
22494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                result = -1;
22594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            }
22694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        } else {
22794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            if (unlinkat(dfd, name, 0) < 0) {
22894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                ALOGE("Couldn't unlinkat %s: %s\n", name, strerror(errno));
22994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                result = -1;
23094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            }
23194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        }
23294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
23394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
23494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    return result;
23594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
23694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
23794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwoodint delete_dir_contents(const char *pathname,
23894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                        int also_delete_dir,
2393aee2c5c749dc2589f001b26fae1ec958ec89524Narayan Kamath                        int (*exclusion_predicate)(const char*, const int))
24094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood{
24194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    int res = 0;
24294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    DIR *d;
24394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
24494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    d = opendir(pathname);
24594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    if (d == NULL) {
24694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        ALOGE("Couldn't opendir %s: %s\n", pathname, strerror(errno));
24794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        return -errno;
24894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
2493aee2c5c749dc2589f001b26fae1ec958ec89524Narayan Kamath    res = _delete_dir_contents(d, exclusion_predicate);
25094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    closedir(d);
25194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    if (also_delete_dir) {
25294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        if (rmdir(pathname)) {
25394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            ALOGE("Couldn't rmdir %s: %s\n", pathname, strerror(errno));
25494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            res = -1;
25594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        }
25694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
25794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    return res;
25894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
25994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
26094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwoodint delete_dir_contents_fd(int dfd, const char *name)
26194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood{
26294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    int fd, res;
26394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    DIR *d;
26494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
26594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    fd = openat(dfd, name, O_RDONLY | O_DIRECTORY);
26694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    if (fd < 0) {
26794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        ALOGE("Couldn't openat %s: %s\n", name, strerror(errno));
26894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        return -1;
26994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
27094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    d = fdopendir(fd);
27194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    if (d == NULL) {
27294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        ALOGE("Couldn't fdopendir %s: %s\n", name, strerror(errno));
27394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        close(fd);
27494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        return -1;
27594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
27694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    res = _delete_dir_contents(d, 0);
27794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    closedir(d);
27894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    return res;
27994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
28094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
28160fd3feecab4336d964ca8e31c7c3220e1afb558Robin Leestatic int _copy_owner_permissions(int srcfd, int dstfd)
28260fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee{
28360fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee    struct stat st;
28460fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee    if (fstat(srcfd, &st) != 0) {
28560fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee        return -1;
28660fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee    }
28760fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee    if (fchmod(dstfd, st.st_mode) != 0) {
28860fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee        return -1;
28960fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee    }
29060fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee    return 0;
29160fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee}
29260fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee
29360fd3feecab4336d964ca8e31c7c3220e1afb558Robin Leestatic int _copy_dir_files(int sdfd, int ddfd, uid_t owner, gid_t group)
29460fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee{
29560fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee    int result = 0;
29660fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee    if (_copy_owner_permissions(sdfd, ddfd) != 0) {
29760fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee        ALOGE("_copy_dir_files failed to copy dir permissions\n");
29860fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee    }
29960fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee    if (fchown(ddfd, owner, group) != 0) {
30060fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee        ALOGE("_copy_dir_files failed to change dir owner\n");
30160fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee    }
30260fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee
30360fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee    DIR *ds = fdopendir(sdfd);
30460fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee    if (ds == NULL) {
30560fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee        ALOGE("Couldn't fdopendir: %s\n", strerror(errno));
30660fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee        return -1;
30760fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee    }
30860fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee    struct dirent *de;
30960fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee    while ((de = readdir(ds))) {
31060fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee        if (de->d_type != DT_REG) {
31160fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee            continue;
31260fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee        }
31360fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee
31460fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee        const char *name = de->d_name;
31560fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee        int fsfd = openat(sdfd, name, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
31660fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee        int fdfd = openat(ddfd, name, O_WRONLY | O_NOFOLLOW | O_CLOEXEC | O_CREAT, 0600);
31760fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee        if (fsfd == -1 || fdfd == -1) {
31860fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee            ALOGW("Couldn't copy %s: %s\n", name, strerror(errno));
31960fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee        } else {
32060fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee            if (_copy_owner_permissions(fsfd, fdfd) != 0) {
32160fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee                ALOGE("Failed to change file permissions\n");
32260fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee            }
32360fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee            if (fchown(fdfd, owner, group) != 0) {
32460fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee                ALOGE("Failed to change file owner\n");
32560fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee            }
32660fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee
32760fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee            char buf[8192];
32860fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee            ssize_t size;
32960fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee            while ((size = read(fsfd, buf, sizeof(buf))) > 0) {
33060fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee                write(fdfd, buf, size);
33160fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee            }
33260fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee            if (size < 0) {
33360fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee                ALOGW("Couldn't copy %s: %s\n", name, strerror(errno));
33460fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee                result = -1;
33560fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee            }
33660fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee        }
33760fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee        close(fdfd);
33860fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee        close(fsfd);
33960fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee    }
34060fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee
34160fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee    return result;
34260fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee}
34360fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee
34460fd3feecab4336d964ca8e31c7c3220e1afb558Robin Leeint copy_dir_files(const char *srcname,
34560fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee                   const char *dstname,
34660fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee                   uid_t owner,
34760fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee                   uid_t group)
34860fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee{
34960fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee    int res = 0;
35060fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee    DIR *ds = NULL;
35160fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee    DIR *dd = NULL;
35260fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee
35360fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee    ds = opendir(srcname);
35460fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee    if (ds == NULL) {
35560fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee        ALOGE("Couldn't opendir %s: %s\n", srcname, strerror(errno));
35660fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee        return -errno;
35760fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee    }
35860fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee
35960fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee    mkdir(dstname, 0600);
36060fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee    dd = opendir(dstname);
36160fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee    if (dd == NULL) {
36260fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee        ALOGE("Couldn't opendir %s: %s\n", dstname, strerror(errno));
36360fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee        closedir(ds);
36460fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee        return -errno;
36560fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee    }
36660fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee
36760fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee    int sdfd = dirfd(ds);
36860fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee    int ddfd = dirfd(dd);
36960fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee    if (sdfd != -1 && ddfd != -1) {
37060fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee        res = _copy_dir_files(sdfd, ddfd, owner, group);
37160fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee    } else {
37260fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee        res = -errno;
37360fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee    }
37460fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee    closedir(dd);
37560fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee    closedir(ds);
37660fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee    return res;
37760fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee}
37860fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee
37994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwoodint lookup_media_dir(char basepath[PATH_MAX], const char *dir)
38094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood{
38194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    DIR *d;
38294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    struct dirent *de;
38394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    struct stat s;
38494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    char* dirpos = basepath + strlen(basepath);
38594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
38694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    if ((*(dirpos-1)) != '/') {
38794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        *dirpos = '/';
38894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        dirpos++;
38994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
39094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
39194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    CACHE_NOISY(ALOGI("Looking up %s in %s\n", dir, basepath));
39294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    // Verify the path won't extend beyond our buffer, to avoid
39394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    // repeated checking later.
39494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    if ((dirpos-basepath+strlen(dir)) >= (PATH_MAX-1)) {
39594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        ALOGW("Path exceeds limit: %s%s", basepath, dir);
39694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        return -1;
39794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
39894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
39994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    // First, can we find this directory with the case that is given?
40094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    strcpy(dirpos, dir);
40194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    if (stat(basepath, &s) >= 0) {
40294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        CACHE_NOISY(ALOGI("Found direct: %s\n", basepath));
40394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        return 0;
40494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
40594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
40694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    // Not found with that case...  search through all entries to find
40794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    // one that matches regardless of case.
40894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    *dirpos = 0;
40994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
41094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    d = opendir(basepath);
41194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    if (d == NULL) {
41294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        return -1;
41394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
41494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
41594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    while ((de = readdir(d))) {
41694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        if (strcasecmp(de->d_name, dir) == 0) {
41794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            strcpy(dirpos, de->d_name);
41894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            closedir(d);
41994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            CACHE_NOISY(ALOGI("Found search: %s\n", basepath));
42094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            return 0;
42194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        }
42294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
42394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
42494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    ALOGW("Couldn't find %s in %s", dir, basepath);
42594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    closedir(d);
42694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    return -1;
42794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
42894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
42941ea424413c0381ef2aa15fc5bd5d4b88abd23c4Jeff Sharkeyint64_t data_disk_free(const std::string& data_path)
43094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood{
43194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    struct statfs sfs;
43241ea424413c0381ef2aa15fc5bd5d4b88abd23c4Jeff Sharkey    if (statfs(data_path.c_str(), &sfs) == 0) {
43394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        return sfs.f_bavail * sfs.f_bsize;
43494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    } else {
43541ea424413c0381ef2aa15fc5bd5d4b88abd23c4Jeff Sharkey        PLOG(ERROR) << "Couldn't statfs " << data_path;
43694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        return -1;
43794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
43894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
43994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
44094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwoodcache_t* start_cache_collection()
44194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood{
44294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    cache_t* cache = (cache_t*)calloc(1, sizeof(cache_t));
44394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    return cache;
44494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
44594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
44694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood#define CACHE_BLOCK_SIZE (512*1024)
44794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
44894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwoodstatic void* _cache_malloc(cache_t* cache, size_t len)
44994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood{
45094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    len = (len+3)&~3;
45194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    if (len > (CACHE_BLOCK_SIZE/2)) {
45294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        // It doesn't make sense to try to put this allocation into one
45394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        // of our blocks, because it is so big.  Instead, make a new dedicated
45494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        // block for it.
45594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        int8_t* res = (int8_t*)malloc(len+sizeof(void*));
45694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        if (res == NULL) {
45794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            return NULL;
45894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        }
45994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        CACHE_NOISY(ALOGI("Allocated large cache mem block: %p size %d", res, len));
46094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        // Link it into our list of blocks, not disrupting the current one.
46194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        if (cache->memBlocks == NULL) {
46294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            *(void**)res = NULL;
46394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            cache->memBlocks = res;
46494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        } else {
46594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            *(void**)res = *(void**)cache->memBlocks;
46694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            *(void**)cache->memBlocks = res;
46794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        }
46894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        return res + sizeof(void*);
46994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
47094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    int8_t* res = cache->curMemBlockAvail;
47194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    int8_t* nextPos = res + len;
47294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    if (cache->memBlocks == NULL || nextPos > cache->curMemBlockEnd) {
47319803807cd7ae01868fcfa50305f4a7dd13765e2Jeff Sharkey        int8_t* newBlock = (int8_t*) malloc(CACHE_BLOCK_SIZE);
47494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        if (newBlock == NULL) {
47594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            return NULL;
47694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        }
47794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        CACHE_NOISY(ALOGI("Allocated new cache mem block: %p", newBlock));
47894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        *(void**)newBlock = cache->memBlocks;
47994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        cache->memBlocks = newBlock;
48094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        res = cache->curMemBlockAvail = newBlock + sizeof(void*);
48194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        cache->curMemBlockEnd = newBlock + CACHE_BLOCK_SIZE;
48294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        nextPos = res + len;
48394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
48494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    CACHE_NOISY(ALOGI("cache_malloc: ret %p size %d, block=%p, nextPos=%p",
48594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            res, len, cache->memBlocks, nextPos));
48694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    cache->curMemBlockAvail = nextPos;
48794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    return res;
48894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
48994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
49094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwoodstatic void* _cache_realloc(cache_t* cache, void* cur, size_t origLen, size_t len)
49194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood{
49294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    // This isn't really a realloc, but it is good enough for our purposes here.
49394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    void* alloc = _cache_malloc(cache, len);
49494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    if (alloc != NULL && cur != NULL) {
49594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        memcpy(alloc, cur, origLen < len ? origLen : len);
49694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
49794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    return alloc;
49894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
49994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
50094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwoodstatic void _inc_num_cache_collected(cache_t* cache)
50194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood{
50294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    cache->numCollected++;
50394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    if ((cache->numCollected%20000) == 0) {
50492dc3fc52cf097bd105460cf377779bdcf146d62Mark Salyzyn        ALOGI("Collected cache so far: %zd directories, %zd files",
50594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            cache->numDirs, cache->numFiles);
50694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
50794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
50894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
50994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwoodstatic cache_dir_t* _add_cache_dir_t(cache_t* cache, cache_dir_t* parent, const char *name)
51094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood{
51194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    size_t nameLen = strlen(name);
51294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    cache_dir_t* dir = (cache_dir_t*)_cache_malloc(cache, sizeof(cache_dir_t)+nameLen+1);
51394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    if (dir != NULL) {
51494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        dir->parent = parent;
51594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        dir->childCount = 0;
51694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        dir->hiddenCount = 0;
51794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        dir->deleted = 0;
51894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        strcpy(dir->name, name);
51994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        if (cache->numDirs >= cache->availDirs) {
52094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            size_t newAvail = cache->availDirs < 1000 ? 1000 : cache->availDirs*2;
52194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            cache_dir_t** newDirs = (cache_dir_t**)_cache_realloc(cache, cache->dirs,
52294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                    cache->availDirs*sizeof(cache_dir_t*), newAvail*sizeof(cache_dir_t*));
52394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            if (newDirs == NULL) {
52494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                ALOGE("Failure growing cache dirs array for %s\n", name);
52594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                return NULL;
52694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            }
52794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            cache->availDirs = newAvail;
52894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            cache->dirs = newDirs;
52994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        }
53094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        cache->dirs[cache->numDirs] = dir;
53194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        cache->numDirs++;
53294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        if (parent != NULL) {
53394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            parent->childCount++;
53494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        }
53594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        _inc_num_cache_collected(cache);
53694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    } else {
53794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        ALOGE("Failure allocating cache_dir_t for %s\n", name);
53894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
53994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    return dir;
54094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
54194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
54294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwoodstatic cache_file_t* _add_cache_file_t(cache_t* cache, cache_dir_t* dir, time_t modTime,
54394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        const char *name)
54494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood{
54594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    size_t nameLen = strlen(name);
54694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    cache_file_t* file = (cache_file_t*)_cache_malloc(cache, sizeof(cache_file_t)+nameLen+1);
54794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    if (file != NULL) {
54894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        file->dir = dir;
54994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        file->modTime = modTime;
55094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        strcpy(file->name, name);
55194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        if (cache->numFiles >= cache->availFiles) {
55294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            size_t newAvail = cache->availFiles < 1000 ? 1000 : cache->availFiles*2;
55394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            cache_file_t** newFiles = (cache_file_t**)_cache_realloc(cache, cache->files,
55494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                    cache->availFiles*sizeof(cache_file_t*), newAvail*sizeof(cache_file_t*));
55594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            if (newFiles == NULL) {
55694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                ALOGE("Failure growing cache file array for %s\n", name);
55794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                return NULL;
55894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            }
55994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            cache->availFiles = newAvail;
56094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            cache->files = newFiles;
56194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        }
56294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        CACHE_NOISY(ALOGI("Setting file %p at position %d in array %p", file,
56394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                cache->numFiles, cache->files));
56494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        cache->files[cache->numFiles] = file;
56594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        cache->numFiles++;
56694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        dir->childCount++;
56794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        _inc_num_cache_collected(cache);
56894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    } else {
56994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        ALOGE("Failure allocating cache_file_t for %s\n", name);
57094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
57194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    return file;
57294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
57394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
57494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwoodstatic int _add_cache_files(cache_t *cache, cache_dir_t *parentDir, const char *dirName,
57594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        DIR* dir, char *pathBase, char *pathPos, size_t pathAvailLen)
57694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood{
57794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    struct dirent *de;
57894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    cache_dir_t* cacheDir = NULL;
57994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    int dfd;
58094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
58194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    CACHE_NOISY(ALOGI("_add_cache_files: parent=%p dirName=%s dir=%p pathBase=%s",
58294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            parentDir, dirName, dir, pathBase));
58394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
58494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    dfd = dirfd(dir);
58594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
58694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    if (dfd < 0) return 0;
58794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
58894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    // Sub-directories always get added to the data structure, so if they
58994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    // are empty we will know about them to delete them later.
59094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    cacheDir = _add_cache_dir_t(cache, parentDir, dirName);
59194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
59294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    while ((de = readdir(dir))) {
59394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        const char *name = de->d_name;
59494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
59594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        if (de->d_type == DT_DIR) {
59694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            int subfd;
59794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            DIR *subdir;
59894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
59994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                /* always skip "." and ".." */
60094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            if (name[0] == '.') {
60194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                if (name[1] == 0) continue;
60294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                if ((name[1] == '.') && (name[2] == 0)) continue;
60394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            }
60494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
60594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            subfd = openat(dfd, name, O_RDONLY | O_DIRECTORY);
60694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            if (subfd < 0) {
60794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                ALOGE("Couldn't openat %s: %s\n", name, strerror(errno));
60894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                continue;
60994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            }
61094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            subdir = fdopendir(subfd);
61194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            if (subdir == NULL) {
61294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                ALOGE("Couldn't fdopendir %s: %s\n", name, strerror(errno));
61394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                close(subfd);
61494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                continue;
61594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            }
61694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            if (cacheDir == NULL) {
61794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                cacheDir = _add_cache_dir_t(cache, parentDir, dirName);
61894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            }
61994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            if (cacheDir != NULL) {
62094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                // Update pathBase for the new path...  this may change dirName
62194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                // if that is also pointing to the path, but we are done with it
62294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                // now.
62394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                size_t finallen = snprintf(pathPos, pathAvailLen, "/%s", name);
62494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                CACHE_NOISY(ALOGI("Collecting dir %s\n", pathBase));
62594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                if (finallen < pathAvailLen) {
62694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                    _add_cache_files(cache, cacheDir, name, subdir, pathBase,
62794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                            pathPos+finallen, pathAvailLen-finallen);
62894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                } else {
62994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                    // Whoops, the final path is too long!  We'll just delete
63094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                    // this directory.
63194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                    ALOGW("Cache dir %s truncated in path %s; deleting dir\n",
63294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                            name, pathBase);
63394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                    _delete_dir_contents(subdir, NULL);
63494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                    if (unlinkat(dfd, name, AT_REMOVEDIR) < 0) {
63594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                        ALOGE("Couldn't unlinkat %s: %s\n", name, strerror(errno));
63694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                    }
63794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                }
63894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            }
63994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            closedir(subdir);
64094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        } else if (de->d_type == DT_REG) {
64194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            // Skip files that start with '.'; they will be deleted if
64294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            // their entire directory is deleted.  This allows for metadata
64394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            // like ".nomedia" to remain in the directory until the entire
64494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            // directory is deleted.
64594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            if (cacheDir == NULL) {
64694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                cacheDir = _add_cache_dir_t(cache, parentDir, dirName);
64794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            }
64894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            if (name[0] == '.') {
64994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                cacheDir->hiddenCount++;
65094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                continue;
65194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            }
65294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            if (cacheDir != NULL) {
65394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                // Build final full path for file...  this may change dirName
65494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                // if that is also pointing to the path, but we are done with it
65594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                // now.
65694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                size_t finallen = snprintf(pathPos, pathAvailLen, "/%s", name);
65794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                CACHE_NOISY(ALOGI("Collecting file %s\n", pathBase));
65894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                if (finallen < pathAvailLen) {
65994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                    struct stat s;
66094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                    if (stat(pathBase, &s) >= 0) {
66194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                        _add_cache_file_t(cache, cacheDir, s.st_mtime, name);
66294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                    } else {
66394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                        ALOGW("Unable to stat cache file %s; deleting\n", pathBase);
66494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                        if (unlink(pathBase) < 0) {
66594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                            ALOGE("Couldn't unlink %s: %s\n", pathBase, strerror(errno));
66694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                        }
66794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                    }
66894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                } else {
66994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                    // Whoops, the final path is too long!  We'll just delete
67094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                    // this file.
67194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                    ALOGW("Cache file %s truncated in path %s; deleting\n",
67294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                            name, pathBase);
67394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                    if (unlinkat(dfd, name, 0) < 0) {
67494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                        *pathPos = 0;
67594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                        ALOGE("Couldn't unlinkat %s in %s: %s\n", name, pathBase,
67694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                                strerror(errno));
67794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                    }
67894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                }
67994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            }
68094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        } else {
68194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            cacheDir->hiddenCount++;
68294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        }
68394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
68494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    return 0;
68594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
68694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
68794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwoodvoid add_cache_files(cache_t* cache, const char *basepath, const char *cachedir)
68894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood{
68994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    DIR *d;
69094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    struct dirent *de;
69194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    char dirname[PATH_MAX];
69294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
69394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    CACHE_NOISY(ALOGI("add_cache_files: base=%s cachedir=%s\n", basepath, cachedir));
69494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
69594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    d = opendir(basepath);
69694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    if (d == NULL) {
69794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        return;
69894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
69994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
70094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    while ((de = readdir(d))) {
70194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        if (de->d_type == DT_DIR) {
70294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            DIR* subdir;
70394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            const char *name = de->d_name;
70494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            char* pathpos;
70594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
70694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                /* always skip "." and ".." */
70794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            if (name[0] == '.') {
70894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                if (name[1] == 0) continue;
70994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                if ((name[1] == '.') && (name[2] == 0)) continue;
71094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            }
71194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
71294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            strcpy(dirname, basepath);
71394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            pathpos = dirname + strlen(dirname);
71494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            if ((*(pathpos-1)) != '/') {
71594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                *pathpos = '/';
71694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                pathpos++;
71794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                *pathpos = 0;
71894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            }
71994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            if (cachedir != NULL) {
72094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                snprintf(pathpos, sizeof(dirname)-(pathpos-dirname), "%s/%s", name, cachedir);
72194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            } else {
72294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                snprintf(pathpos, sizeof(dirname)-(pathpos-dirname), "%s", name);
72394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            }
72494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            CACHE_NOISY(ALOGI("Adding cache files from dir: %s\n", dirname));
72594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            subdir = opendir(dirname);
72694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            if (subdir != NULL) {
72794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                size_t dirnameLen = strlen(dirname);
72894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                _add_cache_files(cache, NULL, dirname, subdir, dirname, dirname+dirnameLen,
72994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                        PATH_MAX - dirnameLen);
73094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                closedir(subdir);
73194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            }
73294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        }
73394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
73494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
73594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    closedir(d);
73694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
73794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
73894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwoodstatic char *create_dir_path(char path[PATH_MAX], cache_dir_t* dir)
73994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood{
74094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    char *pos = path;
74194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    if (dir->parent != NULL) {
74294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        pos = create_dir_path(path, dir->parent);
74394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
74494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    // Note that we don't need to worry about going beyond the buffer,
74594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    // since when we were constructing the cache entries our maximum
74694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    // buffer size for full paths was PATH_MAX.
74794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    strcpy(pos, dir->name);
74894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    pos += strlen(pos);
74994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    *pos = '/';
75094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    pos++;
75194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    *pos = 0;
75294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    return pos;
75394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
75494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
75594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwoodstatic void delete_cache_dir(char path[PATH_MAX], cache_dir_t* dir)
75694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood{
75794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    if (dir->parent != NULL) {
75894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        create_dir_path(path, dir);
75994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        ALOGI("DEL DIR %s\n", path);
76094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        if (dir->hiddenCount <= 0) {
76194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            if (rmdir(path)) {
76294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                ALOGE("Couldn't rmdir %s: %s\n", path, strerror(errno));
76394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                return;
76494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            }
76594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        } else {
76694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            // The directory contains hidden files so we need to delete
76794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            // them along with the directory itself.
76894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            if (delete_dir_contents(path, 1, NULL)) {
76994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                return;
77094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            }
77194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        }
77294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        dir->parent->childCount--;
77394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        dir->deleted = 1;
77494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        if (dir->parent->childCount <= 0) {
77594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            delete_cache_dir(path, dir->parent);
77694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        }
77794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    } else if (dir->hiddenCount > 0) {
77894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        // This is a root directory, but it has hidden files.  Get rid of
77994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        // all of those files, but not the directory itself.
78094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        create_dir_path(path, dir);
78194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        ALOGI("DEL CONTENTS %s\n", path);
78294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        delete_dir_contents(path, 0, NULL);
78394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
78494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
78594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
78694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwoodstatic int cache_modtime_sort(const void *lhsP, const void *rhsP)
78794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood{
78894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    const cache_file_t *lhs = *(const cache_file_t**)lhsP;
78994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    const cache_file_t *rhs = *(const cache_file_t**)rhsP;
79094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    return lhs->modTime < rhs->modTime ? -1 : (lhs->modTime > rhs->modTime ? 1 : 0);
79194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
79294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
79341ea424413c0381ef2aa15fc5bd5d4b88abd23c4Jeff Sharkeyvoid clear_cache_files(const std::string& data_path, cache_t* cache, int64_t free_size)
79494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood{
79594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    size_t i;
79694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    int skip = 0;
79794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    char path[PATH_MAX];
79894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
79992dc3fc52cf097bd105460cf377779bdcf146d62Mark Salyzyn    ALOGI("Collected cache files: %zd directories, %zd files",
80094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        cache->numDirs, cache->numFiles);
80194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
80294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    CACHE_NOISY(ALOGI("Sorting files..."));
80394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    qsort(cache->files, cache->numFiles, sizeof(cache_file_t*),
80494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            cache_modtime_sort);
80594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
80694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    CACHE_NOISY(ALOGI("Cleaning empty directories..."));
80794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    for (i=cache->numDirs; i>0; i--) {
80894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        cache_dir_t* dir = cache->dirs[i-1];
80994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        if (dir->childCount <= 0 && !dir->deleted) {
81094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            delete_cache_dir(path, dir);
81194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        }
81294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
81394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
81494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    CACHE_NOISY(ALOGI("Trimming files..."));
81594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    for (i=0; i<cache->numFiles; i++) {
81694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        skip++;
81794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        if (skip > 10) {
81841ea424413c0381ef2aa15fc5bd5d4b88abd23c4Jeff Sharkey            if (data_disk_free(data_path) > free_size) {
81994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                return;
82094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            }
82194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            skip = 0;
82294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        }
82394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        cache_file_t* file = cache->files[i];
82494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        strcpy(create_dir_path(path, file->dir), file->name);
82594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        ALOGI("DEL (mod %d) %s\n", (int)file->modTime, path);
82694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        if (unlink(path) < 0) {
82794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            ALOGE("Couldn't unlink %s: %s\n", path, strerror(errno));
82894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        }
82994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        file->dir->childCount--;
83094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        if (file->dir->childCount <= 0) {
83194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            delete_cache_dir(path, file->dir);
83294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        }
83394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
83494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
83594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
83694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwoodvoid finish_cache_collection(cache_t* cache)
83794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood{
83899d9fb15b4a1eb534261ae81b2cf25aec4447bd0Chih-Hung Hsieh    CACHE_NOISY(size_t i;)
83994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
84094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    CACHE_NOISY(ALOGI("clear_cache_files: %d dirs, %d files\n", cache->numDirs, cache->numFiles));
84194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    CACHE_NOISY(
84294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        for (i=0; i<cache->numDirs; i++) {
84394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            cache_dir_t* dir = cache->dirs[i];
84494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            ALOGI("dir #%d: %p %s parent=%p\n", i, dir, dir->name, dir->parent);
84594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        })
84694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    CACHE_NOISY(
84794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        for (i=0; i<cache->numFiles; i++) {
84894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            cache_file_t* file = cache->files[i];
84994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            ALOGI("file #%d: %p %s time=%d dir=%p\n", i, file, file->name,
85094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                    (int)file->modTime, file->dir);
85194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        })
85294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    void* block = cache->memBlocks;
85394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    while (block != NULL) {
85494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        void* nextBlock = *(void**)block;
85594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        CACHE_NOISY(ALOGI("Freeing cache mem block: %p", block));
85694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        free(block);
85794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        block = nextBlock;
85894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
85994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    free(cache);
86094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
86194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
86294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood/**
863c597b6dd895dbb2b28c757ce7a2651b3cdc9b00cCalin Juravle * Validate that the path is valid in the context of the provided directory.
864c597b6dd895dbb2b28c757ce7a2651b3cdc9b00cCalin Juravle * The path is allowed to have at most one subdirectory and no indirections
865c597b6dd895dbb2b28c757ce7a2651b3cdc9b00cCalin Juravle * to top level directories (i.e. have "..").
866c597b6dd895dbb2b28c757ce7a2651b3cdc9b00cCalin Juravle */
867e23a13299a4f6f2488935b4786cdbb46f46e3d3cJeff Sharkeystatic int validate_path(const dir_rec_t* dir, const char* path, int maxSubdirs) {
868c597b6dd895dbb2b28c757ce7a2651b3cdc9b00cCalin Juravle    size_t dir_len = dir->len;
869c597b6dd895dbb2b28c757ce7a2651b3cdc9b00cCalin Juravle    const char* subdir = strchr(path + dir_len, '/');
870c597b6dd895dbb2b28c757ce7a2651b3cdc9b00cCalin Juravle
871c597b6dd895dbb2b28c757ce7a2651b3cdc9b00cCalin Juravle    // Only allow the path to have at most one subdirectory.
872c597b6dd895dbb2b28c757ce7a2651b3cdc9b00cCalin Juravle    if (subdir != NULL) {
873c597b6dd895dbb2b28c757ce7a2651b3cdc9b00cCalin Juravle        ++subdir;
874e23a13299a4f6f2488935b4786cdbb46f46e3d3cJeff Sharkey        if ((--maxSubdirs == 0) && strchr(subdir, '/') != NULL) {
875c597b6dd895dbb2b28c757ce7a2651b3cdc9b00cCalin Juravle            ALOGE("invalid apk path '%s' (subdir?)\n", path);
876c597b6dd895dbb2b28c757ce7a2651b3cdc9b00cCalin Juravle            return -1;
877c597b6dd895dbb2b28c757ce7a2651b3cdc9b00cCalin Juravle        }
878c597b6dd895dbb2b28c757ce7a2651b3cdc9b00cCalin Juravle    }
879c597b6dd895dbb2b28c757ce7a2651b3cdc9b00cCalin Juravle
880c597b6dd895dbb2b28c757ce7a2651b3cdc9b00cCalin Juravle    // Directories can't have a period directly after the directory markers to prevent "..".
881c597b6dd895dbb2b28c757ce7a2651b3cdc9b00cCalin Juravle    if ((path[dir_len] == '.') || ((subdir != NULL) && (*subdir == '.'))) {
882c597b6dd895dbb2b28c757ce7a2651b3cdc9b00cCalin Juravle        ALOGE("invalid apk path '%s' (trickery)\n", path);
883c597b6dd895dbb2b28c757ce7a2651b3cdc9b00cCalin Juravle        return -1;
884c597b6dd895dbb2b28c757ce7a2651b3cdc9b00cCalin Juravle    }
885c597b6dd895dbb2b28c757ce7a2651b3cdc9b00cCalin Juravle
886c597b6dd895dbb2b28c757ce7a2651b3cdc9b00cCalin Juravle    return 0;
887c597b6dd895dbb2b28c757ce7a2651b3cdc9b00cCalin Juravle}
888c597b6dd895dbb2b28c757ce7a2651b3cdc9b00cCalin Juravle
889c597b6dd895dbb2b28c757ce7a2651b3cdc9b00cCalin Juravle/**
89094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood * Checks whether a path points to a system app (.apk file). Returns 0
89194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood * if it is a system app or -1 if it is not.
89294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood */
89394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwoodint validate_system_app_path(const char* path) {
89494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    size_t i;
89594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
89694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    for (i = 0; i < android_system_dirs.count; i++) {
89794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        const size_t dir_len = android_system_dirs.dirs[i].len;
89894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        if (!strncmp(path, android_system_dirs.dirs[i].path, dir_len)) {
899e23a13299a4f6f2488935b4786cdbb46f46e3d3cJeff Sharkey            return validate_path(android_system_dirs.dirs + i, path, 1);
90094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        }
90194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
90294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
90394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    return -1;
90494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
90594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
90694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood/**
90794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood * Get the contents of a environment variable that contains a path. Caller
90894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood * owns the string that is inserted into the directory record. Returns
90994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood * 0 on success and -1 on error.
91094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood */
91194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwoodint get_path_from_env(dir_rec_t* rec, const char* var) {
91294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    const char* path = getenv(var);
91394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    int ret = get_path_from_string(rec, path);
91494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    if (ret < 0) {
91594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        ALOGW("Problem finding value for environment variable %s\n", var);
91694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
91794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    return ret;
91894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
91994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
92094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood/**
92194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood * Puts the string into the record as a directory. Appends '/' to the end
92294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood * of all paths. Caller owns the string that is inserted into the directory
92394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood * record. A null value will result in an error.
92494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood *
92594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood * Returns 0 on success and -1 on error.
92694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood */
92794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwoodint get_path_from_string(dir_rec_t* rec, const char* path) {
92894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    if (path == NULL) {
92994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        return -1;
93094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    } else {
93194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        const size_t path_len = strlen(path);
93294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        if (path_len <= 0) {
93394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            return -1;
93494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        }
93594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
93694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        // Make sure path is absolute.
93794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        if (path[0] != '/') {
93894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            return -1;
93994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        }
94094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
94194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        if (path[path_len - 1] == '/') {
94294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            // Path ends with a forward slash. Make our own copy.
94394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
94494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            rec->path = strdup(path);
94594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            if (rec->path == NULL) {
94694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                return -1;
94794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            }
94894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
94994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            rec->len = path_len;
95094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        } else {
95194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            // Path does not end with a slash. Generate a new string.
95294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            char *dst;
95394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
95494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            // Add space for slash and terminating null.
95594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            size_t dst_size = path_len + 2;
95694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
95719803807cd7ae01868fcfa50305f4a7dd13765e2Jeff Sharkey            rec->path = (char*) malloc(dst_size);
95894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            if (rec->path == NULL) {
95994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                return -1;
96094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            }
96194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
96294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            dst = rec->path;
96394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
96494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            if (append_and_increment(&dst, path, &dst_size) < 0
96594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                    || append_and_increment(&dst, "/", &dst_size)) {
96694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                ALOGE("Error canonicalizing path");
96794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                return -1;
96894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            }
96994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
97094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            rec->len = dst - rec->path;
97194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        }
97294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
97394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    return 0;
97494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
97594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
97694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwoodint copy_and_append(dir_rec_t* dst, const dir_rec_t* src, const char* suffix) {
97794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    dst->len = src->len + strlen(suffix);
97894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    const size_t dstSize = dst->len + 1;
97994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    dst->path = (char*) malloc(dstSize);
98094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
98194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    if (dst->path == NULL
98294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            || snprintf(dst->path, dstSize, "%s%s", src->path, suffix)
98394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                    != (ssize_t) dst->len) {
98494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        ALOGE("Could not allocate memory to hold appended path; aborting\n");
98594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        return -1;
98694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
98794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
98894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    return 0;
98994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
99094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
99194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood/**
992fd88ff2edd954117e36372fb095b6f5f35aad0e3Calin Juravle * Check whether path points to a valid path for an APK file. Only one level of
993fd88ff2edd954117e36372fb095b6f5f35aad0e3Calin Juravle * subdirectory names is allowed. Returns -1 when an invalid path is encountered
994fd88ff2edd954117e36372fb095b6f5f35aad0e3Calin Juravle * and 0 when a valid path is encountered.
99594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood */
99694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwoodint validate_apk_path(const char *path)
99794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood{
998c597b6dd895dbb2b28c757ce7a2651b3cdc9b00cCalin Juravle    const dir_rec_t* dir = NULL;
999e23a13299a4f6f2488935b4786cdbb46f46e3d3cJeff Sharkey    int maxSubdirs = 1;
100094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
100194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    if (!strncmp(path, android_app_dir.path, android_app_dir.len)) {
1002c597b6dd895dbb2b28c757ce7a2651b3cdc9b00cCalin Juravle        dir = &android_app_dir;
100394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    } else if (!strncmp(path, android_app_private_dir.path, android_app_private_dir.len)) {
1004c597b6dd895dbb2b28c757ce7a2651b3cdc9b00cCalin Juravle        dir = &android_app_private_dir;
100594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    } else if (!strncmp(path, android_asec_dir.path, android_asec_dir.len)) {
1006c597b6dd895dbb2b28c757ce7a2651b3cdc9b00cCalin Juravle        dir = &android_asec_dir;
1007e23a13299a4f6f2488935b4786cdbb46f46e3d3cJeff Sharkey    } else if (!strncmp(path, android_mnt_expand_dir.path, android_mnt_expand_dir.len)) {
1008e23a13299a4f6f2488935b4786cdbb46f46e3d3cJeff Sharkey        dir = &android_mnt_expand_dir;
1009e23a13299a4f6f2488935b4786cdbb46f46e3d3cJeff Sharkey        maxSubdirs = 2;
101094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    } else {
101194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        return -1;
101294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
101394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
1014e23a13299a4f6f2488935b4786cdbb46f46e3d3cJeff Sharkey    return validate_path(dir, path, maxSubdirs);
101594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
101694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
101794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwoodint append_and_increment(char** dst, const char* src, size_t* dst_size) {
101894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    ssize_t ret = strlcpy(*dst, src, *dst_size);
101994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    if (ret < 0 || (size_t) ret >= *dst_size) {
102094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        return -1;
102194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
102294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    *dst += ret;
102394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    *dst_size -= ret;
102494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    return 0;
102594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
102694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
102719803807cd7ae01868fcfa50305f4a7dd13765e2Jeff Sharkeychar *build_string2(const char *s1, const char *s2) {
102894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    if (s1 == NULL || s2 == NULL) return NULL;
102994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
103094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    int len_s1 = strlen(s1);
103194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    int len_s2 = strlen(s2);
103294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    int len = len_s1 + len_s2 + 1;
103319803807cd7ae01868fcfa50305f4a7dd13765e2Jeff Sharkey    char *result = (char *) malloc(len);
103494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    if (result == NULL) return NULL;
103594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
103694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    strcpy(result, s1);
103794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    strcpy(result + len_s1, s2);
103894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
103994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    return result;
104094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
104194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
104219803807cd7ae01868fcfa50305f4a7dd13765e2Jeff Sharkeychar *build_string3(const char *s1, const char *s2, const char *s3) {
104394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    if (s1 == NULL || s2 == NULL || s3 == NULL) return NULL;
104494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
104594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    int len_s1 = strlen(s1);
104694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    int len_s2 = strlen(s2);
104794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    int len_s3 = strlen(s3);
104894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    int len = len_s1 + len_s2 + len_s3 + 1;
104919803807cd7ae01868fcfa50305f4a7dd13765e2Jeff Sharkey    char *result = (char *) malloc(len);
105094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    if (result == NULL) return NULL;
105194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
105294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    strcpy(result, s1);
105394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    strcpy(result + len_s1, s2);
105494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    strcpy(result + len_s1 + len_s2, s3);
105594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
105694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    return result;
105794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
105894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
105994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood/* Ensure that /data/media directories are prepared for given user. */
106041ea424413c0381ef2aa15fc5bd5d4b88abd23c4Jeff Sharkeyint ensure_media_user_dirs(const char* uuid, userid_t userid) {
106141ea424413c0381ef2aa15fc5bd5d4b88abd23c4Jeff Sharkey    std::string media_user_path(create_data_media_path(uuid, userid));
106241ea424413c0381ef2aa15fc5bd5d4b88abd23c4Jeff Sharkey    if (fs_prepare_dir(media_user_path.c_str(), 0770, AID_MEDIA_RW, AID_MEDIA_RW) == -1) {
106394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        return -1;
106494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
106594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
106694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    return 0;
106794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
1068d93707342a61e66bc3eb2145628158452f577f42Dave Allison
1069095c763dd9aa26a206d10ab7c1d7e1c569298fb3Robin Leeint ensure_config_user_dirs(userid_t userid) {
1070095c763dd9aa26a206d10ab7c1d7e1c569298fb3Robin Lee    char config_user_path[PATH_MAX];
1071095c763dd9aa26a206d10ab7c1d7e1c569298fb3Robin Lee
1072095c763dd9aa26a206d10ab7c1d7e1c569298fb3Robin Lee    // writable by system, readable by any app within the same user
107360fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee    const int uid = multiuser_get_uid(userid, AID_SYSTEM);
107460fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee    const int gid = multiuser_get_uid(userid, AID_EVERYBODY);
1075095c763dd9aa26a206d10ab7c1d7e1c569298fb3Robin Lee
1076095c763dd9aa26a206d10ab7c1d7e1c569298fb3Robin Lee    // Ensure /data/misc/user/<userid> exists
1077095c763dd9aa26a206d10ab7c1d7e1c569298fb3Robin Lee    create_user_config_path(config_user_path, userid);
1078095c763dd9aa26a206d10ab7c1d7e1c569298fb3Robin Lee    if (fs_prepare_dir(config_user_path, 0750, uid, gid) == -1) {
1079095c763dd9aa26a206d10ab7c1d7e1c569298fb3Robin Lee        return -1;
1080095c763dd9aa26a206d10ab7c1d7e1c569298fb3Robin Lee    }
1081095c763dd9aa26a206d10ab7c1d7e1c569298fb3Robin Lee
1082095c763dd9aa26a206d10ab7c1d7e1c569298fb3Robin Lee   return 0;
1083095c763dd9aa26a206d10ab7c1d7e1c569298fb3Robin Lee}
1084095c763dd9aa26a206d10ab7c1d7e1c569298fb3Robin Lee
1085d93707342a61e66bc3eb2145628158452f577f42Dave Allisonint create_profile_file(const char *pkgname, gid_t gid) {
1086d93707342a61e66bc3eb2145628158452f577f42Dave Allison    const char *profile_dir = DALVIK_CACHE_PREFIX "profiles";
1087d93707342a61e66bc3eb2145628158452f577f42Dave Allison    char profile_file[PKG_PATH_MAX];
1088d93707342a61e66bc3eb2145628158452f577f42Dave Allison
1089d93707342a61e66bc3eb2145628158452f577f42Dave Allison    snprintf(profile_file, sizeof(profile_file), "%s/%s", profile_dir, pkgname);
1090d93707342a61e66bc3eb2145628158452f577f42Dave Allison
1091d93707342a61e66bc3eb2145628158452f577f42Dave Allison    // The 'system' user needs to be able to read the profile to determine if dex2oat
1092d93707342a61e66bc3eb2145628158452f577f42Dave Allison    // needs to be run.  This is done in dalvik.system.DexFile.isDexOptNeededInternal().  So
10930db0f97d87bae8118e135d5a30edf7f0146098c0Nick Kralevich    // we assign ownership to AID_SYSTEM and ensure it's not world-readable.
1094d93707342a61e66bc3eb2145628158452f577f42Dave Allison
10950db0f97d87bae8118e135d5a30edf7f0146098c0Nick Kralevich    int fd = open(profile_file, O_WRONLY | O_CREAT | O_NOFOLLOW | O_CLOEXEC, 0660);
1096d93707342a61e66bc3eb2145628158452f577f42Dave Allison
10970db0f97d87bae8118e135d5a30edf7f0146098c0Nick Kralevich    // Always set the uid/gid/permissions. The file could have been previously created
10980db0f97d87bae8118e135d5a30edf7f0146098c0Nick Kralevich    // with different permissions.
1099d93707342a61e66bc3eb2145628158452f577f42Dave Allison    if (fd >= 0) {
11000db0f97d87bae8118e135d5a30edf7f0146098c0Nick Kralevich        if (fchown(fd, AID_SYSTEM, gid) < 0) {
1101d93707342a61e66bc3eb2145628158452f577f42Dave Allison            ALOGE("cannot chown profile file '%s': %s\n", profile_file, strerror(errno));
1102d93707342a61e66bc3eb2145628158452f577f42Dave Allison            close(fd);
1103d93707342a61e66bc3eb2145628158452f577f42Dave Allison            unlink(profile_file);
1104d93707342a61e66bc3eb2145628158452f577f42Dave Allison            return -1;
1105d93707342a61e66bc3eb2145628158452f577f42Dave Allison        }
1106d93707342a61e66bc3eb2145628158452f577f42Dave Allison
11070db0f97d87bae8118e135d5a30edf7f0146098c0Nick Kralevich        if (fchmod(fd, 0660) < 0) {
1108d93707342a61e66bc3eb2145628158452f577f42Dave Allison            ALOGE("cannot chmod profile file '%s': %s\n", profile_file, strerror(errno));
1109d93707342a61e66bc3eb2145628158452f577f42Dave Allison            close(fd);
1110d93707342a61e66bc3eb2145628158452f577f42Dave Allison            unlink(profile_file);
1111d93707342a61e66bc3eb2145628158452f577f42Dave Allison            return -1;
1112d93707342a61e66bc3eb2145628158452f577f42Dave Allison        }
1113d93707342a61e66bc3eb2145628158452f577f42Dave Allison        close(fd);
1114d93707342a61e66bc3eb2145628158452f577f42Dave Allison    }
1115d93707342a61e66bc3eb2145628158452f577f42Dave Allison    return 0;
1116d93707342a61e66bc3eb2145628158452f577f42Dave Allison}
1117d93707342a61e66bc3eb2145628158452f577f42Dave Allison
1118d93707342a61e66bc3eb2145628158452f577f42Dave Allisonvoid remove_profile_file(const char *pkgname) {
1119d93707342a61e66bc3eb2145628158452f577f42Dave Allison    char profile_file[PKG_PATH_MAX];
1120d93707342a61e66bc3eb2145628158452f577f42Dave Allison    snprintf(profile_file, sizeof(profile_file), "%s/%s", DALVIK_CACHE_PREFIX "profiles", pkgname);
1121d93707342a61e66bc3eb2145628158452f577f42Dave Allison    unlink(profile_file);
1122d93707342a61e66bc3eb2145628158452f577f42Dave Allison}
1123