Jeff Sharkey | 1d6fbcc | 2015-04-24 16:00:03 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
Elliott Hughes | 7e128fb | 2015-12-04 15:50:53 -0800 | [diff] [blame] | 22 | #include <android-base/stringprintf.h> |
| 23 | #include <android-base/logging.h> |
Jeff Sharkey | 1d6fbcc | 2015-04-24 16:00:03 -0700 | [diff] [blame] | 24 | #include <private/android_filesystem_config.h> |
Jeff Sharkey | c86ab6f | 2015-06-26 14:02:09 -0700 | [diff] [blame] | 25 | #include <hardware_legacy/power.h> |
Jeff Sharkey | 1d6fbcc | 2015-04-24 16:00:03 -0700 | [diff] [blame] | 26 | |
| 27 | #include <dirent.h> |
| 28 | #include <sys/wait.h> |
| 29 | |
Chih-Hung Hsieh | cc5d580 | 2016-05-11 15:05:05 -0700 | [diff] [blame] | 30 | #define CONSTRAIN(amount, low, high) ((amount) < (low) ? (low) : ((amount) > (high) ? (high) : (amount))) |
Jeff Sharkey | 1d6fbcc | 2015-04-24 16:00:03 -0700 | [diff] [blame] | 31 | |
Jeff Sharkey | fce701b | 2016-07-29 15:52:41 -0600 | [diff] [blame] | 32 | #define EXEC_BLOCKING 0 |
| 33 | |
Jeff Sharkey | 1d6fbcc | 2015-04-24 16:00:03 -0700 | [diff] [blame] | 34 | using android::base::StringPrintf; |
| 35 | |
| 36 | namespace android { |
| 37 | namespace vold { |
| 38 | |
| 39 | // TODO: keep in sync with PackageManager |
| 40 | static const int kMoveSucceeded = -100; |
| 41 | static const int kMoveFailedInternalError = -6; |
| 42 | |
| 43 | static const char* kCpPath = "/system/bin/cp"; |
| 44 | static const char* kRmPath = "/system/bin/rm"; |
| 45 | |
Jeff Sharkey | c86ab6f | 2015-06-26 14:02:09 -0700 | [diff] [blame] | 46 | static const char* kWakeLock = "MoveTask"; |
| 47 | |
Jeff Sharkey | 52f7a91 | 2017-09-15 12:57:44 -0600 | [diff] [blame^] | 48 | MoveTask::MoveTask(const std::shared_ptr<VolumeBase>& from, const std::shared_ptr<VolumeBase>& to, |
| 49 | const android::sp<android::os::IVoldTaskListener>& listener) : |
| 50 | mFrom(from), mTo(to), mListener(listener) { |
Jeff Sharkey | 1d6fbcc | 2015-04-24 16:00:03 -0700 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | MoveTask::~MoveTask() { |
| 54 | } |
| 55 | |
| 56 | void MoveTask::start() { |
| 57 | mThread = std::thread(&MoveTask::run, this); |
| 58 | } |
| 59 | |
Jeff Sharkey | 52f7a91 | 2017-09-15 12:57:44 -0600 | [diff] [blame^] | 60 | void MoveTask::notifyProgress(int progress) { |
| 61 | if (mListener) { |
| 62 | android::os::PersistableBundle extras; |
| 63 | mListener->onStatus(progress, extras); |
| 64 | } |
Jeff Sharkey | 1d6fbcc | 2015-04-24 16:00:03 -0700 | [diff] [blame] | 65 | } |
| 66 | |
Jeff Sharkey | 46bb69f | 2017-06-21 13:52:23 -0600 | [diff] [blame] | 67 | static status_t pushBackContents(const std::string& path, std::vector<std::string>& cmd, |
| 68 | bool addWildcard) { |
Jeff Sharkey | 1d6fbcc | 2015-04-24 16:00:03 -0700 | [diff] [blame] | 69 | DIR* dir = opendir(path.c_str()); |
| 70 | if (dir == NULL) { |
| 71 | return -1; |
| 72 | } |
| 73 | bool found = false; |
| 74 | struct dirent* ent; |
| 75 | while ((ent = readdir(dir)) != NULL) { |
| 76 | if ((!strcmp(ent->d_name, ".")) || (!strcmp(ent->d_name, ".."))) { |
| 77 | continue; |
| 78 | } |
Jeff Sharkey | 46bb69f | 2017-06-21 13:52:23 -0600 | [diff] [blame] | 79 | if (addWildcard) { |
| 80 | cmd.push_back(StringPrintf("%s/%s/*", path.c_str(), ent->d_name)); |
| 81 | } else { |
| 82 | cmd.push_back(StringPrintf("%s/%s", path.c_str(), ent->d_name)); |
| 83 | } |
Jeff Sharkey | 1d6fbcc | 2015-04-24 16:00:03 -0700 | [diff] [blame] | 84 | found = true; |
| 85 | } |
| 86 | closedir(dir); |
| 87 | return found ? OK : -1; |
| 88 | } |
| 89 | |
Jeff Sharkey | 52f7a91 | 2017-09-15 12:57:44 -0600 | [diff] [blame^] | 90 | status_t MoveTask::execRm(const std::string& path, int startProgress, int stepProgress) { |
Jeff Sharkey | 1d6fbcc | 2015-04-24 16:00:03 -0700 | [diff] [blame] | 91 | notifyProgress(startProgress); |
| 92 | |
| 93 | uint64_t expectedBytes = GetTreeBytes(path); |
| 94 | uint64_t startFreeBytes = GetFreeBytes(path); |
| 95 | |
| 96 | std::vector<std::string> cmd; |
| 97 | cmd.push_back(kRmPath); |
| 98 | cmd.push_back("-f"); /* force: remove without confirmation, no error if it doesn't exist */ |
| 99 | cmd.push_back("-R"); /* recursive: remove directory contents */ |
Jeff Sharkey | 46bb69f | 2017-06-21 13:52:23 -0600 | [diff] [blame] | 100 | if (pushBackContents(path, cmd, true) != OK) { |
Jeff Sharkey | 1d6fbcc | 2015-04-24 16:00:03 -0700 | [diff] [blame] | 101 | LOG(WARNING) << "No contents in " << path; |
| 102 | return OK; |
| 103 | } |
| 104 | |
Jeff Sharkey | fce701b | 2016-07-29 15:52:41 -0600 | [diff] [blame] | 105 | #if EXEC_BLOCKING |
| 106 | return ForkExecvp(cmd); |
| 107 | #else |
Jeff Sharkey | 1d6fbcc | 2015-04-24 16:00:03 -0700 | [diff] [blame] | 108 | pid_t pid = ForkExecvpAsync(cmd); |
| 109 | if (pid == -1) return -1; |
| 110 | |
| 111 | int status; |
| 112 | while (true) { |
| 113 | if (waitpid(pid, &status, WNOHANG) == pid) { |
| 114 | if (WIFEXITED(status)) { |
| 115 | LOG(DEBUG) << "Finished rm with status " << WEXITSTATUS(status); |
| 116 | return (WEXITSTATUS(status) == 0) ? OK : -1; |
| 117 | } else { |
| 118 | break; |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | sleep(1); |
| 123 | uint64_t deltaFreeBytes = GetFreeBytes(path) - startFreeBytes; |
| 124 | notifyProgress(startProgress + CONSTRAIN((int) |
| 125 | ((deltaFreeBytes * stepProgress) / expectedBytes), 0, stepProgress)); |
| 126 | } |
| 127 | return -1; |
Jeff Sharkey | fce701b | 2016-07-29 15:52:41 -0600 | [diff] [blame] | 128 | #endif |
Jeff Sharkey | 1d6fbcc | 2015-04-24 16:00:03 -0700 | [diff] [blame] | 129 | } |
| 130 | |
Jeff Sharkey | 52f7a91 | 2017-09-15 12:57:44 -0600 | [diff] [blame^] | 131 | status_t MoveTask::execCp(const std::string& fromPath, const std::string& toPath, |
Jeff Sharkey | 1d6fbcc | 2015-04-24 16:00:03 -0700 | [diff] [blame] | 132 | int startProgress, int stepProgress) { |
| 133 | notifyProgress(startProgress); |
| 134 | |
| 135 | uint64_t expectedBytes = GetTreeBytes(fromPath); |
| 136 | uint64_t startFreeBytes = GetFreeBytes(toPath); |
| 137 | |
Jeff Sharkey | a0220a5 | 2017-04-03 17:11:45 -0600 | [diff] [blame] | 138 | if (expectedBytes > startFreeBytes) { |
| 139 | LOG(ERROR) << "Data size " << expectedBytes << " is too large to fit in free space " |
| 140 | << startFreeBytes; |
| 141 | return -1; |
| 142 | } |
| 143 | |
Jeff Sharkey | 1d6fbcc | 2015-04-24 16:00:03 -0700 | [diff] [blame] | 144 | std::vector<std::string> cmd; |
| 145 | cmd.push_back(kCpPath); |
| 146 | cmd.push_back("-p"); /* preserve timestamps, ownership, and permissions */ |
| 147 | cmd.push_back("-R"); /* recurse into subdirectories (DEST must be a directory) */ |
| 148 | cmd.push_back("-P"); /* Do not follow symlinks [default] */ |
| 149 | cmd.push_back("-d"); /* don't dereference symlinks */ |
Jeff Sharkey | 46bb69f | 2017-06-21 13:52:23 -0600 | [diff] [blame] | 150 | if (pushBackContents(fromPath, cmd, false) != OK) { |
Jeff Sharkey | 1d6fbcc | 2015-04-24 16:00:03 -0700 | [diff] [blame] | 151 | LOG(WARNING) << "No contents in " << fromPath; |
| 152 | return OK; |
| 153 | } |
| 154 | cmd.push_back(toPath.c_str()); |
| 155 | |
Jeff Sharkey | fce701b | 2016-07-29 15:52:41 -0600 | [diff] [blame] | 156 | #if EXEC_BLOCKING |
| 157 | return ForkExecvp(cmd); |
| 158 | #else |
Jeff Sharkey | 1d6fbcc | 2015-04-24 16:00:03 -0700 | [diff] [blame] | 159 | pid_t pid = ForkExecvpAsync(cmd); |
| 160 | if (pid == -1) return -1; |
| 161 | |
| 162 | int status; |
| 163 | while (true) { |
| 164 | if (waitpid(pid, &status, WNOHANG) == pid) { |
| 165 | if (WIFEXITED(status)) { |
| 166 | LOG(DEBUG) << "Finished cp with status " << WEXITSTATUS(status); |
| 167 | return (WEXITSTATUS(status) == 0) ? OK : -1; |
| 168 | } else { |
| 169 | break; |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | sleep(1); |
| 174 | uint64_t deltaFreeBytes = startFreeBytes - GetFreeBytes(toPath); |
| 175 | notifyProgress(startProgress + CONSTRAIN((int) |
| 176 | ((deltaFreeBytes * stepProgress) / expectedBytes), 0, stepProgress)); |
| 177 | } |
| 178 | return -1; |
Jeff Sharkey | fce701b | 2016-07-29 15:52:41 -0600 | [diff] [blame] | 179 | #endif |
Jeff Sharkey | 1d6fbcc | 2015-04-24 16:00:03 -0700 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | static void bringOffline(const std::shared_ptr<VolumeBase>& vol) { |
| 183 | vol->destroy(); |
| 184 | vol->setSilent(true); |
| 185 | vol->create(); |
| 186 | vol->setMountFlags(0); |
| 187 | vol->mount(); |
| 188 | } |
| 189 | |
| 190 | static void bringOnline(const std::shared_ptr<VolumeBase>& vol) { |
| 191 | vol->destroy(); |
| 192 | vol->setSilent(false); |
| 193 | vol->create(); |
| 194 | } |
| 195 | |
| 196 | void MoveTask::run() { |
Jeff Sharkey | c86ab6f | 2015-06-26 14:02:09 -0700 | [diff] [blame] | 197 | acquire_wake_lock(PARTIAL_WAKE_LOCK, kWakeLock); |
| 198 | |
Jeff Sharkey | 1d6fbcc | 2015-04-24 16:00:03 -0700 | [diff] [blame] | 199 | std::string fromPath; |
| 200 | std::string toPath; |
| 201 | |
| 202 | // TODO: add support for public volumes |
| 203 | if (mFrom->getType() != VolumeBase::Type::kEmulated) goto fail; |
| 204 | if (mTo->getType() != VolumeBase::Type::kEmulated) goto fail; |
| 205 | |
| 206 | // Step 1: tear down volumes and mount silently without making |
| 207 | // visible to userspace apps |
Henrik Baard | 7f52bca | 2015-11-26 12:05:13 +0100 | [diff] [blame] | 208 | { |
| 209 | std::lock_guard<std::mutex> lock(VolumeManager::Instance()->getLock()); |
| 210 | bringOffline(mFrom); |
| 211 | bringOffline(mTo); |
| 212 | } |
Jeff Sharkey | 1d6fbcc | 2015-04-24 16:00:03 -0700 | [diff] [blame] | 213 | |
| 214 | fromPath = mFrom->getInternalPath(); |
| 215 | toPath = mTo->getInternalPath(); |
| 216 | |
| 217 | // Step 2: clean up any stale data |
| 218 | if (execRm(toPath, 10, 10) != OK) { |
| 219 | goto fail; |
| 220 | } |
| 221 | |
| 222 | // Step 3: perform actual copy |
| 223 | if (execCp(fromPath, toPath, 20, 60) != OK) { |
Henrik Baard | 77f156d | 2015-12-17 13:58:42 +0100 | [diff] [blame] | 224 | goto copy_fail; |
Jeff Sharkey | 1d6fbcc | 2015-04-24 16:00:03 -0700 | [diff] [blame] | 225 | } |
| 226 | |
| 227 | // NOTE: MountService watches for this magic value to know |
| 228 | // that move was successful |
| 229 | notifyProgress(82); |
Henrik Baard | 7f52bca | 2015-11-26 12:05:13 +0100 | [diff] [blame] | 230 | { |
| 231 | std::lock_guard<std::mutex> lock(VolumeManager::Instance()->getLock()); |
| 232 | bringOnline(mFrom); |
| 233 | bringOnline(mTo); |
| 234 | } |
Jeff Sharkey | 1d6fbcc | 2015-04-24 16:00:03 -0700 | [diff] [blame] | 235 | |
| 236 | // Step 4: clean up old data |
| 237 | if (execRm(fromPath, 85, 15) != OK) { |
| 238 | goto fail; |
| 239 | } |
| 240 | |
| 241 | notifyProgress(kMoveSucceeded); |
Jeff Sharkey | c86ab6f | 2015-06-26 14:02:09 -0700 | [diff] [blame] | 242 | release_wake_lock(kWakeLock); |
Jeff Sharkey | 1d6fbcc | 2015-04-24 16:00:03 -0700 | [diff] [blame] | 243 | return; |
Henrik Baard | 77f156d | 2015-12-17 13:58:42 +0100 | [diff] [blame] | 244 | |
| 245 | copy_fail: |
| 246 | // if we failed to copy the data we should not leave it laying around |
| 247 | // in target location. Do not check return value, we can not do any |
| 248 | // useful anyway. |
| 249 | execRm(toPath, 80, 1); |
Jeff Sharkey | 1d6fbcc | 2015-04-24 16:00:03 -0700 | [diff] [blame] | 250 | fail: |
Henrik Baard | 7f52bca | 2015-11-26 12:05:13 +0100 | [diff] [blame] | 251 | { |
| 252 | std::lock_guard<std::mutex> lock(VolumeManager::Instance()->getLock()); |
| 253 | bringOnline(mFrom); |
| 254 | bringOnline(mTo); |
| 255 | } |
Jeff Sharkey | 1d6fbcc | 2015-04-24 16:00:03 -0700 | [diff] [blame] | 256 | notifyProgress(kMoveFailedInternalError); |
Jeff Sharkey | c86ab6f | 2015-06-26 14:02:09 -0700 | [diff] [blame] | 257 | release_wake_lock(kWakeLock); |
Jeff Sharkey | 1d6fbcc | 2015-04-24 16:00:03 -0700 | [diff] [blame] | 258 | return; |
| 259 | } |
| 260 | |
| 261 | } // namespace vold |
| 262 | } // namespace android |