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.
17141ea424413c0381ef2aa15fc5bd5d4b88abd23c4Jeff Sharkey */
1722f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkeystd::string create_data_user_ce_path(const char* volume_uuid, userid_t userid) {
17341ea424413c0381ef2aa15fc5bd5d4b88abd23c4Jeff Sharkey    std::string data(create_data_path(volume_uuid));
17441ea424413c0381ef2aa15fc5bd5d4b88abd23c4Jeff Sharkey    if (volume_uuid == nullptr) {
17541ea424413c0381ef2aa15fc5bd5d4b88abd23c4Jeff Sharkey        if (userid == 0) {
17641ea424413c0381ef2aa15fc5bd5d4b88abd23c4Jeff Sharkey            return StringPrintf("%s/data", data.c_str());
17741ea424413c0381ef2aa15fc5bd5d4b88abd23c4Jeff Sharkey        } else {
17841ea424413c0381ef2aa15fc5bd5d4b88abd23c4Jeff Sharkey            return StringPrintf("%s/user/%u", data.c_str(), userid);
17994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        }
18041ea424413c0381ef2aa15fc5bd5d4b88abd23c4Jeff Sharkey    } else {
18141ea424413c0381ef2aa15fc5bd5d4b88abd23c4Jeff Sharkey        return StringPrintf("%s/user/%u", data.c_str(), userid);
18294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
18394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
18494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
18594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood/**
18663ec2d64196144b2d15d2baffedccfa011d6494fJeff Sharkey * Create the path name for device encrypted user data for a certain userid.
18763ec2d64196144b2d15d2baffedccfa011d6494fJeff Sharkey */
18863ec2d64196144b2d15d2baffedccfa011d6494fJeff Sharkeystd::string create_data_user_de_path(const char* volume_uuid, userid_t userid) {
18963ec2d64196144b2d15d2baffedccfa011d6494fJeff Sharkey    std::string data(create_data_path(volume_uuid));
19063ec2d64196144b2d15d2baffedccfa011d6494fJeff Sharkey    return StringPrintf("%s/user_de/%u", data.c_str(), userid);
19163ec2d64196144b2d15d2baffedccfa011d6494fJeff Sharkey}
19263ec2d64196144b2d15d2baffedccfa011d6494fJeff Sharkey
19363ec2d64196144b2d15d2baffedccfa011d6494fJeff Sharkey/**
194abe4fe5b46157ecd2a52d28abf938c816c3ce878Jeff Sharkey * Create the path name for media for a certain userid.
19594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood */
19641ea424413c0381ef2aa15fc5bd5d4b88abd23c4Jeff Sharkeystd::string create_data_media_path(const char* volume_uuid, userid_t userid) {
19741ea424413c0381ef2aa15fc5bd5d4b88abd23c4Jeff Sharkey    return StringPrintf("%s/media/%u", create_data_path(volume_uuid).c_str(), userid);
19894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
19994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
200df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkeystd::string create_data_media_obb_path(const char* volume_uuid, const char* package_name) {
201df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey    return StringPrintf("%s/media/obb/%s", create_data_path(volume_uuid).c_str(), package_name);
202df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey}
203df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey
2043dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkeystd::string create_data_media_package_path(const char* volume_uuid, userid_t userid,
2053dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey        const char* data_type, const char* package_name) {
2063dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey    return StringPrintf("%s/Android/%s/%s", create_data_media_path(volume_uuid, userid).c_str(),
2073dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey            data_type, package_name);
2083dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey}
2093dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey
210379a12b0072b322c7f86e690a8e8a220e500861cJeff Sharkeystd::string create_data_misc_legacy_path(userid_t userid) {
211379a12b0072b322c7f86e690a8e8a220e500861cJeff Sharkey    return StringPrintf("%s/misc/user/%u", create_data_path(nullptr).c_str(), userid);
212379a12b0072b322c7f86e690a8e8a220e500861cJeff Sharkey}
213379a12b0072b322c7f86e690a8e8a220e500861cJeff Sharkey
214114f08107be384a3f9cc954bdec2b6b7619354aeCalin Juravlestd::string create_primary_cur_profile_dir_path(userid_t userid) {
2156a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle    return StringPrintf("%s/cur/%u", android_profiles_dir.path, userid);
2166a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle}
2176a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle
218114f08107be384a3f9cc954bdec2b6b7619354aeCalin Juravlestd::string create_primary_current_profile_package_dir_path(userid_t user,
219114f08107be384a3f9cc954bdec2b6b7619354aeCalin Juravle        const std::string& package_name) {
22076268c56febde9a77183387fbd4baabe6694e6b5Calin Juravle    check_package_name(package_name.c_str());
221114f08107be384a3f9cc954bdec2b6b7619354aeCalin Juravle    return StringPrintf("%s/%s",
222114f08107be384a3f9cc954bdec2b6b7619354aeCalin Juravle            create_primary_cur_profile_dir_path(user).c_str(), package_name.c_str());
223df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey}
224df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey
225114f08107be384a3f9cc954bdec2b6b7619354aeCalin Juravlestd::string create_primary_ref_profile_dir_path() {
226df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey    return StringPrintf("%s/ref", android_profiles_dir.path);
2276a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle}
2286a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle
229114f08107be384a3f9cc954bdec2b6b7619354aeCalin Juravlestd::string create_primary_reference_profile_package_dir_path(const std::string& package_name) {
23076268c56febde9a77183387fbd4baabe6694e6b5Calin Juravle    check_package_name(package_name.c_str());
23176268c56febde9a77183387fbd4baabe6694e6b5Calin Juravle    return StringPrintf("%s/ref/%s", android_profiles_dir.path, package_name.c_str());
2326a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle}
2336a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle
2343dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkeystd::string create_data_dalvik_cache_path() {
2353dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey    return "/data/dalvik-cache";
2363dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey}
2373dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey
238114f08107be384a3f9cc954bdec2b6b7619354aeCalin Juravle// Keep profile paths in sync with ActivityThread and LoadedApk.
239114f08107be384a3f9cc954bdec2b6b7619354aeCalin Juravleconst std::string PROFILE_EXT = ".prof";
240114f08107be384a3f9cc954bdec2b6b7619354aeCalin Juravleconst std::string PRIMARY_PROFILE_NAME = "primary" + PROFILE_EXT;
24190aff26f0135379db19432ae90c40c0831ba5954Jeff Sharkey
242114f08107be384a3f9cc954bdec2b6b7619354aeCalin Juravlestd::string create_current_profile_path(userid_t user, const std::string& location,
243114f08107be384a3f9cc954bdec2b6b7619354aeCalin Juravle        bool is_secondary_dex) {
244114f08107be384a3f9cc954bdec2b6b7619354aeCalin Juravle    if (is_secondary_dex) {
245114f08107be384a3f9cc954bdec2b6b7619354aeCalin Juravle        // Secondary dex profiles are stored next to the dex files using .prof extension.
246114f08107be384a3f9cc954bdec2b6b7619354aeCalin Juravle        return StringPrintf("%s%s", location.c_str(), PROFILE_EXT.c_str());
247114f08107be384a3f9cc954bdec2b6b7619354aeCalin Juravle    } else {
248114f08107be384a3f9cc954bdec2b6b7619354aeCalin Juravle        // Profiles for primary apks are under /data/misc/profiles/cur.
249114f08107be384a3f9cc954bdec2b6b7619354aeCalin Juravle        std::string profile_dir = create_primary_current_profile_package_dir_path(user, location);
250114f08107be384a3f9cc954bdec2b6b7619354aeCalin Juravle        return StringPrintf("%s/%s", profile_dir.c_str(), PRIMARY_PROFILE_NAME.c_str());
251114f08107be384a3f9cc954bdec2b6b7619354aeCalin Juravle    }
252114f08107be384a3f9cc954bdec2b6b7619354aeCalin Juravle}
253114f08107be384a3f9cc954bdec2b6b7619354aeCalin Juravle
254114f08107be384a3f9cc954bdec2b6b7619354aeCalin Juravlestd::string create_reference_profile_path(const std::string& location, bool is_secondary_dex) {
255114f08107be384a3f9cc954bdec2b6b7619354aeCalin Juravle    if (is_secondary_dex) {
256114f08107be384a3f9cc954bdec2b6b7619354aeCalin Juravle        // Secondary dex reference profiles are stored next to the dex files under the oat folder.
257114f08107be384a3f9cc954bdec2b6b7619354aeCalin Juravle        size_t dirIndex = location.rfind('/');
258114f08107be384a3f9cc954bdec2b6b7619354aeCalin Juravle        CHECK(dirIndex != std::string::npos)
259114f08107be384a3f9cc954bdec2b6b7619354aeCalin Juravle                << "Unexpected dir structure for secondary dex " << location;
260114f08107be384a3f9cc954bdec2b6b7619354aeCalin Juravle
261114f08107be384a3f9cc954bdec2b6b7619354aeCalin Juravle        std::string dex_dir = location.substr(0, dirIndex);
262114f08107be384a3f9cc954bdec2b6b7619354aeCalin Juravle        std::string dex_name = location.substr(dirIndex +1);
263114f08107be384a3f9cc954bdec2b6b7619354aeCalin Juravle        return StringPrintf("%s/oat/%s%s",
264114f08107be384a3f9cc954bdec2b6b7619354aeCalin Juravle                dex_dir.c_str(), dex_name.c_str(), PROFILE_EXT.c_str());
265114f08107be384a3f9cc954bdec2b6b7619354aeCalin Juravle    } else {
266114f08107be384a3f9cc954bdec2b6b7619354aeCalin Juravle        // Reference profiles for primary apks are stored in /data/misc/profile/ref.
267114f08107be384a3f9cc954bdec2b6b7619354aeCalin Juravle        std::string profile_dir = create_primary_reference_profile_package_dir_path(location);
268114f08107be384a3f9cc954bdec2b6b7619354aeCalin Juravle        return StringPrintf("%s/%s", profile_dir.c_str(), PRIMARY_PROFILE_NAME.c_str());
269114f08107be384a3f9cc954bdec2b6b7619354aeCalin Juravle    }
27090aff26f0135379db19432ae90c40c0831ba5954Jeff Sharkey}
27190aff26f0135379db19432ae90c40c0831ba5954Jeff Sharkey
272e36372423000a906bafae68844ebc6c42d09335aJeff Sharkeystd::vector<userid_t> get_known_users(const char* volume_uuid) {
273e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey    std::vector<userid_t> users;
274e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey
275e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey    // We always have an owner
276e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey    users.push_back(0);
277e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey
278e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey    std::string path(create_data_path(volume_uuid) + "/" + SECONDARY_USER_PREFIX);
279e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey    DIR* dir = opendir(path.c_str());
280e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey    if (dir == NULL) {
281e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey        // Unable to discover other users, but at least return owner
282e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey        PLOG(ERROR) << "Failed to opendir " << path;
283e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey        return users;
284e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey    }
285e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey
286e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey    struct dirent* ent;
287e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey    while ((ent = readdir(dir))) {
288e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey        if (ent->d_type != DT_DIR) {
289e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey            continue;
290e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey        }
291e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey
292e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey        char* end;
293e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey        userid_t user = strtol(ent->d_name, &end, 10);
294e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey        if (*end == '\0' && user != 0) {
295e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey            LOG(DEBUG) << "Found valid user " << user;
296e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey            users.push_back(user);
297e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey        }
298e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey    }
299e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey    closedir(dir);
300e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey
301e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey    return users;
302e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey}
303e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey
3043dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkeyint calculate_tree_size(const std::string& path, int64_t* size,
305df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey        int32_t include_gid, int32_t exclude_gid, bool exclude_apps) {
3063dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey    FTS *fts;
3073dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey    FTSENT *p;
308df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey    int64_t matchedSize = 0;
3093dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey    char *argv[] = { (char*) path.c_str(), nullptr };
310b26786d647b624498c11405075e5223d1853f30aJeff Sharkey    if (!(fts = fts_open(argv, FTS_PHYSICAL | FTS_NOCHDIR | FTS_XDEV, NULL))) {
3113dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey        if (errno != ENOENT) {
3123dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey            PLOG(ERROR) << "Failed to fts_open " << path;
3133dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey        }
3143dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey        return -1;
3153dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey    }
3163dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey    while ((p = fts_read(fts)) != NULL) {
3173dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey        switch (p->fts_info) {
3183dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey        case FTS_D:
3193dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey        case FTS_DEFAULT:
3203dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey        case FTS_F:
3213dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey        case FTS_SL:
3223dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey        case FTS_SLNONE:
323df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey            int32_t uid = p->fts_statp->st_uid;
324df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey            int32_t gid = p->fts_statp->st_gid;
325df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey            int32_t user_uid = multiuser_get_app_id(uid);
326df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey            int32_t user_gid = multiuser_get_app_id(gid);
327df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey            if (exclude_apps && ((user_uid >= AID_APP_START && user_uid <= AID_APP_END)
328df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey                    || (user_gid >= AID_CACHE_GID_START && user_gid <= AID_CACHE_GID_END)
329df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey                    || (user_gid >= AID_SHARED_GID_START && user_gid <= AID_SHARED_GID_END))) {
330df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey                // Don't traverse inside or measure
331df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey                fts_set(fts, p, FTS_SKIP);
3323dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey                break;
3333dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey            }
334df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey            if (include_gid != -1 && gid != include_gid) {
3353dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey                break;
3363dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey            }
337df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey            if (exclude_gid != -1 && gid == exclude_gid) {
338df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey                break;
339df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey            }
340df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey            matchedSize += (p->fts_statp->st_blocks * 512);
3413dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey            break;
3423dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey        }
3433dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey    }
3443dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey    fts_close(fts);
345df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey#if MEASURE_DEBUG
346df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey    if ((include_gid == -1) && (exclude_gid == -1)) {
347df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey        LOG(DEBUG) << "Measured " << path << " size " << matchedSize;
348df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey    } else {
349df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey        LOG(DEBUG) << "Measured " << path << " size " << matchedSize << "; include " << include_gid
350df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey                << " exclude " << exclude_gid;
351df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey    }
352df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey#endif
353df2d754b38796e0c49c70e0a67f7d383e3079ff2Jeff Sharkey    *size += matchedSize;
3543dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey    return 0;
3553dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey}
3563dfae0c008576c873c4039bb4c2e54a6adf3720aJeff Sharkey
35794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwoodint create_move_path(char path[PKG_PATH_MAX],
35894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    const char* pkgname,
35994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    const char* leaf,
36002d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe    userid_t userid ATTRIBUTE_UNUSED)
36194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood{
36294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    if ((android_data_dir.len + strlen(PRIMARY_USER_PREFIX) + strlen(pkgname) + strlen(leaf) + 1)
36394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            >= PKG_PATH_MAX) {
36494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        return -1;
36594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
36694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
36794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    sprintf(path, "%s%s%s/%s", android_data_dir.path, PRIMARY_USER_PREFIX, pkgname, leaf);
36894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    return 0;
36994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
37094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
37194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood/**
37294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood * Checks whether the package name is valid. Returns -1 on error and
37394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood * 0 on success.
37494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood */
375423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkeybool is_valid_package_name(const std::string& packageName) {
376367ace2f77105f7a09b34a50bc875fd4fa591177Jeff Sharkey    // This logic is borrowed from PackageParser.java
377367ace2f77105f7a09b34a50bc875fd4fa591177Jeff Sharkey    bool hasSep = false;
378367ace2f77105f7a09b34a50bc875fd4fa591177Jeff Sharkey    bool front = true;
379367ace2f77105f7a09b34a50bc875fd4fa591177Jeff Sharkey
380367ace2f77105f7a09b34a50bc875fd4fa591177Jeff Sharkey    auto it = packageName.begin();
381367ace2f77105f7a09b34a50bc875fd4fa591177Jeff Sharkey    for (; it != packageName.end() && *it != '-'; it++) {
382367ace2f77105f7a09b34a50bc875fd4fa591177Jeff Sharkey        char c = *it;
383367ace2f77105f7a09b34a50bc875fd4fa591177Jeff Sharkey        if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) {
384367ace2f77105f7a09b34a50bc875fd4fa591177Jeff Sharkey            front = false;
385367ace2f77105f7a09b34a50bc875fd4fa591177Jeff Sharkey            continue;
386367ace2f77105f7a09b34a50bc875fd4fa591177Jeff Sharkey        }
387367ace2f77105f7a09b34a50bc875fd4fa591177Jeff Sharkey        if (!front) {
388367ace2f77105f7a09b34a50bc875fd4fa591177Jeff Sharkey            if ((c >= '0' && c <= '9') || c == '_') {
389367ace2f77105f7a09b34a50bc875fd4fa591177Jeff Sharkey                continue;
39094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            }
39194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        }
392367ace2f77105f7a09b34a50bc875fd4fa591177Jeff Sharkey        if (c == '.') {
393367ace2f77105f7a09b34a50bc875fd4fa591177Jeff Sharkey            hasSep = true;
394367ace2f77105f7a09b34a50bc875fd4fa591177Jeff Sharkey            front = true;
395367ace2f77105f7a09b34a50bc875fd4fa591177Jeff Sharkey            continue;
396367ace2f77105f7a09b34a50bc875fd4fa591177Jeff Sharkey        }
397367ace2f77105f7a09b34a50bc875fd4fa591177Jeff Sharkey        LOG(WARNING) << "Bad package character " << c << " in " << packageName;
398367ace2f77105f7a09b34a50bc875fd4fa591177Jeff Sharkey        return false;
399367ace2f77105f7a09b34a50bc875fd4fa591177Jeff Sharkey    }
40094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
401ab7ac8d5a04bd3f38b85ce20ae5bb382f2a26585Jeff Sharkey    if (front) {
402367ace2f77105f7a09b34a50bc875fd4fa591177Jeff Sharkey        LOG(WARNING) << "Missing separator in " << packageName;
403367ace2f77105f7a09b34a50bc875fd4fa591177Jeff Sharkey        return false;
40494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
40594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
406367ace2f77105f7a09b34a50bc875fd4fa591177Jeff Sharkey    for (; it != packageName.end(); it++) {
407367ace2f77105f7a09b34a50bc875fd4fa591177Jeff Sharkey        char c = *it;
408367ace2f77105f7a09b34a50bc875fd4fa591177Jeff Sharkey        if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) continue;
409367ace2f77105f7a09b34a50bc875fd4fa591177Jeff Sharkey        if ((c >= '0' && c <= '9') || c == '_' || c == '-' || c == '=') continue;
410367ace2f77105f7a09b34a50bc875fd4fa591177Jeff Sharkey        LOG(WARNING) << "Bad suffix character " << c << " in " << packageName;
411367ace2f77105f7a09b34a50bc875fd4fa591177Jeff Sharkey        return false;
41294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
41394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
414423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey    return true;
41594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
41694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
4173aee2c5c749dc2589f001b26fae1ec958ec89524Narayan Kamathstatic int _delete_dir_contents(DIR *d,
4183aee2c5c749dc2589f001b26fae1ec958ec89524Narayan Kamath                                int (*exclusion_predicate)(const char *name, const int is_dir))
41994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood{
42094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    int result = 0;
42194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    struct dirent *de;
42294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    int dfd;
42394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
42494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    dfd = dirfd(d);
42594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
42694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    if (dfd < 0) return -1;
42794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
42894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    while ((de = readdir(d))) {
42994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        const char *name = de->d_name;
43094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
4313aee2c5c749dc2589f001b26fae1ec958ec89524Narayan Kamath            /* check using the exclusion predicate, if provided */
4323aee2c5c749dc2589f001b26fae1ec958ec89524Narayan Kamath        if (exclusion_predicate && exclusion_predicate(name, (de->d_type == DT_DIR))) {
4333aee2c5c749dc2589f001b26fae1ec958ec89524Narayan Kamath            continue;
4343aee2c5c749dc2589f001b26fae1ec958ec89524Narayan Kamath        }
43594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
43694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        if (de->d_type == DT_DIR) {
43799d9fb15b4a1eb534261ae81b2cf25aec4447bd0Chih-Hung Hsieh            int subfd;
43894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            DIR *subdir;
43994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
44094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                /* always skip "." and ".." */
44194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            if (name[0] == '.') {
44294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                if (name[1] == 0) continue;
44394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                if ((name[1] == '.') && (name[2] == 0)) continue;
44494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            }
44594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
4468b7acacc93930df9fa9e1ebea4a4394195b2332eNick Kralevich            subfd = openat(dfd, name, O_RDONLY | O_DIRECTORY | O_NOFOLLOW | O_CLOEXEC);
44794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            if (subfd < 0) {
44894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                ALOGE("Couldn't openat %s: %s\n", name, strerror(errno));
44994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                result = -1;
45094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                continue;
45194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            }
45294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            subdir = fdopendir(subfd);
45394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            if (subdir == NULL) {
45494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                ALOGE("Couldn't fdopendir %s: %s\n", name, strerror(errno));
45594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                close(subfd);
45694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                result = -1;
45794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                continue;
45894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            }
4593aee2c5c749dc2589f001b26fae1ec958ec89524Narayan Kamath            if (_delete_dir_contents(subdir, exclusion_predicate)) {
46094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                result = -1;
46194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            }
46294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            closedir(subdir);
46394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            if (unlinkat(dfd, name, AT_REMOVEDIR) < 0) {
46494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                ALOGE("Couldn't unlinkat %s: %s\n", name, strerror(errno));
46594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                result = -1;
46694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            }
46794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        } else {
46894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            if (unlinkat(dfd, name, 0) < 0) {
46994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                ALOGE("Couldn't unlinkat %s: %s\n", name, strerror(errno));
47094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                result = -1;
47194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            }
47294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        }
47394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
47494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
47594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    return result;
47694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
47794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
478b06f98aabc5381fd6366526d9b31b5d0345481b6Calin Juravleint delete_dir_contents(const std::string& pathname, bool ignore_if_missing) {
479b06f98aabc5381fd6366526d9b31b5d0345481b6Calin Juravle    return delete_dir_contents(pathname.c_str(), 0, NULL, ignore_if_missing);
480ebf728fd43ab5c7d11a1f9e5fdc775d6740fae0aJeff Sharkey}
481ebf728fd43ab5c7d11a1f9e5fdc775d6740fae0aJeff Sharkey
482b06f98aabc5381fd6366526d9b31b5d0345481b6Calin Juravleint delete_dir_contents_and_dir(const std::string& pathname, bool ignore_if_missing) {
483b06f98aabc5381fd6366526d9b31b5d0345481b6Calin Juravle    return delete_dir_contents(pathname.c_str(), 1, NULL, ignore_if_missing);
484ebf728fd43ab5c7d11a1f9e5fdc775d6740fae0aJeff Sharkey}
485ebf728fd43ab5c7d11a1f9e5fdc775d6740fae0aJeff Sharkey
48694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwoodint delete_dir_contents(const char *pathname,
48794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood                        int also_delete_dir,
488b06f98aabc5381fd6366526d9b31b5d0345481b6Calin Juravle                        int (*exclusion_predicate)(const char*, const int),
489b06f98aabc5381fd6366526d9b31b5d0345481b6Calin Juravle                        bool ignore_if_missing)
49094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood{
49194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    int res = 0;
49294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    DIR *d;
49394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
49494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    d = opendir(pathname);
49594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    if (d == NULL) {
496b06f98aabc5381fd6366526d9b31b5d0345481b6Calin Juravle        if (ignore_if_missing && (errno == ENOENT)) {
497b06f98aabc5381fd6366526d9b31b5d0345481b6Calin Juravle            return 0;
498b06f98aabc5381fd6366526d9b31b5d0345481b6Calin Juravle        }
49994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        ALOGE("Couldn't opendir %s: %s\n", pathname, strerror(errno));
50094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        return -errno;
50194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
5023aee2c5c749dc2589f001b26fae1ec958ec89524Narayan Kamath    res = _delete_dir_contents(d, exclusion_predicate);
50394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    closedir(d);
50494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    if (also_delete_dir) {
50594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        if (rmdir(pathname)) {
50694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            ALOGE("Couldn't rmdir %s: %s\n", pathname, strerror(errno));
50794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood            res = -1;
50894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        }
50994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
51094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    return res;
51194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
51294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
51394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwoodint delete_dir_contents_fd(int dfd, const char *name)
51494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood{
51594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    int fd, res;
51694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    DIR *d;
51794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
5188b7acacc93930df9fa9e1ebea4a4394195b2332eNick Kralevich    fd = openat(dfd, name, O_RDONLY | O_DIRECTORY | O_NOFOLLOW | O_CLOEXEC);
51994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    if (fd < 0) {
52094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        ALOGE("Couldn't openat %s: %s\n", name, strerror(errno));
52194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        return -1;
52294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
52394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    d = fdopendir(fd);
52494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    if (d == NULL) {
52594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        ALOGE("Couldn't fdopendir %s: %s\n", name, strerror(errno));
52694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        close(fd);
52794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        return -1;
52894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
52994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    res = _delete_dir_contents(d, 0);
53094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    closedir(d);
53194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    return res;
53294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
53394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
53460fd3feecab4336d964ca8e31c7c3220e1afb558Robin Leestatic int _copy_owner_permissions(int srcfd, int dstfd)
53560fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee{
53660fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee    struct stat st;
53760fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee    if (fstat(srcfd, &st) != 0) {
53860fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee        return -1;
53960fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee    }
54060fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee    if (fchmod(dstfd, st.st_mode) != 0) {
54160fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee        return -1;
54260fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee    }
54360fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee    return 0;
54460fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee}
54560fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee
54660fd3feecab4336d964ca8e31c7c3220e1afb558Robin Leestatic int _copy_dir_files(int sdfd, int ddfd, uid_t owner, gid_t group)
54760fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee{
54860fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee    int result = 0;
54960fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee    if (_copy_owner_permissions(sdfd, ddfd) != 0) {
55060fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee        ALOGE("_copy_dir_files failed to copy dir permissions\n");
55160fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee    }
55260fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee    if (fchown(ddfd, owner, group) != 0) {
55360fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee        ALOGE("_copy_dir_files failed to change dir owner\n");
55460fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee    }
55560fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee
55660fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee    DIR *ds = fdopendir(sdfd);
55760fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee    if (ds == NULL) {
55860fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee        ALOGE("Couldn't fdopendir: %s\n", strerror(errno));
55960fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee        return -1;
56060fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee    }
56160fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee    struct dirent *de;
56260fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee    while ((de = readdir(ds))) {
56360fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee        if (de->d_type != DT_REG) {
56460fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee            continue;
56560fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee        }
56660fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee
56760fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee        const char *name = de->d_name;
56860fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee        int fsfd = openat(sdfd, name, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
56960fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee        int fdfd = openat(ddfd, name, O_WRONLY | O_NOFOLLOW | O_CLOEXEC | O_CREAT, 0600);
57060fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee        if (fsfd == -1 || fdfd == -1) {
57160fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee            ALOGW("Couldn't copy %s: %s\n", name, strerror(errno));
57260fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee        } else {
57360fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee            if (_copy_owner_permissions(fsfd, fdfd) != 0) {
57460fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee                ALOGE("Failed to change file permissions\n");
57560fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee            }
57660fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee            if (fchown(fdfd, owner, group) != 0) {
57760fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee                ALOGE("Failed to change file owner\n");
57860fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee            }
57960fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee
58060fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee            char buf[8192];
58160fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee            ssize_t size;
58260fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee            while ((size = read(fsfd, buf, sizeof(buf))) > 0) {
58360fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee                write(fdfd, buf, size);
58460fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee            }
58560fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee            if (size < 0) {
58660fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee                ALOGW("Couldn't copy %s: %s\n", name, strerror(errno));
58760fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee                result = -1;
58860fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee            }
58960fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee        }
59060fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee        close(fdfd);
59160fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee        close(fsfd);
59260fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee    }
59360fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee
59460fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee    return result;
59560fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee}
59660fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee
59760fd3feecab4336d964ca8e31c7c3220e1afb558Robin Leeint copy_dir_files(const char *srcname,
59860fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee                   const char *dstname,
59960fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee                   uid_t owner,
60060fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee                   uid_t group)
60160fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee{
60260fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee    int res = 0;
60360fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee    DIR *ds = NULL;
60460fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee    DIR *dd = NULL;
60560fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee
60660fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee    ds = opendir(srcname);
60760fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee    if (ds == NULL) {
60860fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee        ALOGE("Couldn't opendir %s: %s\n", srcname, strerror(errno));
60960fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee        return -errno;
61060fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee    }
61160fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee
61260fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee    mkdir(dstname, 0600);
61360fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee    dd = opendir(dstname);
61460fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee    if (dd == NULL) {
61560fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee        ALOGE("Couldn't opendir %s: %s\n", dstname, strerror(errno));
61660fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee        closedir(ds);
61760fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee        return -errno;
61860fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee    }
61960fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee
62060fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee    int sdfd = dirfd(ds);
62160fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee    int ddfd = dirfd(dd);
62260fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee    if (sdfd != -1 && ddfd != -1) {
62360fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee        res = _copy_dir_files(sdfd, ddfd, owner, group);
62460fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee    } else {
62560fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee        res = -errno;
62660fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee    }
62760fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee    closedir(dd);
62860fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee    closedir(ds);
62960fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee    return res;
63060fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee}
63160fd3feecab4336d964ca8e31c7c3220e1afb558Robin Lee
632a836c472f017f09cf16fa68176df461a4958d22aJeff Sharkeyint64_t data_disk_free(const std::string& data_path) {
633ed909ae8db2f44ce7fe7003c6fee457f13669702Jeff Sharkey    struct statvfs sfs;
634ed909ae8db2f44ce7fe7003c6fee457f13669702Jeff Sharkey    if (statvfs(data_path.c_str(), &sfs) == 0) {
635a836c472f017f09cf16fa68176df461a4958d22aJeff Sharkey        return sfs.f_bavail * sfs.f_frsize;
63694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    } else {
637ed909ae8db2f44ce7fe7003c6fee457f13669702Jeff Sharkey        PLOG(ERROR) << "Couldn't statvfs " << data_path;
63894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood        return -1;
63994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood    }
64094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood}
64194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood
6429a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkeyint get_path_inode(const std::string& path, ino_t *inode) {
6439a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey    struct stat buf;
6449a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey    memset(&buf, 0, sizeof(buf));
6459a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey    if (stat(path.c_str(), &buf) != 0) {
6469a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey        PLOG(WARNING) << "Failed to stat " << path;
6479a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey        return -1;
6489a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey    } else {
6499a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey        *inode = buf.st_ino;
6509a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey        return 0;
6519a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey    }
6529a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey}
6539a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey
6549a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey/**
6559a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey * Write the inode of a specific child file into the given xattr on the
6569a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey * parent directory. This allows you to find the child later, even if its
6579a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey * name is encrypted.
6589a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey */
6599a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkeyint write_path_inode(const std::string& parent, const char* name, const char* inode_xattr) {
6609a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey    ino_t inode = 0;
6619a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey    uint64_t inode_raw = 0;
6629a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey    auto path = StringPrintf("%s/%s", parent.c_str(), name);
6639a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey
6649a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey    if (get_path_inode(path, &inode) != 0) {
6659a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey        // Path probably doesn't exist yet; ignore
6669a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey        return 0;
6679a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey    }
6689a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey
6699a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey    // Check to see if already set correctly
6709a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey    if (getxattr(parent.c_str(), inode_xattr, &inode_raw, sizeof(inode_raw)) == sizeof(inode_raw)) {
6719a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey        if (inode_raw == inode) {
6729a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey            // Already set correctly; skip writing
6739a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey            return 0;
6749a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey        } else {
6759a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey            PLOG(WARNING) << "Mismatched inode value; found " << inode
6769a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey                    << " on disk but marked value was " << inode_raw << "; overwriting";
6779a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey        }
6789a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey    }
6799a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey
6809a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey    inode_raw = inode;
6814ed6507cfb4f0fae8567b42037e74a07f7dd28baJeff Sharkey    if (setxattr(parent.c_str(), inode_xattr, &inode_raw, sizeof(inode_raw), 0) != 0 && errno != EOPNOTSUPP) {
6829a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey        PLOG(ERROR) << "Failed to write xattr " << inode_xattr << " at " << parent;
6839a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey        return -1;
6849a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey    } else {
6859a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey        return 0;
6869a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey    }
6879a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey}
6889a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey
6899a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey/**
6909a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey * Read the inode of a specific child file from the given xattr on the
6919a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey * parent directory. Returns a currently valid path for that child, which
6929a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey * might have an encrypted name.
6939a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey */
6949a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkeystd::string read_path_inode(const std::string& parent, const char* name, const char* inode_xattr) {
6959a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey    ino_t inode = 0;
6969a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey    uint64_t inode_raw = 0;
6979a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey    auto fallback = StringPrintf("%s/%s", parent.c_str(), name);
6989a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey
6999a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey    // Lookup the inode value written earlier
7009a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey    if (getxattr(parent.c_str(), inode_xattr, &inode_raw, sizeof(inode_raw)) == sizeof(inode_raw)) {
7019a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey        inode = inode_raw;
7029a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey    }
7039a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey
7049a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey    // For testing purposes, rely on the inode when defined; this could be
7059a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey    // optimized to use access() in the future.
7069a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey    if (inode != 0) {
7079a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey        DIR* dir = opendir(parent.c_str());
7089a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey        if (dir == nullptr) {
7099a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey            PLOG(ERROR) << "Failed to opendir " << parent;
7109a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey            return fallback;
7119a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey        }
7129a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey
7139a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey        struct dirent* ent;
7149a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey        while ((ent = readdir(dir))) {
7159a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey            if (ent->d_ino == inode) {
7169a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey                auto resolved = StringPrintf("%s/%s", parent.c_str(), ent->d_name);
7179a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey#if DEBUG_XATTRS
7189a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey                if (resolved != fallback) {
7199a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey                    LOG(DEBUG) << "Resolved path " << resolved << " for inode " << inode
720