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