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