2015-04-25 01:00:03 +02:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2015 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2017-10-18 00:06:32 +02:00
|
|
|
#include "MoveStorage.h"
|
2015-04-25 01:00:03 +02:00
|
|
|
#include "Utils.h"
|
|
|
|
#include "VolumeManager.h"
|
|
|
|
|
2015-12-05 00:50:53 +01:00
|
|
|
#include <android-base/logging.h>
|
2018-01-05 00:46:34 +01:00
|
|
|
#include <android-base/properties.h>
|
|
|
|
#include <android-base/stringprintf.h>
|
|
|
|
#include <private/android_filesystem_config.h>
|
2019-06-21 21:21:48 +02:00
|
|
|
#include <wakelock/wakelock.h>
|
2015-04-25 01:00:03 +02:00
|
|
|
|
2017-10-18 00:06:32 +02:00
|
|
|
#include <thread>
|
|
|
|
|
2015-04-25 01:00:03 +02:00
|
|
|
#include <dirent.h>
|
|
|
|
#include <sys/wait.h>
|
|
|
|
|
2018-09-18 22:30:21 +02:00
|
|
|
#define CONSTRAIN(amount, low, high) \
|
|
|
|
((amount) < (low) ? (low) : ((amount) > (high) ? (high) : (amount)))
|
2015-04-25 01:00:03 +02:00
|
|
|
|
2018-01-05 00:46:34 +01:00
|
|
|
static const char* kPropBlockingExec = "persist.sys.blocking_exec";
|
2016-07-29 23:52:41 +02:00
|
|
|
|
2015-04-25 01:00:03 +02:00
|
|
|
using android::base::StringPrintf;
|
|
|
|
|
|
|
|
namespace android {
|
|
|
|
namespace vold {
|
|
|
|
|
|
|
|
// TODO: keep in sync with PackageManager
|
|
|
|
static const int kMoveSucceeded = -100;
|
|
|
|
static const int kMoveFailedInternalError = -6;
|
|
|
|
|
|
|
|
static const char* kCpPath = "/system/bin/cp";
|
|
|
|
static const char* kRmPath = "/system/bin/rm";
|
|
|
|
|
2015-06-26 23:02:09 +02:00
|
|
|
static const char* kWakeLock = "MoveTask";
|
|
|
|
|
2017-10-18 00:06:32 +02:00
|
|
|
static void notifyProgress(int progress,
|
2018-09-18 22:30:21 +02:00
|
|
|
const android::sp<android::os::IVoldTaskListener>& listener) {
|
2017-10-18 00:06:32 +02:00
|
|
|
if (listener) {
|
2017-09-15 20:57:44 +02:00
|
|
|
android::os::PersistableBundle extras;
|
2017-10-18 00:06:32 +02:00
|
|
|
listener->onStatus(progress, extras);
|
2017-09-15 20:57:44 +02:00
|
|
|
}
|
2015-04-25 01:00:03 +02:00
|
|
|
}
|
|
|
|
|
2019-03-18 18:26:47 +01:00
|
|
|
static bool pushBackContents(const std::string& path, std::vector<std::string>& cmd,
|
|
|
|
int searchLevels) {
|
|
|
|
if (searchLevels == 0) {
|
|
|
|
cmd.emplace_back(path);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
auto dirp = std::unique_ptr<DIR, int (*)(DIR*)>(opendir(path.c_str()), closedir);
|
|
|
|
if (!dirp) {
|
|
|
|
PLOG(ERROR) << "Unable to open directory: " << path;
|
|
|
|
return false;
|
2015-04-25 01:00:03 +02:00
|
|
|
}
|
|
|
|
bool found = false;
|
|
|
|
struct dirent* ent;
|
2019-03-18 18:26:47 +01:00
|
|
|
while ((ent = readdir(dirp.get())) != NULL) {
|
2020-11-03 00:31:56 +01:00
|
|
|
if (IsDotOrDotDot(*ent)) continue;
|
2019-03-18 18:26:47 +01:00
|
|
|
auto subdir = path + "/" + ent->d_name;
|
|
|
|
found |= pushBackContents(subdir, cmd, searchLevels - 1);
|
2015-04-25 01:00:03 +02:00
|
|
|
}
|
2019-03-18 18:26:47 +01:00
|
|
|
return found;
|
2015-04-25 01:00:03 +02:00
|
|
|
}
|
|
|
|
|
2017-10-18 00:06:32 +02:00
|
|
|
static status_t execRm(const std::string& path, int startProgress, int stepProgress,
|
2018-09-18 22:30:21 +02:00
|
|
|
const android::sp<android::os::IVoldTaskListener>& listener) {
|
2017-10-18 00:06:32 +02:00
|
|
|
notifyProgress(startProgress, listener);
|
2015-04-25 01:00:03 +02:00
|
|
|
|
|
|
|
uint64_t expectedBytes = GetTreeBytes(path);
|
|
|
|
uint64_t startFreeBytes = GetFreeBytes(path);
|
|
|
|
|
|
|
|
std::vector<std::string> cmd;
|
|
|
|
cmd.push_back(kRmPath);
|
|
|
|
cmd.push_back("-f"); /* force: remove without confirmation, no error if it doesn't exist */
|
|
|
|
cmd.push_back("-R"); /* recursive: remove directory contents */
|
2019-03-18 18:26:47 +01:00
|
|
|
if (!pushBackContents(path, cmd, 2)) {
|
2015-04-25 01:00:03 +02:00
|
|
|
LOG(WARNING) << "No contents in " << path;
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
2018-01-05 00:46:34 +01:00
|
|
|
if (android::base::GetBoolProperty(kPropBlockingExec, false)) {
|
|
|
|
return ForkExecvp(cmd);
|
|
|
|
}
|
|
|
|
|
2015-04-25 01:00:03 +02:00
|
|
|
pid_t pid = ForkExecvpAsync(cmd);
|
|
|
|
if (pid == -1) return -1;
|
|
|
|
|
|
|
|
int status;
|
|
|
|
while (true) {
|
|
|
|
if (waitpid(pid, &status, WNOHANG) == pid) {
|
|
|
|
if (WIFEXITED(status)) {
|
|
|
|
LOG(DEBUG) << "Finished rm with status " << WEXITSTATUS(status);
|
|
|
|
return (WEXITSTATUS(status) == 0) ? OK : -1;
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sleep(1);
|
|
|
|
uint64_t deltaFreeBytes = GetFreeBytes(path) - startFreeBytes;
|
2018-09-18 22:30:21 +02:00
|
|
|
notifyProgress(
|
|
|
|
startProgress +
|
|
|
|
CONSTRAIN((int)((deltaFreeBytes * stepProgress) / expectedBytes), 0, stepProgress),
|
|
|
|
listener);
|
2015-04-25 01:00:03 +02:00
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2017-10-18 00:06:32 +02:00
|
|
|
static status_t execCp(const std::string& fromPath, const std::string& toPath, int startProgress,
|
2018-09-18 22:30:21 +02:00
|
|
|
int stepProgress,
|
|
|
|
const android::sp<android::os::IVoldTaskListener>& listener) {
|
2017-10-18 00:06:32 +02:00
|
|
|
notifyProgress(startProgress, listener);
|
2015-04-25 01:00:03 +02:00
|
|
|
|
|
|
|
uint64_t expectedBytes = GetTreeBytes(fromPath);
|
|
|
|
uint64_t startFreeBytes = GetFreeBytes(toPath);
|
|
|
|
|
2017-04-04 01:11:45 +02:00
|
|
|
if (expectedBytes > startFreeBytes) {
|
|
|
|
LOG(ERROR) << "Data size " << expectedBytes << " is too large to fit in free space "
|
2018-09-18 22:30:21 +02:00
|
|
|
<< startFreeBytes;
|
2017-04-04 01:11:45 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2015-04-25 01:00:03 +02:00
|
|
|
std::vector<std::string> cmd;
|
|
|
|
cmd.push_back(kCpPath);
|
|
|
|
cmd.push_back("-p"); /* preserve timestamps, ownership, and permissions */
|
|
|
|
cmd.push_back("-R"); /* recurse into subdirectories (DEST must be a directory) */
|
|
|
|
cmd.push_back("-P"); /* Do not follow symlinks [default] */
|
|
|
|
cmd.push_back("-d"); /* don't dereference symlinks */
|
2019-03-18 18:26:47 +01:00
|
|
|
if (!pushBackContents(fromPath, cmd, 1)) {
|
2015-04-25 01:00:03 +02:00
|
|
|
LOG(WARNING) << "No contents in " << fromPath;
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
cmd.push_back(toPath.c_str());
|
|
|
|
|
2018-01-05 00:46:34 +01:00
|
|
|
if (android::base::GetBoolProperty(kPropBlockingExec, false)) {
|
|
|
|
return ForkExecvp(cmd);
|
|
|
|
}
|
|
|
|
|
2015-04-25 01:00:03 +02:00
|
|
|
pid_t pid = ForkExecvpAsync(cmd);
|
|
|
|
if (pid == -1) return -1;
|
|
|
|
|
|
|
|
int status;
|
|
|
|
while (true) {
|
|
|
|
if (waitpid(pid, &status, WNOHANG) == pid) {
|
|
|
|
if (WIFEXITED(status)) {
|
|
|
|
LOG(DEBUG) << "Finished cp with status " << WEXITSTATUS(status);
|
|
|
|
return (WEXITSTATUS(status) == 0) ? OK : -1;
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sleep(1);
|
|
|
|
uint64_t deltaFreeBytes = startFreeBytes - GetFreeBytes(toPath);
|
2018-09-18 22:30:21 +02:00
|
|
|
notifyProgress(
|
|
|
|
startProgress +
|
|
|
|
CONSTRAIN((int)((deltaFreeBytes * stepProgress) / expectedBytes), 0, stepProgress),
|
|
|
|
listener);
|
2015-04-25 01:00:03 +02:00
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void bringOffline(const std::shared_ptr<VolumeBase>& vol) {
|
|
|
|
vol->destroy();
|
|
|
|
vol->setSilent(true);
|
|
|
|
vol->create();
|
|
|
|
vol->setMountFlags(0);
|
|
|
|
vol->mount();
|
|
|
|
}
|
|
|
|
|
|
|
|
static void bringOnline(const std::shared_ptr<VolumeBase>& vol) {
|
|
|
|
vol->destroy();
|
|
|
|
vol->setSilent(false);
|
|
|
|
vol->create();
|
|
|
|
}
|
|
|
|
|
2017-10-18 00:06:32 +02:00
|
|
|
static status_t moveStorageInternal(const std::shared_ptr<VolumeBase>& from,
|
2018-09-18 22:30:21 +02:00
|
|
|
const std::shared_ptr<VolumeBase>& to,
|
|
|
|
const android::sp<android::os::IVoldTaskListener>& listener) {
|
2015-04-25 01:00:03 +02:00
|
|
|
std::string fromPath;
|
|
|
|
std::string toPath;
|
|
|
|
|
|
|
|
// TODO: add support for public volumes
|
2017-10-18 00:06:32 +02:00
|
|
|
if (from->getType() != VolumeBase::Type::kEmulated) goto fail;
|
|
|
|
if (to->getType() != VolumeBase::Type::kEmulated) goto fail;
|
2015-04-25 01:00:03 +02:00
|
|
|
|
|
|
|
// Step 1: tear down volumes and mount silently without making
|
|
|
|
// visible to userspace apps
|
2015-11-26 12:05:13 +01:00
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> lock(VolumeManager::Instance()->getLock());
|
2017-10-18 00:06:32 +02:00
|
|
|
bringOffline(from);
|
|
|
|
bringOffline(to);
|
2015-11-26 12:05:13 +01:00
|
|
|
}
|
2015-04-25 01:00:03 +02:00
|
|
|
|
2017-10-18 00:06:32 +02:00
|
|
|
fromPath = from->getInternalPath();
|
|
|
|
toPath = to->getInternalPath();
|
2015-04-25 01:00:03 +02:00
|
|
|
|
|
|
|
// Step 2: clean up any stale data
|
2017-10-18 00:06:32 +02:00
|
|
|
if (execRm(toPath, 10, 10, listener) != OK) {
|
2015-04-25 01:00:03 +02:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Step 3: perform actual copy
|
2017-10-18 00:06:32 +02:00
|
|
|
if (execCp(fromPath, toPath, 20, 60, listener) != OK) {
|
2015-12-17 13:58:42 +01:00
|
|
|
goto copy_fail;
|
2015-04-25 01:00:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// NOTE: MountService watches for this magic value to know
|
|
|
|
// that move was successful
|
2017-10-18 00:06:32 +02:00
|
|
|
notifyProgress(82, listener);
|
2015-11-26 12:05:13 +01:00
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> lock(VolumeManager::Instance()->getLock());
|
2017-10-18 00:06:32 +02:00
|
|
|
bringOnline(from);
|
|
|
|
bringOnline(to);
|
2015-11-26 12:05:13 +01:00
|
|
|
}
|
2015-04-25 01:00:03 +02:00
|
|
|
|
|
|
|
// Step 4: clean up old data
|
2017-10-18 00:06:32 +02:00
|
|
|
if (execRm(fromPath, 85, 15, listener) != OK) {
|
2015-04-25 01:00:03 +02:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
2017-10-18 00:06:32 +02:00
|
|
|
notifyProgress(kMoveSucceeded, listener);
|
|
|
|
return OK;
|
2015-12-17 13:58:42 +01:00
|
|
|
|
|
|
|
copy_fail:
|
|
|
|
// if we failed to copy the data we should not leave it laying around
|
|
|
|
// in target location. Do not check return value, we can not do any
|
|
|
|
// useful anyway.
|
2017-10-18 00:06:32 +02:00
|
|
|
execRm(toPath, 80, 1, listener);
|
2015-04-25 01:00:03 +02:00
|
|
|
fail:
|
2018-09-18 22:30:21 +02:00
|
|
|
// clang-format off
|
2015-11-26 12:05:13 +01:00
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> lock(VolumeManager::Instance()->getLock());
|
2017-10-18 00:06:32 +02:00
|
|
|
bringOnline(from);
|
|
|
|
bringOnline(to);
|
|
|
|
}
|
2018-09-18 22:30:21 +02:00
|
|
|
// clang-format on
|
2017-10-18 00:06:32 +02:00
|
|
|
notifyProgress(kMoveFailedInternalError, listener);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
void MoveStorage(const std::shared_ptr<VolumeBase>& from, const std::shared_ptr<VolumeBase>& to,
|
2018-09-18 22:30:21 +02:00
|
|
|
const android::sp<android::os::IVoldTaskListener>& listener) {
|
2019-06-21 21:21:48 +02:00
|
|
|
android::wakelock::WakeLock wl{kWakeLock};
|
2017-10-18 00:06:32 +02:00
|
|
|
|
|
|
|
android::os::PersistableBundle extras;
|
|
|
|
status_t res = moveStorageInternal(from, to, listener);
|
|
|
|
if (listener) {
|
|
|
|
listener->onFinished(res, extras);
|
2015-11-26 12:05:13 +01:00
|
|
|
}
|
2015-04-25 01:00:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace vold
|
|
|
|
} // namespace android
|