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 <private/android_filesystem_config.h>
25 #include <wakelock/wakelock.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 bool pushBackContents(const std::string& path, std::vector<std::string>& cmd,
61 if (searchLevels == 0) {
62 cmd.emplace_back(path);
65 auto dirp = std::unique_ptr<DIR, int (*)(DIR*)>(opendir(path.c_str()), closedir);
67 PLOG(ERROR) << "Unable to open directory: " << path;
72 while ((ent = readdir(dirp.get())) != NULL) {
73 if (IsDotOrDotDot(*ent)) continue;
74 auto subdir = path + "/" + ent->d_name;
75 found |= pushBackContents(subdir, cmd, searchLevels - 1);
80 static status_t execRm(const std::string& path, int startProgress, int stepProgress,
81 const android::sp<android::os::IVoldTaskListener>& listener) {
82 notifyProgress(startProgress, listener);
84 uint64_t expectedBytes = GetTreeBytes(path);
85 uint64_t startFreeBytes = GetFreeBytes(path);
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, 2)) {
92 LOG(WARNING) << "No contents in " << path;
96 if (android::base::GetBoolProperty(kPropBlockingExec, false)) {
97 return ForkExecvp(cmd);
100 pid_t pid = ForkExecvpAsync(cmd);
101 if (pid == -1) return -1;
105 if (waitpid(pid, &status, WNOHANG) == pid) {
106 if (WIFEXITED(status)) {
107 LOG(DEBUG) << "Finished rm with status " << WEXITSTATUS(status);
108 return (WEXITSTATUS(status) == 0) ? OK : -1;
115 uint64_t deltaFreeBytes = GetFreeBytes(path) - startFreeBytes;
118 CONSTRAIN((int)((deltaFreeBytes * stepProgress) / expectedBytes), 0, stepProgress),
124 static status_t execCp(const std::string& fromPath, const std::string& toPath, int startProgress,
126 const android::sp<android::os::IVoldTaskListener>& listener) {
127 notifyProgress(startProgress, listener);
129 uint64_t expectedBytes = GetTreeBytes(fromPath);
130 uint64_t startFreeBytes = GetFreeBytes(toPath);
132 if (expectedBytes > startFreeBytes) {
133 LOG(ERROR) << "Data size " << expectedBytes << " is too large to fit in free space "
138 std::vector<std::string> cmd;
139 cmd.push_back(kCpPath);
140 cmd.push_back("-p"); /* preserve timestamps, ownership, and permissions */
141 cmd.push_back("-R"); /* recurse into subdirectories (DEST must be a directory) */
142 cmd.push_back("-P"); /* Do not follow symlinks [default] */
143 cmd.push_back("-d"); /* don't dereference symlinks */
144 if (!pushBackContents(fromPath, cmd, 1)) {
145 LOG(WARNING) << "No contents in " << fromPath;
148 cmd.push_back(toPath.c_str());
150 if (android::base::GetBoolProperty(kPropBlockingExec, false)) {
151 return ForkExecvp(cmd);
154 pid_t pid = ForkExecvpAsync(cmd);
155 if (pid == -1) return -1;
159 if (waitpid(pid, &status, WNOHANG) == pid) {
160 if (WIFEXITED(status)) {
161 LOG(DEBUG) << "Finished cp with status " << WEXITSTATUS(status);
162 return (WEXITSTATUS(status) == 0) ? OK : -1;
169 uint64_t deltaFreeBytes = startFreeBytes - GetFreeBytes(toPath);
172 CONSTRAIN((int)((deltaFreeBytes * stepProgress) / expectedBytes), 0, stepProgress),
178 static void bringOffline(const std::shared_ptr<VolumeBase>& vol) {
180 vol->setSilent(true);
182 vol->setMountFlags(0);
186 static void bringOnline(const std::shared_ptr<VolumeBase>& vol) {
188 vol->setSilent(false);
192 static status_t moveStorageInternal(const std::shared_ptr<VolumeBase>& from,
193 const std::shared_ptr<VolumeBase>& to,
194 const android::sp<android::os::IVoldTaskListener>& listener) {
195 std::string fromPath;
198 // TODO: add support for public volumes
199 if (from->getType() != VolumeBase::Type::kEmulated) goto fail;
200 if (to->getType() != VolumeBase::Type::kEmulated) goto fail;
202 // Step 1: tear down volumes and mount silently without making
203 // visible to userspace apps
205 std::lock_guard<std::mutex> lock(VolumeManager::Instance()->getLock());
210 fromPath = from->getInternalPath();
211 toPath = to->getInternalPath();
213 // Step 2: clean up any stale data
214 if (execRm(toPath, 10, 10, listener) != OK) {
218 // Step 3: perform actual copy
219 if (execCp(fromPath, toPath, 20, 60, listener) != OK) {
223 // NOTE: MountService watches for this magic value to know
224 // that move was successful
225 notifyProgress(82, listener);
227 std::lock_guard<std::mutex> lock(VolumeManager::Instance()->getLock());
232 // Step 4: clean up old data
233 if (execRm(fromPath, 85, 15, listener) != OK) {
237 notifyProgress(kMoveSucceeded, listener);
241 // if we failed to copy the data we should not leave it laying around
242 // in target location. Do not check return value, we can not do any
244 execRm(toPath, 80, 1, listener);
248 std::lock_guard<std::mutex> lock(VolumeManager::Instance()->getLock());
253 notifyProgress(kMoveFailedInternalError, listener);
257 void MoveStorage(const std::shared_ptr<VolumeBase>& from, const std::shared_ptr<VolumeBase>& to,
258 const android::sp<android::os::IVoldTaskListener>& listener) {
259 auto wl = android::wakelock::WakeLock::tryGet(kWakeLock);
260 if (!wl.has_value()) {
264 android::os::PersistableBundle extras;
265 status_t res = moveStorageInternal(from, to, listener);
267 listener->onFinished(res, extras);
272 } // namespace android