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