blob: c565752312245dc6fe80f49b5193bd655d86911e [file] [log] [blame]
Jeff Sharkey1d6fbcc2015-04-24 16:00:03 -07001/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
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
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
15 */
16
17#include "MoveTask.h"
18#include "Utils.h"
19#include "VolumeManager.h"
20#include "ResponseCode.h"
21
Elliott Hughes7e128fb2015-12-04 15:50:53 -080022#include <android-base/stringprintf.h>
23#include <android-base/logging.h>
Jeff Sharkey1d6fbcc2015-04-24 16:00:03 -070024#include <private/android_filesystem_config.h>
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -070025#include <hardware_legacy/power.h>
Jeff Sharkey1d6fbcc2015-04-24 16:00:03 -070026
27#include <dirent.h>
28#include <sys/wait.h>
29
Chih-Hung Hsiehcc5d5802016-05-11 15:05:05 -070030#define CONSTRAIN(amount, low, high) ((amount) < (low) ? (low) : ((amount) > (high) ? (high) : (amount)))
Jeff Sharkey1d6fbcc2015-04-24 16:00:03 -070031
Jeff Sharkeyfce701b2016-07-29 15:52:41 -060032#define EXEC_BLOCKING 0
33
Jeff Sharkey1d6fbcc2015-04-24 16:00:03 -070034using android::base::StringPrintf;
35
36namespace android {
37namespace vold {
38
39// TODO: keep in sync with PackageManager
40static const int kMoveSucceeded = -100;
41static const int kMoveFailedInternalError = -6;
42
43static const char* kCpPath = "/system/bin/cp";
44static const char* kRmPath = "/system/bin/rm";
45
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -070046static const char* kWakeLock = "MoveTask";
47
Jeff Sharkey1d6fbcc2015-04-24 16:00:03 -070048MoveTask::MoveTask(const std::shared_ptr<VolumeBase>& from,
49 const std::shared_ptr<VolumeBase>& to) :
50 mFrom(from), mTo(to) {
51}
52
53MoveTask::~MoveTask() {
54}
55
56void MoveTask::start() {
57 mThread = std::thread(&MoveTask::run, this);
58}
59
60static void notifyProgress(int progress) {
61 VolumeManager::Instance()->getBroadcaster()->sendBroadcast(ResponseCode::MoveStatus,
62 StringPrintf("%d", progress).c_str(), false);
63}
64
Jeff Sharkey46bb69f2017-06-21 13:52:23 -060065static status_t pushBackContents(const std::string& path, std::vector<std::string>& cmd,
66 bool addWildcard) {
Jeff Sharkey1d6fbcc2015-04-24 16:00:03 -070067 DIR* dir = opendir(path.c_str());
68 if (dir == NULL) {
69 return -1;
70 }
71 bool found = false;
72 struct dirent* ent;
73 while ((ent = readdir(dir)) != NULL) {
74 if ((!strcmp(ent->d_name, ".")) || (!strcmp(ent->d_name, ".."))) {
75 continue;
76 }
Jeff Sharkey46bb69f2017-06-21 13:52:23 -060077 if (addWildcard) {
78 cmd.push_back(StringPrintf("%s/%s/*", path.c_str(), ent->d_name));
79 } else {
80 cmd.push_back(StringPrintf("%s/%s", path.c_str(), ent->d_name));
81 }
Jeff Sharkey1d6fbcc2015-04-24 16:00:03 -070082 found = true;
83 }
84 closedir(dir);
85 return found ? OK : -1;
86}
87
88static status_t execRm(const std::string& path, int startProgress, int stepProgress) {
89 notifyProgress(startProgress);
90
91 uint64_t expectedBytes = GetTreeBytes(path);
92 uint64_t startFreeBytes = GetFreeBytes(path);
93
94 std::vector<std::string> cmd;
95 cmd.push_back(kRmPath);
96 cmd.push_back("-f"); /* force: remove without confirmation, no error if it doesn't exist */
97 cmd.push_back("-R"); /* recursive: remove directory contents */
Jeff Sharkey46bb69f2017-06-21 13:52:23 -060098 if (pushBackContents(path, cmd, true) != OK) {
Jeff Sharkey1d6fbcc2015-04-24 16:00:03 -070099 LOG(WARNING) << "No contents in " << path;
100 return OK;
101 }
102
Jeff Sharkeyfce701b2016-07-29 15:52:41 -0600103#if EXEC_BLOCKING
104 return ForkExecvp(cmd);
105#else
Jeff Sharkey1d6fbcc2015-04-24 16:00:03 -0700106 pid_t pid = ForkExecvpAsync(cmd);
107 if (pid == -1) return -1;
108
109 int status;
110 while (true) {
111 if (waitpid(pid, &status, WNOHANG) == pid) {
112 if (WIFEXITED(status)) {
113 LOG(DEBUG) << "Finished rm with status " << WEXITSTATUS(status);
114 return (WEXITSTATUS(status) == 0) ? OK : -1;
115 } else {
116 break;
117 }
118 }
119
120 sleep(1);
121 uint64_t deltaFreeBytes = GetFreeBytes(path) - startFreeBytes;
122 notifyProgress(startProgress + CONSTRAIN((int)
123 ((deltaFreeBytes * stepProgress) / expectedBytes), 0, stepProgress));
124 }
125 return -1;
Jeff Sharkeyfce701b2016-07-29 15:52:41 -0600126#endif
Jeff Sharkey1d6fbcc2015-04-24 16:00:03 -0700127}
128
129static status_t execCp(const std::string& fromPath, const std::string& toPath,
130 int startProgress, int stepProgress) {
131 notifyProgress(startProgress);
132
133 uint64_t expectedBytes = GetTreeBytes(fromPath);
134 uint64_t startFreeBytes = GetFreeBytes(toPath);
135
Jeff Sharkeya0220a52017-04-03 17:11:45 -0600136 if (expectedBytes > startFreeBytes) {
137 LOG(ERROR) << "Data size " << expectedBytes << " is too large to fit in free space "
138 << startFreeBytes;
139 return -1;
140 }
141
Jeff Sharkey1d6fbcc2015-04-24 16:00:03 -0700142 std::vector<std::string> cmd;
143 cmd.push_back(kCpPath);
144 cmd.push_back("-p"); /* preserve timestamps, ownership, and permissions */
145 cmd.push_back("-R"); /* recurse into subdirectories (DEST must be a directory) */
146 cmd.push_back("-P"); /* Do not follow symlinks [default] */
147 cmd.push_back("-d"); /* don't dereference symlinks */
Jeff Sharkey46bb69f2017-06-21 13:52:23 -0600148 if (pushBackContents(fromPath, cmd, false) != OK) {
Jeff Sharkey1d6fbcc2015-04-24 16:00:03 -0700149 LOG(WARNING) << "No contents in " << fromPath;
150 return OK;
151 }
152 cmd.push_back(toPath.c_str());
153
Jeff Sharkeyfce701b2016-07-29 15:52:41 -0600154#if EXEC_BLOCKING
155 return ForkExecvp(cmd);
156#else
Jeff Sharkey1d6fbcc2015-04-24 16:00:03 -0700157 pid_t pid = ForkExecvpAsync(cmd);
158 if (pid == -1) return -1;
159
160 int status;
161 while (true) {
162 if (waitpid(pid, &status, WNOHANG) == pid) {
163 if (WIFEXITED(status)) {
164 LOG(DEBUG) << "Finished cp with status " << WEXITSTATUS(status);
165 return (WEXITSTATUS(status) == 0) ? OK : -1;
166 } else {
167 break;
168 }
169 }
170
171 sleep(1);
172 uint64_t deltaFreeBytes = startFreeBytes - GetFreeBytes(toPath);
173 notifyProgress(startProgress + CONSTRAIN((int)
174 ((deltaFreeBytes * stepProgress) / expectedBytes), 0, stepProgress));
175 }
176 return -1;
Jeff Sharkeyfce701b2016-07-29 15:52:41 -0600177#endif
Jeff Sharkey1d6fbcc2015-04-24 16:00:03 -0700178}
179
180static void bringOffline(const std::shared_ptr<VolumeBase>& vol) {
181 vol->destroy();
182 vol->setSilent(true);
183 vol->create();
184 vol->setMountFlags(0);
185 vol->mount();
186}
187
188static void bringOnline(const std::shared_ptr<VolumeBase>& vol) {
189 vol->destroy();
190 vol->setSilent(false);
191 vol->create();
192}
193
194void MoveTask::run() {
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700195 acquire_wake_lock(PARTIAL_WAKE_LOCK, kWakeLock);
196
Jeff Sharkey1d6fbcc2015-04-24 16:00:03 -0700197 std::string fromPath;
198 std::string toPath;
199
200 // TODO: add support for public volumes
201 if (mFrom->getType() != VolumeBase::Type::kEmulated) goto fail;
202 if (mTo->getType() != VolumeBase::Type::kEmulated) goto fail;
203
204 // Step 1: tear down volumes and mount silently without making
205 // visible to userspace apps
Henrik Baard7f52bca2015-11-26 12:05:13 +0100206 {
207 std::lock_guard<std::mutex> lock(VolumeManager::Instance()->getLock());
208 bringOffline(mFrom);
209 bringOffline(mTo);
210 }
Jeff Sharkey1d6fbcc2015-04-24 16:00:03 -0700211
212 fromPath = mFrom->getInternalPath();
213 toPath = mTo->getInternalPath();
214
215 // Step 2: clean up any stale data
216 if (execRm(toPath, 10, 10) != OK) {
217 goto fail;
218 }
219
220 // Step 3: perform actual copy
221 if (execCp(fromPath, toPath, 20, 60) != OK) {
Henrik Baard77f156d2015-12-17 13:58:42 +0100222 goto copy_fail;
Jeff Sharkey1d6fbcc2015-04-24 16:00:03 -0700223 }
224
225 // NOTE: MountService watches for this magic value to know
226 // that move was successful
227 notifyProgress(82);
Henrik Baard7f52bca2015-11-26 12:05:13 +0100228 {
229 std::lock_guard<std::mutex> lock(VolumeManager::Instance()->getLock());
230 bringOnline(mFrom);
231 bringOnline(mTo);
232 }
Jeff Sharkey1d6fbcc2015-04-24 16:00:03 -0700233
234 // Step 4: clean up old data
235 if (execRm(fromPath, 85, 15) != OK) {
236 goto fail;
237 }
238
239 notifyProgress(kMoveSucceeded);
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700240 release_wake_lock(kWakeLock);
Jeff Sharkey1d6fbcc2015-04-24 16:00:03 -0700241 return;
Henrik Baard77f156d2015-12-17 13:58:42 +0100242
243copy_fail:
244 // if we failed to copy the data we should not leave it laying around
245 // in target location. Do not check return value, we can not do any
246 // useful anyway.
247 execRm(toPath, 80, 1);
Jeff Sharkey1d6fbcc2015-04-24 16:00:03 -0700248fail:
Henrik Baard7f52bca2015-11-26 12:05:13 +0100249 {
250 std::lock_guard<std::mutex> lock(VolumeManager::Instance()->getLock());
251 bringOnline(mFrom);
252 bringOnline(mTo);
253 }
Jeff Sharkey1d6fbcc2015-04-24 16:00:03 -0700254 notifyProgress(kMoveFailedInternalError);
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700255 release_wake_lock(kWakeLock);
Jeff Sharkey1d6fbcc2015-04-24 16:00:03 -0700256 return;
257}
258
259} // namespace vold
260} // namespace android