The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2007 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Dan Albert | 7664901 | 2015-02-24 15:51:19 -0800 | [diff] [blame] | 17 | #include <dirent.h> |
| 18 | #include <errno.h> |
Spencer Low | 6001c87 | 2015-05-13 00:02:55 -0700 | [diff] [blame] | 19 | #include <inttypes.h> |
Dan Albert | 7664901 | 2015-02-24 15:51:19 -0800 | [diff] [blame] | 20 | #include <limits.h> |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 21 | #include <stdio.h> |
| 22 | #include <stdlib.h> |
| 23 | #include <string.h> |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 24 | #include <sys/stat.h> |
| 25 | #include <sys/time.h> |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 26 | #include <sys/types.h> |
Dan Albert | 7664901 | 2015-02-24 15:51:19 -0800 | [diff] [blame] | 27 | #include <time.h> |
Elliott Hughes | ae5a6c0 | 2015-09-27 12:55:37 -0700 | [diff] [blame] | 28 | #include <unistd.h> |
Greg Hackmann | 7a5e2bd | 2014-05-06 08:48:18 -0700 | [diff] [blame] | 29 | #include <utime.h> |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 30 | |
Elliott Hughes | a925dba | 2015-08-24 14:49:43 -0700 | [diff] [blame] | 31 | #include <memory> |
Elliott Hughes | 6d92997 | 2015-10-27 13:40:35 -0700 | [diff] [blame] | 32 | #include <vector> |
Elliott Hughes | a925dba | 2015-08-24 14:49:43 -0700 | [diff] [blame] | 33 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 34 | #include "sysdeps.h" |
Dan Albert | 7664901 | 2015-02-24 15:51:19 -0800 | [diff] [blame] | 35 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 36 | #include "adb.h" |
| 37 | #include "adb_client.h" |
Dan Albert | cc731cc | 2015-02-24 21:26:58 -0800 | [diff] [blame] | 38 | #include "adb_io.h" |
Alex Vallée | 1421614 | 2015-05-06 17:22:25 -0400 | [diff] [blame] | 39 | #include "adb_utils.h" |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 40 | #include "file_sync_service.h" |
Elliott Hughes | b708d16 | 2015-10-27 16:03:15 -0700 | [diff] [blame] | 41 | #include "line_printer.h" |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 42 | |
Elliott Hughes | ae5a6c0 | 2015-09-27 12:55:37 -0700 | [diff] [blame] | 43 | #include <base/file.h> |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 44 | #include <base/strings.h> |
Elliott Hughes | 5c74270 | 2015-07-30 17:42:01 -0700 | [diff] [blame] | 45 | #include <base/stringprintf.h> |
| 46 | |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 47 | struct syncsendbuf { |
| 48 | unsigned id; |
| 49 | unsigned size; |
| 50 | char data[SYNC_DATA_MAX]; |
| 51 | }; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 52 | |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 53 | class SyncConnection { |
| 54 | public: |
Elliott Hughes | b708d16 | 2015-10-27 16:03:15 -0700 | [diff] [blame] | 55 | SyncConnection() : total_bytes(0), start_time_ms_(CurrentTimeMs()) { |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 56 | max = SYNC_DATA_MAX; // TODO: decide at runtime. |
| 57 | |
| 58 | std::string error; |
| 59 | fd = adb_connect("sync:", &error); |
| 60 | if (fd < 0) { |
Elliott Hughes | b708d16 | 2015-10-27 16:03:15 -0700 | [diff] [blame] | 61 | Error("connect failed: %s", error.c_str()); |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 62 | } |
| 63 | } |
| 64 | |
| 65 | ~SyncConnection() { |
| 66 | if (!IsValid()) return; |
| 67 | |
Spencer Low | 351ecd1 | 2015-10-14 17:32:44 -0700 | [diff] [blame] | 68 | 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 Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 77 | adb_close(fd); |
| 78 | } |
| 79 | |
| 80 | bool IsValid() { return fd >= 0; } |
| 81 | |
Elliott Hughes | ae5a6c0 | 2015-09-27 12:55:37 -0700 | [diff] [blame] | 82 | bool SendRequest(int id, const char* path_and_mode) { |
| 83 | size_t path_length = strlen(path_and_mode); |
| 84 | if (path_length > 1024) { |
Elliott Hughes | b708d16 | 2015-10-27 16:03:15 -0700 | [diff] [blame] | 85 | Error("SendRequest failed: path too long: %zu", path_length); |
Elliott Hughes | ae5a6c0 | 2015-09-27 12:55:37 -0700 | [diff] [blame] | 86 | 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 Hughes | 6d92997 | 2015-10-27 13:40:35 -0700 | [diff] [blame] | 92 | std::vector<char> buf(sizeof(SyncRequest) + path_length); |
| 93 | SyncRequest* req = reinterpret_cast<SyncRequest*>(&buf[0]); |
Elliott Hughes | ae5a6c0 | 2015-09-27 12:55:37 -0700 | [diff] [blame] | 94 | 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 Hughes | 6d92997 | 2015-10-27 13:40:35 -0700 | [diff] [blame] | 99 | return WriteFdExactly(fd, &buf[0], buf.size()); |
Elliott Hughes | ae5a6c0 | 2015-09-27 12:55:37 -0700 | [diff] [blame] | 100 | } |
| 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 Hughes | b708d16 | 2015-10-27 16:03:15 -0700 | [diff] [blame] | 105 | const char* rpath, |
Elliott Hughes | ae5a6c0 | 2015-09-27 12:55:37 -0700 | [diff] [blame] | 106 | const char* data, size_t data_length, |
| 107 | unsigned mtime) { |
Elliott Hughes | b708d16 | 2015-10-27 16:03:15 -0700 | [diff] [blame] | 108 | Print(rpath); |
| 109 | |
Elliott Hughes | ae5a6c0 | 2015-09-27 12:55:37 -0700 | [diff] [blame] | 110 | size_t path_length = strlen(path_and_mode); |
| 111 | if (path_length > 1024) { |
Elliott Hughes | b708d16 | 2015-10-27 16:03:15 -0700 | [diff] [blame] | 112 | Error("SendSmallFile failed: path too long: %zu", path_length); |
Elliott Hughes | ae5a6c0 | 2015-09-27 12:55:37 -0700 | [diff] [blame] | 113 | errno = ENAMETOOLONG; |
| 114 | return false; |
| 115 | } |
| 116 | |
Elliott Hughes | 6d92997 | 2015-10-27 13:40:35 -0700 | [diff] [blame] | 117 | std::vector<char> buf(sizeof(SyncRequest) + path_length + |
Elliott Hughes | ae5a6c0 | 2015-09-27 12:55:37 -0700 | [diff] [blame] | 118 | sizeof(SyncRequest) + data_length + |
Elliott Hughes | 6d92997 | 2015-10-27 13:40:35 -0700 | [diff] [blame] | 119 | sizeof(SyncRequest)); |
| 120 | char* p = &buf[0]; |
Elliott Hughes | ae5a6c0 | 2015-09-27 12:55:37 -0700 | [diff] [blame] | 121 | |
| 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 Hughes | 6d92997 | 2015-10-27 13:40:35 -0700 | [diff] [blame] | 141 | if (!WriteFdExactly(fd, &buf[0], (p - &buf[0]))) return false; |
Elliott Hughes | ae5a6c0 | 2015-09-27 12:55:37 -0700 | [diff] [blame] | 142 | |
| 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 Hughes | b708d16 | 2015-10-27 16:03:15 -0700 | [diff] [blame] | 150 | Error("failed to copy '%s' to '%s': no ID_DONE: %s", from, to, strerror(errno)); |
Elliott Hughes | ae5a6c0 | 2015-09-27 12:55:37 -0700 | [diff] [blame] | 151 | return false; |
| 152 | } |
| 153 | if (msg.status.id == ID_OKAY) { |
| 154 | return true; |
| 155 | } |
| 156 | if (msg.status.id != ID_FAIL) { |
Elliott Hughes | b708d16 | 2015-10-27 16:03:15 -0700 | [diff] [blame] | 157 | Error("failed to copy '%s' to '%s': unknown reason %d", from, to, msg.status.id); |
Elliott Hughes | ae5a6c0 | 2015-09-27 12:55:37 -0700 | [diff] [blame] | 158 | return false; |
| 159 | } |
Elliott Hughes | 8b43c3e | 2015-10-23 21:06:11 -0700 | [diff] [blame] | 160 | return ReportCopyFailure(from, to, msg); |
| 161 | } |
| 162 | |
| 163 | bool ReportCopyFailure(const char* from, const char* to, const syncmsg& msg) { |
Elliott Hughes | 6d92997 | 2015-10-27 13:40:35 -0700 | [diff] [blame] | 164 | std::vector<char> buf(msg.status.msglen + 1); |
| 165 | if (!ReadFdExactly(fd, &buf[0], msg.status.msglen)) { |
Elliott Hughes | b708d16 | 2015-10-27 16:03:15 -0700 | [diff] [blame] | 166 | Error("failed to copy '%s' to '%s'; failed to read reason (!): %s", |
| 167 | from, to, strerror(errno)); |
Elliott Hughes | ae5a6c0 | 2015-09-27 12:55:37 -0700 | [diff] [blame] | 168 | return false; |
| 169 | } |
Elliott Hughes | 6d92997 | 2015-10-27 13:40:35 -0700 | [diff] [blame] | 170 | buf[msg.status.msglen] = 0; |
Elliott Hughes | b708d16 | 2015-10-27 16:03:15 -0700 | [diff] [blame] | 171 | Error("failed to copy '%s' to '%s': %s", from, to, &buf[0]); |
Elliott Hughes | ae5a6c0 | 2015-09-27 12:55:37 -0700 | [diff] [blame] | 172 | return false; |
| 173 | } |
| 174 | |
Elliott Hughes | b708d16 | 2015-10-27 16:03:15 -0700 | [diff] [blame] | 175 | 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 | |
Josh Gao | 983c41c | 2015-11-02 17:15:57 -0800 | [diff] [blame^] | 190 | void Printf(const char* fmt, ...) __attribute__((__format__(ADB_FORMAT_ARCHETYPE, 2, 3))) { |
| 191 | std::string s; |
| 192 | va_list ap; |
| 193 | va_start(ap, fmt); |
| 194 | android::base::StringAppendV(&s, fmt, ap); |
| 195 | va_end(ap); |
| 196 | |
| 197 | Print(s); |
| 198 | } |
| 199 | |
Elliott Hughes | b708d16 | 2015-10-27 16:03:15 -0700 | [diff] [blame] | 200 | void Error(const char* fmt, ...) __attribute__((__format__(ADB_FORMAT_ARCHETYPE, 2, 3))) { |
| 201 | std::string s = "adb: error: "; |
| 202 | |
| 203 | va_list ap; |
| 204 | va_start(ap, fmt); |
| 205 | android::base::StringAppendV(&s, fmt, ap); |
| 206 | va_end(ap); |
| 207 | |
| 208 | line_printer_.Print(s, LinePrinter::FULL); |
| 209 | } |
| 210 | |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 211 | uint64_t total_bytes; |
| 212 | |
| 213 | // TODO: add a char[max] buffer here, to replace syncsendbuf... |
| 214 | int fd; |
| 215 | size_t max; |
| 216 | |
| 217 | private: |
Elliott Hughes | b708d16 | 2015-10-27 16:03:15 -0700 | [diff] [blame] | 218 | uint64_t start_time_ms_; |
| 219 | |
| 220 | LinePrinter line_printer_; |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 221 | |
Spencer Low | 351ecd1 | 2015-10-14 17:32:44 -0700 | [diff] [blame] | 222 | bool SendQuit() { |
| 223 | return SendRequest(ID_QUIT, ""); // TODO: add a SendResponse? |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 224 | } |
| 225 | |
Elliott Hughes | b708d16 | 2015-10-27 16:03:15 -0700 | [diff] [blame] | 226 | static uint64_t CurrentTimeMs() { |
| 227 | struct timeval tv; |
| 228 | gettimeofday(&tv, 0); // (Not clock_gettime because of Mac/Windows.) |
| 229 | return static_cast<uint64_t>(tv.tv_sec) * 1000 + tv.tv_usec / 1000; |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 230 | } |
| 231 | }; |
| 232 | |
| 233 | typedef void (*sync_ls_cb)(unsigned mode, unsigned size, unsigned time, const char* name, void* cookie); |
| 234 | |
Elliott Hughes | ae5a6c0 | 2015-09-27 12:55:37 -0700 | [diff] [blame] | 235 | static bool sync_ls(SyncConnection& sc, const char* path, sync_ls_cb func, void* cookie) { |
| 236 | if (!sc.SendRequest(ID_LIST, path)) return false; |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 237 | |
| 238 | while (true) { |
| 239 | syncmsg msg; |
Elliott Hughes | ae5a6c0 | 2015-09-27 12:55:37 -0700 | [diff] [blame] | 240 | if (!ReadFdExactly(sc.fd, &msg.dent, sizeof(msg.dent))) return false; |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 241 | |
| 242 | if (msg.dent.id == ID_DONE) return true; |
| 243 | if (msg.dent.id != ID_DENT) return false; |
| 244 | |
Elliott Hughes | f446520 | 2015-08-24 14:27:03 -0700 | [diff] [blame] | 245 | size_t len = msg.dent.namelen; |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 246 | if (len > 256) return false; // TODO: resize buffer? continue? |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 247 | |
Elliott Hughes | 5c74270 | 2015-07-30 17:42:01 -0700 | [diff] [blame] | 248 | char buf[257]; |
Elliott Hughes | ae5a6c0 | 2015-09-27 12:55:37 -0700 | [diff] [blame] | 249 | if (!ReadFdExactly(sc.fd, buf, len)) return false; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 250 | buf[len] = 0; |
| 251 | |
Elliott Hughes | f446520 | 2015-08-24 14:27:03 -0700 | [diff] [blame] | 252 | func(msg.dent.mode, msg.dent.size, msg.dent.time, buf, cookie); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 253 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 254 | } |
| 255 | |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 256 | static bool sync_finish_stat(SyncConnection& sc, unsigned int* timestamp, |
| 257 | unsigned int* mode, unsigned int* size) { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 258 | syncmsg msg; |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 259 | if (!ReadFdExactly(sc.fd, &msg.stat, sizeof(msg.stat)) || msg.stat.id != ID_STAT) { |
| 260 | return false; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 261 | } |
| 262 | |
Elliott Hughes | f446520 | 2015-08-24 14:27:03 -0700 | [diff] [blame] | 263 | if (timestamp) *timestamp = msg.stat.time; |
| 264 | if (mode) *mode = msg.stat.mode; |
| 265 | if (size) *size = msg.stat.size; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 266 | |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 267 | return true; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 268 | } |
| 269 | |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 270 | static bool sync_stat(SyncConnection& sc, const char* path, |
| 271 | unsigned int* timestamp, unsigned int* mode, unsigned int* size) { |
Elliott Hughes | ae5a6c0 | 2015-09-27 12:55:37 -0700 | [diff] [blame] | 272 | return sc.SendRequest(ID_STAT, path) && sync_finish_stat(sc, timestamp, mode, size); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 273 | } |
| 274 | |
Elliott Hughes | b708d16 | 2015-10-27 16:03:15 -0700 | [diff] [blame] | 275 | static bool SendLargeFile(SyncConnection& sc, const char* path_and_mode, |
| 276 | const char* lpath, const char* rpath, |
| 277 | unsigned mtime) { |
Elliott Hughes | ae5a6c0 | 2015-09-27 12:55:37 -0700 | [diff] [blame] | 278 | if (!sc.SendRequest(ID_SEND, path_and_mode)) { |
Elliott Hughes | b708d16 | 2015-10-27 16:03:15 -0700 | [diff] [blame] | 279 | sc.Error("failed to send ID_SEND message '%s': %s", path_and_mode, strerror(errno)); |
Elliott Hughes | ae5a6c0 | 2015-09-27 12:55:37 -0700 | [diff] [blame] | 280 | return false; |
| 281 | } |
| 282 | |
Elliott Hughes | b708d16 | 2015-10-27 16:03:15 -0700 | [diff] [blame] | 283 | struct stat st; |
| 284 | if (stat(lpath, &st) == -1) { |
| 285 | sc.Error("cannot stat '%s': %s", lpath, strerror(errno)); |
| 286 | return false; |
Spencer Low | d8cce18 | 2015-08-28 01:07:30 -0700 | [diff] [blame] | 287 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 288 | |
Elliott Hughes | b708d16 | 2015-10-27 16:03:15 -0700 | [diff] [blame] | 289 | uint64_t total_size = st.st_size; |
| 290 | uint64_t bytes_copied = 0; |
| 291 | |
| 292 | int lfd = adb_open(lpath, O_RDONLY); |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 293 | if (lfd < 0) { |
Elliott Hughes | b708d16 | 2015-10-27 16:03:15 -0700 | [diff] [blame] | 294 | sc.Error("cannot open '%s': %s", lpath, strerror(errno)); |
Spencer Low | d8cce18 | 2015-08-28 01:07:30 -0700 | [diff] [blame] | 295 | return false; |
Mark Lindner | 76f2a93 | 2014-03-11 17:55:59 -0700 | [diff] [blame] | 296 | } |
| 297 | |
Elliott Hughes | ae5a6c0 | 2015-09-27 12:55:37 -0700 | [diff] [blame] | 298 | syncsendbuf sbuf; |
| 299 | sbuf.id = ID_DATA; |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 300 | while (true) { |
Elliott Hughes | ae5a6c0 | 2015-09-27 12:55:37 -0700 | [diff] [blame] | 301 | int ret = adb_read(lfd, sbuf.data, sc.max); |
Elliott Hughes | 8fcd8bc | 2015-08-25 10:59:45 -0700 | [diff] [blame] | 302 | if (ret <= 0) { |
Spencer Low | d8cce18 | 2015-08-28 01:07:30 -0700 | [diff] [blame] | 303 | if (ret < 0) { |
Elliott Hughes | b708d16 | 2015-10-27 16:03:15 -0700 | [diff] [blame] | 304 | sc.Error("cannot read '%s': %s", lpath, strerror(errno)); |
Spencer Low | d8cce18 | 2015-08-28 01:07:30 -0700 | [diff] [blame] | 305 | adb_close(lfd); |
| 306 | return false; |
| 307 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 308 | break; |
| 309 | } |
| 310 | |
Elliott Hughes | ae5a6c0 | 2015-09-27 12:55:37 -0700 | [diff] [blame] | 311 | sbuf.size = ret; |
| 312 | if (!WriteFdExactly(sc.fd, &sbuf, sizeof(unsigned) * 2 + ret)) { |
Spencer Low | d8cce18 | 2015-08-28 01:07:30 -0700 | [diff] [blame] | 313 | adb_close(lfd); |
| 314 | return false; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 315 | } |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 316 | sc.total_bytes += ret; |
Mark Lindner | 76f2a93 | 2014-03-11 17:55:59 -0700 | [diff] [blame] | 317 | |
Elliott Hughes | b708d16 | 2015-10-27 16:03:15 -0700 | [diff] [blame] | 318 | bytes_copied += ret; |
| 319 | |
| 320 | int percentage = static_cast<int>(bytes_copied * 100 / total_size); |
Josh Gao | 983c41c | 2015-11-02 17:15:57 -0800 | [diff] [blame^] | 321 | sc.Printf("%s: %d%%", rpath, percentage); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 322 | } |
| 323 | |
| 324 | adb_close(lfd); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 325 | |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 326 | syncmsg msg; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 327 | msg.data.id = ID_DONE; |
Elliott Hughes | f446520 | 2015-08-24 14:27:03 -0700 | [diff] [blame] | 328 | msg.data.size = mtime; |
Elliott Hughes | 081696d | 2015-09-03 11:06:00 -0700 | [diff] [blame] | 329 | if (!WriteFdExactly(sc.fd, &msg.data, sizeof(msg.data))) { |
Elliott Hughes | b708d16 | 2015-10-27 16:03:15 -0700 | [diff] [blame] | 330 | sc.Error("failed to send ID_DONE message for '%s': %s", rpath, strerror(errno)); |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 331 | return false; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 332 | } |
| 333 | |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 334 | return true; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 335 | } |
| 336 | |
Elliott Hughes | ae5a6c0 | 2015-09-27 12:55:37 -0700 | [diff] [blame] | 337 | static bool sync_send(SyncConnection& sc, const char* lpath, const char* rpath, |
Elliott Hughes | b708d16 | 2015-10-27 16:03:15 -0700 | [diff] [blame] | 338 | unsigned mtime, mode_t mode) |
Elliott Hughes | ae5a6c0 | 2015-09-27 12:55:37 -0700 | [diff] [blame] | 339 | { |
| 340 | std::string path_and_mode = android::base::StringPrintf("%s,%d", rpath, mode); |
| 341 | |
| 342 | if (S_ISLNK(mode)) { |
| 343 | #if !defined(_WIN32) |
| 344 | char buf[PATH_MAX]; |
| 345 | ssize_t data_length = readlink(lpath, buf, PATH_MAX - 1); |
| 346 | if (data_length == -1) { |
Elliott Hughes | b708d16 | 2015-10-27 16:03:15 -0700 | [diff] [blame] | 347 | sc.Error("readlink '%s' failed: %s", lpath, strerror(errno)); |
Elliott Hughes | ae5a6c0 | 2015-09-27 12:55:37 -0700 | [diff] [blame] | 348 | return false; |
| 349 | } |
| 350 | buf[data_length++] = '\0'; |
| 351 | |
Elliott Hughes | b708d16 | 2015-10-27 16:03:15 -0700 | [diff] [blame] | 352 | if (!sc.SendSmallFile(path_and_mode.c_str(), rpath, buf, data_length, mtime)) return false; |
Elliott Hughes | ae5a6c0 | 2015-09-27 12:55:37 -0700 | [diff] [blame] | 353 | return sc.CopyDone(lpath, rpath); |
| 354 | #endif |
| 355 | } |
| 356 | |
| 357 | if (!S_ISREG(mode)) { |
Elliott Hughes | b708d16 | 2015-10-27 16:03:15 -0700 | [diff] [blame] | 358 | sc.Error("local file '%s' has unsupported mode: 0o%o", lpath, mode); |
Elliott Hughes | ae5a6c0 | 2015-09-27 12:55:37 -0700 | [diff] [blame] | 359 | return false; |
| 360 | } |
| 361 | |
| 362 | struct stat st; |
| 363 | if (stat(lpath, &st) == -1) { |
Elliott Hughes | b708d16 | 2015-10-27 16:03:15 -0700 | [diff] [blame] | 364 | sc.Error("failed to stat local file '%s': %s", lpath, strerror(errno)); |
Elliott Hughes | ae5a6c0 | 2015-09-27 12:55:37 -0700 | [diff] [blame] | 365 | return false; |
| 366 | } |
| 367 | if (st.st_size < SYNC_DATA_MAX) { |
| 368 | std::string data; |
| 369 | if (!android::base::ReadFileToString(lpath, &data)) { |
Elliott Hughes | b708d16 | 2015-10-27 16:03:15 -0700 | [diff] [blame] | 370 | sc.Error("failed to read all of '%s': %s", lpath, strerror(errno)); |
Elliott Hughes | ae5a6c0 | 2015-09-27 12:55:37 -0700 | [diff] [blame] | 371 | return false; |
| 372 | } |
Elliott Hughes | b708d16 | 2015-10-27 16:03:15 -0700 | [diff] [blame] | 373 | if (!sc.SendSmallFile(path_and_mode.c_str(), rpath, data.data(), data.size(), mtime)) { |
| 374 | return false; |
| 375 | } |
Elliott Hughes | ae5a6c0 | 2015-09-27 12:55:37 -0700 | [diff] [blame] | 376 | } else { |
Elliott Hughes | b708d16 | 2015-10-27 16:03:15 -0700 | [diff] [blame] | 377 | if (!SendLargeFile(sc, path_and_mode.c_str(), lpath, rpath, mtime)) { |
| 378 | return false; |
| 379 | } |
Elliott Hughes | ae5a6c0 | 2015-09-27 12:55:37 -0700 | [diff] [blame] | 380 | } |
| 381 | return sc.CopyDone(lpath, rpath); |
| 382 | } |
| 383 | |
Elliott Hughes | b708d16 | 2015-10-27 16:03:15 -0700 | [diff] [blame] | 384 | static bool sync_recv(SyncConnection& sc, const char* rpath, const char* lpath) { |
| 385 | sc.Print(rpath); |
| 386 | |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 387 | unsigned size = 0; |
Elliott Hughes | b708d16 | 2015-10-27 16:03:15 -0700 | [diff] [blame] | 388 | if (!sync_stat(sc, rpath, nullptr, nullptr, &size)) return false; |
Mark Lindner | 76f2a93 | 2014-03-11 17:55:59 -0700 | [diff] [blame] | 389 | |
Elliott Hughes | ae5a6c0 | 2015-09-27 12:55:37 -0700 | [diff] [blame] | 390 | if (!sc.SendRequest(ID_RECV, rpath)) return false; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 391 | |
Elliott Hughes | 8b43c3e | 2015-10-23 21:06:11 -0700 | [diff] [blame] | 392 | adb_unlink(lpath); |
| 393 | mkdirs(lpath); |
| 394 | int lfd = adb_creat(lpath, 0644); |
| 395 | if (lfd < 0) { |
Elliott Hughes | b708d16 | 2015-10-27 16:03:15 -0700 | [diff] [blame] | 396 | sc.Error("cannot create '%s': %s", lpath, strerror(errno)); |
Elliott Hughes | 8b43c3e | 2015-10-23 21:06:11 -0700 | [diff] [blame] | 397 | return false; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 398 | } |
| 399 | |
Elliott Hughes | b708d16 | 2015-10-27 16:03:15 -0700 | [diff] [blame] | 400 | uint64_t bytes_copied = 0; |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 401 | while (true) { |
Elliott Hughes | 8b43c3e | 2015-10-23 21:06:11 -0700 | [diff] [blame] | 402 | syncmsg msg; |
Elliott Hughes | ae5a6c0 | 2015-09-27 12:55:37 -0700 | [diff] [blame] | 403 | if (!ReadFdExactly(sc.fd, &msg.data, sizeof(msg.data))) { |
Spencer Low | d8cce18 | 2015-08-28 01:07:30 -0700 | [diff] [blame] | 404 | adb_close(lfd); |
Elliott Hughes | 8b43c3e | 2015-10-23 21:06:11 -0700 | [diff] [blame] | 405 | adb_unlink(lpath); |
Elliott Hughes | ae5a6c0 | 2015-09-27 12:55:37 -0700 | [diff] [blame] | 406 | return false; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 407 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 408 | |
Elliott Hughes | 8b43c3e | 2015-10-23 21:06:11 -0700 | [diff] [blame] | 409 | if (msg.data.id == ID_DONE) break; |
| 410 | |
| 411 | if (msg.data.id != ID_DATA) { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 412 | adb_close(lfd); |
Elliott Hughes | 8b43c3e | 2015-10-23 21:06:11 -0700 | [diff] [blame] | 413 | adb_unlink(lpath); |
| 414 | sc.ReportCopyFailure(rpath, lpath, msg); |
Elliott Hughes | ae5a6c0 | 2015-09-27 12:55:37 -0700 | [diff] [blame] | 415 | return false; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 416 | } |
| 417 | |
Elliott Hughes | 8b43c3e | 2015-10-23 21:06:11 -0700 | [diff] [blame] | 418 | if (msg.data.size > sc.max) { |
Elliott Hughes | b708d16 | 2015-10-27 16:03:15 -0700 | [diff] [blame] | 419 | sc.Error("msg.data.size too large: %u (max %zu)", msg.data.size, sc.max); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 420 | adb_close(lfd); |
Elliott Hughes | 8b43c3e | 2015-10-23 21:06:11 -0700 | [diff] [blame] | 421 | adb_unlink(lpath); |
Elliott Hughes | ae5a6c0 | 2015-09-27 12:55:37 -0700 | [diff] [blame] | 422 | return false; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 423 | } |
| 424 | |
Elliott Hughes | 8b43c3e | 2015-10-23 21:06:11 -0700 | [diff] [blame] | 425 | char buffer[SYNC_DATA_MAX]; |
| 426 | if (!ReadFdExactly(sc.fd, buffer, msg.data.size)) { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 427 | adb_close(lfd); |
Elliott Hughes | 8b43c3e | 2015-10-23 21:06:11 -0700 | [diff] [blame] | 428 | adb_unlink(lpath); |
Elliott Hughes | ae5a6c0 | 2015-09-27 12:55:37 -0700 | [diff] [blame] | 429 | return false; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 430 | } |
| 431 | |
Elliott Hughes | 8b43c3e | 2015-10-23 21:06:11 -0700 | [diff] [blame] | 432 | if (!WriteFdExactly(lfd, buffer, msg.data.size)) { |
Elliott Hughes | b708d16 | 2015-10-27 16:03:15 -0700 | [diff] [blame] | 433 | sc.Error("cannot write '%s': %s", lpath, strerror(errno)); |
Elliott Hughes | 8b43c3e | 2015-10-23 21:06:11 -0700 | [diff] [blame] | 434 | adb_close(lfd); |
| 435 | adb_unlink(lpath); |
| 436 | return false; |
| 437 | } |
| 438 | |
| 439 | sc.total_bytes += msg.data.size; |
Mark Lindner | 76f2a93 | 2014-03-11 17:55:59 -0700 | [diff] [blame] | 440 | |
Elliott Hughes | b708d16 | 2015-10-27 16:03:15 -0700 | [diff] [blame] | 441 | bytes_copied += msg.data.size; |
| 442 | |
| 443 | int percentage = static_cast<int>(bytes_copied * 100 / size); |
Josh Gao | 983c41c | 2015-11-02 17:15:57 -0800 | [diff] [blame^] | 444 | sc.Printf("%s: %d%%", rpath, percentage); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 445 | } |
| 446 | |
| 447 | adb_close(lfd); |
Elliott Hughes | ae5a6c0 | 2015-09-27 12:55:37 -0700 | [diff] [blame] | 448 | return true; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 449 | } |
| 450 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 451 | static void do_sync_ls_cb(unsigned mode, unsigned size, unsigned time, |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 452 | const char* name, void* /*cookie*/) { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 453 | printf("%08x %08x %08x %s\n", mode, size, time, name); |
| 454 | } |
| 455 | |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 456 | bool do_sync_ls(const char* path) { |
| 457 | SyncConnection sc; |
| 458 | if (!sc.IsValid()) return false; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 459 | |
Elliott Hughes | ae5a6c0 | 2015-09-27 12:55:37 -0700 | [diff] [blame] | 460 | return sync_ls(sc, path, do_sync_ls_cb, 0); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 461 | } |
| 462 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 463 | struct copyinfo |
| 464 | { |
| 465 | copyinfo *next; |
| 466 | const char *src; |
| 467 | const char *dst; |
| 468 | unsigned int time; |
| 469 | unsigned int mode; |
Elliott Hughes | ae5a6c0 | 2015-09-27 12:55:37 -0700 | [diff] [blame] | 470 | uint64_t size; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 471 | int flag; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 472 | }; |
| 473 | |
Elliott Hughes | 3edd54b | 2015-05-05 18:26:10 -0700 | [diff] [blame] | 474 | static copyinfo* mkcopyinfo(const char* spath, const char* dpath, const char* name, int isdir) { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 475 | int slen = strlen(spath); |
| 476 | int dlen = strlen(dpath); |
| 477 | int nlen = strlen(name); |
| 478 | int ssize = slen + nlen + 2; |
| 479 | int dsize = dlen + nlen + 2; |
| 480 | |
Elliott Hughes | 3edd54b | 2015-05-05 18:26:10 -0700 | [diff] [blame] | 481 | copyinfo *ci = reinterpret_cast<copyinfo*>(malloc(sizeof(copyinfo) + ssize + dsize)); |
Elliott Hughes | b708d16 | 2015-10-27 16:03:15 -0700 | [diff] [blame] | 482 | if (ci == 0) { |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 483 | fprintf(stderr, "out of memory\n"); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 484 | abort(); |
| 485 | } |
| 486 | |
| 487 | ci->next = 0; |
| 488 | ci->time = 0; |
| 489 | ci->mode = 0; |
| 490 | ci->size = 0; |
| 491 | ci->flag = 0; |
| 492 | ci->src = (const char*)(ci + 1); |
| 493 | ci->dst = ci->src + ssize; |
| 494 | snprintf((char*) ci->src, ssize, isdir ? "%s%s/" : "%s%s", spath, name); |
| 495 | snprintf((char*) ci->dst, dsize, isdir ? "%s%s/" : "%s%s", dpath, name); |
| 496 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 497 | return ci; |
| 498 | } |
| 499 | |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 500 | static bool IsDotOrDotDot(const char* name) { |
| 501 | return name[0] == '.' && (name[1] == '\0' || (name[1] == '.' && name[2] == '\0')); |
| 502 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 503 | |
Elliott Hughes | b708d16 | 2015-10-27 16:03:15 -0700 | [diff] [blame] | 504 | static int local_build_list(SyncConnection& sc, |
| 505 | copyinfo** filelist, const char* lpath, const char* rpath) { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 506 | copyinfo *dirlist = 0; |
| 507 | copyinfo *ci, *next; |
| 508 | |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 509 | std::unique_ptr<DIR, int(*)(DIR*)> dir(opendir(lpath), closedir); |
| 510 | if (!dir) { |
Elliott Hughes | b708d16 | 2015-10-27 16:03:15 -0700 | [diff] [blame] | 511 | sc.Error("cannot open '%s': %s", lpath, strerror(errno)); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 512 | return -1; |
| 513 | } |
| 514 | |
Elliott Hughes | b708d16 | 2015-10-27 16:03:15 -0700 | [diff] [blame] | 515 | dirent* de; |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 516 | while ((de = readdir(dir.get()))) { |
| 517 | if (IsDotOrDotDot(de->d_name)) continue; |
| 518 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 519 | char stat_path[PATH_MAX]; |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 520 | if (strlen(lpath) + strlen(de->d_name) + 1 > sizeof(stat_path)) { |
Elliott Hughes | b708d16 | 2015-10-27 16:03:15 -0700 | [diff] [blame] | 521 | sc.Error("skipping long path '%s%s'", lpath, de->d_name); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 522 | continue; |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 523 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 524 | strcpy(stat_path, lpath); |
| 525 | strcat(stat_path, de->d_name); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 526 | |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 527 | struct stat st; |
| 528 | if (!lstat(stat_path, &st)) { |
Daniel Rosenberg | 686bce6 | 2014-06-30 20:29:40 -0700 | [diff] [blame] | 529 | if (S_ISDIR(st.st_mode)) { |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 530 | ci = mkcopyinfo(lpath, rpath, de->d_name, 1); |
Daniel Rosenberg | 686bce6 | 2014-06-30 20:29:40 -0700 | [diff] [blame] | 531 | ci->next = dirlist; |
| 532 | dirlist = ci; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 533 | } else { |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 534 | ci = mkcopyinfo(lpath, rpath, de->d_name, 0); |
| 535 | if (!S_ISREG(st.st_mode) && !S_ISLNK(st.st_mode)) { |
Elliott Hughes | b708d16 | 2015-10-27 16:03:15 -0700 | [diff] [blame] | 536 | sc.Error("skipping special file '%s'", ci->src); |
Daniel Rosenberg | 686bce6 | 2014-06-30 20:29:40 -0700 | [diff] [blame] | 537 | free(ci); |
| 538 | } else { |
| 539 | ci->time = st.st_mtime; |
| 540 | ci->mode = st.st_mode; |
| 541 | ci->size = st.st_size; |
| 542 | ci->next = *filelist; |
| 543 | *filelist = ci; |
| 544 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 545 | } |
Daniel Rosenberg | 686bce6 | 2014-06-30 20:29:40 -0700 | [diff] [blame] | 546 | } else { |
Elliott Hughes | b708d16 | 2015-10-27 16:03:15 -0700 | [diff] [blame] | 547 | sc.Error("cannot lstat '%s': %s",stat_path , strerror(errno)); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 548 | } |
| 549 | } |
| 550 | |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 551 | // Close this directory and recurse. |
| 552 | dir.reset(); |
| 553 | for (ci = dirlist; ci != 0; ci = next) { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 554 | next = ci->next; |
Elliott Hughes | b708d16 | 2015-10-27 16:03:15 -0700 | [diff] [blame] | 555 | local_build_list(sc, filelist, ci->src, ci->dst); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 556 | free(ci); |
| 557 | } |
| 558 | |
| 559 | return 0; |
| 560 | } |
| 561 | |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 562 | static bool copy_local_dir_remote(SyncConnection& sc, const char* lpath, const char* rpath, |
| 563 | bool check_timestamps, bool list_only) { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 564 | copyinfo *filelist = 0; |
| 565 | copyinfo *ci, *next; |
| 566 | int pushed = 0; |
| 567 | int skipped = 0; |
| 568 | |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 569 | if ((lpath[0] == 0) || (rpath[0] == 0)) return false; |
| 570 | if (lpath[strlen(lpath) - 1] != '/') { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 571 | int tmplen = strlen(lpath)+2; |
Dan Albert | bac3474 | 2015-02-25 17:51:28 -0800 | [diff] [blame] | 572 | char *tmp = reinterpret_cast<char*>(malloc(tmplen)); |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 573 | if(tmp == 0) return false; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 574 | snprintf(tmp, tmplen, "%s/",lpath); |
| 575 | lpath = tmp; |
| 576 | } |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 577 | if (rpath[strlen(rpath) - 1] != '/') { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 578 | int tmplen = strlen(rpath)+2; |
Dan Albert | bac3474 | 2015-02-25 17:51:28 -0800 | [diff] [blame] | 579 | char *tmp = reinterpret_cast<char*>(malloc(tmplen)); |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 580 | if(tmp == 0) return false; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 581 | snprintf(tmp, tmplen, "%s/",rpath); |
| 582 | rpath = tmp; |
| 583 | } |
| 584 | |
Elliott Hughes | b708d16 | 2015-10-27 16:03:15 -0700 | [diff] [blame] | 585 | if (local_build_list(sc, &filelist, lpath, rpath)) { |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 586 | return false; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 587 | } |
| 588 | |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 589 | if (check_timestamps) { |
| 590 | for (ci = filelist; ci != 0; ci = ci->next) { |
Elliott Hughes | ae5a6c0 | 2015-09-27 12:55:37 -0700 | [diff] [blame] | 591 | if (!sc.SendRequest(ID_STAT, ci->dst)) return false; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 592 | } |
| 593 | for(ci = filelist; ci != 0; ci = ci->next) { |
| 594 | unsigned int timestamp, mode, size; |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 595 | if (!sync_finish_stat(sc, ×tamp, &mode, &size)) return false; |
| 596 | if (size == ci->size) { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 597 | /* for links, we cannot update the atime/mtime */ |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 598 | if ((S_ISREG(ci->mode & mode) && timestamp == ci->time) || |
| 599 | (S_ISLNK(ci->mode & mode) && timestamp >= ci->time)) { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 600 | ci->flag = 1; |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 601 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 602 | } |
| 603 | } |
| 604 | } |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 605 | for (ci = filelist; ci != 0; ci = next) { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 606 | next = ci->next; |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 607 | if (ci->flag == 0) { |
Elliott Hughes | b708d16 | 2015-10-27 16:03:15 -0700 | [diff] [blame] | 608 | if (list_only) { |
| 609 | fprintf(stderr, "would push: %s -> %s\n", ci->src, ci->dst); |
| 610 | } else { |
| 611 | if (!sync_send(sc, ci->src, ci->dst, ci->time, ci->mode)) { |
| 612 | return false; |
| 613 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 614 | } |
| 615 | pushed++; |
| 616 | } else { |
| 617 | skipped++; |
| 618 | } |
| 619 | free(ci); |
| 620 | } |
| 621 | |
Josh Gao | 983c41c | 2015-11-02 17:15:57 -0800 | [diff] [blame^] | 622 | sc.Printf("%s: %d file%s pushed. %d file%s skipped.%s\n", rpath, pushed, |
| 623 | (pushed == 1) ? "" : "s", skipped, (skipped == 1) ? "" : "s", |
| 624 | sc.TransferRate().c_str()); |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 625 | return true; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 626 | } |
| 627 | |
Elliott Hughes | b708d16 | 2015-10-27 16:03:15 -0700 | [diff] [blame] | 628 | bool do_sync_push(const char* lpath, const char* rpath) { |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 629 | SyncConnection sc; |
| 630 | if (!sc.IsValid()) return false; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 631 | |
Elliott Hughes | 5c74270 | 2015-07-30 17:42:01 -0700 | [diff] [blame] | 632 | struct stat st; |
| 633 | if (stat(lpath, &st)) { |
Elliott Hughes | b708d16 | 2015-10-27 16:03:15 -0700 | [diff] [blame] | 634 | sc.Error("cannot stat '%s': %s", lpath, strerror(errno)); |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 635 | return false; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 636 | } |
| 637 | |
Elliott Hughes | 5c74270 | 2015-07-30 17:42:01 -0700 | [diff] [blame] | 638 | if (S_ISDIR(st.st_mode)) { |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 639 | return copy_local_dir_remote(sc, lpath, rpath, false, false); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 640 | } |
| 641 | |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 642 | unsigned mode; |
| 643 | if (!sync_stat(sc, rpath, nullptr, &mode, nullptr)) return false; |
| 644 | std::string path_holder; |
| 645 | if (mode != 0 && S_ISDIR(mode)) { |
| 646 | // If we're copying a local file to a remote directory, |
| 647 | // we really want to copy to remote_dir + "/" + local_filename. |
| 648 | path_holder = android::base::StringPrintf("%s/%s", rpath, adb_basename(lpath).c_str()); |
| 649 | rpath = path_holder.c_str(); |
| 650 | } |
Elliott Hughes | b708d16 | 2015-10-27 16:03:15 -0700 | [diff] [blame] | 651 | bool result = sync_send(sc, lpath, rpath, st.st_mtime, st.st_mode); |
| 652 | sc.Print("\n"); |
| 653 | return result; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 654 | } |
| 655 | |
Elliott Hughes | 2d4121c | 2015-04-17 09:47:42 -0700 | [diff] [blame] | 656 | struct sync_ls_build_list_cb_args { |
Elliott Hughes | b708d16 | 2015-10-27 16:03:15 -0700 | [diff] [blame] | 657 | SyncConnection* sc; |
| 658 | copyinfo** filelist; |
| 659 | copyinfo** dirlist; |
| 660 | const char* rpath; |
| 661 | const char* lpath; |
Elliott Hughes | 2d4121c | 2015-04-17 09:47:42 -0700 | [diff] [blame] | 662 | }; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 663 | |
Elliott Hughes | 3edd54b | 2015-05-05 18:26:10 -0700 | [diff] [blame] | 664 | static void sync_ls_build_list_cb(unsigned mode, unsigned size, unsigned time, |
| 665 | const char* name, void* cookie) |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 666 | { |
Elliott Hughes | b708d16 | 2015-10-27 16:03:15 -0700 | [diff] [blame] | 667 | sync_ls_build_list_cb_args* args = static_cast<sync_ls_build_list_cb_args*>(cookie); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 668 | copyinfo *ci; |
| 669 | |
| 670 | if (S_ISDIR(mode)) { |
| 671 | copyinfo **dirlist = args->dirlist; |
| 672 | |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 673 | // Don't try recursing down "." or "..". |
| 674 | if (IsDotOrDotDot(name)) return; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 675 | |
| 676 | ci = mkcopyinfo(args->rpath, args->lpath, name, 1); |
| 677 | ci->next = *dirlist; |
| 678 | *dirlist = ci; |
| 679 | } else if (S_ISREG(mode) || S_ISLNK(mode)) { |
| 680 | copyinfo **filelist = args->filelist; |
| 681 | |
| 682 | ci = mkcopyinfo(args->rpath, args->lpath, name, 0); |
| 683 | ci->time = time; |
| 684 | ci->mode = mode; |
| 685 | ci->size = size; |
| 686 | ci->next = *filelist; |
| 687 | *filelist = ci; |
| 688 | } else { |
Josh Gao | 983c41c | 2015-11-02 17:15:57 -0800 | [diff] [blame^] | 689 | args->sc->Printf("skipping special file '%s'\n", name); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 690 | } |
| 691 | } |
| 692 | |
Elliott Hughes | ae5a6c0 | 2015-09-27 12:55:37 -0700 | [diff] [blame] | 693 | static bool remote_build_list(SyncConnection& sc, copyinfo **filelist, |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 694 | const char *rpath, const char *lpath) { |
Elliott Hughes | b708d16 | 2015-10-27 16:03:15 -0700 | [diff] [blame] | 695 | copyinfo* dirlist = nullptr; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 696 | |
Elliott Hughes | b708d16 | 2015-10-27 16:03:15 -0700 | [diff] [blame] | 697 | sync_ls_build_list_cb_args args; |
| 698 | args.sc = ≻ |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 699 | args.filelist = filelist; |
| 700 | args.dirlist = &dirlist; |
| 701 | args.rpath = rpath; |
| 702 | args.lpath = lpath; |
| 703 | |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 704 | // Put the files/dirs in rpath on the lists. |
Elliott Hughes | b708d16 | 2015-10-27 16:03:15 -0700 | [diff] [blame] | 705 | if (!sync_ls(sc, rpath, sync_ls_build_list_cb, &args)) { |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 706 | return false; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 707 | } |
| 708 | |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 709 | // Recurse into each directory we found. |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 710 | while (dirlist != NULL) { |
Elliott Hughes | b708d16 | 2015-10-27 16:03:15 -0700 | [diff] [blame] | 711 | copyinfo* next = dirlist->next; |
Elliott Hughes | ae5a6c0 | 2015-09-27 12:55:37 -0700 | [diff] [blame] | 712 | if (!remote_build_list(sc, filelist, dirlist->src, dirlist->dst)) { |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 713 | return false; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 714 | } |
| 715 | free(dirlist); |
| 716 | dirlist = next; |
| 717 | } |
| 718 | |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 719 | return true; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 720 | } |
| 721 | |
Dan Albert | bac3474 | 2015-02-25 17:51:28 -0800 | [diff] [blame] | 722 | static int set_time_and_mode(const char *lpath, time_t time, unsigned int mode) |
Lajos Molnar | de8ff4a | 2013-04-19 12:41:09 -0700 | [diff] [blame] | 723 | { |
Greg Hackmann | 7a5e2bd | 2014-05-06 08:48:18 -0700 | [diff] [blame] | 724 | struct utimbuf times = { time, time }; |
| 725 | int r1 = utime(lpath, ×); |
Lajos Molnar | de8ff4a | 2013-04-19 12:41:09 -0700 | [diff] [blame] | 726 | |
| 727 | /* use umask for permissions */ |
| 728 | mode_t mask=umask(0000); |
| 729 | umask(mask); |
| 730 | int r2 = chmod(lpath, mode & ~mask); |
| 731 | |
| 732 | return r1 ? : r2; |
| 733 | } |
| 734 | |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 735 | static bool copy_remote_dir_local(SyncConnection& sc, const char* rpath, const char* lpath, |
| 736 | int copy_attrs) { |
| 737 | // Make sure that both directory paths end in a slash. |
| 738 | std::string rpath_clean(rpath); |
| 739 | std::string lpath_clean(lpath); |
| 740 | if (rpath_clean.empty() || lpath_clean.empty()) return false; |
| 741 | if (rpath_clean.back() != '/') rpath_clean.push_back('/'); |
| 742 | if (lpath_clean.back() != '/') lpath_clean.push_back('/'); |
Riley Andrews | 4d04d24 | 2014-12-12 13:12:36 -0800 | [diff] [blame] | 743 | |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 744 | // Recursively build the list of files to copy. |
Elliott Hughes | b708d16 | 2015-10-27 16:03:15 -0700 | [diff] [blame] | 745 | sc.Print("pull: building file list..."); |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 746 | copyinfo* filelist = nullptr; |
Elliott Hughes | ae5a6c0 | 2015-09-27 12:55:37 -0700 | [diff] [blame] | 747 | if (!remote_build_list(sc, &filelist, rpath_clean.c_str(), lpath_clean.c_str())) return false; |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 748 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 749 | int pulled = 0; |
| 750 | int skipped = 0; |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 751 | copyinfo* ci = filelist; |
| 752 | while (ci) { |
| 753 | copyinfo* next = ci->next; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 754 | if (ci->flag == 0) { |
Josh Gao | 983c41c | 2015-11-02 17:15:57 -0800 | [diff] [blame^] | 755 | sc.Printf("pull: %s -> %s", ci->src, ci->dst); |
Elliott Hughes | b708d16 | 2015-10-27 16:03:15 -0700 | [diff] [blame] | 756 | if (!sync_recv(sc, ci->src, ci->dst)) { |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 757 | return false; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 758 | } |
Lajos Molnar | de8ff4a | 2013-04-19 12:41:09 -0700 | [diff] [blame] | 759 | |
| 760 | if (copy_attrs && set_time_and_mode(ci->dst, ci->time, ci->mode)) { |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 761 | return false; |
Lajos Molnar | de8ff4a | 2013-04-19 12:41:09 -0700 | [diff] [blame] | 762 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 763 | pulled++; |
| 764 | } else { |
| 765 | skipped++; |
| 766 | } |
| 767 | free(ci); |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 768 | ci = next; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 769 | } |
| 770 | |
Josh Gao | 983c41c | 2015-11-02 17:15:57 -0800 | [diff] [blame^] | 771 | sc.Printf("%s: %d file%s pulled. %d file%s skipped.%s\n", rpath, pulled, |
| 772 | (pulled == 1) ? "" : "s", skipped, (skipped == 1) ? "" : "s", |
| 773 | sc.TransferRate().c_str()); |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 774 | return true; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 775 | } |
| 776 | |
Elliott Hughes | b708d16 | 2015-10-27 16:03:15 -0700 | [diff] [blame] | 777 | bool do_sync_pull(const char* rpath, const char* lpath, int copy_attrs) { |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 778 | SyncConnection sc; |
| 779 | if (!sc.IsValid()) return false; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 780 | |
Elliott Hughes | 5c74270 | 2015-07-30 17:42:01 -0700 | [diff] [blame] | 781 | unsigned mode, time; |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 782 | if (!sync_stat(sc, rpath, &time, &mode, nullptr)) return false; |
Elliott Hughes | 5c74270 | 2015-07-30 17:42:01 -0700 | [diff] [blame] | 783 | if (mode == 0) { |
Elliott Hughes | b708d16 | 2015-10-27 16:03:15 -0700 | [diff] [blame] | 784 | sc.Error("remote object '%s' does not exist", rpath); |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 785 | return false; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 786 | } |
| 787 | |
Elliott Hughes | 5c74270 | 2015-07-30 17:42:01 -0700 | [diff] [blame] | 788 | if (S_ISREG(mode) || S_ISLNK(mode) || S_ISCHR(mode) || S_ISBLK(mode)) { |
| 789 | std::string path_holder; |
| 790 | struct stat st; |
| 791 | if (stat(lpath, &st) == 0) { |
| 792 | if (S_ISDIR(st.st_mode)) { |
| 793 | // If we're copying a remote file to a local directory, |
| 794 | // we really want to copy to local_dir + "/" + basename(remote). |
| 795 | path_holder = android::base::StringPrintf("%s/%s", lpath, adb_basename(rpath).c_str()); |
| 796 | lpath = path_holder.c_str(); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 797 | } |
| 798 | } |
Elliott Hughes | b708d16 | 2015-10-27 16:03:15 -0700 | [diff] [blame] | 799 | if (!sync_recv(sc, rpath, lpath)) { |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 800 | return false; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 801 | } else { |
Elliott Hughes | 5c74270 | 2015-07-30 17:42:01 -0700 | [diff] [blame] | 802 | if (copy_attrs && set_time_and_mode(lpath, time, mode)) { |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 803 | return false; |
Elliott Hughes | 5c74270 | 2015-07-30 17:42:01 -0700 | [diff] [blame] | 804 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 805 | } |
Elliott Hughes | b708d16 | 2015-10-27 16:03:15 -0700 | [diff] [blame] | 806 | sc.Print("\n"); |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 807 | return true; |
| 808 | } else if (S_ISDIR(mode)) { |
| 809 | return copy_remote_dir_local(sc, rpath, lpath, copy_attrs); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 810 | } |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 811 | |
Elliott Hughes | b708d16 | 2015-10-27 16:03:15 -0700 | [diff] [blame] | 812 | sc.Error("remote object '%s' not a file or directory", rpath); |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 813 | return false; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 814 | } |
| 815 | |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 816 | bool do_sync_sync(const std::string& lpath, const std::string& rpath, bool list_only) { |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 817 | SyncConnection sc; |
| 818 | if (!sc.IsValid()) return false; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 819 | |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 820 | return copy_local_dir_remote(sc, lpath.c_str(), rpath.c_str(), true, list_only); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 821 | } |