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 | |
| 22 | #include <base/stringprintf.h> |
| 23 | #include <base/logging.h> |
| 24 | #include <private/android_filesystem_config.h> |
| 25 | |
| 26 | #include <dirent.h> |
| 27 | #include <sys/wait.h> |
| 28 | |
| 29 | #define CONSTRAIN(amount, low, high) (amount < low ? low : (amount > high ? high : amount)) |
| 30 | |
| 31 | using android::base::StringPrintf; |
| 32 | |
| 33 | namespace android { |
| 34 | namespace vold { |
| 35 | |
| 36 | // TODO: keep in sync with PackageManager |
| 37 | static const int kMoveSucceeded = -100; |
| 38 | static const int kMoveFailedInternalError = -6; |
| 39 | |
| 40 | static const char* kCpPath = "/system/bin/cp"; |
| 41 | static const char* kRmPath = "/system/bin/rm"; |
| 42 | |
| 43 | MoveTask::MoveTask(const std::shared_ptr<VolumeBase>& from, |
| 44 | const std::shared_ptr<VolumeBase>& to) : |
| 45 | mFrom(from), mTo(to) { |
| 46 | } |
| 47 | |
| 48 | MoveTask::~MoveTask() { |
| 49 | } |
| 50 | |
| 51 | void MoveTask::start() { |
| 52 | mThread = std::thread(&MoveTask::run, this); |
| 53 | } |
| 54 | |
| 55 | static void notifyProgress(int progress) { |
| 56 | VolumeManager::Instance()->getBroadcaster()->sendBroadcast(ResponseCode::MoveStatus, |
| 57 | StringPrintf("%d", progress).c_str(), false); |
| 58 | } |
| 59 | |
| 60 | static status_t pushBackContents(const std::string& path, std::vector<std::string>& cmd) { |
| 61 | DIR* dir = opendir(path.c_str()); |
| 62 | if (dir == NULL) { |
| 63 | return -1; |
| 64 | } |
| 65 | bool found = false; |
| 66 | struct dirent* ent; |
| 67 | while ((ent = readdir(dir)) != NULL) { |
| 68 | if ((!strcmp(ent->d_name, ".")) || (!strcmp(ent->d_name, ".."))) { |
| 69 | continue; |
| 70 | } |
| 71 | cmd.push_back(StringPrintf("%s/%s", path.c_str(), ent->d_name)); |
| 72 | found = true; |
| 73 | } |
| 74 | closedir(dir); |
| 75 | return found ? OK : -1; |
| 76 | } |
| 77 | |
| 78 | static status_t execRm(const std::string& path, int startProgress, int stepProgress) { |
| 79 | notifyProgress(startProgress); |
| 80 | |
| 81 | uint64_t expectedBytes = GetTreeBytes(path); |
| 82 | uint64_t startFreeBytes = GetFreeBytes(path); |
| 83 | |
| 84 | std::vector<std::string> cmd; |
| 85 | cmd.push_back(kRmPath); |
| 86 | cmd.push_back("-f"); /* force: remove without confirmation, no error if it doesn't exist */ |
| 87 | cmd.push_back("-R"); /* recursive: remove directory contents */ |
| 88 | if (pushBackContents(path, cmd) != OK) { |
| 89 | LOG(WARNING) << "No contents in " << path; |
| 90 | return OK; |
| 91 | } |
| 92 | |
| 93 | pid_t pid = ForkExecvpAsync(cmd); |
| 94 | if (pid == -1) return -1; |
| 95 | |
| 96 | int status; |
| 97 | while (true) { |
| 98 | if (waitpid(pid, &status, WNOHANG) == pid) { |
| 99 | if (WIFEXITED(status)) { |
| 100 | LOG(DEBUG) << "Finished rm with status " << WEXITSTATUS(status); |
| 101 | return (WEXITSTATUS(status) == 0) ? OK : -1; |
| 102 | } else { |
| 103 | break; |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | sleep(1); |
| 108 | uint64_t deltaFreeBytes = GetFreeBytes(path) - startFreeBytes; |
| 109 | notifyProgress(startProgress + CONSTRAIN((int) |
| 110 | ((deltaFreeBytes * stepProgress) / expectedBytes), 0, stepProgress)); |
| 111 | } |
| 112 | return -1; |
| 113 | } |
| 114 | |
| 115 | static status_t execCp(const std::string& fromPath, const std::string& toPath, |
| 116 | int startProgress, int stepProgress) { |
| 117 | notifyProgress(startProgress); |
| 118 | |
| 119 | uint64_t expectedBytes = GetTreeBytes(fromPath); |
| 120 | uint64_t startFreeBytes = GetFreeBytes(toPath); |
| 121 | |
| 122 | std::vector<std::string> cmd; |
| 123 | cmd.push_back(kCpPath); |
| 124 | cmd.push_back("-p"); /* preserve timestamps, ownership, and permissions */ |
| 125 | cmd.push_back("-R"); /* recurse into subdirectories (DEST must be a directory) */ |
| 126 | cmd.push_back("-P"); /* Do not follow symlinks [default] */ |
| 127 | cmd.push_back("-d"); /* don't dereference symlinks */ |
| 128 | if (pushBackContents(fromPath, cmd) != OK) { |
| 129 | LOG(WARNING) << "No contents in " << fromPath; |
| 130 | return OK; |
| 131 | } |
| 132 | cmd.push_back(toPath.c_str()); |
| 133 | |
| 134 | pid_t pid = ForkExecvpAsync(cmd); |
| 135 | if (pid == -1) return -1; |
| 136 | |
| 137 | int status; |
| 138 | while (true) { |
| 139 | if (waitpid(pid, &status, WNOHANG) == pid) { |
| 140 | if (WIFEXITED(status)) { |
| 141 | LOG(DEBUG) << "Finished cp with status " << WEXITSTATUS(status); |
| 142 | return (WEXITSTATUS(status) == 0) ? OK : -1; |
| 143 | } else { |
| 144 | break; |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | sleep(1); |
| 149 | uint64_t deltaFreeBytes = startFreeBytes - GetFreeBytes(toPath); |
| 150 | notifyProgress(startProgress + CONSTRAIN((int) |
| 151 | ((deltaFreeBytes * stepProgress) / expectedBytes), 0, stepProgress)); |
| 152 | } |
| 153 | return -1; |
| 154 | } |
| 155 | |
| 156 | static void bringOffline(const std::shared_ptr<VolumeBase>& vol) { |
| 157 | vol->destroy(); |
| 158 | vol->setSilent(true); |
| 159 | vol->create(); |
| 160 | vol->setMountFlags(0); |
| 161 | vol->mount(); |
| 162 | } |
| 163 | |
| 164 | static void bringOnline(const std::shared_ptr<VolumeBase>& vol) { |
| 165 | vol->destroy(); |
| 166 | vol->setSilent(false); |
| 167 | vol->create(); |
| 168 | } |
| 169 | |
| 170 | void MoveTask::run() { |
| 171 | std::string fromPath; |
| 172 | std::string toPath; |
| 173 | |
| 174 | // TODO: add support for public volumes |
| 175 | if (mFrom->getType() != VolumeBase::Type::kEmulated) goto fail; |
| 176 | if (mTo->getType() != VolumeBase::Type::kEmulated) goto fail; |
| 177 | |
| 178 | // Step 1: tear down volumes and mount silently without making |
| 179 | // visible to userspace apps |
| 180 | bringOffline(mFrom); |
| 181 | bringOffline(mTo); |
| 182 | |
| 183 | fromPath = mFrom->getInternalPath(); |
| 184 | toPath = mTo->getInternalPath(); |
| 185 | |
| 186 | // Step 2: clean up any stale data |
| 187 | if (execRm(toPath, 10, 10) != OK) { |
| 188 | goto fail; |
| 189 | } |
| 190 | |
| 191 | // Step 3: perform actual copy |
| 192 | if (execCp(fromPath, toPath, 20, 60) != OK) { |
| 193 | goto fail; |
| 194 | } |
| 195 | |
| 196 | // NOTE: MountService watches for this magic value to know |
| 197 | // that move was successful |
| 198 | notifyProgress(82); |
| 199 | bringOnline(mFrom); |
| 200 | bringOnline(mTo); |
| 201 | |
| 202 | // Step 4: clean up old data |
| 203 | if (execRm(fromPath, 85, 15) != OK) { |
| 204 | goto fail; |
| 205 | } |
| 206 | |
| 207 | notifyProgress(kMoveSucceeded); |
| 208 | return; |
| 209 | fail: |
| 210 | bringOnline(mFrom); |
| 211 | bringOnline(mTo); |
| 212 | notifyProgress(kMoveFailedInternalError); |
| 213 | return; |
| 214 | } |
| 215 | |
| 216 | } // namespace vold |
| 217 | } // namespace android |