The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2007 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 | |
Dan Albert | 7664901 | 2015-02-24 15:51:19 -0800 | [diff] [blame] | 17 | #include <dirent.h> |
| 18 | #include <errno.h> |
Spencer Low | 6001c87 | 2015-05-13 00:02:55 -0700 | [diff] [blame] | 19 | #include <inttypes.h> |
Dan Albert | 7664901 | 2015-02-24 15:51:19 -0800 | [diff] [blame] | 20 | #include <limits.h> |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 21 | #include <stdio.h> |
| 22 | #include <stdlib.h> |
| 23 | #include <string.h> |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 24 | #include <sys/stat.h> |
| 25 | #include <sys/time.h> |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 26 | #include <sys/types.h> |
Dan Albert | 7664901 | 2015-02-24 15:51:19 -0800 | [diff] [blame] | 27 | #include <time.h> |
Elliott Hughes | ae5a6c0 | 2015-09-27 12:55:37 -0700 | [diff] [blame] | 28 | #include <unistd.h> |
Greg Hackmann | 7a5e2bd | 2014-05-06 08:48:18 -0700 | [diff] [blame] | 29 | #include <utime.h> |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 30 | |
Josh Gao | fb0c5cb | 2016-08-04 14:53:17 -0700 | [diff] [blame] | 31 | #include <chrono> |
Josh Gao | cda6a2b | 2015-11-02 16:45:47 -0800 | [diff] [blame] | 32 | #include <functional> |
Elliott Hughes | a925dba | 2015-08-24 14:49:43 -0700 | [diff] [blame] | 33 | #include <memory> |
Josh Gao | fb0c5cb | 2016-08-04 14:53:17 -0700 | [diff] [blame] | 34 | #include <sstream> |
| 35 | #include <string> |
Elliott Hughes | 6d92997 | 2015-10-27 13:40:35 -0700 | [diff] [blame] | 36 | #include <vector> |
Elliott Hughes | a925dba | 2015-08-24 14:49:43 -0700 | [diff] [blame] | 37 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 38 | #include "sysdeps.h" |
Dan Albert | 7664901 | 2015-02-24 15:51:19 -0800 | [diff] [blame] | 39 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 40 | #include "adb.h" |
| 41 | #include "adb_client.h" |
Dan Albert | cc731cc | 2015-02-24 21:26:58 -0800 | [diff] [blame] | 42 | #include "adb_io.h" |
Alex Vallée | 1421614 | 2015-05-06 17:22:25 -0400 | [diff] [blame] | 43 | #include "adb_utils.h" |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 44 | #include "file_sync_service.h" |
Elliott Hughes | b708d16 | 2015-10-27 16:03:15 -0700 | [diff] [blame] | 45 | #include "line_printer.h" |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 46 | |
Elliott Hughes | 4f71319 | 2015-12-04 22:00:26 -0800 | [diff] [blame] | 47 | #include <android-base/file.h> |
| 48 | #include <android-base/strings.h> |
| 49 | #include <android-base/stringprintf.h> |
Elliott Hughes | 5c74270 | 2015-07-30 17:42:01 -0700 | [diff] [blame] | 50 | |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 51 | struct syncsendbuf { |
| 52 | unsigned id; |
| 53 | unsigned size; |
| 54 | char data[SYNC_DATA_MAX]; |
| 55 | }; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 56 | |
Elliott Hughes | a00e6ef | 2015-12-17 17:05:29 -0800 | [diff] [blame] | 57 | static void ensure_trailing_separators(std::string& local_path, std::string& remote_path) { |
| 58 | if (!adb_is_separator(local_path.back())) { |
| 59 | local_path.push_back(OS_PATH_SEPARATOR); |
| 60 | } |
| 61 | if (remote_path.back() != '/') { |
| 62 | remote_path.push_back('/'); |
| 63 | } |
| 64 | } |
| 65 | |
Josh Gao | a31ea55 | 2016-03-02 16:11:13 -0800 | [diff] [blame] | 66 | static bool should_pull_file(mode_t mode) { |
| 67 | return mode & (S_IFREG | S_IFBLK | S_IFCHR); |
| 68 | } |
| 69 | |
| 70 | static bool should_push_file(mode_t mode) { |
| 71 | mode_t mask = S_IFREG; |
| 72 | #if !defined(_WIN32) |
| 73 | mask |= S_IFLNK; |
| 74 | #endif |
| 75 | return mode & mask; |
| 76 | } |
| 77 | |
Elliott Hughes | a00e6ef | 2015-12-17 17:05:29 -0800 | [diff] [blame] | 78 | struct copyinfo { |
| 79 | std::string lpath; |
| 80 | std::string rpath; |
| 81 | unsigned int time = 0; |
| 82 | unsigned int mode; |
| 83 | uint64_t size = 0; |
| 84 | bool skip = false; |
| 85 | |
| 86 | copyinfo(const std::string& local_path, |
| 87 | const std::string& remote_path, |
| 88 | const std::string& name, |
| 89 | unsigned int mode) |
| 90 | : lpath(local_path), rpath(remote_path), mode(mode) { |
| 91 | ensure_trailing_separators(lpath, rpath); |
| 92 | lpath.append(name); |
| 93 | rpath.append(name); |
| 94 | if (S_ISDIR(mode)) { |
| 95 | ensure_trailing_separators(lpath, rpath); |
| 96 | } |
| 97 | } |
| 98 | }; |
| 99 | |
Josh Gao | fb0c5cb | 2016-08-04 14:53:17 -0700 | [diff] [blame] | 100 | enum class TransferDirection { |
| 101 | push, |
| 102 | pull, |
| 103 | }; |
| 104 | |
| 105 | struct TransferLedger { |
| 106 | std::chrono::steady_clock::time_point start_time; |
| 107 | uint64_t files_transferred; |
| 108 | uint64_t files_skipped; |
| 109 | uint64_t bytes_transferred; |
| 110 | uint64_t bytes_expected; |
| 111 | bool expect_multiple_files; |
| 112 | |
| 113 | TransferLedger() { |
| 114 | Reset(); |
| 115 | } |
| 116 | |
| 117 | bool operator==(const TransferLedger& other) const { |
| 118 | return files_transferred == other.files_transferred && |
| 119 | files_skipped == other.files_skipped && bytes_transferred == other.bytes_transferred; |
| 120 | } |
| 121 | |
| 122 | bool operator!=(const TransferLedger& other) const { |
| 123 | return !(*this == other); |
| 124 | } |
| 125 | |
| 126 | void Reset() { |
| 127 | start_time = std::chrono::steady_clock::now(); |
| 128 | files_transferred = 0; |
| 129 | files_skipped = 0; |
| 130 | bytes_transferred = 0; |
| 131 | bytes_expected = 0; |
| 132 | } |
| 133 | |
| 134 | std::string TransferRate() { |
| 135 | if (bytes_transferred == 0) return ""; |
| 136 | |
| 137 | std::chrono::duration<double> duration; |
| 138 | duration = std::chrono::steady_clock::now() - start_time; |
| 139 | |
| 140 | double s = duration.count(); |
| 141 | if (s == 0) { |
| 142 | return ""; |
| 143 | } |
| 144 | double rate = (static_cast<double>(bytes_transferred) / s) / (1024 * 1024); |
| 145 | return android::base::StringPrintf(" %.1f MB/s (%" PRIu64 " bytes in %.3fs)", rate, |
| 146 | bytes_transferred, s); |
| 147 | } |
| 148 | |
| 149 | void ReportProgress(LinePrinter& lp, const std::string& file, uint64_t file_copied_bytes, |
| 150 | uint64_t file_total_bytes) { |
| 151 | char overall_percentage_str[5] = "?"; |
| 152 | if (bytes_expected != 0) { |
| 153 | int overall_percentage = static_cast<int>(bytes_transferred * 100 / bytes_expected); |
| 154 | // If we're pulling symbolic links, we'll pull the target of the link rather than |
| 155 | // just create a local link, and that will cause us to go over 100%. |
| 156 | if (overall_percentage <= 100) { |
| 157 | snprintf(overall_percentage_str, sizeof(overall_percentage_str), "%d%%", |
| 158 | overall_percentage); |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | std::string output; |
| 163 | if (file_copied_bytes > file_total_bytes || file_total_bytes == 0) { |
| 164 | // This case can happen if we're racing against something that wrote to the file |
| 165 | // between our stat and our read, or if we're reading a magic file that lies about |
| 166 | // its size. Just show how much we've copied. |
| 167 | output = android::base::StringPrintf("[%4s] %s: %" PRId64 "/?", overall_percentage_str, |
| 168 | file.c_str(), file_copied_bytes); |
| 169 | } else { |
| 170 | // If we're transferring multiple files, we want to know how far through the current |
| 171 | // file we are, as well as the overall percentage. |
| 172 | if (expect_multiple_files) { |
| 173 | int file_percentage = static_cast<int>(file_copied_bytes * 100 / file_total_bytes); |
| 174 | output = android::base::StringPrintf("[%4s] %s: %d%%", overall_percentage_str, |
| 175 | file.c_str(), file_percentage); |
| 176 | } else { |
| 177 | output = |
| 178 | android::base::StringPrintf("[%4s] %s", overall_percentage_str, file.c_str()); |
| 179 | } |
| 180 | } |
| 181 | lp.Print(output, LinePrinter::LineType::INFO); |
| 182 | } |
| 183 | |
| 184 | void ReportTransferRate(LinePrinter& lp, const std::string& name, TransferDirection direction) { |
| 185 | const char* direction_str = (direction == TransferDirection::push) ? "pushed" : "pulled"; |
| 186 | std::stringstream ss; |
| 187 | if (!name.empty()) { |
| 188 | ss << name << ": "; |
| 189 | } |
| 190 | ss << files_transferred << " file" << ((files_transferred == 1) ? "" : "s") << " " |
| 191 | << direction_str << "."; |
| 192 | if (files_skipped > 0) { |
| 193 | ss << " " << files_skipped << " file" << ((files_skipped == 1) ? "" : "s") |
| 194 | << " skipped."; |
| 195 | } |
| 196 | ss << TransferRate(); |
| 197 | |
| 198 | lp.Print(ss.str(), LinePrinter::LineType::INFO); |
| 199 | lp.KeepInfoLine(); |
| 200 | } |
| 201 | }; |
| 202 | |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 203 | class SyncConnection { |
| 204 | public: |
Josh Gao | fb0c5cb | 2016-08-04 14:53:17 -0700 | [diff] [blame] | 205 | SyncConnection() : expect_done_(false) { |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 206 | max = SYNC_DATA_MAX; // TODO: decide at runtime. |
| 207 | |
| 208 | std::string error; |
| 209 | fd = adb_connect("sync:", &error); |
| 210 | if (fd < 0) { |
Elliott Hughes | b708d16 | 2015-10-27 16:03:15 -0700 | [diff] [blame] | 211 | Error("connect failed: %s", error.c_str()); |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 212 | } |
| 213 | } |
| 214 | |
| 215 | ~SyncConnection() { |
| 216 | if (!IsValid()) return; |
| 217 | |
Spencer Low | 351ecd1 | 2015-10-14 17:32:44 -0700 | [diff] [blame] | 218 | if (SendQuit()) { |
| 219 | // We sent a quit command, so the server should be doing orderly |
| 220 | // shutdown soon. But if we encountered an error while we were using |
| 221 | // the connection, the server might still be sending data (before |
| 222 | // doing orderly shutdown), in which case we won't wait for all of |
| 223 | // the data nor the coming orderly shutdown. In the common success |
| 224 | // case, this will wait for the server to do orderly shutdown. |
| 225 | ReadOrderlyShutdown(fd); |
| 226 | } |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 227 | adb_close(fd); |
Elliott Hughes | 77f539a | 2015-12-08 16:01:15 -0800 | [diff] [blame] | 228 | |
| 229 | line_printer_.KeepInfoLine(); |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 230 | } |
| 231 | |
| 232 | bool IsValid() { return fd >= 0; } |
| 233 | |
Josh Gao | afcdcd7 | 2016-02-19 15:55:55 -0800 | [diff] [blame] | 234 | bool ReceivedError(const char* from, const char* to) { |
| 235 | adb_pollfd pfd = {.fd = fd, .events = POLLIN}; |
| 236 | int rc = adb_poll(&pfd, 1, 0); |
| 237 | if (rc < 0) { |
| 238 | Error("failed to poll: %s", strerror(errno)); |
| 239 | return true; |
| 240 | } |
| 241 | return rc != 0; |
| 242 | } |
| 243 | |
Josh Gao | fb0c5cb | 2016-08-04 14:53:17 -0700 | [diff] [blame] | 244 | void NewTransfer() { |
| 245 | current_ledger_.Reset(); |
| 246 | } |
| 247 | |
| 248 | void RecordBytesTransferred(size_t bytes) { |
| 249 | current_ledger_.bytes_transferred += bytes; |
| 250 | global_ledger_.bytes_transferred += bytes; |
| 251 | } |
| 252 | |
| 253 | void RecordFilesTransferred(size_t files) { |
| 254 | current_ledger_.files_transferred += files; |
| 255 | global_ledger_.files_transferred += files; |
| 256 | } |
| 257 | |
| 258 | void RecordFilesSkipped(size_t files) { |
| 259 | current_ledger_.files_skipped += files; |
| 260 | global_ledger_.files_skipped += files; |
| 261 | } |
| 262 | |
| 263 | void ReportProgress(const std::string& file, uint64_t file_copied_bytes, |
| 264 | uint64_t file_total_bytes) { |
| 265 | current_ledger_.ReportProgress(line_printer_, file, file_copied_bytes, file_total_bytes); |
| 266 | } |
| 267 | |
| 268 | void ReportTransferRate(const std::string& file, TransferDirection direction) { |
| 269 | current_ledger_.ReportTransferRate(line_printer_, file, direction); |
| 270 | } |
| 271 | |
| 272 | void ReportOverallTransferRate(TransferDirection direction) { |
| 273 | if (current_ledger_ != global_ledger_) { |
| 274 | global_ledger_.ReportTransferRate(line_printer_, "", direction); |
| 275 | } |
| 276 | } |
| 277 | |
Elliott Hughes | ae5a6c0 | 2015-09-27 12:55:37 -0700 | [diff] [blame] | 278 | bool SendRequest(int id, const char* path_and_mode) { |
| 279 | size_t path_length = strlen(path_and_mode); |
| 280 | if (path_length > 1024) { |
Elliott Hughes | b708d16 | 2015-10-27 16:03:15 -0700 | [diff] [blame] | 281 | Error("SendRequest failed: path too long: %zu", path_length); |
Elliott Hughes | ae5a6c0 | 2015-09-27 12:55:37 -0700 | [diff] [blame] | 282 | errno = ENAMETOOLONG; |
| 283 | return false; |
| 284 | } |
| 285 | |
| 286 | // Sending header and payload in a single write makes a noticeable |
| 287 | // difference to "adb sync" performance. |
Elliott Hughes | 6d92997 | 2015-10-27 13:40:35 -0700 | [diff] [blame] | 288 | std::vector<char> buf(sizeof(SyncRequest) + path_length); |
| 289 | SyncRequest* req = reinterpret_cast<SyncRequest*>(&buf[0]); |
Elliott Hughes | ae5a6c0 | 2015-09-27 12:55:37 -0700 | [diff] [blame] | 290 | req->id = id; |
| 291 | req->path_length = path_length; |
| 292 | char* data = reinterpret_cast<char*>(req + 1); |
| 293 | memcpy(data, path_and_mode, path_length); |
| 294 | |
Elliott Hughes | 6d92997 | 2015-10-27 13:40:35 -0700 | [diff] [blame] | 295 | return WriteFdExactly(fd, &buf[0], buf.size()); |
Elliott Hughes | ae5a6c0 | 2015-09-27 12:55:37 -0700 | [diff] [blame] | 296 | } |
| 297 | |
| 298 | // Sending header, payload, and footer in a single write makes a huge |
| 299 | // difference to "adb sync" performance. |
| 300 | bool SendSmallFile(const char* path_and_mode, |
Elliott Hughes | cc65c3b | 2015-11-20 22:01:06 -0800 | [diff] [blame] | 301 | const char* lpath, const char* rpath, |
Elliott Hughes | 6aab58c | 2015-11-20 17:35:17 -0800 | [diff] [blame] | 302 | unsigned mtime, |
| 303 | const char* data, size_t data_length) { |
Elliott Hughes | ae5a6c0 | 2015-09-27 12:55:37 -0700 | [diff] [blame] | 304 | size_t path_length = strlen(path_and_mode); |
| 305 | if (path_length > 1024) { |
Elliott Hughes | b708d16 | 2015-10-27 16:03:15 -0700 | [diff] [blame] | 306 | Error("SendSmallFile failed: path too long: %zu", path_length); |
Elliott Hughes | ae5a6c0 | 2015-09-27 12:55:37 -0700 | [diff] [blame] | 307 | errno = ENAMETOOLONG; |
| 308 | return false; |
| 309 | } |
| 310 | |
Elliott Hughes | 6d92997 | 2015-10-27 13:40:35 -0700 | [diff] [blame] | 311 | std::vector<char> buf(sizeof(SyncRequest) + path_length + |
Elliott Hughes | 6aab58c | 2015-11-20 17:35:17 -0800 | [diff] [blame] | 312 | sizeof(SyncRequest) + data_length + |
| 313 | sizeof(SyncRequest)); |
Elliott Hughes | 6d92997 | 2015-10-27 13:40:35 -0700 | [diff] [blame] | 314 | char* p = &buf[0]; |
Elliott Hughes | ae5a6c0 | 2015-09-27 12:55:37 -0700 | [diff] [blame] | 315 | |
| 316 | SyncRequest* req_send = reinterpret_cast<SyncRequest*>(p); |
| 317 | req_send->id = ID_SEND; |
| 318 | req_send->path_length = path_length; |
| 319 | p += sizeof(SyncRequest); |
| 320 | memcpy(p, path_and_mode, path_length); |
| 321 | p += path_length; |
| 322 | |
| 323 | SyncRequest* req_data = reinterpret_cast<SyncRequest*>(p); |
| 324 | req_data->id = ID_DATA; |
| 325 | req_data->path_length = data_length; |
| 326 | p += sizeof(SyncRequest); |
| 327 | memcpy(p, data, data_length); |
| 328 | p += data_length; |
| 329 | |
| 330 | SyncRequest* req_done = reinterpret_cast<SyncRequest*>(p); |
| 331 | req_done->id = ID_DONE; |
| 332 | req_done->path_length = mtime; |
| 333 | p += sizeof(SyncRequest); |
| 334 | |
Elliott Hughes | cc65c3b | 2015-11-20 22:01:06 -0800 | [diff] [blame] | 335 | WriteOrDie(lpath, rpath, &buf[0], (p - &buf[0])); |
Josh Gao | afcdcd7 | 2016-02-19 15:55:55 -0800 | [diff] [blame] | 336 | expect_done_ = true; |
Josh Gao | fb0c5cb | 2016-08-04 14:53:17 -0700 | [diff] [blame] | 337 | |
| 338 | // RecordFilesTransferred gets called in CopyDone. |
| 339 | RecordBytesTransferred(data_length); |
Josh Gao | 9fd2f77 | 2016-03-01 11:46:02 -0800 | [diff] [blame] | 340 | ReportProgress(rpath, data_length, data_length); |
Elliott Hughes | ae5a6c0 | 2015-09-27 12:55:37 -0700 | [diff] [blame] | 341 | return true; |
| 342 | } |
| 343 | |
Elliott Hughes | 6aab58c | 2015-11-20 17:35:17 -0800 | [diff] [blame] | 344 | bool SendLargeFile(const char* path_and_mode, |
| 345 | const char* lpath, const char* rpath, |
| 346 | unsigned mtime) { |
| 347 | if (!SendRequest(ID_SEND, path_and_mode)) { |
| 348 | Error("failed to send ID_SEND message '%s': %s", path_and_mode, strerror(errno)); |
| 349 | return false; |
| 350 | } |
| 351 | |
| 352 | struct stat st; |
| 353 | if (stat(lpath, &st) == -1) { |
| 354 | Error("cannot stat '%s': %s", lpath, strerror(errno)); |
| 355 | return false; |
| 356 | } |
| 357 | |
| 358 | uint64_t total_size = st.st_size; |
| 359 | uint64_t bytes_copied = 0; |
| 360 | |
| 361 | int lfd = adb_open(lpath, O_RDONLY); |
| 362 | if (lfd < 0) { |
Elliott Hughes | cc65c3b | 2015-11-20 22:01:06 -0800 | [diff] [blame] | 363 | Error("opening '%s' locally failed: %s", lpath, strerror(errno)); |
Elliott Hughes | 6aab58c | 2015-11-20 17:35:17 -0800 | [diff] [blame] | 364 | return false; |
| 365 | } |
| 366 | |
| 367 | syncsendbuf sbuf; |
| 368 | sbuf.id = ID_DATA; |
| 369 | while (true) { |
Elliott Hughes | cc65c3b | 2015-11-20 22:01:06 -0800 | [diff] [blame] | 370 | int bytes_read = adb_read(lfd, sbuf.data, max); |
| 371 | if (bytes_read == -1) { |
| 372 | Error("reading '%s' locally failed: %s", lpath, strerror(errno)); |
| 373 | adb_close(lfd); |
| 374 | return false; |
| 375 | } else if (bytes_read == 0) { |
Elliott Hughes | 6aab58c | 2015-11-20 17:35:17 -0800 | [diff] [blame] | 376 | break; |
| 377 | } |
| 378 | |
Elliott Hughes | cc65c3b | 2015-11-20 22:01:06 -0800 | [diff] [blame] | 379 | sbuf.size = bytes_read; |
| 380 | WriteOrDie(lpath, rpath, &sbuf, sizeof(SyncRequest) + bytes_read); |
Elliott Hughes | 6aab58c | 2015-11-20 17:35:17 -0800 | [diff] [blame] | 381 | |
Josh Gao | fb0c5cb | 2016-08-04 14:53:17 -0700 | [diff] [blame] | 382 | RecordBytesTransferred(bytes_read); |
Elliott Hughes | cc65c3b | 2015-11-20 22:01:06 -0800 | [diff] [blame] | 383 | bytes_copied += bytes_read; |
Elliott Hughes | 6aab58c | 2015-11-20 17:35:17 -0800 | [diff] [blame] | 384 | |
Josh Gao | afcdcd7 | 2016-02-19 15:55:55 -0800 | [diff] [blame] | 385 | // Check to see if we've received an error from the other side. |
| 386 | if (ReceivedError(lpath, rpath)) { |
| 387 | break; |
| 388 | } |
| 389 | |
Elliott Hughes | a00e6ef | 2015-12-17 17:05:29 -0800 | [diff] [blame] | 390 | ReportProgress(rpath, bytes_copied, total_size); |
Elliott Hughes | 6aab58c | 2015-11-20 17:35:17 -0800 | [diff] [blame] | 391 | } |
| 392 | |
| 393 | adb_close(lfd); |
| 394 | |
| 395 | syncmsg msg; |
| 396 | msg.data.id = ID_DONE; |
| 397 | msg.data.size = mtime; |
Josh Gao | afcdcd7 | 2016-02-19 15:55:55 -0800 | [diff] [blame] | 398 | expect_done_ = true; |
Josh Gao | fb0c5cb | 2016-08-04 14:53:17 -0700 | [diff] [blame] | 399 | |
| 400 | // RecordFilesTransferred gets called in CopyDone. |
Elliott Hughes | cc65c3b | 2015-11-20 22:01:06 -0800 | [diff] [blame] | 401 | return WriteOrDie(lpath, rpath, &msg.data, sizeof(msg.data)); |
Elliott Hughes | 6aab58c | 2015-11-20 17:35:17 -0800 | [diff] [blame] | 402 | } |
| 403 | |
Elliott Hughes | ae5a6c0 | 2015-09-27 12:55:37 -0700 | [diff] [blame] | 404 | bool CopyDone(const char* from, const char* to) { |
| 405 | syncmsg msg; |
| 406 | if (!ReadFdExactly(fd, &msg.status, sizeof(msg.status))) { |
Josh Gao | afcdcd7 | 2016-02-19 15:55:55 -0800 | [diff] [blame] | 407 | Error("failed to copy '%s' to '%s': couldn't read from device", from, to); |
Elliott Hughes | ae5a6c0 | 2015-09-27 12:55:37 -0700 | [diff] [blame] | 408 | return false; |
| 409 | } |
| 410 | if (msg.status.id == ID_OKAY) { |
Josh Gao | afcdcd7 | 2016-02-19 15:55:55 -0800 | [diff] [blame] | 411 | if (expect_done_) { |
| 412 | expect_done_ = false; |
Josh Gao | fb0c5cb | 2016-08-04 14:53:17 -0700 | [diff] [blame] | 413 | RecordFilesTransferred(1); |
Josh Gao | afcdcd7 | 2016-02-19 15:55:55 -0800 | [diff] [blame] | 414 | return true; |
| 415 | } else { |
| 416 | Error("failed to copy '%s' to '%s': received premature success", from, to); |
| 417 | return true; |
| 418 | } |
Elliott Hughes | ae5a6c0 | 2015-09-27 12:55:37 -0700 | [diff] [blame] | 419 | } |
| 420 | if (msg.status.id != ID_FAIL) { |
Elliott Hughes | b708d16 | 2015-10-27 16:03:15 -0700 | [diff] [blame] | 421 | Error("failed to copy '%s' to '%s': unknown reason %d", from, to, msg.status.id); |
Elliott Hughes | ae5a6c0 | 2015-09-27 12:55:37 -0700 | [diff] [blame] | 422 | return false; |
| 423 | } |
Elliott Hughes | 8b43c3e | 2015-10-23 21:06:11 -0700 | [diff] [blame] | 424 | return ReportCopyFailure(from, to, msg); |
| 425 | } |
| 426 | |
| 427 | bool ReportCopyFailure(const char* from, const char* to, const syncmsg& msg) { |
Elliott Hughes | 6d92997 | 2015-10-27 13:40:35 -0700 | [diff] [blame] | 428 | std::vector<char> buf(msg.status.msglen + 1); |
| 429 | if (!ReadFdExactly(fd, &buf[0], msg.status.msglen)) { |
Elliott Hughes | b708d16 | 2015-10-27 16:03:15 -0700 | [diff] [blame] | 430 | Error("failed to copy '%s' to '%s'; failed to read reason (!): %s", |
| 431 | from, to, strerror(errno)); |
Elliott Hughes | ae5a6c0 | 2015-09-27 12:55:37 -0700 | [diff] [blame] | 432 | return false; |
| 433 | } |
Elliott Hughes | 6d92997 | 2015-10-27 13:40:35 -0700 | [diff] [blame] | 434 | buf[msg.status.msglen] = 0; |
Elliott Hughes | b708d16 | 2015-10-27 16:03:15 -0700 | [diff] [blame] | 435 | Error("failed to copy '%s' to '%s': %s", from, to, &buf[0]); |
Elliott Hughes | ae5a6c0 | 2015-09-27 12:55:37 -0700 | [diff] [blame] | 436 | return false; |
| 437 | } |
| 438 | |
Elliott Hughes | b708d16 | 2015-10-27 16:03:15 -0700 | [diff] [blame] | 439 | |
Josh Gao | 983c41c | 2015-11-02 17:15:57 -0800 | [diff] [blame] | 440 | void Printf(const char* fmt, ...) __attribute__((__format__(ADB_FORMAT_ARCHETYPE, 2, 3))) { |
| 441 | std::string s; |
Elliott Hughes | a00e6ef | 2015-12-17 17:05:29 -0800 | [diff] [blame] | 442 | |
Josh Gao | 983c41c | 2015-11-02 17:15:57 -0800 | [diff] [blame] | 443 | va_list ap; |
| 444 | va_start(ap, fmt); |
| 445 | android::base::StringAppendV(&s, fmt, ap); |
| 446 | va_end(ap); |
| 447 | |
Elliott Hughes | a00e6ef | 2015-12-17 17:05:29 -0800 | [diff] [blame] | 448 | line_printer_.Print(s, LinePrinter::INFO); |
Josh Gao | 983c41c | 2015-11-02 17:15:57 -0800 | [diff] [blame] | 449 | } |
| 450 | |
Josh Gao | 1a9979e | 2016-08-04 11:43:00 -0700 | [diff] [blame] | 451 | void Println(const char* fmt, ...) __attribute__((__format__(ADB_FORMAT_ARCHETYPE, 2, 3))) { |
| 452 | std::string s; |
| 453 | |
| 454 | va_list ap; |
| 455 | va_start(ap, fmt); |
| 456 | android::base::StringAppendV(&s, fmt, ap); |
| 457 | va_end(ap); |
| 458 | |
| 459 | line_printer_.Print(s, LinePrinter::INFO); |
| 460 | line_printer_.KeepInfoLine(); |
| 461 | } |
| 462 | |
Elliott Hughes | b708d16 | 2015-10-27 16:03:15 -0700 | [diff] [blame] | 463 | void Error(const char* fmt, ...) __attribute__((__format__(ADB_FORMAT_ARCHETYPE, 2, 3))) { |
| 464 | std::string s = "adb: error: "; |
| 465 | |
| 466 | va_list ap; |
| 467 | va_start(ap, fmt); |
| 468 | android::base::StringAppendV(&s, fmt, ap); |
| 469 | va_end(ap); |
| 470 | |
Elliott Hughes | 77f539a | 2015-12-08 16:01:15 -0800 | [diff] [blame] | 471 | line_printer_.Print(s, LinePrinter::ERROR); |
Elliott Hughes | b708d16 | 2015-10-27 16:03:15 -0700 | [diff] [blame] | 472 | } |
| 473 | |
Josh Gao | 21abf5a | 2015-11-07 17:18:44 -0800 | [diff] [blame] | 474 | void Warning(const char* fmt, ...) __attribute__((__format__(ADB_FORMAT_ARCHETYPE, 2, 3))) { |
| 475 | std::string s = "adb: warning: "; |
| 476 | |
| 477 | va_list ap; |
| 478 | va_start(ap, fmt); |
| 479 | android::base::StringAppendV(&s, fmt, ap); |
| 480 | va_end(ap); |
| 481 | |
Elliott Hughes | 77f539a | 2015-12-08 16:01:15 -0800 | [diff] [blame] | 482 | line_printer_.Print(s, LinePrinter::WARNING); |
Josh Gao | 21abf5a | 2015-11-07 17:18:44 -0800 | [diff] [blame] | 483 | } |
| 484 | |
Elliott Hughes | a00e6ef | 2015-12-17 17:05:29 -0800 | [diff] [blame] | 485 | void ComputeExpectedTotalBytes(const std::vector<copyinfo>& file_list) { |
Josh Gao | fb0c5cb | 2016-08-04 14:53:17 -0700 | [diff] [blame] | 486 | current_ledger_.bytes_expected = 0; |
Elliott Hughes | a00e6ef | 2015-12-17 17:05:29 -0800 | [diff] [blame] | 487 | for (const copyinfo& ci : file_list) { |
| 488 | // Unfortunately, this doesn't work for symbolic links, because we'll copy the |
| 489 | // target of the link rather than just creating a link. (But ci.size is the link size.) |
Josh Gao | fb0c5cb | 2016-08-04 14:53:17 -0700 | [diff] [blame] | 490 | if (!ci.skip) current_ledger_.bytes_expected += ci.size; |
Elliott Hughes | a00e6ef | 2015-12-17 17:05:29 -0800 | [diff] [blame] | 491 | } |
Josh Gao | fb0c5cb | 2016-08-04 14:53:17 -0700 | [diff] [blame] | 492 | current_ledger_.expect_multiple_files = true; |
Elliott Hughes | a00e6ef | 2015-12-17 17:05:29 -0800 | [diff] [blame] | 493 | } |
| 494 | |
| 495 | void SetExpectedTotalBytes(uint64_t expected_total_bytes) { |
Josh Gao | fb0c5cb | 2016-08-04 14:53:17 -0700 | [diff] [blame] | 496 | current_ledger_.bytes_expected = expected_total_bytes; |
| 497 | current_ledger_.expect_multiple_files = false; |
Elliott Hughes | a00e6ef | 2015-12-17 17:05:29 -0800 | [diff] [blame] | 498 | } |
| 499 | |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 500 | // TODO: add a char[max] buffer here, to replace syncsendbuf... |
| 501 | int fd; |
| 502 | size_t max; |
| 503 | |
| 504 | private: |
Josh Gao | afcdcd7 | 2016-02-19 15:55:55 -0800 | [diff] [blame] | 505 | bool expect_done_; |
Elliott Hughes | a00e6ef | 2015-12-17 17:05:29 -0800 | [diff] [blame] | 506 | |
Josh Gao | fb0c5cb | 2016-08-04 14:53:17 -0700 | [diff] [blame] | 507 | TransferLedger global_ledger_; |
| 508 | TransferLedger current_ledger_; |
Elliott Hughes | b708d16 | 2015-10-27 16:03:15 -0700 | [diff] [blame] | 509 | LinePrinter line_printer_; |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 510 | |
Spencer Low | 351ecd1 | 2015-10-14 17:32:44 -0700 | [diff] [blame] | 511 | bool SendQuit() { |
| 512 | return SendRequest(ID_QUIT, ""); // TODO: add a SendResponse? |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 513 | } |
| 514 | |
Elliott Hughes | cc65c3b | 2015-11-20 22:01:06 -0800 | [diff] [blame] | 515 | bool WriteOrDie(const char* from, const char* to, const void* data, size_t data_length) { |
| 516 | if (!WriteFdExactly(fd, data, data_length)) { |
| 517 | if (errno == ECONNRESET) { |
| 518 | // Assume adbd told us why it was closing the connection, and |
| 519 | // try to read failure reason from adbd. |
| 520 | syncmsg msg; |
| 521 | if (!ReadFdExactly(fd, &msg.status, sizeof(msg.status))) { |
| 522 | Error("failed to copy '%s' to '%s': no response: %s", from, to, strerror(errno)); |
| 523 | } else if (msg.status.id != ID_FAIL) { |
| 524 | Error("failed to copy '%s' to '%s': not ID_FAIL: %d", from, to, msg.status.id); |
| 525 | } else { |
| 526 | ReportCopyFailure(from, to, msg); |
| 527 | } |
| 528 | } else { |
| 529 | Error("%zu-byte write failed: %s", data_length, strerror(errno)); |
| 530 | } |
| 531 | _exit(1); |
| 532 | } |
| 533 | return true; |
| 534 | } |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 535 | }; |
| 536 | |
Josh Gao | cda6a2b | 2015-11-02 16:45:47 -0800 | [diff] [blame] | 537 | typedef void (sync_ls_cb)(unsigned mode, unsigned size, unsigned time, const char* name); |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 538 | |
Josh Gao | cda6a2b | 2015-11-02 16:45:47 -0800 | [diff] [blame] | 539 | static bool sync_ls(SyncConnection& sc, const char* path, |
Chih-Hung Hsieh | 8f7b9e3 | 2016-07-27 16:25:51 -0700 | [diff] [blame] | 540 | const std::function<sync_ls_cb>& func) { |
Elliott Hughes | ae5a6c0 | 2015-09-27 12:55:37 -0700 | [diff] [blame] | 541 | if (!sc.SendRequest(ID_LIST, path)) return false; |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 542 | |
| 543 | while (true) { |
| 544 | syncmsg msg; |
Elliott Hughes | ae5a6c0 | 2015-09-27 12:55:37 -0700 | [diff] [blame] | 545 | if (!ReadFdExactly(sc.fd, &msg.dent, sizeof(msg.dent))) return false; |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 546 | |
| 547 | if (msg.dent.id == ID_DONE) return true; |
| 548 | if (msg.dent.id != ID_DENT) return false; |
| 549 | |
Elliott Hughes | f446520 | 2015-08-24 14:27:03 -0700 | [diff] [blame] | 550 | size_t len = msg.dent.namelen; |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 551 | if (len > 256) return false; // TODO: resize buffer? continue? |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 552 | |
Elliott Hughes | 5c74270 | 2015-07-30 17:42:01 -0700 | [diff] [blame] | 553 | char buf[257]; |
Elliott Hughes | ae5a6c0 | 2015-09-27 12:55:37 -0700 | [diff] [blame] | 554 | if (!ReadFdExactly(sc.fd, buf, len)) return false; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 555 | buf[len] = 0; |
| 556 | |
Josh Gao | cda6a2b | 2015-11-02 16:45:47 -0800 | [diff] [blame] | 557 | func(msg.dent.mode, msg.dent.size, msg.dent.time, buf); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 558 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 559 | } |
| 560 | |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 561 | static bool sync_finish_stat(SyncConnection& sc, unsigned int* timestamp, |
| 562 | unsigned int* mode, unsigned int* size) { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 563 | syncmsg msg; |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 564 | if (!ReadFdExactly(sc.fd, &msg.stat, sizeof(msg.stat)) || msg.stat.id != ID_STAT) { |
| 565 | return false; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 566 | } |
| 567 | |
Elliott Hughes | f446520 | 2015-08-24 14:27:03 -0700 | [diff] [blame] | 568 | if (timestamp) *timestamp = msg.stat.time; |
| 569 | if (mode) *mode = msg.stat.mode; |
| 570 | if (size) *size = msg.stat.size; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 571 | |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 572 | return true; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 573 | } |
| 574 | |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 575 | static bool sync_stat(SyncConnection& sc, const char* path, |
| 576 | unsigned int* timestamp, unsigned int* mode, unsigned int* size) { |
Elliott Hughes | ae5a6c0 | 2015-09-27 12:55:37 -0700 | [diff] [blame] | 577 | return sc.SendRequest(ID_STAT, path) && sync_finish_stat(sc, timestamp, mode, size); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 578 | } |
| 579 | |
Elliott Hughes | ae5a6c0 | 2015-09-27 12:55:37 -0700 | [diff] [blame] | 580 | static bool sync_send(SyncConnection& sc, const char* lpath, const char* rpath, |
Elliott Hughes | b708d16 | 2015-10-27 16:03:15 -0700 | [diff] [blame] | 581 | unsigned mtime, mode_t mode) |
Elliott Hughes | ae5a6c0 | 2015-09-27 12:55:37 -0700 | [diff] [blame] | 582 | { |
| 583 | std::string path_and_mode = android::base::StringPrintf("%s,%d", rpath, mode); |
| 584 | |
| 585 | if (S_ISLNK(mode)) { |
| 586 | #if !defined(_WIN32) |
| 587 | char buf[PATH_MAX]; |
| 588 | ssize_t data_length = readlink(lpath, buf, PATH_MAX - 1); |
| 589 | if (data_length == -1) { |
Elliott Hughes | b708d16 | 2015-10-27 16:03:15 -0700 | [diff] [blame] | 590 | sc.Error("readlink '%s' failed: %s", lpath, strerror(errno)); |
Elliott Hughes | ae5a6c0 | 2015-09-27 12:55:37 -0700 | [diff] [blame] | 591 | return false; |
| 592 | } |
| 593 | buf[data_length++] = '\0'; |
| 594 | |
Elliott Hughes | cc65c3b | 2015-11-20 22:01:06 -0800 | [diff] [blame] | 595 | if (!sc.SendSmallFile(path_and_mode.c_str(), lpath, rpath, mtime, buf, data_length)) { |
| 596 | return false; |
| 597 | } |
Elliott Hughes | ae5a6c0 | 2015-09-27 12:55:37 -0700 | [diff] [blame] | 598 | return sc.CopyDone(lpath, rpath); |
| 599 | #endif |
| 600 | } |
| 601 | |
Elliott Hughes | ae5a6c0 | 2015-09-27 12:55:37 -0700 | [diff] [blame] | 602 | struct stat st; |
| 603 | if (stat(lpath, &st) == -1) { |
Elliott Hughes | b708d16 | 2015-10-27 16:03:15 -0700 | [diff] [blame] | 604 | sc.Error("failed to stat local file '%s': %s", lpath, strerror(errno)); |
Elliott Hughes | ae5a6c0 | 2015-09-27 12:55:37 -0700 | [diff] [blame] | 605 | return false; |
| 606 | } |
| 607 | if (st.st_size < SYNC_DATA_MAX) { |
| 608 | std::string data; |
| 609 | if (!android::base::ReadFileToString(lpath, &data)) { |
Elliott Hughes | b708d16 | 2015-10-27 16:03:15 -0700 | [diff] [blame] | 610 | sc.Error("failed to read all of '%s': %s", lpath, strerror(errno)); |
Elliott Hughes | ae5a6c0 | 2015-09-27 12:55:37 -0700 | [diff] [blame] | 611 | return false; |
| 612 | } |
Elliott Hughes | cc65c3b | 2015-11-20 22:01:06 -0800 | [diff] [blame] | 613 | if (!sc.SendSmallFile(path_and_mode.c_str(), lpath, rpath, mtime, |
| 614 | data.data(), data.size())) { |
Elliott Hughes | b708d16 | 2015-10-27 16:03:15 -0700 | [diff] [blame] | 615 | return false; |
| 616 | } |
Elliott Hughes | ae5a6c0 | 2015-09-27 12:55:37 -0700 | [diff] [blame] | 617 | } else { |
Elliott Hughes | 6aab58c | 2015-11-20 17:35:17 -0800 | [diff] [blame] | 618 | if (!sc.SendLargeFile(path_and_mode.c_str(), lpath, rpath, mtime)) { |
Elliott Hughes | b708d16 | 2015-10-27 16:03:15 -0700 | [diff] [blame] | 619 | return false; |
| 620 | } |
Elliott Hughes | ae5a6c0 | 2015-09-27 12:55:37 -0700 | [diff] [blame] | 621 | } |
| 622 | return sc.CopyDone(lpath, rpath); |
| 623 | } |
| 624 | |
Elliott Hughes | b708d16 | 2015-10-27 16:03:15 -0700 | [diff] [blame] | 625 | static bool sync_recv(SyncConnection& sc, const char* rpath, const char* lpath) { |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 626 | unsigned size = 0; |
Elliott Hughes | b708d16 | 2015-10-27 16:03:15 -0700 | [diff] [blame] | 627 | if (!sync_stat(sc, rpath, nullptr, nullptr, &size)) return false; |
Mark Lindner | 76f2a93 | 2014-03-11 17:55:59 -0700 | [diff] [blame] | 628 | |
Elliott Hughes | ae5a6c0 | 2015-09-27 12:55:37 -0700 | [diff] [blame] | 629 | if (!sc.SendRequest(ID_RECV, rpath)) return false; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 630 | |
Elliott Hughes | 8b43c3e | 2015-10-23 21:06:11 -0700 | [diff] [blame] | 631 | adb_unlink(lpath); |
Elliott Hughes | 8b43c3e | 2015-10-23 21:06:11 -0700 | [diff] [blame] | 632 | int lfd = adb_creat(lpath, 0644); |
| 633 | if (lfd < 0) { |
Elliott Hughes | b708d16 | 2015-10-27 16:03:15 -0700 | [diff] [blame] | 634 | sc.Error("cannot create '%s': %s", lpath, strerror(errno)); |
Elliott Hughes | 8b43c3e | 2015-10-23 21:06:11 -0700 | [diff] [blame] | 635 | return false; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 636 | } |
| 637 | |
Elliott Hughes | b708d16 | 2015-10-27 16:03:15 -0700 | [diff] [blame] | 638 | uint64_t bytes_copied = 0; |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 639 | while (true) { |
Elliott Hughes | 8b43c3e | 2015-10-23 21:06:11 -0700 | [diff] [blame] | 640 | syncmsg msg; |
Elliott Hughes | ae5a6c0 | 2015-09-27 12:55:37 -0700 | [diff] [blame] | 641 | if (!ReadFdExactly(sc.fd, &msg.data, sizeof(msg.data))) { |
Spencer Low | d8cce18 | 2015-08-28 01:07:30 -0700 | [diff] [blame] | 642 | adb_close(lfd); |
Elliott Hughes | 8b43c3e | 2015-10-23 21:06:11 -0700 | [diff] [blame] | 643 | adb_unlink(lpath); |
Elliott Hughes | ae5a6c0 | 2015-09-27 12:55:37 -0700 | [diff] [blame] | 644 | return false; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 645 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 646 | |
Elliott Hughes | 8b43c3e | 2015-10-23 21:06:11 -0700 | [diff] [blame] | 647 | if (msg.data.id == ID_DONE) break; |
| 648 | |
| 649 | if (msg.data.id != ID_DATA) { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 650 | adb_close(lfd); |
Elliott Hughes | 8b43c3e | 2015-10-23 21:06:11 -0700 | [diff] [blame] | 651 | adb_unlink(lpath); |
| 652 | sc.ReportCopyFailure(rpath, lpath, msg); |
Elliott Hughes | ae5a6c0 | 2015-09-27 12:55:37 -0700 | [diff] [blame] | 653 | return false; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 654 | } |
| 655 | |
Elliott Hughes | 8b43c3e | 2015-10-23 21:06:11 -0700 | [diff] [blame] | 656 | if (msg.data.size > sc.max) { |
Elliott Hughes | b708d16 | 2015-10-27 16:03:15 -0700 | [diff] [blame] | 657 | sc.Error("msg.data.size too large: %u (max %zu)", msg.data.size, sc.max); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 658 | adb_close(lfd); |
Elliott Hughes | 8b43c3e | 2015-10-23 21:06:11 -0700 | [diff] [blame] | 659 | adb_unlink(lpath); |
Elliott Hughes | ae5a6c0 | 2015-09-27 12:55:37 -0700 | [diff] [blame] | 660 | return false; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 661 | } |
| 662 | |
Elliott Hughes | 8b43c3e | 2015-10-23 21:06:11 -0700 | [diff] [blame] | 663 | char buffer[SYNC_DATA_MAX]; |
| 664 | if (!ReadFdExactly(sc.fd, buffer, msg.data.size)) { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 665 | adb_close(lfd); |
Elliott Hughes | 8b43c3e | 2015-10-23 21:06:11 -0700 | [diff] [blame] | 666 | adb_unlink(lpath); |
Elliott Hughes | ae5a6c0 | 2015-09-27 12:55:37 -0700 | [diff] [blame] | 667 | return false; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 668 | } |
| 669 | |
Elliott Hughes | 8b43c3e | 2015-10-23 21:06:11 -0700 | [diff] [blame] | 670 | if (!WriteFdExactly(lfd, buffer, msg.data.size)) { |
Elliott Hughes | b708d16 | 2015-10-27 16:03:15 -0700 | [diff] [blame] | 671 | sc.Error("cannot write '%s': %s", lpath, strerror(errno)); |
Elliott Hughes | 8b43c3e | 2015-10-23 21:06:11 -0700 | [diff] [blame] | 672 | adb_close(lfd); |
| 673 | adb_unlink(lpath); |
| 674 | return false; |
| 675 | } |
| 676 | |
Elliott Hughes | b708d16 | 2015-10-27 16:03:15 -0700 | [diff] [blame] | 677 | bytes_copied += msg.data.size; |
Josh Gao | fb0c5cb | 2016-08-04 14:53:17 -0700 | [diff] [blame] | 678 | sc.RecordBytesTransferred(msg.data.size); |
Elliott Hughes | a00e6ef | 2015-12-17 17:05:29 -0800 | [diff] [blame] | 679 | sc.ReportProgress(rpath, bytes_copied, size); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 680 | } |
| 681 | |
Josh Gao | fb0c5cb | 2016-08-04 14:53:17 -0700 | [diff] [blame] | 682 | sc.RecordFilesTransferred(1); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 683 | adb_close(lfd); |
Elliott Hughes | ae5a6c0 | 2015-09-27 12:55:37 -0700 | [diff] [blame] | 684 | return true; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 685 | } |
| 686 | |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 687 | bool do_sync_ls(const char* path) { |
| 688 | SyncConnection sc; |
| 689 | if (!sc.IsValid()) return false; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 690 | |
Josh Gao | cda6a2b | 2015-11-02 16:45:47 -0800 | [diff] [blame] | 691 | return sync_ls(sc, path, [](unsigned mode, unsigned size, unsigned time, |
| 692 | const char* name) { |
| 693 | printf("%08x %08x %08x %s\n", mode, size, time, name); |
| 694 | }); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 695 | } |
| 696 | |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 697 | static bool IsDotOrDotDot(const char* name) { |
| 698 | return name[0] == '.' && (name[1] == '\0' || (name[1] == '.' && name[2] == '\0')); |
| 699 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 700 | |
Elliott Hughes | a00e6ef | 2015-12-17 17:05:29 -0800 | [diff] [blame] | 701 | static bool local_build_list(SyncConnection& sc, std::vector<copyinfo>* file_list, |
Josh Gao | cd7c1ed | 2015-11-03 15:26:38 -0800 | [diff] [blame] | 702 | const std::string& lpath, |
| 703 | const std::string& rpath) { |
Josh Gao | cda6a2b | 2015-11-02 16:45:47 -0800 | [diff] [blame] | 704 | std::vector<copyinfo> dirlist; |
Josh Gao | d973157 | 2015-11-03 15:23:03 -0800 | [diff] [blame] | 705 | std::unique_ptr<DIR, int (*)(DIR*)> dir(opendir(lpath.c_str()), closedir); |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 706 | if (!dir) { |
Josh Gao | d973157 | 2015-11-03 15:23:03 -0800 | [diff] [blame] | 707 | sc.Error("cannot open '%s': %s", lpath.c_str(), strerror(errno)); |
Josh Gao | cd7c1ed | 2015-11-03 15:26:38 -0800 | [diff] [blame] | 708 | return false; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 709 | } |
| 710 | |
Josh Gao | 6580096 | 2015-11-04 14:57:04 -0800 | [diff] [blame] | 711 | bool empty_dir = true; |
Elliott Hughes | b708d16 | 2015-10-27 16:03:15 -0700 | [diff] [blame] | 712 | dirent* de; |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 713 | while ((de = readdir(dir.get()))) { |
Josh Gao | 6580096 | 2015-11-04 14:57:04 -0800 | [diff] [blame] | 714 | if (IsDotOrDotDot(de->d_name)) { |
| 715 | continue; |
| 716 | } |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 717 | |
Josh Gao | 6580096 | 2015-11-04 14:57:04 -0800 | [diff] [blame] | 718 | empty_dir = false; |
Josh Gao | d973157 | 2015-11-03 15:23:03 -0800 | [diff] [blame] | 719 | std::string stat_path = lpath + de->d_name; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 720 | |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 721 | struct stat st; |
Josh Gao | 12a2ae9 | 2015-11-07 15:27:26 -0800 | [diff] [blame] | 722 | if (lstat(stat_path.c_str(), &st) == -1) { |
Josh Gao | d973157 | 2015-11-03 15:23:03 -0800 | [diff] [blame] | 723 | sc.Error("cannot lstat '%s': %s", stat_path.c_str(), |
| 724 | strerror(errno)); |
Josh Gao | 12a2ae9 | 2015-11-07 15:27:26 -0800 | [diff] [blame] | 725 | continue; |
| 726 | } |
| 727 | |
Josh Gao | f264224 | 2015-12-09 14:03:30 -0800 | [diff] [blame] | 728 | copyinfo ci(lpath, rpath, de->d_name, st.st_mode); |
Josh Gao | 12a2ae9 | 2015-11-07 15:27:26 -0800 | [diff] [blame] | 729 | if (S_ISDIR(st.st_mode)) { |
| 730 | dirlist.push_back(ci); |
| 731 | } else { |
Josh Gao | a31ea55 | 2016-03-02 16:11:13 -0800 | [diff] [blame] | 732 | if (!should_push_file(st.st_mode)) { |
| 733 | sc.Warning("skipping special file '%s' (mode = 0o%o)", lpath.c_str(), st.st_mode); |
Josh Gao | dd6cc4d | 2015-11-30 10:21:25 -0800 | [diff] [blame] | 734 | ci.skip = true; |
Josh Gao | 12a2ae9 | 2015-11-07 15:27:26 -0800 | [diff] [blame] | 735 | } |
Josh Gao | a31ea55 | 2016-03-02 16:11:13 -0800 | [diff] [blame] | 736 | ci.time = st.st_mtime; |
| 737 | ci.size = st.st_size; |
Elliott Hughes | a00e6ef | 2015-12-17 17:05:29 -0800 | [diff] [blame] | 738 | file_list->push_back(ci); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 739 | } |
| 740 | } |
| 741 | |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 742 | // Close this directory and recurse. |
| 743 | dir.reset(); |
Josh Gao | 6580096 | 2015-11-04 14:57:04 -0800 | [diff] [blame] | 744 | |
| 745 | // Add the current directory to the list if it was empty, to ensure that |
| 746 | // it gets created. |
| 747 | if (empty_dir) { |
| 748 | // TODO(b/25566053): Make pushing empty directories work. |
| 749 | // TODO(b/25457350): We don't preserve permissions on directories. |
Josh Gao | 21abf5a | 2015-11-07 17:18:44 -0800 | [diff] [blame] | 750 | sc.Warning("skipping empty directory '%s'", lpath.c_str()); |
Josh Gao | f264224 | 2015-12-09 14:03:30 -0800 | [diff] [blame] | 751 | copyinfo ci(adb_dirname(lpath), adb_dirname(rpath), adb_basename(lpath), S_IFDIR); |
Josh Gao | 6580096 | 2015-11-04 14:57:04 -0800 | [diff] [blame] | 752 | ci.skip = true; |
Elliott Hughes | a00e6ef | 2015-12-17 17:05:29 -0800 | [diff] [blame] | 753 | file_list->push_back(ci); |
Josh Gao | 6580096 | 2015-11-04 14:57:04 -0800 | [diff] [blame] | 754 | return true; |
| 755 | } |
| 756 | |
Josh Gao | cda6a2b | 2015-11-02 16:45:47 -0800 | [diff] [blame] | 757 | for (const copyinfo& ci : dirlist) { |
Elliott Hughes | a00e6ef | 2015-12-17 17:05:29 -0800 | [diff] [blame] | 758 | local_build_list(sc, file_list, ci.lpath, ci.rpath); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 759 | } |
| 760 | |
Josh Gao | cd7c1ed | 2015-11-03 15:26:38 -0800 | [diff] [blame] | 761 | return true; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 762 | } |
| 763 | |
Josh Gao | d973157 | 2015-11-03 15:23:03 -0800 | [diff] [blame] | 764 | static bool copy_local_dir_remote(SyncConnection& sc, std::string lpath, |
| 765 | std::string rpath, bool check_timestamps, |
| 766 | bool list_only) { |
Josh Gao | fb0c5cb | 2016-08-04 14:53:17 -0700 | [diff] [blame] | 767 | sc.NewTransfer(); |
| 768 | |
Josh Gao | d973157 | 2015-11-03 15:23:03 -0800 | [diff] [blame] | 769 | // Make sure that both directory paths end in a slash. |
Josh Gao | 1a02530 | 2015-11-09 11:12:14 -0800 | [diff] [blame] | 770 | // Both paths are known to be nonempty, so we don't need to check. |
Elliott Hughes | a00e6ef | 2015-12-17 17:05:29 -0800 | [diff] [blame] | 771 | ensure_trailing_separators(lpath, rpath); |
Josh Gao | d973157 | 2015-11-03 15:23:03 -0800 | [diff] [blame] | 772 | |
| 773 | // Recursively build the list of files to copy. |
Elliott Hughes | a00e6ef | 2015-12-17 17:05:29 -0800 | [diff] [blame] | 774 | std::vector<copyinfo> file_list; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 775 | int skipped = 0; |
Elliott Hughes | a00e6ef | 2015-12-17 17:05:29 -0800 | [diff] [blame] | 776 | if (!local_build_list(sc, &file_list, lpath, rpath)) { |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 777 | return false; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 778 | } |
| 779 | |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 780 | if (check_timestamps) { |
Elliott Hughes | a00e6ef | 2015-12-17 17:05:29 -0800 | [diff] [blame] | 781 | for (const copyinfo& ci : file_list) { |
Josh Gao | 1a02530 | 2015-11-09 11:12:14 -0800 | [diff] [blame] | 782 | if (!sc.SendRequest(ID_STAT, ci.rpath.c_str())) { |
Josh Gao | d973157 | 2015-11-03 15:23:03 -0800 | [diff] [blame] | 783 | return false; |
| 784 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 785 | } |
Elliott Hughes | a00e6ef | 2015-12-17 17:05:29 -0800 | [diff] [blame] | 786 | for (copyinfo& ci : file_list) { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 787 | unsigned int timestamp, mode, size; |
Josh Gao | d973157 | 2015-11-03 15:23:03 -0800 | [diff] [blame] | 788 | if (!sync_finish_stat(sc, ×tamp, &mode, &size)) { |
| 789 | return false; |
| 790 | } |
Josh Gao | cda6a2b | 2015-11-02 16:45:47 -0800 | [diff] [blame] | 791 | if (size == ci.size) { |
Elliott Hughes | a00e6ef | 2015-12-17 17:05:29 -0800 | [diff] [blame] | 792 | // For links, we cannot update the atime/mtime. |
Josh Gao | cda6a2b | 2015-11-02 16:45:47 -0800 | [diff] [blame] | 793 | if ((S_ISREG(ci.mode & mode) && timestamp == ci.time) || |
| 794 | (S_ISLNK(ci.mode & mode) && timestamp >= ci.time)) { |
Josh Gao | fc7c3b6 | 2015-11-03 14:44:04 -0800 | [diff] [blame] | 795 | ci.skip = true; |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 796 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 797 | } |
| 798 | } |
| 799 | } |
Josh Gao | cda6a2b | 2015-11-02 16:45:47 -0800 | [diff] [blame] | 800 | |
Elliott Hughes | a00e6ef | 2015-12-17 17:05:29 -0800 | [diff] [blame] | 801 | sc.ComputeExpectedTotalBytes(file_list); |
| 802 | |
| 803 | for (const copyinfo& ci : file_list) { |
Josh Gao | fc7c3b6 | 2015-11-03 14:44:04 -0800 | [diff] [blame] | 804 | if (!ci.skip) { |
Elliott Hughes | b708d16 | 2015-10-27 16:03:15 -0700 | [diff] [blame] | 805 | if (list_only) { |
Josh Gao | baa215e | 2016-08-04 14:55:23 -0700 | [diff] [blame] | 806 | sc.Println("would push: %s -> %s", ci.lpath.c_str(), ci.rpath.c_str()); |
Elliott Hughes | b708d16 | 2015-10-27 16:03:15 -0700 | [diff] [blame] | 807 | } else { |
Elliott Hughes | a00e6ef | 2015-12-17 17:05:29 -0800 | [diff] [blame] | 808 | if (!sync_send(sc, ci.lpath.c_str(), ci.rpath.c_str(), ci.time, ci.mode)) { |
Josh Gao | d973157 | 2015-11-03 15:23:03 -0800 | [diff] [blame] | 809 | return false; |
Elliott Hughes | b708d16 | 2015-10-27 16:03:15 -0700 | [diff] [blame] | 810 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 811 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 812 | } else { |
| 813 | skipped++; |
| 814 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 815 | } |
| 816 | |
Josh Gao | fb0c5cb | 2016-08-04 14:53:17 -0700 | [diff] [blame] | 817 | sc.RecordFilesSkipped(skipped); |
| 818 | sc.ReportTransferRate(lpath, TransferDirection::push); |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 819 | return true; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 820 | } |
| 821 | |
Josh Gao | 0578677 | 2015-10-30 16:57:19 -0700 | [diff] [blame] | 822 | bool do_sync_push(const std::vector<const char*>& srcs, const char* dst) { |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 823 | SyncConnection sc; |
| 824 | if (!sc.IsValid()) return false; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 825 | |
Josh Gao | 0578677 | 2015-10-30 16:57:19 -0700 | [diff] [blame] | 826 | bool success = true; |
Josh Gao | 12a2ae9 | 2015-11-07 15:27:26 -0800 | [diff] [blame] | 827 | unsigned dst_mode; |
| 828 | if (!sync_stat(sc, dst, nullptr, &dst_mode, nullptr)) return false; |
| 829 | bool dst_exists = (dst_mode != 0); |
Josh Gao | 07db119 | 2015-11-07 15:38:19 -0800 | [diff] [blame] | 830 | bool dst_isdir = S_ISDIR(dst_mode); |
Josh Gao | 0578677 | 2015-10-30 16:57:19 -0700 | [diff] [blame] | 831 | |
Josh Gao | 07db119 | 2015-11-07 15:38:19 -0800 | [diff] [blame] | 832 | if (!dst_isdir) { |
Josh Gao | 0578677 | 2015-10-30 16:57:19 -0700 | [diff] [blame] | 833 | if (srcs.size() > 1) { |
| 834 | sc.Error("target '%s' is not a directory", dst); |
| 835 | return false; |
| 836 | } else { |
| 837 | size_t dst_len = strlen(dst); |
Josh Gao | 12a2ae9 | 2015-11-07 15:27:26 -0800 | [diff] [blame] | 838 | |
| 839 | // A path that ends with a slash doesn't have to be a directory if |
| 840 | // it doesn't exist yet. |
| 841 | if (dst[dst_len - 1] == '/' && dst_exists) { |
Josh Gao | 0578677 | 2015-10-30 16:57:19 -0700 | [diff] [blame] | 842 | sc.Error("failed to access '%s': Not a directory", dst); |
| 843 | return false; |
| 844 | } |
| 845 | } |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 846 | } |
Josh Gao | 0578677 | 2015-10-30 16:57:19 -0700 | [diff] [blame] | 847 | |
| 848 | for (const char* src_path : srcs) { |
| 849 | const char* dst_path = dst; |
| 850 | struct stat st; |
Josh Gao | 12a2ae9 | 2015-11-07 15:27:26 -0800 | [diff] [blame] | 851 | if (stat(src_path, &st) == -1) { |
Josh Gao | 0578677 | 2015-10-30 16:57:19 -0700 | [diff] [blame] | 852 | sc.Error("cannot stat '%s': %s", src_path, strerror(errno)); |
| 853 | success = false; |
| 854 | continue; |
| 855 | } |
| 856 | |
| 857 | if (S_ISDIR(st.st_mode)) { |
Josh Gao | 07db119 | 2015-11-07 15:38:19 -0800 | [diff] [blame] | 858 | std::string dst_dir = dst; |
| 859 | |
| 860 | // If the destination path existed originally, the source directory |
| 861 | // should be copied as a child of the destination. |
| 862 | if (dst_exists) { |
| 863 | if (!dst_isdir) { |
| 864 | sc.Error("target '%s' is not a directory", dst); |
| 865 | return false; |
| 866 | } |
| 867 | // dst is a POSIX path, so we don't want to use the sysdeps |
| 868 | // helpers here. |
| 869 | if (dst_dir.back() != '/') { |
| 870 | dst_dir.push_back('/'); |
| 871 | } |
| 872 | dst_dir.append(adb_basename(src_path)); |
| 873 | } |
| 874 | |
| 875 | success &= copy_local_dir_remote(sc, src_path, dst_dir.c_str(), |
| 876 | false, false); |
Josh Gao | 0578677 | 2015-10-30 16:57:19 -0700 | [diff] [blame] | 877 | continue; |
Josh Gao | a31ea55 | 2016-03-02 16:11:13 -0800 | [diff] [blame] | 878 | } else if (!should_push_file(st.st_mode)) { |
| 879 | sc.Warning("skipping special file '%s' (mode = 0o%o)", src_path, st.st_mode); |
| 880 | continue; |
Josh Gao | 0578677 | 2015-10-30 16:57:19 -0700 | [diff] [blame] | 881 | } |
| 882 | |
| 883 | std::string path_holder; |
Josh Gao | 07db119 | 2015-11-07 15:38:19 -0800 | [diff] [blame] | 884 | if (dst_isdir) { |
Josh Gao | 0578677 | 2015-10-30 16:57:19 -0700 | [diff] [blame] | 885 | // If we're copying a local file to a remote directory, |
| 886 | // we really want to copy to remote_dir + "/" + local_filename. |
Josh Gao | 323899b | 2016-02-03 14:55:24 -0800 | [diff] [blame] | 887 | path_holder = dst_path; |
| 888 | if (path_holder.back() != '/') { |
| 889 | path_holder.push_back('/'); |
| 890 | } |
| 891 | path_holder += adb_basename(src_path); |
Josh Gao | 0578677 | 2015-10-30 16:57:19 -0700 | [diff] [blame] | 892 | dst_path = path_holder.c_str(); |
| 893 | } |
Josh Gao | fb0c5cb | 2016-08-04 14:53:17 -0700 | [diff] [blame] | 894 | |
| 895 | sc.NewTransfer(); |
Elliott Hughes | a00e6ef | 2015-12-17 17:05:29 -0800 | [diff] [blame] | 896 | sc.SetExpectedTotalBytes(st.st_size); |
Josh Gao | 0578677 | 2015-10-30 16:57:19 -0700 | [diff] [blame] | 897 | success &= sync_send(sc, src_path, dst_path, st.st_mtime, st.st_mode); |
Josh Gao | fb0c5cb | 2016-08-04 14:53:17 -0700 | [diff] [blame] | 898 | sc.ReportTransferRate(src_path, TransferDirection::push); |
Josh Gao | 0578677 | 2015-10-30 16:57:19 -0700 | [diff] [blame] | 899 | } |
| 900 | |
Josh Gao | fb0c5cb | 2016-08-04 14:53:17 -0700 | [diff] [blame] | 901 | sc.ReportOverallTransferRate(TransferDirection::push); |
Josh Gao | 0578677 | 2015-10-30 16:57:19 -0700 | [diff] [blame] | 902 | return success; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 903 | } |
| 904 | |
Josh Gao | f264224 | 2015-12-09 14:03:30 -0800 | [diff] [blame] | 905 | static bool remote_symlink_isdir(SyncConnection& sc, const std::string& rpath) { |
| 906 | unsigned mode; |
| 907 | std::string dir_path = rpath; |
| 908 | dir_path.push_back('/'); |
| 909 | if (!sync_stat(sc, dir_path.c_str(), nullptr, &mode, nullptr)) { |
| 910 | sc.Error("failed to stat remote symlink '%s'", dir_path.c_str()); |
| 911 | return false; |
| 912 | } |
| 913 | return S_ISDIR(mode); |
| 914 | } |
| 915 | |
Josh Gao | 71728ca | 2016-02-25 17:12:45 -0800 | [diff] [blame] | 916 | static bool remote_build_list(SyncConnection& sc, std::vector<copyinfo>* file_list, |
| 917 | const std::string& rpath, const std::string& lpath) { |
Josh Gao | cda6a2b | 2015-11-02 16:45:47 -0800 | [diff] [blame] | 918 | std::vector<copyinfo> dirlist; |
Josh Gao | f264224 | 2015-12-09 14:03:30 -0800 | [diff] [blame] | 919 | std::vector<copyinfo> linklist; |
Josh Gao | 71728ca | 2016-02-25 17:12:45 -0800 | [diff] [blame] | 920 | |
| 921 | // Add an entry for the current directory to ensure it gets created before pulling its contents. |
Josh Gao | 89ec3a8 | 2016-03-02 16:00:02 -0800 | [diff] [blame] | 922 | copyinfo ci(adb_dirname(lpath), adb_dirname(rpath), adb_basename(lpath), S_IFDIR); |
Josh Gao | 71728ca | 2016-02-25 17:12:45 -0800 | [diff] [blame] | 923 | file_list->push_back(ci); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 924 | |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 925 | // Put the files/dirs in rpath on the lists. |
Josh Gao | dd6cc4d | 2015-11-30 10:21:25 -0800 | [diff] [blame] | 926 | auto callback = [&](unsigned mode, unsigned size, unsigned time, const char* name) { |
Josh Gao | 6580096 | 2015-11-04 14:57:04 -0800 | [diff] [blame] | 927 | if (IsDotOrDotDot(name)) { |
| 928 | return; |
| 929 | } |
Josh Gao | cda6a2b | 2015-11-02 16:45:47 -0800 | [diff] [blame] | 930 | |
Josh Gao | f264224 | 2015-12-09 14:03:30 -0800 | [diff] [blame] | 931 | copyinfo ci(lpath, rpath, name, mode); |
Josh Gao | 6580096 | 2015-11-04 14:57:04 -0800 | [diff] [blame] | 932 | if (S_ISDIR(mode)) { |
| 933 | dirlist.push_back(ci); |
Josh Gao | f264224 | 2015-12-09 14:03:30 -0800 | [diff] [blame] | 934 | } else if (S_ISLNK(mode)) { |
| 935 | linklist.push_back(ci); |
Josh Gao | cda6a2b | 2015-11-02 16:45:47 -0800 | [diff] [blame] | 936 | } else { |
Josh Gao | a31ea55 | 2016-03-02 16:11:13 -0800 | [diff] [blame] | 937 | if (!should_pull_file(ci.mode)) { |
| 938 | sc.Warning("skipping special file '%s' (mode = 0o%o)", ci.rpath.c_str(), ci.mode); |
| 939 | ci.skip = true; |
| 940 | } |
Josh Gao | f264224 | 2015-12-09 14:03:30 -0800 | [diff] [blame] | 941 | ci.time = time; |
| 942 | ci.size = size; |
Elliott Hughes | a00e6ef | 2015-12-17 17:05:29 -0800 | [diff] [blame] | 943 | file_list->push_back(ci); |
Josh Gao | cda6a2b | 2015-11-02 16:45:47 -0800 | [diff] [blame] | 944 | } |
| 945 | }; |
| 946 | |
Josh Gao | d973157 | 2015-11-03 15:23:03 -0800 | [diff] [blame] | 947 | if (!sync_ls(sc, rpath.c_str(), callback)) { |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 948 | return false; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 949 | } |
| 950 | |
Josh Gao | f264224 | 2015-12-09 14:03:30 -0800 | [diff] [blame] | 951 | // Check each symlink we found to see whether it's a file or directory. |
| 952 | for (copyinfo& link_ci : linklist) { |
| 953 | if (remote_symlink_isdir(sc, link_ci.rpath)) { |
| 954 | dirlist.emplace_back(std::move(link_ci)); |
| 955 | } else { |
Elliott Hughes | a00e6ef | 2015-12-17 17:05:29 -0800 | [diff] [blame] | 956 | file_list->emplace_back(std::move(link_ci)); |
Josh Gao | f264224 | 2015-12-09 14:03:30 -0800 | [diff] [blame] | 957 | } |
| 958 | } |
| 959 | |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 960 | // Recurse into each directory we found. |
Josh Gao | cda6a2b | 2015-11-02 16:45:47 -0800 | [diff] [blame] | 961 | while (!dirlist.empty()) { |
| 962 | copyinfo current = dirlist.back(); |
| 963 | dirlist.pop_back(); |
Elliott Hughes | a00e6ef | 2015-12-17 17:05:29 -0800 | [diff] [blame] | 964 | if (!remote_build_list(sc, file_list, current.rpath, current.lpath)) { |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 965 | return false; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 966 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 967 | } |
| 968 | |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 969 | return true; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 970 | } |
| 971 | |
Josh Gao | 1a02530 | 2015-11-09 11:12:14 -0800 | [diff] [blame] | 972 | static int set_time_and_mode(const std::string& lpath, time_t time, |
| 973 | unsigned int mode) { |
Greg Hackmann | 7a5e2bd | 2014-05-06 08:48:18 -0700 | [diff] [blame] | 974 | struct utimbuf times = { time, time }; |
Josh Gao | 1a02530 | 2015-11-09 11:12:14 -0800 | [diff] [blame] | 975 | int r1 = utime(lpath.c_str(), ×); |
Lajos Molnar | de8ff4a | 2013-04-19 12:41:09 -0700 | [diff] [blame] | 976 | |
| 977 | /* use umask for permissions */ |
Josh Gao | cda6a2b | 2015-11-02 16:45:47 -0800 | [diff] [blame] | 978 | mode_t mask = umask(0000); |
Lajos Molnar | de8ff4a | 2013-04-19 12:41:09 -0700 | [diff] [blame] | 979 | umask(mask); |
Josh Gao | 1a02530 | 2015-11-09 11:12:14 -0800 | [diff] [blame] | 980 | int r2 = chmod(lpath.c_str(), mode & ~mask); |
Lajos Molnar | de8ff4a | 2013-04-19 12:41:09 -0700 | [diff] [blame] | 981 | |
Spencer Low | 363af56 | 2015-11-07 18:51:54 -0800 | [diff] [blame] | 982 | return r1 ? r1 : r2; |
Lajos Molnar | de8ff4a | 2013-04-19 12:41:09 -0700 | [diff] [blame] | 983 | } |
| 984 | |
Josh Gao | d973157 | 2015-11-03 15:23:03 -0800 | [diff] [blame] | 985 | static bool copy_remote_dir_local(SyncConnection& sc, std::string rpath, |
| 986 | std::string lpath, bool copy_attrs) { |
Josh Gao | fb0c5cb | 2016-08-04 14:53:17 -0700 | [diff] [blame] | 987 | sc.NewTransfer(); |
| 988 | |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 989 | // Make sure that both directory paths end in a slash. |
Josh Gao | 12a2ae9 | 2015-11-07 15:27:26 -0800 | [diff] [blame] | 990 | // Both paths are known to be nonempty, so we don't need to check. |
Elliott Hughes | a00e6ef | 2015-12-17 17:05:29 -0800 | [diff] [blame] | 991 | ensure_trailing_separators(lpath, rpath); |
Riley Andrews | 4d04d24 | 2014-12-12 13:12:36 -0800 | [diff] [blame] | 992 | |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 993 | // Recursively build the list of files to copy. |
Elliott Hughes | a00e6ef | 2015-12-17 17:05:29 -0800 | [diff] [blame] | 994 | sc.Printf("pull: building file list..."); |
| 995 | std::vector<copyinfo> file_list; |
| 996 | if (!remote_build_list(sc, &file_list, rpath.c_str(), lpath.c_str())) { |
Josh Gao | cda6a2b | 2015-11-02 16:45:47 -0800 | [diff] [blame] | 997 | return false; |
| 998 | } |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 999 | |
Elliott Hughes | a00e6ef | 2015-12-17 17:05:29 -0800 | [diff] [blame] | 1000 | sc.ComputeExpectedTotalBytes(file_list); |
| 1001 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1002 | int skipped = 0; |
Elliott Hughes | a00e6ef | 2015-12-17 17:05:29 -0800 | [diff] [blame] | 1003 | for (const copyinfo &ci : file_list) { |
Josh Gao | fc7c3b6 | 2015-11-03 14:44:04 -0800 | [diff] [blame] | 1004 | if (!ci.skip) { |
Josh Gao | 6580096 | 2015-11-04 14:57:04 -0800 | [diff] [blame] | 1005 | if (S_ISDIR(ci.mode)) { |
| 1006 | // Entry is for an empty directory, create it and continue. |
| 1007 | // TODO(b/25457350): We don't preserve permissions on directories. |
Josh Gao | 1a02530 | 2015-11-09 11:12:14 -0800 | [diff] [blame] | 1008 | if (!mkdirs(ci.lpath)) { |
Josh Gao | 6580096 | 2015-11-04 14:57:04 -0800 | [diff] [blame] | 1009 | sc.Error("failed to create directory '%s': %s", |
Josh Gao | 1a02530 | 2015-11-09 11:12:14 -0800 | [diff] [blame] | 1010 | ci.lpath.c_str(), strerror(errno)); |
Josh Gao | 6580096 | 2015-11-04 14:57:04 -0800 | [diff] [blame] | 1011 | return false; |
| 1012 | } |
Josh Gao | 6580096 | 2015-11-04 14:57:04 -0800 | [diff] [blame] | 1013 | continue; |
| 1014 | } |
| 1015 | |
Josh Gao | 1a02530 | 2015-11-09 11:12:14 -0800 | [diff] [blame] | 1016 | if (!sync_recv(sc, ci.rpath.c_str(), ci.lpath.c_str())) { |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 1017 | return false; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1018 | } |
Lajos Molnar | de8ff4a | 2013-04-19 12:41:09 -0700 | [diff] [blame] | 1019 | |
Josh Gao | 1a02530 | 2015-11-09 11:12:14 -0800 | [diff] [blame] | 1020 | if (copy_attrs && set_time_and_mode(ci.lpath, ci.time, ci.mode)) { |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 1021 | return false; |
Lajos Molnar | de8ff4a | 2013-04-19 12:41:09 -0700 | [diff] [blame] | 1022 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1023 | } else { |
| 1024 | skipped++; |
| 1025 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1026 | } |
| 1027 | |
Josh Gao | fb0c5cb | 2016-08-04 14:53:17 -0700 | [diff] [blame] | 1028 | sc.RecordFilesSkipped(skipped); |
| 1029 | sc.ReportTransferRate(rpath, TransferDirection::pull); |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 1030 | return true; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1031 | } |
| 1032 | |
Josh Gao | 0578677 | 2015-10-30 16:57:19 -0700 | [diff] [blame] | 1033 | bool do_sync_pull(const std::vector<const char*>& srcs, const char* dst, |
| 1034 | bool copy_attrs) { |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 1035 | SyncConnection sc; |
| 1036 | if (!sc.IsValid()) return false; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1037 | |
Josh Gao | 0578677 | 2015-10-30 16:57:19 -0700 | [diff] [blame] | 1038 | bool success = true; |
Josh Gao | 0578677 | 2015-10-30 16:57:19 -0700 | [diff] [blame] | 1039 | struct stat st; |
Josh Gao | 12a2ae9 | 2015-11-07 15:27:26 -0800 | [diff] [blame] | 1040 | bool dst_exists = true; |
| 1041 | |
| 1042 | if (stat(dst, &st) == -1) { |
| 1043 | dst_exists = false; |
| 1044 | |
| 1045 | // If we're only pulling one path, the destination path might point to |
Josh Gao | 0578677 | 2015-10-30 16:57:19 -0700 | [diff] [blame] | 1046 | // a path that doesn't exist yet. |
Josh Gao | 12a2ae9 | 2015-11-07 15:27:26 -0800 | [diff] [blame] | 1047 | if (srcs.size() == 1 && errno == ENOENT) { |
| 1048 | // However, its parent must exist. |
| 1049 | struct stat parent_st; |
| 1050 | if (stat(adb_dirname(dst).c_str(), &parent_st) == -1) { |
| 1051 | sc.Error("cannot create file/directory '%s': %s", dst, strerror(errno)); |
| 1052 | return false; |
| 1053 | } |
| 1054 | } else { |
| 1055 | sc.Error("failed to access '%s': %s", dst, strerror(errno)); |
Josh Gao | 0578677 | 2015-10-30 16:57:19 -0700 | [diff] [blame] | 1056 | return false; |
| 1057 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1058 | } |
| 1059 | |
Josh Gao | 07db119 | 2015-11-07 15:38:19 -0800 | [diff] [blame] | 1060 | bool dst_isdir = dst_exists && S_ISDIR(st.st_mode); |
| 1061 | if (!dst_isdir) { |
Josh Gao | 0578677 | 2015-10-30 16:57:19 -0700 | [diff] [blame] | 1062 | if (srcs.size() > 1) { |
| 1063 | sc.Error("target '%s' is not a directory", dst); |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 1064 | return false; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1065 | } else { |
Josh Gao | 0578677 | 2015-10-30 16:57:19 -0700 | [diff] [blame] | 1066 | size_t dst_len = strlen(dst); |
Josh Gao | 12a2ae9 | 2015-11-07 15:27:26 -0800 | [diff] [blame] | 1067 | |
| 1068 | // A path that ends with a slash doesn't have to be a directory if |
| 1069 | // it doesn't exist yet. |
Josh Gao | 1a02530 | 2015-11-09 11:12:14 -0800 | [diff] [blame] | 1070 | if (adb_is_separator(dst[dst_len - 1]) && dst_exists) { |
Josh Gao | 0578677 | 2015-10-30 16:57:19 -0700 | [diff] [blame] | 1071 | sc.Error("failed to access '%s': Not a directory", dst); |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 1072 | return false; |
Elliott Hughes | 5c74270 | 2015-07-30 17:42:01 -0700 | [diff] [blame] | 1073 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1074 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1075 | } |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 1076 | |
Josh Gao | 0578677 | 2015-10-30 16:57:19 -0700 | [diff] [blame] | 1077 | for (const char* src_path : srcs) { |
| 1078 | const char* dst_path = dst; |
Elliott Hughes | a00e6ef | 2015-12-17 17:05:29 -0800 | [diff] [blame] | 1079 | unsigned src_mode, src_time, src_size; |
| 1080 | if (!sync_stat(sc, src_path, &src_time, &src_mode, &src_size)) { |
Josh Gao | f264224 | 2015-12-09 14:03:30 -0800 | [diff] [blame] | 1081 | sc.Error("failed to stat remote object '%s'", src_path); |
Josh Gao | 12a2ae9 | 2015-11-07 15:27:26 -0800 | [diff] [blame] | 1082 | return false; |
| 1083 | } |
| 1084 | if (src_mode == 0) { |
Josh Gao | 0578677 | 2015-10-30 16:57:19 -0700 | [diff] [blame] | 1085 | sc.Error("remote object '%s' does not exist", src_path); |
| 1086 | success = false; |
| 1087 | continue; |
| 1088 | } |
| 1089 | |
Josh Gao | f264224 | 2015-12-09 14:03:30 -0800 | [diff] [blame] | 1090 | bool src_isdir = S_ISDIR(src_mode); |
| 1091 | if (S_ISLNK(src_mode)) { |
| 1092 | src_isdir = remote_symlink_isdir(sc, src_path); |
| 1093 | } |
| 1094 | |
Josh Gao | f264224 | 2015-12-09 14:03:30 -0800 | [diff] [blame] | 1095 | if (src_isdir) { |
Josh Gao | 07db119 | 2015-11-07 15:38:19 -0800 | [diff] [blame] | 1096 | std::string dst_dir = dst; |
| 1097 | |
| 1098 | // If the destination path existed originally, the source directory |
| 1099 | // should be copied as a child of the destination. |
| 1100 | if (dst_exists) { |
| 1101 | if (!dst_isdir) { |
| 1102 | sc.Error("target '%s' is not a directory", dst); |
| 1103 | return false; |
| 1104 | } |
| 1105 | if (!adb_is_separator(dst_dir.back())) { |
| 1106 | dst_dir.push_back(OS_PATH_SEPARATOR); |
| 1107 | } |
| 1108 | dst_dir.append(adb_basename(src_path)); |
| 1109 | } |
| 1110 | |
Josh Gao | f264224 | 2015-12-09 14:03:30 -0800 | [diff] [blame] | 1111 | success &= copy_remote_dir_local(sc, src_path, dst_dir.c_str(), copy_attrs); |
Josh Gao | 0578677 | 2015-10-30 16:57:19 -0700 | [diff] [blame] | 1112 | continue; |
Josh Gao | a31ea55 | 2016-03-02 16:11:13 -0800 | [diff] [blame] | 1113 | } else if (!should_pull_file(src_mode)) { |
| 1114 | sc.Warning("skipping special file '%s' (mode = 0o%o)", src_path, src_mode); |
| 1115 | continue; |
| 1116 | } |
Josh Gao | f264224 | 2015-12-09 14:03:30 -0800 | [diff] [blame] | 1117 | |
Josh Gao | a31ea55 | 2016-03-02 16:11:13 -0800 | [diff] [blame] | 1118 | std::string path_holder; |
| 1119 | if (dst_isdir) { |
| 1120 | // If we're copying a remote file to a local directory, we |
| 1121 | // really want to copy to local_dir + OS_PATH_SEPARATOR + |
| 1122 | // basename(remote). |
| 1123 | path_holder = android::base::StringPrintf("%s%c%s", dst_path, OS_PATH_SEPARATOR, |
| 1124 | adb_basename(src_path).c_str()); |
| 1125 | dst_path = path_holder.c_str(); |
| 1126 | } |
Josh Gao | f264224 | 2015-12-09 14:03:30 -0800 | [diff] [blame] | 1127 | |
Josh Gao | fb0c5cb | 2016-08-04 14:53:17 -0700 | [diff] [blame] | 1128 | sc.NewTransfer(); |
Josh Gao | a31ea55 | 2016-03-02 16:11:13 -0800 | [diff] [blame] | 1129 | sc.SetExpectedTotalBytes(src_size); |
| 1130 | if (!sync_recv(sc, src_path, dst_path)) { |
| 1131 | success = false; |
| 1132 | continue; |
| 1133 | } |
| 1134 | |
| 1135 | if (copy_attrs && set_time_and_mode(dst_path, src_time, src_mode) != 0) { |
| 1136 | success = false; |
| 1137 | continue; |
Josh Gao | 0578677 | 2015-10-30 16:57:19 -0700 | [diff] [blame] | 1138 | } |
Josh Gao | fb0c5cb | 2016-08-04 14:53:17 -0700 | [diff] [blame] | 1139 | sc.ReportTransferRate(src_path, TransferDirection::pull); |
Josh Gao | 0578677 | 2015-10-30 16:57:19 -0700 | [diff] [blame] | 1140 | } |
| 1141 | |
Josh Gao | fb0c5cb | 2016-08-04 14:53:17 -0700 | [diff] [blame] | 1142 | sc.ReportOverallTransferRate(TransferDirection::pull); |
Josh Gao | 0578677 | 2015-10-30 16:57:19 -0700 | [diff] [blame] | 1143 | return success; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1144 | } |
| 1145 | |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 1146 | bool do_sync_sync(const std::string& lpath, const std::string& rpath, bool list_only) { |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 1147 | SyncConnection sc; |
| 1148 | if (!sc.IsValid()) return false; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1149 | |
Josh Gao | fb0c5cb | 2016-08-04 14:53:17 -0700 | [diff] [blame] | 1150 | bool success = copy_local_dir_remote(sc, lpath, rpath, true, list_only); |
| 1151 | if (!list_only) { |
| 1152 | sc.ReportOverallTransferRate(TransferDirection::push); |
| 1153 | } |
| 1154 | return success; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1155 | } |