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.
17 #include "MoveStorage.h"
19 #include "VolumeManager.h"
21 #include <android-base/logging.h>
22 #include <android-base/properties.h>
23 #include <android-base/stringprintf.h>
24 #include <hardware_legacy/power.h>
25 #include <private/android_filesystem_config.h>
32 #define CONSTRAIN(amount, low, high) \
33 ((amount) < (low) ? (low) : ((amount) > (high) ? (high) : (amount)))
35 static const char* kPropBlockingExec = "persist.sys.blocking_exec";
37 using android::base::StringPrintf;
42 // TODO: keep in sync with PackageManager
43 static const int kMoveSucceeded = -100;
44 static const int kMoveFailedInternalError = -6;
46 static const char* kCpPath = "/system/bin/cp";
47 static const char* kRmPath = "/system/bin/rm";
49 static const char* kWakeLock = "MoveTask";
51 static void notifyProgress(int progress,
52 const android::sp<android::os::IVoldTaskListener>& listener) {
54 android::os::PersistableBundle extras;
55 listener->onStatus(progress, extras);
59 static status_t pushBackContents(const std::string& path, std::vector<std::string>& cmd,
61 DIR* dir = opendir(path.c_str());
67 while ((ent = readdir(dir)) != NULL) {
68 if ((!strcmp(ent->d_name, ".")) || (!strcmp(ent->d_name, ".."))) {
72 cmd.push_back(StringPrintf("%s/%s/*", path.c_str(), ent->d_name));
74 cmd.push_back(StringPrintf("%s/%s", path.c_str(), ent->d_name));
79 return found ? OK : -1;
82 static status_t execRm(const std::string& path, int startProgress, int stepProgress,
83 const android::sp<android::os::IVoldTaskListener>& listener) {
84 notifyProgress(startProgress, listener);
86 uint64_t expectedBytes = GetTreeBytes(path);
87 uint64_t startFreeBytes = GetFreeBytes(path);
89 std::vector<std::string> cmd;
90 cmd.push_back(kRmPath);
91 cmd.push_back("-f"); /* force: remove without confirmation, no error if it doesn't exist */
92 cmd.push_back("-R"); /* recursive: remove directory contents */
93 if (pushBackContents(path, cmd, true) != OK) {
94 LOG(WARNING) << "No contents in " << path;
98 if (android::base::GetBoolProperty(kPropBlockingExec, false)) {
99 return ForkExecvp(cmd);
102 pid_t pid = ForkExecvpAsync(cmd);
103 if (pid == -1) return -1;
107 if (waitpid(pid, &status, WNOHANG) == pid) {
108 if (WIFEXITED(status)) {
109 LOG(DEBUG) << "Finished rm with status " << WEXITSTATUS(status);
110 return (WEXITSTATUS(status) == 0) ? OK : -1;
117 uint64_t deltaFreeBytes = GetFreeBytes(path) - startFreeBytes;
120 CONSTRAIN((int)((deltaFreeBytes * stepProgress) / expectedBytes), 0, stepProgress),
126 static status_t execCp(const std::string& fromPath, const std::string& toPath, int startProgress,
128 const android::sp<android::os::IVoldTaskListener>& listener) {
129 notifyProgress(startProgress, listener);
131 uint64_t expectedBytes = GetTreeBytes(fromPath);
132 uint64_t startFreeBytes = GetFreeBytes(toPath);
134 if (expectedBytes > startFreeBytes) {
135 LOG(ERROR) << "Data size " << expectedBytes << " is too large to fit in free space "
140 std::vector<std::string> cmd;
141 cmd.push_back(kCpPath);
142 cmd.push_back("-p"); /* preserve timestamps, ownership, and permissions */
143 cmd.push_back("-R"); /* recurse into subdirectories (DEST must be a directory) */
144 cmd.push_back("-P"); /* Do not follow symlinks [default] */
145 cmd.push_back("-d"); /* don't dereference symlinks */
146 if (pushBackContents(fromPath, cmd, false) != OK) {
147 LOG(WARNING) << "No contents in " << fromPath;
150 cmd.push_back(toPath.c_str());
152 if (android::base::GetBoolProperty(kPropBlockingExec, false)) {
153 return ForkExecvp(cmd);
156 pid_t pid = ForkExecvpAsync(cmd);
157 if (pid == -1) return -1;
161 if (waitpid(pid, &status, WNOHANG) == pid) {
162 if (WIFEXITED(status)) {
163 LOG(DEBUG) << "Finished cp with status " << WEXITSTATUS(status);
164 return (WEXITSTATUS(status) == 0) ? OK : -1;
171 uint64_t deltaFreeBytes = startFreeBytes - GetFreeBytes(toPath);
174 CONSTRAIN((int)((deltaFreeBytes * stepProgress) / expectedBytes), 0, stepProgress),
180 static void bringOffline(const std::shared_ptr<VolumeBase>& vol) {
182 vol->setSilent(true);
184 vol->setMountFlags(0);
188 static void bringOnline(const std::shared_ptr<VolumeBase>& vol) {
190 vol->setSilent(false);
194 static status_t moveStorageInternal(const std::shared_ptr<VolumeBase>& from,
195 const std::shared_ptr<VolumeBase>& to,
196 const android::sp<android::os::IVoldTaskListener>& listener) {
197 std::string fromPath;
200 // TODO: add support for public volumes
201 if (from->getType() != VolumeBase::Type::kEmulated) goto fail;
202 if (to->getType() != VolumeBase::Type::kEmulated) goto fail;
204 // Step 1: tear down volumes and mount silently without making
205 // visible to userspace apps
207 std::lock_guard<std::mutex> lock(VolumeManager::Instance()->getLock());
212 fromPath = from->getInternalPath();
213 toPath = to->getInternalPath();
215 // Step 2: clean up any stale data
216 if (execRm(toPath, 10, 10, listener) != OK) {
220 // Step 3: perform actual copy
221 if (execCp(fromPath, toPath, 20, 60, listener) != OK) {
225 // NOTE: MountService watches for this magic value to know
226 // that move was successful
227 notifyProgress(82, listener);
229 std::lock_guard<std::mutex> lock(VolumeManager::Instance()->getLock());
234 // Step 4: clean up old data
235 if (execRm(fromPath, 85, 15, listener) != OK) {
239 notifyProgress(kMoveSucceeded, listener);
243 // if we failed to copy the data we should not leave it laying around
244 // in target location. Do not check return value, we can not do any
246 execRm(toPath, 80, 1, listener);
250 std::lock_guard<std::mutex> lock(VolumeManager::Instance()->getLock());
255 notifyProgress(kMoveFailedInternalError, listener);
259 void MoveStorage(const std::shared_ptr<VolumeBase>& from, const std::shared_ptr<VolumeBase>& to,
260 const android::sp<android::os::IVoldTaskListener>& listener) {
261 acquire_wake_lock(PARTIAL_WAKE_LOCK, kWakeLock);
263 android::os::PersistableBundle extras;
264 status_t res = moveStorageInternal(from, to, listener);
266 listener->onFinished(res, extras);
269 release_wake_lock(kWakeLock);
273 } // namespace android