blob: 4268ed455fad98a4c49dd66441a30f06a993f47e [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"
Jeff Sharkey1d6fbcc2015-04-24 16:00:03 -070020
Elliott Hughes7e128fb2015-12-04 15:50:53 -080021#include <android-base/stringprintf.h>
22#include <android-base/logging.h>
Jeff Sharkey1d6fbcc2015-04-24 16:00:03 -070023#include <private/android_filesystem_config.h>
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -070024#include <hardware_legacy/power.h>
Jeff Sharkey1d6fbcc2015-04-24 16:00:03 -070025
26#include <dirent.h>
27#include <sys/wait.h>
28
Chih-Hung Hsiehcc5d5802016-05-11 15:05:05 -070029#define CONSTRAIN(amount, low, high) ((amount) < (low) ? (low) : ((amount) > (high) ? (high) : (amount)))
Jeff Sharkey1d6fbcc2015-04-24 16:00:03 -070030
Jeff Sharkeyfce701b2016-07-29 15:52:41 -060031#define EXEC_BLOCKING 0
32
Jeff Sharkey1d6fbcc2015-04-24 16:00:03 -070033using android::base::StringPrintf;
34
35namespace android {
36namespace vold {
37
38// TODO: keep in sync with PackageManager
39static const int kMoveSucceeded = -100;
40static const int kMoveFailedInternalError = -6;
41
42static const char* kCpPath = "/system/bin/cp";
43static const char* kRmPath = "/system/bin/rm";
44
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -070045static const char* kWakeLock = "MoveTask";
46
Jeff Sharkey52f7a912017-09-15 12:57:44 -060047MoveTask::MoveTask(const std::shared_ptr<VolumeBase>& from, const std::shared_ptr<VolumeBase>& to,
48 const android::sp<android::os::IVoldTaskListener>& listener) :
49 mFrom(from), mTo(to), mListener(listener) {
Jeff Sharkey1d6fbcc2015-04-24 16:00:03 -070050}
51
52MoveTask::~MoveTask() {
53}
54
55void MoveTask::start() {
56 mThread = std::thread(&MoveTask::run, this);
57}
58
Jeff Sharkey52f7a912017-09-15 12:57:44 -060059void MoveTask::notifyProgress(int progress) {
60 if (mListener) {
61 android::os::PersistableBundle extras;
62 mListener->onStatus(progress, extras);
63 }
Jeff Sharkey1d6fbcc2015-04-24 16:00:03 -070064}
65
Jeff Sharkey46bb69f2017-06-21 13:52:23 -060066static status_t pushBackContents(const std::string& path, std::vector<std::string>& cmd,
67 bool addWildcard) {
Jeff Sharkey1d6fbcc2015-04-24 16:00:03 -070068 DIR* dir = opendir(path.c_str());
69 if (dir == NULL) {
70 return -1;
71 }
72 bool found = false;
73 struct dirent* ent;
74 while ((ent = readdir(dir)) != NULL) {
75 if ((!strcmp(ent->d_name, ".")) || (!strcmp(ent->d_name, ".."))) {
76 continue;
77 }
Jeff Sharkey46bb69f2017-06-21 13:52:23 -060078 if (addWildcard) {
79 cmd.push_back(StringPrintf("%s/%s/*", path.c_str(), ent->d_name));
80 } else {
81 cmd.push_back(StringPrintf("%s/%s", path.c_str(), ent->d_name));
82 }
Jeff Sharkey1d6fbcc2015-04-24 16:00:03 -070083 found = true;
84 }
85 closedir(dir);
86 return found ? OK : -1;
87}
88
Jeff Sharkey52f7a912017-09-15 12:57:44 -060089status_t MoveTask::execRm(const std::string& path, int startProgress, int stepProgress) {
Jeff Sharkey1d6fbcc2015-04-24 16:00:03 -070090 notifyProgress(startProgress);
91
92 uint64_t expectedBytes = GetTreeBytes(path);
93 uint64_t startFreeBytes = GetFreeBytes(path);
94
95 std::vector<std::string> cmd;
96 cmd.push_back(kRmPath);
97 cmd.push_back("-f"); /* force: remove without confirmation, no error if it doesn't exist */
98 cmd.push_back("-R"); /* recursive: remove directory contents */
Jeff Sharkey46bb69f2017-06-21 13:52:23 -060099 if (pushBackContents(path, cmd, true) != OK) {
Jeff Sharkey1d6fbcc2015-04-24 16:00:03 -0700100 LOG(WARNING) << "No contents in " << path;
101 return OK;
102 }
103
Jeff Sharkeyfce701b2016-07-29 15:52:41 -0600104#if EXEC_BLOCKING
105 return ForkExecvp(cmd);
106#else
Jeff Sharkey1d6fbcc2015-04-24 16:00:03 -0700107 pid_t pid = ForkExecvpAsync(cmd);
108 if (pid == -1) return -1;
109
110 int status;
111 while (true) {
112 if (waitpid(pid, &status, WNOHANG) == pid) {
113 if (WIFEXITED(status)) {
114 LOG(DEBUG) << "Finished rm with status " << WEXITSTATUS(status);
115 return (WEXITSTATUS(status) == 0) ? OK : -1;
116 } else {
117 break;
118 }
119 }
120
121 sleep(1);
122 uint64_t deltaFreeBytes = GetFreeBytes(path) - startFreeBytes;
123 notifyProgress(startProgress + CONSTRAIN((int)
124 ((deltaFreeBytes * stepProgress) / expectedBytes), 0, stepProgress));
125 }
126 return -1;
Jeff Sharkeyfce701b2016-07-29 15:52:41 -0600127#endif
Jeff Sharkey1d6fbcc2015-04-24 16:00:03 -0700128}
129
Jeff Sharkey52f7a912017-09-15 12:57:44 -0600130status_t MoveTask::execCp(const std::string& fromPath, const std::string& toPath,
Jeff Sharkey1d6fbcc2015-04-24 16:00:03 -0700131 int startProgress, int stepProgress) {
132 notifyProgress(startProgress);
133
134 uint64_t expectedBytes = GetTreeBytes(fromPath);
135 uint64_t startFreeBytes = GetFreeBytes(toPath);
136
Jeff Sharkeya0220a52017-04-03 17:11:45 -0600137 if (expectedBytes > startFreeBytes) {
138 LOG(ERROR) << "Data size " << expectedBytes << " is too large to fit in free space "
139 << startFreeBytes;
140 return -1;
141 }
142
Jeff Sharkey1d6fbcc2015-04-24 16:00:03 -0700143 std::vector<std::string> cmd;
144 cmd.push_back(kCpPath);
145 cmd.push_back("-p"); /* preserve timestamps, ownership, and permissions */
146 cmd.push_back("-R"); /* recurse into subdirectories (DEST must be a directory) */
147 cmd.push_back("-P"); /* Do not follow symlinks [default] */
148 cmd.push_back("-d"); /* don't dereference symlinks */
Jeff Sharkey46bb69f2017-06-21 13:52:23 -0600149 if (pushBackContents(fromPath, cmd, false) != OK) {
Jeff Sharkey1d6fbcc2015-04-24 16:00:03 -0700150 LOG(WARNING) << "No contents in " << fromPath;
151 return OK;
152 }
153 cmd.push_back(toPath.c_str());
154
Jeff Sharkeyfce701b2016-07-29 15:52:41 -0600155#if EXEC_BLOCKING
156 return ForkExecvp(cmd);
157#else
Jeff Sharkey1d6fbcc2015-04-24 16:00:03 -0700158 pid_t pid = ForkExecvpAsync(cmd);
159 if (pid == -1) return -1;
160
161 int status;
162 while (true) {
163 if (waitpid(pid, &status, WNOHANG) == pid) {
164 if (WIFEXITED(status)) {
165 LOG(DEBUG) << "Finished cp with status " << WEXITSTATUS(status);
166 return (WEXITSTATUS(status) == 0) ? OK : -1;
167 } else {
168 break;
169 }
170 }
171
172 sleep(1);
173 uint64_t deltaFreeBytes = startFreeBytes - GetFreeBytes(toPath);
174 notifyProgress(startProgress + CONSTRAIN((int)
175 ((deltaFreeBytes * stepProgress) / expectedBytes), 0, stepProgress));
176 }
177 return -1;
Jeff Sharkeyfce701b2016-07-29 15:52:41 -0600178#endif
Jeff Sharkey1d6fbcc2015-04-24 16:00:03 -0700179}
180
181static void bringOffline(const std::shared_ptr<VolumeBase>& vol) {
182 vol->destroy();
183 vol->setSilent(true);
184 vol->create();
185 vol->setMountFlags(0);
186 vol->mount();
187}
188
189static void bringOnline(const std::shared_ptr<VolumeBase>& vol) {
190 vol->destroy();
191 vol->setSilent(false);
192 vol->create();
193}
194
195void MoveTask::run() {
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700196 acquire_wake_lock(PARTIAL_WAKE_LOCK, kWakeLock);
197
Jeff Sharkey1d6fbcc2015-04-24 16:00:03 -0700198 std::string fromPath;
199 std::string toPath;
200
201 // TODO: add support for public volumes
202 if (mFrom->getType() != VolumeBase::Type::kEmulated) goto fail;
203 if (mTo->getType() != VolumeBase::Type::kEmulated) goto fail;
204
205 // Step 1: tear down volumes and mount silently without making
206 // visible to userspace apps
Henrik Baard7f52bca2015-11-26 12:05:13 +0100207 {
208 std::lock_guard<std::mutex> lock(VolumeManager::Instance()->getLock());
209 bringOffline(mFrom);
210 bringOffline(mTo);
211 }
Jeff Sharkey1d6fbcc2015-04-24 16:00:03 -0700212
213 fromPath = mFrom->getInternalPath();
214 toPath = mTo->getInternalPath();
215
216 // Step 2: clean up any stale data
217 if (execRm(toPath, 10, 10) != OK) {
218 goto fail;
219 }
220
221 // Step 3: perform actual copy
222 if (execCp(fromPath, toPath, 20, 60) != OK) {
Henrik Baard77f156d2015-12-17 13:58:42 +0100223 goto copy_fail;
Jeff Sharkey1d6fbcc2015-04-24 16:00:03 -0700224 }
225
226 // NOTE: MountService watches for this magic value to know
227 // that move was successful
228 notifyProgress(82);
Henrik Baard7f52bca2015-11-26 12:05:13 +0100229 {
230 std::lock_guard<std::mutex> lock(VolumeManager::Instance()->getLock());
231 bringOnline(mFrom);
232 bringOnline(mTo);
233 }
Jeff Sharkey1d6fbcc2015-04-24 16:00:03 -0700234
235 // Step 4: clean up old data
236 if (execRm(fromPath, 85, 15) != OK) {
237 goto fail;
238 }
239
240 notifyProgress(kMoveSucceeded);
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700241 release_wake_lock(kWakeLock);
Jeff Sharkey1d6fbcc2015-04-24 16:00:03 -0700242 return;
Henrik Baard77f156d2015-12-17 13:58:42 +0100243
244copy_fail:
245 // if we failed to copy the data we should not leave it laying around
246 // in target location. Do not check return value, we can not do any
247 // useful anyway.
248 execRm(toPath, 80, 1);
Jeff Sharkey1d6fbcc2015-04-24 16:00:03 -0700249fail:
Henrik Baard7f52bca2015-11-26 12:05:13 +0100250 {
251 std::lock_guard<std::mutex> lock(VolumeManager::Instance()->getLock());
252 bringOnline(mFrom);
253 bringOnline(mTo);
254 }
Jeff Sharkey1d6fbcc2015-04-24 16:00:03 -0700255 notifyProgress(kMoveFailedInternalError);
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700256 release_wake_lock(kWakeLock);
Jeff Sharkey1d6fbcc2015-04-24 16:00:03 -0700257 return;
258}
259
260} // namespace vold
261} // namespace android