utils.cpp revision 3aee2c5c749dc2589f001b26fae1ec958ec89524
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
1994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood#define CACHE_NOISY(x) //x
2094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
2194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwoodint create_pkg_path_in_dir(char path[PKG_PATH_MAX],
2294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                                const dir_rec_t* dir,
2394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                                const char* pkgname,
2494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                                const char* postfix)
2594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood{
2694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood     const size_t postfix_len = strlen(postfix);
2794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
2894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood     const size_t pkgname_len = strlen(pkgname);
2994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood     if (pkgname_len > PKG_NAME_MAX) {
3094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood         return -1;
3194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood     }
3294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
3394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood     if (is_valid_package_name(pkgname) < 0) {
3494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood         return -1;
3594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood     }
3694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
3794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood     if ((pkgname_len + dir->len + postfix_len) >= PKG_PATH_MAX) {
3894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood         return -1;
3994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood     }
4094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
4194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood     char *dst = path;
4294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood     size_t dst_size = PKG_PATH_MAX;
4394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
4494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood     if (append_and_increment(&dst, dir->path, &dst_size) < 0
4594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood             || append_and_increment(&dst, pkgname, &dst_size) < 0
4694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood             || append_and_increment(&dst, postfix, &dst_size) < 0) {
4794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood         ALOGE("Error building APK path");
4894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood         return -1;
4994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood     }
5094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
5194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood     return 0;
5294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
5394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
5494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood/**
5594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood * Create the package path name for a given package name with a postfix for
56abe4fe5b46157ecd2a52d28abf938c816c3ce878Jeff Sharkey * a certain userid. Returns 0 on success, and -1 on failure.
5794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood */
5894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwoodint create_pkg_path(char path[PKG_PATH_MAX],
5994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                    const char *pkgname,
6094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                    const char *postfix,
61abe4fe5b46157ecd2a52d28abf938c816c3ce878Jeff Sharkey                    userid_t userid)
6294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood{
63abe4fe5b46157ecd2a52d28abf938c816c3ce878Jeff Sharkey    size_t userid_len;
64abe4fe5b46157ecd2a52d28abf938c816c3ce878Jeff Sharkey    char* userid_prefix;
65abe4fe5b46157ecd2a52d28abf938c816c3ce878Jeff Sharkey    if (userid == 0) {
66abe4fe5b46157ecd2a52d28abf938c816c3ce878Jeff Sharkey        userid_prefix = PRIMARY_USER_PREFIX;
67abe4fe5b46157ecd2a52d28abf938c816c3ce878Jeff Sharkey        userid_len = 0;
6894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    } else {
69abe4fe5b46157ecd2a52d28abf938c816c3ce878Jeff Sharkey        userid_prefix = SECONDARY_USER_PREFIX;
70abe4fe5b46157ecd2a52d28abf938c816c3ce878Jeff Sharkey        userid_len = snprintf(NULL, 0, "%d", userid);
7194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
7294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
73abe4fe5b46157ecd2a52d28abf938c816c3ce878Jeff Sharkey    const size_t prefix_len = android_data_dir.len + strlen(userid_prefix)
74abe4fe5b46157ecd2a52d28abf938c816c3ce878Jeff Sharkey            + userid_len + 1 /*slash*/;
7594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    char prefix[prefix_len + 1];
7694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
7794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    char *dst = prefix;
7894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    size_t dst_size = sizeof(prefix);
7994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
8094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    if (append_and_increment(&dst, android_data_dir.path, &dst_size) < 0
81abe4fe5b46157ecd2a52d28abf938c816c3ce878Jeff Sharkey            || append_and_increment(&dst, userid_prefix, &dst_size) < 0) {
8294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        ALOGE("Error building prefix for APK path");
8394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        return -1;
8494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
8594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
86abe4fe5b46157ecd2a52d28abf938c816c3ce878Jeff Sharkey    if (userid != 0) {
87abe4fe5b46157ecd2a52d28abf938c816c3ce878Jeff Sharkey        int ret = snprintf(dst, dst_size, "%d/", userid);
88abe4fe5b46157ecd2a52d28abf938c816c3ce878Jeff Sharkey        if (ret < 0 || (size_t) ret != userid_len + 1) {
8994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            ALOGW("Error appending UID to APK path");
9094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            return -1;
9194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        }
9294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
9394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
9494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    dir_rec_t dir;
9594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    dir.path = prefix;
9694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    dir.len = prefix_len;
9794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
9894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    return create_pkg_path_in_dir(path, &dir, pkgname, postfix);
9994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
10094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
10194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood/**
102abe4fe5b46157ecd2a52d28abf938c816c3ce878Jeff Sharkey * Create the path name for user data for a certain userid.
10394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood * Returns 0 on success, and -1 on failure.
10494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood */
105abe4fe5b46157ecd2a52d28abf938c816c3ce878Jeff Sharkeyint create_user_path(char path[PKG_PATH_MAX],
106abe4fe5b46157ecd2a52d28abf938c816c3ce878Jeff Sharkey                    userid_t userid)
10794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood{
108abe4fe5b46157ecd2a52d28abf938c816c3ce878Jeff Sharkey    size_t userid_len;
109abe4fe5b46157ecd2a52d28abf938c816c3ce878Jeff Sharkey    char* userid_prefix;
110abe4fe5b46157ecd2a52d28abf938c816c3ce878Jeff Sharkey    if (userid == 0) {
111abe4fe5b46157ecd2a52d28abf938c816c3ce878Jeff Sharkey        userid_prefix = PRIMARY_USER_PREFIX;
112abe4fe5b46157ecd2a52d28abf938c816c3ce878Jeff Sharkey        userid_len = 0;
11394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    } else {
114abe4fe5b46157ecd2a52d28abf938c816c3ce878Jeff Sharkey        userid_prefix = SECONDARY_USER_PREFIX;
115abe4fe5b46157ecd2a52d28abf938c816c3ce878Jeff Sharkey        userid_len = snprintf(NULL, 0, "%d/", userid);
11694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
11794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
11894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    char *dst = path;
11994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    size_t dst_size = PKG_PATH_MAX;
12094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
12194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    if (append_and_increment(&dst, android_data_dir.path, &dst_size) < 0
122abe4fe5b46157ecd2a52d28abf938c816c3ce878Jeff Sharkey            || append_and_increment(&dst, userid_prefix, &dst_size) < 0) {
12394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        ALOGE("Error building prefix for user path");
12494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        return -1;
12594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
12694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
127abe4fe5b46157ecd2a52d28abf938c816c3ce878Jeff Sharkey    if (userid != 0) {
128abe4fe5b46157ecd2a52d28abf938c816c3ce878Jeff Sharkey        if (dst_size < userid_len + 1) {
12994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            ALOGE("Error building user path");
13094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            return -1;
13194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        }
132abe4fe5b46157ecd2a52d28abf938c816c3ce878Jeff Sharkey        int ret = snprintf(dst, dst_size, "%d/", userid);
133abe4fe5b46157ecd2a52d28abf938c816c3ce878Jeff Sharkey        if (ret < 0 || (size_t) ret != userid_len) {
134abe4fe5b46157ecd2a52d28abf938c816c3ce878Jeff Sharkey            ALOGE("Error appending userid to path");
13594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            return -1;
13694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        }
13794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
13894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    return 0;
13994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
14094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
14194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood/**
142abe4fe5b46157ecd2a52d28abf938c816c3ce878Jeff Sharkey * Create the path name for media for a certain userid.
14394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood * Returns 0 on success, and -1 on failure.
14494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood */
145abe4fe5b46157ecd2a52d28abf938c816c3ce878Jeff Sharkeyint create_user_media_path(char path[PATH_MAX], userid_t userid) {
14694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    if (snprintf(path, PATH_MAX, "%s%d", android_media_dir.path, userid) > PATH_MAX) {
14794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        return -1;
14894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
14994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    return 0;
15094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
15194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
15294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwoodint create_move_path(char path[PKG_PATH_MAX],
15394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    const char* pkgname,
15494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    const char* leaf,
155abe4fe5b46157ecd2a52d28abf938c816c3ce878Jeff Sharkey    userid_t userid)
15694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood{
15794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    if ((android_data_dir.len + strlen(PRIMARY_USER_PREFIX) + strlen(pkgname) + strlen(leaf) + 1)
15894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            >= PKG_PATH_MAX) {
15994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        return -1;
16094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
16194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
16294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    sprintf(path, "%s%s%s/%s", android_data_dir.path, PRIMARY_USER_PREFIX, pkgname, leaf);
16394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    return 0;
16494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
16594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
16694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood/**
16794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood * Checks whether the package name is valid. Returns -1 on error and
16894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood * 0 on success.
16994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood */
17094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwoodint is_valid_package_name(const char* pkgname) {
17194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    const char *x = pkgname;
17294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    int alpha = -1;
17394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
17494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    while (*x) {
17594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        if (isalnum(*x) || (*x == '_')) {
17694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                /* alphanumeric or underscore are fine */
17794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        } else if (*x == '.') {
17894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            if ((x == pkgname) || (x[1] == '.') || (x[1] == 0)) {
17994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                    /* periods must not be first, last, or doubled */
18094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                ALOGE("invalid package name '%s'\n", pkgname);
18194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                return -1;
18294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            }
18394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        } else if (*x == '-') {
18494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            /* Suffix -X is fine to let versioning of packages.
18594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood               But whatever follows should be alphanumeric.*/
18694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            alpha = 1;
18794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        } else {
18894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                /* anything not A-Z, a-z, 0-9, _, or . is invalid */
18994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            ALOGE("invalid package name '%s'\n", pkgname);
19094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            return -1;
19194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        }
19294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
19394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        x++;
19494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
19594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
19694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    if (alpha == 1) {
19794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        // Skip current character
19894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        x++;
19994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        while (*x) {
20094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            if (!isalnum(*x)) {
20194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                ALOGE("invalid package name '%s' should include only numbers after -\n", pkgname);
20294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                return -1;
20394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            }
20494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            x++;
20594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        }
20694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
20794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
20894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    return 0;
20994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
21094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
2113aee2c5c749dc2589f001b26fae1ec958ec89524Narayan Kamathstatic int _delete_dir_contents(DIR *d,
2123aee2c5c749dc2589f001b26fae1ec958ec89524Narayan Kamath                                int (*exclusion_predicate)(const char *name, const int is_dir))
21394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood{
21494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    int result = 0;
21594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    struct dirent *de;
21694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    int dfd;
21794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
21894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    dfd = dirfd(d);
21994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
22094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    if (dfd < 0) return -1;
22194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
22294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    while ((de = readdir(d))) {
22394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        const char *name = de->d_name;
22494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
2253aee2c5c749dc2589f001b26fae1ec958ec89524Narayan Kamath            /* check using the exclusion predicate, if provided */
2263aee2c5c749dc2589f001b26fae1ec958ec89524Narayan Kamath        if (exclusion_predicate && exclusion_predicate(name, (de->d_type == DT_DIR))) {
2273aee2c5c749dc2589f001b26fae1ec958ec89524Narayan Kamath            continue;
2283aee2c5c749dc2589f001b26fae1ec958ec89524Narayan Kamath        }
22994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
23094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        if (de->d_type == DT_DIR) {
23194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            int r, subfd;
23294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            DIR *subdir;
23394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
23494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                /* always skip "." and ".." */
23594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            if (name[0] == '.') {
23694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                if (name[1] == 0) continue;
23794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                if ((name[1] == '.') && (name[2] == 0)) continue;
23894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            }
23994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
24094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            subfd = openat(dfd, name, O_RDONLY | O_DIRECTORY);
24194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            if (subfd < 0) {
24294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                ALOGE("Couldn't openat %s: %s\n", name, strerror(errno));
24394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                result = -1;
24494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                continue;
24594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            }
24694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            subdir = fdopendir(subfd);
24794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            if (subdir == NULL) {
24894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                ALOGE("Couldn't fdopendir %s: %s\n", name, strerror(errno));
24994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                close(subfd);
25094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                result = -1;
25194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                continue;
25294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            }
2533aee2c5c749dc2589f001b26fae1ec958ec89524Narayan Kamath            if (_delete_dir_contents(subdir, exclusion_predicate)) {
25494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                result = -1;
25594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            }
25694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            closedir(subdir);
25794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            if (unlinkat(dfd, name, AT_REMOVEDIR) < 0) {
25894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                ALOGE("Couldn't unlinkat %s: %s\n", name, strerror(errno));
25994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                result = -1;
26094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            }
26194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        } else {
26294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            if (unlinkat(dfd, name, 0) < 0) {
26394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                ALOGE("Couldn't unlinkat %s: %s\n", name, strerror(errno));
26494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                result = -1;
26594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            }
26694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        }
26794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
26894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
26994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    return result;
27094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
27194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
27294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwoodint delete_dir_contents(const char *pathname,
27394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                        int also_delete_dir,
2743aee2c5c749dc2589f001b26fae1ec958ec89524Narayan Kamath                        int (*exclusion_predicate)(const char*, const int))
27594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood{
27694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    int res = 0;
27794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    DIR *d;
27894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
27994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    d = opendir(pathname);
28094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    if (d == NULL) {
28194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        ALOGE("Couldn't opendir %s: %s\n", pathname, strerror(errno));
28294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        return -errno;
28394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
2843aee2c5c749dc2589f001b26fae1ec958ec89524Narayan Kamath    res = _delete_dir_contents(d, exclusion_predicate);
28594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    closedir(d);
28694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    if (also_delete_dir) {
28794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        if (rmdir(pathname)) {
28894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            ALOGE("Couldn't rmdir %s: %s\n", pathname, strerror(errno));
28994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            res = -1;
29094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        }
29194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
29294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    return res;
29394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
29494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
29594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwoodint delete_dir_contents_fd(int dfd, const char *name)
29694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood{
29794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    int fd, res;
29894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    DIR *d;
29994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
30094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    fd = openat(dfd, name, O_RDONLY | O_DIRECTORY);
30194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    if (fd < 0) {
30294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        ALOGE("Couldn't openat %s: %s\n", name, strerror(errno));
30394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        return -1;
30494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
30594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    d = fdopendir(fd);
30694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    if (d == NULL) {
30794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        ALOGE("Couldn't fdopendir %s: %s\n", name, strerror(errno));
30894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        close(fd);
30994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        return -1;
31094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
31194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    res = _delete_dir_contents(d, 0);
31294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    closedir(d);
31394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    return res;
31494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
31594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
31694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwoodint lookup_media_dir(char basepath[PATH_MAX], const char *dir)
31794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood{
31894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    DIR *d;
31994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    struct dirent *de;
32094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    struct stat s;
32194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    char* dirpos = basepath + strlen(basepath);
32294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
32394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    if ((*(dirpos-1)) != '/') {
32494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        *dirpos = '/';
32594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        dirpos++;
32694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
32794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
32894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    CACHE_NOISY(ALOGI("Looking up %s in %s\n", dir, basepath));
32994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    // Verify the path won't extend beyond our buffer, to avoid
33094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    // repeated checking later.
33194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    if ((dirpos-basepath+strlen(dir)) >= (PATH_MAX-1)) {
33294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        ALOGW("Path exceeds limit: %s%s", basepath, dir);
33394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        return -1;
33494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
33594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
33694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    // First, can we find this directory with the case that is given?
33794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    strcpy(dirpos, dir);
33894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    if (stat(basepath, &s) >= 0) {
33994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        CACHE_NOISY(ALOGI("Found direct: %s\n", basepath));
34094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        return 0;
34194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
34294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
34394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    // Not found with that case...  search through all entries to find
34494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    // one that matches regardless of case.
34594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    *dirpos = 0;
34694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
34794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    d = opendir(basepath);
34894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    if (d == NULL) {
34994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        return -1;
35094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
35194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
35294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    while ((de = readdir(d))) {
35394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        if (strcasecmp(de->d_name, dir) == 0) {
35494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            strcpy(dirpos, de->d_name);
35594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            closedir(d);
35694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            CACHE_NOISY(ALOGI("Found search: %s\n", basepath));
35794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            return 0;
35894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        }
35994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
36094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
36194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    ALOGW("Couldn't find %s in %s", dir, basepath);
36294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    closedir(d);
36394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    return -1;
36494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
36594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
36694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwoodint64_t data_disk_free()
36794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood{
36894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    struct statfs sfs;
36994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    if (statfs(android_data_dir.path, &sfs) == 0) {
37094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        return sfs.f_bavail * sfs.f_bsize;
37194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    } else {
37294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        ALOGE("Couldn't statfs %s: %s\n", android_data_dir.path, strerror(errno));
37394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        return -1;
37494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
37594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
37694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
37794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwoodcache_t* start_cache_collection()
37894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood{
37994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    cache_t* cache = (cache_t*)calloc(1, sizeof(cache_t));
38094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    return cache;
38194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
38294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
38394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood#define CACHE_BLOCK_SIZE (512*1024)
38494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
38594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwoodstatic void* _cache_malloc(cache_t* cache, size_t len)
38694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood{
38794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    len = (len+3)&~3;
38894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    if (len > (CACHE_BLOCK_SIZE/2)) {
38994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        // It doesn't make sense to try to put this allocation into one
39094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        // of our blocks, because it is so big.  Instead, make a new dedicated
39194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        // block for it.
39294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        int8_t* res = (int8_t*)malloc(len+sizeof(void*));
39394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        if (res == NULL) {
39494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            return NULL;
39594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        }
39694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        CACHE_NOISY(ALOGI("Allocated large cache mem block: %p size %d", res, len));
39794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        // Link it into our list of blocks, not disrupting the current one.
39894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        if (cache->memBlocks == NULL) {
39994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            *(void**)res = NULL;
40094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            cache->memBlocks = res;
40194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        } else {
40294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            *(void**)res = *(void**)cache->memBlocks;
40394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            *(void**)cache->memBlocks = res;
40494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        }
40594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        return res + sizeof(void*);
40694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
40794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    int8_t* res = cache->curMemBlockAvail;
40894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    int8_t* nextPos = res + len;
40994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    if (cache->memBlocks == NULL || nextPos > cache->curMemBlockEnd) {
41094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        int8_t* newBlock = malloc(CACHE_BLOCK_SIZE);
41194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        if (newBlock == NULL) {
41294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            return NULL;
41394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        }
41494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        CACHE_NOISY(ALOGI("Allocated new cache mem block: %p", newBlock));
41594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        *(void**)newBlock = cache->memBlocks;
41694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        cache->memBlocks = newBlock;
41794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        res = cache->curMemBlockAvail = newBlock + sizeof(void*);
41894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        cache->curMemBlockEnd = newBlock + CACHE_BLOCK_SIZE;
41994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        nextPos = res + len;
42094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
42194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    CACHE_NOISY(ALOGI("cache_malloc: ret %p size %d, block=%p, nextPos=%p",
42294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            res, len, cache->memBlocks, nextPos));
42394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    cache->curMemBlockAvail = nextPos;
42494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    return res;
42594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
42694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
42794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwoodstatic void* _cache_realloc(cache_t* cache, void* cur, size_t origLen, size_t len)
42894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood{
42994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    // This isn't really a realloc, but it is good enough for our purposes here.
43094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    void* alloc = _cache_malloc(cache, len);
43194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    if (alloc != NULL && cur != NULL) {
43294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        memcpy(alloc, cur, origLen < len ? origLen : len);
43394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
43494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    return alloc;
43594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
43694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
43794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwoodstatic void _inc_num_cache_collected(cache_t* cache)
43894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood{
43994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    cache->numCollected++;
44094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    if ((cache->numCollected%20000) == 0) {
44192dc3fc52cf097bd105460cf377779bdcf146d62Mark Salyzyn        ALOGI("Collected cache so far: %zd directories, %zd files",
44294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            cache->numDirs, cache->numFiles);
44394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
44494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
44594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
44694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwoodstatic cache_dir_t* _add_cache_dir_t(cache_t* cache, cache_dir_t* parent, const char *name)
44794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood{
44894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    size_t nameLen = strlen(name);
44994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    cache_dir_t* dir = (cache_dir_t*)_cache_malloc(cache, sizeof(cache_dir_t)+nameLen+1);
45094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    if (dir != NULL) {
45194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        dir->parent = parent;
45294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        dir->childCount = 0;
45394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        dir->hiddenCount = 0;
45494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        dir->deleted = 0;
45594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        strcpy(dir->name, name);
45694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        if (cache->numDirs >= cache->availDirs) {
45794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            size_t newAvail = cache->availDirs < 1000 ? 1000 : cache->availDirs*2;
45894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            cache_dir_t** newDirs = (cache_dir_t**)_cache_realloc(cache, cache->dirs,
45994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                    cache->availDirs*sizeof(cache_dir_t*), newAvail*sizeof(cache_dir_t*));
46094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            if (newDirs == NULL) {
46194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                ALOGE("Failure growing cache dirs array for %s\n", name);
46294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                return NULL;
46394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            }
46494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            cache->availDirs = newAvail;
46594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            cache->dirs = newDirs;
46694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        }
46794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        cache->dirs[cache->numDirs] = dir;
46894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        cache->numDirs++;
46994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        if (parent != NULL) {
47094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            parent->childCount++;
47194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        }
47294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        _inc_num_cache_collected(cache);
47394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    } else {
47494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        ALOGE("Failure allocating cache_dir_t for %s\n", name);
47594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
47694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    return dir;
47794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
47894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
47994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwoodstatic cache_file_t* _add_cache_file_t(cache_t* cache, cache_dir_t* dir, time_t modTime,
48094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        const char *name)
48194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood{
48294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    size_t nameLen = strlen(name);
48394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    cache_file_t* file = (cache_file_t*)_cache_malloc(cache, sizeof(cache_file_t)+nameLen+1);
48494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    if (file != NULL) {
48594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        file->dir = dir;
48694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        file->modTime = modTime;
48794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        strcpy(file->name, name);
48894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        if (cache->numFiles >= cache->availFiles) {
48994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            size_t newAvail = cache->availFiles < 1000 ? 1000 : cache->availFiles*2;
49094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            cache_file_t** newFiles = (cache_file_t**)_cache_realloc(cache, cache->files,
49194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                    cache->availFiles*sizeof(cache_file_t*), newAvail*sizeof(cache_file_t*));
49294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            if (newFiles == NULL) {
49394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                ALOGE("Failure growing cache file array for %s\n", name);
49494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                return NULL;
49594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            }
49694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            cache->availFiles = newAvail;
49794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            cache->files = newFiles;
49894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        }
49994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        CACHE_NOISY(ALOGI("Setting file %p at position %d in array %p", file,
50094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                cache->numFiles, cache->files));
50194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        cache->files[cache->numFiles] = file;
50294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        cache->numFiles++;
50394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        dir->childCount++;
50494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        _inc_num_cache_collected(cache);
50594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    } else {
50694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        ALOGE("Failure allocating cache_file_t for %s\n", name);
50794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
50894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    return file;
50994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
51094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
51194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwoodstatic int _add_cache_files(cache_t *cache, cache_dir_t *parentDir, const char *dirName,
51294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        DIR* dir, char *pathBase, char *pathPos, size_t pathAvailLen)
51394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood{
51494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    struct dirent *de;
51594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    cache_dir_t* cacheDir = NULL;
51694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    int dfd;
51794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
51894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    CACHE_NOISY(ALOGI("_add_cache_files: parent=%p dirName=%s dir=%p pathBase=%s",
51994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            parentDir, dirName, dir, pathBase));
52094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
52194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    dfd = dirfd(dir);
52294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
52394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    if (dfd < 0) return 0;
52494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
52594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    // Sub-directories always get added to the data structure, so if they
52694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    // are empty we will know about them to delete them later.
52794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    cacheDir = _add_cache_dir_t(cache, parentDir, dirName);
52894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
52994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    while ((de = readdir(dir))) {
53094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        const char *name = de->d_name;
53194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
53294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        if (de->d_type == DT_DIR) {
53394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            int subfd;
53494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            DIR *subdir;
53594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
53694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                /* always skip "." and ".." */
53794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            if (name[0] == '.') {
53894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                if (name[1] == 0) continue;
53994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                if ((name[1] == '.') && (name[2] == 0)) continue;
54094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            }
54194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
54294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            subfd = openat(dfd, name, O_RDONLY | O_DIRECTORY);
54394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            if (subfd < 0) {
54494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                ALOGE("Couldn't openat %s: %s\n", name, strerror(errno));
54594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                continue;
54694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            }
54794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            subdir = fdopendir(subfd);
54894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            if (subdir == NULL) {
54994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                ALOGE("Couldn't fdopendir %s: %s\n", name, strerror(errno));
55094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                close(subfd);
55194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                continue;
55294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            }
55394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            if (cacheDir == NULL) {
55494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                cacheDir = _add_cache_dir_t(cache, parentDir, dirName);
55594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            }
55694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            if (cacheDir != NULL) {
55794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                // Update pathBase for the new path...  this may change dirName
55894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                // if that is also pointing to the path, but we are done with it
55994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                // now.
56094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                size_t finallen = snprintf(pathPos, pathAvailLen, "/%s", name);
56194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                CACHE_NOISY(ALOGI("Collecting dir %s\n", pathBase));
56294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                if (finallen < pathAvailLen) {
56394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                    _add_cache_files(cache, cacheDir, name, subdir, pathBase,
56494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                            pathPos+finallen, pathAvailLen-finallen);
56594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                } else {
56694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                    // Whoops, the final path is too long!  We'll just delete
56794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                    // this directory.
56894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                    ALOGW("Cache dir %s truncated in path %s; deleting dir\n",
56994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                            name, pathBase);
57094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                    _delete_dir_contents(subdir, NULL);
57194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                    if (unlinkat(dfd, name, AT_REMOVEDIR) < 0) {
57294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                        ALOGE("Couldn't unlinkat %s: %s\n", name, strerror(errno));
57394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                    }
57494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                }
57594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            }
57694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            closedir(subdir);
57794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        } else if (de->d_type == DT_REG) {
57894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            // Skip files that start with '.'; they will be deleted if
57994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            // their entire directory is deleted.  This allows for metadata
58094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            // like ".nomedia" to remain in the directory until the entire
58194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            // directory is deleted.
58294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            if (cacheDir == NULL) {
58394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                cacheDir = _add_cache_dir_t(cache, parentDir, dirName);
58494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            }
58594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            if (name[0] == '.') {
58694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                cacheDir->hiddenCount++;
58794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                continue;
58894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            }
58994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            if (cacheDir != NULL) {
59094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                // Build final full path for file...  this may change dirName
59194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                // if that is also pointing to the path, but we are done with it
59294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                // now.
59394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                size_t finallen = snprintf(pathPos, pathAvailLen, "/%s", name);
59494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                CACHE_NOISY(ALOGI("Collecting file %s\n", pathBase));
59594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                if (finallen < pathAvailLen) {
59694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                    struct stat s;
59794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                    if (stat(pathBase, &s) >= 0) {
59894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                        _add_cache_file_t(cache, cacheDir, s.st_mtime, name);
59994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                    } else {
60094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                        ALOGW("Unable to stat cache file %s; deleting\n", pathBase);
60194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                        if (unlink(pathBase) < 0) {
60294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                            ALOGE("Couldn't unlink %s: %s\n", pathBase, strerror(errno));
60394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                        }
60494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                    }
60594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                } else {
60694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                    // Whoops, the final path is too long!  We'll just delete
60794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                    // this file.
60894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                    ALOGW("Cache file %s truncated in path %s; deleting\n",
60994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                            name, pathBase);
61094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                    if (unlinkat(dfd, name, 0) < 0) {
61194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                        *pathPos = 0;
61294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                        ALOGE("Couldn't unlinkat %s in %s: %s\n", name, pathBase,
61394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                                strerror(errno));
61494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                    }
61594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                }
61694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            }
61794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        } else {
61894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            cacheDir->hiddenCount++;
61994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        }
62094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
62194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    return 0;
62294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
62394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
62494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwoodvoid add_cache_files(cache_t* cache, const char *basepath, const char *cachedir)
62594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood{
62694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    DIR *d;
62794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    struct dirent *de;
62894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    char dirname[PATH_MAX];
62994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
63094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    CACHE_NOISY(ALOGI("add_cache_files: base=%s cachedir=%s\n", basepath, cachedir));
63194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
63294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    d = opendir(basepath);
63394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    if (d == NULL) {
63494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        return;
63594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
63694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
63794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    while ((de = readdir(d))) {
63894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        if (de->d_type == DT_DIR) {
63994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            DIR* subdir;
64094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            const char *name = de->d_name;
64194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            char* pathpos;
64294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
64394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                /* always skip "." and ".." */
64494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            if (name[0] == '.') {
64594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                if (name[1] == 0) continue;
64694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                if ((name[1] == '.') && (name[2] == 0)) continue;
64794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            }
64894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
64994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            strcpy(dirname, basepath);
65094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            pathpos = dirname + strlen(dirname);
65194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            if ((*(pathpos-1)) != '/') {
65294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                *pathpos = '/';
65394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                pathpos++;
65494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                *pathpos = 0;
65594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            }
65694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            if (cachedir != NULL) {
65794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                snprintf(pathpos, sizeof(dirname)-(pathpos-dirname), "%s/%s", name, cachedir);
65894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            } else {
65994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                snprintf(pathpos, sizeof(dirname)-(pathpos-dirname), "%s", name);
66094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            }
66194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            CACHE_NOISY(ALOGI("Adding cache files from dir: %s\n", dirname));
66294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            subdir = opendir(dirname);
66394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            if (subdir != NULL) {
66494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                size_t dirnameLen = strlen(dirname);
66594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                _add_cache_files(cache, NULL, dirname, subdir, dirname, dirname+dirnameLen,
66694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                        PATH_MAX - dirnameLen);
66794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                closedir(subdir);
66894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            }
66994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        }
67094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
67194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
67294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    closedir(d);
67394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
67494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
67594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwoodstatic char *create_dir_path(char path[PATH_MAX], cache_dir_t* dir)
67694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood{
67794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    char *pos = path;
67894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    if (dir->parent != NULL) {
67994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        pos = create_dir_path(path, dir->parent);
68094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
68194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    // Note that we don't need to worry about going beyond the buffer,
68294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    // since when we were constructing the cache entries our maximum
68394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    // buffer size for full paths was PATH_MAX.
68494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    strcpy(pos, dir->name);
68594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    pos += strlen(pos);
68694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    *pos = '/';
68794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    pos++;
68894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    *pos = 0;
68994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    return pos;
69094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
69194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
69294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwoodstatic void delete_cache_dir(char path[PATH_MAX], cache_dir_t* dir)
69394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood{
69494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    if (dir->parent != NULL) {
69594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        create_dir_path(path, dir);
69694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        ALOGI("DEL DIR %s\n", path);
69794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        if (dir->hiddenCount <= 0) {
69894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            if (rmdir(path)) {
69994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                ALOGE("Couldn't rmdir %s: %s\n", path, strerror(errno));
70094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                return;
70194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            }
70294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        } else {
70394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            // The directory contains hidden files so we need to delete
70494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            // them along with the directory itself.
70594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            if (delete_dir_contents(path, 1, NULL)) {
70694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                return;
70794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            }
70894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        }
70994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        dir->parent->childCount--;
71094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        dir->deleted = 1;
71194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        if (dir->parent->childCount <= 0) {
71294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            delete_cache_dir(path, dir->parent);
71394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        }
71494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    } else if (dir->hiddenCount > 0) {
71594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        // This is a root directory, but it has hidden files.  Get rid of
71694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        // all of those files, but not the directory itself.
71794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        create_dir_path(path, dir);
71894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        ALOGI("DEL CONTENTS %s\n", path);
71994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        delete_dir_contents(path, 0, NULL);
72094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
72194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
72294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
72394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwoodstatic int cache_modtime_sort(const void *lhsP, const void *rhsP)
72494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood{
72594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    const cache_file_t *lhs = *(const cache_file_t**)lhsP;
72694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    const cache_file_t *rhs = *(const cache_file_t**)rhsP;
72794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    return lhs->modTime < rhs->modTime ? -1 : (lhs->modTime > rhs->modTime ? 1 : 0);
72894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
72994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
73094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwoodvoid clear_cache_files(cache_t* cache, int64_t free_size)
73194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood{
73294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    size_t i;
73394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    int skip = 0;
73494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    char path[PATH_MAX];
73594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
73692dc3fc52cf097bd105460cf377779bdcf146d62Mark Salyzyn    ALOGI("Collected cache files: %zd directories, %zd files",
73794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        cache->numDirs, cache->numFiles);
73894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
73994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    CACHE_NOISY(ALOGI("Sorting files..."));
74094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    qsort(cache->files, cache->numFiles, sizeof(cache_file_t*),
74194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            cache_modtime_sort);
74294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
74394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    CACHE_NOISY(ALOGI("Cleaning empty directories..."));
74494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    for (i=cache->numDirs; i>0; i--) {
74594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        cache_dir_t* dir = cache->dirs[i-1];
74694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        if (dir->childCount <= 0 && !dir->deleted) {
74794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            delete_cache_dir(path, dir);
74894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        }
74994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
75094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
75194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    CACHE_NOISY(ALOGI("Trimming files..."));
75294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    for (i=0; i<cache->numFiles; i++) {
75394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        skip++;
75494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        if (skip > 10) {
75594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            if (data_disk_free() > free_size) {
75694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                return;
75794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            }
75894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            skip = 0;
75994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        }
76094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        cache_file_t* file = cache->files[i];
76194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        strcpy(create_dir_path(path, file->dir), file->name);
76294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        ALOGI("DEL (mod %d) %s\n", (int)file->modTime, path);
76394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        if (unlink(path) < 0) {
76494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            ALOGE("Couldn't unlink %s: %s\n", path, strerror(errno));
76594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        }
76694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        file->dir->childCount--;
76794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        if (file->dir->childCount <= 0) {
76894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            delete_cache_dir(path, file->dir);
76994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        }
77094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
77194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
77294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
77394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwoodvoid finish_cache_collection(cache_t* cache)
77494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood{
77594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    size_t i;
77694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
77794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    CACHE_NOISY(ALOGI("clear_cache_files: %d dirs, %d files\n", cache->numDirs, cache->numFiles));
77894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    CACHE_NOISY(
77994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        for (i=0; i<cache->numDirs; i++) {
78094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            cache_dir_t* dir = cache->dirs[i];
78194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            ALOGI("dir #%d: %p %s parent=%p\n", i, dir, dir->name, dir->parent);
78294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        })
78394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    CACHE_NOISY(
78494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        for (i=0; i<cache->numFiles; i++) {
78594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            cache_file_t* file = cache->files[i];
78694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            ALOGI("file #%d: %p %s time=%d dir=%p\n", i, file, file->name,
78794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                    (int)file->modTime, file->dir);
78894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        })
78994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    void* block = cache->memBlocks;
79094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    while (block != NULL) {
79194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        void* nextBlock = *(void**)block;
79294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        CACHE_NOISY(ALOGI("Freeing cache mem block: %p", block));
79394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        free(block);
79494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        block = nextBlock;
79594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
79694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    free(cache);
79794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
79894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
79994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood/**
80094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood * Checks whether a path points to a system app (.apk file). Returns 0
80194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood * if it is a system app or -1 if it is not.
80294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood */
80394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwoodint validate_system_app_path(const char* path) {
80494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    size_t i;
80594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
80694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    for (i = 0; i < android_system_dirs.count; i++) {
80794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        const size_t dir_len = android_system_dirs.dirs[i].len;
80894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        if (!strncmp(path, android_system_dirs.dirs[i].path, dir_len)) {
80994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            if (path[dir_len] == '.' || strchr(path + dir_len, '/') != NULL) {
81094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                ALOGE("invalid system apk path '%s' (trickery)\n", path);
81194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                return -1;
81294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            }
81394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            return 0;
81494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        }
81594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
81694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
81794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    return -1;
81894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
81994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
82094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood/**
82194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood * Get the contents of a environment variable that contains a path. Caller
82294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood * owns the string that is inserted into the directory record. Returns
82394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood * 0 on success and -1 on error.
82494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood */
82594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwoodint get_path_from_env(dir_rec_t* rec, const char* var) {
82694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    const char* path = getenv(var);
82794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    int ret = get_path_from_string(rec, path);
82894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    if (ret < 0) {
82994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        ALOGW("Problem finding value for environment variable %s\n", var);
83094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
83194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    return ret;
83294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
83394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
83494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood/**
83594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood * Puts the string into the record as a directory. Appends '/' to the end
83694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood * of all paths. Caller owns the string that is inserted into the directory
83794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood * record. A null value will result in an error.
83894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood *
83994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood * Returns 0 on success and -1 on error.
84094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood */
84194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwoodint get_path_from_string(dir_rec_t* rec, const char* path) {
84294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    if (path == NULL) {
84394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        return -1;
84494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    } else {
84594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        const size_t path_len = strlen(path);
84694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        if (path_len <= 0) {
84794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            return -1;
84894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        }
84994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
85094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        // Make sure path is absolute.
85194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        if (path[0] != '/') {
85294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            return -1;
85394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        }
85494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
85594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        if (path[path_len - 1] == '/') {
85694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            // Path ends with a forward slash. Make our own copy.
85794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
85894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            rec->path = strdup(path);
85994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            if (rec->path == NULL) {
86094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                return -1;
86194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            }
86294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
86394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            rec->len = path_len;
86494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        } else {
86594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            // Path does not end with a slash. Generate a new string.
86694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            char *dst;
86794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
86894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            // Add space for slash and terminating null.
86994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            size_t dst_size = path_len + 2;
87094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
87194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            rec->path = malloc(dst_size);
87294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            if (rec->path == NULL) {
87394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                return -1;
87494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            }
87594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
87694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            dst = rec->path;
87794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
87894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            if (append_and_increment(&dst, path, &dst_size) < 0
87994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                    || append_and_increment(&dst, "/", &dst_size)) {
88094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                ALOGE("Error canonicalizing path");
88194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                return -1;
88294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            }
88394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
88494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            rec->len = dst - rec->path;
88594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        }
88694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
88794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    return 0;
88894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
88994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
89094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwoodint copy_and_append(dir_rec_t* dst, const dir_rec_t* src, const char* suffix) {
89194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    dst->len = src->len + strlen(suffix);
89294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    const size_t dstSize = dst->len + 1;
89394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    dst->path = (char*) malloc(dstSize);
89494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
89594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    if (dst->path == NULL
89694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            || snprintf(dst->path, dstSize, "%s%s", src->path, suffix)
89794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                    != (ssize_t) dst->len) {
89894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        ALOGE("Could not allocate memory to hold appended path; aborting\n");
89994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        return -1;
90094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
90194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
90294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    return 0;
90394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
90494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
90594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood/**
90694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood * Check whether path points to a valid path for an APK file. An ASEC
90794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood * directory is allowed to have one level of subdirectory names. Returns -1
90894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood * when an invalid path is encountered and 0 when a valid path is encountered.
90994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood */
91094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwoodint validate_apk_path(const char *path)
91194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood{
91294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    int allowsubdir = 0;
91394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    char *subdir = NULL;
91494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    size_t dir_len;
91594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    size_t path_len;
91694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
91794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    if (!strncmp(path, android_app_dir.path, android_app_dir.len)) {
91894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        dir_len = android_app_dir.len;
91994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    } else if (!strncmp(path, android_app_private_dir.path, android_app_private_dir.len)) {
92094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        dir_len = android_app_private_dir.len;
92194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    } else if (!strncmp(path, android_asec_dir.path, android_asec_dir.len)) {
92294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        dir_len = android_asec_dir.len;
92394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        allowsubdir = 1;
92494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    } else {
92594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        ALOGE("invalid apk path '%s' (bad prefix)\n", path);
92694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        return -1;
92794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
92894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
92994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    path_len = strlen(path);
93094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
93194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    /*
93294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood     * Only allow the path to have a subdirectory if it's been marked as being allowed.
93394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood     */
93494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    if ((subdir = strchr(path + dir_len, '/')) != NULL) {
93594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        ++subdir;
93694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        if (!allowsubdir
93794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                || (path_len > (size_t) (subdir - path) && (strchr(subdir, '/') != NULL))) {
93894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            ALOGE("invalid apk path '%s' (subdir?)\n", path);
93994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            return -1;
94094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        }
94194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
94294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
94394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    /*
94494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood     *  Directories can't have a period directly after the directory markers
94594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood     *  to prevent ".."
94694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood     */
94794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    if (path[dir_len] == '.'
94894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            || (subdir != NULL && ((*subdir == '.') || (strchr(subdir, '/') != NULL)))) {
94994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        ALOGE("invalid apk path '%s' (trickery)\n", path);
95094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        return -1;
95194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
95294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
95394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    return 0;
95494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
95594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
95694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwoodint append_and_increment(char** dst, const char* src, size_t* dst_size) {
95794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    ssize_t ret = strlcpy(*dst, src, *dst_size);
95894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    if (ret < 0 || (size_t) ret >= *dst_size) {
95994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        return -1;
96094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
96194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    *dst += ret;
96294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    *dst_size -= ret;
96394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    return 0;
96494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
96594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
96694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwoodchar *build_string2(char *s1, char *s2) {
96794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    if (s1 == NULL || s2 == NULL) return NULL;
96894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
96994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    int len_s1 = strlen(s1);
97094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    int len_s2 = strlen(s2);
97194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    int len = len_s1 + len_s2 + 1;
97294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    char *result = malloc(len);
97394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    if (result == NULL) return NULL;
97494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
97594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    strcpy(result, s1);
97694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    strcpy(result + len_s1, s2);
97794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
97894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    return result;
97994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
98094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
98194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwoodchar *build_string3(char *s1, char *s2, char *s3) {
98294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    if (s1 == NULL || s2 == NULL || s3 == NULL) return NULL;
98394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
98494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    int len_s1 = strlen(s1);
98594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    int len_s2 = strlen(s2);
98694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    int len_s3 = strlen(s3);
98794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    int len = len_s1 + len_s2 + len_s3 + 1;
98894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    char *result = malloc(len);
98994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    if (result == NULL) return NULL;
99094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
99194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    strcpy(result, s1);
99294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    strcpy(result + len_s1, s2);
99394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    strcpy(result + len_s1 + len_s2, s3);
99494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
99594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    return result;
99694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
99794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
99894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood/* Ensure that /data/media directories are prepared for given user. */
99994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwoodint ensure_media_user_dirs(userid_t userid) {
100094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    char media_user_path[PATH_MAX];
100194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    char path[PATH_MAX];
100294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
100394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    // Ensure /data/media/<userid> exists
1004abe4fe5b46157ecd2a52d28abf938c816c3ce878Jeff Sharkey    create_user_media_path(media_user_path, userid);
100594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    if (fs_prepare_dir(media_user_path, 0770, AID_MEDIA_RW, AID_MEDIA_RW) == -1) {
100694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        return -1;
100794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
100894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
100994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    return 0;
101094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
1011d93707342a61e66bc3eb2145628158452f577f42Dave Allison
1012d93707342a61e66bc3eb2145628158452f577f42Dave Allisonint create_profile_file(const char *pkgname, gid_t gid) {
1013d93707342a61e66bc3eb2145628158452f577f42Dave Allison    const char *profile_dir = DALVIK_CACHE_PREFIX "profiles";
1014d93707342a61e66bc3eb2145628158452f577f42Dave Allison    struct stat profileStat;
1015d93707342a61e66bc3eb2145628158452f577f42Dave Allison    char profile_file[PKG_PATH_MAX];
1016d93707342a61e66bc3eb2145628158452f577f42Dave Allison
1017d93707342a61e66bc3eb2145628158452f577f42Dave Allison    // If we don't have a profile directory under dalvik-cache we need to create one.
1018d93707342a61e66bc3eb2145628158452f577f42Dave Allison    if (stat(profile_dir, &profileStat) < 0) {
1019d93707342a61e66bc3eb2145628158452f577f42Dave Allison        // Create the profile directory under dalvik-cache.
1020d93707342a61e66bc3eb2145628158452f577f42Dave Allison        if (mkdir(profile_dir, 0711) < 0) {
1021d93707342a61e66bc3eb2145628158452f577f42Dave Allison            ALOGE("cannot make profile dir '%s': %s\n", profile_dir, strerror(errno));
1022d93707342a61e66bc3eb2145628158452f577f42Dave Allison            return -1;
1023d93707342a61e66bc3eb2145628158452f577f42Dave Allison        }
1024d93707342a61e66bc3eb2145628158452f577f42Dave Allison
1025d93707342a61e66bc3eb2145628158452f577f42Dave Allison        // Make the profile directory write-only for group and other. Owner can rwx it.
1026d93707342a61e66bc3eb2145628158452f577f42Dave Allison        if (chmod(profile_dir, 0711) < 0) {
1027d93707342a61e66bc3eb2145628158452f577f42Dave Allison            ALOGE("cannot chown profile dir '%s': %s\n", profile_dir, strerror(errno));
1028a240733137d1a0c006ca68415a0f8ed28cc4b11aStephen Smalley            rmdir(profile_dir);
1029a240733137d1a0c006ca68415a0f8ed28cc4b11aStephen Smalley            return -1;
1030a240733137d1a0c006ca68415a0f8ed28cc4b11aStephen Smalley        }
1031a240733137d1a0c006ca68415a0f8ed28cc4b11aStephen Smalley
1032a240733137d1a0c006ca68415a0f8ed28cc4b11aStephen Smalley        if (selinux_android_restorecon(profile_dir, 0) < 0) {
1033a240733137d1a0c006ca68415a0f8ed28cc4b11aStephen Smalley            ALOGE("cannot restorecon profile dir '%s': %s\n", profile_dir, strerror(errno));
1034a240733137d1a0c006ca68415a0f8ed28cc4b11aStephen Smalley            rmdir(profile_dir);
1035d93707342a61e66bc3eb2145628158452f577f42Dave Allison            return -1;
1036d93707342a61e66bc3eb2145628158452f577f42Dave Allison        }
1037d93707342a61e66bc3eb2145628158452f577f42Dave Allison    }
1038d93707342a61e66bc3eb2145628158452f577f42Dave Allison
1039d93707342a61e66bc3eb2145628158452f577f42Dave Allison    snprintf(profile_file, sizeof(profile_file), "%s/%s", profile_dir, pkgname);
1040d93707342a61e66bc3eb2145628158452f577f42Dave Allison
1041d93707342a61e66bc3eb2145628158452f577f42Dave Allison    // The 'system' user needs to be able to read the profile to determine if dex2oat
1042d93707342a61e66bc3eb2145628158452f577f42Dave Allison    // needs to be run.  This is done in dalvik.system.DexFile.isDexOptNeededInternal().  So
1043d93707342a61e66bc3eb2145628158452f577f42Dave Allison    // we make it world readable.  Not a problem since the dalvik cache is world
1044d93707342a61e66bc3eb2145628158452f577f42Dave Allison    // readable anyway.
1045d93707342a61e66bc3eb2145628158452f577f42Dave Allison
1046d93707342a61e66bc3eb2145628158452f577f42Dave Allison    int fd = open(profile_file, O_WRONLY | O_CREAT | O_EXCL | O_NOFOLLOW, 0664);
1047d93707342a61e66bc3eb2145628158452f577f42Dave Allison
1048d93707342a61e66bc3eb2145628158452f577f42Dave Allison    // Open will fail if the file already exists.  We want to ignore that.
1049d93707342a61e66bc3eb2145628158452f577f42Dave Allison    if (fd >= 0) {
1050d93707342a61e66bc3eb2145628158452f577f42Dave Allison        if (fchown(fd, -1, gid) < 0) {
1051d93707342a61e66bc3eb2145628158452f577f42Dave Allison            ALOGE("cannot chown profile file '%s': %s\n", profile_file, strerror(errno));
1052d93707342a61e66bc3eb2145628158452f577f42Dave Allison            close(fd);
1053d93707342a61e66bc3eb2145628158452f577f42Dave Allison            unlink(profile_file);
1054d93707342a61e66bc3eb2145628158452f577f42Dave Allison            return -1;
1055d93707342a61e66bc3eb2145628158452f577f42Dave Allison        }
1056d93707342a61e66bc3eb2145628158452f577f42Dave Allison
1057d93707342a61e66bc3eb2145628158452f577f42Dave Allison        if (fchmod(fd, 0664) < 0) {
1058d93707342a61e66bc3eb2145628158452f577f42Dave Allison            ALOGE("cannot chmod profile file '%s': %s\n", profile_file, strerror(errno));
1059d93707342a61e66bc3eb2145628158452f577f42Dave Allison            close(fd);
1060d93707342a61e66bc3eb2145628158452f577f42Dave Allison            unlink(profile_file);
1061d93707342a61e66bc3eb2145628158452f577f42Dave Allison            return -1;
1062d93707342a61e66bc3eb2145628158452f577f42Dave Allison        }
1063d93707342a61e66bc3eb2145628158452f577f42Dave Allison        close(fd);
1064d93707342a61e66bc3eb2145628158452f577f42Dave Allison    }
1065d93707342a61e66bc3eb2145628158452f577f42Dave Allison    return 0;
1066d93707342a61e66bc3eb2145628158452f577f42Dave Allison}
1067d93707342a61e66bc3eb2145628158452f577f42Dave Allison
1068d93707342a61e66bc3eb2145628158452f577f42Dave Allisonvoid remove_profile_file(const char *pkgname) {
1069d93707342a61e66bc3eb2145628158452f577f42Dave Allison    char profile_file[PKG_PATH_MAX];
1070d93707342a61e66bc3eb2145628158452f577f42Dave Allison    snprintf(profile_file, sizeof(profile_file), "%s/%s", DALVIK_CACHE_PREFIX "profiles", pkgname);
1071d93707342a61e66bc3eb2145628158452f577f42Dave Allison    unlink(profile_file);
1072d93707342a61e66bc3eb2145628158452f577f42Dave Allison}
1073