blob: 10cd54948414167dc55baf975d207c39bb42fd79 [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
22#include <base/stringprintf.h>
23#include <base/logging.h>
24#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
30#define CONSTRAIN(amount, low, high) (amount < low ? low : (amount > high ? high : amount))
31
32using android::base::StringPrintf;
33
34namespace android {
35namespace vold {
36
37// TODO: keep in sync with PackageManager
38static const int kMoveSucceeded = -100;
39static const int kMoveFailedInternalError = -6;
40
41static const char* kCpPath = "/system/bin/cp";
42static const char* kRmPath = "/system/bin/rm";
43
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -070044static const char* kWakeLock = "MoveTask";
45
Jeff Sharkey1d6fbcc2015-04-24 16:00:03 -070046MoveTask::MoveTask(const std::shared_ptr<VolumeBase>& from,
47 const std::shared_ptr<VolumeBase>& to) :
48 mFrom(from), mTo(to) {
49}
50
51MoveTask::~MoveTask() {
52}
53
54void MoveTask::start() {
55 mThread = std::thread(&MoveTask::run, this);
56}
57
58static void notifyProgress(int progress) {
59 VolumeManager::Instance()->getBroadcaster()->sendBroadcast(ResponseCode::MoveStatus,
60 StringPrintf("%d", progress).c_str(), false);
61}
62
63static status_t pushBackContents(const std::string& path, std::vector<std::string>& cmd) {
64 DIR* dir = opendir(path.c_str());
65 if (dir == NULL) {
66 return -1;
67 }
68 bool found = false;
69 struct dirent* ent;
70 while ((ent = readdir(dir)) != NULL) {
71 if ((!strcmp(ent->d_name, ".")) || (!strcmp(ent->d_name, ".."))) {
72 continue;
73 }
74 cmd.push_back(StringPrintf("%s/%s", path.c_str(), ent->d_name));
75 found = true;
76 }
77 closedir(dir);
78 return found ? OK : -1;
79}
80
81static status_t execRm(const std::string& path, int startProgress, int stepProgress) {
82 notifyProgress(startProgress);
83
84 uint64_t expectedBytes = GetTreeBytes(path);
85 uint64_t startFreeBytes = GetFreeBytes(path);
86
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;
93 return OK;
94 }
95
96 pid_t pid = ForkExecvpAsync(cmd);
97 if (pid == -1) return -1;
98
99 int status;
100 while (true) {
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;
105 } else {
106 break;
107 }
108 }
109
110 sleep(1);
111 uint64_t deltaFreeBytes = GetFreeBytes(path) - startFreeBytes;
112 notifyProgress(startProgress + CONSTRAIN((int)
113 ((deltaFreeBytes * stepProgress) / expectedBytes), 0, stepProgress));
114 }
115 return -1;
116}
117
118static status_t execCp(const std::string& fromPath, const std::string& toPath,
119 int startProgress, int stepProgress) {
120 notifyProgress(startProgress);
121
122 uint64_t expectedBytes = GetTreeBytes(fromPath);
123 uint64_t startFreeBytes = GetFreeBytes(toPath);
124
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;
133 return OK;
134 }
135 cmd.push_back(toPath.c_str());
136
137 pid_t pid = ForkExecvpAsync(cmd);
138 if (pid == -1) return -1;
139
140 int status;
141 while (true) {
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;
146 } else {
147 break;
148 }
149 }
150
151 sleep(1);
152 uint64_t deltaFreeBytes = startFreeBytes - GetFreeBytes(toPath);
153 notifyProgress(startProgress + CONSTRAIN((int)
154 ((deltaFreeBytes * stepProgress) / expectedBytes), 0, stepProgress));
155 }
156 return -1;
157}
158
159static void bringOffline(const std::shared_ptr<VolumeBase>& vol) {
160 vol->destroy();
161 vol->setSilent(true);
162 vol->create();
163 vol->setMountFlags(0);
164 vol->mount();
165}
166
167static void bringOnline(const std::shared_ptr<VolumeBase>& vol) {
168 vol->destroy();
169 vol->setSilent(false);
170 vol->create();
171}
172
173void MoveTask::run() {
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700174 acquire_wake_lock(PARTIAL_WAKE_LOCK, kWakeLock);
175
Jeff Sharkey1d6fbcc2015-04-24 16:00:03 -0700176 std::string fromPath;
177 std::string toPath;
178
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;
182
183 // Step 1: tear down volumes and mount silently without making
184 // visible to userspace apps
185 bringOffline(mFrom);
186 bringOffline(mTo);
187
188 fromPath = mFrom->getInternalPath();
189 toPath = mTo->getInternalPath();
190
191 // Step 2: clean up any stale data
192 if (execRm(toPath, 10, 10) != OK) {
193 goto fail;
194 }
195
196 // Step 3: perform actual copy
197 if (execCp(fromPath, toPath, 20, 60) != OK) {
198 goto fail;
199 }
200
201 // NOTE: MountService watches for this magic value to know
202 // that move was successful
203 notifyProgress(82);
204 bringOnline(mFrom);
205 bringOnline(mTo);
206
207 // Step 4: clean up old data
208 if (execRm(fromPath, 85, 15) != OK) {
209 goto fail;
210 }
211
212 notifyProgress(kMoveSucceeded);
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700213 release_wake_lock(kWakeLock);
Jeff Sharkey1d6fbcc2015-04-24 16:00:03 -0700214 return;
215fail:
216 bringOnline(mFrom);
217 bringOnline(mTo);
218 notifyProgress(kMoveFailedInternalError);
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700219 release_wake_lock(kWakeLock);
Jeff Sharkey1d6fbcc2015-04-24 16:00:03 -0700220 return;
221}
222
223} // namespace vold
224} // namespace android