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