194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood/*
294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood** Copyright 2008, The Android Open Source Project
394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood**
4d93707342a61e66bc3eb2145628158452f577f42Dave Allison** Licensed under the Apache License, Version 2.0 (the "License");
5d93707342a61e66bc3eb2145628158452f577f42Dave Allison** you may not use this file except in compliance with the License.
6d93707342a61e66bc3eb2145628158452f577f42Dave Allison** You may obtain a copy of the License at
794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood**
8d93707342a61e66bc3eb2145628158452f577f42Dave Allison**     http://www.apache.org/licenses/LICENSE-2.0
994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood**
10d93707342a61e66bc3eb2145628158452f577f42Dave Allison** Unless required by applicable law or agreed to in writing, software
11d93707342a61e66bc3eb2145628158452f577f42Dave Allison** distributed under the License is distributed on an "AS IS" BASIS,
12d93707342a61e66bc3eb2145628158452f577f42Dave Allison** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13d93707342a61e66bc3eb2145628158452f577f42Dave Allison** See the License for the specific language governing permissions and
1494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood** limitations under the License.
1594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood*/
1694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
1702d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe#include "utils.h"
1802d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe
1902d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe#include <errno.h>
2002d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe#include <fcntl.h>
213dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey#include <fts.h>
2202d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe#include <stdlib.h>
2302d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe#include <sys/stat.h>
2402d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe#include <sys/wait.h>
259a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey#include <sys/xattr.h>
26ed909ae8db2f44ce7fe7003c6fee457f13669702Jeff Sharkey#include <sys/statvfs.h>
2794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
28e4ec9eb7b4c452493589983970ba5ccc501728d1Elliott Hughes#include <android-base/logging.h>
2902d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe#include <android-base/stringprintf.h>
3002d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe#include <cutils/fs.h>
31871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey#include <cutils/properties.h>
327823e124e00576e20e47ec717cbe8bc89f0f2bf2Mark Salyzyn#include <log/log.h>
3302d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe#include <private/android_filesystem_config.h>
3402d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe
3502d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe#include "globals.h"  // extern variables.
3694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
3702d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe#ifndef LOG_TAG
3802d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe#define LOG_TAG "installd"
3902d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe#endif
409a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey
419a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey#define DEBUG_XATTRS 0
4294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
43c03de09173f98506e73e7cf7df21fe11795d4b24Jeff Sharkeyusing android::base::StringPrintf;
4494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
4502d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampenamespace android {
4602d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampenamespace installd {
4702d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe
4894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood/**
49c03de09173f98506e73e7cf7df21fe11795d4b24Jeff Sharkey * Check that given string is valid filename, and that it attempts no
50c03de09173f98506e73e7cf7df21fe11795d4b24Jeff Sharkey * parent or child directory traversal.
5194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood */
52423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkeybool is_valid_filename(const std::string& name) {
53c03de09173f98506e73e7cf7df21fe11795d4b24Jeff Sharkey    if (name.empty() || (name == ".") || (name == "..")
54c03de09173f98506e73e7cf7df21fe11795d4b24Jeff Sharkey            || (name.find('/') != std::string::npos)) {
55c03de09173f98506e73e7cf7df21fe11795d4b24Jeff Sharkey        return false;
5694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    } else {
57c03de09173f98506e73e7cf7df21fe11795d4b24Jeff Sharkey        return true;
5894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
59c03de09173f98506e73e7cf7df21fe11795d4b24Jeff Sharkey}
6094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
616a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravlestatic void check_package_name(const char* package_name) {
626a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle    CHECK(is_valid_filename(package_name));
63423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey    CHECK(is_valid_package_name(package_name));
646a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle}
656a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle
66c03de09173f98506e73e7cf7df21fe11795d4b24Jeff Sharkey/**
67d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey * Create the path name where package app contents should be stored for
68d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey * the given volume UUID and package name.  An empty UUID is assumed to
69d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey * be internal storage.
70d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey */
71d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkeystd::string create_data_app_package_path(const char* volume_uuid,
72d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey        const char* package_name) {
736a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle    check_package_name(package_name);
74d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey    return StringPrintf("%s/%s",
75d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey            create_data_app_path(volume_uuid).c_str(), package_name);
76d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey}
77d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey
78d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey/**
79c03de09173f98506e73e7cf7df21fe11795d4b24Jeff Sharkey * Create the path name where package data should be stored for the given
80c03de09173f98506e73e7cf7df21fe11795d4b24Jeff Sharkey * volume UUID, package name, and user ID. An empty UUID is assumed to be
81c03de09173f98506e73e7cf7df21fe11795d4b24Jeff Sharkey * internal storage.
82c03de09173f98506e73e7cf7df21fe11795d4b24Jeff Sharkey */
832f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkeystd::string create_data_user_ce_package_path(const char* volume_uuid,
84d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey        userid_t user, const char* package_name) {
856a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle    check_package_name(package_name);
86d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey    return StringPrintf("%s/%s",
872f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey            create_data_user_ce_path(volume_uuid, user).c_str(), package_name);
882f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey}
892f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey
902f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkeystd::string create_data_user_ce_package_path(const char* volume_uuid, userid_t user,
912f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey        const char* package_name, ino_t ce_data_inode) {
922f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey    // For testing purposes, rely on the inode when defined; this could be
932f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey    // optimized to use access() in the future.
942f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey    auto fallback = create_data_user_ce_package_path(volume_uuid, user, package_name);
952f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey    if (ce_data_inode != 0) {
962f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey        auto user_path = create_data_user_ce_path(volume_uuid, user);
972f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey        DIR* dir = opendir(user_path.c_str());
982f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey        if (dir == nullptr) {
992f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey            PLOG(ERROR) << "Failed to opendir " << user_path;
1002f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey            return fallback;
1012f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey        }
1022f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey
1032f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey        struct dirent* ent;
1042f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey        while ((ent = readdir(dir))) {
1052f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey            if (ent->d_ino == ce_data_inode) {
1061d992f9f7886f98e46f98430d6c1d061cc31fdb3Jeff Sharkey                auto resolved = StringPrintf("%s/%s", user_path.c_str(), ent->d_name);
1079a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey#if DEBUG_XATTRS
1081d992f9f7886f98e46f98430d6c1d061cc31fdb3Jeff Sharkey                if (resolved != fallback) {
1091d992f9f7886f98e46f98430d6c1d061cc31fdb3Jeff Sharkey                    LOG(DEBUG) << "Resolved path " << resolved << " for inode " << ce_data_inode
1101d992f9f7886f98e46f98430d6c1d061cc31fdb3Jeff Sharkey                            << " instead of " << fallback;
1111d992f9f7886f98e46f98430d6c1d061cc31fdb3Jeff Sharkey                }
1129a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey#endif
1132f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey                closedir(dir);
1141d992f9f7886f98e46f98430d6c1d061cc31fdb3Jeff Sharkey                return resolved;
1152f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey            }
1162f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey        }
1171d992f9f7886f98e46f98430d6c1d061cc31fdb3Jeff Sharkey        LOG(WARNING) << "Failed to resolve inode " << ce_data_inode << "; using " << fallback;
1182f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey        closedir(dir);
1192f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey        return fallback;
1202f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey    } else {
1212f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey        return fallback;
1222f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey    }
123c03de09173f98506e73e7cf7df21fe11795d4b24Jeff Sharkey}
12494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
12563ec2d64196144b2d15d2baffedccfa011d6494fJeff Sharkeystd::string create_data_user_de_package_path(const char* volume_uuid,
12663ec2d64196144b2d15d2baffedccfa011d6494fJeff Sharkey        userid_t user, const char* package_name) {
1276a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle    check_package_name(package_name);
12863ec2d64196144b2d15d2baffedccfa011d6494fJeff Sharkey    return StringPrintf("%s/%s",
12963ec2d64196144b2d15d2baffedccfa011d6494fJeff Sharkey            create_data_user_de_path(volume_uuid, user).c_str(), package_name);
13063ec2d64196144b2d15d2baffedccfa011d6494fJeff Sharkey}
13163ec2d64196144b2d15d2baffedccfa011d6494fJeff Sharkey
132c03de09173f98506e73e7cf7df21fe11795d4b24Jeff Sharkeyint create_pkg_path(char path[PKG_PATH_MAX], const char *pkgname,
133c03de09173f98506e73e7cf7df21fe11795d4b24Jeff Sharkey        const char *postfix, userid_t userid) {
134423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey    if (!is_valid_package_name(pkgname)) {
135c03de09173f98506e73e7cf7df21fe11795d4b24Jeff Sharkey        path[0] = '\0';
13694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        return -1;
13794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
13894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
1392f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey    std::string _tmp(create_data_user_ce_package_path(nullptr, userid, pkgname) + postfix);
140c03de09173f98506e73e7cf7df21fe11795d4b24Jeff Sharkey    const char* tmp = _tmp.c_str();
141c03de09173f98506e73e7cf7df21fe11795d4b24Jeff Sharkey    if (strlen(tmp) >= PKG_PATH_MAX) {
142c03de09173f98506e73e7cf7df21fe11795d4b24Jeff Sharkey        path[0] = '\0';
143c03de09173f98506e73e7cf7df21fe11795d4b24Jeff Sharkey        return -1;
144c03de09173f98506e73e7cf7df21fe11795d4b24Jeff Sharkey    } else {
145c03de09173f98506e73e7cf7df21fe11795d4b24Jeff Sharkey        strcpy(path, tmp);
146c03de09173f98506e73e7cf7df21fe11795d4b24Jeff Sharkey        return 0;
14794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
14894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
14994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
15041ea424413c0381ef2aa15fc5bd5d4b88abd23c4Jeff Sharkeystd::string create_data_path(const char* volume_uuid) {
15141ea424413c0381ef2aa15fc5bd5d4b88abd23c4Jeff Sharkey    if (volume_uuid == nullptr) {
15241ea424413c0381ef2aa15fc5bd5d4b88abd23c4Jeff Sharkey        return "/data";
153871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey    } else if (!strcmp(volume_uuid, "TEST")) {
154871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey        CHECK(property_get_bool("ro.debuggable", false));
155871a8f236ef2a055b9955b47a342b2c44c020ef7Jeff Sharkey        return "/data/local/tmp";
15694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    } else {
15741ea424413c0381ef2aa15fc5bd5d4b88abd23c4Jeff Sharkey        CHECK(is_valid_filename(volume_uuid));
15841ea424413c0381ef2aa15fc5bd5d4b88abd23c4Jeff Sharkey        return StringPrintf("/mnt/expand/%s", volume_uuid);
15994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
16041ea424413c0381ef2aa15fc5bd5d4b88abd23c4Jeff Sharkey}
16194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
16241ea424413c0381ef2aa15fc5bd5d4b88abd23c4Jeff Sharkey/**
163d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey * Create the path name for app data.
164d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey */
165d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkeystd::string create_data_app_path(const char* volume_uuid) {
166d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey    return StringPrintf("%s/app", create_data_path(volume_uuid).c_str());
167d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey}
168d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey
169d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey/**
17041ea424413c0381ef2aa15fc5bd5d4b88abd23c4Jeff Sharkey * Create the path name for user data for a certain userid.
17175d4e57dfa11a5fcc99a260facf71309945ade67cjbao * Keep same implementation as vold to minimize path walking overhead
17241ea424413c0381ef2aa15fc5bd5d4b88abd23c4Jeff Sharkey */
1732f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkeystd::string create_data_user_ce_path(const char* volume_uuid, userid_t userid) {
17441ea424413c0381ef2aa15fc5bd5d4b88abd23c4Jeff Sharkey    std::string data(create_data_path(volume_uuid));
17575d4e57dfa11a5fcc99a260facf71309945ade67cjbao    if (volume_uuid == nullptr && userid == 0) {
17675d4e57dfa11a5fcc99a260facf71309945ade67cjbao        std::string legacy = StringPrintf("%s/data", data.c_str());
17775d4e57dfa11a5fcc99a260facf71309945ade67cjbao        struct stat sb;
17875d4e57dfa11a5fcc99a260facf71309945ade67cjbao        if (lstat(legacy.c_str(), &sb) == 0 && S_ISDIR(sb.st_mode)) {
17975d4e57dfa11a5fcc99a260facf71309945ade67cjbao            /* /data/data is dir, return /data/data for legacy system */
18075d4e57dfa11a5fcc99a260facf71309945ade67cjbao            return legacy;
18194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        }
18294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
18375d4e57dfa11a5fcc99a260facf71309945ade67cjbao    return StringPrintf("%s/user/%u", data.c_str(), userid);
18494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
18594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
18694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood/**
18763ec2d64196144b2d15d2baffedccfa011d6494fJeff Sharkey * Create the path name for device encrypted user data for a certain userid.
18863ec2d64196144b2d15d2baffedccfa011d6494fJeff Sharkey */
18963ec2d64196144b2d15d2baffedccfa011d6494fJeff Sharkeystd::string create_data_user_de_path(const char* volume_uuid, userid_t userid) {
19063ec2d64196144b2d15d2baffedccfa011d6494fJeff Sharkey    std::string data(create_data_path(volume_uuid));
19163ec2d64196144b2d15d2baffedccfa011d6494fJeff Sharkey    return StringPrintf("%s/user_de/%u", data.c_str(), userid);
19263ec2d64196144b2d15d2baffedccfa011d6494fJeff Sharkey}
19363ec2d64196144b2d15d2baffedccfa011d6494fJeff Sharkey
19463ec2d64196144b2d15d2baffedccfa011d6494fJeff Sharkey/**
195abe4fe5b46157ecd2a52d28abf938c816c3ce878Jeff Sharkey * Create the path name for media for a certain userid.
19694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood */
19741ea424413c0381ef2aa15fc5bd5d4b88abd23c4Jeff Sharkeystd::string create_data_media_path(const char* volume_uuid, userid_t userid) {
19841ea424413c0381ef2aa15fc5bd5d4b88abd23c4Jeff Sharkey    return StringPrintf("%s/media/%u", create_data_path(volume_uuid).c_str(), userid);
19994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
20094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
201df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkeystd::string create_data_media_obb_path(const char* volume_uuid, const char* package_name) {
202df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey    return StringPrintf("%s/media/obb/%s", create_data_path(volume_uuid).c_str(), package_name);
203df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey}
204df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey
2053dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkeystd::string create_data_media_package_path(const char* volume_uuid, userid_t userid,
2063dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey        const char* data_type, const char* package_name) {
2073dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey    return StringPrintf("%s/Android/%s/%s", create_data_media_path(volume_uuid, userid).c_str(),
2083dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey            data_type, package_name);
2093dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey}
2103dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey
211379a12b0072b322c7f86e690a8e8a220e500861cJeff Sharkeystd::string create_data_misc_legacy_path(userid_t userid) {
212379a12b0072b322c7f86e690a8e8a220e500861cJeff Sharkey    return StringPrintf("%s/misc/user/%u", create_data_path(nullptr).c_str(), userid);
213379a12b0072b322c7f86e690a8e8a220e500861cJeff Sharkey}
214379a12b0072b322c7f86e690a8e8a220e500861cJeff Sharkey
215114f08107be384a3f9cc954bdec2b6b7619354aeCalin Juravlestd::string create_primary_cur_profile_dir_path(userid_t userid) {
2166a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle    return StringPrintf("%s/cur/%u", android_profiles_dir.path, userid);
2176a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle}
2186a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle
219114f08107be384a3f9cc954bdec2b6b7619354aeCalin Juravlestd::string create_primary_current_profile_package_dir_path(userid_t user,
220114f08107be384a3f9cc954bdec2b6b7619354aeCalin Juravle        const std::string& package_name) {
22176268c56febde9a77183387fbd4baabe6694e6b5Calin Juravle    check_package_name(package_name.c_str());
222114f08107be384a3f9cc954bdec2b6b7619354aeCalin Juravle    return StringPrintf("%s/%s",
223114f08107be384a3f9cc954bdec2b6b7619354aeCalin Juravle            create_primary_cur_profile_dir_path(user).c_str(), package_name.c_str());
224df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey}
225df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey
226114f08107be384a3f9cc954bdec2b6b7619354aeCalin Juravlestd::string create_primary_ref_profile_dir_path() {
227df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey    return StringPrintf("%s/ref", android_profiles_dir.path);
2286a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle}
2296a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle
230114f08107be384a3f9cc954bdec2b6b7619354aeCalin Juravlestd::string create_primary_reference_profile_package_dir_path(const std::string& package_name) {
23176268c56febde9a77183387fbd4baabe6694e6b5Calin Juravle    check_package_name(package_name.c_str());
23276268c56febde9a77183387fbd4baabe6694e6b5Calin Juravle    return StringPrintf("%s/ref/%s", android_profiles_dir.path, package_name.c_str());
2336a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle}
2346a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle
2353dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkeystd::string create_data_dalvik_cache_path() {
2363dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey    return "/data/dalvik-cache";
2373dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey}
2383dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey
239114f08107be384a3f9cc954bdec2b6b7619354aeCalin Juravle// Keep profile paths in sync with ActivityThread and LoadedApk.
240114f08107be384a3f9cc954bdec2b6b7619354aeCalin Juravleconst std::string PROFILE_EXT = ".prof";
2411c809c7068f610ff22174dbe96b49f1b8ada9c1eCalin Juravleconst std::string CURRENT_PROFILE_EXT = ".cur";
242114f08107be384a3f9cc954bdec2b6b7619354aeCalin Juravleconst std::string PRIMARY_PROFILE_NAME = "primary" + PROFILE_EXT;
24390aff26f0135379db19432ae90c40c0831ba5954Jeff Sharkey
2441c809c7068f610ff22174dbe96b49f1b8ada9c1eCalin Juravle// Gets the parent directory and the file name for the given secondary dex path.
2451c809c7068f610ff22174dbe96b49f1b8ada9c1eCalin Juravle// Returns true on success, false on failure (if the dex_path does not have the expected
2461c809c7068f610ff22174dbe96b49f1b8ada9c1eCalin Juravle// structure).
2471c809c7068f610ff22174dbe96b49f1b8ada9c1eCalin Juravlestatic bool get_secondary_dex_location(const std::string& dex_path,
2481c809c7068f610ff22174dbe96b49f1b8ada9c1eCalin Juravle        std::string* out_dir_name, std::string* out_file_name) {
2491c809c7068f610ff22174dbe96b49f1b8ada9c1eCalin Juravle   size_t dirIndex = dex_path.rfind('/');
2501c809c7068f610ff22174dbe96b49f1b8ada9c1eCalin Juravle   if (dirIndex == std::string::npos) {
2511c809c7068f610ff22174dbe96b49f1b8ada9c1eCalin Juravle        return false;
2521c809c7068f610ff22174dbe96b49f1b8ada9c1eCalin Juravle   }
2531c809c7068f610ff22174dbe96b49f1b8ada9c1eCalin Juravle   if (dirIndex == dex_path.size() - 1) {
2541c809c7068f610ff22174dbe96b49f1b8ada9c1eCalin Juravle        return false;
2551c809c7068f610ff22174dbe96b49f1b8ada9c1eCalin Juravle   }
2561c809c7068f610ff22174dbe96b49f1b8ada9c1eCalin Juravle   *out_dir_name = dex_path.substr(0, dirIndex);
2571c809c7068f610ff22174dbe96b49f1b8ada9c1eCalin Juravle   *out_file_name = dex_path.substr(dirIndex + 1);
2581c809c7068f610ff22174dbe96b49f1b8ada9c1eCalin Juravle
2591c809c7068f610ff22174dbe96b49f1b8ada9c1eCalin Juravle   return true;
2601c809c7068f610ff22174dbe96b49f1b8ada9c1eCalin Juravle}
2611c809c7068f610ff22174dbe96b49f1b8ada9c1eCalin Juravle
262114f08107be384a3f9cc954bdec2b6b7619354aeCalin Juravlestd::string create_current_profile_path(userid_t user, const std::string& location,
263114f08107be384a3f9cc954bdec2b6b7619354aeCalin Juravle        bool is_secondary_dex) {
264114f08107be384a3f9cc954bdec2b6b7619354aeCalin Juravle    if (is_secondary_dex) {
2651c809c7068f610ff22174dbe96b49f1b8ada9c1eCalin Juravle        // Secondary dex current profiles are stored next to the dex files under the oat folder.
2661c809c7068f610ff22174dbe96b49f1b8ada9c1eCalin Juravle        std::string dex_dir;
2671c809c7068f610ff22174dbe96b49f1b8ada9c1eCalin Juravle        std::string dex_name;
2681c809c7068f610ff22174dbe96b49f1b8ada9c1eCalin Juravle        CHECK(get_secondary_dex_location(location, &dex_dir, &dex_name))
2691c809c7068f610ff22174dbe96b49f1b8ada9c1eCalin Juravle                << "Unexpected dir structure for secondary dex " << location;
2701c809c7068f610ff22174dbe96b49f1b8ada9c1eCalin Juravle        return StringPrintf("%s/oat/%s%s%s",
2711c809c7068f610ff22174dbe96b49f1b8ada9c1eCalin Juravle                dex_dir.c_str(), dex_name.c_str(), CURRENT_PROFILE_EXT.c_str(),
2721c809c7068f610ff22174dbe96b49f1b8ada9c1eCalin Juravle                PROFILE_EXT.c_str());
273114f08107be384a3f9cc954bdec2b6b7619354aeCalin Juravle    } else {
274114f08107be384a3f9cc954bdec2b6b7619354aeCalin Juravle        // Profiles for primary apks are under /data/misc/profiles/cur.
275114f08107be384a3f9cc954bdec2b6b7619354aeCalin Juravle        std::string profile_dir = create_primary_current_profile_package_dir_path(user, location);
276114f08107be384a3f9cc954bdec2b6b7619354aeCalin Juravle        return StringPrintf("%s/%s", profile_dir.c_str(), PRIMARY_PROFILE_NAME.c_str());
277114f08107be384a3f9cc954bdec2b6b7619354aeCalin Juravle    }
278114f08107be384a3f9cc954bdec2b6b7619354aeCalin Juravle}
279114f08107be384a3f9cc954bdec2b6b7619354aeCalin Juravle
280114f08107be384a3f9cc954bdec2b6b7619354aeCalin Juravlestd::string create_reference_profile_path(const std::string& location, bool is_secondary_dex) {
281114f08107be384a3f9cc954bdec2b6b7619354aeCalin Juravle    if (is_secondary_dex) {
282114f08107be384a3f9cc954bdec2b6b7619354aeCalin Juravle        // Secondary dex reference profiles are stored next to the dex files under the oat folder.
2831c809c7068f610ff22174dbe96b49f1b8ada9c1eCalin Juravle        std::string dex_dir;
2841c809c7068f610ff22174dbe96b49f1b8ada9c1eCalin Juravle        std::string dex_name;
2851c809c7068f610ff22174dbe96b49f1b8ada9c1eCalin Juravle        CHECK(get_secondary_dex_location(location, &dex_dir, &dex_name))
286114f08107be384a3f9cc954bdec2b6b7619354aeCalin Juravle                << "Unexpected dir structure for secondary dex " << location;
287114f08107be384a3f9cc954bdec2b6b7619354aeCalin Juravle        return StringPrintf("%s/oat/%s%s",
288114f08107be384a3f9cc954bdec2b6b7619354aeCalin Juravle                dex_dir.c_str(), dex_name.c_str(), PROFILE_EXT.c_str());
289114f08107be384a3f9cc954bdec2b6b7619354aeCalin Juravle    } else {
290114f08107be384a3f9cc954bdec2b6b7619354aeCalin Juravle        // Reference profiles for primary apks are stored in /data/misc/profile/ref.
291114f08107be384a3f9cc954bdec2b6b7619354aeCalin Juravle        std::string profile_dir = create_primary_reference_profile_package_dir_path(location);
292114f08107be384a3f9cc954bdec2b6b7619354aeCalin Juravle        return StringPrintf("%s/%s", profile_dir.c_str(), PRIMARY_PROFILE_NAME.c_str());
293114f08107be384a3f9cc954bdec2b6b7619354aeCalin Juravle    }
29490aff26f0135379db19432ae90c40c0831ba5954Jeff Sharkey}
29590aff26f0135379db19432ae90c40c0831ba5954Jeff Sharkey
296e36372423000a906bafae68844ebc6c42d09335aJeff Sharkeystd::vector<userid_t> get_known_users(const char* volume_uuid) {
297e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey    std::vector<userid_t> users;
298e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey
299e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey    // We always have an owner
300e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey    users.push_back(0);
301e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey
302e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey    std::string path(create_data_path(volume_uuid) + "/" + SECONDARY_USER_PREFIX);
303e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey    DIR* dir = opendir(path.c_str());
304e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey    if (dir == NULL) {
305e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey        // Unable to discover other users, but at least return owner
306e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey        PLOG(ERROR) << "Failed to opendir " << path;
307e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey        return users;
308e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey    }
309e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey
310e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey    struct dirent* ent;
311e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey    while ((ent = readdir(dir))) {
312e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey        if (ent->d_type != DT_DIR) {
313e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey            continue;
314e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey        }
315e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey
316e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey        char* end;
317e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey        userid_t user = strtol(ent->d_name, &end, 10);
318e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey        if (*end == '\0' && user != 0) {
319e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey            LOG(DEBUG) << "Found valid user " << user;
320e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey            users.push_back(user);
321e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey        }
322e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey    }
323e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey    closedir(dir);
324e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey
325e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey    return users;
326e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey}
327e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey
3283dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkeyint calculate_tree_size(const std::string& path, int64_t* size,
329df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey        int32_t include_gid, int32_t exclude_gid, bool exclude_apps) {
3303dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey    FTS *fts;
3313dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey    FTSENT *p;
332df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey    int64_t matchedSize = 0;
3333dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey    char *argv[] = { (char*) path.c_str(), nullptr };
334b26786d647b624498c11405075e5223d1853f30aJeff Sharkey    if (!(fts = fts_open(argv, FTS_PHYSICAL | FTS_NOCHDIR | FTS_XDEV, NULL))) {
3353dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey        if (errno != ENOENT) {
3363dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey            PLOG(ERROR) << "Failed to fts_open " << path;
3373dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey        }
3383dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey        return -1;
3393dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey    }
3403dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey    while ((p = fts_read(fts)) != NULL) {
3413dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey        switch (p->fts_info) {
3423dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey        case FTS_D:
3433dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey        case FTS_DEFAULT:
3443dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey        case FTS_F:
3453dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey        case FTS_SL:
3463dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey        case FTS_SLNONE:
347df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey            int32_t uid = p->fts_statp->st_uid;
348df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey            int32_t gid = p->fts_statp->st_gid;
349df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey            int32_t user_uid = multiuser_get_app_id(uid);
350df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey            int32_t user_gid = multiuser_get_app_id(gid);
351df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey            if (exclude_apps && ((user_uid >= AID_APP_START && user_uid <= AID_APP_END)
352df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey                    || (user_gid >= AID_CACHE_GID_START && user_gid <= AID_CACHE_GID_END)
353df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey                    || (user_gid >= AID_SHARED_GID_START && user_gid <= AID_SHARED_GID_END))) {
354df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey                // Don't traverse inside or measure
355df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey                fts_set(fts, p, FTS_SKIP);
3563dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey                break;
3573dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey            }
358df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey            if (include_gid != -1 && gid != include_gid) {
3593dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey                break;
3603dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey            }
361df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey            if (exclude_gid != -1 && gid == exclude_gid) {
362df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey                break;
363df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey            }
364df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey            matchedSize += (p->fts_statp->st_blocks * 512);
3653dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey            break;
3663dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey        }
3673dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey    }
3683dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey    fts_close(fts);
369df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey#if MEASURE_DEBUG
370df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey    if ((include_gid == -1) && (exclude_gid == -1)) {
371df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey        LOG(DEBUG) << "Measured " << path << " size " << matchedSize;
372df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey    } else {
373df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey        LOG(DEBUG) << "Measured " << path << " size " << matchedSize << "; include " << include_gid
374df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey                << " exclude " << exclude_gid;
375df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey    }
376df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey#endif
377df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey    *size += matchedSize;
3783dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey    return 0;
3793dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey}
3803dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey
38194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwoodint create_move_path(char path[PKG_PATH_MAX],
38294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    const char* pkgname,
38394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    const char* leaf,
38402d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe    userid_t userid ATTRIBUTE_UNUSED)
38594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood{
38694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    if ((android_data_dir.len + strlen(PRIMARY_USER_PREFIX) + strlen(pkgname) + strlen(leaf) + 1)
38794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            >= PKG_PATH_MAX) {
38894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        return -1;
38994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
39094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
39194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    sprintf(path, "%s%s%s/%s", android_data_dir.path, PRIMARY_USER_PREFIX, pkgname, leaf);
39294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    return 0;
39394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
39494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
39594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood/**
39694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood * Checks whether the package name is valid. Returns -1 on error and
39794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood * 0 on success.
39894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood */
399423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkeybool is_valid_package_name(const std::string& packageName) {
400367ace2f77105f7a09b34a50bc875fd4fa591177Jeff Sharkey    // This logic is borrowed from PackageParser.java
401367ace2f77105f7a09b34a50bc875fd4fa591177Jeff Sharkey    bool hasSep = false;
402367ace2f77105f7a09b34a50bc875fd4fa591177Jeff Sharkey    bool front = true;
403367ace2f77105f7a09b34a50bc875fd4fa591177Jeff Sharkey
404367ace2f77105f7a09b34a50bc875fd4fa591177Jeff Sharkey    auto it = packageName.begin();
405367ace2f77105f7a09b34a50bc875fd4fa591177Jeff Sharkey    for (; it != packageName.end() && *it != '-'; it++) {
406367ace2f77105f7a09b34a50bc875fd4fa591177Jeff Sharkey        char c = *it;
407367ace2f77105f7a09b34a50bc875fd4fa591177Jeff Sharkey        if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) {
408367ace2f77105f7a09b34a50bc875fd4fa591177Jeff Sharkey            front = false;
409367ace2f77105f7a09b34a50bc875fd4fa591177Jeff Sharkey            continue;
410367ace2f77105f7a09b34a50bc875fd4fa591177Jeff Sharkey        }
411367ace2f77105f7a09b34a50bc875fd4fa591177Jeff Sharkey        if (!front) {
412367ace2f77105f7a09b34a50bc875fd4fa591177Jeff Sharkey            if ((c >= '0' && c <= '9') || c == '_') {
413367ace2f77105f7a09b34a50bc875fd4fa591177Jeff Sharkey                continue;
41494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            }
41594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        }
416367ace2f77105f7a09b34a50bc875fd4fa591177Jeff Sharkey        if (c == '.') {
417367ace2f77105f7a09b34a50bc875fd4fa591177Jeff Sharkey            hasSep = true;
418367ace2f77105f7a09b34a50bc875fd4fa591177Jeff Sharkey            front = true;
419367ace2f77105f7a09b34a50bc875fd4fa591177Jeff Sharkey            continue;
420367ace2f77105f7a09b34a50bc875fd4fa591177Jeff Sharkey        }
421367ace2f77105f7a09b34a50bc875fd4fa591177Jeff Sharkey        LOG(WARNING) << "Bad package character " << c << " in " << packageName;
422367ace2f77105f7a09b34a50bc875fd4fa591177Jeff Sharkey        return false;
423367ace2f77105f7a09b34a50bc875fd4fa591177Jeff Sharkey    }
42494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
425ab7ac8d5a04bd3f38b85ce20ae5bb382f2a26585Jeff Sharkey    if (front) {
426367ace2f77105f7a09b34a50bc875fd4fa591177Jeff Sharkey        LOG(WARNING) << "Missing separator in " << packageName;
427367ace2f77105f7a09b34a50bc875fd4fa591177Jeff Sharkey        return false;
42894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
42994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
430367ace2f77105f7a09b34a50bc875fd4fa591177Jeff Sharkey    for (; it != packageName.end(); it++) {
431367ace2f77105f7a09b34a50bc875fd4fa591177Jeff Sharkey        char c = *it;
432367ace2f77105f7a09b34a50bc875fd4fa591177Jeff Sharkey        if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) continue;
433367ace2f77105f7a09b34a50bc875fd4fa591177Jeff Sharkey        if ((c >= '0' && c <= '9') || c == '_' || c == '-' || c == '=') continue;
434367ace2f77105f7a09b34a50bc875fd4fa591177Jeff Sharkey        LOG(WARNING) << "Bad suffix character " << c << " in " << packageName;
435367ace2f77105f7a09b34a50bc875fd4fa591177Jeff Sharkey        return false;
43694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
43794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
438423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey    return true;
43994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
44094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
4413aee2c5c749dc2589f001b26fae1ec958ec89524Narayan Kamathstatic int _delete_dir_contents(DIR *d,
4423aee2c5c749dc2589f001b26fae1ec958ec89524Narayan Kamath                                int (*exclusion_predicate)(const char *name, const int is_dir))
44394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood{
44494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    int result = 0;
44594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    struct dirent *de;
44694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    int dfd;
44794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
44894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    dfd = dirfd(d);
44994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
45094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    if (dfd < 0) return -1;
45194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
45294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    while ((de = readdir(d))) {
45394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        const char *name = de->d_name;
45494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
4553aee2c5c749dc2589f001b26fae1ec958ec89524Narayan Kamath            /* check using the exclusion predicate, if provided */
4563aee2c5c749dc2589f001b26fae1ec958ec89524Narayan Kamath        if (exclusion_predicate && exclusion_predicate(name, (de->d_type == DT_DIR))) {
4573aee2c5c749dc2589f001b26fae1ec958ec89524Narayan Kamath            continue;
4583aee2c5c749dc2589f001b26fae1ec958ec89524Narayan Kamath        }
45994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
46094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        if (de->d_type == DT_DIR) {
46199d9fb15b4a1eb534261ae81b2cf25aec4447bd0Chih-Hung Hsieh            int subfd;
46294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            DIR *subdir;
46394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
46494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                /* always skip "." and ".." */
46594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            if (name[0] == '.') {
46694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                if (name[1] == 0) continue;
46794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                if ((name[1] == '.') && (name[2] == 0)) continue;
46894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            }
46994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
4708b7acacc93930df9fa9e1ebea4a4394195b2332eNick Kralevich            subfd = openat(dfd, name, O_RDONLY | O_DIRECTORY | O_NOFOLLOW | O_CLOEXEC);
47194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            if (subfd < 0) {
47294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                ALOGE("Couldn't openat %s: %s\n", name, strerror(errno));
47394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                result = -1;
47494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                continue;
47594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            }
47694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            subdir = fdopendir(subfd);
47794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            if (subdir == NULL) {
47894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                ALOGE("Couldn't fdopendir %s: %s\n", name, strerror(errno));
47994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                close(subfd);
48094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                result = -1;
48194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                continue;
48294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            }
4833aee2c5c749dc2589f001b26fae1ec958ec89524Narayan Kamath            if (_delete_dir_contents(subdir, exclusion_predicate)) {
48494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                result = -1;
48594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            }
48694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            closedir(subdir);
48794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            if (unlinkat(dfd, name, AT_REMOVEDIR) < 0) {
48894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                ALOGE("Couldn't unlinkat %s: %s\n", name, strerror(errno));
48994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                result = -1;
49094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            }
49194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        } else {
49294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            if (unlinkat(dfd, name, 0) < 0) {
49394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                ALOGE("Couldn't unlinkat %s: %s\n", name, strerror(errno));
49494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                result = -1;
49594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            }
49694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        }
49794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
49894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
49994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    return result;
50094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
50194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
502b06f98aabc5381fd6366526d9b31b5d0345481b6Calin Juravleint delete_dir_contents(const std::string& pathname, bool ignore_if_missing) {
503b06f98aabc5381fd6366526d9b31b5d0345481b6Calin Juravle    return delete_dir_contents(pathname.c_str(), 0, NULL, ignore_if_missing);
504ebf728fd43ab5c7d11a1f9e5fdc775d6740fae0aJeff Sharkey}
505ebf728fd43ab5c7d11a1f9e5fdc775d6740fae0aJeff Sharkey
506b06f98aabc5381fd6366526d9b31b5d0345481b6Calin Juravleint delete_dir_contents_and_dir(const std::string& pathname, bool ignore_if_missing) {
507b06f98aabc5381fd6366526d9b31b5d0345481b6Calin Juravle    return delete_dir_contents(pathname.c_str(), 1, NULL, ignore_if_missing);
508ebf728fd43ab5c7d11a1f9e5fdc775d6740fae0aJeff Sharkey}
509ebf728fd43ab5c7d11a1f9e5fdc775d6740fae0aJeff Sharkey
51094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwoodint delete_dir_contents(const char *pathname,
51194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                        int also_delete_dir,
512b06f98aabc5381fd6366526d9b31b5d0345481b6Calin Juravle                        int (*exclusion_predicate)(const char*, const int),
513b06f98aabc5381fd6366526d9b31b5d0345481b6Calin Juravle                        bool ignore_if_missing)
51494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood{
51594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    int res = 0;
51694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    DIR *d;
51794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
51894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    d = opendir(pathname);
51994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    if (d == NULL) {
520b06f98aabc5381fd6366526d9b31b5d0345481b6Calin Juravle        if (ignore_if_missing && (errno == ENOENT)) {
521b06f98aabc5381fd6366526d9b31b5d0345481b6Calin Juravle            return 0;
522b06f98aabc5381fd6366526d9b31b5d0345481b6Calin Juravle        }
52394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        ALOGE("Couldn't opendir %s: %s\n", pathname, strerror(errno));
52494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        return -errno;
52594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
5263aee2c5c749dc2589f001b26fae1ec958ec89524Narayan Kamath    res = _delete_dir_contents(d, exclusion_predicate);
52794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    closedir(d);
52894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    if (also_delete_dir) {
52994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        if (rmdir(pathname)) {
53094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            ALOGE("Couldn't rmdir %s: %s\n", pathname, strerror(errno));
53194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            res = -1;
53294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        }
53394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
53494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    return res;
53594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
53694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
53794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwoodint delete_dir_contents_fd(int dfd, const char *name)
53894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood{
53994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    int fd, res;
54094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    DIR *d;
54194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
5428b7acacc93930df9fa9e1ebea4a4394195b2332eNick Kralevich    fd = openat(dfd, name, O_RDONLY | O_DIRECTORY | O_NOFOLLOW | O_CLOEXEC);
54394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    if (fd < 0) {
54494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        ALOGE("Couldn't openat %s: %s\n", name, strerror(errno));
54594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        return -1;
54694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
54794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    d = fdopendir(fd);
54894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    if (d == NULL) {
54994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        ALOGE("Couldn't fdopendir %s: %s\n", name, strerror(errno));
55094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        close(fd);
55194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        return -1;
55294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
55394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    res = _delete_dir_contents(d, 0);
55494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    closedir(d);
55594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    return res;
55694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
55794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
55860fd3feecab4336d964ca8e31c7c3220e1afb558Robin Leestatic int _copy_owner_permissions(int srcfd, int dstfd)
55960fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee{
56060fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee    struct stat st;
56160fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee    if (fstat(srcfd, &st) != 0) {
56260fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee        return -1;
56360fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee    }
56460fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee    if (fchmod(dstfd, st.st_mode) != 0) {
56560fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee        return -1;
56660fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee    }
56760fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee    return 0;
56860fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee}
56960fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee
57060fd3feecab4336d964ca8e31c7c3220e1afb558Robin Leestatic int _copy_dir_files(int sdfd, int ddfd, uid_t owner, gid_t group)
57160fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee{
57260fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee    int result = 0;
57360fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee    if (_copy_owner_permissions(sdfd, ddfd) != 0) {
57460fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee        ALOGE("_copy_dir_files failed to copy dir permissions\n");
57560fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee    }
57660fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee    if (fchown(ddfd, owner, group) != 0) {
57760fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee        ALOGE("_copy_dir_files failed to change dir owner\n");
57860fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee    }
57960fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee
58060fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee    DIR *ds = fdopendir(sdfd);
58160fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee    if (ds == NULL) {
58260fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee        ALOGE("Couldn't fdopendir: %s\n", strerror(errno));
58360fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee        return -1;
58460fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee    }
58560fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee    struct dirent *de;
58660fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee    while ((de = readdir(ds))) {
58760fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee        if (de->d_type != DT_REG) {
58860fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee            continue;
58960fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee        }
59060fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee
59160fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee        const char *name = de->d_name;
59260fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee        int fsfd = openat(sdfd, name, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
59360fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee        int fdfd = openat(ddfd, name, O_WRONLY | O_NOFOLLOW | O_CLOEXEC | O_CREAT, 0600);
59460fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee        if (fsfd == -1 || fdfd == -1) {
59560fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee            ALOGW("Couldn't copy %s: %s\n", name, strerror(errno));
59660fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee        } else {
59760fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee            if (_copy_owner_permissions(fsfd, fdfd) != 0) {
59860fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee                ALOGE("Failed to change file permissions\n");
59960fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee            }
60060fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee            if (fchown(fdfd, owner, group) != 0) {
60160fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee                ALOGE("Failed to change file owner\n");
60260fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee            }
60360fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee
60460fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee            char buf[8192];
60560fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee            ssize_t size;
60660fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee            while ((size = read(fsfd, buf, sizeof(buf))) > 0) {
60760fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee                write(fdfd, buf, size);
60860fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee            }
60960fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee            if (size < 0) {
61060fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee                ALOGW("Couldn't copy %s: %s\n", name, strerror(errno));
61160fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee                result = -1;
61260fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee            }
61360fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee        }
61460fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee        close(fdfd);
61560fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee        close(fsfd);
61660fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee    }
61760fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee
61860fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee    return result;
61960fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee}
62060fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee
62160fd3feecab4336d964ca8e31c7c3220e1afb558Robin Leeint copy_dir_files(const char *srcname,
62260fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee                   const char *dstname,
62360fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee                   uid_t owner,
62460fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee                   uid_t group)
62560fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee{
62660fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee    int res = 0;
62760fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee    DIR *ds = NULL;
62860fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee    DIR *dd = NULL;
62960fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee
63060fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee    ds = opendir(srcname);
63160fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee    if (ds == NULL) {
63260fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee        ALOGE("Couldn't opendir %s: %s\n", srcname, strerror(errno));
63360fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee        return -errno;
63460fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee    }
63560fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee
63660fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee    mkdir(dstname, 0600);
63760fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee    dd = opendir(dstname);
63860fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee    if (dd == NULL) {
63960fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee        ALOGE("Couldn't opendir %s: %s\n", dstname, strerror(errno));
64060fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee        closedir(ds);
64160fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee        return -errno;
64260fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee    }
64360fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee
64460fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee    int sdfd = dirfd(ds);
64560fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee    int ddfd = dirfd(dd);
64660fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee    if (sdfd != -1 && ddfd != -1) {
64760fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee        res = _copy_dir_files(sdfd, ddfd, owner, group);
64860fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee    } else {
64960fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee        res = -errno;
65060fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee    }
65160fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee    closedir(dd);
65260fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee    closedir(ds);
65360fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee    return res;
65460fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee}
65560fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee
656a836c472f017f09cf16fa68176df461a4958d22aJeff Sharkeyint64_t data_disk_free(const std::string& data_path) {
657ed909ae8db2f44ce7fe7003c6fee457f13669702Jeff Sharkey    struct statvfs sfs;
658ed909ae8db2f44ce7fe7003c6fee457f13669702Jeff Sharkey    if (statvfs(data_path.c_str(), &sfs) == 0) {
659bdd4de8a98d57110befb0c29f662e8a3e4cfc275Jeff Sharkey        return static_cast<int64_t>(sfs.f_bavail) * sfs.f_frsize;
66094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    } else {
661ed909ae8db2f44ce7fe7003c6fee457f13669702Jeff Sharkey        PLOG(ERROR) << "Couldn't statvfs " << data_path;
66294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        return -1;
66394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
66494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
66594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
6669a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkeyint get_path_inode(const std::string& path, ino_t *inode) {
6679a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey    struct stat buf;
6689a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey    memset(&buf, 0, sizeof(buf));
6699a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey    if (stat(path.c_str(), &buf) != 0) {
6709a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey        PLOG(WARNING) << "Failed to stat " << path;
6719a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey        return -1;
6729a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey    } else {
6739a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey        *inode = buf.st_ino;
6749a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey        return 0;
6759a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey    }
6769a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey}
6779a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey
6789a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey/**
6799a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey * Write the inode of a specific child file into the given xattr on the
6809a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey * parent directory. This allows you to find the child later, even if its
6819a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey * name is encrypted.
6829a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey */
6839a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkeyint write_path_inode(const std::string& parent, const char* name, const char* inode_xattr) {
6849a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey    ino_t inode = 0;
6859a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey    uint64_t inode_raw = 0;
6869a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey    auto path = StringPrintf("%s/%s", parent.c_str(), name);
6879a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey
6889a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey    if (get_path_inode(path, &inode) != 0) {
6899a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey        // Path probably doesn't exist yet; ignore
6909a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey        return 0;
6919a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey    }
6929a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey
6939a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey    // Check to see if already set correctly
6949a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey    if (getxattr(parent.c_str(), inode_xattr, &inode_raw, sizeof(inode_raw)) == sizeof(inode_raw)) {
6959a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey        if (inode_raw == inode) {
6969a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey            // Already set correctly; skip writing
6979a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey            return 0;
6989a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey        } else {
6999a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey            PLOG(WARNING) << "Mismatched inode value; found " << inode
7009a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey                    << " on disk but marked value was " << inode_raw << "; overwriting";
7019a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey        }
7029a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey    }
7039a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey
7049a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey    inode_raw = inode;
7054ed6507cfb4f0fae8567b42037e74a07f7dd28baJeff Sharkey    if (setxattr(parent.c_str(), inode_xattr, &inode_raw, sizeof(inode_raw), 0) != 0 && errno != EOPNOTSUPP) {
7069a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey        PLOG(ERROR) << "Failed to write xattr " << inode_xattr << " at " << parent;
7079a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey        return -1;
7089a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey    } else {
7099a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey        return 0;
7109a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey    }
7119a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey}
7129a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey
7139a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey/**
7149a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey * Read the inode of a specific child file from the given xattr on the
7159a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey * parent directory. Returns a currently valid path for that child, which
7169a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey * might have an encrypted name.
7179a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey */
7189a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkeystd::string read_path_inode(const std::string& parent, const char* name, const char* inode_xattr) {
7199a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey    ino_t inode = 0;
7209a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey    uint64_t inode_raw = 0;
7219a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey    auto fallback = StringPrintf("%s/%s", parent.c_str(), name);
7229a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey
7239a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey    // Lookup the inode value written earlier
7249a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey    if (getxattr(parent.c_str(), inode_xattr, &inode_raw, sizeof(inode_raw)) == sizeof(inode_raw)) {
7259a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey        inode = inode_raw;
7269a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey    }
7279a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey
7289a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey    // For testing purposes, rely on the inode when defined; this could be
7299a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey    // optimized to use access() in the future.
7309a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey    if (inode != 0) {
7319a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey        DIR* dir = opendir(parent.c_str());
7329a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey        if (dir == nullptr) {
7339a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey            PLOG(ERROR) << "Failed to opendir " << parent;
7349a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey            return fallback;
7359a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey        }
7369a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey
7379a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey        struct dirent* ent;
7389a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey        while ((ent = readdir(dir))) {
7399a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey            if (ent->d_ino == inode) {
7409a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey                auto resolved = StringPrintf("%s/%s", parent.c_str(), ent->d_name);
7419a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey#if DEBUG_XATTRS
7429a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey                if (resolved != fallback) {
7439a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey                    LOG(DEBUG) << "Resolved path " << resolved << " for inode " << inode
7449a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey                            << " instead of " << fallback;
7459a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey                }
7469a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey#endif
7479a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey                closedir(dir);
7489a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey                return resolved;
7499a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey            }
7509a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey        }
7519a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey        LOG(WARNING) << "Failed to resolve inode " << inode << "; using " << fallback;
7529a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey        closedir(dir);
7539a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey        return fallback;
7549a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey    } else {
7559a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey        return fallback;
7569a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey    }
7579a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey}
7589a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey
75994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood/**
760c597b6dd895dbb2b28c757ce7a2651b3cdc9b00cCalin Juravle * Validate that the path is valid in the context of the provided directory.
761c597b6dd895dbb2b28c757ce7a2651b3cdc9b00cCalin Juravle * The path is allowed to have at most one subdirectory and no indirections
762c597b6dd895dbb2b28c757ce7a2651b3cdc9b00cCalin Juravle * to top level directories (i.e. have "..").
763c597b6dd895dbb2b28c757ce7a2651b3cdc9b00cCalin Juravle */
764e23a13299a4f6f2488935b4786cdbb46f46e3d3cJeff Sharkeystatic int validate_path(const dir_rec_t* dir, const char* path, int maxSubdirs) {
765c597b6dd895dbb2b28c757ce7a2651b3cdc9b00cCalin Juravle    size_t dir_len = dir->len;
766c597b6dd895dbb2b28c757ce7a2651b3cdc9b00cCalin Juravle    const char* subdir = strchr(path + dir_len, '/');
767c597b6dd895dbb2b28c757ce7a2651b3cdc9b00cCalin Juravle
768c597b6dd895dbb2b28c757ce7a2651b3cdc9b00cCalin Juravle    // Only allow the path to have at most one subdirectory.
769c597b6dd895dbb2b28c757ce7a2651b3cdc9b00cCalin Juravle    if (subdir != NULL) {
770c597b6dd895dbb2b28c757ce7a2651b3cdc9b00cCalin Juravle        ++subdir;
771e23a13299a4f6f2488935b4786cdbb46f46e3d3cJeff Sharkey        if ((--maxSubdirs == 0) && strchr(subdir, '/') != NULL) {
772c597b6dd895dbb2b28c757ce7a2651b3cdc9b00cCalin Juravle            ALOGE("invalid apk path '%s' (subdir?)\n", path);
773c597b6dd895dbb2b28c757ce7a2651b3cdc9b00cCalin Juravle            return -1;
774c597b6dd895dbb2b28c757ce7a2651b3cdc9b00cCalin Juravle        }
775c597b6dd895dbb2b28c757ce7a2651b3cdc9b00cCalin Juravle    }
776c597b6dd895dbb2b28c757ce7a2651b3cdc9b00cCalin Juravle
777c597b6dd895dbb2b28c757ce7a2651b3cdc9b00cCalin Juravle    // Directories can't have a period directly after the directory markers to prevent "..".
778c597b6dd895dbb2b28c757ce7a2651b3cdc9b00cCalin Juravle    if ((path[dir_len] == '.') || ((subdir != NULL) && (*subdir == '.'))) {
779c597b6dd895dbb2b28c757ce7a2651b3cdc9b00cCalin Juravle        ALOGE("invalid apk path '%s' (trickery)\n", path);
780c597b6dd895dbb2b28c757ce7a2651b3cdc9b00cCalin Juravle        return -1;
781c597b6dd895dbb2b28c757ce7a2651b3cdc9b00cCalin Juravle    }
782c597b6dd895dbb2b28c757ce7a2651b3cdc9b00cCalin Juravle
783c597b6dd895dbb2b28c757ce7a2651b3cdc9b00cCalin Juravle    return 0;
784c597b6dd895dbb2b28c757ce7a2651b3cdc9b00cCalin Juravle}
785c597b6dd895dbb2b28c757ce7a2651b3cdc9b00cCalin Juravle
786c597b6dd895dbb2b28c757ce7a2651b3cdc9b00cCalin Juravle/**
78794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood * Checks whether a path points to a system app (.apk file). Returns 0
78894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood * if it is a system app or -1 if it is not.
78994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood */
79094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwoodint validate_system_app_path(const char* path) {
79194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    size_t i;
79294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
79394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    for (i = 0; i < android_system_dirs.count; i++) {
79494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        const size_t dir_len = android_system_dirs.dirs[i].len;
79594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        if (!strncmp(path, android_system_dirs.dirs[i].path, dir_len)) {
796e23a13299a4f6f2488935b4786cdbb46f46e3d3cJeff Sharkey            return validate_path(android_system_dirs.dirs + i, path, 1);
79794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        }
79894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
79994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
80094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    return -1;
80194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
80294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
803114f08107be384a3f9cc954bdec2b6b7619354aeCalin Juravlebool validate_secondary_dex_path(const std::string& pkgname, const std::string& dex_path,
8049cd45602a89b2af5d4b8086cbb806b64ff6b78a4Calin Juravle        const char* volume_uuid, int uid, int storage_flag, bool validate_package_path) {
80542451c029b0e87990e5833daea2286bb12c21df5Calin Juravle    CHECK(storage_flag == FLAG_STORAGE_CE || storage_flag == FLAG_STORAGE_DE);
80642451c029b0e87990e5833daea2286bb12c21df5Calin Juravle
8071c809c7068f610ff22174dbe96b49f1b8ada9c1eCalin Juravle    // Empty paths are not allowed.
8081c809c7068f610ff22174dbe96b49f1b8ada9c1eCalin Juravle    if (dex_path.empty()) { return false; }
8091c809c7068f610ff22174dbe96b49f1b8ada9c1eCalin Juravle    // First character should always be '/'. No relative paths.
8101c809c7068f610ff22174dbe96b49f1b8ada9c1eCalin Juravle    if (dex_path[0] != '/') { return false; }
8111c809c7068f610ff22174dbe96b49f1b8ada9c1eCalin Juravle    // The last character should not be '/'.
8121c809c7068f610ff22174dbe96b49f1b8ada9c1eCalin Juravle    if (dex_path[dex_path.size() - 1] == '/') { return false; }
8131c809c7068f610ff22174dbe96b49f1b8ada9c1eCalin Juravle    // There should be no '.' after the directory marker.
8141c809c7068f610ff22174dbe96b49f1b8ada9c1eCalin Juravle    if (dex_path.find("/.") != std::string::npos) { return false; }
8151c809c7068f610ff22174dbe96b49f1b8ada9c1eCalin Juravle    // The path should be at most PKG_PATH_MAX long.
8161c809c7068f610ff22174dbe96b49f1b8ada9c1eCalin Juravle    if (dex_path.size() > PKG_PATH_MAX) { return false; }
8171c809c7068f610ff22174dbe96b49f1b8ada9c1eCalin Juravle
8189cd45602a89b2af5d4b8086cbb806b64ff6b78a4Calin Juravle    if (validate_package_path) {
8199cd45602a89b2af5d4b8086cbb806b64ff6b78a4Calin Juravle        // If we are asked to validate the package path check that
8209cd45602a89b2af5d4b8086cbb806b64ff6b78a4Calin Juravle        // the dex_path is under the app data directory.
8219cd45602a89b2af5d4b8086cbb806b64ff6b78a4Calin Juravle        std::string app_private_dir = storage_flag == FLAG_STORAGE_CE
8229cd45602a89b2af5d4b8086cbb806b64ff6b78a4Calin Juravle            ? create_data_user_ce_package_path(
8239cd45602a89b2af5d4b8086cbb806b64ff6b78a4Calin Juravle                    volume_uuid, multiuser_get_user_id(uid), pkgname.c_str())
8249cd45602a89b2af5d4b8086cbb806b64ff6b78a4Calin Juravle            : create_data_user_de_package_path(
8259cd45602a89b2af5d4b8086cbb806b64ff6b78a4Calin Juravle                    volume_uuid, multiuser_get_user_id(uid), pkgname.c_str());
8269cd45602a89b2af5d4b8086cbb806b64ff6b78a4Calin Juravle
8279cd45602a89b2af5d4b8086cbb806b64ff6b78a4Calin Juravle        if (strncmp(dex_path.c_str(), app_private_dir.c_str(), app_private_dir.size()) != 0) {
8289cd45602a89b2af5d4b8086cbb806b64ff6b78a4Calin Juravle            return false;
8299cd45602a89b2af5d4b8086cbb806b64ff6b78a4Calin Juravle        }
83042451c029b0e87990e5833daea2286bb12c21df5Calin Juravle    }
8311c809c7068f610ff22174dbe96b49f1b8ada9c1eCalin Juravle
8321c809c7068f610ff22174dbe96b49f1b8ada9c1eCalin Juravle    // If we got here we have a valid path.
8331c809c7068f610ff22174dbe96b49f1b8ada9c1eCalin Juravle    return true;
83442451c029b0e87990e5833daea2286bb12c21df5Calin Juravle}
83542451c029b0e87990e5833daea2286bb12c21df5Calin Juravle
83694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood/**
83794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood * Get the contents of a environment variable that contains a path. Caller
83894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood * owns the string that is inserted into the directory record. Returns
83994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood * 0 on success and -1 on error.
84094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood */
84194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwoodint get_path_from_env(dir_rec_t* rec, const char* var) {
84294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    const char* path = getenv(var);
84394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    int ret = get_path_from_string(rec, path);
84494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    if (ret < 0) {
84594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        ALOGW("Problem finding value for environment variable %s\n", var);
84694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
84794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    return ret;
84894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
84994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
85094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood/**
85194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood * Puts the string into the record as a directory. Appends '/' to the end
85294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood * of all paths. Caller owns the string that is inserted into the directory
85394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood * record. A null value will result in an error.
85494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood *
85594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood * Returns 0 on success and -1 on error.
85694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood */
85794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwoodint get_path_from_string(dir_rec_t* rec, const char* path) {
85894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    if (path == NULL) {
85994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        return -1;
86094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    } else {
86194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        const size_t path_len = strlen(path);
86294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        if (path_len <= 0) {
86394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            return -1;
86494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        }
86594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
86694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        // Make sure path is absolute.
86794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        if (path[0] != '/') {
86894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            return -1;
86994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        }
87094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
87194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        if (path[path_len - 1] == '/') {
87294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            // Path ends with a forward slash. Make our own copy.
87394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
87494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            rec->path = strdup(path);
87594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            if (rec->path == NULL) {
87694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                return -1;
87794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            }
87894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
87994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            rec->len = path_len;
88094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        } else {
88194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            // Path does not end with a slash. Generate a new string.
88294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            char *dst;
88394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
88494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            // Add space for slash and terminating null.
88594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            size_t dst_size = path_len + 2;
88694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
88719803807cd7ae01868fcfa50305f4a7dd13765e2Jeff Sharkey            rec->path = (char*) malloc(dst_size);
88894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            if (rec->path == NULL) {
88994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                return -1;
89094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            }
89194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
89294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            dst = rec->path;
89394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
89494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            if (append_and_increment(&dst, path, &dst_size) < 0
89594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                    || append_and_increment(&dst, "/", &dst_size)) {
89694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                ALOGE("Error canonicalizing path");
89794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                return -1;
89894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            }
89994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
90094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            rec->len = dst - rec->path;
90194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        }
90294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
90394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    return 0;
90494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
90594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
90694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwoodint copy_and_append(dir_rec_t* dst, const dir_rec_t* src, const char* suffix) {
90794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    dst->len = src->len + strlen(suffix);
90894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    const size_t dstSize = dst->len + 1;
90994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    dst->path = (char*) malloc(dstSize);
91094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
91194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    if (dst->path == NULL
91294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            || snprintf(dst->path, dstSize, "%s%s", src->path, suffix)
91394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                    != (ssize_t) dst->len) {
91494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        ALOGE("Could not allocate memory to hold appended path; aborting\n");
91594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        return -1;
91694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
91794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
91894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    return 0;
91994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
92094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
92194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood/**
922d845c96128a40ca5802c0840ae190fa0af7d4735Narayan Kamath * Check whether path points to a valid path for an APK file. The path must
923d845c96128a40ca5802c0840ae190fa0af7d4735Narayan Kamath * begin with a whitelisted prefix path and must be no deeper than |maxSubdirs| within
924d845c96128a40ca5802c0840ae190fa0af7d4735Narayan Kamath * that path. Returns -1 when an invalid path is encountered and 0 when a valid path
925d845c96128a40ca5802c0840ae190fa0af7d4735Narayan Kamath * is encountered.
92694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood */
927d845c96128a40ca5802c0840ae190fa0af7d4735Narayan Kamathstatic int validate_apk_path_internal(const char *path, int maxSubdirs) {
928c597b6dd895dbb2b28c757ce7a2651b3cdc9b00cCalin Juravle    const dir_rec_t* dir = NULL;
92994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    if (!strncmp(path, android_app_dir.path, android_app_dir.len)) {
930c597b6dd895dbb2b28c757ce7a2651b3cdc9b00cCalin Juravle        dir = &android_app_dir;
93194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    } else if (!strncmp(path, android_app_private_dir.path, android_app_private_dir.len)) {
932c597b6dd895dbb2b28c757ce7a2651b3cdc9b00cCalin Juravle        dir = &android_app_private_dir;
9335c1a910e6a2d2c42002dc6ed88ff770336afcb3fTodd Kennedy    } else if (!strncmp(path, android_app_ephemeral_dir.path, android_app_ephemeral_dir.len)) {
9345c1a910e6a2d2c42002dc6ed88ff770336afcb3fTodd Kennedy        dir = &android_app_ephemeral_dir;
93594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    } else if (!strncmp(path, android_asec_dir.path, android_asec_dir.len)) {
936c597b6dd895dbb2b28c757ce7a2651b3cdc9b00cCalin Juravle        dir = &android_asec_dir;
937e23a13299a4f6f2488935b4786cdbb46f46e3d3cJeff Sharkey    } else if (!strncmp(path, android_mnt_expand_dir.path, android_mnt_expand_dir.len)) {
938e23a13299a4f6f2488935b4786cdbb46f46e3d3cJeff Sharkey        dir = &android_mnt_expand_dir;
939d845c96128a40ca5802c0840ae190fa0af7d4735Narayan Kamath        if (maxSubdirs < 2) {
940d845c96128a40ca5802c0840ae190fa0af7d4735Narayan Kamath            maxSubdirs = 2;
941d845c96128a40ca5802c0840ae190fa0af7d4735Narayan Kamath        }
94294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    } else {
94394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        return -1;
94494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
94594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
946e23a13299a4f6f2488935b4786cdbb46f46e3d3cJeff Sharkey    return validate_path(dir, path, maxSubdirs);
94794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
94894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
949d845c96128a40ca5802c0840ae190fa0af7d4735Narayan Kamathint validate_apk_path(const char* path) {
950d845c96128a40ca5802c0840ae190fa0af7d4735Narayan Kamath    return validate_apk_path_internal(path, 1 /* maxSubdirs */);
951d845c96128a40ca5802c0840ae190fa0af7d4735Narayan Kamath}
952d845c96128a40ca5802c0840ae190fa0af7d4735Narayan Kamath
953d845c96128a40ca5802c0840ae190fa0af7d4735Narayan Kamathint validate_apk_path_subdirs(const char* path) {
954d845c96128a40ca5802c0840ae190fa0af7d4735Narayan Kamath    return validate_apk_path_internal(path, 3 /* maxSubdirs */);
955d845c96128a40ca5802c0840ae190fa0af7d4735Narayan Kamath}
956d845c96128a40ca5802c0840ae190fa0af7d4735Narayan Kamath
95794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwoodint append_and_increment(char** dst, const char* src, size_t* dst_size) {
95894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    ssize_t ret = strlcpy(*dst, src, *dst_size);
95994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    if (ret < 0 || (size_t) ret >= *dst_size) {
96094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        return -1;
96194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
96294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    *dst += ret;
96394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    *dst_size -= ret;
96494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    return 0;
96594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
96694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
96719803807cd7ae01868fcfa50305f4a7dd13765e2Jeff Sharkeychar *build_string2(const char *s1, const char *s2) {
96894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    if (s1 == NULL || s2 == NULL) return NULL;
96994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
97094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    int len_s1 = strlen(s1);
97194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    int len_s2 = strlen(s2);
97294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    int len = len_s1 + len_s2 + 1;
97319803807cd7ae01868fcfa50305f4a7dd13765e2Jeff Sharkey    char *result = (char *) malloc(len);
97494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    if (result == NULL) return NULL;
97594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
97694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    strcpy(result, s1);
97794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    strcpy(result + len_s1, s2);
97894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
97994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    return result;
98094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
98194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
98219803807cd7ae01868fcfa50305f4a7dd13765e2Jeff Sharkeychar *build_string3(const char *s1, const char *s2, const char *s3) {
98394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    if (s1 == NULL || s2 == NULL || s3 == NULL) return NULL;
98494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
98594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    int len_s1 = strlen(s1);
98694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    int len_s2 = strlen(s2);
98794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    int len_s3 = strlen(s3);
98894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    int len = len_s1 + len_s2 + len_s3 + 1;
98919803807cd7ae01868fcfa50305f4a7dd13765e2Jeff Sharkey    char *result = (char *) malloc(len);
99094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    if (result == NULL) return NULL;
99194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
99294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    strcpy(result, s1);
99394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    strcpy(result + len_s1, s2);
99494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    strcpy(result + len_s1 + len_s2, s3);
99594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
99694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    return result;
99794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
99894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
999095c763dd9aa26a206d10ab7c1d7e1c569298fb3Robin Leeint ensure_config_user_dirs(userid_t userid) {
1000095c763dd9aa26a206d10ab7c1d7e1c569298fb3Robin Lee    // writable by system, readable by any app within the same user
100160fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee    const int uid = multiuser_get_uid(userid, AID_SYSTEM);
100260fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee    const int gid = multiuser_get_uid(userid, AID_EVERYBODY);
1003095c763dd9aa26a206d10ab7c1d7e1c569298fb3Robin Lee
1004095c763dd9aa26a206d10ab7c1d7e1c569298fb3Robin Lee    // Ensure /data/misc/user/<userid> exists
1005379a12b0072b322c7f86e690a8e8a220e500861cJeff Sharkey    auto path = create_data_misc_legacy_path(userid);
1006379a12b0072b322c7f86e690a8e8a220e500861cJeff Sharkey    return fs_prepare_dir(path.c_str(), 0750, uid, gid);
1007095c763dd9aa26a206d10ab7c1d7e1c569298fb3Robin Lee}
100802d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe
100902d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampeint wait_child(pid_t pid)
101002d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe{
101102d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe    int status;
101202d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe    pid_t got_pid;
101302d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe
101402d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe    while (1) {
101502d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe        got_pid = waitpid(pid, &status, 0);
101602d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe        if (got_pid == -1 && errno == EINTR) {
101702d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe            printf("waitpid interrupted, retrying\n");
101802d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe        } else {
101902d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe            break;
102002d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe        }
102102d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe    }
102202d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe    if (got_pid != pid) {
102302d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe        ALOGW("waitpid failed: wanted %d, got %d: %s\n",
102402d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe            (int) pid, (int) got_pid, strerror(errno));
102502d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe        return 1;
102602d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe    }
102702d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe
102802d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe    if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
102902d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe        return 0;
103002d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe    } else {
103102d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe        return status;      /* always nonzero */
103202d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe    }
103302d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe}
103402d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe
103542451c029b0e87990e5833daea2286bb12c21df5Calin Juravle/**
103642451c029b0e87990e5833daea2286bb12c21df5Calin Juravle * Prepare an app cache directory, which offers to fix-up the GID and
103742451c029b0e87990e5833daea2286bb12c21df5Calin Juravle * directory mode flags during a platform upgrade.
103842451c029b0e87990e5833daea2286bb12c21df5Calin Juravle * The app cache directory path will be 'parent'/'name'.
103942451c029b0e87990e5833daea2286bb12c21df5Calin Juravle */
104042451c029b0e87990e5833daea2286bb12c21df5Calin Juravleint prepare_app_cache_dir(const std::string& parent, const char* name, mode_t target_mode,
104142451c029b0e87990e5833daea2286bb12c21df5Calin Juravle        uid_t uid, gid_t gid) {
104242451c029b0e87990e5833daea2286bb12c21df5Calin Juravle    auto path = StringPrintf("%s/%s", parent.c_str(), name);
104342451c029b0e87990e5833daea2286bb12c21df5Calin Juravle    struct stat st;
104442451c029b0e87990e5833daea2286bb12c21df5Calin Juravle    if (stat(path.c_str(), &st) != 0) {
104542451c029b0e87990e5833daea2286bb12c21df5Calin Juravle        if (errno == ENOENT) {
104642451c029b0e87990e5833daea2286bb12c21df5Calin Juravle            // This is fine, just create it
104742451c029b0e87990e5833daea2286bb12c21df5Calin Juravle            if (fs_prepare_dir_strict(path.c_str(), target_mode, uid, gid) != 0) {
104842451c029b0e87990e5833daea2286bb12c21df5Calin Juravle                PLOG(ERROR) << "Failed to prepare " << path;
104942451c029b0e87990e5833daea2286bb12c21df5Calin Juravle                return -1;
105042451c029b0e87990e5833daea2286bb12c21df5Calin Juravle            } else {
105142451c029b0e87990e5833daea2286bb12c21df5Calin Juravle                return 0;
105242451c029b0e87990e5833daea2286bb12c21df5Calin Juravle            }
105342451c029b0e87990e5833daea2286bb12c21df5Calin Juravle        } else {
105442451c029b0e87990e5833daea2286bb12c21df5Calin Juravle            PLOG(ERROR) << "Failed to stat " << path;
105542451c029b0e87990e5833daea2286bb12c21df5Calin Juravle            return -1;
105642451c029b0e87990e5833daea2286bb12c21df5Calin Juravle        }
105742451c029b0e87990e5833daea2286bb12c21df5Calin Juravle    }
105842451c029b0e87990e5833daea2286bb12c21df5Calin Juravle
105942451c029b0e87990e5833daea2286bb12c21df5Calin Juravle    mode_t actual_mode = st.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO | S_ISGID);
106042451c029b0e87990e5833daea2286bb12c21df5Calin Juravle    if (st.st_uid != uid) {
106142451c029b0e87990e5833daea2286bb12c21df5Calin Juravle        // Mismatched UID is real trouble; we can't recover
106242451c029b0e87990e5833daea2286bb12c21df5Calin Juravle        LOG(ERROR) << "Mismatched UID at " << path << ": found " << st.st_uid
106342451c029b0e87990e5833daea2286bb12c21df5Calin Juravle                << " but expected " << uid;
106442451c029b0e87990e5833daea2286bb12c21df5Calin Juravle        return -1;
106542451c029b0e87990e5833daea2286bb12c21df5Calin Juravle    } else if (st.st_gid == gid && actual_mode == target_mode) {
106642451c029b0e87990e5833daea2286bb12c21df5Calin Juravle        // Everything looks good!
106742451c029b0e87990e5833daea2286bb12c21df5Calin Juravle        return 0;
1068e59c85cc0e78bfcc8fec6acc8e37e6a472ffc07fJeff Sharkey    } else {
1069e59c85cc0e78bfcc8fec6acc8e37e6a472ffc07fJeff Sharkey        // Mismatched GID/mode is recoverable; fall through to update
1070e59c85cc0e78bfcc8fec6acc8e37e6a472ffc07fJeff Sharkey        LOG(DEBUG) << "Mismatched cache GID/mode at " << path << ": found " << st.st_gid
1071e59c85cc0e78bfcc8fec6acc8e37e6a472ffc07fJeff Sharkey                << " but expected " << gid;
107242451c029b0e87990e5833daea2286bb12c21df5Calin Juravle    }
107342451c029b0e87990e5833daea2286bb12c21df5Calin Juravle
107442451c029b0e87990e5833daea2286bb12c21df5Calin Juravle    // Directory is owned correctly, but GID or mode mismatch means it's
107542451c029b0e87990e5833daea2286bb12c21df5Calin Juravle    // probably a platform upgrade so we need to fix them
107642451c029b0e87990e5833daea2286bb12c21df5Calin Juravle    FTS *fts;
107742451c029b0e87990e5833daea2286bb12c21df5Calin Juravle    FTSENT *p;
107842451c029b0e87990e5833daea2286bb12c21df5Calin Juravle    char *argv[] = { (char*) path.c_str(), nullptr };
1079b26786d647b624498c11405075e5223d1853f30aJeff Sharkey    if (!(fts = fts_open(argv, FTS_PHYSICAL | FTS_NOCHDIR | FTS_XDEV, NULL))) {
108042451c029b0e87990e5833daea2286bb12c21df5Calin Juravle        PLOG(ERROR) << "Failed to fts_open " << path;
108142451c029b0e87990e5833daea2286bb12c21df5Calin Juravle        return -1;
108242451c029b0e87990e5833daea2286bb12c21df5Calin Juravle    }
108342451c029b0e87990e5833daea2286bb12c21df5Calin Juravle    while ((p = fts_read(fts)) != NULL) {
108442451c029b0e87990e5833daea2286bb12c21df5Calin Juravle        switch (p->fts_info) {
108542451c029b0e87990e5833daea2286bb12c21df5Calin Juravle        case FTS_DP:
1086e12d5964a8d14abe7f2eb6e57469cbe7f7391a19Jeff Sharkey            if (chmod(p->fts_path, target_mode) != 0) {
108742451c029b0e87990e5833daea2286bb12c21df5Calin Juravle                PLOG(WARNING) << "Failed to chmod " << p->fts_path;
108842451c029b0e87990e5833daea2286bb12c21df5Calin Juravle            }
108942451c029b0e87990e5833daea2286bb12c21df5Calin Juravle            // Intentional fall through to also set GID
109042451c029b0e87990e5833daea2286bb12c21df5Calin Juravle        case FTS_F:
1091e12d5964a8d14abe7f2eb6e57469cbe7f7391a19Jeff Sharkey            if (chown(p->fts_path, -1, gid) != 0) {
109242451c029b0e87990e5833daea2286bb12c21df5Calin Juravle                PLOG(WARNING) << "Failed to chown " << p->fts_path;
109342451c029b0e87990e5833daea2286bb12c21df5Calin Juravle            }
109442451c029b0e87990e5833daea2286bb12c21df5Calin Juravle            break;
109542451c029b0e87990e5833daea2286bb12c21df5Calin Juravle        case FTS_SL:
109642451c029b0e87990e5833daea2286bb12c21df5Calin Juravle        case FTS_SLNONE:
1097e12d5964a8d14abe7f2eb6e57469cbe7f7391a19Jeff Sharkey            if (lchown(p->fts_path, -1, gid) != 0) {
109842451c029b0e87990e5833daea2286bb12c21df5Calin Juravle                PLOG(WARNING) << "Failed to chown " << p->fts_path;
109942451c029b0e87990e5833daea2286bb12c21df5Calin Juravle            }
110042451c029b0e87990e5833daea2286bb12c21df5Calin Juravle            break;
110142451c029b0e87990e5833daea2286bb12c21df5Calin Juravle        }
110242451c029b0e87990e5833daea2286bb12c21df5Calin Juravle    }
110342451c029b0e87990e5833daea2286bb12c21df5Calin Juravle    fts_close(fts);
110442451c029b0e87990e5833daea2286bb12c21df5Calin Juravle    return 0;
110542451c029b0e87990e5833daea2286bb12c21df5Calin Juravle}
110642451c029b0e87990e5833daea2286bb12c21df5Calin Juravle
110702d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe}  // namespace installd
110802d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe}  // namespace android
1109