blob: 22bd2f29759ca8fb54d044790da87fcaa1f0b09b [file] [log] [blame]
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -08001/*
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 Albertb302d122015-02-24 15:51:19 -080017#include <dirent.h>
Spencer Low803451e2015-05-13 00:02:55 -070018#include <inttypes.h>
Dan Albertb302d122015-02-24 15:51:19 -080019#include <limits.h>
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080020#include <stdio.h>
21#include <stdlib.h>
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080022#include <sys/stat.h>
23#include <sys/time.h>
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080024#include <sys/types.h>
Dan Albertb302d122015-02-24 15:51:19 -080025#include <time.h>
Elliott Hughesdde00be2015-09-27 12:55:37 -070026#include <unistd.h>
Greg Hackmann8b689142014-05-06 08:48:18 -070027#include <utime.h>
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080028
Josh Gaoa408fd22016-08-04 14:53:17 -070029#include <chrono>
Josh Gao204d21e2015-11-02 16:45:47 -080030#include <functional>
Elliott Hughes4e7848d2015-08-24 14:49:43 -070031#include <memory>
Josh Gaoa408fd22016-08-04 14:53:17 -070032#include <sstream>
33#include <string>
Elliott Hughes6c73bfc2015-10-27 13:40:35 -070034#include <vector>
Elliott Hughes4e7848d2015-08-24 14:49:43 -070035
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080036#include "sysdeps.h"
Dan Albertb302d122015-02-24 15:51:19 -080037
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080038#include "adb.h"
39#include "adb_client.h"
Dan Albert66a91b02015-02-24 21:26:58 -080040#include "adb_io.h"
Alex Valléee9163152015-05-06 17:22:25 -040041#include "adb_utils.h"
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080042#include "file_sync_service.h"
Elliott Hughesf5cdc1d2015-10-27 16:03:15 -070043#include "line_printer.h"
Josh Gaoa2cf3752016-12-05 17:11:34 -080044#include "sysdeps/errno.h"
Josh Gao8998a8d2016-11-18 15:17:07 -080045#include "sysdeps/stat.h"
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080046
Elliott Hughesf55ead92015-12-04 22:00:26 -080047#include <android-base/file.h>
48#include <android-base/strings.h>
49#include <android-base/stringprintf.h>
Elliott Hughesd189cfb2015-07-30 17:42:01 -070050
Elliott Hughesb628cb12015-08-03 10:38:08 -070051struct syncsendbuf {
52 unsigned id;
53 unsigned size;
54 char data[SYNC_DATA_MAX];
55};
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080056
Elliott Hughes7b43fa52015-12-17 17:05:29 -080057static 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 Gaod13def22016-03-02 16:11:13 -080066static bool should_pull_file(mode_t mode) {
Josh Gao8998a8d2016-11-18 15:17:07 -080067 return S_ISREG(mode) || S_ISBLK(mode) || S_ISCHR(mode);
Josh Gaod13def22016-03-02 16:11:13 -080068}
69
70static bool should_push_file(mode_t mode) {
Josh Gao8998a8d2016-11-18 15:17:07 -080071 return S_ISREG(mode) || S_ISLNK(mode);
Josh Gaod13def22016-03-02 16:11:13 -080072}
73
Elliott Hughes7b43fa52015-12-17 17:05:29 -080074struct copyinfo {
75 std::string lpath;
76 std::string rpath;
Josh Gaoa2cf3752016-12-05 17:11:34 -080077 int64_t time = 0;
78 uint32_t mode;
Elliott Hughes7b43fa52015-12-17 17:05:29 -080079 uint64_t size = 0;
80 bool skip = false;
81
82 copyinfo(const std::string& local_path,
83 const std::string& remote_path,
84 const std::string& name,
85 unsigned int mode)
86 : lpath(local_path), rpath(remote_path), mode(mode) {
87 ensure_trailing_separators(lpath, rpath);
88 lpath.append(name);
89 rpath.append(name);
90 if (S_ISDIR(mode)) {
91 ensure_trailing_separators(lpath, rpath);
92 }
93 }
94};
95
Josh Gaoa408fd22016-08-04 14:53:17 -070096enum class TransferDirection {
97 push,
98 pull,
99};
100
101struct TransferLedger {
102 std::chrono::steady_clock::time_point start_time;
103 uint64_t files_transferred;
104 uint64_t files_skipped;
105 uint64_t bytes_transferred;
106 uint64_t bytes_expected;
107 bool expect_multiple_files;
108
109 TransferLedger() {
110 Reset();
111 }
112
113 bool operator==(const TransferLedger& other) const {
114 return files_transferred == other.files_transferred &&
115 files_skipped == other.files_skipped && bytes_transferred == other.bytes_transferred;
116 }
117
118 bool operator!=(const TransferLedger& other) const {
119 return !(*this == other);
120 }
121
122 void Reset() {
123 start_time = std::chrono::steady_clock::now();
124 files_transferred = 0;
125 files_skipped = 0;
126 bytes_transferred = 0;
127 bytes_expected = 0;
128 }
129
130 std::string TransferRate() {
131 if (bytes_transferred == 0) return "";
132
133 std::chrono::duration<double> duration;
134 duration = std::chrono::steady_clock::now() - start_time;
135
136 double s = duration.count();
137 if (s == 0) {
138 return "";
139 }
140 double rate = (static_cast<double>(bytes_transferred) / s) / (1024 * 1024);
141 return android::base::StringPrintf(" %.1f MB/s (%" PRIu64 " bytes in %.3fs)", rate,
142 bytes_transferred, s);
143 }
144
145 void ReportProgress(LinePrinter& lp, const std::string& file, uint64_t file_copied_bytes,
146 uint64_t file_total_bytes) {
147 char overall_percentage_str[5] = "?";
Josh Gao4459c132016-11-17 16:02:36 -0800148 if (bytes_expected != 0 && bytes_transferred <= bytes_expected) {
Josh Gaoa408fd22016-08-04 14:53:17 -0700149 int overall_percentage = static_cast<int>(bytes_transferred * 100 / bytes_expected);
150 // If we're pulling symbolic links, we'll pull the target of the link rather than
151 // just create a local link, and that will cause us to go over 100%.
152 if (overall_percentage <= 100) {
153 snprintf(overall_percentage_str, sizeof(overall_percentage_str), "%d%%",
154 overall_percentage);
155 }
156 }
157
158 std::string output;
159 if (file_copied_bytes > file_total_bytes || file_total_bytes == 0) {
160 // This case can happen if we're racing against something that wrote to the file
161 // between our stat and our read, or if we're reading a magic file that lies about
162 // its size. Just show how much we've copied.
163 output = android::base::StringPrintf("[%4s] %s: %" PRId64 "/?", overall_percentage_str,
164 file.c_str(), file_copied_bytes);
165 } else {
166 // If we're transferring multiple files, we want to know how far through the current
167 // file we are, as well as the overall percentage.
168 if (expect_multiple_files) {
169 int file_percentage = static_cast<int>(file_copied_bytes * 100 / file_total_bytes);
170 output = android::base::StringPrintf("[%4s] %s: %d%%", overall_percentage_str,
171 file.c_str(), file_percentage);
172 } else {
173 output =
174 android::base::StringPrintf("[%4s] %s", overall_percentage_str, file.c_str());
175 }
176 }
177 lp.Print(output, LinePrinter::LineType::INFO);
178 }
179
180 void ReportTransferRate(LinePrinter& lp, const std::string& name, TransferDirection direction) {
181 const char* direction_str = (direction == TransferDirection::push) ? "pushed" : "pulled";
182 std::stringstream ss;
183 if (!name.empty()) {
184 ss << name << ": ";
185 }
186 ss << files_transferred << " file" << ((files_transferred == 1) ? "" : "s") << " "
187 << direction_str << ".";
188 if (files_skipped > 0) {
189 ss << " " << files_skipped << " file" << ((files_skipped == 1) ? "" : "s")
190 << " skipped.";
191 }
192 ss << TransferRate();
193
194 lp.Print(ss.str(), LinePrinter::LineType::INFO);
195 lp.KeepInfoLine();
196 }
197};
198
Elliott Hughesb628cb12015-08-03 10:38:08 -0700199class SyncConnection {
200 public:
Josh Gaoa408fd22016-08-04 14:53:17 -0700201 SyncConnection() : expect_done_(false) {
Elliott Hughesb628cb12015-08-03 10:38:08 -0700202 max = SYNC_DATA_MAX; // TODO: decide at runtime.
203
204 std::string error;
Josh Gaoa2cf3752016-12-05 17:11:34 -0800205 FeatureSet features;
206 if (!adb_get_feature_set(&features, &error)) {
207 fd = -1;
208 Error("failed to get feature set: %s", error.c_str());
209 } else {
210 have_stat_v2_ = CanUseFeature(features, kFeatureStat2);
211 fd = adb_connect("sync:", &error);
212 if (fd < 0) {
213 Error("connect failed: %s", error.c_str());
214 }
Elliott Hughesb628cb12015-08-03 10:38:08 -0700215 }
216 }
217
218 ~SyncConnection() {
219 if (!IsValid()) return;
220
Spencer Lowcc4a4b12015-10-14 17:32:44 -0700221 if (SendQuit()) {
222 // We sent a quit command, so the server should be doing orderly
223 // shutdown soon. But if we encountered an error while we were using
224 // the connection, the server might still be sending data (before
225 // doing orderly shutdown), in which case we won't wait for all of
226 // the data nor the coming orderly shutdown. In the common success
227 // case, this will wait for the server to do orderly shutdown.
228 ReadOrderlyShutdown(fd);
229 }
Elliott Hughesb628cb12015-08-03 10:38:08 -0700230 adb_close(fd);
Elliott Hughes78a37a52015-12-08 16:01:15 -0800231
232 line_printer_.KeepInfoLine();
Elliott Hughesb628cb12015-08-03 10:38:08 -0700233 }
234
235 bool IsValid() { return fd >= 0; }
236
Josh Gao2db75ce2016-02-19 15:55:55 -0800237 bool ReceivedError(const char* from, const char* to) {
238 adb_pollfd pfd = {.fd = fd, .events = POLLIN};
239 int rc = adb_poll(&pfd, 1, 0);
240 if (rc < 0) {
241 Error("failed to poll: %s", strerror(errno));
242 return true;
243 }
244 return rc != 0;
245 }
246
Josh Gaoa408fd22016-08-04 14:53:17 -0700247 void NewTransfer() {
248 current_ledger_.Reset();
249 }
250
251 void RecordBytesTransferred(size_t bytes) {
252 current_ledger_.bytes_transferred += bytes;
253 global_ledger_.bytes_transferred += bytes;
254 }
255
256 void RecordFilesTransferred(size_t files) {
257 current_ledger_.files_transferred += files;
258 global_ledger_.files_transferred += files;
259 }
260
261 void RecordFilesSkipped(size_t files) {
262 current_ledger_.files_skipped += files;
263 global_ledger_.files_skipped += files;
264 }
265
266 void ReportProgress(const std::string& file, uint64_t file_copied_bytes,
267 uint64_t file_total_bytes) {
268 current_ledger_.ReportProgress(line_printer_, file, file_copied_bytes, file_total_bytes);
269 }
270
271 void ReportTransferRate(const std::string& file, TransferDirection direction) {
272 current_ledger_.ReportTransferRate(line_printer_, file, direction);
273 }
274
275 void ReportOverallTransferRate(TransferDirection direction) {
276 if (current_ledger_ != global_ledger_) {
277 global_ledger_.ReportTransferRate(line_printer_, "", direction);
278 }
279 }
280
Elliott Hughesdde00be2015-09-27 12:55:37 -0700281 bool SendRequest(int id, const char* path_and_mode) {
282 size_t path_length = strlen(path_and_mode);
283 if (path_length > 1024) {
Elliott Hughesf5cdc1d2015-10-27 16:03:15 -0700284 Error("SendRequest failed: path too long: %zu", path_length);
Elliott Hughesdde00be2015-09-27 12:55:37 -0700285 errno = ENAMETOOLONG;
286 return false;
287 }
288
289 // Sending header and payload in a single write makes a noticeable
290 // difference to "adb sync" performance.
Elliott Hughes6c73bfc2015-10-27 13:40:35 -0700291 std::vector<char> buf(sizeof(SyncRequest) + path_length);
292 SyncRequest* req = reinterpret_cast<SyncRequest*>(&buf[0]);
Elliott Hughesdde00be2015-09-27 12:55:37 -0700293 req->id = id;
294 req->path_length = path_length;
295 char* data = reinterpret_cast<char*>(req + 1);
296 memcpy(data, path_and_mode, path_length);
297
Elliott Hughes6c73bfc2015-10-27 13:40:35 -0700298 return WriteFdExactly(fd, &buf[0], buf.size());
Elliott Hughesdde00be2015-09-27 12:55:37 -0700299 }
300
Josh Gaoa2cf3752016-12-05 17:11:34 -0800301 bool SendStat(const char* path_and_mode) {
302 if (!have_stat_v2_) {
303 errno = ENOTSUP;
304 return false;
305 }
306 return SendRequest(ID_STAT_V2, path_and_mode);
307 }
308
309 bool SendLstat(const char* path_and_mode) {
310 if (have_stat_v2_) {
311 return SendRequest(ID_LSTAT_V2, path_and_mode);
312 } else {
313 return SendRequest(ID_LSTAT_V1, path_and_mode);
314 }
315 }
316
317 bool FinishStat(struct stat* st) {
318 syncmsg msg;
319
320 memset(st, 0, sizeof(*st));
321 if (have_stat_v2_) {
322 if (!ReadFdExactly(fd, &msg.stat_v2, sizeof(msg.stat_v2))) {
323 fatal_errno("protocol fault: failed to read stat response");
324 }
325
326 if (msg.stat_v2.id != ID_LSTAT_V2 && msg.stat_v2.id != ID_STAT_V2) {
327 fatal_errno("protocol fault: stat response has wrong message id: %" PRIx32,
328 msg.stat_v2.id);
329 }
330
331 if (msg.stat_v2.error != 0) {
332 errno = errno_from_wire(msg.stat_v2.error);
333 return false;
334 }
335
336 st->st_dev = msg.stat_v2.dev;
337 st->st_ino = msg.stat_v2.ino;
338 st->st_mode = msg.stat_v2.mode;
339 st->st_nlink = msg.stat_v2.nlink;
340 st->st_uid = msg.stat_v2.uid;
341 st->st_gid = msg.stat_v2.gid;
342 st->st_size = msg.stat_v2.size;
343 st->st_atime = msg.stat_v2.atime;
344 st->st_mtime = msg.stat_v2.mtime;
345 st->st_ctime = msg.stat_v2.ctime;
346 return true;
347 } else {
348 if (!ReadFdExactly(fd, &msg.stat_v1, sizeof(msg.stat_v1))) {
349 fatal_errno("protocol fault: failed to read stat response");
350 }
351
352 if (msg.stat_v1.id != ID_LSTAT_V1) {
353 fatal_errno("protocol fault: stat response has wrong message id: %" PRIx32,
354 msg.stat_v1.id);
355 }
356
357 if (msg.stat_v1.mode == 0 && msg.stat_v1.size == 0 && msg.stat_v1.time == 0) {
358 // There's no way for us to know what the error was.
359 errno = ENOPROTOOPT;
360 return false;
361 }
362
363 st->st_mode = msg.stat_v1.mode;
364 st->st_size = msg.stat_v1.size;
365 st->st_ctime = msg.stat_v1.time;
366 st->st_mtime = msg.stat_v1.time;
367 }
368
369 return true;
370 }
371
Elliott Hughesdde00be2015-09-27 12:55:37 -0700372 // Sending header, payload, and footer in a single write makes a huge
373 // difference to "adb sync" performance.
374 bool SendSmallFile(const char* path_and_mode,
Elliott Hughes54e3efe2015-11-20 22:01:06 -0800375 const char* lpath, const char* rpath,
Elliott Hughes7275d802015-11-20 17:35:17 -0800376 unsigned mtime,
377 const char* data, size_t data_length) {
Elliott Hughesdde00be2015-09-27 12:55:37 -0700378 size_t path_length = strlen(path_and_mode);
379 if (path_length > 1024) {
Elliott Hughesf5cdc1d2015-10-27 16:03:15 -0700380 Error("SendSmallFile failed: path too long: %zu", path_length);
Elliott Hughesdde00be2015-09-27 12:55:37 -0700381 errno = ENAMETOOLONG;
382 return false;
383 }
384
Elliott Hughes6c73bfc2015-10-27 13:40:35 -0700385 std::vector<char> buf(sizeof(SyncRequest) + path_length +
Elliott Hughes7275d802015-11-20 17:35:17 -0800386 sizeof(SyncRequest) + data_length +
387 sizeof(SyncRequest));
Elliott Hughes6c73bfc2015-10-27 13:40:35 -0700388 char* p = &buf[0];
Elliott Hughesdde00be2015-09-27 12:55:37 -0700389
390 SyncRequest* req_send = reinterpret_cast<SyncRequest*>(p);
391 req_send->id = ID_SEND;
392 req_send->path_length = path_length;
393 p += sizeof(SyncRequest);
394 memcpy(p, path_and_mode, path_length);
395 p += path_length;
396
397 SyncRequest* req_data = reinterpret_cast<SyncRequest*>(p);
398 req_data->id = ID_DATA;
399 req_data->path_length = data_length;
400 p += sizeof(SyncRequest);
401 memcpy(p, data, data_length);
402 p += data_length;
403
404 SyncRequest* req_done = reinterpret_cast<SyncRequest*>(p);
405 req_done->id = ID_DONE;
406 req_done->path_length = mtime;
407 p += sizeof(SyncRequest);
408
Elliott Hughes54e3efe2015-11-20 22:01:06 -0800409 WriteOrDie(lpath, rpath, &buf[0], (p - &buf[0]));
Josh Gao2db75ce2016-02-19 15:55:55 -0800410 expect_done_ = true;
Josh Gaoa408fd22016-08-04 14:53:17 -0700411
412 // RecordFilesTransferred gets called in CopyDone.
413 RecordBytesTransferred(data_length);
Josh Gao37e8ab42016-03-01 11:46:02 -0800414 ReportProgress(rpath, data_length, data_length);
Elliott Hughesdde00be2015-09-27 12:55:37 -0700415 return true;
416 }
417
Elliott Hughes7275d802015-11-20 17:35:17 -0800418 bool SendLargeFile(const char* path_and_mode,
419 const char* lpath, const char* rpath,
420 unsigned mtime) {
421 if (!SendRequest(ID_SEND, path_and_mode)) {
422 Error("failed to send ID_SEND message '%s': %s", path_and_mode, strerror(errno));
423 return false;
424 }
425
426 struct stat st;
427 if (stat(lpath, &st) == -1) {
428 Error("cannot stat '%s': %s", lpath, strerror(errno));
429 return false;
430 }
431
432 uint64_t total_size = st.st_size;
433 uint64_t bytes_copied = 0;
434
435 int lfd = adb_open(lpath, O_RDONLY);
436 if (lfd < 0) {
Elliott Hughes54e3efe2015-11-20 22:01:06 -0800437 Error("opening '%s' locally failed: %s", lpath, strerror(errno));
Elliott Hughes7275d802015-11-20 17:35:17 -0800438 return false;
439 }
440
441 syncsendbuf sbuf;
442 sbuf.id = ID_DATA;
443 while (true) {
Elliott Hughes54e3efe2015-11-20 22:01:06 -0800444 int bytes_read = adb_read(lfd, sbuf.data, max);
445 if (bytes_read == -1) {
446 Error("reading '%s' locally failed: %s", lpath, strerror(errno));
447 adb_close(lfd);
448 return false;
449 } else if (bytes_read == 0) {
Elliott Hughes7275d802015-11-20 17:35:17 -0800450 break;
451 }
452
Elliott Hughes54e3efe2015-11-20 22:01:06 -0800453 sbuf.size = bytes_read;
454 WriteOrDie(lpath, rpath, &sbuf, sizeof(SyncRequest) + bytes_read);
Elliott Hughes7275d802015-11-20 17:35:17 -0800455
Josh Gaoa408fd22016-08-04 14:53:17 -0700456 RecordBytesTransferred(bytes_read);
Elliott Hughes54e3efe2015-11-20 22:01:06 -0800457 bytes_copied += bytes_read;
Elliott Hughes7275d802015-11-20 17:35:17 -0800458
Josh Gao2db75ce2016-02-19 15:55:55 -0800459 // Check to see if we've received an error from the other side.
460 if (ReceivedError(lpath, rpath)) {
461 break;
462 }
463
Elliott Hughes7b43fa52015-12-17 17:05:29 -0800464 ReportProgress(rpath, bytes_copied, total_size);
Elliott Hughes7275d802015-11-20 17:35:17 -0800465 }
466
467 adb_close(lfd);
468
469 syncmsg msg;
470 msg.data.id = ID_DONE;
471 msg.data.size = mtime;
Josh Gao2db75ce2016-02-19 15:55:55 -0800472 expect_done_ = true;
Josh Gaoa408fd22016-08-04 14:53:17 -0700473
474 // RecordFilesTransferred gets called in CopyDone.
Elliott Hughes54e3efe2015-11-20 22:01:06 -0800475 return WriteOrDie(lpath, rpath, &msg.data, sizeof(msg.data));
Elliott Hughes7275d802015-11-20 17:35:17 -0800476 }
477
Elliott Hughesdde00be2015-09-27 12:55:37 -0700478 bool CopyDone(const char* from, const char* to) {
479 syncmsg msg;
480 if (!ReadFdExactly(fd, &msg.status, sizeof(msg.status))) {
Josh Gao2db75ce2016-02-19 15:55:55 -0800481 Error("failed to copy '%s' to '%s': couldn't read from device", from, to);
Elliott Hughesdde00be2015-09-27 12:55:37 -0700482 return false;
483 }
484 if (msg.status.id == ID_OKAY) {
Josh Gao2db75ce2016-02-19 15:55:55 -0800485 if (expect_done_) {
486 expect_done_ = false;
Josh Gaoa408fd22016-08-04 14:53:17 -0700487 RecordFilesTransferred(1);
Josh Gao2db75ce2016-02-19 15:55:55 -0800488 return true;
489 } else {
490 Error("failed to copy '%s' to '%s': received premature success", from, to);
491 return true;
492 }
Elliott Hughesdde00be2015-09-27 12:55:37 -0700493 }
494 if (msg.status.id != ID_FAIL) {
Elliott Hughesf5cdc1d2015-10-27 16:03:15 -0700495 Error("failed to copy '%s' to '%s': unknown reason %d", from, to, msg.status.id);
Elliott Hughesdde00be2015-09-27 12:55:37 -0700496 return false;
497 }
Elliott Hughese4ed32f2015-10-23 21:06:11 -0700498 return ReportCopyFailure(from, to, msg);
499 }
500
501 bool ReportCopyFailure(const char* from, const char* to, const syncmsg& msg) {
Elliott Hughes6c73bfc2015-10-27 13:40:35 -0700502 std::vector<char> buf(msg.status.msglen + 1);
503 if (!ReadFdExactly(fd, &buf[0], msg.status.msglen)) {
Elliott Hughesf5cdc1d2015-10-27 16:03:15 -0700504 Error("failed to copy '%s' to '%s'; failed to read reason (!): %s",
505 from, to, strerror(errno));
Elliott Hughesdde00be2015-09-27 12:55:37 -0700506 return false;
507 }
Elliott Hughes6c73bfc2015-10-27 13:40:35 -0700508 buf[msg.status.msglen] = 0;
Josh Gaoa2cf3752016-12-05 17:11:34 -0800509 Error("failed to copy '%s' to '%s': remote %s", from, to, &buf[0]);
Elliott Hughesdde00be2015-09-27 12:55:37 -0700510 return false;
511 }
512
Elliott Hughesf5cdc1d2015-10-27 16:03:15 -0700513
Josh Gaocbf485f2015-11-02 17:15:57 -0800514 void Printf(const char* fmt, ...) __attribute__((__format__(ADB_FORMAT_ARCHETYPE, 2, 3))) {
515 std::string s;
Elliott Hughes7b43fa52015-12-17 17:05:29 -0800516
Josh Gaocbf485f2015-11-02 17:15:57 -0800517 va_list ap;
518 va_start(ap, fmt);
519 android::base::StringAppendV(&s, fmt, ap);
520 va_end(ap);
521
Elliott Hughes7b43fa52015-12-17 17:05:29 -0800522 line_printer_.Print(s, LinePrinter::INFO);
Josh Gaocbf485f2015-11-02 17:15:57 -0800523 }
524
Josh Gaof35fede2016-08-04 11:43:00 -0700525 void Println(const char* fmt, ...) __attribute__((__format__(ADB_FORMAT_ARCHETYPE, 2, 3))) {
526 std::string s;
527
528 va_list ap;
529 va_start(ap, fmt);
530 android::base::StringAppendV(&s, fmt, ap);
531 va_end(ap);
532
533 line_printer_.Print(s, LinePrinter::INFO);
534 line_printer_.KeepInfoLine();
535 }
536
Elliott Hughesf5cdc1d2015-10-27 16:03:15 -0700537 void Error(const char* fmt, ...) __attribute__((__format__(ADB_FORMAT_ARCHETYPE, 2, 3))) {
538 std::string s = "adb: error: ";
539
540 va_list ap;
541 va_start(ap, fmt);
542 android::base::StringAppendV(&s, fmt, ap);
543 va_end(ap);
544
Elliott Hughes78a37a52015-12-08 16:01:15 -0800545 line_printer_.Print(s, LinePrinter::ERROR);
Elliott Hughesf5cdc1d2015-10-27 16:03:15 -0700546 }
547
Josh Gaoa544cc62015-11-07 17:18:44 -0800548 void Warning(const char* fmt, ...) __attribute__((__format__(ADB_FORMAT_ARCHETYPE, 2, 3))) {
549 std::string s = "adb: warning: ";
550
551 va_list ap;
552 va_start(ap, fmt);
553 android::base::StringAppendV(&s, fmt, ap);
554 va_end(ap);
555
Elliott Hughes78a37a52015-12-08 16:01:15 -0800556 line_printer_.Print(s, LinePrinter::WARNING);
Josh Gaoa544cc62015-11-07 17:18:44 -0800557 }
558
Elliott Hughes7b43fa52015-12-17 17:05:29 -0800559 void ComputeExpectedTotalBytes(const std::vector<copyinfo>& file_list) {
Josh Gaoa408fd22016-08-04 14:53:17 -0700560 current_ledger_.bytes_expected = 0;
Elliott Hughes7b43fa52015-12-17 17:05:29 -0800561 for (const copyinfo& ci : file_list) {
562 // Unfortunately, this doesn't work for symbolic links, because we'll copy the
563 // target of the link rather than just creating a link. (But ci.size is the link size.)
Josh Gaoa408fd22016-08-04 14:53:17 -0700564 if (!ci.skip) current_ledger_.bytes_expected += ci.size;
Elliott Hughes7b43fa52015-12-17 17:05:29 -0800565 }
Josh Gaoa408fd22016-08-04 14:53:17 -0700566 current_ledger_.expect_multiple_files = true;
Elliott Hughes7b43fa52015-12-17 17:05:29 -0800567 }
568
569 void SetExpectedTotalBytes(uint64_t expected_total_bytes) {
Josh Gaoa408fd22016-08-04 14:53:17 -0700570 current_ledger_.bytes_expected = expected_total_bytes;
571 current_ledger_.expect_multiple_files = false;
Elliott Hughes7b43fa52015-12-17 17:05:29 -0800572 }
573
Elliott Hughesb628cb12015-08-03 10:38:08 -0700574 // TODO: add a char[max] buffer here, to replace syncsendbuf...
575 int fd;
576 size_t max;
577
578 private:
Josh Gao2db75ce2016-02-19 15:55:55 -0800579 bool expect_done_;
Josh Gaoa2cf3752016-12-05 17:11:34 -0800580 bool have_stat_v2_;
Elliott Hughes7b43fa52015-12-17 17:05:29 -0800581
Josh Gaoa408fd22016-08-04 14:53:17 -0700582 TransferLedger global_ledger_;
583 TransferLedger current_ledger_;
Elliott Hughesf5cdc1d2015-10-27 16:03:15 -0700584 LinePrinter line_printer_;
Elliott Hughesb628cb12015-08-03 10:38:08 -0700585
Spencer Lowcc4a4b12015-10-14 17:32:44 -0700586 bool SendQuit() {
587 return SendRequest(ID_QUIT, ""); // TODO: add a SendResponse?
Elliott Hughesb628cb12015-08-03 10:38:08 -0700588 }
589
Elliott Hughes54e3efe2015-11-20 22:01:06 -0800590 bool WriteOrDie(const char* from, const char* to, const void* data, size_t data_length) {
591 if (!WriteFdExactly(fd, data, data_length)) {
592 if (errno == ECONNRESET) {
593 // Assume adbd told us why it was closing the connection, and
594 // try to read failure reason from adbd.
595 syncmsg msg;
596 if (!ReadFdExactly(fd, &msg.status, sizeof(msg.status))) {
597 Error("failed to copy '%s' to '%s': no response: %s", from, to, strerror(errno));
598 } else if (msg.status.id != ID_FAIL) {
599 Error("failed to copy '%s' to '%s': not ID_FAIL: %d", from, to, msg.status.id);
600 } else {
601 ReportCopyFailure(from, to, msg);
602 }
603 } else {
604 Error("%zu-byte write failed: %s", data_length, strerror(errno));
605 }
606 _exit(1);
607 }
608 return true;
609 }
Elliott Hughesb628cb12015-08-03 10:38:08 -0700610};
611
Josh Gao204d21e2015-11-02 16:45:47 -0800612typedef void (sync_ls_cb)(unsigned mode, unsigned size, unsigned time, const char* name);
Elliott Hughesb628cb12015-08-03 10:38:08 -0700613
Josh Gao204d21e2015-11-02 16:45:47 -0800614static bool sync_ls(SyncConnection& sc, const char* path,
Chih-Hung Hsieh90b40f62016-07-27 16:25:51 -0700615 const std::function<sync_ls_cb>& func) {
Elliott Hughesdde00be2015-09-27 12:55:37 -0700616 if (!sc.SendRequest(ID_LIST, path)) return false;
Elliott Hughesb628cb12015-08-03 10:38:08 -0700617
618 while (true) {
619 syncmsg msg;
Elliott Hughesdde00be2015-09-27 12:55:37 -0700620 if (!ReadFdExactly(sc.fd, &msg.dent, sizeof(msg.dent))) return false;
Elliott Hughesb628cb12015-08-03 10:38:08 -0700621
622 if (msg.dent.id == ID_DONE) return true;
623 if (msg.dent.id != ID_DENT) return false;
624
Elliott Hughes9e8e3552015-08-24 14:27:03 -0700625 size_t len = msg.dent.namelen;
Elliott Hughesb628cb12015-08-03 10:38:08 -0700626 if (len > 256) return false; // TODO: resize buffer? continue?
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800627
Elliott Hughesd189cfb2015-07-30 17:42:01 -0700628 char buf[257];
Elliott Hughesdde00be2015-09-27 12:55:37 -0700629 if (!ReadFdExactly(sc.fd, buf, len)) return false;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800630 buf[len] = 0;
631
Josh Gao204d21e2015-11-02 16:45:47 -0800632 func(msg.dent.mode, msg.dent.size, msg.dent.time, buf);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800633 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800634}
635
Josh Gaoa2cf3752016-12-05 17:11:34 -0800636static bool sync_stat(SyncConnection& sc, const char* path, struct stat* st) {
637 return sc.SendStat(path) && sc.FinishStat(st);
638}
639
640static bool sync_lstat(SyncConnection& sc, const char* path, struct stat* st) {
641 return sc.SendLstat(path) && sc.FinishStat(st);
642}
643
644static bool sync_stat_fallback(SyncConnection& sc, const char* path, struct stat* st) {
645 if (sync_stat(sc, path, st)) {
646 return true;
647 }
648
649 if (errno != ENOTSUP) {
Elliott Hughesb628cb12015-08-03 10:38:08 -0700650 return false;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800651 }
652
Josh Gaoa2cf3752016-12-05 17:11:34 -0800653 // Try to emulate the parts we can when talking to older adbds.
654 bool lstat_result = sync_lstat(sc, path, st);
655 if (!lstat_result) {
656 return false;
657 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800658
Josh Gaoa2cf3752016-12-05 17:11:34 -0800659 if (S_ISLNK(st->st_mode)) {
660 // If the target is a symlink, figure out whether it's a file or a directory.
661 // Also, zero out the st_size field, since no one actually cares what the path length is.
662 st->st_size = 0;
663 std::string dir_path = path;
664 dir_path.push_back('/');
665 struct stat tmp_st;
666
667 st->st_mode &= ~S_IFMT;
668 if (sync_lstat(sc, dir_path.c_str(), &tmp_st)) {
669 st->st_mode |= S_IFDIR;
670 } else {
671 st->st_mode |= S_IFREG;
672 }
673 }
Elliott Hughesb628cb12015-08-03 10:38:08 -0700674 return true;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800675}
676
Elliott Hughesdde00be2015-09-27 12:55:37 -0700677static bool sync_send(SyncConnection& sc, const char* lpath, const char* rpath,
Elliott Hughesf5cdc1d2015-10-27 16:03:15 -0700678 unsigned mtime, mode_t mode)
Elliott Hughesdde00be2015-09-27 12:55:37 -0700679{
680 std::string path_and_mode = android::base::StringPrintf("%s,%d", rpath, mode);
681
682 if (S_ISLNK(mode)) {
683#if !defined(_WIN32)
684 char buf[PATH_MAX];
685 ssize_t data_length = readlink(lpath, buf, PATH_MAX - 1);
686 if (data_length == -1) {
Elliott Hughesf5cdc1d2015-10-27 16:03:15 -0700687 sc.Error("readlink '%s' failed: %s", lpath, strerror(errno));
Elliott Hughesdde00be2015-09-27 12:55:37 -0700688 return false;
689 }
690 buf[data_length++] = '\0';
691
Elliott Hughes54e3efe2015-11-20 22:01:06 -0800692 if (!sc.SendSmallFile(path_and_mode.c_str(), lpath, rpath, mtime, buf, data_length)) {
693 return false;
694 }
Elliott Hughesdde00be2015-09-27 12:55:37 -0700695 return sc.CopyDone(lpath, rpath);
696#endif
697 }
698
Elliott Hughesdde00be2015-09-27 12:55:37 -0700699 struct stat st;
700 if (stat(lpath, &st) == -1) {
Elliott Hughesf5cdc1d2015-10-27 16:03:15 -0700701 sc.Error("failed to stat local file '%s': %s", lpath, strerror(errno));
Elliott Hughesdde00be2015-09-27 12:55:37 -0700702 return false;
703 }
704 if (st.st_size < SYNC_DATA_MAX) {
705 std::string data;
Josh Gao1deea102016-09-14 16:13:50 -0700706 if (!android::base::ReadFileToString(lpath, &data, true)) {
Elliott Hughesf5cdc1d2015-10-27 16:03:15 -0700707 sc.Error("failed to read all of '%s': %s", lpath, strerror(errno));
Elliott Hughesdde00be2015-09-27 12:55:37 -0700708 return false;
709 }
Elliott Hughes54e3efe2015-11-20 22:01:06 -0800710 if (!sc.SendSmallFile(path_and_mode.c_str(), lpath, rpath, mtime,
711 data.data(), data.size())) {
Elliott Hughesf5cdc1d2015-10-27 16:03:15 -0700712 return false;
713 }
Elliott Hughesdde00be2015-09-27 12:55:37 -0700714 } else {
Elliott Hughes7275d802015-11-20 17:35:17 -0800715 if (!sc.SendLargeFile(path_and_mode.c_str(), lpath, rpath, mtime)) {
Elliott Hughesf5cdc1d2015-10-27 16:03:15 -0700716 return false;
717 }
Elliott Hughesdde00be2015-09-27 12:55:37 -0700718 }
719 return sc.CopyDone(lpath, rpath);
720}
721
Felipe Lemeb6447032016-04-01 17:43:27 -0700722static bool sync_recv(SyncConnection& sc, const char* rpath, const char* lpath,
Josh Gao5c3e3fb2016-12-06 14:07:53 -0800723 const char* name, uint64_t expected_size) {
Elliott Hughesdde00be2015-09-27 12:55:37 -0700724 if (!sc.SendRequest(ID_RECV, rpath)) return false;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800725
Elliott Hughese4ed32f2015-10-23 21:06:11 -0700726 adb_unlink(lpath);
Elliott Hughese4ed32f2015-10-23 21:06:11 -0700727 int lfd = adb_creat(lpath, 0644);
728 if (lfd < 0) {
Elliott Hughesf5cdc1d2015-10-27 16:03:15 -0700729 sc.Error("cannot create '%s': %s", lpath, strerror(errno));
Elliott Hughese4ed32f2015-10-23 21:06:11 -0700730 return false;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800731 }
732
Elliott Hughesf5cdc1d2015-10-27 16:03:15 -0700733 uint64_t bytes_copied = 0;
Elliott Hughesb628cb12015-08-03 10:38:08 -0700734 while (true) {
Elliott Hughese4ed32f2015-10-23 21:06:11 -0700735 syncmsg msg;
Elliott Hughesdde00be2015-09-27 12:55:37 -0700736 if (!ReadFdExactly(sc.fd, &msg.data, sizeof(msg.data))) {
Spencer Lowc1a31332015-08-28 01:07:30 -0700737 adb_close(lfd);
Elliott Hughese4ed32f2015-10-23 21:06:11 -0700738 adb_unlink(lpath);
Elliott Hughesdde00be2015-09-27 12:55:37 -0700739 return false;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800740 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800741
Elliott Hughese4ed32f2015-10-23 21:06:11 -0700742 if (msg.data.id == ID_DONE) break;
743
744 if (msg.data.id != ID_DATA) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800745 adb_close(lfd);
Elliott Hughese4ed32f2015-10-23 21:06:11 -0700746 adb_unlink(lpath);
747 sc.ReportCopyFailure(rpath, lpath, msg);
Elliott Hughesdde00be2015-09-27 12:55:37 -0700748 return false;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800749 }
750
Elliott Hughese4ed32f2015-10-23 21:06:11 -0700751 if (msg.data.size > sc.max) {
Elliott Hughesf5cdc1d2015-10-27 16:03:15 -0700752 sc.Error("msg.data.size too large: %u (max %zu)", msg.data.size, sc.max);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800753 adb_close(lfd);
Elliott Hughese4ed32f2015-10-23 21:06:11 -0700754 adb_unlink(lpath);
Elliott Hughesdde00be2015-09-27 12:55:37 -0700755 return false;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800756 }
757
Elliott Hughese4ed32f2015-10-23 21:06:11 -0700758 char buffer[SYNC_DATA_MAX];
759 if (!ReadFdExactly(sc.fd, buffer, msg.data.size)) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800760 adb_close(lfd);
Elliott Hughese4ed32f2015-10-23 21:06:11 -0700761 adb_unlink(lpath);
Elliott Hughesdde00be2015-09-27 12:55:37 -0700762 return false;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800763 }
764
Elliott Hughese4ed32f2015-10-23 21:06:11 -0700765 if (!WriteFdExactly(lfd, buffer, msg.data.size)) {
Elliott Hughesf5cdc1d2015-10-27 16:03:15 -0700766 sc.Error("cannot write '%s': %s", lpath, strerror(errno));
Elliott Hughese4ed32f2015-10-23 21:06:11 -0700767 adb_close(lfd);
768 adb_unlink(lpath);
769 return false;
770 }
771
Elliott Hughesf5cdc1d2015-10-27 16:03:15 -0700772 bytes_copied += msg.data.size;
773
Josh Gaoa408fd22016-08-04 14:53:17 -0700774 sc.RecordBytesTransferred(msg.data.size);
Josh Gao5c3e3fb2016-12-06 14:07:53 -0800775 sc.ReportProgress(name != nullptr ? name : rpath, bytes_copied, expected_size);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800776 }
777
Josh Gaoa408fd22016-08-04 14:53:17 -0700778 sc.RecordFilesTransferred(1);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800779 adb_close(lfd);
Elliott Hughesdde00be2015-09-27 12:55:37 -0700780 return true;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800781}
782
Elliott Hughesb628cb12015-08-03 10:38:08 -0700783bool do_sync_ls(const char* path) {
784 SyncConnection sc;
785 if (!sc.IsValid()) return false;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800786
Josh Gao204d21e2015-11-02 16:45:47 -0800787 return sync_ls(sc, path, [](unsigned mode, unsigned size, unsigned time,
788 const char* name) {
789 printf("%08x %08x %08x %s\n", mode, size, time, name);
790 });
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800791}
792
Elliott Hughesb628cb12015-08-03 10:38:08 -0700793static bool IsDotOrDotDot(const char* name) {
794 return name[0] == '.' && (name[1] == '\0' || (name[1] == '.' && name[2] == '\0'));
795}
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800796
Elliott Hughes7b43fa52015-12-17 17:05:29 -0800797static bool local_build_list(SyncConnection& sc, std::vector<copyinfo>* file_list,
Josh Gao8efa6462015-11-03 15:26:38 -0800798 const std::string& lpath,
799 const std::string& rpath) {
Josh Gao204d21e2015-11-02 16:45:47 -0800800 std::vector<copyinfo> dirlist;
Josh Gaod20cf382015-11-03 15:23:03 -0800801 std::unique_ptr<DIR, int (*)(DIR*)> dir(opendir(lpath.c_str()), closedir);
Elliott Hughesb628cb12015-08-03 10:38:08 -0700802 if (!dir) {
Josh Gaod20cf382015-11-03 15:23:03 -0800803 sc.Error("cannot open '%s': %s", lpath.c_str(), strerror(errno));
Josh Gao8efa6462015-11-03 15:26:38 -0800804 return false;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800805 }
806
Josh Gaoa90563e2015-11-04 14:57:04 -0800807 bool empty_dir = true;
Elliott Hughesf5cdc1d2015-10-27 16:03:15 -0700808 dirent* de;
Elliott Hughesb628cb12015-08-03 10:38:08 -0700809 while ((de = readdir(dir.get()))) {
Josh Gaoa90563e2015-11-04 14:57:04 -0800810 if (IsDotOrDotDot(de->d_name)) {
811 continue;
812 }
Elliott Hughesb628cb12015-08-03 10:38:08 -0700813
Josh Gaoa90563e2015-11-04 14:57:04 -0800814 empty_dir = false;
Josh Gaod20cf382015-11-03 15:23:03 -0800815 std::string stat_path = lpath + de->d_name;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800816
Elliott Hughesb628cb12015-08-03 10:38:08 -0700817 struct stat st;
Josh Gaoa5cea712015-11-07 15:27:26 -0800818 if (lstat(stat_path.c_str(), &st) == -1) {
Josh Gaod20cf382015-11-03 15:23:03 -0800819 sc.Error("cannot lstat '%s': %s", stat_path.c_str(),
820 strerror(errno));
Josh Gaoa5cea712015-11-07 15:27:26 -0800821 continue;
822 }
823
Josh Gaod9a2fd62015-12-09 14:03:30 -0800824 copyinfo ci(lpath, rpath, de->d_name, st.st_mode);
Josh Gaoa5cea712015-11-07 15:27:26 -0800825 if (S_ISDIR(st.st_mode)) {
826 dirlist.push_back(ci);
827 } else {
Josh Gaod13def22016-03-02 16:11:13 -0800828 if (!should_push_file(st.st_mode)) {
829 sc.Warning("skipping special file '%s' (mode = 0o%o)", lpath.c_str(), st.st_mode);
Josh Gaob3cab932015-11-30 10:21:25 -0800830 ci.skip = true;
Josh Gaoa5cea712015-11-07 15:27:26 -0800831 }
Josh Gaod13def22016-03-02 16:11:13 -0800832 ci.time = st.st_mtime;
833 ci.size = st.st_size;
Elliott Hughes7b43fa52015-12-17 17:05:29 -0800834 file_list->push_back(ci);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800835 }
836 }
837
Elliott Hughesb628cb12015-08-03 10:38:08 -0700838 // Close this directory and recurse.
839 dir.reset();
Josh Gaoa90563e2015-11-04 14:57:04 -0800840
841 // Add the current directory to the list if it was empty, to ensure that
842 // it gets created.
843 if (empty_dir) {
844 // TODO(b/25566053): Make pushing empty directories work.
845 // TODO(b/25457350): We don't preserve permissions on directories.
Josh Gaoa544cc62015-11-07 17:18:44 -0800846 sc.Warning("skipping empty directory '%s'", lpath.c_str());
Colin Crosse6794072017-02-23 21:23:05 -0800847 copyinfo ci(android::base::Dirname(lpath), android::base::Dirname(rpath),
848 android::base::Basename(lpath), S_IFDIR);
Josh Gaoa90563e2015-11-04 14:57:04 -0800849 ci.skip = true;
Elliott Hughes7b43fa52015-12-17 17:05:29 -0800850 file_list->push_back(ci);
Josh Gaoa90563e2015-11-04 14:57:04 -0800851 return true;
852 }
853
Josh Gao204d21e2015-11-02 16:45:47 -0800854 for (const copyinfo& ci : dirlist) {
Elliott Hughes7b43fa52015-12-17 17:05:29 -0800855 local_build_list(sc, file_list, ci.lpath, ci.rpath);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800856 }
857
Josh Gao8efa6462015-11-03 15:26:38 -0800858 return true;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800859}
860
Josh Gaod20cf382015-11-03 15:23:03 -0800861static bool copy_local_dir_remote(SyncConnection& sc, std::string lpath,
862 std::string rpath, bool check_timestamps,
863 bool list_only) {
Josh Gaoa408fd22016-08-04 14:53:17 -0700864 sc.NewTransfer();
865
Josh Gaod20cf382015-11-03 15:23:03 -0800866 // Make sure that both directory paths end in a slash.
Josh Gaoa3b6a062015-11-09 11:12:14 -0800867 // Both paths are known to be nonempty, so we don't need to check.
Elliott Hughes7b43fa52015-12-17 17:05:29 -0800868 ensure_trailing_separators(lpath, rpath);
Josh Gaod20cf382015-11-03 15:23:03 -0800869
870 // Recursively build the list of files to copy.
Elliott Hughes7b43fa52015-12-17 17:05:29 -0800871 std::vector<copyinfo> file_list;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800872 int skipped = 0;
Elliott Hughes7b43fa52015-12-17 17:05:29 -0800873 if (!local_build_list(sc, &file_list, lpath, rpath)) {
Elliott Hughesb628cb12015-08-03 10:38:08 -0700874 return false;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800875 }
876
Elliott Hughesb628cb12015-08-03 10:38:08 -0700877 if (check_timestamps) {
Elliott Hughes7b43fa52015-12-17 17:05:29 -0800878 for (const copyinfo& ci : file_list) {
Josh Gaoa2cf3752016-12-05 17:11:34 -0800879 if (!sc.SendLstat(ci.rpath.c_str())) {
880 sc.Error("failed to send lstat");
Josh Gaod20cf382015-11-03 15:23:03 -0800881 return false;
882 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800883 }
Elliott Hughes7b43fa52015-12-17 17:05:29 -0800884 for (copyinfo& ci : file_list) {
Josh Gaoa2cf3752016-12-05 17:11:34 -0800885 struct stat st;
886 if (sc.FinishStat(&st)) {
887 if (st.st_size == static_cast<off_t>(ci.size)) {
888 // For links, we cannot update the atime/mtime.
889 if ((S_ISREG(ci.mode & st.st_mode) && st.st_mtime == ci.time) ||
890 (S_ISLNK(ci.mode & st.st_mode) && st.st_mtime >= ci.time)) {
891 ci.skip = true;
892 }
Elliott Hughesb628cb12015-08-03 10:38:08 -0700893 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800894 }
895 }
896 }
Josh Gao204d21e2015-11-02 16:45:47 -0800897
Elliott Hughes7b43fa52015-12-17 17:05:29 -0800898 sc.ComputeExpectedTotalBytes(file_list);
899
900 for (const copyinfo& ci : file_list) {
Josh Gaocb094c62015-11-03 14:44:04 -0800901 if (!ci.skip) {
Elliott Hughesf5cdc1d2015-10-27 16:03:15 -0700902 if (list_only) {
Josh Gao23891f12016-08-04 14:55:23 -0700903 sc.Println("would push: %s -> %s", ci.lpath.c_str(), ci.rpath.c_str());
Elliott Hughesf5cdc1d2015-10-27 16:03:15 -0700904 } else {
Elliott Hughes7b43fa52015-12-17 17:05:29 -0800905 if (!sync_send(sc, ci.lpath.c_str(), ci.rpath.c_str(), ci.time, ci.mode)) {
Josh Gaod20cf382015-11-03 15:23:03 -0800906 return false;
Elliott Hughesf5cdc1d2015-10-27 16:03:15 -0700907 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800908 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800909 } else {
910 skipped++;
911 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800912 }
913
Josh Gaoa408fd22016-08-04 14:53:17 -0700914 sc.RecordFilesSkipped(skipped);
915 sc.ReportTransferRate(lpath, TransferDirection::push);
Elliott Hughesb628cb12015-08-03 10:38:08 -0700916 return true;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800917}
918
Josh Gao5d093b22015-10-30 16:57:19 -0700919bool do_sync_push(const std::vector<const char*>& srcs, const char* dst) {
Elliott Hughesb628cb12015-08-03 10:38:08 -0700920 SyncConnection sc;
921 if (!sc.IsValid()) return false;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800922
Josh Gao5d093b22015-10-30 16:57:19 -0700923 bool success = true;
Josh Gaoa2cf3752016-12-05 17:11:34 -0800924 bool dst_exists;
925 bool dst_isdir;
926
927 struct stat st;
928 if (sync_stat_fallback(sc, dst, &st)) {
929 dst_exists = true;
930 dst_isdir = S_ISDIR(st.st_mode);
931 } else {
932 if (errno == ENOENT || errno == ENOPROTOOPT) {
933 dst_exists = false;
934 dst_isdir = false;
935 } else {
936 sc.Error("stat failed when trying to push to %s: %s", dst, strerror(errno));
937 return false;
938 }
939 }
Josh Gao5d093b22015-10-30 16:57:19 -0700940
Josh Gao8acf06c2015-11-07 15:38:19 -0800941 if (!dst_isdir) {
Josh Gao5d093b22015-10-30 16:57:19 -0700942 if (srcs.size() > 1) {
943 sc.Error("target '%s' is not a directory", dst);
944 return false;
945 } else {
946 size_t dst_len = strlen(dst);
Josh Gaoa5cea712015-11-07 15:27:26 -0800947
948 // A path that ends with a slash doesn't have to be a directory if
949 // it doesn't exist yet.
950 if (dst[dst_len - 1] == '/' && dst_exists) {
Josh Gao5d093b22015-10-30 16:57:19 -0700951 sc.Error("failed to access '%s': Not a directory", dst);
952 return false;
953 }
954 }
Elliott Hughesb628cb12015-08-03 10:38:08 -0700955 }
Josh Gao5d093b22015-10-30 16:57:19 -0700956
957 for (const char* src_path : srcs) {
958 const char* dst_path = dst;
959 struct stat st;
Josh Gaoa5cea712015-11-07 15:27:26 -0800960 if (stat(src_path, &st) == -1) {
Josh Gao5d093b22015-10-30 16:57:19 -0700961 sc.Error("cannot stat '%s': %s", src_path, strerror(errno));
962 success = false;
963 continue;
964 }
965
966 if (S_ISDIR(st.st_mode)) {
Josh Gao8acf06c2015-11-07 15:38:19 -0800967 std::string dst_dir = dst;
968
969 // If the destination path existed originally, the source directory
970 // should be copied as a child of the destination.
971 if (dst_exists) {
972 if (!dst_isdir) {
973 sc.Error("target '%s' is not a directory", dst);
974 return false;
975 }
976 // dst is a POSIX path, so we don't want to use the sysdeps
977 // helpers here.
978 if (dst_dir.back() != '/') {
979 dst_dir.push_back('/');
980 }
Colin Crosse6794072017-02-23 21:23:05 -0800981 dst_dir.append(android::base::Basename(src_path));
Josh Gao8acf06c2015-11-07 15:38:19 -0800982 }
983
Josh Gaoa2cf3752016-12-05 17:11:34 -0800984 success &= copy_local_dir_remote(sc, src_path, dst_dir.c_str(), false, false);
Josh Gao5d093b22015-10-30 16:57:19 -0700985 continue;
Josh Gaod13def22016-03-02 16:11:13 -0800986 } else if (!should_push_file(st.st_mode)) {
987 sc.Warning("skipping special file '%s' (mode = 0o%o)", src_path, st.st_mode);
988 continue;
Josh Gao5d093b22015-10-30 16:57:19 -0700989 }
990
991 std::string path_holder;
Josh Gao8acf06c2015-11-07 15:38:19 -0800992 if (dst_isdir) {
Josh Gao5d093b22015-10-30 16:57:19 -0700993 // If we're copying a local file to a remote directory,
994 // we really want to copy to remote_dir + "/" + local_filename.
Josh Gao2e4ed0a2016-02-03 14:55:24 -0800995 path_holder = dst_path;
996 if (path_holder.back() != '/') {
997 path_holder.push_back('/');
998 }
Colin Crosse6794072017-02-23 21:23:05 -0800999 path_holder += android::base::Basename(src_path);
Josh Gao5d093b22015-10-30 16:57:19 -07001000 dst_path = path_holder.c_str();
1001 }
Josh Gaoa408fd22016-08-04 14:53:17 -07001002
1003 sc.NewTransfer();
Elliott Hughes7b43fa52015-12-17 17:05:29 -08001004 sc.SetExpectedTotalBytes(st.st_size);
Josh Gao5d093b22015-10-30 16:57:19 -07001005 success &= sync_send(sc, src_path, dst_path, st.st_mtime, st.st_mode);
Josh Gaoa408fd22016-08-04 14:53:17 -07001006 sc.ReportTransferRate(src_path, TransferDirection::push);
Josh Gao5d093b22015-10-30 16:57:19 -07001007 }
1008
Josh Gaoa408fd22016-08-04 14:53:17 -07001009 sc.ReportOverallTransferRate(TransferDirection::push);
Josh Gao5d093b22015-10-30 16:57:19 -07001010 return success;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -08001011}
1012
Josh Gao5c0d55a2016-02-25 17:12:45 -08001013static bool remote_build_list(SyncConnection& sc, std::vector<copyinfo>* file_list,
1014 const std::string& rpath, const std::string& lpath) {
Josh Gao204d21e2015-11-02 16:45:47 -08001015 std::vector<copyinfo> dirlist;
Josh Gaod9a2fd62015-12-09 14:03:30 -08001016 std::vector<copyinfo> linklist;
Josh Gao5c0d55a2016-02-25 17:12:45 -08001017
1018 // Add an entry for the current directory to ensure it gets created before pulling its contents.
Colin Crosse6794072017-02-23 21:23:05 -08001019 copyinfo ci(android::base::Dirname(lpath), android::base::Dirname(rpath),
1020 android::base::Basename(lpath), S_IFDIR);
Josh Gao5c0d55a2016-02-25 17:12:45 -08001021 file_list->push_back(ci);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -08001022
Elliott Hughesb628cb12015-08-03 10:38:08 -07001023 // Put the files/dirs in rpath on the lists.
Josh Gaob3cab932015-11-30 10:21:25 -08001024 auto callback = [&](unsigned mode, unsigned size, unsigned time, const char* name) {
Josh Gaoa90563e2015-11-04 14:57:04 -08001025 if (IsDotOrDotDot(name)) {
1026 return;
1027 }
Josh Gao204d21e2015-11-02 16:45:47 -08001028
Josh Gaod9a2fd62015-12-09 14:03:30 -08001029 copyinfo ci(lpath, rpath, name, mode);
Josh Gaoa90563e2015-11-04 14:57:04 -08001030 if (S_ISDIR(mode)) {
1031 dirlist.push_back(ci);
Josh Gaod9a2fd62015-12-09 14:03:30 -08001032 } else if (S_ISLNK(mode)) {
1033 linklist.push_back(ci);
Josh Gao204d21e2015-11-02 16:45:47 -08001034 } else {
Josh Gaod13def22016-03-02 16:11:13 -08001035 if (!should_pull_file(ci.mode)) {
1036 sc.Warning("skipping special file '%s' (mode = 0o%o)", ci.rpath.c_str(), ci.mode);
1037 ci.skip = true;
1038 }
Josh Gaod9a2fd62015-12-09 14:03:30 -08001039 ci.time = time;
1040 ci.size = size;
Elliott Hughes7b43fa52015-12-17 17:05:29 -08001041 file_list->push_back(ci);
Josh Gao204d21e2015-11-02 16:45:47 -08001042 }
1043 };
1044
Josh Gaod20cf382015-11-03 15:23:03 -08001045 if (!sync_ls(sc, rpath.c_str(), callback)) {
Elliott Hughesb628cb12015-08-03 10:38:08 -07001046 return false;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -08001047 }
1048
Josh Gaod9a2fd62015-12-09 14:03:30 -08001049 // Check each symlink we found to see whether it's a file or directory.
1050 for (copyinfo& link_ci : linklist) {
Josh Gaoa2cf3752016-12-05 17:11:34 -08001051 struct stat st;
1052 if (!sync_stat_fallback(sc, link_ci.rpath.c_str(), &st)) {
1053 sc.Warning("stat failed for path %s: %s", link_ci.rpath.c_str(), strerror(errno));
1054 continue;
1055 }
1056
1057 if (S_ISDIR(st.st_mode)) {
Josh Gaod9a2fd62015-12-09 14:03:30 -08001058 dirlist.emplace_back(std::move(link_ci));
1059 } else {
Elliott Hughes7b43fa52015-12-17 17:05:29 -08001060 file_list->emplace_back(std::move(link_ci));
Josh Gaod9a2fd62015-12-09 14:03:30 -08001061 }
1062 }
1063
Elliott Hughesb628cb12015-08-03 10:38:08 -07001064 // Recurse into each directory we found.
Josh Gao204d21e2015-11-02 16:45:47 -08001065 while (!dirlist.empty()) {
1066 copyinfo current = dirlist.back();
1067 dirlist.pop_back();
Elliott Hughes7b43fa52015-12-17 17:05:29 -08001068 if (!remote_build_list(sc, file_list, current.rpath, current.lpath)) {
Elliott Hughesb628cb12015-08-03 10:38:08 -07001069 return false;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -08001070 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -08001071 }
1072
Elliott Hughesb628cb12015-08-03 10:38:08 -07001073 return true;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -08001074}
1075
Josh Gaoa3b6a062015-11-09 11:12:14 -08001076static int set_time_and_mode(const std::string& lpath, time_t time,
1077 unsigned int mode) {
Greg Hackmann8b689142014-05-06 08:48:18 -07001078 struct utimbuf times = { time, time };
Josh Gaoa3b6a062015-11-09 11:12:14 -08001079 int r1 = utime(lpath.c_str(), &times);
Lajos Molnar4e23e3c2013-04-19 12:41:09 -07001080
1081 /* use umask for permissions */
Josh Gao204d21e2015-11-02 16:45:47 -08001082 mode_t mask = umask(0000);
Lajos Molnar4e23e3c2013-04-19 12:41:09 -07001083 umask(mask);
Josh Gaoa3b6a062015-11-09 11:12:14 -08001084 int r2 = chmod(lpath.c_str(), mode & ~mask);
Lajos Molnar4e23e3c2013-04-19 12:41:09 -07001085
Spencer Low28bc2cb2015-11-07 18:51:54 -08001086 return r1 ? r1 : r2;
Lajos Molnar4e23e3c2013-04-19 12:41:09 -07001087}
1088
Josh Gaod20cf382015-11-03 15:23:03 -08001089static bool copy_remote_dir_local(SyncConnection& sc, std::string rpath,
1090 std::string lpath, bool copy_attrs) {
Josh Gaoa408fd22016-08-04 14:53:17 -07001091 sc.NewTransfer();
1092
Elliott Hughesb628cb12015-08-03 10:38:08 -07001093 // Make sure that both directory paths end in a slash.
Josh Gaoa5cea712015-11-07 15:27:26 -08001094 // Both paths are known to be nonempty, so we don't need to check.
Elliott Hughes7b43fa52015-12-17 17:05:29 -08001095 ensure_trailing_separators(lpath, rpath);
Riley Andrewsc736a942014-12-12 13:12:36 -08001096
Elliott Hughesb628cb12015-08-03 10:38:08 -07001097 // Recursively build the list of files to copy.
Elliott Hughes7b43fa52015-12-17 17:05:29 -08001098 sc.Printf("pull: building file list...");
1099 std::vector<copyinfo> file_list;
1100 if (!remote_build_list(sc, &file_list, rpath.c_str(), lpath.c_str())) {
Josh Gao204d21e2015-11-02 16:45:47 -08001101 return false;
1102 }
Elliott Hughesb628cb12015-08-03 10:38:08 -07001103
Elliott Hughes7b43fa52015-12-17 17:05:29 -08001104 sc.ComputeExpectedTotalBytes(file_list);
1105
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -08001106 int skipped = 0;
Elliott Hughes7b43fa52015-12-17 17:05:29 -08001107 for (const copyinfo &ci : file_list) {
Josh Gaocb094c62015-11-03 14:44:04 -08001108 if (!ci.skip) {
Josh Gaoa90563e2015-11-04 14:57:04 -08001109 if (S_ISDIR(ci.mode)) {
1110 // Entry is for an empty directory, create it and continue.
1111 // TODO(b/25457350): We don't preserve permissions on directories.
Josh Gaoa3b6a062015-11-09 11:12:14 -08001112 if (!mkdirs(ci.lpath)) {
Josh Gaoa90563e2015-11-04 14:57:04 -08001113 sc.Error("failed to create directory '%s': %s",
Josh Gaoa3b6a062015-11-09 11:12:14 -08001114 ci.lpath.c_str(), strerror(errno));
Josh Gaoa90563e2015-11-04 14:57:04 -08001115 return false;
1116 }
Josh Gaoa90563e2015-11-04 14:57:04 -08001117 continue;
1118 }
1119
Josh Gao5c3e3fb2016-12-06 14:07:53 -08001120 if (!sync_recv(sc, ci.rpath.c_str(), ci.lpath.c_str(), nullptr, ci.size)) {
Elliott Hughesb628cb12015-08-03 10:38:08 -07001121 return false;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -08001122 }
Lajos Molnar4e23e3c2013-04-19 12:41:09 -07001123
Josh Gaoa3b6a062015-11-09 11:12:14 -08001124 if (copy_attrs && set_time_and_mode(ci.lpath, ci.time, ci.mode)) {
Elliott Hughesb628cb12015-08-03 10:38:08 -07001125 return false;
Lajos Molnar4e23e3c2013-04-19 12:41:09 -07001126 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -08001127 } else {
1128 skipped++;
1129 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -08001130 }
1131
Josh Gaoa408fd22016-08-04 14:53:17 -07001132 sc.RecordFilesSkipped(skipped);
1133 sc.ReportTransferRate(rpath, TransferDirection::pull);
Elliott Hughesb628cb12015-08-03 10:38:08 -07001134 return true;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -08001135}
1136
Josh Gao5d093b22015-10-30 16:57:19 -07001137bool do_sync_pull(const std::vector<const char*>& srcs, const char* dst,
Felipe Lemeb6447032016-04-01 17:43:27 -07001138 bool copy_attrs, const char* name) {
Elliott Hughesb628cb12015-08-03 10:38:08 -07001139 SyncConnection sc;
1140 if (!sc.IsValid()) return false;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -08001141
Josh Gao5d093b22015-10-30 16:57:19 -07001142 bool success = true;
Josh Gao5d093b22015-10-30 16:57:19 -07001143 struct stat st;
Josh Gaoa5cea712015-11-07 15:27:26 -08001144 bool dst_exists = true;
1145
1146 if (stat(dst, &st) == -1) {
1147 dst_exists = false;
1148
1149 // If we're only pulling one path, the destination path might point to
Josh Gao5d093b22015-10-30 16:57:19 -07001150 // a path that doesn't exist yet.
Josh Gaoa5cea712015-11-07 15:27:26 -08001151 if (srcs.size() == 1 && errno == ENOENT) {
1152 // However, its parent must exist.
1153 struct stat parent_st;
Colin Crosse6794072017-02-23 21:23:05 -08001154 if (stat(android::base::Dirname(dst).c_str(), &parent_st) == -1) {
Josh Gaoa5cea712015-11-07 15:27:26 -08001155 sc.Error("cannot create file/directory '%s': %s", dst, strerror(errno));
1156 return false;
1157 }
1158 } else {
1159 sc.Error("failed to access '%s': %s", dst, strerror(errno));
Josh Gao5d093b22015-10-30 16:57:19 -07001160 return false;
1161 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -08001162 }
1163
Josh Gao8acf06c2015-11-07 15:38:19 -08001164 bool dst_isdir = dst_exists && S_ISDIR(st.st_mode);
1165 if (!dst_isdir) {
Josh Gao5d093b22015-10-30 16:57:19 -07001166 if (srcs.size() > 1) {
1167 sc.Error("target '%s' is not a directory", dst);
Elliott Hughesb628cb12015-08-03 10:38:08 -07001168 return false;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -08001169 } else {
Josh Gao5d093b22015-10-30 16:57:19 -07001170 size_t dst_len = strlen(dst);
Josh Gaoa5cea712015-11-07 15:27:26 -08001171
1172 // A path that ends with a slash doesn't have to be a directory if
1173 // it doesn't exist yet.
Josh Gaoa3b6a062015-11-09 11:12:14 -08001174 if (adb_is_separator(dst[dst_len - 1]) && dst_exists) {
Josh Gao5d093b22015-10-30 16:57:19 -07001175 sc.Error("failed to access '%s': Not a directory", dst);
Elliott Hughesb628cb12015-08-03 10:38:08 -07001176 return false;
Elliott Hughesd189cfb2015-07-30 17:42:01 -07001177 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -08001178 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -08001179 }
Elliott Hughesb628cb12015-08-03 10:38:08 -07001180
Josh Gao5d093b22015-10-30 16:57:19 -07001181 for (const char* src_path : srcs) {
1182 const char* dst_path = dst;
Josh Gaoa2cf3752016-12-05 17:11:34 -08001183 struct stat src_st;
1184 if (!sync_stat_fallback(sc, src_path, &src_st)) {
1185 if (errno == ENOPROTOOPT) {
1186 sc.Error("remote object '%s' does not exist", src_path);
1187 } else {
1188 sc.Error("failed to stat remote object '%s': %s", src_path, strerror(errno));
1189 }
1190
Josh Gao5d093b22015-10-30 16:57:19 -07001191 success = false;
1192 continue;
1193 }
1194
Josh Gaoa2cf3752016-12-05 17:11:34 -08001195 bool src_isdir = S_ISDIR(src_st.st_mode);
Josh Gaod9a2fd62015-12-09 14:03:30 -08001196 if (src_isdir) {
Josh Gao8acf06c2015-11-07 15:38:19 -08001197 std::string dst_dir = dst;
1198
1199 // If the destination path existed originally, the source directory
1200 // should be copied as a child of the destination.
1201 if (dst_exists) {
1202 if (!dst_isdir) {
1203 sc.Error("target '%s' is not a directory", dst);
1204 return false;
1205 }
1206 if (!adb_is_separator(dst_dir.back())) {
1207 dst_dir.push_back(OS_PATH_SEPARATOR);
1208 }
Colin Crosse6794072017-02-23 21:23:05 -08001209 dst_dir.append(android::base::Basename(src_path));
Josh Gao8acf06c2015-11-07 15:38:19 -08001210 }
1211
Josh Gaod9a2fd62015-12-09 14:03:30 -08001212 success &= copy_remote_dir_local(sc, src_path, dst_dir.c_str(), copy_attrs);
Josh Gao5d093b22015-10-30 16:57:19 -07001213 continue;
Josh Gaoa2cf3752016-12-05 17:11:34 -08001214 } else if (!should_pull_file(src_st.st_mode)) {
1215 sc.Warning("skipping special file '%s' (mode = 0o%o)", src_path, src_st.st_mode);
Josh Gaod13def22016-03-02 16:11:13 -08001216 continue;
1217 }
Josh Gaod9a2fd62015-12-09 14:03:30 -08001218
Josh Gaod13def22016-03-02 16:11:13 -08001219 std::string path_holder;
1220 if (dst_isdir) {
1221 // If we're copying a remote file to a local directory, we
1222 // really want to copy to local_dir + OS_PATH_SEPARATOR +
1223 // basename(remote).
1224 path_holder = android::base::StringPrintf("%s%c%s", dst_path, OS_PATH_SEPARATOR,
Colin Crosse6794072017-02-23 21:23:05 -08001225 android::base::Basename(src_path).c_str());
Josh Gaod13def22016-03-02 16:11:13 -08001226 dst_path = path_holder.c_str();
1227 }
Josh Gaod9a2fd62015-12-09 14:03:30 -08001228
Josh Gaoa408fd22016-08-04 14:53:17 -07001229 sc.NewTransfer();
Josh Gaoa2cf3752016-12-05 17:11:34 -08001230 sc.SetExpectedTotalBytes(src_st.st_size);
Josh Gao5c3e3fb2016-12-06 14:07:53 -08001231 if (!sync_recv(sc, src_path, dst_path, name, src_st.st_size)) {
Josh Gaod13def22016-03-02 16:11:13 -08001232 success = false;
1233 continue;
1234 }
1235
Josh Gaoa2cf3752016-12-05 17:11:34 -08001236 if (copy_attrs && set_time_and_mode(dst_path, src_st.st_mtime, src_st.st_mode) != 0) {
Josh Gaod13def22016-03-02 16:11:13 -08001237 success = false;
1238 continue;
Josh Gao5d093b22015-10-30 16:57:19 -07001239 }
Josh Gaoa408fd22016-08-04 14:53:17 -07001240 sc.ReportTransferRate(src_path, TransferDirection::pull);
Josh Gao5d093b22015-10-30 16:57:19 -07001241 }
1242
Josh Gaoa408fd22016-08-04 14:53:17 -07001243 sc.ReportOverallTransferRate(TransferDirection::pull);
Josh Gao5d093b22015-10-30 16:57:19 -07001244 return success;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -08001245}
1246
Elliott Hughesb628cb12015-08-03 10:38:08 -07001247bool do_sync_sync(const std::string& lpath, const std::string& rpath, bool list_only) {
Elliott Hughesb628cb12015-08-03 10:38:08 -07001248 SyncConnection sc;
1249 if (!sc.IsValid()) return false;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -08001250
Josh Gaoa408fd22016-08-04 14:53:17 -07001251 bool success = copy_local_dir_remote(sc, lpath, rpath, true, list_only);
1252 if (!list_only) {
1253 sc.ReportOverallTransferRate(TransferDirection::push);
1254 }
1255 return success;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -08001256}