The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2007 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Dan Albert | b302d12 | 2015-02-24 15:51:19 -0800 | [diff] [blame] | 17 | #include <dirent.h> |
| 18 | #include <errno.h> |
Spencer Low | 803451e | 2015-05-13 00:02:55 -0700 | [diff] [blame] | 19 | #include <inttypes.h> |
Dan Albert | b302d12 | 2015-02-24 15:51:19 -0800 | [diff] [blame] | 20 | #include <limits.h> |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 21 | #include <stdio.h> |
| 22 | #include <stdlib.h> |
| 23 | #include <string.h> |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 24 | #include <sys/stat.h> |
| 25 | #include <sys/time.h> |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 26 | #include <sys/types.h> |
Dan Albert | b302d12 | 2015-02-24 15:51:19 -0800 | [diff] [blame] | 27 | #include <time.h> |
Elliott Hughes | dde00be | 2015-09-27 12:55:37 -0700 | [diff] [blame] | 28 | #include <unistd.h> |
Greg Hackmann | 8b68914 | 2014-05-06 08:48:18 -0700 | [diff] [blame] | 29 | #include <utime.h> |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 30 | |
Josh Gao | 204d21e | 2015-11-02 16:45:47 -0800 | [diff] [blame] | 31 | #include <functional> |
Elliott Hughes | 4e7848d | 2015-08-24 14:49:43 -0700 | [diff] [blame] | 32 | #include <memory> |
Elliott Hughes | 6c73bfc | 2015-10-27 13:40:35 -0700 | [diff] [blame] | 33 | #include <vector> |
Elliott Hughes | 4e7848d | 2015-08-24 14:49:43 -0700 | [diff] [blame] | 34 | |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 35 | #include "sysdeps.h" |
Dan Albert | b302d12 | 2015-02-24 15:51:19 -0800 | [diff] [blame] | 36 | |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 37 | #include "adb.h" |
| 38 | #include "adb_client.h" |
Dan Albert | 66a91b0 | 2015-02-24 21:26:58 -0800 | [diff] [blame] | 39 | #include "adb_io.h" |
Alex Vallée | e916315 | 2015-05-06 17:22:25 -0400 | [diff] [blame] | 40 | #include "adb_utils.h" |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 41 | #include "file_sync_service.h" |
Elliott Hughes | f5cdc1d | 2015-10-27 16:03:15 -0700 | [diff] [blame] | 42 | #include "line_printer.h" |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 43 | |
Elliott Hughes | f55ead9 | 2015-12-04 22:00:26 -0800 | [diff] [blame] | 44 | #include <android-base/file.h> |
| 45 | #include <android-base/strings.h> |
| 46 | #include <android-base/stringprintf.h> |
Elliott Hughes | d189cfb | 2015-07-30 17:42:01 -0700 | [diff] [blame] | 47 | |
Elliott Hughes | b628cb1 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 48 | struct syncsendbuf { |
| 49 | unsigned id; |
| 50 | unsigned size; |
| 51 | char data[SYNC_DATA_MAX]; |
| 52 | }; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 53 | |
Elliott Hughes | 7b43fa5 | 2015-12-17 17:05:29 -0800 | [diff] [blame] | 54 | static void ensure_trailing_separators(std::string& local_path, std::string& remote_path) { |
| 55 | if (!adb_is_separator(local_path.back())) { |
| 56 | local_path.push_back(OS_PATH_SEPARATOR); |
| 57 | } |
| 58 | if (remote_path.back() != '/') { |
| 59 | remote_path.push_back('/'); |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | struct copyinfo { |
| 64 | std::string lpath; |
| 65 | std::string rpath; |
| 66 | unsigned int time = 0; |
| 67 | unsigned int mode; |
| 68 | uint64_t size = 0; |
| 69 | bool skip = false; |
| 70 | |
| 71 | copyinfo(const std::string& local_path, |
| 72 | const std::string& remote_path, |
| 73 | const std::string& name, |
| 74 | unsigned int mode) |
| 75 | : lpath(local_path), rpath(remote_path), mode(mode) { |
| 76 | ensure_trailing_separators(lpath, rpath); |
| 77 | lpath.append(name); |
| 78 | rpath.append(name); |
| 79 | if (S_ISDIR(mode)) { |
| 80 | ensure_trailing_separators(lpath, rpath); |
| 81 | } |
| 82 | } |
| 83 | }; |
| 84 | |
Elliott Hughes | b628cb1 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 85 | class SyncConnection { |
| 86 | public: |
Elliott Hughes | 7b43fa5 | 2015-12-17 17:05:29 -0800 | [diff] [blame] | 87 | SyncConnection() |
| 88 | : total_bytes_(0), |
| 89 | start_time_ms_(CurrentTimeMs()), |
| 90 | expected_total_bytes_(0), |
| 91 | expect_multiple_files_(false) { |
Elliott Hughes | b628cb1 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 92 | max = SYNC_DATA_MAX; // TODO: decide at runtime. |
| 93 | |
| 94 | std::string error; |
| 95 | fd = adb_connect("sync:", &error); |
| 96 | if (fd < 0) { |
Elliott Hughes | f5cdc1d | 2015-10-27 16:03:15 -0700 | [diff] [blame] | 97 | Error("connect failed: %s", error.c_str()); |
Elliott Hughes | b628cb1 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 98 | } |
| 99 | } |
| 100 | |
| 101 | ~SyncConnection() { |
| 102 | if (!IsValid()) return; |
| 103 | |
Spencer Low | cc4a4b1 | 2015-10-14 17:32:44 -0700 | [diff] [blame] | 104 | if (SendQuit()) { |
| 105 | // We sent a quit command, so the server should be doing orderly |
| 106 | // shutdown soon. But if we encountered an error while we were using |
| 107 | // the connection, the server might still be sending data (before |
| 108 | // doing orderly shutdown), in which case we won't wait for all of |
| 109 | // the data nor the coming orderly shutdown. In the common success |
| 110 | // case, this will wait for the server to do orderly shutdown. |
| 111 | ReadOrderlyShutdown(fd); |
| 112 | } |
Elliott Hughes | b628cb1 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 113 | adb_close(fd); |
Elliott Hughes | 78a37a5 | 2015-12-08 16:01:15 -0800 | [diff] [blame] | 114 | |
| 115 | line_printer_.KeepInfoLine(); |
Elliott Hughes | b628cb1 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | bool IsValid() { return fd >= 0; } |
| 119 | |
Elliott Hughes | dde00be | 2015-09-27 12:55:37 -0700 | [diff] [blame] | 120 | bool SendRequest(int id, const char* path_and_mode) { |
| 121 | size_t path_length = strlen(path_and_mode); |
| 122 | if (path_length > 1024) { |
Elliott Hughes | f5cdc1d | 2015-10-27 16:03:15 -0700 | [diff] [blame] | 123 | Error("SendRequest failed: path too long: %zu", path_length); |
Elliott Hughes | dde00be | 2015-09-27 12:55:37 -0700 | [diff] [blame] | 124 | errno = ENAMETOOLONG; |
| 125 | return false; |
| 126 | } |
| 127 | |
| 128 | // Sending header and payload in a single write makes a noticeable |
| 129 | // difference to "adb sync" performance. |
Elliott Hughes | 6c73bfc | 2015-10-27 13:40:35 -0700 | [diff] [blame] | 130 | std::vector<char> buf(sizeof(SyncRequest) + path_length); |
| 131 | SyncRequest* req = reinterpret_cast<SyncRequest*>(&buf[0]); |
Elliott Hughes | dde00be | 2015-09-27 12:55:37 -0700 | [diff] [blame] | 132 | req->id = id; |
| 133 | req->path_length = path_length; |
| 134 | char* data = reinterpret_cast<char*>(req + 1); |
| 135 | memcpy(data, path_and_mode, path_length); |
| 136 | |
Elliott Hughes | 6c73bfc | 2015-10-27 13:40:35 -0700 | [diff] [blame] | 137 | return WriteFdExactly(fd, &buf[0], buf.size()); |
Elliott Hughes | dde00be | 2015-09-27 12:55:37 -0700 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | // Sending header, payload, and footer in a single write makes a huge |
| 141 | // difference to "adb sync" performance. |
| 142 | bool SendSmallFile(const char* path_and_mode, |
Elliott Hughes | 54e3efe | 2015-11-20 22:01:06 -0800 | [diff] [blame] | 143 | const char* lpath, const char* rpath, |
Elliott Hughes | 7275d80 | 2015-11-20 17:35:17 -0800 | [diff] [blame] | 144 | unsigned mtime, |
| 145 | const char* data, size_t data_length) { |
Elliott Hughes | dde00be | 2015-09-27 12:55:37 -0700 | [diff] [blame] | 146 | size_t path_length = strlen(path_and_mode); |
| 147 | if (path_length > 1024) { |
Elliott Hughes | f5cdc1d | 2015-10-27 16:03:15 -0700 | [diff] [blame] | 148 | Error("SendSmallFile failed: path too long: %zu", path_length); |
Elliott Hughes | dde00be | 2015-09-27 12:55:37 -0700 | [diff] [blame] | 149 | errno = ENAMETOOLONG; |
| 150 | return false; |
| 151 | } |
| 152 | |
Elliott Hughes | 6c73bfc | 2015-10-27 13:40:35 -0700 | [diff] [blame] | 153 | std::vector<char> buf(sizeof(SyncRequest) + path_length + |
Elliott Hughes | 7275d80 | 2015-11-20 17:35:17 -0800 | [diff] [blame] | 154 | sizeof(SyncRequest) + data_length + |
| 155 | sizeof(SyncRequest)); |
Elliott Hughes | 6c73bfc | 2015-10-27 13:40:35 -0700 | [diff] [blame] | 156 | char* p = &buf[0]; |
Elliott Hughes | dde00be | 2015-09-27 12:55:37 -0700 | [diff] [blame] | 157 | |
| 158 | SyncRequest* req_send = reinterpret_cast<SyncRequest*>(p); |
| 159 | req_send->id = ID_SEND; |
| 160 | req_send->path_length = path_length; |
| 161 | p += sizeof(SyncRequest); |
| 162 | memcpy(p, path_and_mode, path_length); |
| 163 | p += path_length; |
| 164 | |
| 165 | SyncRequest* req_data = reinterpret_cast<SyncRequest*>(p); |
| 166 | req_data->id = ID_DATA; |
| 167 | req_data->path_length = data_length; |
| 168 | p += sizeof(SyncRequest); |
| 169 | memcpy(p, data, data_length); |
| 170 | p += data_length; |
| 171 | |
| 172 | SyncRequest* req_done = reinterpret_cast<SyncRequest*>(p); |
| 173 | req_done->id = ID_DONE; |
| 174 | req_done->path_length = mtime; |
| 175 | p += sizeof(SyncRequest); |
| 176 | |
Elliott Hughes | 54e3efe | 2015-11-20 22:01:06 -0800 | [diff] [blame] | 177 | WriteOrDie(lpath, rpath, &buf[0], (p - &buf[0])); |
Elliott Hughes | 7b43fa5 | 2015-12-17 17:05:29 -0800 | [diff] [blame] | 178 | total_bytes_ += data_length; |
Elliott Hughes | dde00be | 2015-09-27 12:55:37 -0700 | [diff] [blame] | 179 | return true; |
| 180 | } |
| 181 | |
Elliott Hughes | 7275d80 | 2015-11-20 17:35:17 -0800 | [diff] [blame] | 182 | bool SendLargeFile(const char* path_and_mode, |
| 183 | const char* lpath, const char* rpath, |
| 184 | unsigned mtime) { |
| 185 | if (!SendRequest(ID_SEND, path_and_mode)) { |
| 186 | Error("failed to send ID_SEND message '%s': %s", path_and_mode, strerror(errno)); |
| 187 | return false; |
| 188 | } |
| 189 | |
| 190 | struct stat st; |
| 191 | if (stat(lpath, &st) == -1) { |
| 192 | Error("cannot stat '%s': %s", lpath, strerror(errno)); |
| 193 | return false; |
| 194 | } |
| 195 | |
| 196 | uint64_t total_size = st.st_size; |
| 197 | uint64_t bytes_copied = 0; |
| 198 | |
| 199 | int lfd = adb_open(lpath, O_RDONLY); |
| 200 | if (lfd < 0) { |
Elliott Hughes | 54e3efe | 2015-11-20 22:01:06 -0800 | [diff] [blame] | 201 | Error("opening '%s' locally failed: %s", lpath, strerror(errno)); |
Elliott Hughes | 7275d80 | 2015-11-20 17:35:17 -0800 | [diff] [blame] | 202 | return false; |
| 203 | } |
| 204 | |
| 205 | syncsendbuf sbuf; |
| 206 | sbuf.id = ID_DATA; |
| 207 | while (true) { |
Elliott Hughes | 54e3efe | 2015-11-20 22:01:06 -0800 | [diff] [blame] | 208 | int bytes_read = adb_read(lfd, sbuf.data, max); |
| 209 | if (bytes_read == -1) { |
| 210 | Error("reading '%s' locally failed: %s", lpath, strerror(errno)); |
| 211 | adb_close(lfd); |
| 212 | return false; |
| 213 | } else if (bytes_read == 0) { |
Elliott Hughes | 7275d80 | 2015-11-20 17:35:17 -0800 | [diff] [blame] | 214 | break; |
| 215 | } |
| 216 | |
Elliott Hughes | 54e3efe | 2015-11-20 22:01:06 -0800 | [diff] [blame] | 217 | sbuf.size = bytes_read; |
| 218 | WriteOrDie(lpath, rpath, &sbuf, sizeof(SyncRequest) + bytes_read); |
Elliott Hughes | 7275d80 | 2015-11-20 17:35:17 -0800 | [diff] [blame] | 219 | |
Elliott Hughes | 7b43fa5 | 2015-12-17 17:05:29 -0800 | [diff] [blame] | 220 | total_bytes_ += bytes_read; |
Elliott Hughes | 54e3efe | 2015-11-20 22:01:06 -0800 | [diff] [blame] | 221 | bytes_copied += bytes_read; |
Elliott Hughes | 7275d80 | 2015-11-20 17:35:17 -0800 | [diff] [blame] | 222 | |
Elliott Hughes | 7b43fa5 | 2015-12-17 17:05:29 -0800 | [diff] [blame] | 223 | ReportProgress(rpath, bytes_copied, total_size); |
Elliott Hughes | 7275d80 | 2015-11-20 17:35:17 -0800 | [diff] [blame] | 224 | } |
| 225 | |
| 226 | adb_close(lfd); |
| 227 | |
| 228 | syncmsg msg; |
| 229 | msg.data.id = ID_DONE; |
| 230 | msg.data.size = mtime; |
Elliott Hughes | 54e3efe | 2015-11-20 22:01:06 -0800 | [diff] [blame] | 231 | return WriteOrDie(lpath, rpath, &msg.data, sizeof(msg.data)); |
Elliott Hughes | 7275d80 | 2015-11-20 17:35:17 -0800 | [diff] [blame] | 232 | } |
| 233 | |
Elliott Hughes | dde00be | 2015-09-27 12:55:37 -0700 | [diff] [blame] | 234 | bool CopyDone(const char* from, const char* to) { |
| 235 | syncmsg msg; |
| 236 | if (!ReadFdExactly(fd, &msg.status, sizeof(msg.status))) { |
Elliott Hughes | f5cdc1d | 2015-10-27 16:03:15 -0700 | [diff] [blame] | 237 | 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] | 238 | return false; |
| 239 | } |
| 240 | if (msg.status.id == ID_OKAY) { |
| 241 | return true; |
| 242 | } |
| 243 | if (msg.status.id != ID_FAIL) { |
Elliott Hughes | f5cdc1d | 2015-10-27 16:03:15 -0700 | [diff] [blame] | 244 | 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] | 245 | return false; |
| 246 | } |
Elliott Hughes | e4ed32f | 2015-10-23 21:06:11 -0700 | [diff] [blame] | 247 | return ReportCopyFailure(from, to, msg); |
| 248 | } |
| 249 | |
| 250 | bool ReportCopyFailure(const char* from, const char* to, const syncmsg& msg) { |
Elliott Hughes | 6c73bfc | 2015-10-27 13:40:35 -0700 | [diff] [blame] | 251 | std::vector<char> buf(msg.status.msglen + 1); |
| 252 | if (!ReadFdExactly(fd, &buf[0], msg.status.msglen)) { |
Elliott Hughes | f5cdc1d | 2015-10-27 16:03:15 -0700 | [diff] [blame] | 253 | Error("failed to copy '%s' to '%s'; failed to read reason (!): %s", |
| 254 | from, to, strerror(errno)); |
Elliott Hughes | dde00be | 2015-09-27 12:55:37 -0700 | [diff] [blame] | 255 | return false; |
| 256 | } |
Elliott Hughes | 6c73bfc | 2015-10-27 13:40:35 -0700 | [diff] [blame] | 257 | buf[msg.status.msglen] = 0; |
Elliott Hughes | f5cdc1d | 2015-10-27 16:03:15 -0700 | [diff] [blame] | 258 | Error("failed to copy '%s' to '%s': %s", from, to, &buf[0]); |
Elliott Hughes | dde00be | 2015-09-27 12:55:37 -0700 | [diff] [blame] | 259 | return false; |
| 260 | } |
| 261 | |
Elliott Hughes | f5cdc1d | 2015-10-27 16:03:15 -0700 | [diff] [blame] | 262 | std::string TransferRate() { |
| 263 | uint64_t ms = CurrentTimeMs() - start_time_ms_; |
Elliott Hughes | 7b43fa5 | 2015-12-17 17:05:29 -0800 | [diff] [blame] | 264 | if (total_bytes_ == 0 || ms == 0) return ""; |
Elliott Hughes | f5cdc1d | 2015-10-27 16:03:15 -0700 | [diff] [blame] | 265 | |
| 266 | double s = static_cast<double>(ms) / 1000LL; |
Elliott Hughes | 7b43fa5 | 2015-12-17 17:05:29 -0800 | [diff] [blame] | 267 | double rate = (static_cast<double>(total_bytes_) / s) / (1024*1024); |
Elliott Hughes | f5cdc1d | 2015-10-27 16:03:15 -0700 | [diff] [blame] | 268 | return android::base::StringPrintf(" %.1f MB/s (%" PRId64 " bytes in %.3fs)", |
Elliott Hughes | 7b43fa5 | 2015-12-17 17:05:29 -0800 | [diff] [blame] | 269 | rate, total_bytes_, s); |
Elliott Hughes | f5cdc1d | 2015-10-27 16:03:15 -0700 | [diff] [blame] | 270 | } |
| 271 | |
Elliott Hughes | 7b43fa5 | 2015-12-17 17:05:29 -0800 | [diff] [blame] | 272 | void ReportProgress(const char* file, uint64_t file_copied_bytes, uint64_t file_total_bytes) { |
| 273 | char overall_percentage_str[5] = "?"; |
| 274 | if (expected_total_bytes_ != 0) { |
| 275 | int overall_percentage = static_cast<int>(total_bytes_ * 100 / expected_total_bytes_); |
| 276 | // If we're pulling symbolic links, we'll pull the target of the link rather than |
| 277 | // just create a local link, and that will cause us to go over 100%. |
| 278 | if (overall_percentage <= 100) { |
| 279 | snprintf(overall_percentage_str, sizeof(overall_percentage_str), "%d%%", |
| 280 | overall_percentage); |
| 281 | } |
| 282 | } |
| 283 | |
| 284 | if (file_copied_bytes > file_total_bytes || file_total_bytes == 0) { |
| 285 | // This case can happen if we're racing against something that wrote to the file |
| 286 | // between our stat and our read, or if we're reading a magic file that lies about |
| 287 | // its size. Just show how much we've copied. |
| 288 | Printf("[%4s] %s: %" PRId64 "/?", overall_percentage_str, file, file_copied_bytes); |
| 289 | } else { |
| 290 | // If we're transferring multiple files, we want to know how far through the current |
| 291 | // file we are, as well as the overall percentage. |
| 292 | if (expect_multiple_files_) { |
| 293 | int file_percentage = static_cast<int>(file_copied_bytes * 100 / file_total_bytes); |
| 294 | Printf("[%4s] %s: %d%%", overall_percentage_str, file, file_percentage); |
| 295 | } else { |
| 296 | Printf("[%4s] %s", overall_percentage_str, file); |
| 297 | } |
| 298 | } |
Elliott Hughes | f5cdc1d | 2015-10-27 16:03:15 -0700 | [diff] [blame] | 299 | } |
| 300 | |
Josh Gao | cbf485f | 2015-11-02 17:15:57 -0800 | [diff] [blame] | 301 | void Printf(const char* fmt, ...) __attribute__((__format__(ADB_FORMAT_ARCHETYPE, 2, 3))) { |
| 302 | std::string s; |
Elliott Hughes | 7b43fa5 | 2015-12-17 17:05:29 -0800 | [diff] [blame] | 303 | |
Josh Gao | cbf485f | 2015-11-02 17:15:57 -0800 | [diff] [blame] | 304 | va_list ap; |
| 305 | va_start(ap, fmt); |
| 306 | android::base::StringAppendV(&s, fmt, ap); |
| 307 | va_end(ap); |
| 308 | |
Elliott Hughes | 7b43fa5 | 2015-12-17 17:05:29 -0800 | [diff] [blame] | 309 | line_printer_.Print(s, LinePrinter::INFO); |
Josh Gao | cbf485f | 2015-11-02 17:15:57 -0800 | [diff] [blame] | 310 | } |
| 311 | |
Elliott Hughes | f5cdc1d | 2015-10-27 16:03:15 -0700 | [diff] [blame] | 312 | void Error(const char* fmt, ...) __attribute__((__format__(ADB_FORMAT_ARCHETYPE, 2, 3))) { |
| 313 | std::string s = "adb: error: "; |
| 314 | |
| 315 | va_list ap; |
| 316 | va_start(ap, fmt); |
| 317 | android::base::StringAppendV(&s, fmt, ap); |
| 318 | va_end(ap); |
| 319 | |
Elliott Hughes | 78a37a5 | 2015-12-08 16:01:15 -0800 | [diff] [blame] | 320 | line_printer_.Print(s, LinePrinter::ERROR); |
Elliott Hughes | f5cdc1d | 2015-10-27 16:03:15 -0700 | [diff] [blame] | 321 | } |
| 322 | |
Josh Gao | a544cc6 | 2015-11-07 17:18:44 -0800 | [diff] [blame] | 323 | void Warning(const char* fmt, ...) __attribute__((__format__(ADB_FORMAT_ARCHETYPE, 2, 3))) { |
| 324 | std::string s = "adb: warning: "; |
| 325 | |
| 326 | va_list ap; |
| 327 | va_start(ap, fmt); |
| 328 | android::base::StringAppendV(&s, fmt, ap); |
| 329 | va_end(ap); |
| 330 | |
Elliott Hughes | 78a37a5 | 2015-12-08 16:01:15 -0800 | [diff] [blame] | 331 | line_printer_.Print(s, LinePrinter::WARNING); |
Josh Gao | a544cc6 | 2015-11-07 17:18:44 -0800 | [diff] [blame] | 332 | } |
| 333 | |
Elliott Hughes | 7b43fa5 | 2015-12-17 17:05:29 -0800 | [diff] [blame] | 334 | void ComputeExpectedTotalBytes(const std::vector<copyinfo>& file_list) { |
| 335 | expected_total_bytes_ = 0; |
| 336 | for (const copyinfo& ci : file_list) { |
| 337 | // Unfortunately, this doesn't work for symbolic links, because we'll copy the |
| 338 | // target of the link rather than just creating a link. (But ci.size is the link size.) |
| 339 | if (!ci.skip) expected_total_bytes_ += ci.size; |
| 340 | } |
| 341 | expect_multiple_files_ = true; |
| 342 | } |
| 343 | |
| 344 | void SetExpectedTotalBytes(uint64_t expected_total_bytes) { |
| 345 | expected_total_bytes_ = expected_total_bytes; |
| 346 | expect_multiple_files_ = false; |
| 347 | } |
| 348 | |
| 349 | uint64_t total_bytes_; |
Elliott Hughes | b628cb1 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 350 | |
| 351 | // TODO: add a char[max] buffer here, to replace syncsendbuf... |
| 352 | int fd; |
| 353 | size_t max; |
| 354 | |
| 355 | private: |
Elliott Hughes | f5cdc1d | 2015-10-27 16:03:15 -0700 | [diff] [blame] | 356 | uint64_t start_time_ms_; |
| 357 | |
Elliott Hughes | 7b43fa5 | 2015-12-17 17:05:29 -0800 | [diff] [blame] | 358 | uint64_t expected_total_bytes_; |
| 359 | bool expect_multiple_files_; |
| 360 | |
Elliott Hughes | f5cdc1d | 2015-10-27 16:03:15 -0700 | [diff] [blame] | 361 | LinePrinter line_printer_; |
Elliott Hughes | b628cb1 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 362 | |
Spencer Low | cc4a4b1 | 2015-10-14 17:32:44 -0700 | [diff] [blame] | 363 | bool SendQuit() { |
| 364 | return SendRequest(ID_QUIT, ""); // TODO: add a SendResponse? |
Elliott Hughes | b628cb1 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 365 | } |
| 366 | |
Elliott Hughes | 54e3efe | 2015-11-20 22:01:06 -0800 | [diff] [blame] | 367 | bool WriteOrDie(const char* from, const char* to, const void* data, size_t data_length) { |
| 368 | if (!WriteFdExactly(fd, data, data_length)) { |
| 369 | if (errno == ECONNRESET) { |
| 370 | // Assume adbd told us why it was closing the connection, and |
| 371 | // try to read failure reason from adbd. |
| 372 | syncmsg msg; |
| 373 | if (!ReadFdExactly(fd, &msg.status, sizeof(msg.status))) { |
| 374 | Error("failed to copy '%s' to '%s': no response: %s", from, to, strerror(errno)); |
| 375 | } else if (msg.status.id != ID_FAIL) { |
| 376 | Error("failed to copy '%s' to '%s': not ID_FAIL: %d", from, to, msg.status.id); |
| 377 | } else { |
| 378 | ReportCopyFailure(from, to, msg); |
| 379 | } |
| 380 | } else { |
| 381 | Error("%zu-byte write failed: %s", data_length, strerror(errno)); |
| 382 | } |
| 383 | _exit(1); |
| 384 | } |
| 385 | return true; |
| 386 | } |
| 387 | |
Elliott Hughes | f5cdc1d | 2015-10-27 16:03:15 -0700 | [diff] [blame] | 388 | static uint64_t CurrentTimeMs() { |
| 389 | struct timeval tv; |
| 390 | gettimeofday(&tv, 0); // (Not clock_gettime because of Mac/Windows.) |
| 391 | 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] | 392 | } |
| 393 | }; |
| 394 | |
Josh Gao | 204d21e | 2015-11-02 16:45:47 -0800 | [diff] [blame] | 395 | typedef void (sync_ls_cb)(unsigned mode, unsigned size, unsigned time, const char* name); |
Elliott Hughes | b628cb1 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 396 | |
Josh Gao | 204d21e | 2015-11-02 16:45:47 -0800 | [diff] [blame] | 397 | static bool sync_ls(SyncConnection& sc, const char* path, |
| 398 | std::function<sync_ls_cb> func) { |
Elliott Hughes | dde00be | 2015-09-27 12:55:37 -0700 | [diff] [blame] | 399 | if (!sc.SendRequest(ID_LIST, path)) return false; |
Elliott Hughes | b628cb1 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 400 | |
| 401 | while (true) { |
| 402 | syncmsg msg; |
Elliott Hughes | dde00be | 2015-09-27 12:55:37 -0700 | [diff] [blame] | 403 | if (!ReadFdExactly(sc.fd, &msg.dent, sizeof(msg.dent))) return false; |
Elliott Hughes | b628cb1 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 404 | |
| 405 | if (msg.dent.id == ID_DONE) return true; |
| 406 | if (msg.dent.id != ID_DENT) return false; |
| 407 | |
Elliott Hughes | 9e8e355 | 2015-08-24 14:27:03 -0700 | [diff] [blame] | 408 | size_t len = msg.dent.namelen; |
Elliott Hughes | b628cb1 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 409 | if (len > 256) return false; // TODO: resize buffer? continue? |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 410 | |
Elliott Hughes | d189cfb | 2015-07-30 17:42:01 -0700 | [diff] [blame] | 411 | char buf[257]; |
Elliott Hughes | dde00be | 2015-09-27 12:55:37 -0700 | [diff] [blame] | 412 | if (!ReadFdExactly(sc.fd, buf, len)) return false; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 413 | buf[len] = 0; |
| 414 | |
Josh Gao | 204d21e | 2015-11-02 16:45:47 -0800 | [diff] [blame] | 415 | func(msg.dent.mode, msg.dent.size, msg.dent.time, buf); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 416 | } |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 417 | } |
| 418 | |
Elliott Hughes | b628cb1 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 419 | static bool sync_finish_stat(SyncConnection& sc, unsigned int* timestamp, |
| 420 | unsigned int* mode, unsigned int* size) { |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 421 | syncmsg msg; |
Elliott Hughes | b628cb1 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 422 | if (!ReadFdExactly(sc.fd, &msg.stat, sizeof(msg.stat)) || msg.stat.id != ID_STAT) { |
| 423 | return false; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 424 | } |
| 425 | |
Elliott Hughes | 9e8e355 | 2015-08-24 14:27:03 -0700 | [diff] [blame] | 426 | if (timestamp) *timestamp = msg.stat.time; |
| 427 | if (mode) *mode = msg.stat.mode; |
| 428 | if (size) *size = msg.stat.size; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 429 | |
Elliott Hughes | b628cb1 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 430 | return true; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 431 | } |
| 432 | |
Elliott Hughes | b628cb1 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 433 | static bool sync_stat(SyncConnection& sc, const char* path, |
| 434 | unsigned int* timestamp, unsigned int* mode, unsigned int* size) { |
Elliott Hughes | dde00be | 2015-09-27 12:55:37 -0700 | [diff] [blame] | 435 | 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] | 436 | } |
| 437 | |
Elliott Hughes | dde00be | 2015-09-27 12:55:37 -0700 | [diff] [blame] | 438 | static bool sync_send(SyncConnection& sc, const char* lpath, const char* rpath, |
Elliott Hughes | f5cdc1d | 2015-10-27 16:03:15 -0700 | [diff] [blame] | 439 | unsigned mtime, mode_t mode) |
Elliott Hughes | dde00be | 2015-09-27 12:55:37 -0700 | [diff] [blame] | 440 | { |
| 441 | std::string path_and_mode = android::base::StringPrintf("%s,%d", rpath, mode); |
| 442 | |
| 443 | if (S_ISLNK(mode)) { |
| 444 | #if !defined(_WIN32) |
| 445 | char buf[PATH_MAX]; |
| 446 | ssize_t data_length = readlink(lpath, buf, PATH_MAX - 1); |
| 447 | if (data_length == -1) { |
Elliott Hughes | f5cdc1d | 2015-10-27 16:03:15 -0700 | [diff] [blame] | 448 | sc.Error("readlink '%s' failed: %s", lpath, strerror(errno)); |
Elliott Hughes | dde00be | 2015-09-27 12:55:37 -0700 | [diff] [blame] | 449 | return false; |
| 450 | } |
| 451 | buf[data_length++] = '\0'; |
| 452 | |
Elliott Hughes | 54e3efe | 2015-11-20 22:01:06 -0800 | [diff] [blame] | 453 | if (!sc.SendSmallFile(path_and_mode.c_str(), lpath, rpath, mtime, buf, data_length)) { |
| 454 | return false; |
| 455 | } |
Elliott Hughes | dde00be | 2015-09-27 12:55:37 -0700 | [diff] [blame] | 456 | return sc.CopyDone(lpath, rpath); |
| 457 | #endif |
| 458 | } |
| 459 | |
| 460 | if (!S_ISREG(mode)) { |
Elliott Hughes | f5cdc1d | 2015-10-27 16:03:15 -0700 | [diff] [blame] | 461 | sc.Error("local file '%s' has unsupported mode: 0o%o", lpath, mode); |
Elliott Hughes | dde00be | 2015-09-27 12:55:37 -0700 | [diff] [blame] | 462 | return false; |
| 463 | } |
| 464 | |
| 465 | struct stat st; |
| 466 | if (stat(lpath, &st) == -1) { |
Elliott Hughes | f5cdc1d | 2015-10-27 16:03:15 -0700 | [diff] [blame] | 467 | sc.Error("failed to stat local file '%s': %s", lpath, strerror(errno)); |
Elliott Hughes | dde00be | 2015-09-27 12:55:37 -0700 | [diff] [blame] | 468 | return false; |
| 469 | } |
| 470 | if (st.st_size < SYNC_DATA_MAX) { |
| 471 | std::string data; |
| 472 | if (!android::base::ReadFileToString(lpath, &data)) { |
Elliott Hughes | f5cdc1d | 2015-10-27 16:03:15 -0700 | [diff] [blame] | 473 | sc.Error("failed to read all of '%s': %s", lpath, strerror(errno)); |
Elliott Hughes | dde00be | 2015-09-27 12:55:37 -0700 | [diff] [blame] | 474 | return false; |
| 475 | } |
Elliott Hughes | 54e3efe | 2015-11-20 22:01:06 -0800 | [diff] [blame] | 476 | if (!sc.SendSmallFile(path_and_mode.c_str(), lpath, rpath, mtime, |
| 477 | data.data(), data.size())) { |
Elliott Hughes | f5cdc1d | 2015-10-27 16:03:15 -0700 | [diff] [blame] | 478 | return false; |
| 479 | } |
Elliott Hughes | dde00be | 2015-09-27 12:55:37 -0700 | [diff] [blame] | 480 | } else { |
Elliott Hughes | 7275d80 | 2015-11-20 17:35:17 -0800 | [diff] [blame] | 481 | if (!sc.SendLargeFile(path_and_mode.c_str(), lpath, rpath, mtime)) { |
Elliott Hughes | f5cdc1d | 2015-10-27 16:03:15 -0700 | [diff] [blame] | 482 | return false; |
| 483 | } |
Elliott Hughes | dde00be | 2015-09-27 12:55:37 -0700 | [diff] [blame] | 484 | } |
| 485 | return sc.CopyDone(lpath, rpath); |
| 486 | } |
| 487 | |
Elliott Hughes | f5cdc1d | 2015-10-27 16:03:15 -0700 | [diff] [blame] | 488 | static bool sync_recv(SyncConnection& sc, const char* rpath, const char* lpath) { |
Elliott Hughes | b628cb1 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 489 | unsigned size = 0; |
Elliott Hughes | f5cdc1d | 2015-10-27 16:03:15 -0700 | [diff] [blame] | 490 | if (!sync_stat(sc, rpath, nullptr, nullptr, &size)) return false; |
Mark Lindner | 9f9d145 | 2014-03-11 17:55:59 -0700 | [diff] [blame] | 491 | |
Elliott Hughes | dde00be | 2015-09-27 12:55:37 -0700 | [diff] [blame] | 492 | if (!sc.SendRequest(ID_RECV, rpath)) return false; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 493 | |
Elliott Hughes | e4ed32f | 2015-10-23 21:06:11 -0700 | [diff] [blame] | 494 | adb_unlink(lpath); |
Josh Gao | e3a1216 | 2015-11-17 14:08:20 -0800 | [diff] [blame] | 495 | const std::string dirpath = adb_dirname(lpath); |
| 496 | if (!mkdirs(dirpath.c_str())) { |
| 497 | sc.Error("failed to create parent directory '%s': %s", dirpath.c_str(), strerror(errno)); |
Josh Gao | fe1a612 | 2015-11-04 14:51:23 -0800 | [diff] [blame] | 498 | return false; |
| 499 | } |
| 500 | |
Elliott Hughes | e4ed32f | 2015-10-23 21:06:11 -0700 | [diff] [blame] | 501 | int lfd = adb_creat(lpath, 0644); |
| 502 | if (lfd < 0) { |
Elliott Hughes | f5cdc1d | 2015-10-27 16:03:15 -0700 | [diff] [blame] | 503 | sc.Error("cannot create '%s': %s", lpath, strerror(errno)); |
Elliott Hughes | e4ed32f | 2015-10-23 21:06:11 -0700 | [diff] [blame] | 504 | return false; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 505 | } |
| 506 | |
Elliott Hughes | f5cdc1d | 2015-10-27 16:03:15 -0700 | [diff] [blame] | 507 | uint64_t bytes_copied = 0; |
Elliott Hughes | b628cb1 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 508 | while (true) { |
Elliott Hughes | e4ed32f | 2015-10-23 21:06:11 -0700 | [diff] [blame] | 509 | syncmsg msg; |
Elliott Hughes | dde00be | 2015-09-27 12:55:37 -0700 | [diff] [blame] | 510 | if (!ReadFdExactly(sc.fd, &msg.data, sizeof(msg.data))) { |
Spencer Low | c1a3133 | 2015-08-28 01:07:30 -0700 | [diff] [blame] | 511 | adb_close(lfd); |
Elliott Hughes | e4ed32f | 2015-10-23 21:06:11 -0700 | [diff] [blame] | 512 | adb_unlink(lpath); |
Elliott Hughes | dde00be | 2015-09-27 12:55:37 -0700 | [diff] [blame] | 513 | return false; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 514 | } |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 515 | |
Elliott Hughes | e4ed32f | 2015-10-23 21:06:11 -0700 | [diff] [blame] | 516 | if (msg.data.id == ID_DONE) break; |
| 517 | |
| 518 | if (msg.data.id != ID_DATA) { |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 519 | adb_close(lfd); |
Elliott Hughes | e4ed32f | 2015-10-23 21:06:11 -0700 | [diff] [blame] | 520 | adb_unlink(lpath); |
| 521 | sc.ReportCopyFailure(rpath, lpath, msg); |
Elliott Hughes | dde00be | 2015-09-27 12:55:37 -0700 | [diff] [blame] | 522 | return false; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 523 | } |
| 524 | |
Elliott Hughes | e4ed32f | 2015-10-23 21:06:11 -0700 | [diff] [blame] | 525 | if (msg.data.size > sc.max) { |
Elliott Hughes | f5cdc1d | 2015-10-27 16:03:15 -0700 | [diff] [blame] | 526 | 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] | 527 | adb_close(lfd); |
Elliott Hughes | e4ed32f | 2015-10-23 21:06:11 -0700 | [diff] [blame] | 528 | adb_unlink(lpath); |
Elliott Hughes | dde00be | 2015-09-27 12:55:37 -0700 | [diff] [blame] | 529 | return false; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 530 | } |
| 531 | |
Elliott Hughes | e4ed32f | 2015-10-23 21:06:11 -0700 | [diff] [blame] | 532 | char buffer[SYNC_DATA_MAX]; |
| 533 | if (!ReadFdExactly(sc.fd, buffer, msg.data.size)) { |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 534 | adb_close(lfd); |
Elliott Hughes | e4ed32f | 2015-10-23 21:06:11 -0700 | [diff] [blame] | 535 | adb_unlink(lpath); |
Elliott Hughes | dde00be | 2015-09-27 12:55:37 -0700 | [diff] [blame] | 536 | return false; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 537 | } |
| 538 | |
Elliott Hughes | e4ed32f | 2015-10-23 21:06:11 -0700 | [diff] [blame] | 539 | if (!WriteFdExactly(lfd, buffer, msg.data.size)) { |
Elliott Hughes | f5cdc1d | 2015-10-27 16:03:15 -0700 | [diff] [blame] | 540 | sc.Error("cannot write '%s': %s", lpath, strerror(errno)); |
Elliott Hughes | e4ed32f | 2015-10-23 21:06:11 -0700 | [diff] [blame] | 541 | adb_close(lfd); |
| 542 | adb_unlink(lpath); |
| 543 | return false; |
| 544 | } |
| 545 | |
Elliott Hughes | 7b43fa5 | 2015-12-17 17:05:29 -0800 | [diff] [blame] | 546 | sc.total_bytes_ += msg.data.size; |
Mark Lindner | 9f9d145 | 2014-03-11 17:55:59 -0700 | [diff] [blame] | 547 | |
Elliott Hughes | f5cdc1d | 2015-10-27 16:03:15 -0700 | [diff] [blame] | 548 | bytes_copied += msg.data.size; |
| 549 | |
Elliott Hughes | 7b43fa5 | 2015-12-17 17:05:29 -0800 | [diff] [blame] | 550 | sc.ReportProgress(rpath, bytes_copied, size); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 551 | } |
| 552 | |
| 553 | adb_close(lfd); |
Elliott Hughes | dde00be | 2015-09-27 12:55:37 -0700 | [diff] [blame] | 554 | return true; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 555 | } |
| 556 | |
Elliott Hughes | b628cb1 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 557 | bool do_sync_ls(const char* path) { |
| 558 | SyncConnection sc; |
| 559 | if (!sc.IsValid()) return false; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 560 | |
Josh Gao | 204d21e | 2015-11-02 16:45:47 -0800 | [diff] [blame] | 561 | return sync_ls(sc, path, [](unsigned mode, unsigned size, unsigned time, |
| 562 | const char* name) { |
| 563 | printf("%08x %08x %08x %s\n", mode, size, time, name); |
| 564 | }); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 565 | } |
| 566 | |
Elliott Hughes | b628cb1 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 567 | static bool IsDotOrDotDot(const char* name) { |
| 568 | return name[0] == '.' && (name[1] == '\0' || (name[1] == '.' && name[2] == '\0')); |
| 569 | } |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 570 | |
Elliott Hughes | 7b43fa5 | 2015-12-17 17:05:29 -0800 | [diff] [blame] | 571 | static bool local_build_list(SyncConnection& sc, std::vector<copyinfo>* file_list, |
Josh Gao | 8efa646 | 2015-11-03 15:26:38 -0800 | [diff] [blame] | 572 | const std::string& lpath, |
| 573 | const std::string& rpath) { |
Josh Gao | 204d21e | 2015-11-02 16:45:47 -0800 | [diff] [blame] | 574 | std::vector<copyinfo> dirlist; |
Josh Gao | d20cf38 | 2015-11-03 15:23:03 -0800 | [diff] [blame] | 575 | std::unique_ptr<DIR, int (*)(DIR*)> dir(opendir(lpath.c_str()), closedir); |
Elliott Hughes | b628cb1 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 576 | if (!dir) { |
Josh Gao | d20cf38 | 2015-11-03 15:23:03 -0800 | [diff] [blame] | 577 | sc.Error("cannot open '%s': %s", lpath.c_str(), strerror(errno)); |
Josh Gao | 8efa646 | 2015-11-03 15:26:38 -0800 | [diff] [blame] | 578 | return false; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 579 | } |
| 580 | |
Josh Gao | a90563e | 2015-11-04 14:57:04 -0800 | [diff] [blame] | 581 | bool empty_dir = true; |
Elliott Hughes | f5cdc1d | 2015-10-27 16:03:15 -0700 | [diff] [blame] | 582 | dirent* de; |
Elliott Hughes | b628cb1 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 583 | while ((de = readdir(dir.get()))) { |
Josh Gao | a90563e | 2015-11-04 14:57:04 -0800 | [diff] [blame] | 584 | if (IsDotOrDotDot(de->d_name)) { |
| 585 | continue; |
| 586 | } |
Elliott Hughes | b628cb1 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 587 | |
Josh Gao | a90563e | 2015-11-04 14:57:04 -0800 | [diff] [blame] | 588 | empty_dir = false; |
Josh Gao | d20cf38 | 2015-11-03 15:23:03 -0800 | [diff] [blame] | 589 | std::string stat_path = lpath + de->d_name; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 590 | |
Elliott Hughes | b628cb1 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 591 | struct stat st; |
Josh Gao | a5cea71 | 2015-11-07 15:27:26 -0800 | [diff] [blame] | 592 | if (lstat(stat_path.c_str(), &st) == -1) { |
Josh Gao | d20cf38 | 2015-11-03 15:23:03 -0800 | [diff] [blame] | 593 | sc.Error("cannot lstat '%s': %s", stat_path.c_str(), |
| 594 | strerror(errno)); |
Josh Gao | a5cea71 | 2015-11-07 15:27:26 -0800 | [diff] [blame] | 595 | continue; |
| 596 | } |
| 597 | |
Josh Gao | d9a2fd6 | 2015-12-09 14:03:30 -0800 | [diff] [blame] | 598 | copyinfo ci(lpath, rpath, de->d_name, st.st_mode); |
Josh Gao | a5cea71 | 2015-11-07 15:27:26 -0800 | [diff] [blame] | 599 | if (S_ISDIR(st.st_mode)) { |
| 600 | dirlist.push_back(ci); |
| 601 | } else { |
| 602 | if (!S_ISREG(st.st_mode) && !S_ISLNK(st.st_mode)) { |
| 603 | sc.Error("skipping special file '%s'", lpath.c_str()); |
Josh Gao | b3cab93 | 2015-11-30 10:21:25 -0800 | [diff] [blame] | 604 | ci.skip = true; |
Josh Gao | a5cea71 | 2015-11-07 15:27:26 -0800 | [diff] [blame] | 605 | } else { |
| 606 | ci.time = st.st_mtime; |
| 607 | ci.size = st.st_size; |
Josh Gao | a5cea71 | 2015-11-07 15:27:26 -0800 | [diff] [blame] | 608 | } |
Elliott Hughes | 7b43fa5 | 2015-12-17 17:05:29 -0800 | [diff] [blame] | 609 | file_list->push_back(ci); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 610 | } |
| 611 | } |
| 612 | |
Elliott Hughes | b628cb1 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 613 | // Close this directory and recurse. |
| 614 | dir.reset(); |
Josh Gao | a90563e | 2015-11-04 14:57:04 -0800 | [diff] [blame] | 615 | |
| 616 | // Add the current directory to the list if it was empty, to ensure that |
| 617 | // it gets created. |
| 618 | if (empty_dir) { |
| 619 | // TODO(b/25566053): Make pushing empty directories work. |
| 620 | // TODO(b/25457350): We don't preserve permissions on directories. |
Josh Gao | a544cc6 | 2015-11-07 17:18:44 -0800 | [diff] [blame] | 621 | sc.Warning("skipping empty directory '%s'", lpath.c_str()); |
Josh Gao | d9a2fd6 | 2015-12-09 14:03:30 -0800 | [diff] [blame] | 622 | copyinfo ci(adb_dirname(lpath), adb_dirname(rpath), adb_basename(lpath), S_IFDIR); |
Josh Gao | a90563e | 2015-11-04 14:57:04 -0800 | [diff] [blame] | 623 | ci.skip = true; |
Elliott Hughes | 7b43fa5 | 2015-12-17 17:05:29 -0800 | [diff] [blame] | 624 | file_list->push_back(ci); |
Josh Gao | a90563e | 2015-11-04 14:57:04 -0800 | [diff] [blame] | 625 | return true; |
| 626 | } |
| 627 | |
Josh Gao | 204d21e | 2015-11-02 16:45:47 -0800 | [diff] [blame] | 628 | for (const copyinfo& ci : dirlist) { |
Elliott Hughes | 7b43fa5 | 2015-12-17 17:05:29 -0800 | [diff] [blame] | 629 | local_build_list(sc, file_list, ci.lpath, ci.rpath); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 630 | } |
| 631 | |
Josh Gao | 8efa646 | 2015-11-03 15:26:38 -0800 | [diff] [blame] | 632 | return true; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 633 | } |
| 634 | |
Josh Gao | d20cf38 | 2015-11-03 15:23:03 -0800 | [diff] [blame] | 635 | static bool copy_local_dir_remote(SyncConnection& sc, std::string lpath, |
| 636 | std::string rpath, bool check_timestamps, |
| 637 | bool list_only) { |
| 638 | // Make sure that both directory paths end in a slash. |
Josh Gao | a3b6a06 | 2015-11-09 11:12:14 -0800 | [diff] [blame] | 639 | // Both paths are known to be nonempty, so we don't need to check. |
Elliott Hughes | 7b43fa5 | 2015-12-17 17:05:29 -0800 | [diff] [blame] | 640 | ensure_trailing_separators(lpath, rpath); |
Josh Gao | d20cf38 | 2015-11-03 15:23:03 -0800 | [diff] [blame] | 641 | |
| 642 | // Recursively build the list of files to copy. |
Elliott Hughes | 7b43fa5 | 2015-12-17 17:05:29 -0800 | [diff] [blame] | 643 | std::vector<copyinfo> file_list; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 644 | int pushed = 0; |
| 645 | int skipped = 0; |
Elliott Hughes | 7b43fa5 | 2015-12-17 17:05:29 -0800 | [diff] [blame] | 646 | if (!local_build_list(sc, &file_list, lpath, rpath)) { |
Elliott Hughes | b628cb1 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 647 | return false; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 648 | } |
| 649 | |
Elliott Hughes | b628cb1 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 650 | if (check_timestamps) { |
Elliott Hughes | 7b43fa5 | 2015-12-17 17:05:29 -0800 | [diff] [blame] | 651 | for (const copyinfo& ci : file_list) { |
Josh Gao | a3b6a06 | 2015-11-09 11:12:14 -0800 | [diff] [blame] | 652 | if (!sc.SendRequest(ID_STAT, ci.rpath.c_str())) { |
Josh Gao | d20cf38 | 2015-11-03 15:23:03 -0800 | [diff] [blame] | 653 | return false; |
| 654 | } |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 655 | } |
Elliott Hughes | 7b43fa5 | 2015-12-17 17:05:29 -0800 | [diff] [blame] | 656 | for (copyinfo& ci : file_list) { |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 657 | unsigned int timestamp, mode, size; |
Josh Gao | d20cf38 | 2015-11-03 15:23:03 -0800 | [diff] [blame] | 658 | if (!sync_finish_stat(sc, ×tamp, &mode, &size)) { |
| 659 | return false; |
| 660 | } |
Josh Gao | 204d21e | 2015-11-02 16:45:47 -0800 | [diff] [blame] | 661 | if (size == ci.size) { |
Elliott Hughes | 7b43fa5 | 2015-12-17 17:05:29 -0800 | [diff] [blame] | 662 | // For links, we cannot update the atime/mtime. |
Josh Gao | 204d21e | 2015-11-02 16:45:47 -0800 | [diff] [blame] | 663 | if ((S_ISREG(ci.mode & mode) && timestamp == ci.time) || |
| 664 | (S_ISLNK(ci.mode & mode) && timestamp >= ci.time)) { |
Josh Gao | cb094c6 | 2015-11-03 14:44:04 -0800 | [diff] [blame] | 665 | ci.skip = true; |
Elliott Hughes | b628cb1 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 666 | } |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 667 | } |
| 668 | } |
| 669 | } |
Josh Gao | 204d21e | 2015-11-02 16:45:47 -0800 | [diff] [blame] | 670 | |
Elliott Hughes | 7b43fa5 | 2015-12-17 17:05:29 -0800 | [diff] [blame] | 671 | sc.ComputeExpectedTotalBytes(file_list); |
| 672 | |
| 673 | for (const copyinfo& ci : file_list) { |
Josh Gao | cb094c6 | 2015-11-03 14:44:04 -0800 | [diff] [blame] | 674 | if (!ci.skip) { |
Elliott Hughes | f5cdc1d | 2015-10-27 16:03:15 -0700 | [diff] [blame] | 675 | if (list_only) { |
Elliott Hughes | 7b43fa5 | 2015-12-17 17:05:29 -0800 | [diff] [blame] | 676 | sc.Error("would push: %s -> %s", ci.lpath.c_str(), ci.rpath.c_str()); |
Elliott Hughes | f5cdc1d | 2015-10-27 16:03:15 -0700 | [diff] [blame] | 677 | } else { |
Elliott Hughes | 7b43fa5 | 2015-12-17 17:05:29 -0800 | [diff] [blame] | 678 | if (!sync_send(sc, ci.lpath.c_str(), ci.rpath.c_str(), ci.time, ci.mode)) { |
Josh Gao | d20cf38 | 2015-11-03 15:23:03 -0800 | [diff] [blame] | 679 | return false; |
Elliott Hughes | f5cdc1d | 2015-10-27 16:03:15 -0700 | [diff] [blame] | 680 | } |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 681 | } |
| 682 | pushed++; |
| 683 | } else { |
| 684 | skipped++; |
| 685 | } |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 686 | } |
| 687 | |
Elliott Hughes | 78a37a5 | 2015-12-08 16:01:15 -0800 | [diff] [blame] | 688 | sc.Printf("%s: %d file%s pushed. %d file%s skipped.%s", rpath.c_str(), |
Josh Gao | d20cf38 | 2015-11-03 15:23:03 -0800 | [diff] [blame] | 689 | pushed, (pushed == 1) ? "" : "s", skipped, |
| 690 | (skipped == 1) ? "" : "s", sc.TransferRate().c_str()); |
Elliott Hughes | b628cb1 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 691 | return true; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 692 | } |
| 693 | |
Josh Gao | 5d093b2 | 2015-10-30 16:57:19 -0700 | [diff] [blame] | 694 | bool do_sync_push(const std::vector<const char*>& srcs, const char* dst) { |
Elliott Hughes | b628cb1 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 695 | SyncConnection sc; |
| 696 | if (!sc.IsValid()) return false; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 697 | |
Josh Gao | 5d093b2 | 2015-10-30 16:57:19 -0700 | [diff] [blame] | 698 | bool success = true; |
Josh Gao | a5cea71 | 2015-11-07 15:27:26 -0800 | [diff] [blame] | 699 | unsigned dst_mode; |
| 700 | if (!sync_stat(sc, dst, nullptr, &dst_mode, nullptr)) return false; |
| 701 | bool dst_exists = (dst_mode != 0); |
Josh Gao | 8acf06c | 2015-11-07 15:38:19 -0800 | [diff] [blame] | 702 | bool dst_isdir = S_ISDIR(dst_mode); |
Josh Gao | 5d093b2 | 2015-10-30 16:57:19 -0700 | [diff] [blame] | 703 | |
Josh Gao | 8acf06c | 2015-11-07 15:38:19 -0800 | [diff] [blame] | 704 | if (!dst_isdir) { |
Josh Gao | 5d093b2 | 2015-10-30 16:57:19 -0700 | [diff] [blame] | 705 | if (srcs.size() > 1) { |
| 706 | sc.Error("target '%s' is not a directory", dst); |
| 707 | return false; |
| 708 | } else { |
| 709 | size_t dst_len = strlen(dst); |
Josh Gao | a5cea71 | 2015-11-07 15:27:26 -0800 | [diff] [blame] | 710 | |
| 711 | // A path that ends with a slash doesn't have to be a directory if |
| 712 | // it doesn't exist yet. |
| 713 | if (dst[dst_len - 1] == '/' && dst_exists) { |
Josh Gao | 5d093b2 | 2015-10-30 16:57:19 -0700 | [diff] [blame] | 714 | sc.Error("failed to access '%s': Not a directory", dst); |
| 715 | return false; |
| 716 | } |
| 717 | } |
Elliott Hughes | b628cb1 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 718 | } |
Josh Gao | 5d093b2 | 2015-10-30 16:57:19 -0700 | [diff] [blame] | 719 | |
| 720 | for (const char* src_path : srcs) { |
| 721 | const char* dst_path = dst; |
| 722 | struct stat st; |
Josh Gao | a5cea71 | 2015-11-07 15:27:26 -0800 | [diff] [blame] | 723 | if (stat(src_path, &st) == -1) { |
Josh Gao | 5d093b2 | 2015-10-30 16:57:19 -0700 | [diff] [blame] | 724 | sc.Error("cannot stat '%s': %s", src_path, strerror(errno)); |
| 725 | success = false; |
| 726 | continue; |
| 727 | } |
| 728 | |
| 729 | if (S_ISDIR(st.st_mode)) { |
Josh Gao | 8acf06c | 2015-11-07 15:38:19 -0800 | [diff] [blame] | 730 | std::string dst_dir = dst; |
| 731 | |
| 732 | // If the destination path existed originally, the source directory |
| 733 | // should be copied as a child of the destination. |
| 734 | if (dst_exists) { |
| 735 | if (!dst_isdir) { |
| 736 | sc.Error("target '%s' is not a directory", dst); |
| 737 | return false; |
| 738 | } |
| 739 | // dst is a POSIX path, so we don't want to use the sysdeps |
| 740 | // helpers here. |
| 741 | if (dst_dir.back() != '/') { |
| 742 | dst_dir.push_back('/'); |
| 743 | } |
| 744 | dst_dir.append(adb_basename(src_path)); |
| 745 | } |
| 746 | |
| 747 | success &= copy_local_dir_remote(sc, src_path, dst_dir.c_str(), |
| 748 | false, false); |
Josh Gao | 5d093b2 | 2015-10-30 16:57:19 -0700 | [diff] [blame] | 749 | continue; |
| 750 | } |
| 751 | |
| 752 | std::string path_holder; |
Josh Gao | 8acf06c | 2015-11-07 15:38:19 -0800 | [diff] [blame] | 753 | if (dst_isdir) { |
Josh Gao | 5d093b2 | 2015-10-30 16:57:19 -0700 | [diff] [blame] | 754 | // If we're copying a local file to a remote directory, |
| 755 | // we really want to copy to remote_dir + "/" + local_filename. |
Josh Gao | 2e4ed0a | 2016-02-03 14:55:24 -0800 | [diff] [blame] | 756 | path_holder = dst_path; |
| 757 | if (path_holder.back() != '/') { |
| 758 | path_holder.push_back('/'); |
| 759 | } |
| 760 | path_holder += adb_basename(src_path); |
Josh Gao | 5d093b2 | 2015-10-30 16:57:19 -0700 | [diff] [blame] | 761 | dst_path = path_holder.c_str(); |
| 762 | } |
Elliott Hughes | 7b43fa5 | 2015-12-17 17:05:29 -0800 | [diff] [blame] | 763 | sc.SetExpectedTotalBytes(st.st_size); |
Josh Gao | 5d093b2 | 2015-10-30 16:57:19 -0700 | [diff] [blame] | 764 | success &= sync_send(sc, src_path, dst_path, st.st_mtime, st.st_mode); |
| 765 | } |
| 766 | |
Josh Gao | 5d093b2 | 2015-10-30 16:57:19 -0700 | [diff] [blame] | 767 | return success; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 768 | } |
| 769 | |
Josh Gao | d9a2fd6 | 2015-12-09 14:03:30 -0800 | [diff] [blame] | 770 | static bool remote_symlink_isdir(SyncConnection& sc, const std::string& rpath) { |
| 771 | unsigned mode; |
| 772 | std::string dir_path = rpath; |
| 773 | dir_path.push_back('/'); |
| 774 | if (!sync_stat(sc, dir_path.c_str(), nullptr, &mode, nullptr)) { |
| 775 | sc.Error("failed to stat remote symlink '%s'", dir_path.c_str()); |
| 776 | return false; |
| 777 | } |
| 778 | return S_ISDIR(mode); |
| 779 | } |
| 780 | |
Josh Gao | 204d21e | 2015-11-02 16:45:47 -0800 | [diff] [blame] | 781 | static bool remote_build_list(SyncConnection& sc, |
Elliott Hughes | 7b43fa5 | 2015-12-17 17:05:29 -0800 | [diff] [blame] | 782 | std::vector<copyinfo>* file_list, |
Josh Gao | d20cf38 | 2015-11-03 15:23:03 -0800 | [diff] [blame] | 783 | const std::string& rpath, |
| 784 | const std::string& lpath) { |
Josh Gao | 204d21e | 2015-11-02 16:45:47 -0800 | [diff] [blame] | 785 | std::vector<copyinfo> dirlist; |
Josh Gao | d9a2fd6 | 2015-12-09 14:03:30 -0800 | [diff] [blame] | 786 | std::vector<copyinfo> linklist; |
Josh Gao | a90563e | 2015-11-04 14:57:04 -0800 | [diff] [blame] | 787 | bool empty_dir = true; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 788 | |
Elliott Hughes | b628cb1 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 789 | // Put the files/dirs in rpath on the lists. |
Josh Gao | b3cab93 | 2015-11-30 10:21:25 -0800 | [diff] [blame] | 790 | auto callback = [&](unsigned mode, unsigned size, unsigned time, const char* name) { |
Josh Gao | a90563e | 2015-11-04 14:57:04 -0800 | [diff] [blame] | 791 | if (IsDotOrDotDot(name)) { |
| 792 | return; |
| 793 | } |
Josh Gao | 204d21e | 2015-11-02 16:45:47 -0800 | [diff] [blame] | 794 | |
Josh Gao | a90563e | 2015-11-04 14:57:04 -0800 | [diff] [blame] | 795 | // We found a child that isn't '.' or '..'. |
| 796 | empty_dir = false; |
| 797 | |
Josh Gao | d9a2fd6 | 2015-12-09 14:03:30 -0800 | [diff] [blame] | 798 | copyinfo ci(lpath, rpath, name, mode); |
Josh Gao | a90563e | 2015-11-04 14:57:04 -0800 | [diff] [blame] | 799 | if (S_ISDIR(mode)) { |
| 800 | dirlist.push_back(ci); |
Josh Gao | d9a2fd6 | 2015-12-09 14:03:30 -0800 | [diff] [blame] | 801 | } else if (S_ISLNK(mode)) { |
| 802 | linklist.push_back(ci); |
Josh Gao | 204d21e | 2015-11-02 16:45:47 -0800 | [diff] [blame] | 803 | } else { |
Josh Gao | d9a2fd6 | 2015-12-09 14:03:30 -0800 | [diff] [blame] | 804 | ci.time = time; |
| 805 | ci.size = size; |
Elliott Hughes | 7b43fa5 | 2015-12-17 17:05:29 -0800 | [diff] [blame] | 806 | file_list->push_back(ci); |
Josh Gao | 204d21e | 2015-11-02 16:45:47 -0800 | [diff] [blame] | 807 | } |
| 808 | }; |
| 809 | |
Josh Gao | d20cf38 | 2015-11-03 15:23:03 -0800 | [diff] [blame] | 810 | if (!sync_ls(sc, rpath.c_str(), callback)) { |
Elliott Hughes | b628cb1 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 811 | return false; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 812 | } |
| 813 | |
Josh Gao | d9a2fd6 | 2015-12-09 14:03:30 -0800 | [diff] [blame] | 814 | // Add the current directory to the list if it was empty, to ensure that it gets created. |
Josh Gao | a90563e | 2015-11-04 14:57:04 -0800 | [diff] [blame] | 815 | if (empty_dir) { |
Josh Gao | d9a2fd6 | 2015-12-09 14:03:30 -0800 | [diff] [blame] | 816 | copyinfo ci(adb_dirname(lpath), adb_dirname(rpath), adb_basename(rpath), S_IFDIR); |
Elliott Hughes | 7b43fa5 | 2015-12-17 17:05:29 -0800 | [diff] [blame] | 817 | file_list->push_back(ci); |
Josh Gao | a90563e | 2015-11-04 14:57:04 -0800 | [diff] [blame] | 818 | return true; |
| 819 | } |
| 820 | |
Josh Gao | d9a2fd6 | 2015-12-09 14:03:30 -0800 | [diff] [blame] | 821 | // Check each symlink we found to see whether it's a file or directory. |
| 822 | for (copyinfo& link_ci : linklist) { |
| 823 | if (remote_symlink_isdir(sc, link_ci.rpath)) { |
| 824 | dirlist.emplace_back(std::move(link_ci)); |
| 825 | } else { |
Elliott Hughes | 7b43fa5 | 2015-12-17 17:05:29 -0800 | [diff] [blame] | 826 | file_list->emplace_back(std::move(link_ci)); |
Josh Gao | d9a2fd6 | 2015-12-09 14:03:30 -0800 | [diff] [blame] | 827 | } |
| 828 | } |
| 829 | |
Elliott Hughes | b628cb1 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 830 | // Recurse into each directory we found. |
Josh Gao | 204d21e | 2015-11-02 16:45:47 -0800 | [diff] [blame] | 831 | while (!dirlist.empty()) { |
| 832 | copyinfo current = dirlist.back(); |
| 833 | dirlist.pop_back(); |
Elliott Hughes | 7b43fa5 | 2015-12-17 17:05:29 -0800 | [diff] [blame] | 834 | if (!remote_build_list(sc, file_list, current.rpath, current.lpath)) { |
Elliott Hughes | b628cb1 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 835 | return false; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 836 | } |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 837 | } |
| 838 | |
Elliott Hughes | b628cb1 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 839 | return true; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 840 | } |
| 841 | |
Josh Gao | a3b6a06 | 2015-11-09 11:12:14 -0800 | [diff] [blame] | 842 | static int set_time_and_mode(const std::string& lpath, time_t time, |
| 843 | unsigned int mode) { |
Greg Hackmann | 8b68914 | 2014-05-06 08:48:18 -0700 | [diff] [blame] | 844 | struct utimbuf times = { time, time }; |
Josh Gao | a3b6a06 | 2015-11-09 11:12:14 -0800 | [diff] [blame] | 845 | int r1 = utime(lpath.c_str(), ×); |
Lajos Molnar | 4e23e3c | 2013-04-19 12:41:09 -0700 | [diff] [blame] | 846 | |
| 847 | /* use umask for permissions */ |
Josh Gao | 204d21e | 2015-11-02 16:45:47 -0800 | [diff] [blame] | 848 | mode_t mask = umask(0000); |
Lajos Molnar | 4e23e3c | 2013-04-19 12:41:09 -0700 | [diff] [blame] | 849 | umask(mask); |
Josh Gao | a3b6a06 | 2015-11-09 11:12:14 -0800 | [diff] [blame] | 850 | int r2 = chmod(lpath.c_str(), mode & ~mask); |
Lajos Molnar | 4e23e3c | 2013-04-19 12:41:09 -0700 | [diff] [blame] | 851 | |
Spencer Low | 28bc2cb | 2015-11-07 18:51:54 -0800 | [diff] [blame] | 852 | return r1 ? r1 : r2; |
Lajos Molnar | 4e23e3c | 2013-04-19 12:41:09 -0700 | [diff] [blame] | 853 | } |
| 854 | |
Josh Gao | d20cf38 | 2015-11-03 15:23:03 -0800 | [diff] [blame] | 855 | static bool copy_remote_dir_local(SyncConnection& sc, std::string rpath, |
| 856 | std::string lpath, bool copy_attrs) { |
Elliott Hughes | b628cb1 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 857 | // Make sure that both directory paths end in a slash. |
Josh Gao | a5cea71 | 2015-11-07 15:27:26 -0800 | [diff] [blame] | 858 | // Both paths are known to be nonempty, so we don't need to check. |
Elliott Hughes | 7b43fa5 | 2015-12-17 17:05:29 -0800 | [diff] [blame] | 859 | ensure_trailing_separators(lpath, rpath); |
Riley Andrews | c736a94 | 2014-12-12 13:12:36 -0800 | [diff] [blame] | 860 | |
Elliott Hughes | b628cb1 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 861 | // Recursively build the list of files to copy. |
Elliott Hughes | 7b43fa5 | 2015-12-17 17:05:29 -0800 | [diff] [blame] | 862 | sc.Printf("pull: building file list..."); |
| 863 | std::vector<copyinfo> file_list; |
| 864 | if (!remote_build_list(sc, &file_list, rpath.c_str(), lpath.c_str())) { |
Josh Gao | 204d21e | 2015-11-02 16:45:47 -0800 | [diff] [blame] | 865 | return false; |
| 866 | } |
Elliott Hughes | b628cb1 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 867 | |
Elliott Hughes | 7b43fa5 | 2015-12-17 17:05:29 -0800 | [diff] [blame] | 868 | sc.ComputeExpectedTotalBytes(file_list); |
| 869 | |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 870 | int pulled = 0; |
| 871 | int skipped = 0; |
Elliott Hughes | 7b43fa5 | 2015-12-17 17:05:29 -0800 | [diff] [blame] | 872 | for (const copyinfo &ci : file_list) { |
Josh Gao | cb094c6 | 2015-11-03 14:44:04 -0800 | [diff] [blame] | 873 | if (!ci.skip) { |
Josh Gao | a90563e | 2015-11-04 14:57:04 -0800 | [diff] [blame] | 874 | if (S_ISDIR(ci.mode)) { |
| 875 | // Entry is for an empty directory, create it and continue. |
| 876 | // TODO(b/25457350): We don't preserve permissions on directories. |
Josh Gao | a3b6a06 | 2015-11-09 11:12:14 -0800 | [diff] [blame] | 877 | if (!mkdirs(ci.lpath)) { |
Josh Gao | a90563e | 2015-11-04 14:57:04 -0800 | [diff] [blame] | 878 | sc.Error("failed to create directory '%s': %s", |
Josh Gao | a3b6a06 | 2015-11-09 11:12:14 -0800 | [diff] [blame] | 879 | ci.lpath.c_str(), strerror(errno)); |
Josh Gao | a90563e | 2015-11-04 14:57:04 -0800 | [diff] [blame] | 880 | return false; |
| 881 | } |
| 882 | pulled++; |
| 883 | continue; |
| 884 | } |
| 885 | |
Josh Gao | a3b6a06 | 2015-11-09 11:12:14 -0800 | [diff] [blame] | 886 | if (!sync_recv(sc, ci.rpath.c_str(), ci.lpath.c_str())) { |
Elliott Hughes | b628cb1 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 887 | return false; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 888 | } |
Lajos Molnar | 4e23e3c | 2013-04-19 12:41:09 -0700 | [diff] [blame] | 889 | |
Josh Gao | a3b6a06 | 2015-11-09 11:12:14 -0800 | [diff] [blame] | 890 | if (copy_attrs && set_time_and_mode(ci.lpath, ci.time, ci.mode)) { |
Elliott Hughes | b628cb1 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 891 | return false; |
Lajos Molnar | 4e23e3c | 2013-04-19 12:41:09 -0700 | [diff] [blame] | 892 | } |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 893 | pulled++; |
| 894 | } else { |
| 895 | skipped++; |
| 896 | } |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 897 | } |
| 898 | |
Elliott Hughes | 78a37a5 | 2015-12-08 16:01:15 -0800 | [diff] [blame] | 899 | sc.Printf("%s: %d file%s pulled. %d file%s skipped.%s", rpath.c_str(), |
Josh Gao | d20cf38 | 2015-11-03 15:23:03 -0800 | [diff] [blame] | 900 | pulled, (pulled == 1) ? "" : "s", skipped, |
| 901 | (skipped == 1) ? "" : "s", sc.TransferRate().c_str()); |
Elliott Hughes | b628cb1 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 902 | return true; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 903 | } |
| 904 | |
Josh Gao | 5d093b2 | 2015-10-30 16:57:19 -0700 | [diff] [blame] | 905 | bool do_sync_pull(const std::vector<const char*>& srcs, const char* dst, |
| 906 | bool copy_attrs) { |
Elliott Hughes | b628cb1 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 907 | SyncConnection sc; |
| 908 | if (!sc.IsValid()) return false; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 909 | |
Josh Gao | 5d093b2 | 2015-10-30 16:57:19 -0700 | [diff] [blame] | 910 | bool success = true; |
Josh Gao | 5d093b2 | 2015-10-30 16:57:19 -0700 | [diff] [blame] | 911 | struct stat st; |
Josh Gao | a5cea71 | 2015-11-07 15:27:26 -0800 | [diff] [blame] | 912 | bool dst_exists = true; |
| 913 | |
| 914 | if (stat(dst, &st) == -1) { |
| 915 | dst_exists = false; |
| 916 | |
| 917 | // If we're only pulling one path, the destination path might point to |
Josh Gao | 5d093b2 | 2015-10-30 16:57:19 -0700 | [diff] [blame] | 918 | // a path that doesn't exist yet. |
Josh Gao | a5cea71 | 2015-11-07 15:27:26 -0800 | [diff] [blame] | 919 | if (srcs.size() == 1 && errno == ENOENT) { |
| 920 | // However, its parent must exist. |
| 921 | struct stat parent_st; |
| 922 | if (stat(adb_dirname(dst).c_str(), &parent_st) == -1) { |
| 923 | sc.Error("cannot create file/directory '%s': %s", dst, strerror(errno)); |
| 924 | return false; |
| 925 | } |
| 926 | } else { |
| 927 | sc.Error("failed to access '%s': %s", dst, strerror(errno)); |
Josh Gao | 5d093b2 | 2015-10-30 16:57:19 -0700 | [diff] [blame] | 928 | return false; |
| 929 | } |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 930 | } |
| 931 | |
Josh Gao | 8acf06c | 2015-11-07 15:38:19 -0800 | [diff] [blame] | 932 | bool dst_isdir = dst_exists && S_ISDIR(st.st_mode); |
| 933 | if (!dst_isdir) { |
Josh Gao | 5d093b2 | 2015-10-30 16:57:19 -0700 | [diff] [blame] | 934 | if (srcs.size() > 1) { |
| 935 | sc.Error("target '%s' is not a directory", dst); |
Elliott Hughes | b628cb1 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 936 | return false; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 937 | } else { |
Josh Gao | 5d093b2 | 2015-10-30 16:57:19 -0700 | [diff] [blame] | 938 | size_t dst_len = strlen(dst); |
Josh Gao | a5cea71 | 2015-11-07 15:27:26 -0800 | [diff] [blame] | 939 | |
| 940 | // A path that ends with a slash doesn't have to be a directory if |
| 941 | // it doesn't exist yet. |
Josh Gao | a3b6a06 | 2015-11-09 11:12:14 -0800 | [diff] [blame] | 942 | if (adb_is_separator(dst[dst_len - 1]) && dst_exists) { |
Josh Gao | 5d093b2 | 2015-10-30 16:57:19 -0700 | [diff] [blame] | 943 | sc.Error("failed to access '%s': Not a directory", dst); |
Elliott Hughes | b628cb1 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 944 | return false; |
Elliott Hughes | d189cfb | 2015-07-30 17:42:01 -0700 | [diff] [blame] | 945 | } |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 946 | } |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 947 | } |
Elliott Hughes | b628cb1 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 948 | |
Josh Gao | 5d093b2 | 2015-10-30 16:57:19 -0700 | [diff] [blame] | 949 | for (const char* src_path : srcs) { |
| 950 | const char* dst_path = dst; |
Elliott Hughes | 7b43fa5 | 2015-12-17 17:05:29 -0800 | [diff] [blame] | 951 | unsigned src_mode, src_time, src_size; |
| 952 | if (!sync_stat(sc, src_path, &src_time, &src_mode, &src_size)) { |
Josh Gao | d9a2fd6 | 2015-12-09 14:03:30 -0800 | [diff] [blame] | 953 | sc.Error("failed to stat remote object '%s'", src_path); |
Josh Gao | a5cea71 | 2015-11-07 15:27:26 -0800 | [diff] [blame] | 954 | return false; |
| 955 | } |
| 956 | if (src_mode == 0) { |
Josh Gao | 5d093b2 | 2015-10-30 16:57:19 -0700 | [diff] [blame] | 957 | sc.Error("remote object '%s' does not exist", src_path); |
| 958 | success = false; |
| 959 | continue; |
| 960 | } |
| 961 | |
Josh Gao | d9a2fd6 | 2015-12-09 14:03:30 -0800 | [diff] [blame] | 962 | bool src_isdir = S_ISDIR(src_mode); |
| 963 | if (S_ISLNK(src_mode)) { |
| 964 | src_isdir = remote_symlink_isdir(sc, src_path); |
| 965 | } |
| 966 | |
| 967 | if ((src_mode & (S_IFREG | S_IFDIR | S_IFBLK | S_IFCHR)) == 0) { |
| 968 | sc.Error("skipping remote object '%s' (mode = 0o%o)", src_path, src_mode); |
| 969 | continue; |
| 970 | } |
| 971 | |
| 972 | if (src_isdir) { |
Josh Gao | 8acf06c | 2015-11-07 15:38:19 -0800 | [diff] [blame] | 973 | std::string dst_dir = dst; |
| 974 | |
| 975 | // If the destination path existed originally, the source directory |
| 976 | // should be copied as a child of the destination. |
| 977 | if (dst_exists) { |
| 978 | if (!dst_isdir) { |
| 979 | sc.Error("target '%s' is not a directory", dst); |
| 980 | return false; |
| 981 | } |
| 982 | if (!adb_is_separator(dst_dir.back())) { |
| 983 | dst_dir.push_back(OS_PATH_SEPARATOR); |
| 984 | } |
| 985 | dst_dir.append(adb_basename(src_path)); |
| 986 | } |
| 987 | |
Josh Gao | d9a2fd6 | 2015-12-09 14:03:30 -0800 | [diff] [blame] | 988 | success &= copy_remote_dir_local(sc, src_path, dst_dir.c_str(), copy_attrs); |
Josh Gao | 5d093b2 | 2015-10-30 16:57:19 -0700 | [diff] [blame] | 989 | continue; |
| 990 | } else { |
Josh Gao | d9a2fd6 | 2015-12-09 14:03:30 -0800 | [diff] [blame] | 991 | std::string path_holder; |
| 992 | if (dst_isdir) { |
| 993 | // If we're copying a remote file to a local directory, we |
| 994 | // really want to copy to local_dir + OS_PATH_SEPARATOR + |
| 995 | // basename(remote). |
| 996 | path_holder = android::base::StringPrintf("%s%c%s", dst_path, OS_PATH_SEPARATOR, |
| 997 | adb_basename(src_path).c_str()); |
| 998 | dst_path = path_holder.c_str(); |
| 999 | } |
| 1000 | |
Elliott Hughes | 7b43fa5 | 2015-12-17 17:05:29 -0800 | [diff] [blame] | 1001 | sc.SetExpectedTotalBytes(src_size); |
Josh Gao | d9a2fd6 | 2015-12-09 14:03:30 -0800 | [diff] [blame] | 1002 | if (!sync_recv(sc, src_path, dst_path)) { |
| 1003 | success = false; |
| 1004 | continue; |
| 1005 | } |
| 1006 | |
| 1007 | if (copy_attrs && set_time_and_mode(dst_path, src_time, src_mode) != 0) { |
| 1008 | success = false; |
| 1009 | continue; |
| 1010 | } |
Josh Gao | 5d093b2 | 2015-10-30 16:57:19 -0700 | [diff] [blame] | 1011 | } |
| 1012 | } |
| 1013 | |
Josh Gao | 5d093b2 | 2015-10-30 16:57:19 -0700 | [diff] [blame] | 1014 | return success; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1015 | } |
| 1016 | |
Elliott Hughes | b628cb1 | 2015-08-03 10:38:08 -0700 | [diff] [blame] | 1017 | 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] | 1018 | SyncConnection sc; |
| 1019 | if (!sc.IsValid()) return false; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1020 | |
Josh Gao | d20cf38 | 2015-11-03 15:23:03 -0800 | [diff] [blame] | 1021 | return copy_local_dir_remote(sc, lpath, rpath, true, list_only); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1022 | } |