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