blob: 463c1c0d11d524908bd096bbc34c3265a77628e9 [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
Josh Gaocda6a2b2015-11-02 16:45:47 -080031#include <functional>
Elliott Hughesa925dba2015-08-24 14:49:43 -070032#include <memory>
Elliott Hughes6d929972015-10-27 13:40:35 -070033#include <vector>
Elliott Hughesa925dba2015-08-24 14:49:43 -070034
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080035#include "sysdeps.h"
Dan Albert76649012015-02-24 15:51:19 -080036
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080037#include "adb.h"
38#include "adb_client.h"
Dan Albertcc731cc2015-02-24 21:26:58 -080039#include "adb_io.h"
Alex Vallée14216142015-05-06 17:22:25 -040040#include "adb_utils.h"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080041#include "file_sync_service.h"
Elliott Hughesb708d162015-10-27 16:03:15 -070042#include "line_printer.h"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080043
Elliott Hughes4f713192015-12-04 22:00:26 -080044#include <android-base/file.h>
45#include <android-base/strings.h>
46#include <android-base/stringprintf.h>
Elliott Hughes5c742702015-07-30 17:42:01 -070047
Elliott Hughesaa245492015-08-03 10:38:08 -070048struct syncsendbuf {
49 unsigned id;
50 unsigned size;
51 char data[SYNC_DATA_MAX];
52};
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080053
Elliott Hughesaa245492015-08-03 10:38:08 -070054class SyncConnection {
55 public:
Elliott Hughesb708d162015-10-27 16:03:15 -070056 SyncConnection() : total_bytes(0), start_time_ms_(CurrentTimeMs()) {
Elliott Hughesaa245492015-08-03 10:38:08 -070057 max = SYNC_DATA_MAX; // TODO: decide at runtime.
58
59 std::string error;
60 fd = adb_connect("sync:", &error);
61 if (fd < 0) {
Elliott Hughesb708d162015-10-27 16:03:15 -070062 Error("connect failed: %s", error.c_str());
Elliott Hughesaa245492015-08-03 10:38:08 -070063 }
64 }
65
66 ~SyncConnection() {
67 if (!IsValid()) return;
68
Spencer Low351ecd12015-10-14 17:32:44 -070069 if (SendQuit()) {
70 // We sent a quit command, so the server should be doing orderly
71 // shutdown soon. But if we encountered an error while we were using
72 // the connection, the server might still be sending data (before
73 // doing orderly shutdown), in which case we won't wait for all of
74 // the data nor the coming orderly shutdown. In the common success
75 // case, this will wait for the server to do orderly shutdown.
76 ReadOrderlyShutdown(fd);
77 }
Elliott Hughesaa245492015-08-03 10:38:08 -070078 adb_close(fd);
79 }
80
81 bool IsValid() { return fd >= 0; }
82
Elliott Hughesae5a6c02015-09-27 12:55:37 -070083 bool SendRequest(int id, const char* path_and_mode) {
84 size_t path_length = strlen(path_and_mode);
85 if (path_length > 1024) {
Elliott Hughesb708d162015-10-27 16:03:15 -070086 Error("SendRequest failed: path too long: %zu", path_length);
Elliott Hughesae5a6c02015-09-27 12:55:37 -070087 errno = ENAMETOOLONG;
88 return false;
89 }
90
91 // Sending header and payload in a single write makes a noticeable
92 // difference to "adb sync" performance.
Elliott Hughes6d929972015-10-27 13:40:35 -070093 std::vector<char> buf(sizeof(SyncRequest) + path_length);
94 SyncRequest* req = reinterpret_cast<SyncRequest*>(&buf[0]);
Elliott Hughesae5a6c02015-09-27 12:55:37 -070095 req->id = id;
96 req->path_length = path_length;
97 char* data = reinterpret_cast<char*>(req + 1);
98 memcpy(data, path_and_mode, path_length);
99
Elliott Hughes6d929972015-10-27 13:40:35 -0700100 return WriteFdExactly(fd, &buf[0], buf.size());
Elliott Hughesae5a6c02015-09-27 12:55:37 -0700101 }
102
103 // Sending header, payload, and footer in a single write makes a huge
104 // difference to "adb sync" performance.
105 bool SendSmallFile(const char* path_and_mode,
Elliott Hughescc65c3b2015-11-20 22:01:06 -0800106 const char* lpath, const char* rpath,
Elliott Hughes6aab58c2015-11-20 17:35:17 -0800107 unsigned mtime,
108 const char* data, size_t data_length) {
Elliott Hughesb708d162015-10-27 16:03:15 -0700109 Print(rpath);
110
Elliott Hughesae5a6c02015-09-27 12:55:37 -0700111 size_t path_length = strlen(path_and_mode);
112 if (path_length > 1024) {
Elliott Hughesb708d162015-10-27 16:03:15 -0700113 Error("SendSmallFile failed: path too long: %zu", path_length);
Elliott Hughesae5a6c02015-09-27 12:55:37 -0700114 errno = ENAMETOOLONG;
115 return false;
116 }
117
Elliott Hughes6d929972015-10-27 13:40:35 -0700118 std::vector<char> buf(sizeof(SyncRequest) + path_length +
Elliott Hughes6aab58c2015-11-20 17:35:17 -0800119 sizeof(SyncRequest) + data_length +
120 sizeof(SyncRequest));
Elliott Hughes6d929972015-10-27 13:40:35 -0700121 char* p = &buf[0];
Elliott Hughesae5a6c02015-09-27 12:55:37 -0700122
123 SyncRequest* req_send = reinterpret_cast<SyncRequest*>(p);
124 req_send->id = ID_SEND;
125 req_send->path_length = path_length;
126 p += sizeof(SyncRequest);
127 memcpy(p, path_and_mode, path_length);
128 p += path_length;
129
130 SyncRequest* req_data = reinterpret_cast<SyncRequest*>(p);
131 req_data->id = ID_DATA;
132 req_data->path_length = data_length;
133 p += sizeof(SyncRequest);
134 memcpy(p, data, data_length);
135 p += data_length;
136
137 SyncRequest* req_done = reinterpret_cast<SyncRequest*>(p);
138 req_done->id = ID_DONE;
139 req_done->path_length = mtime;
140 p += sizeof(SyncRequest);
141
Elliott Hughescc65c3b2015-11-20 22:01:06 -0800142 WriteOrDie(lpath, rpath, &buf[0], (p - &buf[0]));
Elliott Hughesae5a6c02015-09-27 12:55:37 -0700143 total_bytes += data_length;
144 return true;
145 }
146
Elliott Hughes6aab58c2015-11-20 17:35:17 -0800147 bool SendLargeFile(const char* path_and_mode,
148 const char* lpath, const char* rpath,
149 unsigned mtime) {
150 if (!SendRequest(ID_SEND, path_and_mode)) {
151 Error("failed to send ID_SEND message '%s': %s", path_and_mode, strerror(errno));
152 return false;
153 }
154
155 struct stat st;
156 if (stat(lpath, &st) == -1) {
157 Error("cannot stat '%s': %s", lpath, strerror(errno));
158 return false;
159 }
160
161 uint64_t total_size = st.st_size;
162 uint64_t bytes_copied = 0;
163
164 int lfd = adb_open(lpath, O_RDONLY);
165 if (lfd < 0) {
Elliott Hughescc65c3b2015-11-20 22:01:06 -0800166 Error("opening '%s' locally failed: %s", lpath, strerror(errno));
Elliott Hughes6aab58c2015-11-20 17:35:17 -0800167 return false;
168 }
169
170 syncsendbuf sbuf;
171 sbuf.id = ID_DATA;
172 while (true) {
Elliott Hughescc65c3b2015-11-20 22:01:06 -0800173 int bytes_read = adb_read(lfd, sbuf.data, max);
174 if (bytes_read == -1) {
175 Error("reading '%s' locally failed: %s", lpath, strerror(errno));
176 adb_close(lfd);
177 return false;
178 } else if (bytes_read == 0) {
Elliott Hughes6aab58c2015-11-20 17:35:17 -0800179 break;
180 }
181
Elliott Hughescc65c3b2015-11-20 22:01:06 -0800182 sbuf.size = bytes_read;
183 WriteOrDie(lpath, rpath, &sbuf, sizeof(SyncRequest) + bytes_read);
Elliott Hughes6aab58c2015-11-20 17:35:17 -0800184
Elliott Hughescc65c3b2015-11-20 22:01:06 -0800185 total_bytes += bytes_read;
186 bytes_copied += bytes_read;
Elliott Hughes6aab58c2015-11-20 17:35:17 -0800187
Josh Gaob0e039f2015-11-30 10:53:22 -0800188 if (total_size == 0) {
189 // This case can happen if we're racing against something that wrote to the file
190 // between our stat and our read, or if we're reading a magic file that lies about
191 // its size.
192 Printf("%s: ?%%", rpath);
193 } else {
194 int percentage = static_cast<int>(bytes_copied * 100 / total_size);
195 Printf("%s: %d%%", rpath, percentage);
196 }
Elliott Hughes6aab58c2015-11-20 17:35:17 -0800197 }
198
199 adb_close(lfd);
200
201 syncmsg msg;
202 msg.data.id = ID_DONE;
203 msg.data.size = mtime;
Elliott Hughescc65c3b2015-11-20 22:01:06 -0800204 return WriteOrDie(lpath, rpath, &msg.data, sizeof(msg.data));
Elliott Hughes6aab58c2015-11-20 17:35:17 -0800205 }
206
Elliott Hughesae5a6c02015-09-27 12:55:37 -0700207 bool CopyDone(const char* from, const char* to) {
208 syncmsg msg;
209 if (!ReadFdExactly(fd, &msg.status, sizeof(msg.status))) {
Elliott Hughesb708d162015-10-27 16:03:15 -0700210 Error("failed to copy '%s' to '%s': no ID_DONE: %s", from, to, strerror(errno));
Elliott Hughesae5a6c02015-09-27 12:55:37 -0700211 return false;
212 }
213 if (msg.status.id == ID_OKAY) {
214 return true;
215 }
216 if (msg.status.id != ID_FAIL) {
Elliott Hughesb708d162015-10-27 16:03:15 -0700217 Error("failed to copy '%s' to '%s': unknown reason %d", from, to, msg.status.id);
Elliott Hughesae5a6c02015-09-27 12:55:37 -0700218 return false;
219 }
Elliott Hughes8b43c3e2015-10-23 21:06:11 -0700220 return ReportCopyFailure(from, to, msg);
221 }
222
223 bool ReportCopyFailure(const char* from, const char* to, const syncmsg& msg) {
Elliott Hughes6d929972015-10-27 13:40:35 -0700224 std::vector<char> buf(msg.status.msglen + 1);
225 if (!ReadFdExactly(fd, &buf[0], msg.status.msglen)) {
Elliott Hughesb708d162015-10-27 16:03:15 -0700226 Error("failed to copy '%s' to '%s'; failed to read reason (!): %s",
227 from, to, strerror(errno));
Elliott Hughesae5a6c02015-09-27 12:55:37 -0700228 return false;
229 }
Elliott Hughes6d929972015-10-27 13:40:35 -0700230 buf[msg.status.msglen] = 0;
Elliott Hughesb708d162015-10-27 16:03:15 -0700231 Error("failed to copy '%s' to '%s': %s", from, to, &buf[0]);
Elliott Hughesae5a6c02015-09-27 12:55:37 -0700232 return false;
233 }
234
Elliott Hughesb708d162015-10-27 16:03:15 -0700235 std::string TransferRate() {
236 uint64_t ms = CurrentTimeMs() - start_time_ms_;
237 if (total_bytes == 0 || ms == 0) return "";
238
239 double s = static_cast<double>(ms) / 1000LL;
240 double rate = (static_cast<double>(total_bytes) / s) / (1024*1024);
241 return android::base::StringPrintf(" %.1f MB/s (%" PRId64 " bytes in %.3fs)",
242 rate, total_bytes, s);
243 }
244
245 void Print(const std::string& s) {
246 // TODO: we actually don't want ELIDE; we want "ELIDE if smart, FULL if dumb".
247 line_printer_.Print(s, LinePrinter::ELIDE);
248 }
249
Josh Gao983c41c2015-11-02 17:15:57 -0800250 void Printf(const char* fmt, ...) __attribute__((__format__(ADB_FORMAT_ARCHETYPE, 2, 3))) {
251 std::string s;
252 va_list ap;
253 va_start(ap, fmt);
254 android::base::StringAppendV(&s, fmt, ap);
255 va_end(ap);
256
257 Print(s);
258 }
259
Elliott Hughesb708d162015-10-27 16:03:15 -0700260 void Error(const char* fmt, ...) __attribute__((__format__(ADB_FORMAT_ARCHETYPE, 2, 3))) {
261 std::string s = "adb: error: ";
262
263 va_list ap;
264 va_start(ap, fmt);
265 android::base::StringAppendV(&s, fmt, ap);
266 va_end(ap);
267
268 line_printer_.Print(s, LinePrinter::FULL);
269 }
270
Josh Gao21abf5a2015-11-07 17:18:44 -0800271 void Warning(const char* fmt, ...) __attribute__((__format__(ADB_FORMAT_ARCHETYPE, 2, 3))) {
272 std::string s = "adb: warning: ";
273
274 va_list ap;
275 va_start(ap, fmt);
276 android::base::StringAppendV(&s, fmt, ap);
277 va_end(ap);
278
279 line_printer_.Print(s, LinePrinter::FULL);
280 }
281
Elliott Hughesaa245492015-08-03 10:38:08 -0700282 uint64_t total_bytes;
283
284 // TODO: add a char[max] buffer here, to replace syncsendbuf...
285 int fd;
286 size_t max;
287
288 private:
Elliott Hughesb708d162015-10-27 16:03:15 -0700289 uint64_t start_time_ms_;
290
291 LinePrinter line_printer_;
Elliott Hughesaa245492015-08-03 10:38:08 -0700292
Spencer Low351ecd12015-10-14 17:32:44 -0700293 bool SendQuit() {
294 return SendRequest(ID_QUIT, ""); // TODO: add a SendResponse?
Elliott Hughesaa245492015-08-03 10:38:08 -0700295 }
296
Elliott Hughescc65c3b2015-11-20 22:01:06 -0800297 bool WriteOrDie(const char* from, const char* to, const void* data, size_t data_length) {
298 if (!WriteFdExactly(fd, data, data_length)) {
299 if (errno == ECONNRESET) {
300 // Assume adbd told us why it was closing the connection, and
301 // try to read failure reason from adbd.
302 syncmsg msg;
303 if (!ReadFdExactly(fd, &msg.status, sizeof(msg.status))) {
304 Error("failed to copy '%s' to '%s': no response: %s", from, to, strerror(errno));
305 } else if (msg.status.id != ID_FAIL) {
306 Error("failed to copy '%s' to '%s': not ID_FAIL: %d", from, to, msg.status.id);
307 } else {
308 ReportCopyFailure(from, to, msg);
309 }
310 } else {
311 Error("%zu-byte write failed: %s", data_length, strerror(errno));
312 }
313 _exit(1);
314 }
315 return true;
316 }
317
Elliott Hughesb708d162015-10-27 16:03:15 -0700318 static uint64_t CurrentTimeMs() {
319 struct timeval tv;
320 gettimeofday(&tv, 0); // (Not clock_gettime because of Mac/Windows.)
321 return static_cast<uint64_t>(tv.tv_sec) * 1000 + tv.tv_usec / 1000;
Elliott Hughesaa245492015-08-03 10:38:08 -0700322 }
323};
324
Josh Gaocda6a2b2015-11-02 16:45:47 -0800325typedef void (sync_ls_cb)(unsigned mode, unsigned size, unsigned time, const char* name);
Elliott Hughesaa245492015-08-03 10:38:08 -0700326
Josh Gaocda6a2b2015-11-02 16:45:47 -0800327static bool sync_ls(SyncConnection& sc, const char* path,
328 std::function<sync_ls_cb> func) {
Elliott Hughesae5a6c02015-09-27 12:55:37 -0700329 if (!sc.SendRequest(ID_LIST, path)) return false;
Elliott Hughesaa245492015-08-03 10:38:08 -0700330
331 while (true) {
332 syncmsg msg;
Elliott Hughesae5a6c02015-09-27 12:55:37 -0700333 if (!ReadFdExactly(sc.fd, &msg.dent, sizeof(msg.dent))) return false;
Elliott Hughesaa245492015-08-03 10:38:08 -0700334
335 if (msg.dent.id == ID_DONE) return true;
336 if (msg.dent.id != ID_DENT) return false;
337
Elliott Hughesf4465202015-08-24 14:27:03 -0700338 size_t len = msg.dent.namelen;
Elliott Hughesaa245492015-08-03 10:38:08 -0700339 if (len > 256) return false; // TODO: resize buffer? continue?
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800340
Elliott Hughes5c742702015-07-30 17:42:01 -0700341 char buf[257];
Elliott Hughesae5a6c02015-09-27 12:55:37 -0700342 if (!ReadFdExactly(sc.fd, buf, len)) return false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800343 buf[len] = 0;
344
Josh Gaocda6a2b2015-11-02 16:45:47 -0800345 func(msg.dent.mode, msg.dent.size, msg.dent.time, buf);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800346 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800347}
348
Elliott Hughesaa245492015-08-03 10:38:08 -0700349static bool sync_finish_stat(SyncConnection& sc, unsigned int* timestamp,
350 unsigned int* mode, unsigned int* size) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800351 syncmsg msg;
Elliott Hughesaa245492015-08-03 10:38:08 -0700352 if (!ReadFdExactly(sc.fd, &msg.stat, sizeof(msg.stat)) || msg.stat.id != ID_STAT) {
353 return false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800354 }
355
Elliott Hughesf4465202015-08-24 14:27:03 -0700356 if (timestamp) *timestamp = msg.stat.time;
357 if (mode) *mode = msg.stat.mode;
358 if (size) *size = msg.stat.size;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800359
Elliott Hughesaa245492015-08-03 10:38:08 -0700360 return true;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800361}
362
Elliott Hughesaa245492015-08-03 10:38:08 -0700363static bool sync_stat(SyncConnection& sc, const char* path,
364 unsigned int* timestamp, unsigned int* mode, unsigned int* size) {
Elliott Hughesae5a6c02015-09-27 12:55:37 -0700365 return sc.SendRequest(ID_STAT, path) && sync_finish_stat(sc, timestamp, mode, size);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800366}
367
Elliott Hughesae5a6c02015-09-27 12:55:37 -0700368static bool sync_send(SyncConnection& sc, const char* lpath, const char* rpath,
Elliott Hughesb708d162015-10-27 16:03:15 -0700369 unsigned mtime, mode_t mode)
Elliott Hughesae5a6c02015-09-27 12:55:37 -0700370{
371 std::string path_and_mode = android::base::StringPrintf("%s,%d", rpath, mode);
372
373 if (S_ISLNK(mode)) {
374#if !defined(_WIN32)
375 char buf[PATH_MAX];
376 ssize_t data_length = readlink(lpath, buf, PATH_MAX - 1);
377 if (data_length == -1) {
Elliott Hughesb708d162015-10-27 16:03:15 -0700378 sc.Error("readlink '%s' failed: %s", lpath, strerror(errno));
Elliott Hughesae5a6c02015-09-27 12:55:37 -0700379 return false;
380 }
381 buf[data_length++] = '\0';
382
Elliott Hughescc65c3b2015-11-20 22:01:06 -0800383 if (!sc.SendSmallFile(path_and_mode.c_str(), lpath, rpath, mtime, buf, data_length)) {
384 return false;
385 }
Elliott Hughesae5a6c02015-09-27 12:55:37 -0700386 return sc.CopyDone(lpath, rpath);
387#endif
388 }
389
390 if (!S_ISREG(mode)) {
Elliott Hughesb708d162015-10-27 16:03:15 -0700391 sc.Error("local file '%s' has unsupported mode: 0o%o", lpath, mode);
Elliott Hughesae5a6c02015-09-27 12:55:37 -0700392 return false;
393 }
394
395 struct stat st;
396 if (stat(lpath, &st) == -1) {
Elliott Hughesb708d162015-10-27 16:03:15 -0700397 sc.Error("failed to stat local file '%s': %s", lpath, strerror(errno));
Elliott Hughesae5a6c02015-09-27 12:55:37 -0700398 return false;
399 }
400 if (st.st_size < SYNC_DATA_MAX) {
401 std::string data;
402 if (!android::base::ReadFileToString(lpath, &data)) {
Elliott Hughesb708d162015-10-27 16:03:15 -0700403 sc.Error("failed to read all of '%s': %s", lpath, strerror(errno));
Elliott Hughesae5a6c02015-09-27 12:55:37 -0700404 return false;
405 }
Elliott Hughescc65c3b2015-11-20 22:01:06 -0800406 if (!sc.SendSmallFile(path_and_mode.c_str(), lpath, rpath, mtime,
407 data.data(), data.size())) {
Elliott Hughesb708d162015-10-27 16:03:15 -0700408 return false;
409 }
Elliott Hughesae5a6c02015-09-27 12:55:37 -0700410 } else {
Elliott Hughes6aab58c2015-11-20 17:35:17 -0800411 if (!sc.SendLargeFile(path_and_mode.c_str(), lpath, rpath, mtime)) {
Elliott Hughesb708d162015-10-27 16:03:15 -0700412 return false;
413 }
Elliott Hughesae5a6c02015-09-27 12:55:37 -0700414 }
415 return sc.CopyDone(lpath, rpath);
416}
417
Elliott Hughesb708d162015-10-27 16:03:15 -0700418static bool sync_recv(SyncConnection& sc, const char* rpath, const char* lpath) {
419 sc.Print(rpath);
420
Elliott Hughesaa245492015-08-03 10:38:08 -0700421 unsigned size = 0;
Elliott Hughesb708d162015-10-27 16:03:15 -0700422 if (!sync_stat(sc, rpath, nullptr, nullptr, &size)) return false;
Mark Lindner76f2a932014-03-11 17:55:59 -0700423
Elliott Hughesae5a6c02015-09-27 12:55:37 -0700424 if (!sc.SendRequest(ID_RECV, rpath)) return false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800425
Elliott Hughes8b43c3e2015-10-23 21:06:11 -0700426 adb_unlink(lpath);
Josh Gaof6e65e32015-11-17 14:08:20 -0800427 const std::string dirpath = adb_dirname(lpath);
428 if (!mkdirs(dirpath.c_str())) {
429 sc.Error("failed to create parent directory '%s': %s", dirpath.c_str(), strerror(errno));
Josh Gao45b6fc82015-11-04 14:51:23 -0800430 return false;
431 }
432
Elliott Hughes8b43c3e2015-10-23 21:06:11 -0700433 int lfd = adb_creat(lpath, 0644);
434 if (lfd < 0) {
Elliott Hughesb708d162015-10-27 16:03:15 -0700435 sc.Error("cannot create '%s': %s", lpath, strerror(errno));
Elliott Hughes8b43c3e2015-10-23 21:06:11 -0700436 return false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800437 }
438
Elliott Hughesb708d162015-10-27 16:03:15 -0700439 uint64_t bytes_copied = 0;
Elliott Hughesaa245492015-08-03 10:38:08 -0700440 while (true) {
Elliott Hughes8b43c3e2015-10-23 21:06:11 -0700441 syncmsg msg;
Elliott Hughesae5a6c02015-09-27 12:55:37 -0700442 if (!ReadFdExactly(sc.fd, &msg.data, sizeof(msg.data))) {
Spencer Lowd8cce182015-08-28 01:07:30 -0700443 adb_close(lfd);
Elliott Hughes8b43c3e2015-10-23 21:06:11 -0700444 adb_unlink(lpath);
Elliott Hughesae5a6c02015-09-27 12:55:37 -0700445 return false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800446 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800447
Elliott Hughes8b43c3e2015-10-23 21:06:11 -0700448 if (msg.data.id == ID_DONE) break;
449
450 if (msg.data.id != ID_DATA) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800451 adb_close(lfd);
Elliott Hughes8b43c3e2015-10-23 21:06:11 -0700452 adb_unlink(lpath);
453 sc.ReportCopyFailure(rpath, lpath, msg);
Elliott Hughesae5a6c02015-09-27 12:55:37 -0700454 return false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800455 }
456
Elliott Hughes8b43c3e2015-10-23 21:06:11 -0700457 if (msg.data.size > sc.max) {
Elliott Hughesb708d162015-10-27 16:03:15 -0700458 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 -0800459 adb_close(lfd);
Elliott Hughes8b43c3e2015-10-23 21:06:11 -0700460 adb_unlink(lpath);
Elliott Hughesae5a6c02015-09-27 12:55:37 -0700461 return false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800462 }
463
Elliott Hughes8b43c3e2015-10-23 21:06:11 -0700464 char buffer[SYNC_DATA_MAX];
465 if (!ReadFdExactly(sc.fd, buffer, msg.data.size)) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800466 adb_close(lfd);
Elliott Hughes8b43c3e2015-10-23 21:06:11 -0700467 adb_unlink(lpath);
Elliott Hughesae5a6c02015-09-27 12:55:37 -0700468 return false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800469 }
470
Elliott Hughes8b43c3e2015-10-23 21:06:11 -0700471 if (!WriteFdExactly(lfd, buffer, msg.data.size)) {
Elliott Hughesb708d162015-10-27 16:03:15 -0700472 sc.Error("cannot write '%s': %s", lpath, strerror(errno));
Elliott Hughes8b43c3e2015-10-23 21:06:11 -0700473 adb_close(lfd);
474 adb_unlink(lpath);
475 return false;
476 }
477
478 sc.total_bytes += msg.data.size;
Mark Lindner76f2a932014-03-11 17:55:59 -0700479
Elliott Hughesb708d162015-10-27 16:03:15 -0700480 bytes_copied += msg.data.size;
481
Josh Gaob0e039f2015-11-30 10:53:22 -0800482 if (size == 0) {
483 // This case can happen if we're racing against something that wrote to the file between
484 // our stat and our read, or if we're reading a magic file that lies about its size.
485 sc.Printf("%s: ?%%", rpath);
486 } else {
487 int percentage = static_cast<int>(bytes_copied * 100 / size);
488 sc.Printf("%s: %d%%", rpath, percentage);
489 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800490 }
491
492 adb_close(lfd);
Elliott Hughesae5a6c02015-09-27 12:55:37 -0700493 return true;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800494}
495
Elliott Hughesaa245492015-08-03 10:38:08 -0700496bool do_sync_ls(const char* path) {
497 SyncConnection sc;
498 if (!sc.IsValid()) return false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800499
Josh Gaocda6a2b2015-11-02 16:45:47 -0800500 return sync_ls(sc, path, [](unsigned mode, unsigned size, unsigned time,
501 const char* name) {
502 printf("%08x %08x %08x %s\n", mode, size, time, name);
503 });
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800504}
505
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800506struct copyinfo
507{
Josh Gao1a025302015-11-09 11:12:14 -0800508 std::string lpath;
509 std::string rpath;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800510 unsigned int time;
511 unsigned int mode;
Elliott Hughesae5a6c02015-09-27 12:55:37 -0700512 uint64_t size;
Josh Gaofc7c3b62015-11-03 14:44:04 -0800513 bool skip;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800514};
515
Josh Gao1a025302015-11-09 11:12:14 -0800516static void ensure_trailing_separator(std::string& lpath, std::string& rpath) {
517 if (!adb_is_separator(lpath.back())) {
518 lpath.push_back(OS_PATH_SEPARATOR);
519 }
520 if (rpath.back() != '/') {
521 rpath.push_back('/');
522 }
523}
524
525static copyinfo mkcopyinfo(std::string lpath, std::string rpath,
Josh Gao65800962015-11-04 14:57:04 -0800526 const std::string& name, unsigned int mode) {
Josh Gaocda6a2b2015-11-02 16:45:47 -0800527 copyinfo result;
Josh Gao1a025302015-11-09 11:12:14 -0800528 result.lpath = std::move(lpath);
529 result.rpath = std::move(rpath);
530 ensure_trailing_separator(result.lpath, result.rpath);
531 result.lpath.append(name);
532 result.rpath.append(name);
Josh Gao12a2ae92015-11-07 15:27:26 -0800533
Josh Gao1a025302015-11-09 11:12:14 -0800534 if (S_ISDIR(mode)) {
535 ensure_trailing_separator(result.lpath, result.rpath);
Josh Gaod9731572015-11-03 15:23:03 -0800536 }
Josh Gao65800962015-11-04 14:57:04 -0800537
Josh Gaocda6a2b2015-11-02 16:45:47 -0800538 result.time = 0;
Josh Gao65800962015-11-04 14:57:04 -0800539 result.mode = mode;
Josh Gaocda6a2b2015-11-02 16:45:47 -0800540 result.size = 0;
Josh Gaofc7c3b62015-11-03 14:44:04 -0800541 result.skip = false;
Josh Gaocda6a2b2015-11-02 16:45:47 -0800542 return result;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800543}
544
Elliott Hughesaa245492015-08-03 10:38:08 -0700545static bool IsDotOrDotDot(const char* name) {
546 return name[0] == '.' && (name[1] == '\0' || (name[1] == '.' && name[2] == '\0'));
547}
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800548
Josh Gaocd7c1ed2015-11-03 15:26:38 -0800549static bool local_build_list(SyncConnection& sc, std::vector<copyinfo>* filelist,
550 const std::string& lpath,
551 const std::string& rpath) {
Josh Gaocda6a2b2015-11-02 16:45:47 -0800552 std::vector<copyinfo> dirlist;
Josh Gaod9731572015-11-03 15:23:03 -0800553 std::unique_ptr<DIR, int (*)(DIR*)> dir(opendir(lpath.c_str()), closedir);
Elliott Hughesaa245492015-08-03 10:38:08 -0700554 if (!dir) {
Josh Gaod9731572015-11-03 15:23:03 -0800555 sc.Error("cannot open '%s': %s", lpath.c_str(), strerror(errno));
Josh Gaocd7c1ed2015-11-03 15:26:38 -0800556 return false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800557 }
558
Josh Gao65800962015-11-04 14:57:04 -0800559 bool empty_dir = true;
Elliott Hughesb708d162015-10-27 16:03:15 -0700560 dirent* de;
Elliott Hughesaa245492015-08-03 10:38:08 -0700561 while ((de = readdir(dir.get()))) {
Josh Gao65800962015-11-04 14:57:04 -0800562 if (IsDotOrDotDot(de->d_name)) {
563 continue;
564 }
Elliott Hughesaa245492015-08-03 10:38:08 -0700565
Josh Gao65800962015-11-04 14:57:04 -0800566 empty_dir = false;
Josh Gaod9731572015-11-03 15:23:03 -0800567 std::string stat_path = lpath + de->d_name;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800568
Elliott Hughesaa245492015-08-03 10:38:08 -0700569 struct stat st;
Josh Gao12a2ae92015-11-07 15:27:26 -0800570 if (lstat(stat_path.c_str(), &st) == -1) {
Josh Gaod9731572015-11-03 15:23:03 -0800571 sc.Error("cannot lstat '%s': %s", stat_path.c_str(),
572 strerror(errno));
Josh Gao12a2ae92015-11-07 15:27:26 -0800573 continue;
574 }
575
576 copyinfo ci = mkcopyinfo(lpath, rpath, de->d_name, st.st_mode);
577 if (S_ISDIR(st.st_mode)) {
578 dirlist.push_back(ci);
579 } else {
580 if (!S_ISREG(st.st_mode) && !S_ISLNK(st.st_mode)) {
581 sc.Error("skipping special file '%s'", lpath.c_str());
Josh Gaodd6cc4d2015-11-30 10:21:25 -0800582 ci.skip = true;
Josh Gao12a2ae92015-11-07 15:27:26 -0800583 } else {
584 ci.time = st.st_mtime;
585 ci.size = st.st_size;
Josh Gao12a2ae92015-11-07 15:27:26 -0800586 }
Josh Gaodd6cc4d2015-11-30 10:21:25 -0800587 filelist->push_back(ci);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800588 }
589 }
590
Elliott Hughesaa245492015-08-03 10:38:08 -0700591 // Close this directory and recurse.
592 dir.reset();
Josh Gao65800962015-11-04 14:57:04 -0800593
594 // Add the current directory to the list if it was empty, to ensure that
595 // it gets created.
596 if (empty_dir) {
597 // TODO(b/25566053): Make pushing empty directories work.
598 // TODO(b/25457350): We don't preserve permissions on directories.
Josh Gao21abf5a2015-11-07 17:18:44 -0800599 sc.Warning("skipping empty directory '%s'", lpath.c_str());
Josh Gao65800962015-11-04 14:57:04 -0800600 copyinfo ci = mkcopyinfo(adb_dirname(lpath), adb_dirname(rpath),
601 adb_basename(lpath), S_IFDIR);
602 ci.skip = true;
603 filelist->push_back(ci);
604 return true;
605 }
606
Josh Gaocda6a2b2015-11-02 16:45:47 -0800607 for (const copyinfo& ci : dirlist) {
Josh Gao1a025302015-11-09 11:12:14 -0800608 local_build_list(sc, filelist, ci.lpath, ci.rpath);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800609 }
610
Josh Gaocd7c1ed2015-11-03 15:26:38 -0800611 return true;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800612}
613
Josh Gaod9731572015-11-03 15:23:03 -0800614static bool copy_local_dir_remote(SyncConnection& sc, std::string lpath,
615 std::string rpath, bool check_timestamps,
616 bool list_only) {
617 // Make sure that both directory paths end in a slash.
Josh Gao1a025302015-11-09 11:12:14 -0800618 // Both paths are known to be nonempty, so we don't need to check.
619 ensure_trailing_separator(lpath, rpath);
Josh Gaod9731572015-11-03 15:23:03 -0800620
621 // Recursively build the list of files to copy.
Josh Gaocda6a2b2015-11-02 16:45:47 -0800622 std::vector<copyinfo> filelist;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800623 int pushed = 0;
624 int skipped = 0;
Josh Gaocd7c1ed2015-11-03 15:26:38 -0800625 if (!local_build_list(sc, &filelist, lpath, rpath)) {
Elliott Hughesaa245492015-08-03 10:38:08 -0700626 return false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800627 }
628
Elliott Hughesaa245492015-08-03 10:38:08 -0700629 if (check_timestamps) {
Josh Gaocda6a2b2015-11-02 16:45:47 -0800630 for (const copyinfo& ci : filelist) {
Josh Gao1a025302015-11-09 11:12:14 -0800631 if (!sc.SendRequest(ID_STAT, ci.rpath.c_str())) {
Josh Gaod9731572015-11-03 15:23:03 -0800632 return false;
633 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800634 }
Josh Gaocda6a2b2015-11-02 16:45:47 -0800635 for (copyinfo& ci : filelist) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800636 unsigned int timestamp, mode, size;
Josh Gaod9731572015-11-03 15:23:03 -0800637 if (!sync_finish_stat(sc, &timestamp, &mode, &size)) {
638 return false;
639 }
Josh Gaocda6a2b2015-11-02 16:45:47 -0800640 if (size == ci.size) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800641 /* for links, we cannot update the atime/mtime */
Josh Gaocda6a2b2015-11-02 16:45:47 -0800642 if ((S_ISREG(ci.mode & mode) && timestamp == ci.time) ||
643 (S_ISLNK(ci.mode & mode) && timestamp >= ci.time)) {
Josh Gaofc7c3b62015-11-03 14:44:04 -0800644 ci.skip = true;
Elliott Hughesaa245492015-08-03 10:38:08 -0700645 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800646 }
647 }
648 }
Josh Gaocda6a2b2015-11-02 16:45:47 -0800649
650 for (const copyinfo& ci : filelist) {
Josh Gaofc7c3b62015-11-03 14:44:04 -0800651 if (!ci.skip) {
Elliott Hughesb708d162015-10-27 16:03:15 -0700652 if (list_only) {
Josh Gao1a025302015-11-09 11:12:14 -0800653 sc.Error("would push: %s -> %s", ci.lpath.c_str(),
654 ci.rpath.c_str());
Elliott Hughesb708d162015-10-27 16:03:15 -0700655 } else {
Josh Gao1a025302015-11-09 11:12:14 -0800656 if (!sync_send(sc, ci.lpath.c_str(), ci.rpath.c_str(), ci.time,
Josh Gaod9731572015-11-03 15:23:03 -0800657 ci.mode)) {
658 return false;
Elliott Hughesb708d162015-10-27 16:03:15 -0700659 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800660 }
661 pushed++;
662 } else {
663 skipped++;
664 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800665 }
666
Josh Gaod9731572015-11-03 15:23:03 -0800667 sc.Printf("%s: %d file%s pushed. %d file%s skipped.%s\n", rpath.c_str(),
668 pushed, (pushed == 1) ? "" : "s", skipped,
669 (skipped == 1) ? "" : "s", sc.TransferRate().c_str());
Elliott Hughesaa245492015-08-03 10:38:08 -0700670 return true;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800671}
672
Josh Gao05786772015-10-30 16:57:19 -0700673bool do_sync_push(const std::vector<const char*>& srcs, const char* dst) {
Elliott Hughesaa245492015-08-03 10:38:08 -0700674 SyncConnection sc;
675 if (!sc.IsValid()) return false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800676
Josh Gao05786772015-10-30 16:57:19 -0700677 bool success = true;
Josh Gao12a2ae92015-11-07 15:27:26 -0800678 unsigned dst_mode;
679 if (!sync_stat(sc, dst, nullptr, &dst_mode, nullptr)) return false;
680 bool dst_exists = (dst_mode != 0);
Josh Gao07db1192015-11-07 15:38:19 -0800681 bool dst_isdir = S_ISDIR(dst_mode);
Josh Gao05786772015-10-30 16:57:19 -0700682
Josh Gao07db1192015-11-07 15:38:19 -0800683 if (!dst_isdir) {
Josh Gao05786772015-10-30 16:57:19 -0700684 if (srcs.size() > 1) {
685 sc.Error("target '%s' is not a directory", dst);
686 return false;
687 } else {
688 size_t dst_len = strlen(dst);
Josh Gao12a2ae92015-11-07 15:27:26 -0800689
690 // A path that ends with a slash doesn't have to be a directory if
691 // it doesn't exist yet.
692 if (dst[dst_len - 1] == '/' && dst_exists) {
Josh Gao05786772015-10-30 16:57:19 -0700693 sc.Error("failed to access '%s': Not a directory", dst);
694 return false;
695 }
696 }
Elliott Hughesaa245492015-08-03 10:38:08 -0700697 }
Josh Gao05786772015-10-30 16:57:19 -0700698
699 for (const char* src_path : srcs) {
700 const char* dst_path = dst;
701 struct stat st;
Josh Gao12a2ae92015-11-07 15:27:26 -0800702 if (stat(src_path, &st) == -1) {
Josh Gao05786772015-10-30 16:57:19 -0700703 sc.Error("cannot stat '%s': %s", src_path, strerror(errno));
704 success = false;
705 continue;
706 }
707
708 if (S_ISDIR(st.st_mode)) {
Josh Gao07db1192015-11-07 15:38:19 -0800709 std::string dst_dir = dst;
710
711 // If the destination path existed originally, the source directory
712 // should be copied as a child of the destination.
713 if (dst_exists) {
714 if (!dst_isdir) {
715 sc.Error("target '%s' is not a directory", dst);
716 return false;
717 }
718 // dst is a POSIX path, so we don't want to use the sysdeps
719 // helpers here.
720 if (dst_dir.back() != '/') {
721 dst_dir.push_back('/');
722 }
723 dst_dir.append(adb_basename(src_path));
724 }
725
726 success &= copy_local_dir_remote(sc, src_path, dst_dir.c_str(),
727 false, false);
Josh Gao05786772015-10-30 16:57:19 -0700728 continue;
729 }
730
731 std::string path_holder;
Josh Gao07db1192015-11-07 15:38:19 -0800732 if (dst_isdir) {
Josh Gao05786772015-10-30 16:57:19 -0700733 // If we're copying a local file to a remote directory,
734 // we really want to copy to remote_dir + "/" + local_filename.
735 path_holder = android::base::StringPrintf(
736 "%s/%s", dst_path, adb_basename(src_path).c_str());
737 dst_path = path_holder.c_str();
738 }
739 success &= sync_send(sc, src_path, dst_path, st.st_mtime, st.st_mode);
740 }
741
Elliott Hughesb708d162015-10-27 16:03:15 -0700742 sc.Print("\n");
Josh Gao05786772015-10-30 16:57:19 -0700743 return success;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800744}
745
Josh Gaocda6a2b2015-11-02 16:45:47 -0800746static bool remote_build_list(SyncConnection& sc,
747 std::vector<copyinfo>* filelist,
Josh Gaod9731572015-11-03 15:23:03 -0800748 const std::string& rpath,
749 const std::string& lpath) {
Josh Gaocda6a2b2015-11-02 16:45:47 -0800750 std::vector<copyinfo> dirlist;
Josh Gao65800962015-11-04 14:57:04 -0800751 bool empty_dir = true;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800752
Elliott Hughesaa245492015-08-03 10:38:08 -0700753 // Put the files/dirs in rpath on the lists.
Josh Gaodd6cc4d2015-11-30 10:21:25 -0800754 auto callback = [&](unsigned mode, unsigned size, unsigned time, const char* name) {
Josh Gao65800962015-11-04 14:57:04 -0800755 if (IsDotOrDotDot(name)) {
756 return;
757 }
Josh Gaocda6a2b2015-11-02 16:45:47 -0800758
Josh Gao65800962015-11-04 14:57:04 -0800759 // We found a child that isn't '.' or '..'.
760 empty_dir = false;
761
Josh Gao1a025302015-11-09 11:12:14 -0800762 copyinfo ci = mkcopyinfo(lpath, rpath, name, mode);
Josh Gao65800962015-11-04 14:57:04 -0800763 if (S_ISDIR(mode)) {
764 dirlist.push_back(ci);
Josh Gaocda6a2b2015-11-02 16:45:47 -0800765 } else {
Josh Gao7b284b22015-11-30 10:28:15 -0800766 if (S_ISREG(mode)) {
Josh Gaodd6cc4d2015-11-30 10:21:25 -0800767 ci.time = time;
768 ci.size = size;
Josh Gao7b284b22015-11-30 10:28:15 -0800769 } else if (S_ISLNK(mode)) {
770 sc.Warning("skipping symlink '%s'", name);
771 ci.skip = true;
Josh Gaodd6cc4d2015-11-30 10:21:25 -0800772 } else {
Josh Gaod3266e02015-11-30 10:27:54 -0800773 sc.Warning("skipping special file '%s'", name);
Josh Gaodd6cc4d2015-11-30 10:21:25 -0800774 ci.skip = true;
775 }
776 filelist->push_back(ci);
Josh Gaocda6a2b2015-11-02 16:45:47 -0800777 }
778 };
779
Josh Gaod9731572015-11-03 15:23:03 -0800780 if (!sync_ls(sc, rpath.c_str(), callback)) {
Elliott Hughesaa245492015-08-03 10:38:08 -0700781 return false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800782 }
783
Josh Gao65800962015-11-04 14:57:04 -0800784 // Add the current directory to the list if it was empty, to ensure that
785 // it gets created.
786 if (empty_dir) {
Josh Gao1a025302015-11-09 11:12:14 -0800787 filelist->push_back(mkcopyinfo(adb_dirname(lpath), adb_dirname(rpath),
Josh Gao65800962015-11-04 14:57:04 -0800788 adb_basename(rpath), S_IFDIR));
789 return true;
790 }
791
Elliott Hughesaa245492015-08-03 10:38:08 -0700792 // Recurse into each directory we found.
Josh Gaocda6a2b2015-11-02 16:45:47 -0800793 while (!dirlist.empty()) {
794 copyinfo current = dirlist.back();
795 dirlist.pop_back();
Josh Gao1a025302015-11-09 11:12:14 -0800796 if (!remote_build_list(sc, filelist, current.rpath, current.lpath)) {
Elliott Hughesaa245492015-08-03 10:38:08 -0700797 return false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800798 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800799 }
800
Elliott Hughesaa245492015-08-03 10:38:08 -0700801 return true;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800802}
803
Josh Gao1a025302015-11-09 11:12:14 -0800804static int set_time_and_mode(const std::string& lpath, time_t time,
805 unsigned int mode) {
Greg Hackmann7a5e2bd2014-05-06 08:48:18 -0700806 struct utimbuf times = { time, time };
Josh Gao1a025302015-11-09 11:12:14 -0800807 int r1 = utime(lpath.c_str(), &times);
Lajos Molnarde8ff4a2013-04-19 12:41:09 -0700808
809 /* use umask for permissions */
Josh Gaocda6a2b2015-11-02 16:45:47 -0800810 mode_t mask = umask(0000);
Lajos Molnarde8ff4a2013-04-19 12:41:09 -0700811 umask(mask);
Josh Gao1a025302015-11-09 11:12:14 -0800812 int r2 = chmod(lpath.c_str(), mode & ~mask);
Lajos Molnarde8ff4a2013-04-19 12:41:09 -0700813
Spencer Low363af562015-11-07 18:51:54 -0800814 return r1 ? r1 : r2;
Lajos Molnarde8ff4a2013-04-19 12:41:09 -0700815}
816
Josh Gaod9731572015-11-03 15:23:03 -0800817static bool copy_remote_dir_local(SyncConnection& sc, std::string rpath,
818 std::string lpath, bool copy_attrs) {
Elliott Hughesaa245492015-08-03 10:38:08 -0700819 // Make sure that both directory paths end in a slash.
Josh Gao12a2ae92015-11-07 15:27:26 -0800820 // Both paths are known to be nonempty, so we don't need to check.
Josh Gao1a025302015-11-09 11:12:14 -0800821 ensure_trailing_separator(lpath, rpath);
Riley Andrews4d04d242014-12-12 13:12:36 -0800822
Elliott Hughesaa245492015-08-03 10:38:08 -0700823 // Recursively build the list of files to copy.
Elliott Hughesb708d162015-10-27 16:03:15 -0700824 sc.Print("pull: building file list...");
Josh Gaocda6a2b2015-11-02 16:45:47 -0800825 std::vector<copyinfo> filelist;
Josh Gaod9731572015-11-03 15:23:03 -0800826 if (!remote_build_list(sc, &filelist, rpath.c_str(), lpath.c_str())) {
Josh Gaocda6a2b2015-11-02 16:45:47 -0800827 return false;
828 }
Elliott Hughesaa245492015-08-03 10:38:08 -0700829
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800830 int pulled = 0;
831 int skipped = 0;
Josh Gaocda6a2b2015-11-02 16:45:47 -0800832 for (const copyinfo &ci : filelist) {
Josh Gaofc7c3b62015-11-03 14:44:04 -0800833 if (!ci.skip) {
Josh Gao1a025302015-11-09 11:12:14 -0800834 sc.Printf("pull: %s -> %s", ci.rpath.c_str(), ci.lpath.c_str());
Josh Gao65800962015-11-04 14:57:04 -0800835
836 if (S_ISDIR(ci.mode)) {
837 // Entry is for an empty directory, create it and continue.
838 // TODO(b/25457350): We don't preserve permissions on directories.
Josh Gao1a025302015-11-09 11:12:14 -0800839 if (!mkdirs(ci.lpath)) {
Josh Gao65800962015-11-04 14:57:04 -0800840 sc.Error("failed to create directory '%s': %s",
Josh Gao1a025302015-11-09 11:12:14 -0800841 ci.lpath.c_str(), strerror(errno));
Josh Gao65800962015-11-04 14:57:04 -0800842 return false;
843 }
844 pulled++;
845 continue;
846 }
847
Josh Gao1a025302015-11-09 11:12:14 -0800848 if (!sync_recv(sc, ci.rpath.c_str(), ci.lpath.c_str())) {
Elliott Hughesaa245492015-08-03 10:38:08 -0700849 return false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800850 }
Lajos Molnarde8ff4a2013-04-19 12:41:09 -0700851
Josh Gao1a025302015-11-09 11:12:14 -0800852 if (copy_attrs && set_time_and_mode(ci.lpath, ci.time, ci.mode)) {
Elliott Hughesaa245492015-08-03 10:38:08 -0700853 return false;
Lajos Molnarde8ff4a2013-04-19 12:41:09 -0700854 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800855 pulled++;
856 } else {
857 skipped++;
858 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800859 }
860
Josh Gaod9731572015-11-03 15:23:03 -0800861 sc.Printf("%s: %d file%s pulled. %d file%s skipped.%s\n", rpath.c_str(),
862 pulled, (pulled == 1) ? "" : "s", skipped,
863 (skipped == 1) ? "" : "s", sc.TransferRate().c_str());
Elliott Hughesaa245492015-08-03 10:38:08 -0700864 return true;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800865}
866
Josh Gao05786772015-10-30 16:57:19 -0700867bool do_sync_pull(const std::vector<const char*>& srcs, const char* dst,
868 bool copy_attrs) {
Elliott Hughesaa245492015-08-03 10:38:08 -0700869 SyncConnection sc;
870 if (!sc.IsValid()) return false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800871
Josh Gao05786772015-10-30 16:57:19 -0700872 bool success = true;
Josh Gao05786772015-10-30 16:57:19 -0700873 struct stat st;
Josh Gao12a2ae92015-11-07 15:27:26 -0800874 bool dst_exists = true;
875
876 if (stat(dst, &st) == -1) {
877 dst_exists = false;
878
879 // If we're only pulling one path, the destination path might point to
Josh Gao05786772015-10-30 16:57:19 -0700880 // a path that doesn't exist yet.
Josh Gao12a2ae92015-11-07 15:27:26 -0800881 if (srcs.size() == 1 && errno == ENOENT) {
882 // However, its parent must exist.
883 struct stat parent_st;
884 if (stat(adb_dirname(dst).c_str(), &parent_st) == -1) {
885 sc.Error("cannot create file/directory '%s': %s", dst, strerror(errno));
886 return false;
887 }
888 } else {
889 sc.Error("failed to access '%s': %s", dst, strerror(errno));
Josh Gao05786772015-10-30 16:57:19 -0700890 return false;
891 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800892 }
893
Josh Gao07db1192015-11-07 15:38:19 -0800894 bool dst_isdir = dst_exists && S_ISDIR(st.st_mode);
895 if (!dst_isdir) {
Josh Gao05786772015-10-30 16:57:19 -0700896 if (srcs.size() > 1) {
897 sc.Error("target '%s' is not a directory", dst);
Elliott Hughesaa245492015-08-03 10:38:08 -0700898 return false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800899 } else {
Josh Gao05786772015-10-30 16:57:19 -0700900 size_t dst_len = strlen(dst);
Josh Gao12a2ae92015-11-07 15:27:26 -0800901
902 // A path that ends with a slash doesn't have to be a directory if
903 // it doesn't exist yet.
Josh Gao1a025302015-11-09 11:12:14 -0800904 if (adb_is_separator(dst[dst_len - 1]) && dst_exists) {
Josh Gao05786772015-10-30 16:57:19 -0700905 sc.Error("failed to access '%s': Not a directory", dst);
Elliott Hughesaa245492015-08-03 10:38:08 -0700906 return false;
Elliott Hughes5c742702015-07-30 17:42:01 -0700907 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800908 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800909 }
Elliott Hughesaa245492015-08-03 10:38:08 -0700910
Josh Gao05786772015-10-30 16:57:19 -0700911 for (const char* src_path : srcs) {
912 const char* dst_path = dst;
Josh Gao12a2ae92015-11-07 15:27:26 -0800913 unsigned src_mode, src_time;
914 if (!sync_stat(sc, src_path, &src_time, &src_mode, nullptr)) {
915 return false;
916 }
917 if (src_mode == 0) {
Josh Gao05786772015-10-30 16:57:19 -0700918 sc.Error("remote object '%s' does not exist", src_path);
919 success = false;
920 continue;
921 }
922
Josh Gaof96dc732015-11-11 15:10:53 -0800923 if (S_ISREG(src_mode)) {
Josh Gao05786772015-10-30 16:57:19 -0700924 std::string path_holder;
Josh Gao07db1192015-11-07 15:38:19 -0800925 if (dst_isdir) {
926 // If we're copying a remote file to a local directory, we
Josh Gao1a025302015-11-09 11:12:14 -0800927 // really want to copy to local_dir + OS_PATH_SEPARATOR +
928 // basename(remote).
Josh Gao07db1192015-11-07 15:38:19 -0800929 path_holder = android::base::StringPrintf(
Josh Gao1a025302015-11-09 11:12:14 -0800930 "%s%c%s", dst_path, OS_PATH_SEPARATOR,
931 adb_basename(src_path).c_str());
Josh Gao07db1192015-11-07 15:38:19 -0800932 dst_path = path_holder.c_str();
Josh Gao05786772015-10-30 16:57:19 -0700933 }
934 if (!sync_recv(sc, src_path, dst_path)) {
935 success = false;
936 continue;
937 } else {
Josh Gao12a2ae92015-11-07 15:27:26 -0800938 if (copy_attrs &&
939 set_time_and_mode(dst_path, src_time, src_mode) != 0) {
Josh Gao05786772015-10-30 16:57:19 -0700940 success = false;
941 continue;
942 }
943 }
Josh Gao12a2ae92015-11-07 15:27:26 -0800944 } else if (S_ISDIR(src_mode)) {
Josh Gao07db1192015-11-07 15:38:19 -0800945 std::string dst_dir = dst;
946
947 // If the destination path existed originally, the source directory
948 // should be copied as a child of the destination.
949 if (dst_exists) {
950 if (!dst_isdir) {
951 sc.Error("target '%s' is not a directory", dst);
952 return false;
953 }
954 if (!adb_is_separator(dst_dir.back())) {
955 dst_dir.push_back(OS_PATH_SEPARATOR);
956 }
957 dst_dir.append(adb_basename(src_path));
958 }
959
960 success &= copy_remote_dir_local(sc, src_path, dst_dir.c_str(),
961 copy_attrs);
Josh Gao05786772015-10-30 16:57:19 -0700962 continue;
963 } else {
964 sc.Error("remote object '%s' not a file or directory", src_path);
965 success = false;
966 continue;
967 }
968 }
969
970 sc.Print("\n");
971 return success;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800972}
973
Elliott Hughesaa245492015-08-03 10:38:08 -0700974bool do_sync_sync(const std::string& lpath, const std::string& rpath, bool list_only) {
Elliott Hughesaa245492015-08-03 10:38:08 -0700975 SyncConnection sc;
976 if (!sc.IsValid()) return false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800977
Josh Gaod9731572015-11-03 15:23:03 -0800978 return copy_local_dir_remote(sc, lpath, rpath, true, list_only);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800979}