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) ((amount) < (low) ? (low) : ((amount) > (high) ? (high) : (amount)))
34 static const char* kPropBlockingExec = "persist.sys.blocking_exec";
36 using android::base::StringPrintf;
41 // TODO: keep in sync with PackageManager
42 static const int kMoveSucceeded = -100;
43 static const int kMoveFailedInternalError = -6;
45 static const char* kCpPath = "/system/bin/cp";
46 static const char* kRmPath = "/system/bin/rm";
48 static const char* kWakeLock = "MoveTask";
50 static void notifyProgress(int progress,
51 const android::sp<android::os::IVoldTaskListener>& listener) {
53 android::os::PersistableBundle extras;
54 listener->onStatus(progress, extras);
58 static status_t pushBackContents(const std::string& path, std::vector<std::string>& cmd,
60 DIR* dir = opendir(path.c_str());
66 while ((ent = readdir(dir)) != NULL) {
67 if ((!strcmp(ent->d_name, ".")) || (!strcmp(ent->d_name, ".."))) {
71 cmd.push_back(StringPrintf("%s/%s/*", path.c_str(), ent->d_name));
73 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 const android::sp<android::os::IVoldTaskListener>& listener) {
83 notifyProgress(startProgress, listener);
85 uint64_t expectedBytes = GetTreeBytes(path);
86 uint64_t startFreeBytes = GetFreeBytes(path);
88 std::vector<std::string> cmd;
89 cmd.push_back(kRmPath);
90 cmd.push_back("-f"); /* force: remove without confirmation, no error if it doesn't exist */
91 cmd.push_back("-R"); /* recursive: remove directory contents */
92 if (pushBackContents(path, cmd, true) != OK) {
93 LOG(WARNING) << "No contents in " << path;
97 if (android::base::GetBoolProperty(kPropBlockingExec, false)) {
98 return ForkExecvp(cmd);
101 pid_t pid = ForkExecvpAsync(cmd);
102 if (pid == -1) return -1;
106 if (waitpid(pid, &status, WNOHANG) == pid) {
107 if (WIFEXITED(status)) {
108 LOG(DEBUG) << "Finished rm with status " << WEXITSTATUS(status);
109 return (WEXITSTATUS(status) == 0) ? OK : -1;
116 uint64_t deltaFreeBytes = GetFreeBytes(path) - startFreeBytes;
117 notifyProgress(startProgress + CONSTRAIN((int)
118 ((deltaFreeBytes * stepProgress) / expectedBytes), 0, stepProgress), listener);
123 static status_t execCp(const std::string& fromPath, const std::string& toPath, int startProgress,
124 int stepProgress, const android::sp<android::os::IVoldTaskListener>& listener) {
125 notifyProgress(startProgress, listener);
127 uint64_t expectedBytes = GetTreeBytes(fromPath);
128 uint64_t startFreeBytes = GetFreeBytes(toPath);
130 if (expectedBytes > startFreeBytes) {
131 LOG(ERROR) << "Data size " << expectedBytes << " is too large to fit in free space "
136 std::vector<std::string> cmd;
137 cmd.push_back(kCpPath);
138 cmd.push_back("-p"); /* preserve timestamps, ownership, and permissions */
139 cmd.push_back("-R"); /* recurse into subdirectories (DEST must be a directory) */
140 cmd.push_back("-P"); /* Do not follow symlinks [default] */
141 cmd.push_back("-d"); /* don't dereference symlinks */
142 if (pushBackContents(fromPath, cmd, false) != OK) {
143 LOG(WARNING) << "No contents in " << fromPath;
146 cmd.push_back(toPath.c_str());
148 if (android::base::GetBoolProperty(kPropBlockingExec, false)) {
149 return ForkExecvp(cmd);
152 pid_t pid = ForkExecvpAsync(cmd);
153 if (pid == -1) return -1;
157 if (waitpid(pid, &status, WNOHANG) == pid) {
158 if (WIFEXITED(status)) {
159 LOG(DEBUG) << "Finished cp with status " << WEXITSTATUS(status);
160 return (WEXITSTATUS(status) == 0) ? OK : -1;
167 uint64_t deltaFreeBytes = startFreeBytes - GetFreeBytes(toPath);
168 notifyProgress(startProgress + CONSTRAIN((int)
169 ((deltaFreeBytes * stepProgress) / expectedBytes), 0, stepProgress), listener);
174 static void bringOffline(const std::shared_ptr<VolumeBase>& vol) {
176 vol->setSilent(true);
178 vol->setMountFlags(0);
182 static void bringOnline(const std::shared_ptr<VolumeBase>& vol) {
184 vol->setSilent(false);
188 static status_t moveStorageInternal(const std::shared_ptr<VolumeBase>& from,
189 const std::shared_ptr<VolumeBase>& to,
190 const android::sp<android::os::IVoldTaskListener>& listener) {
191 std::string fromPath;
194 // TODO: add support for public volumes
195 if (from->getType() != VolumeBase::Type::kEmulated) goto fail;
196 if (to->getType() != VolumeBase::Type::kEmulated) goto fail;
198 // Step 1: tear down volumes and mount silently without making
199 // visible to userspace apps
201 std::lock_guard<std::mutex> lock(VolumeManager::Instance()->getLock());
206 fromPath = from->getInternalPath();
207 toPath = to->getInternalPath();
209 // Step 2: clean up any stale data
210 if (execRm(toPath, 10, 10, listener) != OK) {
214 // Step 3: perform actual copy
215 if (execCp(fromPath, toPath, 20, 60, listener) != OK) {
219 // NOTE: MountService watches for this magic value to know
220 // that move was successful
221 notifyProgress(82, listener);
223 std::lock_guard<std::mutex> lock(VolumeManager::Instance()->getLock());
228 // Step 4: clean up old data
229 if (execRm(fromPath, 85, 15, listener) != OK) {
233 notifyProgress(kMoveSucceeded, listener);
237 // if we failed to copy the data we should not leave it laying around
238 // in target location. Do not check return value, we can not do any
240 execRm(toPath, 80, 1, listener);
243 std::lock_guard<std::mutex> lock(VolumeManager::Instance()->getLock());
247 notifyProgress(kMoveFailedInternalError, listener);
251 void MoveStorage(const std::shared_ptr<VolumeBase>& from, const std::shared_ptr<VolumeBase>& to,
252 const android::sp<android::os::IVoldTaskListener>& listener) {
253 acquire_wake_lock(PARTIAL_WAKE_LOCK, kWakeLock);
255 android::os::PersistableBundle extras;
256 status_t res = moveStorageInternal(from, to, listener);
258 listener->onFinished(res, extras);
261 release_wake_lock(kWakeLock);
265 } // namespace android