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 | |
Josh Gao | cda6a2b | 2015-11-02 16:45:47 -0800 | [diff] [blame] | 31 | #include <functional> |
Elliott Hughes | a925dba | 2015-08-24 14:49:43 -0700 | [diff] [blame] | 32 | #include <memory> |
Elliott Hughes | 6d92997 | 2015-10-27 13:40:35 -0700 | [diff] [blame] | 33 | #include <vector> |
Elliott Hughes | a925dba | 2015-08-24 14:49:43 -0700 | [diff] [blame] | 34 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 35 | #include "sysdeps.h" |
Dan Albert | 7664901 | 2015-02-24 15:51:19 -0800 | [diff] [blame] | 36 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 37 | #include "adb.h" |
| 38 | #include "adb_client.h" |
Dan Albert | cc731cc | 2015-02-24 21:26:58 -0800 | [diff] [blame] | 39 | #include "adb_io.h" |
Alex Vallée | 1421614 | 2015-05-06 17:22:25 -0400 | [diff] [blame] | 40 | #include "adb_utils.h" |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 41 | #include "file_sync_service.h" |
Elliott Hughes | b708d16 | 2015-10-27 16:03:15 -0700 | [diff] [blame] | 42 | #include "line_printer.h" |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 43 | |
Elliott Hughes | ae5a6c0 | 2015-09-27 12:55:37 -0700 | [diff] [blame] | 44 | #include <base/file.h> |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 45 | #include <base/strings.h> |
Elliott Hughes | 5c74270 | 2015-07-30 17:42:01 -0700 | [diff] [blame] | 46 | #include <base/stringprintf.h> |
| 47 | |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 48 | struct syncsendbuf { |
| 49 | unsigned id; |
| 50 | unsigned size; |
| 51 | char data[SYNC_DATA_MAX]; |
| 52 | }; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 53 | |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 54 | class SyncConnection { |
| 55 | public: |
Elliott Hughes | b708d16 | 2015-10-27 16:03:15 -0700 | [diff] [blame] | 56 | SyncConnection() : total_bytes(0), start_time_ms_(CurrentTimeMs()) { |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 57 | max = SYNC_DATA_MAX; // TODO: decide at runtime. |
| 58 | |
| 59 | std::string error; |
| 60 | fd = adb_connect("sync:", &error); |
| 61 | if (fd < 0) { |
Elliott Hughes | b708d16 | 2015-10-27 16:03:15 -0700 | [diff] [blame] | 62 | Error("connect failed: %s", error.c_str()); |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 63 | } |
| 64 | } |
| 65 | |
| 66 | ~SyncConnection() { |
| 67 | if (!IsValid()) return; |
| 68 | |
Spencer Low | 351ecd1 | 2015-10-14 17:32:44 -0700 | [diff] [blame] | 69 | 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 Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 78 | adb_close(fd); |
| 79 | } |
| 80 | |
| 81 | bool IsValid() { return fd >= 0; } |
| 82 | |
Elliott Hughes | ae5a6c0 | 2015-09-27 12:55:37 -0700 | [diff] [blame] | 83 | bool SendRequest(int id, const char* path_and_mode) { |
| 84 | size_t path_length = strlen(path_and_mode); |
| 85 | if (path_length > 1024) { |
Elliott Hughes | b708d16 | 2015-10-27 16:03:15 -0700 | [diff] [blame] | 86 | Error("SendRequest failed: path too long: %zu", path_length); |
Elliott Hughes | ae5a6c0 | 2015-09-27 12:55:37 -0700 | [diff] [blame] | 87 | 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 Hughes | 6d92997 | 2015-10-27 13:40:35 -0700 | [diff] [blame] | 93 | std::vector<char> buf(sizeof(SyncRequest) + path_length); |
| 94 | SyncRequest* req = reinterpret_cast<SyncRequest*>(&buf[0]); |
Elliott Hughes | ae5a6c0 | 2015-09-27 12:55:37 -0700 | [diff] [blame] | 95 | 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 Hughes | 6d92997 | 2015-10-27 13:40:35 -0700 | [diff] [blame] | 100 | return WriteFdExactly(fd, &buf[0], buf.size()); |
Elliott Hughes | ae5a6c0 | 2015-09-27 12:55:37 -0700 | [diff] [blame] | 101 | } |
| 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 Hughes | b708d16 | 2015-10-27 16:03:15 -0700 | [diff] [blame] | 106 | const char* rpath, |
Elliott Hughes | 6aab58c | 2015-11-20 17:35:17 -0800 | [diff] [blame] | 107 | unsigned mtime, |
| 108 | const char* data, size_t data_length) { |
Elliott Hughes | b708d16 | 2015-10-27 16:03:15 -0700 | [diff] [blame] | 109 | Print(rpath); |
| 110 | |
Elliott Hughes | ae5a6c0 | 2015-09-27 12:55:37 -0700 | [diff] [blame] | 111 | size_t path_length = strlen(path_and_mode); |
| 112 | if (path_length > 1024) { |
Elliott Hughes | b708d16 | 2015-10-27 16:03:15 -0700 | [diff] [blame] | 113 | Error("SendSmallFile failed: path too long: %zu", path_length); |
Elliott Hughes | ae5a6c0 | 2015-09-27 12:55:37 -0700 | [diff] [blame] | 114 | errno = ENAMETOOLONG; |
| 115 | return false; |
| 116 | } |
| 117 | |
Elliott Hughes | 6d92997 | 2015-10-27 13:40:35 -0700 | [diff] [blame] | 118 | std::vector<char> buf(sizeof(SyncRequest) + path_length + |
Elliott Hughes | 6aab58c | 2015-11-20 17:35:17 -0800 | [diff] [blame] | 119 | sizeof(SyncRequest) + data_length + |
| 120 | sizeof(SyncRequest)); |
Elliott Hughes | 6d92997 | 2015-10-27 13:40:35 -0700 | [diff] [blame] | 121 | char* p = &buf[0]; |
Elliott Hughes | ae5a6c0 | 2015-09-27 12:55:37 -0700 | [diff] [blame] | 122 | |
| 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 Hughes | 6d92997 | 2015-10-27 13:40:35 -0700 | [diff] [blame] | 142 | if (!WriteFdExactly(fd, &buf[0], (p - &buf[0]))) return false; |
Elliott Hughes | ae5a6c0 | 2015-09-27 12:55:37 -0700 | [diff] [blame] | 143 | |
| 144 | total_bytes += data_length; |
| 145 | return true; |
| 146 | } |
| 147 | |
Elliott Hughes | 6aab58c | 2015-11-20 17:35:17 -0800 | [diff] [blame] | 148 | bool SendLargeFile(const char* path_and_mode, |
| 149 | const char* lpath, const char* rpath, |
| 150 | unsigned mtime) { |
| 151 | if (!SendRequest(ID_SEND, path_and_mode)) { |
| 152 | Error("failed to send ID_SEND message '%s': %s", path_and_mode, strerror(errno)); |
| 153 | return false; |
| 154 | } |
| 155 | |
| 156 | struct stat st; |
| 157 | if (stat(lpath, &st) == -1) { |
| 158 | Error("cannot stat '%s': %s", lpath, strerror(errno)); |
| 159 | return false; |
| 160 | } |
| 161 | |
| 162 | uint64_t total_size = st.st_size; |
| 163 | uint64_t bytes_copied = 0; |
| 164 | |
| 165 | int lfd = adb_open(lpath, O_RDONLY); |
| 166 | if (lfd < 0) { |
| 167 | Error("cannot open '%s': %s", lpath, strerror(errno)); |
| 168 | return false; |
| 169 | } |
| 170 | |
| 171 | syncsendbuf sbuf; |
| 172 | sbuf.id = ID_DATA; |
| 173 | while (true) { |
| 174 | int ret = adb_read(lfd, sbuf.data, max); |
| 175 | if (ret <= 0) { |
| 176 | if (ret < 0) { |
| 177 | Error("cannot read '%s': %s", lpath, strerror(errno)); |
| 178 | adb_close(lfd); |
| 179 | return false; |
| 180 | } |
| 181 | break; |
| 182 | } |
| 183 | |
| 184 | sbuf.size = ret; |
| 185 | if (!WriteFdExactly(fd, &sbuf, sizeof(unsigned) * 2 + ret)) { |
| 186 | adb_close(lfd); |
| 187 | return false; |
| 188 | } |
| 189 | total_bytes += ret; |
| 190 | |
| 191 | bytes_copied += ret; |
| 192 | |
| 193 | int percentage = static_cast<int>(bytes_copied * 100 / total_size); |
| 194 | Printf("%s: %d%%", rpath, percentage); |
| 195 | } |
| 196 | |
| 197 | adb_close(lfd); |
| 198 | |
| 199 | syncmsg msg; |
| 200 | msg.data.id = ID_DONE; |
| 201 | msg.data.size = mtime; |
| 202 | if (!WriteFdExactly(fd, &msg.data, sizeof(msg.data))) { |
| 203 | Error("failed to send ID_DONE message for '%s': %s", rpath, strerror(errno)); |
| 204 | return false; |
| 205 | } |
| 206 | |
| 207 | return true; |
| 208 | } |
| 209 | |
Elliott Hughes | ae5a6c0 | 2015-09-27 12:55:37 -0700 | [diff] [blame] | 210 | bool CopyDone(const char* from, const char* to) { |
| 211 | syncmsg msg; |
| 212 | if (!ReadFdExactly(fd, &msg.status, sizeof(msg.status))) { |
Elliott Hughes | b708d16 | 2015-10-27 16:03:15 -0700 | [diff] [blame] | 213 | 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] | 214 | return false; |
| 215 | } |
| 216 | if (msg.status.id == ID_OKAY) { |
| 217 | return true; |
| 218 | } |
| 219 | if (msg.status.id != ID_FAIL) { |
Elliott Hughes | b708d16 | 2015-10-27 16:03:15 -0700 | [diff] [blame] | 220 | 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] | 221 | return false; |
| 222 | } |
Elliott Hughes | 8b43c3e | 2015-10-23 21:06:11 -0700 | [diff] [blame] | 223 | return ReportCopyFailure(from, to, msg); |
| 224 | } |
| 225 | |
| 226 | bool ReportCopyFailure(const char* from, const char* to, const syncmsg& msg) { |
Elliott Hughes | 6d92997 | 2015-10-27 13:40:35 -0700 | [diff] [blame] | 227 | std::vector<char> buf(msg.status.msglen + 1); |
| 228 | if (!ReadFdExactly(fd, &buf[0], msg.status.msglen)) { |
Elliott Hughes | b708d16 | 2015-10-27 16:03:15 -0700 | [diff] [blame] | 229 | Error("failed to copy '%s' to '%s'; failed to read reason (!): %s", |
| 230 | from, to, strerror(errno)); |
Elliott Hughes | ae5a6c0 | 2015-09-27 12:55:37 -0700 | [diff] [blame] | 231 | return false; |
| 232 | } |
Elliott Hughes | 6d92997 | 2015-10-27 13:40:35 -0700 | [diff] [blame] | 233 | buf[msg.status.msglen] = 0; |
Elliott Hughes | b708d16 | 2015-10-27 16:03:15 -0700 | [diff] [blame] | 234 | Error("failed to copy '%s' to '%s': %s", from, to, &buf[0]); |
Elliott Hughes | ae5a6c0 | 2015-09-27 12:55:37 -0700 | [diff] [blame] | 235 | return false; |
| 236 | } |
| 237 | |
Elliott Hughes | b708d16 | 2015-10-27 16:03:15 -0700 | [diff] [blame] | 238 | std::string TransferRate() { |
| 239 | uint64_t ms = CurrentTimeMs() - start_time_ms_; |
| 240 | if (total_bytes == 0 || ms == 0) return ""; |
| 241 | |
| 242 | double s = static_cast<double>(ms) / 1000LL; |
| 243 | double rate = (static_cast<double>(total_bytes) / s) / (1024*1024); |
| 244 | return android::base::StringPrintf(" %.1f MB/s (%" PRId64 " bytes in %.3fs)", |
| 245 | rate, total_bytes, s); |
| 246 | } |
| 247 | |
| 248 | void Print(const std::string& s) { |
| 249 | // TODO: we actually don't want ELIDE; we want "ELIDE if smart, FULL if dumb". |
| 250 | line_printer_.Print(s, LinePrinter::ELIDE); |
| 251 | } |
| 252 | |
Josh Gao | 983c41c | 2015-11-02 17:15:57 -0800 | [diff] [blame] | 253 | void Printf(const char* fmt, ...) __attribute__((__format__(ADB_FORMAT_ARCHETYPE, 2, 3))) { |
| 254 | std::string s; |
| 255 | va_list ap; |
| 256 | va_start(ap, fmt); |
| 257 | android::base::StringAppendV(&s, fmt, ap); |
| 258 | va_end(ap); |
| 259 | |
| 260 | Print(s); |
| 261 | } |
| 262 | |
Elliott Hughes | b708d16 | 2015-10-27 16:03:15 -0700 | [diff] [blame] | 263 | void Error(const char* fmt, ...) __attribute__((__format__(ADB_FORMAT_ARCHETYPE, 2, 3))) { |
| 264 | std::string s = "adb: error: "; |
| 265 | |
| 266 | va_list ap; |
| 267 | va_start(ap, fmt); |
| 268 | android::base::StringAppendV(&s, fmt, ap); |
| 269 | va_end(ap); |
| 270 | |
| 271 | line_printer_.Print(s, LinePrinter::FULL); |
| 272 | } |
| 273 | |
Josh Gao | 21abf5a | 2015-11-07 17:18:44 -0800 | [diff] [blame] | 274 | void Warning(const char* fmt, ...) __attribute__((__format__(ADB_FORMAT_ARCHETYPE, 2, 3))) { |
| 275 | std::string s = "adb: warning: "; |
| 276 | |
| 277 | va_list ap; |
| 278 | va_start(ap, fmt); |
| 279 | android::base::StringAppendV(&s, fmt, ap); |
| 280 | va_end(ap); |
| 281 | |
| 282 | line_printer_.Print(s, LinePrinter::FULL); |
| 283 | } |
| 284 | |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 285 | uint64_t total_bytes; |
| 286 | |
| 287 | // TODO: add a char[max] buffer here, to replace syncsendbuf... |
| 288 | int fd; |
| 289 | size_t max; |
| 290 | |
| 291 | private: |
Elliott Hughes | b708d16 | 2015-10-27 16:03:15 -0700 | [diff] [blame] | 292 | uint64_t start_time_ms_; |
| 293 | |
| 294 | LinePrinter line_printer_; |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 295 | |
Spencer Low | 351ecd1 | 2015-10-14 17:32:44 -0700 | [diff] [blame] | 296 | bool SendQuit() { |
| 297 | return SendRequest(ID_QUIT, ""); // TODO: add a SendResponse? |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 298 | } |
| 299 | |
Elliott Hughes | b708d16 | 2015-10-27 16:03:15 -0700 | [diff] [blame] | 300 | static uint64_t CurrentTimeMs() { |
| 301 | struct timeval tv; |
| 302 | gettimeofday(&tv, 0); // (Not clock_gettime because of Mac/Windows.) |
| 303 | 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] | 304 | } |
| 305 | }; |
| 306 | |
Josh Gao | cda6a2b | 2015-11-02 16:45:47 -0800 | [diff] [blame] | 307 | typedef void (sync_ls_cb)(unsigned mode, unsigned size, unsigned time, const char* name); |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 308 | |
Josh Gao | cda6a2b | 2015-11-02 16:45:47 -0800 | [diff] [blame] | 309 | static bool sync_ls(SyncConnection& sc, const char* path, |
| 310 | std::function<sync_ls_cb> func) { |
Elliott Hughes | ae5a6c0 | 2015-09-27 12:55:37 -0700 | [diff] [blame] | 311 | if (!sc.SendRequest(ID_LIST, path)) return false; |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 312 | |
| 313 | while (true) { |
| 314 | syncmsg msg; |
Elliott Hughes | ae5a6c0 | 2015-09-27 12:55:37 -0700 | [diff] [blame] | 315 | if (!ReadFdExactly(sc.fd, &msg.dent, sizeof(msg.dent))) return false; |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 316 | |
| 317 | if (msg.dent.id == ID_DONE) return true; |
| 318 | if (msg.dent.id != ID_DENT) return false; |
| 319 | |
Elliott Hughes | f446520 | 2015-08-24 14:27:03 -0700 | [diff] [blame] | 320 | size_t len = msg.dent.namelen; |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 321 | if (len > 256) return false; // TODO: resize buffer? continue? |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 322 | |
Elliott Hughes | 5c74270 | 2015-07-30 17:42:01 -0700 | [diff] [blame] | 323 | char buf[257]; |
Elliott Hughes | ae5a6c0 | 2015-09-27 12:55:37 -0700 | [diff] [blame] | 324 | if (!ReadFdExactly(sc.fd, buf, len)) return false; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 325 | buf[len] = 0; |
| 326 | |
Josh Gao | cda6a2b | 2015-11-02 16:45:47 -0800 | [diff] [blame] | 327 | func(msg.dent.mode, msg.dent.size, msg.dent.time, buf); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 328 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 329 | } |
| 330 | |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 331 | static bool sync_finish_stat(SyncConnection& sc, unsigned int* timestamp, |
| 332 | unsigned int* mode, unsigned int* size) { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 333 | syncmsg msg; |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 334 | if (!ReadFdExactly(sc.fd, &msg.stat, sizeof(msg.stat)) || msg.stat.id != ID_STAT) { |
| 335 | return false; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 336 | } |
| 337 | |
Elliott Hughes | f446520 | 2015-08-24 14:27:03 -0700 | [diff] [blame] | 338 | if (timestamp) *timestamp = msg.stat.time; |
| 339 | if (mode) *mode = msg.stat.mode; |
| 340 | if (size) *size = msg.stat.size; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 341 | |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 342 | return true; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 343 | } |
| 344 | |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 345 | static bool sync_stat(SyncConnection& sc, const char* path, |
| 346 | unsigned int* timestamp, unsigned int* mode, unsigned int* size) { |
Elliott Hughes | ae5a6c0 | 2015-09-27 12:55:37 -0700 | [diff] [blame] | 347 | 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] | 348 | } |
| 349 | |
Elliott Hughes | ae5a6c0 | 2015-09-27 12:55:37 -0700 | [diff] [blame] | 350 | static bool sync_send(SyncConnection& sc, const char* lpath, const char* rpath, |
Elliott Hughes | b708d16 | 2015-10-27 16:03:15 -0700 | [diff] [blame] | 351 | unsigned mtime, mode_t mode) |
Elliott Hughes | ae5a6c0 | 2015-09-27 12:55:37 -0700 | [diff] [blame] | 352 | { |
| 353 | std::string path_and_mode = android::base::StringPrintf("%s,%d", rpath, mode); |
| 354 | |
| 355 | if (S_ISLNK(mode)) { |
| 356 | #if !defined(_WIN32) |
| 357 | char buf[PATH_MAX]; |
| 358 | ssize_t data_length = readlink(lpath, buf, PATH_MAX - 1); |
| 359 | if (data_length == -1) { |
Elliott Hughes | b708d16 | 2015-10-27 16:03:15 -0700 | [diff] [blame] | 360 | sc.Error("readlink '%s' failed: %s", lpath, strerror(errno)); |
Elliott Hughes | ae5a6c0 | 2015-09-27 12:55:37 -0700 | [diff] [blame] | 361 | return false; |
| 362 | } |
| 363 | buf[data_length++] = '\0'; |
| 364 | |
Elliott Hughes | 6aab58c | 2015-11-20 17:35:17 -0800 | [diff] [blame] | 365 | if (!sc.SendSmallFile(path_and_mode.c_str(), rpath, mtime, buf, data_length)) return false; |
Elliott Hughes | ae5a6c0 | 2015-09-27 12:55:37 -0700 | [diff] [blame] | 366 | return sc.CopyDone(lpath, rpath); |
| 367 | #endif |
| 368 | } |
| 369 | |
| 370 | if (!S_ISREG(mode)) { |
Elliott Hughes | b708d16 | 2015-10-27 16:03:15 -0700 | [diff] [blame] | 371 | sc.Error("local file '%s' has unsupported mode: 0o%o", lpath, mode); |
Elliott Hughes | ae5a6c0 | 2015-09-27 12:55:37 -0700 | [diff] [blame] | 372 | return false; |
| 373 | } |
| 374 | |
| 375 | struct stat st; |
| 376 | if (stat(lpath, &st) == -1) { |
Elliott Hughes | b708d16 | 2015-10-27 16:03:15 -0700 | [diff] [blame] | 377 | sc.Error("failed to stat local file '%s': %s", lpath, strerror(errno)); |
Elliott Hughes | ae5a6c0 | 2015-09-27 12:55:37 -0700 | [diff] [blame] | 378 | return false; |
| 379 | } |
| 380 | if (st.st_size < SYNC_DATA_MAX) { |
| 381 | std::string data; |
| 382 | if (!android::base::ReadFileToString(lpath, &data)) { |
Elliott Hughes | b708d16 | 2015-10-27 16:03:15 -0700 | [diff] [blame] | 383 | sc.Error("failed to read all of '%s': %s", lpath, strerror(errno)); |
Elliott Hughes | ae5a6c0 | 2015-09-27 12:55:37 -0700 | [diff] [blame] | 384 | return false; |
| 385 | } |
Elliott Hughes | 6aab58c | 2015-11-20 17:35:17 -0800 | [diff] [blame] | 386 | if (!sc.SendSmallFile(path_and_mode.c_str(), rpath, mtime, data.data(), data.size())) { |
Elliott Hughes | b708d16 | 2015-10-27 16:03:15 -0700 | [diff] [blame] | 387 | return false; |
| 388 | } |
Elliott Hughes | ae5a6c0 | 2015-09-27 12:55:37 -0700 | [diff] [blame] | 389 | } else { |
Elliott Hughes | 6aab58c | 2015-11-20 17:35:17 -0800 | [diff] [blame] | 390 | if (!sc.SendLargeFile(path_and_mode.c_str(), lpath, rpath, mtime)) { |
Elliott Hughes | b708d16 | 2015-10-27 16:03:15 -0700 | [diff] [blame] | 391 | return false; |
| 392 | } |
Elliott Hughes | ae5a6c0 | 2015-09-27 12:55:37 -0700 | [diff] [blame] | 393 | } |
| 394 | return sc.CopyDone(lpath, rpath); |
| 395 | } |
| 396 | |
Elliott Hughes | b708d16 | 2015-10-27 16:03:15 -0700 | [diff] [blame] | 397 | static bool sync_recv(SyncConnection& sc, const char* rpath, const char* lpath) { |
| 398 | sc.Print(rpath); |
| 399 | |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 400 | unsigned size = 0; |
Elliott Hughes | b708d16 | 2015-10-27 16:03:15 -0700 | [diff] [blame] | 401 | if (!sync_stat(sc, rpath, nullptr, nullptr, &size)) return false; |
Mark Lindner | 76f2a93 | 2014-03-11 17:55:59 -0700 | [diff] [blame] | 402 | |
Elliott Hughes | ae5a6c0 | 2015-09-27 12:55:37 -0700 | [diff] [blame] | 403 | if (!sc.SendRequest(ID_RECV, rpath)) return false; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 404 | |
Elliott Hughes | 8b43c3e | 2015-10-23 21:06:11 -0700 | [diff] [blame] | 405 | adb_unlink(lpath); |
Josh Gao | f6e65e3 | 2015-11-17 14:08:20 -0800 | [diff] [blame] | 406 | const std::string dirpath = adb_dirname(lpath); |
| 407 | if (!mkdirs(dirpath.c_str())) { |
| 408 | sc.Error("failed to create parent directory '%s': %s", dirpath.c_str(), strerror(errno)); |
Josh Gao | 45b6fc8 | 2015-11-04 14:51:23 -0800 | [diff] [blame] | 409 | return false; |
| 410 | } |
| 411 | |
Elliott Hughes | 8b43c3e | 2015-10-23 21:06:11 -0700 | [diff] [blame] | 412 | int lfd = adb_creat(lpath, 0644); |
| 413 | if (lfd < 0) { |
Elliott Hughes | b708d16 | 2015-10-27 16:03:15 -0700 | [diff] [blame] | 414 | sc.Error("cannot create '%s': %s", lpath, strerror(errno)); |
Elliott Hughes | 8b43c3e | 2015-10-23 21:06:11 -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 | b708d16 | 2015-10-27 16:03:15 -0700 | [diff] [blame] | 418 | uint64_t bytes_copied = 0; |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 419 | while (true) { |
Elliott Hughes | 8b43c3e | 2015-10-23 21:06:11 -0700 | [diff] [blame] | 420 | syncmsg msg; |
Elliott Hughes | ae5a6c0 | 2015-09-27 12:55:37 -0700 | [diff] [blame] | 421 | if (!ReadFdExactly(sc.fd, &msg.data, sizeof(msg.data))) { |
Spencer Low | d8cce18 | 2015-08-28 01:07:30 -0700 | [diff] [blame] | 422 | adb_close(lfd); |
Elliott Hughes | 8b43c3e | 2015-10-23 21:06:11 -0700 | [diff] [blame] | 423 | adb_unlink(lpath); |
Elliott Hughes | ae5a6c0 | 2015-09-27 12:55:37 -0700 | [diff] [blame] | 424 | return false; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 425 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 426 | |
Elliott Hughes | 8b43c3e | 2015-10-23 21:06:11 -0700 | [diff] [blame] | 427 | if (msg.data.id == ID_DONE) break; |
| 428 | |
| 429 | if (msg.data.id != ID_DATA) { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 430 | adb_close(lfd); |
Elliott Hughes | 8b43c3e | 2015-10-23 21:06:11 -0700 | [diff] [blame] | 431 | adb_unlink(lpath); |
| 432 | sc.ReportCopyFailure(rpath, lpath, msg); |
Elliott Hughes | ae5a6c0 | 2015-09-27 12:55:37 -0700 | [diff] [blame] | 433 | return false; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 434 | } |
| 435 | |
Elliott Hughes | 8b43c3e | 2015-10-23 21:06:11 -0700 | [diff] [blame] | 436 | if (msg.data.size > sc.max) { |
Elliott Hughes | b708d16 | 2015-10-27 16:03:15 -0700 | [diff] [blame] | 437 | 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] | 438 | adb_close(lfd); |
Elliott Hughes | 8b43c3e | 2015-10-23 21:06:11 -0700 | [diff] [blame] | 439 | adb_unlink(lpath); |
Elliott Hughes | ae5a6c0 | 2015-09-27 12:55:37 -0700 | [diff] [blame] | 440 | return false; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 441 | } |
| 442 | |
Elliott Hughes | 8b43c3e | 2015-10-23 21:06:11 -0700 | [diff] [blame] | 443 | char buffer[SYNC_DATA_MAX]; |
| 444 | if (!ReadFdExactly(sc.fd, buffer, msg.data.size)) { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 445 | adb_close(lfd); |
Elliott Hughes | 8b43c3e | 2015-10-23 21:06:11 -0700 | [diff] [blame] | 446 | adb_unlink(lpath); |
Elliott Hughes | ae5a6c0 | 2015-09-27 12:55:37 -0700 | [diff] [blame] | 447 | return false; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 448 | } |
| 449 | |
Elliott Hughes | 8b43c3e | 2015-10-23 21:06:11 -0700 | [diff] [blame] | 450 | if (!WriteFdExactly(lfd, buffer, msg.data.size)) { |
Elliott Hughes | b708d16 | 2015-10-27 16:03:15 -0700 | [diff] [blame] | 451 | sc.Error("cannot write '%s': %s", lpath, strerror(errno)); |
Elliott Hughes | 8b43c3e | 2015-10-23 21:06:11 -0700 | [diff] [blame] | 452 | adb_close(lfd); |
| 453 | adb_unlink(lpath); |
| 454 | return false; |
| 455 | } |
| 456 | |
| 457 | sc.total_bytes += msg.data.size; |
Mark Lindner | 76f2a93 | 2014-03-11 17:55:59 -0700 | [diff] [blame] | 458 | |
Elliott Hughes | b708d16 | 2015-10-27 16:03:15 -0700 | [diff] [blame] | 459 | bytes_copied += msg.data.size; |
| 460 | |
| 461 | int percentage = static_cast<int>(bytes_copied * 100 / size); |
Josh Gao | 983c41c | 2015-11-02 17:15:57 -0800 | [diff] [blame] | 462 | sc.Printf("%s: %d%%", rpath, percentage); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 463 | } |
| 464 | |
| 465 | adb_close(lfd); |
Elliott Hughes | ae5a6c0 | 2015-09-27 12:55:37 -0700 | [diff] [blame] | 466 | return true; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 467 | } |
| 468 | |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 469 | bool do_sync_ls(const char* path) { |
| 470 | SyncConnection sc; |
| 471 | if (!sc.IsValid()) return false; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 472 | |
Josh Gao | cda6a2b | 2015-11-02 16:45:47 -0800 | [diff] [blame] | 473 | return sync_ls(sc, path, [](unsigned mode, unsigned size, unsigned time, |
| 474 | const char* name) { |
| 475 | printf("%08x %08x %08x %s\n", mode, size, time, name); |
| 476 | }); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 477 | } |
| 478 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 479 | struct copyinfo |
| 480 | { |
Josh Gao | 1a02530 | 2015-11-09 11:12:14 -0800 | [diff] [blame] | 481 | std::string lpath; |
| 482 | std::string rpath; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 483 | unsigned int time; |
| 484 | unsigned int mode; |
Elliott Hughes | ae5a6c0 | 2015-09-27 12:55:37 -0700 | [diff] [blame] | 485 | uint64_t size; |
Josh Gao | fc7c3b6 | 2015-11-03 14:44:04 -0800 | [diff] [blame] | 486 | bool skip; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 487 | }; |
| 488 | |
Josh Gao | 1a02530 | 2015-11-09 11:12:14 -0800 | [diff] [blame] | 489 | static void ensure_trailing_separator(std::string& lpath, std::string& rpath) { |
| 490 | if (!adb_is_separator(lpath.back())) { |
| 491 | lpath.push_back(OS_PATH_SEPARATOR); |
| 492 | } |
| 493 | if (rpath.back() != '/') { |
| 494 | rpath.push_back('/'); |
| 495 | } |
| 496 | } |
| 497 | |
| 498 | static copyinfo mkcopyinfo(std::string lpath, std::string rpath, |
Josh Gao | 6580096 | 2015-11-04 14:57:04 -0800 | [diff] [blame] | 499 | const std::string& name, unsigned int mode) { |
Josh Gao | cda6a2b | 2015-11-02 16:45:47 -0800 | [diff] [blame] | 500 | copyinfo result; |
Josh Gao | 1a02530 | 2015-11-09 11:12:14 -0800 | [diff] [blame] | 501 | result.lpath = std::move(lpath); |
| 502 | result.rpath = std::move(rpath); |
| 503 | ensure_trailing_separator(result.lpath, result.rpath); |
| 504 | result.lpath.append(name); |
| 505 | result.rpath.append(name); |
Josh Gao | 12a2ae9 | 2015-11-07 15:27:26 -0800 | [diff] [blame] | 506 | |
Josh Gao | 1a02530 | 2015-11-09 11:12:14 -0800 | [diff] [blame] | 507 | if (S_ISDIR(mode)) { |
| 508 | ensure_trailing_separator(result.lpath, result.rpath); |
Josh Gao | d973157 | 2015-11-03 15:23:03 -0800 | [diff] [blame] | 509 | } |
Josh Gao | 6580096 | 2015-11-04 14:57:04 -0800 | [diff] [blame] | 510 | |
Josh Gao | cda6a2b | 2015-11-02 16:45:47 -0800 | [diff] [blame] | 511 | result.time = 0; |
Josh Gao | 6580096 | 2015-11-04 14:57:04 -0800 | [diff] [blame] | 512 | result.mode = mode; |
Josh Gao | cda6a2b | 2015-11-02 16:45:47 -0800 | [diff] [blame] | 513 | result.size = 0; |
Josh Gao | fc7c3b6 | 2015-11-03 14:44:04 -0800 | [diff] [blame] | 514 | result.skip = false; |
Josh Gao | cda6a2b | 2015-11-02 16:45:47 -0800 | [diff] [blame] | 515 | return result; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 516 | } |
| 517 | |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 518 | static bool IsDotOrDotDot(const char* name) { |
| 519 | return name[0] == '.' && (name[1] == '\0' || (name[1] == '.' && name[2] == '\0')); |
| 520 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 521 | |
Josh Gao | cd7c1ed | 2015-11-03 15:26:38 -0800 | [diff] [blame] | 522 | static bool local_build_list(SyncConnection& sc, std::vector<copyinfo>* filelist, |
| 523 | const std::string& lpath, |
| 524 | const std::string& rpath) { |
Josh Gao | cda6a2b | 2015-11-02 16:45:47 -0800 | [diff] [blame] | 525 | std::vector<copyinfo> dirlist; |
Josh Gao | d973157 | 2015-11-03 15:23:03 -0800 | [diff] [blame] | 526 | std::unique_ptr<DIR, int (*)(DIR*)> dir(opendir(lpath.c_str()), closedir); |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 527 | if (!dir) { |
Josh Gao | d973157 | 2015-11-03 15:23:03 -0800 | [diff] [blame] | 528 | sc.Error("cannot open '%s': %s", lpath.c_str(), strerror(errno)); |
Josh Gao | cd7c1ed | 2015-11-03 15:26:38 -0800 | [diff] [blame] | 529 | return false; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 530 | } |
| 531 | |
Josh Gao | 6580096 | 2015-11-04 14:57:04 -0800 | [diff] [blame] | 532 | bool empty_dir = true; |
Elliott Hughes | b708d16 | 2015-10-27 16:03:15 -0700 | [diff] [blame] | 533 | dirent* de; |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 534 | while ((de = readdir(dir.get()))) { |
Josh Gao | 6580096 | 2015-11-04 14:57:04 -0800 | [diff] [blame] | 535 | if (IsDotOrDotDot(de->d_name)) { |
| 536 | continue; |
| 537 | } |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 538 | |
Josh Gao | 6580096 | 2015-11-04 14:57:04 -0800 | [diff] [blame] | 539 | empty_dir = false; |
Josh Gao | d973157 | 2015-11-03 15:23:03 -0800 | [diff] [blame] | 540 | std::string stat_path = lpath + de->d_name; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 541 | |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 542 | struct stat st; |
Josh Gao | 12a2ae9 | 2015-11-07 15:27:26 -0800 | [diff] [blame] | 543 | if (lstat(stat_path.c_str(), &st) == -1) { |
Josh Gao | d973157 | 2015-11-03 15:23:03 -0800 | [diff] [blame] | 544 | sc.Error("cannot lstat '%s': %s", stat_path.c_str(), |
| 545 | strerror(errno)); |
Josh Gao | 12a2ae9 | 2015-11-07 15:27:26 -0800 | [diff] [blame] | 546 | continue; |
| 547 | } |
| 548 | |
| 549 | copyinfo ci = mkcopyinfo(lpath, rpath, de->d_name, st.st_mode); |
| 550 | if (S_ISDIR(st.st_mode)) { |
| 551 | dirlist.push_back(ci); |
| 552 | } else { |
| 553 | if (!S_ISREG(st.st_mode) && !S_ISLNK(st.st_mode)) { |
| 554 | sc.Error("skipping special file '%s'", lpath.c_str()); |
Josh Gao | dd6cc4d | 2015-11-30 10:21:25 -0800 | [diff] [blame^] | 555 | ci.skip = true; |
Josh Gao | 12a2ae9 | 2015-11-07 15:27:26 -0800 | [diff] [blame] | 556 | } else { |
| 557 | ci.time = st.st_mtime; |
| 558 | ci.size = st.st_size; |
Josh Gao | 12a2ae9 | 2015-11-07 15:27:26 -0800 | [diff] [blame] | 559 | } |
Josh Gao | dd6cc4d | 2015-11-30 10:21:25 -0800 | [diff] [blame^] | 560 | filelist->push_back(ci); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 561 | } |
| 562 | } |
| 563 | |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 564 | // Close this directory and recurse. |
| 565 | dir.reset(); |
Josh Gao | 6580096 | 2015-11-04 14:57:04 -0800 | [diff] [blame] | 566 | |
| 567 | // Add the current directory to the list if it was empty, to ensure that |
| 568 | // it gets created. |
| 569 | if (empty_dir) { |
| 570 | // TODO(b/25566053): Make pushing empty directories work. |
| 571 | // TODO(b/25457350): We don't preserve permissions on directories. |
Josh Gao | 21abf5a | 2015-11-07 17:18:44 -0800 | [diff] [blame] | 572 | sc.Warning("skipping empty directory '%s'", lpath.c_str()); |
Josh Gao | 6580096 | 2015-11-04 14:57:04 -0800 | [diff] [blame] | 573 | copyinfo ci = mkcopyinfo(adb_dirname(lpath), adb_dirname(rpath), |
| 574 | adb_basename(lpath), S_IFDIR); |
| 575 | ci.skip = true; |
| 576 | filelist->push_back(ci); |
| 577 | return true; |
| 578 | } |
| 579 | |
Josh Gao | cda6a2b | 2015-11-02 16:45:47 -0800 | [diff] [blame] | 580 | for (const copyinfo& ci : dirlist) { |
Josh Gao | 1a02530 | 2015-11-09 11:12:14 -0800 | [diff] [blame] | 581 | local_build_list(sc, filelist, ci.lpath, ci.rpath); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 582 | } |
| 583 | |
Josh Gao | cd7c1ed | 2015-11-03 15:26:38 -0800 | [diff] [blame] | 584 | return true; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 585 | } |
| 586 | |
Josh Gao | d973157 | 2015-11-03 15:23:03 -0800 | [diff] [blame] | 587 | static bool copy_local_dir_remote(SyncConnection& sc, std::string lpath, |
| 588 | std::string rpath, bool check_timestamps, |
| 589 | bool list_only) { |
| 590 | // Make sure that both directory paths end in a slash. |
Josh Gao | 1a02530 | 2015-11-09 11:12:14 -0800 | [diff] [blame] | 591 | // Both paths are known to be nonempty, so we don't need to check. |
| 592 | ensure_trailing_separator(lpath, rpath); |
Josh Gao | d973157 | 2015-11-03 15:23:03 -0800 | [diff] [blame] | 593 | |
| 594 | // Recursively build the list of files to copy. |
Josh Gao | cda6a2b | 2015-11-02 16:45:47 -0800 | [diff] [blame] | 595 | std::vector<copyinfo> filelist; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 596 | int pushed = 0; |
| 597 | int skipped = 0; |
Josh Gao | cd7c1ed | 2015-11-03 15:26:38 -0800 | [diff] [blame] | 598 | if (!local_build_list(sc, &filelist, lpath, rpath)) { |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 599 | return false; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 600 | } |
| 601 | |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 602 | if (check_timestamps) { |
Josh Gao | cda6a2b | 2015-11-02 16:45:47 -0800 | [diff] [blame] | 603 | for (const copyinfo& ci : filelist) { |
Josh Gao | 1a02530 | 2015-11-09 11:12:14 -0800 | [diff] [blame] | 604 | if (!sc.SendRequest(ID_STAT, ci.rpath.c_str())) { |
Josh Gao | d973157 | 2015-11-03 15:23:03 -0800 | [diff] [blame] | 605 | return false; |
| 606 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 607 | } |
Josh Gao | cda6a2b | 2015-11-02 16:45:47 -0800 | [diff] [blame] | 608 | for (copyinfo& ci : filelist) { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 609 | unsigned int timestamp, mode, size; |
Josh Gao | d973157 | 2015-11-03 15:23:03 -0800 | [diff] [blame] | 610 | if (!sync_finish_stat(sc, ×tamp, &mode, &size)) { |
| 611 | return false; |
| 612 | } |
Josh Gao | cda6a2b | 2015-11-02 16:45:47 -0800 | [diff] [blame] | 613 | if (size == ci.size) { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 614 | /* for links, we cannot update the atime/mtime */ |
Josh Gao | cda6a2b | 2015-11-02 16:45:47 -0800 | [diff] [blame] | 615 | if ((S_ISREG(ci.mode & mode) && timestamp == ci.time) || |
| 616 | (S_ISLNK(ci.mode & mode) && timestamp >= ci.time)) { |
Josh Gao | fc7c3b6 | 2015-11-03 14:44:04 -0800 | [diff] [blame] | 617 | ci.skip = true; |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 618 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 619 | } |
| 620 | } |
| 621 | } |
Josh Gao | cda6a2b | 2015-11-02 16:45:47 -0800 | [diff] [blame] | 622 | |
| 623 | for (const copyinfo& ci : filelist) { |
Josh Gao | fc7c3b6 | 2015-11-03 14:44:04 -0800 | [diff] [blame] | 624 | if (!ci.skip) { |
Elliott Hughes | b708d16 | 2015-10-27 16:03:15 -0700 | [diff] [blame] | 625 | if (list_only) { |
Josh Gao | 1a02530 | 2015-11-09 11:12:14 -0800 | [diff] [blame] | 626 | sc.Error("would push: %s -> %s", ci.lpath.c_str(), |
| 627 | ci.rpath.c_str()); |
Elliott Hughes | b708d16 | 2015-10-27 16:03:15 -0700 | [diff] [blame] | 628 | } else { |
Josh Gao | 1a02530 | 2015-11-09 11:12:14 -0800 | [diff] [blame] | 629 | if (!sync_send(sc, ci.lpath.c_str(), ci.rpath.c_str(), ci.time, |
Josh Gao | d973157 | 2015-11-03 15:23:03 -0800 | [diff] [blame] | 630 | ci.mode)) { |
| 631 | return false; |
Elliott Hughes | b708d16 | 2015-10-27 16:03:15 -0700 | [diff] [blame] | 632 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 633 | } |
| 634 | pushed++; |
| 635 | } else { |
| 636 | skipped++; |
| 637 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 638 | } |
| 639 | |
Josh Gao | d973157 | 2015-11-03 15:23:03 -0800 | [diff] [blame] | 640 | sc.Printf("%s: %d file%s pushed. %d file%s skipped.%s\n", rpath.c_str(), |
| 641 | pushed, (pushed == 1) ? "" : "s", skipped, |
| 642 | (skipped == 1) ? "" : "s", sc.TransferRate().c_str()); |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 643 | return true; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 644 | } |
| 645 | |
Josh Gao | 0578677 | 2015-10-30 16:57:19 -0700 | [diff] [blame] | 646 | bool do_sync_push(const std::vector<const char*>& srcs, const char* dst) { |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 647 | SyncConnection sc; |
| 648 | if (!sc.IsValid()) return false; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 649 | |
Josh Gao | 0578677 | 2015-10-30 16:57:19 -0700 | [diff] [blame] | 650 | bool success = true; |
Josh Gao | 12a2ae9 | 2015-11-07 15:27:26 -0800 | [diff] [blame] | 651 | unsigned dst_mode; |
| 652 | if (!sync_stat(sc, dst, nullptr, &dst_mode, nullptr)) return false; |
| 653 | bool dst_exists = (dst_mode != 0); |
Josh Gao | 07db119 | 2015-11-07 15:38:19 -0800 | [diff] [blame] | 654 | bool dst_isdir = S_ISDIR(dst_mode); |
Josh Gao | 0578677 | 2015-10-30 16:57:19 -0700 | [diff] [blame] | 655 | |
Josh Gao | 07db119 | 2015-11-07 15:38:19 -0800 | [diff] [blame] | 656 | if (!dst_isdir) { |
Josh Gao | 0578677 | 2015-10-30 16:57:19 -0700 | [diff] [blame] | 657 | if (srcs.size() > 1) { |
| 658 | sc.Error("target '%s' is not a directory", dst); |
| 659 | return false; |
| 660 | } else { |
| 661 | size_t dst_len = strlen(dst); |
Josh Gao | 12a2ae9 | 2015-11-07 15:27:26 -0800 | [diff] [blame] | 662 | |
| 663 | // A path that ends with a slash doesn't have to be a directory if |
| 664 | // it doesn't exist yet. |
| 665 | if (dst[dst_len - 1] == '/' && dst_exists) { |
Josh Gao | 0578677 | 2015-10-30 16:57:19 -0700 | [diff] [blame] | 666 | sc.Error("failed to access '%s': Not a directory", dst); |
| 667 | return false; |
| 668 | } |
| 669 | } |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 670 | } |
Josh Gao | 0578677 | 2015-10-30 16:57:19 -0700 | [diff] [blame] | 671 | |
| 672 | for (const char* src_path : srcs) { |
| 673 | const char* dst_path = dst; |
| 674 | struct stat st; |
Josh Gao | 12a2ae9 | 2015-11-07 15:27:26 -0800 | [diff] [blame] | 675 | if (stat(src_path, &st) == -1) { |
Josh Gao | 0578677 | 2015-10-30 16:57:19 -0700 | [diff] [blame] | 676 | sc.Error("cannot stat '%s': %s", src_path, strerror(errno)); |
| 677 | success = false; |
| 678 | continue; |
| 679 | } |
| 680 | |
| 681 | if (S_ISDIR(st.st_mode)) { |
Josh Gao | 07db119 | 2015-11-07 15:38:19 -0800 | [diff] [blame] | 682 | std::string dst_dir = dst; |
| 683 | |
| 684 | // If the destination path existed originally, the source directory |
| 685 | // should be copied as a child of the destination. |
| 686 | if (dst_exists) { |
| 687 | if (!dst_isdir) { |
| 688 | sc.Error("target '%s' is not a directory", dst); |
| 689 | return false; |
| 690 | } |
| 691 | // dst is a POSIX path, so we don't want to use the sysdeps |
| 692 | // helpers here. |
| 693 | if (dst_dir.back() != '/') { |
| 694 | dst_dir.push_back('/'); |
| 695 | } |
| 696 | dst_dir.append(adb_basename(src_path)); |
| 697 | } |
| 698 | |
| 699 | success &= copy_local_dir_remote(sc, src_path, dst_dir.c_str(), |
| 700 | false, false); |
Josh Gao | 0578677 | 2015-10-30 16:57:19 -0700 | [diff] [blame] | 701 | continue; |
| 702 | } |
| 703 | |
| 704 | std::string path_holder; |
Josh Gao | 07db119 | 2015-11-07 15:38:19 -0800 | [diff] [blame] | 705 | if (dst_isdir) { |
Josh Gao | 0578677 | 2015-10-30 16:57:19 -0700 | [diff] [blame] | 706 | // If we're copying a local file to a remote directory, |
| 707 | // we really want to copy to remote_dir + "/" + local_filename. |
| 708 | path_holder = android::base::StringPrintf( |
| 709 | "%s/%s", dst_path, adb_basename(src_path).c_str()); |
| 710 | dst_path = path_holder.c_str(); |
| 711 | } |
| 712 | success &= sync_send(sc, src_path, dst_path, st.st_mtime, st.st_mode); |
| 713 | } |
| 714 | |
Elliott Hughes | b708d16 | 2015-10-27 16:03:15 -0700 | [diff] [blame] | 715 | sc.Print("\n"); |
Josh Gao | 0578677 | 2015-10-30 16:57:19 -0700 | [diff] [blame] | 716 | return success; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 717 | } |
| 718 | |
Josh Gao | cda6a2b | 2015-11-02 16:45:47 -0800 | [diff] [blame] | 719 | static bool remote_build_list(SyncConnection& sc, |
| 720 | std::vector<copyinfo>* filelist, |
Josh Gao | d973157 | 2015-11-03 15:23:03 -0800 | [diff] [blame] | 721 | const std::string& rpath, |
| 722 | const std::string& lpath) { |
Josh Gao | cda6a2b | 2015-11-02 16:45:47 -0800 | [diff] [blame] | 723 | std::vector<copyinfo> dirlist; |
Josh Gao | 6580096 | 2015-11-04 14:57:04 -0800 | [diff] [blame] | 724 | bool empty_dir = true; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 725 | |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 726 | // Put the files/dirs in rpath on the lists. |
Josh Gao | dd6cc4d | 2015-11-30 10:21:25 -0800 | [diff] [blame^] | 727 | auto callback = [&](unsigned mode, unsigned size, unsigned time, const char* name) { |
Josh Gao | 6580096 | 2015-11-04 14:57:04 -0800 | [diff] [blame] | 728 | if (IsDotOrDotDot(name)) { |
| 729 | return; |
| 730 | } |
Josh Gao | cda6a2b | 2015-11-02 16:45:47 -0800 | [diff] [blame] | 731 | |
Josh Gao | 6580096 | 2015-11-04 14:57:04 -0800 | [diff] [blame] | 732 | // We found a child that isn't '.' or '..'. |
| 733 | empty_dir = false; |
| 734 | |
Josh Gao | 1a02530 | 2015-11-09 11:12:14 -0800 | [diff] [blame] | 735 | copyinfo ci = mkcopyinfo(lpath, rpath, name, mode); |
Josh Gao | 6580096 | 2015-11-04 14:57:04 -0800 | [diff] [blame] | 736 | if (S_ISDIR(mode)) { |
| 737 | dirlist.push_back(ci); |
Josh Gao | cda6a2b | 2015-11-02 16:45:47 -0800 | [diff] [blame] | 738 | } else { |
Josh Gao | dd6cc4d | 2015-11-30 10:21:25 -0800 | [diff] [blame^] | 739 | if (S_ISREG(mode) || S_ISLNK(mode)) { |
| 740 | ci.time = time; |
| 741 | ci.size = size; |
| 742 | } else { |
| 743 | sc.Warning("skipping special file '%s'\n", name); |
| 744 | ci.skip = true; |
| 745 | } |
| 746 | filelist->push_back(ci); |
Josh Gao | cda6a2b | 2015-11-02 16:45:47 -0800 | [diff] [blame] | 747 | } |
| 748 | }; |
| 749 | |
Josh Gao | d973157 | 2015-11-03 15:23:03 -0800 | [diff] [blame] | 750 | if (!sync_ls(sc, rpath.c_str(), callback)) { |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 751 | return false; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 752 | } |
| 753 | |
Josh Gao | 6580096 | 2015-11-04 14:57:04 -0800 | [diff] [blame] | 754 | // Add the current directory to the list if it was empty, to ensure that |
| 755 | // it gets created. |
| 756 | if (empty_dir) { |
Josh Gao | 1a02530 | 2015-11-09 11:12:14 -0800 | [diff] [blame] | 757 | filelist->push_back(mkcopyinfo(adb_dirname(lpath), adb_dirname(rpath), |
Josh Gao | 6580096 | 2015-11-04 14:57:04 -0800 | [diff] [blame] | 758 | adb_basename(rpath), S_IFDIR)); |
| 759 | return true; |
| 760 | } |
| 761 | |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 762 | // Recurse into each directory we found. |
Josh Gao | cda6a2b | 2015-11-02 16:45:47 -0800 | [diff] [blame] | 763 | while (!dirlist.empty()) { |
| 764 | copyinfo current = dirlist.back(); |
| 765 | dirlist.pop_back(); |
Josh Gao | 1a02530 | 2015-11-09 11:12:14 -0800 | [diff] [blame] | 766 | if (!remote_build_list(sc, filelist, current.rpath, current.lpath)) { |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 767 | return false; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 768 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 769 | } |
| 770 | |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 771 | return true; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 772 | } |
| 773 | |
Josh Gao | 1a02530 | 2015-11-09 11:12:14 -0800 | [diff] [blame] | 774 | static int set_time_and_mode(const std::string& lpath, time_t time, |
| 775 | unsigned int mode) { |
Greg Hackmann | 7a5e2bd | 2014-05-06 08:48:18 -0700 | [diff] [blame] | 776 | struct utimbuf times = { time, time }; |
Josh Gao | 1a02530 | 2015-11-09 11:12:14 -0800 | [diff] [blame] | 777 | int r1 = utime(lpath.c_str(), ×); |
Lajos Molnar | de8ff4a | 2013-04-19 12:41:09 -0700 | [diff] [blame] | 778 | |
| 779 | /* use umask for permissions */ |
Josh Gao | cda6a2b | 2015-11-02 16:45:47 -0800 | [diff] [blame] | 780 | mode_t mask = umask(0000); |
Lajos Molnar | de8ff4a | 2013-04-19 12:41:09 -0700 | [diff] [blame] | 781 | umask(mask); |
Josh Gao | 1a02530 | 2015-11-09 11:12:14 -0800 | [diff] [blame] | 782 | int r2 = chmod(lpath.c_str(), mode & ~mask); |
Lajos Molnar | de8ff4a | 2013-04-19 12:41:09 -0700 | [diff] [blame] | 783 | |
Spencer Low | 363af56 | 2015-11-07 18:51:54 -0800 | [diff] [blame] | 784 | return r1 ? r1 : r2; |
Lajos Molnar | de8ff4a | 2013-04-19 12:41:09 -0700 | [diff] [blame] | 785 | } |
| 786 | |
Josh Gao | d973157 | 2015-11-03 15:23:03 -0800 | [diff] [blame] | 787 | static bool copy_remote_dir_local(SyncConnection& sc, std::string rpath, |
| 788 | std::string lpath, bool copy_attrs) { |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 789 | // Make sure that both directory paths end in a slash. |
Josh Gao | 12a2ae9 | 2015-11-07 15:27:26 -0800 | [diff] [blame] | 790 | // Both paths are known to be nonempty, so we don't need to check. |
Josh Gao | 1a02530 | 2015-11-09 11:12:14 -0800 | [diff] [blame] | 791 | ensure_trailing_separator(lpath, rpath); |
Riley Andrews | 4d04d24 | 2014-12-12 13:12:36 -0800 | [diff] [blame] | 792 | |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 793 | // Recursively build the list of files to copy. |
Elliott Hughes | b708d16 | 2015-10-27 16:03:15 -0700 | [diff] [blame] | 794 | sc.Print("pull: building file list..."); |
Josh Gao | cda6a2b | 2015-11-02 16:45:47 -0800 | [diff] [blame] | 795 | std::vector<copyinfo> filelist; |
Josh Gao | d973157 | 2015-11-03 15:23:03 -0800 | [diff] [blame] | 796 | if (!remote_build_list(sc, &filelist, rpath.c_str(), lpath.c_str())) { |
Josh Gao | cda6a2b | 2015-11-02 16:45:47 -0800 | [diff] [blame] | 797 | return false; |
| 798 | } |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 799 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 800 | int pulled = 0; |
| 801 | int skipped = 0; |
Josh Gao | cda6a2b | 2015-11-02 16:45:47 -0800 | [diff] [blame] | 802 | for (const copyinfo &ci : filelist) { |
Josh Gao | fc7c3b6 | 2015-11-03 14:44:04 -0800 | [diff] [blame] | 803 | if (!ci.skip) { |
Josh Gao | 1a02530 | 2015-11-09 11:12:14 -0800 | [diff] [blame] | 804 | sc.Printf("pull: %s -> %s", ci.rpath.c_str(), ci.lpath.c_str()); |
Josh Gao | 6580096 | 2015-11-04 14:57:04 -0800 | [diff] [blame] | 805 | |
| 806 | if (S_ISDIR(ci.mode)) { |
| 807 | // Entry is for an empty directory, create it and continue. |
| 808 | // TODO(b/25457350): We don't preserve permissions on directories. |
Josh Gao | 1a02530 | 2015-11-09 11:12:14 -0800 | [diff] [blame] | 809 | if (!mkdirs(ci.lpath)) { |
Josh Gao | 6580096 | 2015-11-04 14:57:04 -0800 | [diff] [blame] | 810 | sc.Error("failed to create directory '%s': %s", |
Josh Gao | 1a02530 | 2015-11-09 11:12:14 -0800 | [diff] [blame] | 811 | ci.lpath.c_str(), strerror(errno)); |
Josh Gao | 6580096 | 2015-11-04 14:57:04 -0800 | [diff] [blame] | 812 | return false; |
| 813 | } |
| 814 | pulled++; |
| 815 | continue; |
| 816 | } |
| 817 | |
Josh Gao | 1a02530 | 2015-11-09 11:12:14 -0800 | [diff] [blame] | 818 | if (!sync_recv(sc, ci.rpath.c_str(), ci.lpath.c_str())) { |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 819 | return false; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 820 | } |
Lajos Molnar | de8ff4a | 2013-04-19 12:41:09 -0700 | [diff] [blame] | 821 | |
Josh Gao | 1a02530 | 2015-11-09 11:12:14 -0800 | [diff] [blame] | 822 | if (copy_attrs && set_time_and_mode(ci.lpath, ci.time, ci.mode)) { |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 823 | return false; |
Lajos Molnar | de8ff4a | 2013-04-19 12:41:09 -0700 | [diff] [blame] | 824 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 825 | pulled++; |
| 826 | } else { |
| 827 | skipped++; |
| 828 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 829 | } |
| 830 | |
Josh Gao | d973157 | 2015-11-03 15:23:03 -0800 | [diff] [blame] | 831 | sc.Printf("%s: %d file%s pulled. %d file%s skipped.%s\n", rpath.c_str(), |
| 832 | pulled, (pulled == 1) ? "" : "s", skipped, |
| 833 | (skipped == 1) ? "" : "s", sc.TransferRate().c_str()); |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 834 | return true; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 835 | } |
| 836 | |
Josh Gao | 0578677 | 2015-10-30 16:57:19 -0700 | [diff] [blame] | 837 | bool do_sync_pull(const std::vector<const char*>& srcs, const char* dst, |
| 838 | bool copy_attrs) { |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 839 | SyncConnection sc; |
| 840 | if (!sc.IsValid()) return false; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 841 | |
Josh Gao | 0578677 | 2015-10-30 16:57:19 -0700 | [diff] [blame] | 842 | bool success = true; |
Josh Gao | 0578677 | 2015-10-30 16:57:19 -0700 | [diff] [blame] | 843 | struct stat st; |
Josh Gao | 12a2ae9 | 2015-11-07 15:27:26 -0800 | [diff] [blame] | 844 | bool dst_exists = true; |
| 845 | |
| 846 | if (stat(dst, &st) == -1) { |
| 847 | dst_exists = false; |
| 848 | |
| 849 | // If we're only pulling one path, the destination path might point to |
Josh Gao | 0578677 | 2015-10-30 16:57:19 -0700 | [diff] [blame] | 850 | // a path that doesn't exist yet. |
Josh Gao | 12a2ae9 | 2015-11-07 15:27:26 -0800 | [diff] [blame] | 851 | if (srcs.size() == 1 && errno == ENOENT) { |
| 852 | // However, its parent must exist. |
| 853 | struct stat parent_st; |
| 854 | if (stat(adb_dirname(dst).c_str(), &parent_st) == -1) { |
| 855 | sc.Error("cannot create file/directory '%s': %s", dst, strerror(errno)); |
| 856 | return false; |
| 857 | } |
| 858 | } else { |
| 859 | sc.Error("failed to access '%s': %s", dst, strerror(errno)); |
Josh Gao | 0578677 | 2015-10-30 16:57:19 -0700 | [diff] [blame] | 860 | return false; |
| 861 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 862 | } |
| 863 | |
Josh Gao | 07db119 | 2015-11-07 15:38:19 -0800 | [diff] [blame] | 864 | bool dst_isdir = dst_exists && S_ISDIR(st.st_mode); |
| 865 | if (!dst_isdir) { |
Josh Gao | 0578677 | 2015-10-30 16:57:19 -0700 | [diff] [blame] | 866 | if (srcs.size() > 1) { |
| 867 | sc.Error("target '%s' is not a directory", dst); |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 868 | return false; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 869 | } else { |
Josh Gao | 0578677 | 2015-10-30 16:57:19 -0700 | [diff] [blame] | 870 | size_t dst_len = strlen(dst); |
Josh Gao | 12a2ae9 | 2015-11-07 15:27:26 -0800 | [diff] [blame] | 871 | |
| 872 | // A path that ends with a slash doesn't have to be a directory if |
| 873 | // it doesn't exist yet. |
Josh Gao | 1a02530 | 2015-11-09 11:12:14 -0800 | [diff] [blame] | 874 | if (adb_is_separator(dst[dst_len - 1]) && dst_exists) { |
Josh Gao | 0578677 | 2015-10-30 16:57:19 -0700 | [diff] [blame] | 875 | sc.Error("failed to access '%s': Not a directory", dst); |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 876 | return false; |
Elliott Hughes | 5c74270 | 2015-07-30 17:42:01 -0700 | [diff] [blame] | 877 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 878 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 879 | } |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 880 | |
Josh Gao | 0578677 | 2015-10-30 16:57:19 -0700 | [diff] [blame] | 881 | for (const char* src_path : srcs) { |
| 882 | const char* dst_path = dst; |
Josh Gao | 12a2ae9 | 2015-11-07 15:27:26 -0800 | [diff] [blame] | 883 | unsigned src_mode, src_time; |
| 884 | if (!sync_stat(sc, src_path, &src_time, &src_mode, nullptr)) { |
| 885 | return false; |
| 886 | } |
| 887 | if (src_mode == 0) { |
Josh Gao | 0578677 | 2015-10-30 16:57:19 -0700 | [diff] [blame] | 888 | sc.Error("remote object '%s' does not exist", src_path); |
| 889 | success = false; |
| 890 | continue; |
| 891 | } |
| 892 | |
Josh Gao | f96dc73 | 2015-11-11 15:10:53 -0800 | [diff] [blame] | 893 | if (S_ISREG(src_mode)) { |
Josh Gao | 0578677 | 2015-10-30 16:57:19 -0700 | [diff] [blame] | 894 | std::string path_holder; |
Josh Gao | 07db119 | 2015-11-07 15:38:19 -0800 | [diff] [blame] | 895 | if (dst_isdir) { |
| 896 | // If we're copying a remote file to a local directory, we |
Josh Gao | 1a02530 | 2015-11-09 11:12:14 -0800 | [diff] [blame] | 897 | // really want to copy to local_dir + OS_PATH_SEPARATOR + |
| 898 | // basename(remote). |
Josh Gao | 07db119 | 2015-11-07 15:38:19 -0800 | [diff] [blame] | 899 | path_holder = android::base::StringPrintf( |
Josh Gao | 1a02530 | 2015-11-09 11:12:14 -0800 | [diff] [blame] | 900 | "%s%c%s", dst_path, OS_PATH_SEPARATOR, |
| 901 | adb_basename(src_path).c_str()); |
Josh Gao | 07db119 | 2015-11-07 15:38:19 -0800 | [diff] [blame] | 902 | dst_path = path_holder.c_str(); |
Josh Gao | 0578677 | 2015-10-30 16:57:19 -0700 | [diff] [blame] | 903 | } |
| 904 | if (!sync_recv(sc, src_path, dst_path)) { |
| 905 | success = false; |
| 906 | continue; |
| 907 | } else { |
Josh Gao | 12a2ae9 | 2015-11-07 15:27:26 -0800 | [diff] [blame] | 908 | if (copy_attrs && |
| 909 | set_time_and_mode(dst_path, src_time, src_mode) != 0) { |
Josh Gao | 0578677 | 2015-10-30 16:57:19 -0700 | [diff] [blame] | 910 | success = false; |
| 911 | continue; |
| 912 | } |
| 913 | } |
Josh Gao | 12a2ae9 | 2015-11-07 15:27:26 -0800 | [diff] [blame] | 914 | } else if (S_ISDIR(src_mode)) { |
Josh Gao | 07db119 | 2015-11-07 15:38:19 -0800 | [diff] [blame] | 915 | std::string dst_dir = dst; |
| 916 | |
| 917 | // If the destination path existed originally, the source directory |
| 918 | // should be copied as a child of the destination. |
| 919 | if (dst_exists) { |
| 920 | if (!dst_isdir) { |
| 921 | sc.Error("target '%s' is not a directory", dst); |
| 922 | return false; |
| 923 | } |
| 924 | if (!adb_is_separator(dst_dir.back())) { |
| 925 | dst_dir.push_back(OS_PATH_SEPARATOR); |
| 926 | } |
| 927 | dst_dir.append(adb_basename(src_path)); |
| 928 | } |
| 929 | |
| 930 | success &= copy_remote_dir_local(sc, src_path, dst_dir.c_str(), |
| 931 | copy_attrs); |
Josh Gao | 0578677 | 2015-10-30 16:57:19 -0700 | [diff] [blame] | 932 | continue; |
| 933 | } else { |
| 934 | sc.Error("remote object '%s' not a file or directory", src_path); |
| 935 | success = false; |
| 936 | continue; |
| 937 | } |
| 938 | } |
| 939 | |
| 940 | sc.Print("\n"); |
| 941 | return success; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 942 | } |
| 943 | |
Elliott Hughes | aa24549 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 944 | 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] | 945 | SyncConnection sc; |
| 946 | if (!sc.IsValid()) return false; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 947 | |
Josh Gao | d973157 | 2015-11-03 15:23:03 -0800 | [diff] [blame] | 948 | return copy_local_dir_remote(sc, lpath, rpath, true, list_only); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 949 | } |