blob: 8c9f7a6862e263be5cc4805d7d4683b216bbb616 [file] [log] [blame]
The Android Open Source Projectdd7bc332009-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 Albert76649012015-02-24 15:51:19 -080017#include <dirent.h>
18#include <errno.h>
Spencer Low6001c872015-05-13 00:02:55 -070019#include <inttypes.h>
Dan Albert76649012015-02-24 15:51:19 -080020#include <limits.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080021#include <stdio.h>
22#include <stdlib.h>
23#include <string.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080024#include <sys/stat.h>
25#include <sys/time.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080026#include <sys/types.h>
Dan Albert76649012015-02-24 15:51:19 -080027#include <time.h>
Elliott Hughesae5a6c02015-09-27 12:55:37 -070028#include <unistd.h>
Greg Hackmann7a5e2bd2014-05-06 08:48:18 -070029#include <utime.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080030
Elliott Hughesa925dba2015-08-24 14:49:43 -070031#include <memory>
Elliott Hughes6d929972015-10-27 13:40:35 -070032#include <vector>
Elliott Hughesa925dba2015-08-24 14:49:43 -070033
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080034#include "sysdeps.h"
Dan Albert76649012015-02-24 15:51:19 -080035
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080036#include "adb.h"
37#include "adb_client.h"
Dan Albertcc731cc2015-02-24 21:26:58 -080038#include "adb_io.h"
Alex Vallée14216142015-05-06 17:22:25 -040039#include "adb_utils.h"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080040#include "file_sync_service.h"
Elliott Hughesb708d162015-10-27 16:03:15 -070041#include "line_printer.h"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080042
Elliott Hughesae5a6c02015-09-27 12:55:37 -070043#include <base/file.h>
Elliott Hughesaa245492015-08-03 10:38:08 -070044#include <base/strings.h>
Elliott Hughes5c742702015-07-30 17:42:01 -070045#include <base/stringprintf.h>
46
Elliott Hughesaa245492015-08-03 10:38:08 -070047struct syncsendbuf {
48 unsigned id;
49 unsigned size;
50 char data[SYNC_DATA_MAX];
51};
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080052
Elliott Hughesaa245492015-08-03 10:38:08 -070053class SyncConnection {
54 public:
Elliott Hughesb708d162015-10-27 16:03:15 -070055 SyncConnection() : total_bytes(0), start_time_ms_(CurrentTimeMs()) {
Elliott Hughesaa245492015-08-03 10:38:08 -070056 max = SYNC_DATA_MAX; // TODO: decide at runtime.
57
58 std::string error;
59 fd = adb_connect("sync:", &error);
60 if (fd < 0) {
Elliott Hughesb708d162015-10-27 16:03:15 -070061 Error("connect failed: %s", error.c_str());
Elliott Hughesaa245492015-08-03 10:38:08 -070062 }
63 }
64
65 ~SyncConnection() {
66 if (!IsValid()) return;
67
Spencer Low351ecd12015-10-14 17:32:44 -070068 if (SendQuit()) {
69 // We sent a quit command, so the server should be doing orderly
70 // shutdown soon. But if we encountered an error while we were using
71 // the connection, the server might still be sending data (before
72 // doing orderly shutdown), in which case we won't wait for all of
73 // the data nor the coming orderly shutdown. In the common success
74 // case, this will wait for the server to do orderly shutdown.
75 ReadOrderlyShutdown(fd);
76 }
Elliott Hughesaa245492015-08-03 10:38:08 -070077 adb_close(fd);
78 }
79
80 bool IsValid() { return fd >= 0; }
81
Elliott Hughesae5a6c02015-09-27 12:55:37 -070082 bool SendRequest(int id, const char* path_and_mode) {
83 size_t path_length = strlen(path_and_mode);
84 if (path_length > 1024) {
Elliott Hughesb708d162015-10-27 16:03:15 -070085 Error("SendRequest failed: path too long: %zu", path_length);
Elliott Hughesae5a6c02015-09-27 12:55:37 -070086 errno = ENAMETOOLONG;
87 return false;
88 }
89
90 // Sending header and payload in a single write makes a noticeable
91 // difference to "adb sync" performance.
Elliott Hughes6d929972015-10-27 13:40:35 -070092 std::vector<char> buf(sizeof(SyncRequest) + path_length);
93 SyncRequest* req = reinterpret_cast<SyncRequest*>(&buf[0]);
Elliott Hughesae5a6c02015-09-27 12:55:37 -070094 req->id = id;
95 req->path_length = path_length;
96 char* data = reinterpret_cast<char*>(req + 1);
97 memcpy(data, path_and_mode, path_length);
98
Elliott Hughes6d929972015-10-27 13:40:35 -070099 return WriteFdExactly(fd, &buf[0], buf.size());
Elliott Hughesae5a6c02015-09-27 12:55:37 -0700100 }
101
102 // Sending header, payload, and footer in a single write makes a huge
103 // difference to "adb sync" performance.
104 bool SendSmallFile(const char* path_and_mode,
Elliott Hughesb708d162015-10-27 16:03:15 -0700105 const char* rpath,
Elliott Hughesae5a6c02015-09-27 12:55:37 -0700106 const char* data, size_t data_length,
107 unsigned mtime) {
Elliott Hughesb708d162015-10-27 16:03:15 -0700108 Print(rpath);
109
Elliott Hughesae5a6c02015-09-27 12:55:37 -0700110 size_t path_length = strlen(path_and_mode);
111 if (path_length > 1024) {
Elliott Hughesb708d162015-10-27 16:03:15 -0700112 Error("SendSmallFile failed: path too long: %zu", path_length);
Elliott Hughesae5a6c02015-09-27 12:55:37 -0700113 errno = ENAMETOOLONG;
114 return false;
115 }
116
Elliott Hughes6d929972015-10-27 13:40:35 -0700117 std::vector<char> buf(sizeof(SyncRequest) + path_length +
Elliott Hughesae5a6c02015-09-27 12:55:37 -0700118 sizeof(SyncRequest) + data_length +
Elliott Hughes6d929972015-10-27 13:40:35 -0700119 sizeof(SyncRequest));
120 char* p = &buf[0];
Elliott Hughesae5a6c02015-09-27 12:55:37 -0700121
122 SyncRequest* req_send = reinterpret_cast<SyncRequest*>(p);
123 req_send->id = ID_SEND;
124 req_send->path_length = path_length;
125 p += sizeof(SyncRequest);
126 memcpy(p, path_and_mode, path_length);
127 p += path_length;
128
129 SyncRequest* req_data = reinterpret_cast<SyncRequest*>(p);
130 req_data->id = ID_DATA;
131 req_data->path_length = data_length;
132 p += sizeof(SyncRequest);
133 memcpy(p, data, data_length);
134 p += data_length;
135
136 SyncRequest* req_done = reinterpret_cast<SyncRequest*>(p);
137 req_done->id = ID_DONE;
138 req_done->path_length = mtime;
139 p += sizeof(SyncRequest);
140
Elliott Hughes6d929972015-10-27 13:40:35 -0700141 if (!WriteFdExactly(fd, &buf[0], (p - &buf[0]))) return false;
Elliott Hughesae5a6c02015-09-27 12:55:37 -0700142
143 total_bytes += data_length;
144 return true;
145 }
146
147 bool CopyDone(const char* from, const char* to) {
148 syncmsg msg;
149 if (!ReadFdExactly(fd, &msg.status, sizeof(msg.status))) {
Elliott Hughesb708d162015-10-27 16:03:15 -0700150 Error("failed to copy '%s' to '%s': no ID_DONE: %s", from, to, strerror(errno));
Elliott Hughesae5a6c02015-09-27 12:55:37 -0700151 return false;
152 }
153 if (msg.status.id == ID_OKAY) {
154 return true;
155 }
156 if (msg.status.id != ID_FAIL) {
Elliott Hughesb708d162015-10-27 16:03:15 -0700157 Error("failed to copy '%s' to '%s': unknown reason %d", from, to, msg.status.id);
Elliott Hughesae5a6c02015-09-27 12:55:37 -0700158 return false;
159 }
Elliott Hughes8b43c3e2015-10-23 21:06:11 -0700160 return ReportCopyFailure(from, to, msg);
161 }
162
163 bool ReportCopyFailure(const char* from, const char* to, const syncmsg& msg) {
Elliott Hughes6d929972015-10-27 13:40:35 -0700164 std::vector<char> buf(msg.status.msglen + 1);
165 if (!ReadFdExactly(fd, &buf[0], msg.status.msglen)) {
Elliott Hughesb708d162015-10-27 16:03:15 -0700166 Error("failed to copy '%s' to '%s'; failed to read reason (!): %s",
167 from, to, strerror(errno));
Elliott Hughesae5a6c02015-09-27 12:55:37 -0700168 return false;
169 }
Elliott Hughes6d929972015-10-27 13:40:35 -0700170 buf[msg.status.msglen] = 0;
Elliott Hughesb708d162015-10-27 16:03:15 -0700171 Error("failed to copy '%s' to '%s': %s", from, to, &buf[0]);
Elliott Hughesae5a6c02015-09-27 12:55:37 -0700172 return false;
173 }
174
Elliott Hughesb708d162015-10-27 16:03:15 -0700175 std::string TransferRate() {
176 uint64_t ms = CurrentTimeMs() - start_time_ms_;
177 if (total_bytes == 0 || ms == 0) return "";
178
179 double s = static_cast<double>(ms) / 1000LL;
180 double rate = (static_cast<double>(total_bytes) / s) / (1024*1024);
181 return android::base::StringPrintf(" %.1f MB/s (%" PRId64 " bytes in %.3fs)",
182 rate, total_bytes, s);
183 }
184
185 void Print(const std::string& s) {
186 // TODO: we actually don't want ELIDE; we want "ELIDE if smart, FULL if dumb".
187 line_printer_.Print(s, LinePrinter::ELIDE);
188 }
189
190 void Error(const char* fmt, ...) __attribute__((__format__(ADB_FORMAT_ARCHETYPE, 2, 3))) {
191 std::string s = "adb: error: ";
192
193 va_list ap;
194 va_start(ap, fmt);
195 android::base::StringAppendV(&s, fmt, ap);
196 va_end(ap);
197
198 line_printer_.Print(s, LinePrinter::FULL);
199 }
200
Elliott Hughesaa245492015-08-03 10:38:08 -0700201 uint64_t total_bytes;
202
203 // TODO: add a char[max] buffer here, to replace syncsendbuf...
204 int fd;
205 size_t max;
206
207 private:
Elliott Hughesb708d162015-10-27 16:03:15 -0700208 uint64_t start_time_ms_;
209
210 LinePrinter line_printer_;
Elliott Hughesaa245492015-08-03 10:38:08 -0700211
Spencer Low351ecd12015-10-14 17:32:44 -0700212 bool SendQuit() {
213 return SendRequest(ID_QUIT, ""); // TODO: add a SendResponse?
Elliott Hughesaa245492015-08-03 10:38:08 -0700214 }
215
Elliott Hughesb708d162015-10-27 16:03:15 -0700216 static uint64_t CurrentTimeMs() {
217 struct timeval tv;
218 gettimeofday(&tv, 0); // (Not clock_gettime because of Mac/Windows.)
219 return static_cast<uint64_t>(tv.tv_sec) * 1000 + tv.tv_usec / 1000;
Elliott Hughesaa245492015-08-03 10:38:08 -0700220 }
221};
222
223typedef void (*sync_ls_cb)(unsigned mode, unsigned size, unsigned time, const char* name, void* cookie);
224
Elliott Hughesae5a6c02015-09-27 12:55:37 -0700225static bool sync_ls(SyncConnection& sc, const char* path, sync_ls_cb func, void* cookie) {
226 if (!sc.SendRequest(ID_LIST, path)) return false;
Elliott Hughesaa245492015-08-03 10:38:08 -0700227
228 while (true) {
229 syncmsg msg;
Elliott Hughesae5a6c02015-09-27 12:55:37 -0700230 if (!ReadFdExactly(sc.fd, &msg.dent, sizeof(msg.dent))) return false;
Elliott Hughesaa245492015-08-03 10:38:08 -0700231
232 if (msg.dent.id == ID_DONE) return true;
233 if (msg.dent.id != ID_DENT) return false;
234
Elliott Hughesf4465202015-08-24 14:27:03 -0700235 size_t len = msg.dent.namelen;
Elliott Hughesaa245492015-08-03 10:38:08 -0700236 if (len > 256) return false; // TODO: resize buffer? continue?
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800237
Elliott Hughes5c742702015-07-30 17:42:01 -0700238 char buf[257];
Elliott Hughesae5a6c02015-09-27 12:55:37 -0700239 if (!ReadFdExactly(sc.fd, buf, len)) return false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800240 buf[len] = 0;
241
Elliott Hughesf4465202015-08-24 14:27:03 -0700242 func(msg.dent.mode, msg.dent.size, msg.dent.time, buf, cookie);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800243 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800244}
245
Elliott Hughesaa245492015-08-03 10:38:08 -0700246static bool sync_finish_stat(SyncConnection& sc, unsigned int* timestamp,
247 unsigned int* mode, unsigned int* size) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800248 syncmsg msg;
Elliott Hughesaa245492015-08-03 10:38:08 -0700249 if (!ReadFdExactly(sc.fd, &msg.stat, sizeof(msg.stat)) || msg.stat.id != ID_STAT) {
250 return false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800251 }
252
Elliott Hughesf4465202015-08-24 14:27:03 -0700253 if (timestamp) *timestamp = msg.stat.time;
254 if (mode) *mode = msg.stat.mode;
255 if (size) *size = msg.stat.size;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800256
Elliott Hughesaa245492015-08-03 10:38:08 -0700257 return true;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800258}
259
Elliott Hughesaa245492015-08-03 10:38:08 -0700260static bool sync_stat(SyncConnection& sc, const char* path,
261 unsigned int* timestamp, unsigned int* mode, unsigned int* size) {
Elliott Hughesae5a6c02015-09-27 12:55:37 -0700262 return sc.SendRequest(ID_STAT, path) && sync_finish_stat(sc, timestamp, mode, size);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800263}
264
Elliott Hughesb708d162015-10-27 16:03:15 -0700265static bool SendLargeFile(SyncConnection& sc, const char* path_and_mode,
266 const char* lpath, const char* rpath,
267 unsigned mtime) {
Elliott Hughesae5a6c02015-09-27 12:55:37 -0700268 if (!sc.SendRequest(ID_SEND, path_and_mode)) {
Elliott Hughesb708d162015-10-27 16:03:15 -0700269 sc.Error("failed to send ID_SEND message '%s': %s", path_and_mode, strerror(errno));
Elliott Hughesae5a6c02015-09-27 12:55:37 -0700270 return false;
271 }
272
Elliott Hughesb708d162015-10-27 16:03:15 -0700273 struct stat st;
274 if (stat(lpath, &st) == -1) {
275 sc.Error("cannot stat '%s': %s", lpath, strerror(errno));
276 return false;
Spencer Lowd8cce182015-08-28 01:07:30 -0700277 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800278
Elliott Hughesb708d162015-10-27 16:03:15 -0700279 uint64_t total_size = st.st_size;
280 uint64_t bytes_copied = 0;
281
282 int lfd = adb_open(lpath, O_RDONLY);
Elliott Hughesaa245492015-08-03 10:38:08 -0700283 if (lfd < 0) {
Elliott Hughesb708d162015-10-27 16:03:15 -0700284 sc.Error("cannot open '%s': %s", lpath, strerror(errno));
Spencer Lowd8cce182015-08-28 01:07:30 -0700285 return false;
Mark Lindner76f2a932014-03-11 17:55:59 -0700286 }
287
Elliott Hughesae5a6c02015-09-27 12:55:37 -0700288 syncsendbuf sbuf;
289 sbuf.id = ID_DATA;
Elliott Hughesaa245492015-08-03 10:38:08 -0700290 while (true) {
Elliott Hughesae5a6c02015-09-27 12:55:37 -0700291 int ret = adb_read(lfd, sbuf.data, sc.max);
Elliott Hughes8fcd8bc2015-08-25 10:59:45 -0700292 if (ret <= 0) {
Spencer Lowd8cce182015-08-28 01:07:30 -0700293 if (ret < 0) {
Elliott Hughesb708d162015-10-27 16:03:15 -0700294 sc.Error("cannot read '%s': %s", lpath, strerror(errno));
Spencer Lowd8cce182015-08-28 01:07:30 -0700295 adb_close(lfd);
296 return false;
297 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800298 break;
299 }
300
Elliott Hughesae5a6c02015-09-27 12:55:37 -0700301 sbuf.size = ret;
302 if (!WriteFdExactly(sc.fd, &sbuf, sizeof(unsigned) * 2 + ret)) {
Spencer Lowd8cce182015-08-28 01:07:30 -0700303 adb_close(lfd);
304 return false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800305 }
Elliott Hughesaa245492015-08-03 10:38:08 -0700306 sc.total_bytes += ret;
Mark Lindner76f2a932014-03-11 17:55:59 -0700307
Elliott Hughesb708d162015-10-27 16:03:15 -0700308 bytes_copied += ret;
309
310 int percentage = static_cast<int>(bytes_copied * 100 / total_size);
311 sc.Print(android::base::StringPrintf("%s: %d%%", rpath, percentage));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800312 }
313
314 adb_close(lfd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800315
Elliott Hughesaa245492015-08-03 10:38:08 -0700316 syncmsg msg;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800317 msg.data.id = ID_DONE;
Elliott Hughesf4465202015-08-24 14:27:03 -0700318 msg.data.size = mtime;
Elliott Hughes081696d2015-09-03 11:06:00 -0700319 if (!WriteFdExactly(sc.fd, &msg.data, sizeof(msg.data))) {
Elliott Hughesb708d162015-10-27 16:03:15 -0700320 sc.Error("failed to send ID_DONE message for '%s': %s", rpath, strerror(errno));
Elliott Hughesaa245492015-08-03 10:38:08 -0700321 return false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800322 }
323
Elliott Hughesaa245492015-08-03 10:38:08 -0700324 return true;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800325}
326
Elliott Hughesae5a6c02015-09-27 12:55:37 -0700327static bool sync_send(SyncConnection& sc, const char* lpath, const char* rpath,
Elliott Hughesb708d162015-10-27 16:03:15 -0700328 unsigned mtime, mode_t mode)
Elliott Hughesae5a6c02015-09-27 12:55:37 -0700329{
330 std::string path_and_mode = android::base::StringPrintf("%s,%d", rpath, mode);
331
332 if (S_ISLNK(mode)) {
333#if !defined(_WIN32)
334 char buf[PATH_MAX];
335 ssize_t data_length = readlink(lpath, buf, PATH_MAX - 1);
336 if (data_length == -1) {
Elliott Hughesb708d162015-10-27 16:03:15 -0700337 sc.Error("readlink '%s' failed: %s", lpath, strerror(errno));
Elliott Hughesae5a6c02015-09-27 12:55:37 -0700338 return false;
339 }
340 buf[data_length++] = '\0';
341
Elliott Hughesb708d162015-10-27 16:03:15 -0700342 if (!sc.SendSmallFile(path_and_mode.c_str(), rpath, buf, data_length, mtime)) return false;
Elliott Hughesae5a6c02015-09-27 12:55:37 -0700343 return sc.CopyDone(lpath, rpath);
344#endif
345 }
346
347 if (!S_ISREG(mode)) {
Elliott Hughesb708d162015-10-27 16:03:15 -0700348 sc.Error("local file '%s' has unsupported mode: 0o%o", lpath, mode);
Elliott Hughesae5a6c02015-09-27 12:55:37 -0700349 return false;
350 }
351
352 struct stat st;
353 if (stat(lpath, &st) == -1) {
Elliott Hughesb708d162015-10-27 16:03:15 -0700354 sc.Error("failed to stat local file '%s': %s", lpath, strerror(errno));
Elliott Hughesae5a6c02015-09-27 12:55:37 -0700355 return false;
356 }
357 if (st.st_size < SYNC_DATA_MAX) {
358 std::string data;
359 if (!android::base::ReadFileToString(lpath, &data)) {
Elliott Hughesb708d162015-10-27 16:03:15 -0700360 sc.Error("failed to read all of '%s': %s", lpath, strerror(errno));
Elliott Hughesae5a6c02015-09-27 12:55:37 -0700361 return false;
362 }
Elliott Hughesb708d162015-10-27 16:03:15 -0700363 if (!sc.SendSmallFile(path_and_mode.c_str(), rpath, data.data(), data.size(), mtime)) {
364 return false;
365 }
Elliott Hughesae5a6c02015-09-27 12:55:37 -0700366 } else {
Elliott Hughesb708d162015-10-27 16:03:15 -0700367 if (!SendLargeFile(sc, path_and_mode.c_str(), lpath, rpath, mtime)) {
368 return false;
369 }
Elliott Hughesae5a6c02015-09-27 12:55:37 -0700370 }
371 return sc.CopyDone(lpath, rpath);
372}
373
Elliott Hughesb708d162015-10-27 16:03:15 -0700374static bool sync_recv(SyncConnection& sc, const char* rpath, const char* lpath) {
375 sc.Print(rpath);
376
Elliott Hughesaa245492015-08-03 10:38:08 -0700377 unsigned size = 0;
Elliott Hughesb708d162015-10-27 16:03:15 -0700378 if (!sync_stat(sc, rpath, nullptr, nullptr, &size)) return false;
Mark Lindner76f2a932014-03-11 17:55:59 -0700379
Elliott Hughesae5a6c02015-09-27 12:55:37 -0700380 if (!sc.SendRequest(ID_RECV, rpath)) return false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800381
Elliott Hughes8b43c3e2015-10-23 21:06:11 -0700382 adb_unlink(lpath);
383 mkdirs(lpath);
384 int lfd = adb_creat(lpath, 0644);
385 if (lfd < 0) {
Elliott Hughesb708d162015-10-27 16:03:15 -0700386 sc.Error("cannot create '%s': %s", lpath, strerror(errno));
Elliott Hughes8b43c3e2015-10-23 21:06:11 -0700387 return false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800388 }
389
Elliott Hughesb708d162015-10-27 16:03:15 -0700390 uint64_t bytes_copied = 0;
Elliott Hughesaa245492015-08-03 10:38:08 -0700391 while (true) {
Elliott Hughes8b43c3e2015-10-23 21:06:11 -0700392 syncmsg msg;
Elliott Hughesae5a6c02015-09-27 12:55:37 -0700393 if (!ReadFdExactly(sc.fd, &msg.data, sizeof(msg.data))) {
Spencer Lowd8cce182015-08-28 01:07:30 -0700394 adb_close(lfd);
Elliott Hughes8b43c3e2015-10-23 21:06:11 -0700395 adb_unlink(lpath);
Elliott Hughesae5a6c02015-09-27 12:55:37 -0700396 return false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800397 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800398
Elliott Hughes8b43c3e2015-10-23 21:06:11 -0700399 if (msg.data.id == ID_DONE) break;
400
401 if (msg.data.id != ID_DATA) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800402 adb_close(lfd);
Elliott Hughes8b43c3e2015-10-23 21:06:11 -0700403 adb_unlink(lpath);
404 sc.ReportCopyFailure(rpath, lpath, msg);
Elliott Hughesae5a6c02015-09-27 12:55:37 -0700405 return false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800406 }
407
Elliott Hughes8b43c3e2015-10-23 21:06:11 -0700408 if (msg.data.size > sc.max) {
Elliott Hughesb708d162015-10-27 16:03:15 -0700409 sc.Error("msg.data.size too large: %u (max %zu)", msg.data.size, sc.max);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800410 adb_close(lfd);
Elliott Hughes8b43c3e2015-10-23 21:06:11 -0700411 adb_unlink(lpath);
Elliott Hughesae5a6c02015-09-27 12:55:37 -0700412 return false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800413 }
414
Elliott Hughes8b43c3e2015-10-23 21:06:11 -0700415 char buffer[SYNC_DATA_MAX];
416 if (!ReadFdExactly(sc.fd, buffer, msg.data.size)) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800417 adb_close(lfd);
Elliott Hughes8b43c3e2015-10-23 21:06:11 -0700418 adb_unlink(lpath);
Elliott Hughesae5a6c02015-09-27 12:55:37 -0700419 return false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800420 }
421
Elliott Hughes8b43c3e2015-10-23 21:06:11 -0700422 if (!WriteFdExactly(lfd, buffer, msg.data.size)) {
Elliott Hughesb708d162015-10-27 16:03:15 -0700423 sc.Error("cannot write '%s': %s", lpath, strerror(errno));
Elliott Hughes8b43c3e2015-10-23 21:06:11 -0700424 adb_close(lfd);
425 adb_unlink(lpath);
426 return false;
427 }
428
429 sc.total_bytes += msg.data.size;
Mark Lindner76f2a932014-03-11 17:55:59 -0700430
Elliott Hughesb708d162015-10-27 16:03:15 -0700431 bytes_copied += msg.data.size;
432
433 int percentage = static_cast<int>(bytes_copied * 100 / size);
434 sc.Print(android::base::StringPrintf("%s: %d%%", rpath, percentage));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800435 }
436
437 adb_close(lfd);
Elliott Hughesae5a6c02015-09-27 12:55:37 -0700438 return true;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800439}
440
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800441static void do_sync_ls_cb(unsigned mode, unsigned size, unsigned time,
Elliott Hughesaa245492015-08-03 10:38:08 -0700442 const char* name, void* /*cookie*/) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800443 printf("%08x %08x %08x %s\n", mode, size, time, name);
444}
445
Elliott Hughesaa245492015-08-03 10:38:08 -0700446bool do_sync_ls(const char* path) {
447 SyncConnection sc;
448 if (!sc.IsValid()) return false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800449
Elliott Hughesae5a6c02015-09-27 12:55:37 -0700450 return sync_ls(sc, path, do_sync_ls_cb, 0);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800451}
452
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800453struct copyinfo
454{
455 copyinfo *next;
456 const char *src;
457 const char *dst;
458 unsigned int time;
459 unsigned int mode;
Elliott Hughesae5a6c02015-09-27 12:55:37 -0700460 uint64_t size;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800461 int flag;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800462};
463
Elliott Hughes3edd54b2015-05-05 18:26:10 -0700464static copyinfo* mkcopyinfo(const char* spath, const char* dpath, const char* name, int isdir) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800465 int slen = strlen(spath);
466 int dlen = strlen(dpath);
467 int nlen = strlen(name);
468 int ssize = slen + nlen + 2;
469 int dsize = dlen + nlen + 2;
470
Elliott Hughes3edd54b2015-05-05 18:26:10 -0700471 copyinfo *ci = reinterpret_cast<copyinfo*>(malloc(sizeof(copyinfo) + ssize + dsize));
Elliott Hughesb708d162015-10-27 16:03:15 -0700472 if (ci == 0) {
Elliott Hughesaa245492015-08-03 10:38:08 -0700473 fprintf(stderr, "out of memory\n");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800474 abort();
475 }
476
477 ci->next = 0;
478 ci->time = 0;
479 ci->mode = 0;
480 ci->size = 0;
481 ci->flag = 0;
482 ci->src = (const char*)(ci + 1);
483 ci->dst = ci->src + ssize;
484 snprintf((char*) ci->src, ssize, isdir ? "%s%s/" : "%s%s", spath, name);
485 snprintf((char*) ci->dst, dsize, isdir ? "%s%s/" : "%s%s", dpath, name);
486
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800487 return ci;
488}
489
Elliott Hughesaa245492015-08-03 10:38:08 -0700490static bool IsDotOrDotDot(const char* name) {
491 return name[0] == '.' && (name[1] == '\0' || (name[1] == '.' && name[2] == '\0'));
492}
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800493
Elliott Hughesb708d162015-10-27 16:03:15 -0700494static int local_build_list(SyncConnection& sc,
495 copyinfo** filelist, const char* lpath, const char* rpath) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800496 copyinfo *dirlist = 0;
497 copyinfo *ci, *next;
498
Elliott Hughesaa245492015-08-03 10:38:08 -0700499 std::unique_ptr<DIR, int(*)(DIR*)> dir(opendir(lpath), closedir);
500 if (!dir) {
Elliott Hughesb708d162015-10-27 16:03:15 -0700501 sc.Error("cannot open '%s': %s", lpath, strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800502 return -1;
503 }
504
Elliott Hughesb708d162015-10-27 16:03:15 -0700505 dirent* de;
Elliott Hughesaa245492015-08-03 10:38:08 -0700506 while ((de = readdir(dir.get()))) {
507 if (IsDotOrDotDot(de->d_name)) continue;
508
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800509 char stat_path[PATH_MAX];
Elliott Hughesaa245492015-08-03 10:38:08 -0700510 if (strlen(lpath) + strlen(de->d_name) + 1 > sizeof(stat_path)) {
Elliott Hughesb708d162015-10-27 16:03:15 -0700511 sc.Error("skipping long path '%s%s'", lpath, de->d_name);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800512 continue;
Elliott Hughesaa245492015-08-03 10:38:08 -0700513 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800514 strcpy(stat_path, lpath);
515 strcat(stat_path, de->d_name);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800516
Elliott Hughesaa245492015-08-03 10:38:08 -0700517 struct stat st;
518 if (!lstat(stat_path, &st)) {
Daniel Rosenberg686bce62014-06-30 20:29:40 -0700519 if (S_ISDIR(st.st_mode)) {
Elliott Hughesaa245492015-08-03 10:38:08 -0700520 ci = mkcopyinfo(lpath, rpath, de->d_name, 1);
Daniel Rosenberg686bce62014-06-30 20:29:40 -0700521 ci->next = dirlist;
522 dirlist = ci;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800523 } else {
Elliott Hughesaa245492015-08-03 10:38:08 -0700524 ci = mkcopyinfo(lpath, rpath, de->d_name, 0);
525 if (!S_ISREG(st.st_mode) && !S_ISLNK(st.st_mode)) {
Elliott Hughesb708d162015-10-27 16:03:15 -0700526 sc.Error("skipping special file '%s'", ci->src);
Daniel Rosenberg686bce62014-06-30 20:29:40 -0700527 free(ci);
528 } else {
529 ci->time = st.st_mtime;
530 ci->mode = st.st_mode;
531 ci->size = st.st_size;
532 ci->next = *filelist;
533 *filelist = ci;
534 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800535 }
Daniel Rosenberg686bce62014-06-30 20:29:40 -0700536 } else {
Elliott Hughesb708d162015-10-27 16:03:15 -0700537 sc.Error("cannot lstat '%s': %s",stat_path , strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800538 }
539 }
540
Elliott Hughesaa245492015-08-03 10:38:08 -0700541 // Close this directory and recurse.
542 dir.reset();
543 for (ci = dirlist; ci != 0; ci = next) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800544 next = ci->next;
Elliott Hughesb708d162015-10-27 16:03:15 -0700545 local_build_list(sc, filelist, ci->src, ci->dst);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800546 free(ci);
547 }
548
549 return 0;
550}
551
Elliott Hughesaa245492015-08-03 10:38:08 -0700552static bool copy_local_dir_remote(SyncConnection& sc, const char* lpath, const char* rpath,
553 bool check_timestamps, bool list_only) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800554 copyinfo *filelist = 0;
555 copyinfo *ci, *next;
556 int pushed = 0;
557 int skipped = 0;
558
Elliott Hughesaa245492015-08-03 10:38:08 -0700559 if ((lpath[0] == 0) || (rpath[0] == 0)) return false;
560 if (lpath[strlen(lpath) - 1] != '/') {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800561 int tmplen = strlen(lpath)+2;
Dan Albertbac34742015-02-25 17:51:28 -0800562 char *tmp = reinterpret_cast<char*>(malloc(tmplen));
Elliott Hughesaa245492015-08-03 10:38:08 -0700563 if(tmp == 0) return false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800564 snprintf(tmp, tmplen, "%s/",lpath);
565 lpath = tmp;
566 }
Elliott Hughesaa245492015-08-03 10:38:08 -0700567 if (rpath[strlen(rpath) - 1] != '/') {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800568 int tmplen = strlen(rpath)+2;
Dan Albertbac34742015-02-25 17:51:28 -0800569 char *tmp = reinterpret_cast<char*>(malloc(tmplen));
Elliott Hughesaa245492015-08-03 10:38:08 -0700570 if(tmp == 0) return false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800571 snprintf(tmp, tmplen, "%s/",rpath);
572 rpath = tmp;
573 }
574
Elliott Hughesb708d162015-10-27 16:03:15 -0700575 if (local_build_list(sc, &filelist, lpath, rpath)) {
Elliott Hughesaa245492015-08-03 10:38:08 -0700576 return false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800577 }
578
Elliott Hughesaa245492015-08-03 10:38:08 -0700579 if (check_timestamps) {
580 for (ci = filelist; ci != 0; ci = ci->next) {
Elliott Hughesae5a6c02015-09-27 12:55:37 -0700581 if (!sc.SendRequest(ID_STAT, ci->dst)) return false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800582 }
583 for(ci = filelist; ci != 0; ci = ci->next) {
584 unsigned int timestamp, mode, size;
Elliott Hughesaa245492015-08-03 10:38:08 -0700585 if (!sync_finish_stat(sc, &timestamp, &mode, &size)) return false;
586 if (size == ci->size) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800587 /* for links, we cannot update the atime/mtime */
Elliott Hughesaa245492015-08-03 10:38:08 -0700588 if ((S_ISREG(ci->mode & mode) && timestamp == ci->time) ||
589 (S_ISLNK(ci->mode & mode) && timestamp >= ci->time)) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800590 ci->flag = 1;
Elliott Hughesaa245492015-08-03 10:38:08 -0700591 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800592 }
593 }
594 }
Elliott Hughesaa245492015-08-03 10:38:08 -0700595 for (ci = filelist; ci != 0; ci = next) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800596 next = ci->next;
Elliott Hughesaa245492015-08-03 10:38:08 -0700597 if (ci->flag == 0) {
Elliott Hughesb708d162015-10-27 16:03:15 -0700598 if (list_only) {
599 fprintf(stderr, "would push: %s -> %s\n", ci->src, ci->dst);
600 } else {
601 if (!sync_send(sc, ci->src, ci->dst, ci->time, ci->mode)) {
602 return false;
603 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800604 }
605 pushed++;
606 } else {
607 skipped++;
608 }
609 free(ci);
610 }
611
Elliott Hughesb708d162015-10-27 16:03:15 -0700612 sc.Print(android::base::StringPrintf("%s: %d file%s pushed. %d file%s skipped.%s\n",
613 rpath,
614 pushed, (pushed == 1) ? "" : "s",
615 skipped, (skipped == 1) ? "" : "s",
616 sc.TransferRate().c_str()));
Elliott Hughesaa245492015-08-03 10:38:08 -0700617 return true;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800618}
619
Elliott Hughesb708d162015-10-27 16:03:15 -0700620bool do_sync_push(const char* lpath, const char* rpath) {
Elliott Hughesaa245492015-08-03 10:38:08 -0700621 SyncConnection sc;
622 if (!sc.IsValid()) return false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800623
Elliott Hughes5c742702015-07-30 17:42:01 -0700624 struct stat st;
625 if (stat(lpath, &st)) {
Elliott Hughesb708d162015-10-27 16:03:15 -0700626 sc.Error("cannot stat '%s': %s", lpath, strerror(errno));
Elliott Hughesaa245492015-08-03 10:38:08 -0700627 return false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800628 }
629
Elliott Hughes5c742702015-07-30 17:42:01 -0700630 if (S_ISDIR(st.st_mode)) {
Elliott Hughesaa245492015-08-03 10:38:08 -0700631 return copy_local_dir_remote(sc, lpath, rpath, false, false);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800632 }
633
Elliott Hughesaa245492015-08-03 10:38:08 -0700634 unsigned mode;
635 if (!sync_stat(sc, rpath, nullptr, &mode, nullptr)) return false;
636 std::string path_holder;
637 if (mode != 0 && S_ISDIR(mode)) {
638 // If we're copying a local file to a remote directory,
639 // we really want to copy to remote_dir + "/" + local_filename.
640 path_holder = android::base::StringPrintf("%s/%s", rpath, adb_basename(lpath).c_str());
641 rpath = path_holder.c_str();
642 }
Elliott Hughesb708d162015-10-27 16:03:15 -0700643 bool result = sync_send(sc, lpath, rpath, st.st_mtime, st.st_mode);
644 sc.Print("\n");
645 return result;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800646}
647
Elliott Hughes2d4121c2015-04-17 09:47:42 -0700648struct sync_ls_build_list_cb_args {
Elliott Hughesb708d162015-10-27 16:03:15 -0700649 SyncConnection* sc;
650 copyinfo** filelist;
651 copyinfo** dirlist;
652 const char* rpath;
653 const char* lpath;
Elliott Hughes2d4121c2015-04-17 09:47:42 -0700654};
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800655
Elliott Hughes3edd54b2015-05-05 18:26:10 -0700656static void sync_ls_build_list_cb(unsigned mode, unsigned size, unsigned time,
657 const char* name, void* cookie)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800658{
Elliott Hughesb708d162015-10-27 16:03:15 -0700659 sync_ls_build_list_cb_args* args = static_cast<sync_ls_build_list_cb_args*>(cookie);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800660 copyinfo *ci;
661
662 if (S_ISDIR(mode)) {
663 copyinfo **dirlist = args->dirlist;
664
Elliott Hughesaa245492015-08-03 10:38:08 -0700665 // Don't try recursing down "." or "..".
666 if (IsDotOrDotDot(name)) return;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800667
668 ci = mkcopyinfo(args->rpath, args->lpath, name, 1);
669 ci->next = *dirlist;
670 *dirlist = ci;
671 } else if (S_ISREG(mode) || S_ISLNK(mode)) {
672 copyinfo **filelist = args->filelist;
673
674 ci = mkcopyinfo(args->rpath, args->lpath, name, 0);
675 ci->time = time;
676 ci->mode = mode;
677 ci->size = size;
678 ci->next = *filelist;
679 *filelist = ci;
680 } else {
Elliott Hughesb708d162015-10-27 16:03:15 -0700681 args->sc->Print(android::base::StringPrintf("skipping special file '%s'\n", name));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800682 }
683}
684
Elliott Hughesae5a6c02015-09-27 12:55:37 -0700685static bool remote_build_list(SyncConnection& sc, copyinfo **filelist,
Elliott Hughesaa245492015-08-03 10:38:08 -0700686 const char *rpath, const char *lpath) {
Elliott Hughesb708d162015-10-27 16:03:15 -0700687 copyinfo* dirlist = nullptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800688
Elliott Hughesb708d162015-10-27 16:03:15 -0700689 sync_ls_build_list_cb_args args;
690 args.sc = &sc;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800691 args.filelist = filelist;
692 args.dirlist = &dirlist;
693 args.rpath = rpath;
694 args.lpath = lpath;
695
Elliott Hughesaa245492015-08-03 10:38:08 -0700696 // Put the files/dirs in rpath on the lists.
Elliott Hughesb708d162015-10-27 16:03:15 -0700697 if (!sync_ls(sc, rpath, sync_ls_build_list_cb, &args)) {
Elliott Hughesaa245492015-08-03 10:38:08 -0700698 return false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800699 }
700
Elliott Hughesaa245492015-08-03 10:38:08 -0700701 // Recurse into each directory we found.
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800702 while (dirlist != NULL) {
Elliott Hughesb708d162015-10-27 16:03:15 -0700703 copyinfo* next = dirlist->next;
Elliott Hughesae5a6c02015-09-27 12:55:37 -0700704 if (!remote_build_list(sc, filelist, dirlist->src, dirlist->dst)) {
Elliott Hughesaa245492015-08-03 10:38:08 -0700705 return false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800706 }
707 free(dirlist);
708 dirlist = next;
709 }
710
Elliott Hughesaa245492015-08-03 10:38:08 -0700711 return true;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800712}
713
Dan Albertbac34742015-02-25 17:51:28 -0800714static int set_time_and_mode(const char *lpath, time_t time, unsigned int mode)
Lajos Molnarde8ff4a2013-04-19 12:41:09 -0700715{
Greg Hackmann7a5e2bd2014-05-06 08:48:18 -0700716 struct utimbuf times = { time, time };
717 int r1 = utime(lpath, &times);
Lajos Molnarde8ff4a2013-04-19 12:41:09 -0700718
719 /* use umask for permissions */
720 mode_t mask=umask(0000);
721 umask(mask);
722 int r2 = chmod(lpath, mode & ~mask);
723
724 return r1 ? : r2;
725}
726
Elliott Hughesaa245492015-08-03 10:38:08 -0700727static bool copy_remote_dir_local(SyncConnection& sc, const char* rpath, const char* lpath,
728 int copy_attrs) {
729 // Make sure that both directory paths end in a slash.
730 std::string rpath_clean(rpath);
731 std::string lpath_clean(lpath);
732 if (rpath_clean.empty() || lpath_clean.empty()) return false;
733 if (rpath_clean.back() != '/') rpath_clean.push_back('/');
734 if (lpath_clean.back() != '/') lpath_clean.push_back('/');
Riley Andrews4d04d242014-12-12 13:12:36 -0800735
Elliott Hughesaa245492015-08-03 10:38:08 -0700736 // Recursively build the list of files to copy.
Elliott Hughesb708d162015-10-27 16:03:15 -0700737 sc.Print("pull: building file list...");
Elliott Hughesaa245492015-08-03 10:38:08 -0700738 copyinfo* filelist = nullptr;
Elliott Hughesae5a6c02015-09-27 12:55:37 -0700739 if (!remote_build_list(sc, &filelist, rpath_clean.c_str(), lpath_clean.c_str())) return false;
Elliott Hughesaa245492015-08-03 10:38:08 -0700740
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800741 int pulled = 0;
742 int skipped = 0;
Elliott Hughesaa245492015-08-03 10:38:08 -0700743 copyinfo* ci = filelist;
744 while (ci) {
745 copyinfo* next = ci->next;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800746 if (ci->flag == 0) {
Elliott Hughesb708d162015-10-27 16:03:15 -0700747 sc.Print(android::base::StringPrintf("pull: %s -> %s", ci->src, ci->dst));
748 if (!sync_recv(sc, ci->src, ci->dst)) {
Elliott Hughesaa245492015-08-03 10:38:08 -0700749 return false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800750 }
Lajos Molnarde8ff4a2013-04-19 12:41:09 -0700751
752 if (copy_attrs && set_time_and_mode(ci->dst, ci->time, ci->mode)) {
Elliott Hughesaa245492015-08-03 10:38:08 -0700753 return false;
Lajos Molnarde8ff4a2013-04-19 12:41:09 -0700754 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800755 pulled++;
756 } else {
757 skipped++;
758 }
759 free(ci);
Elliott Hughesaa245492015-08-03 10:38:08 -0700760 ci = next;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800761 }
762
Elliott Hughesb708d162015-10-27 16:03:15 -0700763 sc.Print(android::base::StringPrintf("%s: %d file%s pulled. %d file%s skipped.%s\n",
764 rpath,
765 pulled, (pulled == 1) ? "" : "s",
766 skipped, (skipped == 1) ? "" : "s",
767 sc.TransferRate().c_str()));
Elliott Hughesaa245492015-08-03 10:38:08 -0700768 return true;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800769}
770
Elliott Hughesb708d162015-10-27 16:03:15 -0700771bool do_sync_pull(const char* rpath, const char* lpath, int copy_attrs) {
Elliott Hughesaa245492015-08-03 10:38:08 -0700772 SyncConnection sc;
773 if (!sc.IsValid()) return false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800774
Elliott Hughes5c742702015-07-30 17:42:01 -0700775 unsigned mode, time;
Elliott Hughesaa245492015-08-03 10:38:08 -0700776 if (!sync_stat(sc, rpath, &time, &mode, nullptr)) return false;
Elliott Hughes5c742702015-07-30 17:42:01 -0700777 if (mode == 0) {
Elliott Hughesb708d162015-10-27 16:03:15 -0700778 sc.Error("remote object '%s' does not exist", rpath);
Elliott Hughesaa245492015-08-03 10:38:08 -0700779 return false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800780 }
781
Elliott Hughes5c742702015-07-30 17:42:01 -0700782 if (S_ISREG(mode) || S_ISLNK(mode) || S_ISCHR(mode) || S_ISBLK(mode)) {
783 std::string path_holder;
784 struct stat st;
785 if (stat(lpath, &st) == 0) {
786 if (S_ISDIR(st.st_mode)) {
787 // If we're copying a remote file to a local directory,
788 // we really want to copy to local_dir + "/" + basename(remote).
789 path_holder = android::base::StringPrintf("%s/%s", lpath, adb_basename(rpath).c_str());
790 lpath = path_holder.c_str();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800791 }
792 }
Elliott Hughesb708d162015-10-27 16:03:15 -0700793 if (!sync_recv(sc, rpath, lpath)) {
Elliott Hughesaa245492015-08-03 10:38:08 -0700794 return false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800795 } else {
Elliott Hughes5c742702015-07-30 17:42:01 -0700796 if (copy_attrs && set_time_and_mode(lpath, time, mode)) {
Elliott Hughesaa245492015-08-03 10:38:08 -0700797 return false;
Elliott Hughes5c742702015-07-30 17:42:01 -0700798 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800799 }
Elliott Hughesb708d162015-10-27 16:03:15 -0700800 sc.Print("\n");
Elliott Hughesaa245492015-08-03 10:38:08 -0700801 return true;
802 } else if (S_ISDIR(mode)) {
803 return copy_remote_dir_local(sc, rpath, lpath, copy_attrs);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800804 }
Elliott Hughesaa245492015-08-03 10:38:08 -0700805
Elliott Hughesb708d162015-10-27 16:03:15 -0700806 sc.Error("remote object '%s' not a file or directory", rpath);
Elliott Hughesaa245492015-08-03 10:38:08 -0700807 return false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800808}
809
Elliott Hughesaa245492015-08-03 10:38:08 -0700810bool do_sync_sync(const std::string& lpath, const std::string& rpath, bool list_only) {
Elliott Hughesaa245492015-08-03 10:38:08 -0700811 SyncConnection sc;
812 if (!sc.IsValid()) return false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800813
Elliott Hughesaa245492015-08-03 10:38:08 -0700814 return copy_local_dir_remote(sc, lpath.c_str(), rpath.c_str(), true, list_only);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800815}