The Android Open Source Project | 9ca14dc | 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 | b302d12 | 2015-02-24 15:51:19 -0800 | [diff] [blame] | 17 | #include <dirent.h> |
| 18 | #include <errno.h> |
Spencer Low | 803451e | 2015-05-13 00:02:55 -0700 | [diff] [blame] | 19 | #include <inttypes.h> |
Dan Albert | b302d12 | 2015-02-24 15:51:19 -0800 | [diff] [blame] | 20 | #include <limits.h> |
The Android Open Source Project | 9ca14dc | 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 | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 24 | #include <sys/stat.h> |
| 25 | #include <sys/time.h> |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 26 | #include <sys/types.h> |
Dan Albert | b302d12 | 2015-02-24 15:51:19 -0800 | [diff] [blame] | 27 | #include <time.h> |
Elliott Hughes | dde00be | 2015-09-27 12:55:37 -0700 | [diff] [blame] | 28 | #include <unistd.h> |
Greg Hackmann | 8b68914 | 2014-05-06 08:48:18 -0700 | [diff] [blame] | 29 | #include <utime.h> |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 30 | |
Josh Gao | a408fd2 | 2016-08-04 14:53:17 -0700 | [diff] [blame] | 31 | #include <chrono> |
Josh Gao | 204d21e | 2015-11-02 16:45:47 -0800 | [diff] [blame] | 32 | #include <functional> |
Elliott Hughes | 4e7848d | 2015-08-24 14:49:43 -0700 | [diff] [blame] | 33 | #include <memory> |
Josh Gao | a408fd2 | 2016-08-04 14:53:17 -0700 | [diff] [blame] | 34 | #include <sstream> |
| 35 | #include <string> |
Elliott Hughes | 6c73bfc | 2015-10-27 13:40:35 -0700 | [diff] [blame] | 36 | #include <vector> |
Elliott Hughes | 4e7848d | 2015-08-24 14:49:43 -0700 | [diff] [blame] | 37 | |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 38 | #include "sysdeps.h" |
Dan Albert | b302d12 | 2015-02-24 15:51:19 -0800 | [diff] [blame] | 39 | |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 40 | #include "adb.h" |
| 41 | #include "adb_client.h" |
Dan Albert | 66a91b0 | 2015-02-24 21:26:58 -0800 | [diff] [blame] | 42 | #include "adb_io.h" |
Alex Vallée | e916315 | 2015-05-06 17:22:25 -0400 | [diff] [blame] | 43 | #include "adb_utils.h" |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 44 | #include "file_sync_service.h" |
Elliott Hughes | f5cdc1d | 2015-10-27 16:03:15 -0700 | [diff] [blame] | 45 | #include "line_printer.h" |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 46 | |
Elliott Hughes | f55ead9 | 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 | d189cfb | 2015-07-30 17:42:01 -0700 | [diff] [blame] | 50 | |
Elliott Hughes | b628cb1 | 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 | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 56 | |
Elliott Hughes | 7b43fa5 | 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 | d13def2 | 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 | 7b43fa5 | 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 | a408fd2 | 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 | b628cb1 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 203 | class SyncConnection { |
| 204 | public: |
Josh Gao | a408fd2 | 2016-08-04 14:53:17 -0700 | [diff] [blame] | 205 | SyncConnection() : expect_done_(false) { |
Elliott Hughes | b628cb1 | 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 | f5cdc1d | 2015-10-27 16:03:15 -0700 | [diff] [blame] | 211 | Error("connect failed: %s", error.c_str()); |
Elliott Hughes | b628cb1 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 212 | } |
| 213 | } |
| 214 | |
| 215 | ~SyncConnection() { |
| 216 | if (!IsValid()) return; |
| 217 | |
Spencer Low | cc4a4b1 | 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 | b628cb1 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 227 | adb_close(fd); |
Elliott Hughes | 78a37a5 | 2015-12-08 16:01:15 -0800 | [diff] [blame] | 228 | |
| 229 | line_printer_.KeepInfoLine(); |
Elliott Hughes | b628cb1 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 230 | } |
| 231 | |
| 232 | bool IsValid() { return fd >= 0; } |
| 233 | |
Josh Gao | 2db75ce | 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 | a408fd2 | 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 | dde00be | 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 | f5cdc1d | 2015-10-27 16:03:15 -0700 | [diff] [blame] | 281 | Error("SendRequest failed: path too long: %zu", path_length); |
Elliott Hughes | dde00be | 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 | 6c73bfc | 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 | dde00be | 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 | 6c73bfc | 2015-10-27 13:40:35 -0700 | [diff] [blame] | 295 | return WriteFdExactly(fd, &buf[0], buf.size()); |
Elliott Hughes | dde00be | 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 | 54e3efe | 2015-11-20 22:01:06 -0800 | [diff] [blame] | 301 | const char* lpath, const char* rpath, |
Elliott Hughes | 7275d80 | 2015-11-20 17:35:17 -0800 | [diff] [blame] | 302 | unsigned mtime, |
| 303 | const char* data, size_t data_length) { |
Elliott Hughes | dde00be | 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 | f5cdc1d | 2015-10-27 16:03:15 -0700 | [diff] [blame] | 306 | Error("SendSmallFile failed: path too long: %zu", path_length); |
Elliott Hughes | dde00be | 2015-09-27 12:55:37 -0700 | [diff] [blame] | 307 | errno = ENAMETOOLONG; |
| 308 | return false; |
| 309 | } |
| 310 | |
Elliott Hughes | 6c73bfc | 2015-10-27 13:40:35 -0700 | [diff] [blame] | 311 | std::vector<char> buf(sizeof(SyncRequest) + path_length + |
Elliott Hughes | 7275d80 | 2015-11-20 17:35:17 -0800 | [diff] [blame] | 312 | sizeof(SyncRequest) + data_length + |
| 313 | sizeof(SyncRequest)); |
Elliott Hughes | 6c73bfc | 2015-10-27 13:40:35 -0700 | [diff] [blame] | 314 | char* p = &buf[0]; |
Elliott Hughes | dde00be | 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 | 54e3efe | 2015-11-20 22:01:06 -0800 | [diff] [blame] | 335 | WriteOrDie(lpath, rpath, &buf[0], (p - &buf[0])); |
Josh Gao | 2db75ce | 2016-02-19 15:55:55 -0800 | [diff] [blame] | 336 | expect_done_ = true; |
Josh Gao | a408fd2 | 2016-08-04 14:53:17 -0700 | [diff] [blame] | 337 | |
| 338 | // RecordFilesTransferred gets called in CopyDone. |
| 339 | RecordBytesTransferred(data_length); |
Josh Gao | 37e8ab4 | 2016-03-01 11:46:02 -0800 | [diff] [blame] | 340 | ReportProgress(rpath, data_length, data_length); |
Elliott Hughes | dde00be | 2015-09-27 12:55:37 -0700 | [diff] [blame] | 341 | return true; |
| 342 | } |
| 343 | |
Elliott Hughes | 7275d80 | 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 | 54e3efe | 2015-11-20 22:01:06 -0800 | [diff] [blame] | 363 | Error("opening '%s' locally failed: %s", lpath, strerror(errno)); |
Elliott Hughes | 7275d80 | 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 | 54e3efe | 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 | 7275d80 | 2015-11-20 17:35:17 -0800 | [diff] [blame] | 376 | break; |
| 377 | } |
| 378 | |
Elliott Hughes | 54e3efe | 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 | 7275d80 | 2015-11-20 17:35:17 -0800 | [diff] [blame] | 381 | |
Josh Gao | a408fd2 | 2016-08-04 14:53:17 -0700 | [diff] [blame] | 382 | RecordBytesTransferred(bytes_read); |
Elliott Hughes | 54e3efe | 2015-11-20 22:01:06 -0800 | [diff] [blame] | 383 | bytes_copied += bytes_read; |
Elliott Hughes | 7275d80 | 2015-11-20 17:35:17 -0800 | [diff] [blame] | 384 | |
Josh Gao | 2db75ce | 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 | 7b43fa5 | 2015-12-17 17:05:29 -0800 | [diff] [blame] | 390 | ReportProgress(rpath, bytes_copied, total_size); |
Elliott Hughes | 7275d80 | 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 | 2db75ce | 2016-02-19 15:55:55 -0800 | [diff] [blame] | 398 | expect_done_ = true; |
Josh Gao | a408fd2 | 2016-08-04 14:53:17 -0700 | [diff] [blame] | 399 | |
| 400 | // RecordFilesTransferred gets called in CopyDone. |
Elliott Hughes | 54e3efe | 2015-11-20 22:01:06 -0800 | [diff] [blame] | 401 | return WriteOrDie(lpath, rpath, &msg.data, sizeof(msg.data)); |
Elliott Hughes | 7275d80 | 2015-11-20 17:35:17 -0800 | [diff] [blame] | 402 | } |
| 403 | |
Elliott Hughes | dde00be | 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 | 2db75ce | 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 | dde00be | 2015-09-27 12:55:37 -0700 | [diff] [blame] | 408 | return false; |
| 409 | } |
| 410 | if (msg.status.id == ID_OKAY) { |
Josh Gao | 2db75ce | 2016-02-19 15:55:55 -0800 | [diff] [blame] | 411 | if (expect_done_) { |
| 412 | expect_done_ = false; |
Josh Gao | a408fd2 | 2016-08-04 14:53:17 -0700 | [diff] [blame] | 413 | RecordFilesTransferred(1); |
Josh Gao | 2db75ce | 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 | dde00be | 2015-09-27 12:55:37 -0700 | [diff] [blame] | 419 | } |
| 420 | if (msg.status.id != ID_FAIL) { |
Elliott Hughes | f5cdc1d | 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 | dde00be | 2015-09-27 12:55:37 -0700 | [diff] [blame] | 422 | return false; |
| 423 | } |
Elliott Hughes | e4ed32f | 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 | 6c73bfc | 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 | f5cdc1d | 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 | dde00be | 2015-09-27 12:55:37 -0700 | [diff] [blame] | 432 | return false; |
| 433 | } |
Elliott Hughes | 6c73bfc | 2015-10-27 13:40:35 -0700 | [diff] [blame] | 434 | buf[msg.status.msglen] = 0; |
Elliott Hughes | f5cdc1d | 2015-10-27 16:03:15 -0700 | [diff] [blame] | 435 | Error("failed to copy '%s' to '%s': %s", from, to, &buf[0]); |
Elliott Hughes | dde00be | 2015-09-27 12:55:37 -0700 | [diff] [blame] | 436 | return false; |
| 437 | } |
| 438 | |
Elliott Hughes | f5cdc1d | 2015-10-27 16:03:15 -0700 | [diff] [blame] | 439 | |
Josh Gao | cbf485f | 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 | 7b43fa5 | 2015-12-17 17:05:29 -0800 | [diff] [blame] | 442 | |
Josh Gao | cbf485f | 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 | 7b43fa5 | 2015-12-17 17:05:29 -0800 | [diff] [blame] | 448 | line_printer_.Print(s, LinePrinter::INFO); |
Josh Gao | cbf485f | 2015-11-02 17:15:57 -0800 | [diff] [blame] | 449 | } |
| 450 | |
Josh Gao | f35fede | 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 | f5cdc1d | 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 | 78a37a5 | 2015-12-08 16:01:15 -0800 | [diff] [blame] | 471 | line_printer_.Print(s, LinePrinter::ERROR); |
Elliott Hughes | f5cdc1d | 2015-10-27 16:03:15 -0700 | [diff] [blame] | 472 | } |
| 473 | |
Josh Gao | a544cc6 | 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 | 78a37a5 | 2015-12-08 16:01:15 -0800 | [diff] [blame] | 482 | line_printer_.Print(s, LinePrinter::WARNING); |
Josh Gao | a544cc6 | 2015-11-07 17:18:44 -0800 | [diff] [blame] | 483 | } |
| 484 | |
Elliott Hughes | 7b43fa5 | 2015-12-17 17:05:29 -0800 | [diff] [blame] | 485 | void ComputeExpectedTotalBytes(const std::vector<copyinfo>& file_list) { |
Josh Gao | a408fd2 | 2016-08-04 14:53:17 -0700 | [diff] [blame] | 486 | current_ledger_.bytes_expected = 0; |
Elliott Hughes | 7b43fa5 | 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 | a408fd2 | 2016-08-04 14:53:17 -0700 | [diff] [blame] | 490 | if (!ci.skip) current_ledger_.bytes_expected += ci.size; |
Elliott Hughes | 7b43fa5 | 2015-12-17 17:05:29 -0800 | [diff] [blame] | 491 | } |
Josh Gao | a408fd2 | 2016-08-04 14:53:17 -0700 | [diff] [blame] | 492 | current_ledger_.expect_multiple_files = true; |
Elliott Hughes | 7b43fa5 | 2015-12-17 17:05:29 -0800 | [diff] [blame] | 493 | } |
| 494 | |
| 495 | void SetExpectedTotalBytes(uint64_t expected_total_bytes) { |
Josh Gao | a408fd2 | 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 | 7b43fa5 | 2015-12-17 17:05:29 -0800 | [diff] [blame] | 498 | } |
| 499 | |
Elliott Hughes | b628cb1 | 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 | 2db75ce | 2016-02-19 15:55:55 -0800 | [diff] [blame] | 505 | bool expect_done_; |
Elliott Hughes | 7b43fa5 | 2015-12-17 17:05:29 -0800 | [diff] [blame] | 506 | |
Josh Gao | a408fd2 | 2016-08-04 14:53:17 -0700 | [diff] [blame] | 507 | TransferLedger global_ledger_; |
| 508 | TransferLedger current_ledger_; |
Elliott Hughes | f5cdc1d | 2015-10-27 16:03:15 -0700 | [diff] [blame] | 509 | LinePrinter line_printer_; |
Elliott Hughes | b628cb1 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 510 | |
Spencer Low | cc4a4b1 | 2015-10-14 17:32:44 -0700 | [diff] [blame] | 511 | bool SendQuit() { |
| 512 | return SendRequest(ID_QUIT, ""); // TODO: add a SendResponse? |
Elliott Hughes | b628cb1 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 513 | } |
| 514 | |
Elliott Hughes | 54e3efe | 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 | b628cb1 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 535 | }; |
| 536 | |
Josh Gao | 204d21e | 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 | b628cb1 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 538 | |
Josh Gao | 204d21e | 2015-11-02 16:45:47 -0800 | [diff] [blame] | 539 | static bool sync_ls(SyncConnection& sc, const char* path, |
Chih-Hung Hsieh | 90b40f6 | 2016-07-27 16:25:51 -0700 | [diff] [blame] | 540 | const std::function<sync_ls_cb>& func) { |
Elliott Hughes | dde00be | 2015-09-27 12:55:37 -0700 | [diff] [blame] | 541 | if (!sc.SendRequest(ID_LIST, path)) return false; |
Elliott Hughes | b628cb1 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 542 | |
| 543 | while (true) { |
| 544 | syncmsg msg; |
Elliott Hughes | dde00be | 2015-09-27 12:55:37 -0700 | [diff] [blame] | 545 | if (!ReadFdExactly(sc.fd, &msg.dent, sizeof(msg.dent))) return false; |
Elliott Hughes | b628cb1 | 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 | 9e8e355 | 2015-08-24 14:27:03 -0700 | [diff] [blame] | 550 | size_t len = msg.dent.namelen; |
Elliott Hughes | b628cb1 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 551 | if (len > 256) return false; // TODO: resize buffer? continue? |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 552 | |
Elliott Hughes | d189cfb | 2015-07-30 17:42:01 -0700 | [diff] [blame] | 553 | char buf[257]; |
Elliott Hughes | dde00be | 2015-09-27 12:55:37 -0700 | [diff] [blame] | 554 | if (!ReadFdExactly(sc.fd, buf, len)) return false; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 555 | buf[len] = 0; |
| 556 | |
Josh Gao | 204d21e | 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 | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 558 | } |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 559 | } |
| 560 | |
Elliott Hughes | b628cb1 | 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 | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 563 | syncmsg msg; |
Elliott Hughes | b628cb1 | 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 | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 566 | } |
| 567 | |
Elliott Hughes | 9e8e355 | 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 | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 571 | |
Elliott Hughes | b628cb1 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 572 | return true; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 573 | } |
| 574 | |
Elliott Hughes | b628cb1 | 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 | dde00be | 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 | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 578 | } |
| 579 | |
Elliott Hughes | dde00be | 2015-09-27 12:55:37 -0700 | [diff] [blame] | 580 | static bool sync_send(SyncConnection& sc, const char* lpath, const char* rpath, |
Elliott Hughes | f5cdc1d | 2015-10-27 16:03:15 -0700 | [diff] [blame] | 581 | unsigned mtime, mode_t mode) |
Elliott Hughes | dde00be | 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 | f5cdc1d | 2015-10-27 16:03:15 -0700 | [diff] [blame] | 590 | sc.Error("readlink '%s' failed: %s", lpath, strerror(errno)); |
Elliott Hughes | dde00be | 2015-09-27 12:55:37 -0700 | [diff] [blame] | 591 | return false; |
| 592 | } |
| 593 | buf[data_length++] = '\0'; |
| 594 | |
Elliott Hughes | 54e3efe | 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 | dde00be | 2015-09-27 12:55:37 -0700 | [diff] [blame] | 598 | return sc.CopyDone(lpath, rpath); |
| 599 | #endif |
| 600 | } |
| 601 | |
Elliott Hughes | dde00be | 2015-09-27 12:55:37 -0700 | [diff] [blame] | 602 | struct stat st; |
| 603 | if (stat(lpath, &st) == -1) { |
Elliott Hughes | f5cdc1d | 2015-10-27 16:03:15 -0700 | [diff] [blame] | 604 | sc.Error("failed to stat local file '%s': %s", lpath, strerror(errno)); |
Elliott Hughes | dde00be | 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; |
Josh Gao | 1deea10 | 2016-09-14 16:13:50 -0700 | [diff] [blame] | 609 | if (!android::base::ReadFileToString(lpath, &data, true)) { |
Elliott Hughes | f5cdc1d | 2015-10-27 16:03:15 -0700 | [diff] [blame] | 610 | sc.Error("failed to read all of '%s': %s", lpath, strerror(errno)); |
Elliott Hughes | dde00be | 2015-09-27 12:55:37 -0700 | [diff] [blame] | 611 | return false; |
| 612 | } |
Elliott Hughes | 54e3efe | 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 | f5cdc1d | 2015-10-27 16:03:15 -0700 | [diff] [blame] | 615 | return false; |
| 616 | } |
Elliott Hughes | dde00be | 2015-09-27 12:55:37 -0700 | [diff] [blame] | 617 | } else { |
Elliott Hughes | 7275d80 | 2015-11-20 17:35:17 -0800 | [diff] [blame] | 618 | if (!sc.SendLargeFile(path_and_mode.c_str(), lpath, rpath, mtime)) { |
Elliott Hughes | f5cdc1d | 2015-10-27 16:03:15 -0700 | [diff] [blame] | 619 | return false; |
| 620 | } |
Elliott Hughes | dde00be | 2015-09-27 12:55:37 -0700 | [diff] [blame] | 621 | } |
| 622 | return sc.CopyDone(lpath, rpath); |
| 623 | } |
| 624 | |
Felipe Leme | b644703 | 2016-04-01 17:43:27 -0700 | [diff] [blame] | 625 | static bool sync_recv(SyncConnection& sc, const char* rpath, const char* lpath, |
| 626 | const char* name=nullptr) { |
Elliott Hughes | b628cb1 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 627 | unsigned size = 0; |
Elliott Hughes | f5cdc1d | 2015-10-27 16:03:15 -0700 | [diff] [blame] | 628 | if (!sync_stat(sc, rpath, nullptr, nullptr, &size)) return false; |
Mark Lindner | 9f9d145 | 2014-03-11 17:55:59 -0700 | [diff] [blame] | 629 | |
Elliott Hughes | dde00be | 2015-09-27 12:55:37 -0700 | [diff] [blame] | 630 | if (!sc.SendRequest(ID_RECV, rpath)) return false; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 631 | |
Elliott Hughes | e4ed32f | 2015-10-23 21:06:11 -0700 | [diff] [blame] | 632 | adb_unlink(lpath); |
Elliott Hughes | e4ed32f | 2015-10-23 21:06:11 -0700 | [diff] [blame] | 633 | int lfd = adb_creat(lpath, 0644); |
| 634 | if (lfd < 0) { |
Elliott Hughes | f5cdc1d | 2015-10-27 16:03:15 -0700 | [diff] [blame] | 635 | sc.Error("cannot create '%s': %s", lpath, strerror(errno)); |
Elliott Hughes | e4ed32f | 2015-10-23 21:06:11 -0700 | [diff] [blame] | 636 | return false; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 637 | } |
| 638 | |
Elliott Hughes | f5cdc1d | 2015-10-27 16:03:15 -0700 | [diff] [blame] | 639 | uint64_t bytes_copied = 0; |
Elliott Hughes | b628cb1 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 640 | while (true) { |
Elliott Hughes | e4ed32f | 2015-10-23 21:06:11 -0700 | [diff] [blame] | 641 | syncmsg msg; |
Elliott Hughes | dde00be | 2015-09-27 12:55:37 -0700 | [diff] [blame] | 642 | if (!ReadFdExactly(sc.fd, &msg.data, sizeof(msg.data))) { |
Spencer Low | c1a3133 | 2015-08-28 01:07:30 -0700 | [diff] [blame] | 643 | adb_close(lfd); |
Elliott Hughes | e4ed32f | 2015-10-23 21:06:11 -0700 | [diff] [blame] | 644 | adb_unlink(lpath); |
Elliott Hughes | dde00be | 2015-09-27 12:55:37 -0700 | [diff] [blame] | 645 | return false; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 646 | } |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 647 | |
Elliott Hughes | e4ed32f | 2015-10-23 21:06:11 -0700 | [diff] [blame] | 648 | if (msg.data.id == ID_DONE) break; |
| 649 | |
| 650 | if (msg.data.id != ID_DATA) { |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 651 | adb_close(lfd); |
Elliott Hughes | e4ed32f | 2015-10-23 21:06:11 -0700 | [diff] [blame] | 652 | adb_unlink(lpath); |
| 653 | sc.ReportCopyFailure(rpath, lpath, msg); |
Elliott Hughes | dde00be | 2015-09-27 12:55:37 -0700 | [diff] [blame] | 654 | return false; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 655 | } |
| 656 | |
Elliott Hughes | e4ed32f | 2015-10-23 21:06:11 -0700 | [diff] [blame] | 657 | if (msg.data.size > sc.max) { |
Elliott Hughes | f5cdc1d | 2015-10-27 16:03:15 -0700 | [diff] [blame] | 658 | sc.Error("msg.data.size too large: %u (max %zu)", msg.data.size, sc.max); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 659 | adb_close(lfd); |
Elliott Hughes | e4ed32f | 2015-10-23 21:06:11 -0700 | [diff] [blame] | 660 | adb_unlink(lpath); |
Elliott Hughes | dde00be | 2015-09-27 12:55:37 -0700 | [diff] [blame] | 661 | return false; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 662 | } |
| 663 | |
Elliott Hughes | e4ed32f | 2015-10-23 21:06:11 -0700 | [diff] [blame] | 664 | char buffer[SYNC_DATA_MAX]; |
| 665 | if (!ReadFdExactly(sc.fd, buffer, msg.data.size)) { |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 666 | adb_close(lfd); |
Elliott Hughes | e4ed32f | 2015-10-23 21:06:11 -0700 | [diff] [blame] | 667 | adb_unlink(lpath); |
Elliott Hughes | dde00be | 2015-09-27 12:55:37 -0700 | [diff] [blame] | 668 | return false; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 669 | } |
| 670 | |
Elliott Hughes | e4ed32f | 2015-10-23 21:06:11 -0700 | [diff] [blame] | 671 | if (!WriteFdExactly(lfd, buffer, msg.data.size)) { |
Elliott Hughes | f5cdc1d | 2015-10-27 16:03:15 -0700 | [diff] [blame] | 672 | sc.Error("cannot write '%s': %s", lpath, strerror(errno)); |
Elliott Hughes | e4ed32f | 2015-10-23 21:06:11 -0700 | [diff] [blame] | 673 | adb_close(lfd); |
| 674 | adb_unlink(lpath); |
| 675 | return false; |
| 676 | } |
| 677 | |
Elliott Hughes | f5cdc1d | 2015-10-27 16:03:15 -0700 | [diff] [blame] | 678 | bytes_copied += msg.data.size; |
| 679 | |
Josh Gao | a408fd2 | 2016-08-04 14:53:17 -0700 | [diff] [blame] | 680 | sc.RecordBytesTransferred(msg.data.size); |
Josh Gao | 8fc9d68 | 2016-08-05 00:39:16 -0700 | [diff] [blame] | 681 | sc.ReportProgress(name != nullptr ? name : rpath, bytes_copied, size); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 682 | } |
| 683 | |
Josh Gao | a408fd2 | 2016-08-04 14:53:17 -0700 | [diff] [blame] | 684 | sc.RecordFilesTransferred(1); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 685 | adb_close(lfd); |
Elliott Hughes | dde00be | 2015-09-27 12:55:37 -0700 | [diff] [blame] | 686 | return true; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 687 | } |
| 688 | |
Elliott Hughes | b628cb1 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 689 | bool do_sync_ls(const char* path) { |
| 690 | SyncConnection sc; |
| 691 | if (!sc.IsValid()) return false; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 692 | |
Josh Gao | 204d21e | 2015-11-02 16:45:47 -0800 | [diff] [blame] | 693 | return sync_ls(sc, path, [](unsigned mode, unsigned size, unsigned time, |
| 694 | const char* name) { |
| 695 | printf("%08x %08x %08x %s\n", mode, size, time, name); |
| 696 | }); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 697 | } |
| 698 | |
Elliott Hughes | b628cb1 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 699 | static bool IsDotOrDotDot(const char* name) { |
| 700 | return name[0] == '.' && (name[1] == '\0' || (name[1] == '.' && name[2] == '\0')); |
| 701 | } |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 702 | |
Elliott Hughes | 7b43fa5 | 2015-12-17 17:05:29 -0800 | [diff] [blame] | 703 | static bool local_build_list(SyncConnection& sc, std::vector<copyinfo>* file_list, |
Josh Gao | 8efa646 | 2015-11-03 15:26:38 -0800 | [diff] [blame] | 704 | const std::string& lpath, |
| 705 | const std::string& rpath) { |
Josh Gao | 204d21e | 2015-11-02 16:45:47 -0800 | [diff] [blame] | 706 | std::vector<copyinfo> dirlist; |
Josh Gao | d20cf38 | 2015-11-03 15:23:03 -0800 | [diff] [blame] | 707 | std::unique_ptr<DIR, int (*)(DIR*)> dir(opendir(lpath.c_str()), closedir); |
Elliott Hughes | b628cb1 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 708 | if (!dir) { |
Josh Gao | d20cf38 | 2015-11-03 15:23:03 -0800 | [diff] [blame] | 709 | sc.Error("cannot open '%s': %s", lpath.c_str(), strerror(errno)); |
Josh Gao | 8efa646 | 2015-11-03 15:26:38 -0800 | [diff] [blame] | 710 | return false; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 711 | } |
| 712 | |
Josh Gao | a90563e | 2015-11-04 14:57:04 -0800 | [diff] [blame] | 713 | bool empty_dir = true; |
Elliott Hughes | f5cdc1d | 2015-10-27 16:03:15 -0700 | [diff] [blame] | 714 | dirent* de; |
Elliott Hughes | b628cb1 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 715 | while ((de = readdir(dir.get()))) { |
Josh Gao | a90563e | 2015-11-04 14:57:04 -0800 | [diff] [blame] | 716 | if (IsDotOrDotDot(de->d_name)) { |
| 717 | continue; |
| 718 | } |
Elliott Hughes | b628cb1 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 719 | |
Josh Gao | a90563e | 2015-11-04 14:57:04 -0800 | [diff] [blame] | 720 | empty_dir = false; |
Josh Gao | d20cf38 | 2015-11-03 15:23:03 -0800 | [diff] [blame] | 721 | std::string stat_path = lpath + de->d_name; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 722 | |
Elliott Hughes | b628cb1 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 723 | struct stat st; |
Josh Gao | a5cea71 | 2015-11-07 15:27:26 -0800 | [diff] [blame] | 724 | if (lstat(stat_path.c_str(), &st) == -1) { |
Josh Gao | d20cf38 | 2015-11-03 15:23:03 -0800 | [diff] [blame] | 725 | sc.Error("cannot lstat '%s': %s", stat_path.c_str(), |
| 726 | strerror(errno)); |
Josh Gao | a5cea71 | 2015-11-07 15:27:26 -0800 | [diff] [blame] | 727 | continue; |
| 728 | } |
| 729 | |
Josh Gao | d9a2fd6 | 2015-12-09 14:03:30 -0800 | [diff] [blame] | 730 | copyinfo ci(lpath, rpath, de->d_name, st.st_mode); |
Josh Gao | a5cea71 | 2015-11-07 15:27:26 -0800 | [diff] [blame] | 731 | if (S_ISDIR(st.st_mode)) { |
| 732 | dirlist.push_back(ci); |
| 733 | } else { |
Josh Gao | d13def2 | 2016-03-02 16:11:13 -0800 | [diff] [blame] | 734 | if (!should_push_file(st.st_mode)) { |
| 735 | sc.Warning("skipping special file '%s' (mode = 0o%o)", lpath.c_str(), st.st_mode); |
Josh Gao | b3cab93 | 2015-11-30 10:21:25 -0800 | [diff] [blame] | 736 | ci.skip = true; |
Josh Gao | a5cea71 | 2015-11-07 15:27:26 -0800 | [diff] [blame] | 737 | } |
Josh Gao | d13def2 | 2016-03-02 16:11:13 -0800 | [diff] [blame] | 738 | ci.time = st.st_mtime; |
| 739 | ci.size = st.st_size; |
Elliott Hughes | 7b43fa5 | 2015-12-17 17:05:29 -0800 | [diff] [blame] | 740 | file_list->push_back(ci); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 741 | } |
| 742 | } |
| 743 | |
Elliott Hughes | b628cb1 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 744 | // Close this directory and recurse. |
| 745 | dir.reset(); |
Josh Gao | a90563e | 2015-11-04 14:57:04 -0800 | [diff] [blame] | 746 | |
| 747 | // Add the current directory to the list if it was empty, to ensure that |
| 748 | // it gets created. |
| 749 | if (empty_dir) { |
| 750 | // TODO(b/25566053): Make pushing empty directories work. |
| 751 | // TODO(b/25457350): We don't preserve permissions on directories. |
Josh Gao | a544cc6 | 2015-11-07 17:18:44 -0800 | [diff] [blame] | 752 | sc.Warning("skipping empty directory '%s'", lpath.c_str()); |
Josh Gao | d9a2fd6 | 2015-12-09 14:03:30 -0800 | [diff] [blame] | 753 | copyinfo ci(adb_dirname(lpath), adb_dirname(rpath), adb_basename(lpath), S_IFDIR); |
Josh Gao | a90563e | 2015-11-04 14:57:04 -0800 | [diff] [blame] | 754 | ci.skip = true; |
Elliott Hughes | 7b43fa5 | 2015-12-17 17:05:29 -0800 | [diff] [blame] | 755 | file_list->push_back(ci); |
Josh Gao | a90563e | 2015-11-04 14:57:04 -0800 | [diff] [blame] | 756 | return true; |
| 757 | } |
| 758 | |
Josh Gao | 204d21e | 2015-11-02 16:45:47 -0800 | [diff] [blame] | 759 | for (const copyinfo& ci : dirlist) { |
Elliott Hughes | 7b43fa5 | 2015-12-17 17:05:29 -0800 | [diff] [blame] | 760 | local_build_list(sc, file_list, ci.lpath, ci.rpath); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 761 | } |
| 762 | |
Josh Gao | 8efa646 | 2015-11-03 15:26:38 -0800 | [diff] [blame] | 763 | return true; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 764 | } |
| 765 | |
Josh Gao | d20cf38 | 2015-11-03 15:23:03 -0800 | [diff] [blame] | 766 | static bool copy_local_dir_remote(SyncConnection& sc, std::string lpath, |
| 767 | std::string rpath, bool check_timestamps, |
| 768 | bool list_only) { |
Josh Gao | a408fd2 | 2016-08-04 14:53:17 -0700 | [diff] [blame] | 769 | sc.NewTransfer(); |
| 770 | |
Josh Gao | d20cf38 | 2015-11-03 15:23:03 -0800 | [diff] [blame] | 771 | // Make sure that both directory paths end in a slash. |
Josh Gao | a3b6a06 | 2015-11-09 11:12:14 -0800 | [diff] [blame] | 772 | // Both paths are known to be nonempty, so we don't need to check. |
Elliott Hughes | 7b43fa5 | 2015-12-17 17:05:29 -0800 | [diff] [blame] | 773 | ensure_trailing_separators(lpath, rpath); |
Josh Gao | d20cf38 | 2015-11-03 15:23:03 -0800 | [diff] [blame] | 774 | |
| 775 | // Recursively build the list of files to copy. |
Elliott Hughes | 7b43fa5 | 2015-12-17 17:05:29 -0800 | [diff] [blame] | 776 | std::vector<copyinfo> file_list; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 777 | int skipped = 0; |
Elliott Hughes | 7b43fa5 | 2015-12-17 17:05:29 -0800 | [diff] [blame] | 778 | if (!local_build_list(sc, &file_list, lpath, rpath)) { |
Elliott Hughes | b628cb1 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 779 | return false; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 780 | } |
| 781 | |
Elliott Hughes | b628cb1 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 782 | if (check_timestamps) { |
Elliott Hughes | 7b43fa5 | 2015-12-17 17:05:29 -0800 | [diff] [blame] | 783 | for (const copyinfo& ci : file_list) { |
Josh Gao | a3b6a06 | 2015-11-09 11:12:14 -0800 | [diff] [blame] | 784 | if (!sc.SendRequest(ID_STAT, ci.rpath.c_str())) { |
Josh Gao | d20cf38 | 2015-11-03 15:23:03 -0800 | [diff] [blame] | 785 | return false; |
| 786 | } |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 787 | } |
Elliott Hughes | 7b43fa5 | 2015-12-17 17:05:29 -0800 | [diff] [blame] | 788 | for (copyinfo& ci : file_list) { |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 789 | unsigned int timestamp, mode, size; |
Josh Gao | d20cf38 | 2015-11-03 15:23:03 -0800 | [diff] [blame] | 790 | if (!sync_finish_stat(sc, ×tamp, &mode, &size)) { |
| 791 | return false; |
| 792 | } |
Josh Gao | 204d21e | 2015-11-02 16:45:47 -0800 | [diff] [blame] | 793 | if (size == ci.size) { |
Elliott Hughes | 7b43fa5 | 2015-12-17 17:05:29 -0800 | [diff] [blame] | 794 | // For links, we cannot update the atime/mtime. |
Josh Gao | 204d21e | 2015-11-02 16:45:47 -0800 | [diff] [blame] | 795 | if ((S_ISREG(ci.mode & mode) && timestamp == ci.time) || |
| 796 | (S_ISLNK(ci.mode & mode) && timestamp >= ci.time)) { |
Josh Gao | cb094c6 | 2015-11-03 14:44:04 -0800 | [diff] [blame] | 797 | ci.skip = true; |
Elliott Hughes | b628cb1 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 798 | } |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 799 | } |
| 800 | } |
| 801 | } |
Josh Gao | 204d21e | 2015-11-02 16:45:47 -0800 | [diff] [blame] | 802 | |
Elliott Hughes | 7b43fa5 | 2015-12-17 17:05:29 -0800 | [diff] [blame] | 803 | sc.ComputeExpectedTotalBytes(file_list); |
| 804 | |
| 805 | for (const copyinfo& ci : file_list) { |
Josh Gao | cb094c6 | 2015-11-03 14:44:04 -0800 | [diff] [blame] | 806 | if (!ci.skip) { |
Elliott Hughes | f5cdc1d | 2015-10-27 16:03:15 -0700 | [diff] [blame] | 807 | if (list_only) { |
Josh Gao | 23891f1 | 2016-08-04 14:55:23 -0700 | [diff] [blame] | 808 | sc.Println("would push: %s -> %s", ci.lpath.c_str(), ci.rpath.c_str()); |
Elliott Hughes | f5cdc1d | 2015-10-27 16:03:15 -0700 | [diff] [blame] | 809 | } else { |
Elliott Hughes | 7b43fa5 | 2015-12-17 17:05:29 -0800 | [diff] [blame] | 810 | if (!sync_send(sc, ci.lpath.c_str(), ci.rpath.c_str(), ci.time, ci.mode)) { |
Josh Gao | d20cf38 | 2015-11-03 15:23:03 -0800 | [diff] [blame] | 811 | return false; |
Elliott Hughes | f5cdc1d | 2015-10-27 16:03:15 -0700 | [diff] [blame] | 812 | } |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 813 | } |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 814 | } else { |
| 815 | skipped++; |
| 816 | } |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 817 | } |
| 818 | |
Josh Gao | a408fd2 | 2016-08-04 14:53:17 -0700 | [diff] [blame] | 819 | sc.RecordFilesSkipped(skipped); |
| 820 | sc.ReportTransferRate(lpath, TransferDirection::push); |
Elliott Hughes | b628cb1 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 821 | return true; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 822 | } |
| 823 | |
Josh Gao | 5d093b2 | 2015-10-30 16:57:19 -0700 | [diff] [blame] | 824 | bool do_sync_push(const std::vector<const char*>& srcs, const char* dst) { |
Elliott Hughes | b628cb1 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 825 | SyncConnection sc; |
| 826 | if (!sc.IsValid()) return false; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 827 | |
Josh Gao | 5d093b2 | 2015-10-30 16:57:19 -0700 | [diff] [blame] | 828 | bool success = true; |
Josh Gao | a5cea71 | 2015-11-07 15:27:26 -0800 | [diff] [blame] | 829 | unsigned dst_mode; |
| 830 | if (!sync_stat(sc, dst, nullptr, &dst_mode, nullptr)) return false; |
| 831 | bool dst_exists = (dst_mode != 0); |
Josh Gao | 8acf06c | 2015-11-07 15:38:19 -0800 | [diff] [blame] | 832 | bool dst_isdir = S_ISDIR(dst_mode); |
Josh Gao | 5d093b2 | 2015-10-30 16:57:19 -0700 | [diff] [blame] | 833 | |
Josh Gao | 8acf06c | 2015-11-07 15:38:19 -0800 | [diff] [blame] | 834 | if (!dst_isdir) { |
Josh Gao | 5d093b2 | 2015-10-30 16:57:19 -0700 | [diff] [blame] | 835 | if (srcs.size() > 1) { |
| 836 | sc.Error("target '%s' is not a directory", dst); |
| 837 | return false; |
| 838 | } else { |
| 839 | size_t dst_len = strlen(dst); |
Josh Gao | a5cea71 | 2015-11-07 15:27:26 -0800 | [diff] [blame] | 840 | |
| 841 | // A path that ends with a slash doesn't have to be a directory if |
| 842 | // it doesn't exist yet. |
| 843 | if (dst[dst_len - 1] == '/' && dst_exists) { |
Josh Gao | 5d093b2 | 2015-10-30 16:57:19 -0700 | [diff] [blame] | 844 | sc.Error("failed to access '%s': Not a directory", dst); |
| 845 | return false; |
| 846 | } |
| 847 | } |
Elliott Hughes | b628cb1 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 848 | } |
Josh Gao | 5d093b2 | 2015-10-30 16:57:19 -0700 | [diff] [blame] | 849 | |
| 850 | for (const char* src_path : srcs) { |
| 851 | const char* dst_path = dst; |
| 852 | struct stat st; |
Josh Gao | a5cea71 | 2015-11-07 15:27:26 -0800 | [diff] [blame] | 853 | if (stat(src_path, &st) == -1) { |
Josh Gao | 5d093b2 | 2015-10-30 16:57:19 -0700 | [diff] [blame] | 854 | sc.Error("cannot stat '%s': %s", src_path, strerror(errno)); |
| 855 | success = false; |
| 856 | continue; |
| 857 | } |
| 858 | |
| 859 | if (S_ISDIR(st.st_mode)) { |
Josh Gao | 8acf06c | 2015-11-07 15:38:19 -0800 | [diff] [blame] | 860 | std::string dst_dir = dst; |
| 861 | |
| 862 | // If the destination path existed originally, the source directory |
| 863 | // should be copied as a child of the destination. |
| 864 | if (dst_exists) { |
| 865 | if (!dst_isdir) { |
| 866 | sc.Error("target '%s' is not a directory", dst); |
| 867 | return false; |
| 868 | } |
| 869 | // dst is a POSIX path, so we don't want to use the sysdeps |
| 870 | // helpers here. |
| 871 | if (dst_dir.back() != '/') { |
| 872 | dst_dir.push_back('/'); |
| 873 | } |
| 874 | dst_dir.append(adb_basename(src_path)); |
| 875 | } |
| 876 | |
| 877 | success &= copy_local_dir_remote(sc, src_path, dst_dir.c_str(), |
| 878 | false, false); |
Josh Gao | 5d093b2 | 2015-10-30 16:57:19 -0700 | [diff] [blame] | 879 | continue; |
Josh Gao | d13def2 | 2016-03-02 16:11:13 -0800 | [diff] [blame] | 880 | } else if (!should_push_file(st.st_mode)) { |
| 881 | sc.Warning("skipping special file '%s' (mode = 0o%o)", src_path, st.st_mode); |
| 882 | continue; |
Josh Gao | 5d093b2 | 2015-10-30 16:57:19 -0700 | [diff] [blame] | 883 | } |
| 884 | |
| 885 | std::string path_holder; |
Josh Gao | 8acf06c | 2015-11-07 15:38:19 -0800 | [diff] [blame] | 886 | if (dst_isdir) { |
Josh Gao | 5d093b2 | 2015-10-30 16:57:19 -0700 | [diff] [blame] | 887 | // If we're copying a local file to a remote directory, |
| 888 | // we really want to copy to remote_dir + "/" + local_filename. |
Josh Gao | 2e4ed0a | 2016-02-03 14:55:24 -0800 | [diff] [blame] | 889 | path_holder = dst_path; |
| 890 | if (path_holder.back() != '/') { |
| 891 | path_holder.push_back('/'); |
| 892 | } |
| 893 | path_holder += adb_basename(src_path); |
Josh Gao | 5d093b2 | 2015-10-30 16:57:19 -0700 | [diff] [blame] | 894 | dst_path = path_holder.c_str(); |
| 895 | } |
Josh Gao | a408fd2 | 2016-08-04 14:53:17 -0700 | [diff] [blame] | 896 | |
| 897 | sc.NewTransfer(); |
Elliott Hughes | 7b43fa5 | 2015-12-17 17:05:29 -0800 | [diff] [blame] | 898 | sc.SetExpectedTotalBytes(st.st_size); |
Josh Gao | 5d093b2 | 2015-10-30 16:57:19 -0700 | [diff] [blame] | 899 | success &= sync_send(sc, src_path, dst_path, st.st_mtime, st.st_mode); |
Josh Gao | a408fd2 | 2016-08-04 14:53:17 -0700 | [diff] [blame] | 900 | sc.ReportTransferRate(src_path, TransferDirection::push); |
Josh Gao | 5d093b2 | 2015-10-30 16:57:19 -0700 | [diff] [blame] | 901 | } |
| 902 | |
Josh Gao | a408fd2 | 2016-08-04 14:53:17 -0700 | [diff] [blame] | 903 | sc.ReportOverallTransferRate(TransferDirection::push); |
Josh Gao | 5d093b2 | 2015-10-30 16:57:19 -0700 | [diff] [blame] | 904 | return success; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 905 | } |
| 906 | |
Josh Gao | d9a2fd6 | 2015-12-09 14:03:30 -0800 | [diff] [blame] | 907 | static bool remote_symlink_isdir(SyncConnection& sc, const std::string& rpath) { |
| 908 | unsigned mode; |
| 909 | std::string dir_path = rpath; |
| 910 | dir_path.push_back('/'); |
| 911 | if (!sync_stat(sc, dir_path.c_str(), nullptr, &mode, nullptr)) { |
| 912 | sc.Error("failed to stat remote symlink '%s'", dir_path.c_str()); |
| 913 | return false; |
| 914 | } |
| 915 | return S_ISDIR(mode); |
| 916 | } |
| 917 | |
Josh Gao | 5c0d55a | 2016-02-25 17:12:45 -0800 | [diff] [blame] | 918 | static bool remote_build_list(SyncConnection& sc, std::vector<copyinfo>* file_list, |
| 919 | const std::string& rpath, const std::string& lpath) { |
Josh Gao | 204d21e | 2015-11-02 16:45:47 -0800 | [diff] [blame] | 920 | std::vector<copyinfo> dirlist; |
Josh Gao | d9a2fd6 | 2015-12-09 14:03:30 -0800 | [diff] [blame] | 921 | std::vector<copyinfo> linklist; |
Josh Gao | 5c0d55a | 2016-02-25 17:12:45 -0800 | [diff] [blame] | 922 | |
| 923 | // Add an entry for the current directory to ensure it gets created before pulling its contents. |
Josh Gao | 177253f | 2016-03-02 16:00:02 -0800 | [diff] [blame] | 924 | copyinfo ci(adb_dirname(lpath), adb_dirname(rpath), adb_basename(lpath), S_IFDIR); |
Josh Gao | 5c0d55a | 2016-02-25 17:12:45 -0800 | [diff] [blame] | 925 | file_list->push_back(ci); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 926 | |
Elliott Hughes | b628cb1 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 927 | // Put the files/dirs in rpath on the lists. |
Josh Gao | b3cab93 | 2015-11-30 10:21:25 -0800 | [diff] [blame] | 928 | auto callback = [&](unsigned mode, unsigned size, unsigned time, const char* name) { |
Josh Gao | a90563e | 2015-11-04 14:57:04 -0800 | [diff] [blame] | 929 | if (IsDotOrDotDot(name)) { |
| 930 | return; |
| 931 | } |
Josh Gao | 204d21e | 2015-11-02 16:45:47 -0800 | [diff] [blame] | 932 | |
Josh Gao | d9a2fd6 | 2015-12-09 14:03:30 -0800 | [diff] [blame] | 933 | copyinfo ci(lpath, rpath, name, mode); |
Josh Gao | a90563e | 2015-11-04 14:57:04 -0800 | [diff] [blame] | 934 | if (S_ISDIR(mode)) { |
| 935 | dirlist.push_back(ci); |
Josh Gao | d9a2fd6 | 2015-12-09 14:03:30 -0800 | [diff] [blame] | 936 | } else if (S_ISLNK(mode)) { |
| 937 | linklist.push_back(ci); |
Josh Gao | 204d21e | 2015-11-02 16:45:47 -0800 | [diff] [blame] | 938 | } else { |
Josh Gao | d13def2 | 2016-03-02 16:11:13 -0800 | [diff] [blame] | 939 | if (!should_pull_file(ci.mode)) { |
| 940 | sc.Warning("skipping special file '%s' (mode = 0o%o)", ci.rpath.c_str(), ci.mode); |
| 941 | ci.skip = true; |
| 942 | } |
Josh Gao | d9a2fd6 | 2015-12-09 14:03:30 -0800 | [diff] [blame] | 943 | ci.time = time; |
| 944 | ci.size = size; |
Elliott Hughes | 7b43fa5 | 2015-12-17 17:05:29 -0800 | [diff] [blame] | 945 | file_list->push_back(ci); |
Josh Gao | 204d21e | 2015-11-02 16:45:47 -0800 | [diff] [blame] | 946 | } |
| 947 | }; |
| 948 | |
Josh Gao | d20cf38 | 2015-11-03 15:23:03 -0800 | [diff] [blame] | 949 | if (!sync_ls(sc, rpath.c_str(), callback)) { |
Elliott Hughes | b628cb1 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 950 | return false; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 951 | } |
| 952 | |
Josh Gao | d9a2fd6 | 2015-12-09 14:03:30 -0800 | [diff] [blame] | 953 | // Check each symlink we found to see whether it's a file or directory. |
| 954 | for (copyinfo& link_ci : linklist) { |
| 955 | if (remote_symlink_isdir(sc, link_ci.rpath)) { |
| 956 | dirlist.emplace_back(std::move(link_ci)); |
| 957 | } else { |
Elliott Hughes | 7b43fa5 | 2015-12-17 17:05:29 -0800 | [diff] [blame] | 958 | file_list->emplace_back(std::move(link_ci)); |
Josh Gao | d9a2fd6 | 2015-12-09 14:03:30 -0800 | [diff] [blame] | 959 | } |
| 960 | } |
| 961 | |
Elliott Hughes | b628cb1 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 962 | // Recurse into each directory we found. |
Josh Gao | 204d21e | 2015-11-02 16:45:47 -0800 | [diff] [blame] | 963 | while (!dirlist.empty()) { |
| 964 | copyinfo current = dirlist.back(); |
| 965 | dirlist.pop_back(); |
Elliott Hughes | 7b43fa5 | 2015-12-17 17:05:29 -0800 | [diff] [blame] | 966 | if (!remote_build_list(sc, file_list, current.rpath, current.lpath)) { |
Elliott Hughes | b628cb1 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 967 | return false; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 968 | } |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 969 | } |
| 970 | |
Elliott Hughes | b628cb1 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 971 | return true; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 972 | } |
| 973 | |
Josh Gao | a3b6a06 | 2015-11-09 11:12:14 -0800 | [diff] [blame] | 974 | static int set_time_and_mode(const std::string& lpath, time_t time, |
| 975 | unsigned int mode) { |
Greg Hackmann | 8b68914 | 2014-05-06 08:48:18 -0700 | [diff] [blame] | 976 | struct utimbuf times = { time, time }; |
Josh Gao | a3b6a06 | 2015-11-09 11:12:14 -0800 | [diff] [blame] | 977 | int r1 = utime(lpath.c_str(), ×); |
Lajos Molnar | 4e23e3c | 2013-04-19 12:41:09 -0700 | [diff] [blame] | 978 | |
| 979 | /* use umask for permissions */ |
Josh Gao | 204d21e | 2015-11-02 16:45:47 -0800 | [diff] [blame] | 980 | mode_t mask = umask(0000); |
Lajos Molnar | 4e23e3c | 2013-04-19 12:41:09 -0700 | [diff] [blame] | 981 | umask(mask); |
Josh Gao | a3b6a06 | 2015-11-09 11:12:14 -0800 | [diff] [blame] | 982 | int r2 = chmod(lpath.c_str(), mode & ~mask); |
Lajos Molnar | 4e23e3c | 2013-04-19 12:41:09 -0700 | [diff] [blame] | 983 | |
Spencer Low | 28bc2cb | 2015-11-07 18:51:54 -0800 | [diff] [blame] | 984 | return r1 ? r1 : r2; |
Lajos Molnar | 4e23e3c | 2013-04-19 12:41:09 -0700 | [diff] [blame] | 985 | } |
| 986 | |
Josh Gao | d20cf38 | 2015-11-03 15:23:03 -0800 | [diff] [blame] | 987 | static bool copy_remote_dir_local(SyncConnection& sc, std::string rpath, |
| 988 | std::string lpath, bool copy_attrs) { |
Josh Gao | a408fd2 | 2016-08-04 14:53:17 -0700 | [diff] [blame] | 989 | sc.NewTransfer(); |
| 990 | |
Elliott Hughes | b628cb1 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 991 | // Make sure that both directory paths end in a slash. |
Josh Gao | a5cea71 | 2015-11-07 15:27:26 -0800 | [diff] [blame] | 992 | // Both paths are known to be nonempty, so we don't need to check. |
Elliott Hughes | 7b43fa5 | 2015-12-17 17:05:29 -0800 | [diff] [blame] | 993 | ensure_trailing_separators(lpath, rpath); |
Riley Andrews | c736a94 | 2014-12-12 13:12:36 -0800 | [diff] [blame] | 994 | |
Elliott Hughes | b628cb1 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 995 | // Recursively build the list of files to copy. |
Elliott Hughes | 7b43fa5 | 2015-12-17 17:05:29 -0800 | [diff] [blame] | 996 | sc.Printf("pull: building file list..."); |
| 997 | std::vector<copyinfo> file_list; |
| 998 | if (!remote_build_list(sc, &file_list, rpath.c_str(), lpath.c_str())) { |
Josh Gao | 204d21e | 2015-11-02 16:45:47 -0800 | [diff] [blame] | 999 | return false; |
| 1000 | } |
Elliott Hughes | b628cb1 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 1001 | |
Elliott Hughes | 7b43fa5 | 2015-12-17 17:05:29 -0800 | [diff] [blame] | 1002 | sc.ComputeExpectedTotalBytes(file_list); |
| 1003 | |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1004 | int skipped = 0; |
Elliott Hughes | 7b43fa5 | 2015-12-17 17:05:29 -0800 | [diff] [blame] | 1005 | for (const copyinfo &ci : file_list) { |
Josh Gao | cb094c6 | 2015-11-03 14:44:04 -0800 | [diff] [blame] | 1006 | if (!ci.skip) { |
Josh Gao | a90563e | 2015-11-04 14:57:04 -0800 | [diff] [blame] | 1007 | if (S_ISDIR(ci.mode)) { |
| 1008 | // Entry is for an empty directory, create it and continue. |
| 1009 | // TODO(b/25457350): We don't preserve permissions on directories. |
Josh Gao | a3b6a06 | 2015-11-09 11:12:14 -0800 | [diff] [blame] | 1010 | if (!mkdirs(ci.lpath)) { |
Josh Gao | a90563e | 2015-11-04 14:57:04 -0800 | [diff] [blame] | 1011 | sc.Error("failed to create directory '%s': %s", |
Josh Gao | a3b6a06 | 2015-11-09 11:12:14 -0800 | [diff] [blame] | 1012 | ci.lpath.c_str(), strerror(errno)); |
Josh Gao | a90563e | 2015-11-04 14:57:04 -0800 | [diff] [blame] | 1013 | return false; |
| 1014 | } |
Josh Gao | a90563e | 2015-11-04 14:57:04 -0800 | [diff] [blame] | 1015 | continue; |
| 1016 | } |
| 1017 | |
Josh Gao | a3b6a06 | 2015-11-09 11:12:14 -0800 | [diff] [blame] | 1018 | if (!sync_recv(sc, ci.rpath.c_str(), ci.lpath.c_str())) { |
Elliott Hughes | b628cb1 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 1019 | return false; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1020 | } |
Lajos Molnar | 4e23e3c | 2013-04-19 12:41:09 -0700 | [diff] [blame] | 1021 | |
Josh Gao | a3b6a06 | 2015-11-09 11:12:14 -0800 | [diff] [blame] | 1022 | if (copy_attrs && set_time_and_mode(ci.lpath, ci.time, ci.mode)) { |
Elliott Hughes | b628cb1 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 1023 | return false; |
Lajos Molnar | 4e23e3c | 2013-04-19 12:41:09 -0700 | [diff] [blame] | 1024 | } |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1025 | } else { |
| 1026 | skipped++; |
| 1027 | } |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1028 | } |
| 1029 | |
Josh Gao | a408fd2 | 2016-08-04 14:53:17 -0700 | [diff] [blame] | 1030 | sc.RecordFilesSkipped(skipped); |
| 1031 | sc.ReportTransferRate(rpath, TransferDirection::pull); |
Elliott Hughes | b628cb1 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 1032 | return true; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1033 | } |
| 1034 | |
Josh Gao | 5d093b2 | 2015-10-30 16:57:19 -0700 | [diff] [blame] | 1035 | bool do_sync_pull(const std::vector<const char*>& srcs, const char* dst, |
Felipe Leme | b644703 | 2016-04-01 17:43:27 -0700 | [diff] [blame] | 1036 | bool copy_attrs, const char* name) { |
Elliott Hughes | b628cb1 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 1037 | SyncConnection sc; |
| 1038 | if (!sc.IsValid()) return false; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1039 | |
Josh Gao | 5d093b2 | 2015-10-30 16:57:19 -0700 | [diff] [blame] | 1040 | bool success = true; |
Josh Gao | 5d093b2 | 2015-10-30 16:57:19 -0700 | [diff] [blame] | 1041 | struct stat st; |
Josh Gao | a5cea71 | 2015-11-07 15:27:26 -0800 | [diff] [blame] | 1042 | bool dst_exists = true; |
| 1043 | |
| 1044 | if (stat(dst, &st) == -1) { |
| 1045 | dst_exists = false; |
| 1046 | |
| 1047 | // If we're only pulling one path, the destination path might point to |
Josh Gao | 5d093b2 | 2015-10-30 16:57:19 -0700 | [diff] [blame] | 1048 | // a path that doesn't exist yet. |
Josh Gao | a5cea71 | 2015-11-07 15:27:26 -0800 | [diff] [blame] | 1049 | if (srcs.size() == 1 && errno == ENOENT) { |
| 1050 | // However, its parent must exist. |
| 1051 | struct stat parent_st; |
| 1052 | if (stat(adb_dirname(dst).c_str(), &parent_st) == -1) { |
| 1053 | sc.Error("cannot create file/directory '%s': %s", dst, strerror(errno)); |
| 1054 | return false; |
| 1055 | } |
| 1056 | } else { |
| 1057 | sc.Error("failed to access '%s': %s", dst, strerror(errno)); |
Josh Gao | 5d093b2 | 2015-10-30 16:57:19 -0700 | [diff] [blame] | 1058 | return false; |
| 1059 | } |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1060 | } |
| 1061 | |
Josh Gao | 8acf06c | 2015-11-07 15:38:19 -0800 | [diff] [blame] | 1062 | bool dst_isdir = dst_exists && S_ISDIR(st.st_mode); |
| 1063 | if (!dst_isdir) { |
Josh Gao | 5d093b2 | 2015-10-30 16:57:19 -0700 | [diff] [blame] | 1064 | if (srcs.size() > 1) { |
| 1065 | sc.Error("target '%s' is not a directory", dst); |
Elliott Hughes | b628cb1 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 1066 | return false; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1067 | } else { |
Josh Gao | 5d093b2 | 2015-10-30 16:57:19 -0700 | [diff] [blame] | 1068 | size_t dst_len = strlen(dst); |
Josh Gao | a5cea71 | 2015-11-07 15:27:26 -0800 | [diff] [blame] | 1069 | |
| 1070 | // A path that ends with a slash doesn't have to be a directory if |
| 1071 | // it doesn't exist yet. |
Josh Gao | a3b6a06 | 2015-11-09 11:12:14 -0800 | [diff] [blame] | 1072 | if (adb_is_separator(dst[dst_len - 1]) && dst_exists) { |
Josh Gao | 5d093b2 | 2015-10-30 16:57:19 -0700 | [diff] [blame] | 1073 | sc.Error("failed to access '%s': Not a directory", dst); |
Elliott Hughes | b628cb1 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 1074 | return false; |
Elliott Hughes | d189cfb | 2015-07-30 17:42:01 -0700 | [diff] [blame] | 1075 | } |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1076 | } |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1077 | } |
Elliott Hughes | b628cb1 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 1078 | |
Josh Gao | 5d093b2 | 2015-10-30 16:57:19 -0700 | [diff] [blame] | 1079 | for (const char* src_path : srcs) { |
| 1080 | const char* dst_path = dst; |
Elliott Hughes | 7b43fa5 | 2015-12-17 17:05:29 -0800 | [diff] [blame] | 1081 | unsigned src_mode, src_time, src_size; |
| 1082 | if (!sync_stat(sc, src_path, &src_time, &src_mode, &src_size)) { |
Josh Gao | d9a2fd6 | 2015-12-09 14:03:30 -0800 | [diff] [blame] | 1083 | sc.Error("failed to stat remote object '%s'", src_path); |
Josh Gao | a5cea71 | 2015-11-07 15:27:26 -0800 | [diff] [blame] | 1084 | return false; |
| 1085 | } |
| 1086 | if (src_mode == 0) { |
Josh Gao | 5d093b2 | 2015-10-30 16:57:19 -0700 | [diff] [blame] | 1087 | sc.Error("remote object '%s' does not exist", src_path); |
| 1088 | success = false; |
| 1089 | continue; |
| 1090 | } |
| 1091 | |
Josh Gao | d9a2fd6 | 2015-12-09 14:03:30 -0800 | [diff] [blame] | 1092 | bool src_isdir = S_ISDIR(src_mode); |
| 1093 | if (S_ISLNK(src_mode)) { |
| 1094 | src_isdir = remote_symlink_isdir(sc, src_path); |
| 1095 | } |
| 1096 | |
Josh Gao | d9a2fd6 | 2015-12-09 14:03:30 -0800 | [diff] [blame] | 1097 | if (src_isdir) { |
Josh Gao | 8acf06c | 2015-11-07 15:38:19 -0800 | [diff] [blame] | 1098 | std::string dst_dir = dst; |
| 1099 | |
| 1100 | // If the destination path existed originally, the source directory |
| 1101 | // should be copied as a child of the destination. |
| 1102 | if (dst_exists) { |
| 1103 | if (!dst_isdir) { |
| 1104 | sc.Error("target '%s' is not a directory", dst); |
| 1105 | return false; |
| 1106 | } |
| 1107 | if (!adb_is_separator(dst_dir.back())) { |
| 1108 | dst_dir.push_back(OS_PATH_SEPARATOR); |
| 1109 | } |
| 1110 | dst_dir.append(adb_basename(src_path)); |
| 1111 | } |
| 1112 | |
Josh Gao | d9a2fd6 | 2015-12-09 14:03:30 -0800 | [diff] [blame] | 1113 | success &= copy_remote_dir_local(sc, src_path, dst_dir.c_str(), copy_attrs); |
Josh Gao | 5d093b2 | 2015-10-30 16:57:19 -0700 | [diff] [blame] | 1114 | continue; |
Josh Gao | d13def2 | 2016-03-02 16:11:13 -0800 | [diff] [blame] | 1115 | } else if (!should_pull_file(src_mode)) { |
| 1116 | sc.Warning("skipping special file '%s' (mode = 0o%o)", src_path, src_mode); |
| 1117 | continue; |
| 1118 | } |
Josh Gao | d9a2fd6 | 2015-12-09 14:03:30 -0800 | [diff] [blame] | 1119 | |
Josh Gao | d13def2 | 2016-03-02 16:11:13 -0800 | [diff] [blame] | 1120 | std::string path_holder; |
| 1121 | if (dst_isdir) { |
| 1122 | // If we're copying a remote file to a local directory, we |
| 1123 | // really want to copy to local_dir + OS_PATH_SEPARATOR + |
| 1124 | // basename(remote). |
| 1125 | path_holder = android::base::StringPrintf("%s%c%s", dst_path, OS_PATH_SEPARATOR, |
| 1126 | adb_basename(src_path).c_str()); |
| 1127 | dst_path = path_holder.c_str(); |
| 1128 | } |
Josh Gao | d9a2fd6 | 2015-12-09 14:03:30 -0800 | [diff] [blame] | 1129 | |
Josh Gao | a408fd2 | 2016-08-04 14:53:17 -0700 | [diff] [blame] | 1130 | sc.NewTransfer(); |
Josh Gao | d13def2 | 2016-03-02 16:11:13 -0800 | [diff] [blame] | 1131 | sc.SetExpectedTotalBytes(src_size); |
Felipe Leme | b644703 | 2016-04-01 17:43:27 -0700 | [diff] [blame] | 1132 | if (!sync_recv(sc, src_path, dst_path, name)) { |
Josh Gao | d13def2 | 2016-03-02 16:11:13 -0800 | [diff] [blame] | 1133 | success = false; |
| 1134 | continue; |
| 1135 | } |
| 1136 | |
| 1137 | if (copy_attrs && set_time_and_mode(dst_path, src_time, src_mode) != 0) { |
| 1138 | success = false; |
| 1139 | continue; |
Josh Gao | 5d093b2 | 2015-10-30 16:57:19 -0700 | [diff] [blame] | 1140 | } |
Josh Gao | a408fd2 | 2016-08-04 14:53:17 -0700 | [diff] [blame] | 1141 | sc.ReportTransferRate(src_path, TransferDirection::pull); |
Josh Gao | 5d093b2 | 2015-10-30 16:57:19 -0700 | [diff] [blame] | 1142 | } |
| 1143 | |
Josh Gao | a408fd2 | 2016-08-04 14:53:17 -0700 | [diff] [blame] | 1144 | sc.ReportOverallTransferRate(TransferDirection::pull); |
Josh Gao | 5d093b2 | 2015-10-30 16:57:19 -0700 | [diff] [blame] | 1145 | return success; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1146 | } |
| 1147 | |
Elliott Hughes | b628cb1 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 1148 | bool do_sync_sync(const std::string& lpath, const std::string& rpath, bool list_only) { |
Elliott Hughes | b628cb1 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 1149 | SyncConnection sc; |
| 1150 | if (!sc.IsValid()) return false; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1151 | |
Josh Gao | a408fd2 | 2016-08-04 14:53:17 -0700 | [diff] [blame] | 1152 | bool success = copy_local_dir_remote(sc, lpath, rpath, true, list_only); |
| 1153 | if (!list_only) { |
| 1154 | sc.ReportOverallTransferRate(TransferDirection::push); |
| 1155 | } |
| 1156 | return success; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1157 | } |