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