2 * Copyright (C) 2015 The Android Open Source Project
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
19 #include "VolumeManager.h"
20 #include "ResponseCode.h"
22 #include <base/stringprintf.h>
23 #include <base/logging.h>
24 #include <private/android_filesystem_config.h>
25 #include <hardware_legacy/power.h>
30 #define CONSTRAIN(amount, low, high) (amount < low ? low : (amount > high ? high : amount))
32 using android::base::StringPrintf;
37 // TODO: keep in sync with PackageManager
38 static const int kMoveSucceeded = -100;
39 static const int kMoveFailedInternalError = -6;
41 static const char* kCpPath = "/system/bin/cp";
42 static const char* kRmPath = "/system/bin/rm";
44 static const char* kWakeLock = "MoveTask";
46 MoveTask::MoveTask(const std::shared_ptr<VolumeBase>& from,
47 const std::shared_ptr<VolumeBase>& to) :
48 mFrom(from), mTo(to) {
51 MoveTask::~MoveTask() {
54 void MoveTask::start() {
55 mThread = std::thread(&MoveTask::run, this);
58 static void notifyProgress(int progress) {
59 VolumeManager::Instance()->getBroadcaster()->sendBroadcast(ResponseCode::MoveStatus,
60 StringPrintf("%d", progress).c_str(), false);
63 static status_t pushBackContents(const std::string& path, std::vector<std::string>& cmd) {
64 DIR* dir = opendir(path.c_str());
70 while ((ent = readdir(dir)) != NULL) {
71 if ((!strcmp(ent->d_name, ".")) || (!strcmp(ent->d_name, ".."))) {
74 cmd.push_back(StringPrintf("%s/%s", path.c_str(), ent->d_name));
78 return found ? OK : -1;
81 static status_t execRm(const std::string& path, int startProgress, int stepProgress) {
82 notifyProgress(startProgress);
84 uint64_t expectedBytes = GetTreeBytes(path);
85 uint64_t startFreeBytes = GetFreeBytes(path);
87 std::vector<std::string> cmd;
88 cmd.push_back(kRmPath);
89 cmd.push_back("-f"); /* force: remove without confirmation, no error if it doesn't exist */
90 cmd.push_back("-R"); /* recursive: remove directory contents */
91 if (pushBackContents(path, cmd) != OK) {
92 LOG(WARNING) << "No contents in " << path;
96 pid_t pid = ForkExecvpAsync(cmd);
97 if (pid == -1) return -1;
101 if (waitpid(pid, &status, WNOHANG) == pid) {
102 if (WIFEXITED(status)) {
103 LOG(DEBUG) << "Finished rm with status " << WEXITSTATUS(status);
104 return (WEXITSTATUS(status) == 0) ? OK : -1;
111 uint64_t deltaFreeBytes = GetFreeBytes(path) - startFreeBytes;
112 notifyProgress(startProgress + CONSTRAIN((int)
113 ((deltaFreeBytes * stepProgress) / expectedBytes), 0, stepProgress));
118 static status_t execCp(const std::string& fromPath, const std::string& toPath,
119 int startProgress, int stepProgress) {
120 notifyProgress(startProgress);
122 uint64_t expectedBytes = GetTreeBytes(fromPath);
123 uint64_t startFreeBytes = GetFreeBytes(toPath);
125 std::vector<std::string> cmd;
126 cmd.push_back(kCpPath);
127 cmd.push_back("-p"); /* preserve timestamps, ownership, and permissions */
128 cmd.push_back("-R"); /* recurse into subdirectories (DEST must be a directory) */
129 cmd.push_back("-P"); /* Do not follow symlinks [default] */
130 cmd.push_back("-d"); /* don't dereference symlinks */
131 if (pushBackContents(fromPath, cmd) != OK) {
132 LOG(WARNING) << "No contents in " << fromPath;
135 cmd.push_back(toPath.c_str());
137 pid_t pid = ForkExecvpAsync(cmd);
138 if (pid == -1) return -1;
142 if (waitpid(pid, &status, WNOHANG) == pid) {
143 if (WIFEXITED(status)) {
144 LOG(DEBUG) << "Finished cp with status " << WEXITSTATUS(status);
145 return (WEXITSTATUS(status) == 0) ? OK : -1;
152 uint64_t deltaFreeBytes = startFreeBytes - GetFreeBytes(toPath);
153 notifyProgress(startProgress + CONSTRAIN((int)
154 ((deltaFreeBytes * stepProgress) / expectedBytes), 0, stepProgress));
159 static void bringOffline(const std::shared_ptr<VolumeBase>& vol) {
161 vol->setSilent(true);
163 vol->setMountFlags(0);
167 static void bringOnline(const std::shared_ptr<VolumeBase>& vol) {
169 vol->setSilent(false);
173 void MoveTask::run() {
174 acquire_wake_lock(PARTIAL_WAKE_LOCK, kWakeLock);
176 std::string fromPath;
179 // TODO: add support for public volumes
180 if (mFrom->getType() != VolumeBase::Type::kEmulated) goto fail;
181 if (mTo->getType() != VolumeBase::Type::kEmulated) goto fail;
183 // Step 1: tear down volumes and mount silently without making
184 // visible to userspace apps
188 fromPath = mFrom->getInternalPath();
189 toPath = mTo->getInternalPath();
191 // Step 2: clean up any stale data
192 if (execRm(toPath, 10, 10) != OK) {
196 // Step 3: perform actual copy
197 if (execCp(fromPath, toPath, 20, 60) != OK) {
201 // NOTE: MountService watches for this magic value to know
202 // that move was successful
207 // Step 4: clean up old data
208 if (execRm(fromPath, 85, 15) != OK) {
212 notifyProgress(kMoveSucceeded);
213 release_wake_lock(kWakeLock);
218 notifyProgress(kMoveFailedInternalError);
219 release_wake_lock(kWakeLock);
224 } // namespace android