Oleksiy Vyalov | 05a55de | 2015-03-23 21:03:02 +0000 | [diff] [blame] | 1 | //===-- AdbClient.cpp -------------------------------------------*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | // Other libraries and framework includes |
Oleksiy Vyalov | 6e181cf | 2016-06-30 18:10:27 +0000 | [diff] [blame] | 11 | #include "AdbClient.h" |
| 12 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 13 | #include "llvm/ADT/STLExtras.h" |
Oleksiy Vyalov | 05a55de | 2015-03-23 21:03:02 +0000 | [diff] [blame] | 14 | #include "llvm/ADT/SmallVector.h" |
| 15 | #include "llvm/ADT/StringRef.h" |
Oleksiy Vyalov | 09e9079d | 2015-05-18 23:44:06 +0000 | [diff] [blame] | 16 | #include "llvm/Support/FileUtilities.h" |
Oleksiy Vyalov | 05a55de | 2015-03-23 21:03:02 +0000 | [diff] [blame] | 17 | |
Oleksiy Vyalov | 6e181cf | 2016-06-30 18:10:27 +0000 | [diff] [blame] | 18 | #include "lldb/Core/DataBuffer.h" |
| 19 | #include "lldb/Core/DataBufferHeap.h" |
| 20 | #include "lldb/Core/DataEncoder.h" |
| 21 | #include "lldb/Core/DataExtractor.h" |
| 22 | #include "lldb/Core/StreamString.h" |
| 23 | #include "lldb/Host/ConnectionFileDescriptor.h" |
| 24 | #include "lldb/Host/FileSpec.h" |
Pavel Labath | 1408bf7 | 2016-11-01 16:11:14 +0000 | [diff] [blame] | 25 | #include "lldb/Host/FileSystem.h" |
Zachary Turner | f343968f | 2016-08-09 23:06:08 +0000 | [diff] [blame] | 26 | #include "lldb/Host/PosixApi.h" |
Oleksiy Vyalov | 05a55de | 2015-03-23 21:03:02 +0000 | [diff] [blame] | 27 | |
Oleksiy Vyalov | 09e9079d | 2015-05-18 23:44:06 +0000 | [diff] [blame] | 28 | #include <limits.h> |
| 29 | |
Oleksiy Vyalov | 6f00106 | 2015-03-25 17:58:13 +0000 | [diff] [blame] | 30 | #include <algorithm> |
Oleksiy Vyalov | c6ac2e1 | 2016-07-08 17:45:37 +0000 | [diff] [blame] | 31 | #include <cstdlib> |
Oleksiy Vyalov | 09e9079d | 2015-05-18 23:44:06 +0000 | [diff] [blame] | 32 | #include <fstream> |
Oleksiy Vyalov | 05a55de | 2015-03-23 21:03:02 +0000 | [diff] [blame] | 33 | #include <sstream> |
| 34 | |
Adrian McCarthy | 1341d4c | 2016-06-30 20:55:50 +0000 | [diff] [blame] | 35 | // On Windows, transitive dependencies pull in <Windows.h>, which defines a |
| 36 | // macro that clashes with a method name. |
| 37 | #ifdef SendMessage |
| 38 | #undef SendMessage |
| 39 | #endif |
| 40 | |
Oleksiy Vyalov | 05a55de | 2015-03-23 21:03:02 +0000 | [diff] [blame] | 41 | using namespace lldb; |
| 42 | using namespace lldb_private; |
Tamas Berghammer | db264a6 | 2015-03-31 09:52:22 +0000 | [diff] [blame] | 43 | using namespace lldb_private::platform_android; |
Oleksiy Vyalov | 05a55de | 2015-03-23 21:03:02 +0000 | [diff] [blame] | 44 | |
| 45 | namespace { |
| 46 | |
Pavel Labath | 204ef66 | 2016-05-16 11:41:36 +0000 | [diff] [blame] | 47 | const std::chrono::seconds kReadTimeout(8); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 48 | const char *kOKAY = "OKAY"; |
| 49 | const char *kFAIL = "FAIL"; |
| 50 | const char *kDATA = "DATA"; |
| 51 | const char *kDONE = "DONE"; |
Oleksiy Vyalov | 6002a31 | 2015-06-05 01:32:45 +0000 | [diff] [blame] | 52 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 53 | const char *kSEND = "SEND"; |
| 54 | const char *kRECV = "RECV"; |
| 55 | const char *kSTAT = "STAT"; |
Oleksiy Vyalov | 6002a31 | 2015-06-05 01:32:45 +0000 | [diff] [blame] | 56 | |
Oleksiy Vyalov | 09e9079d | 2015-05-18 23:44:06 +0000 | [diff] [blame] | 57 | const size_t kSyncPacketLen = 8; |
Robert Flack | 62efb1f | 2015-05-27 02:18:50 +0000 | [diff] [blame] | 58 | // Maximum size of a filesync DATA packet. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 59 | const size_t kMaxPushData = 2 * 1024; |
Robert Flack | 62efb1f | 2015-05-27 02:18:50 +0000 | [diff] [blame] | 60 | // Default mode for pushed files. |
| 61 | const uint32_t kDefaultMode = 0100770; // S_IFREG | S_IRWXU | S_IRWXG |
Oleksiy Vyalov | 05a55de | 2015-03-23 21:03:02 +0000 | [diff] [blame] | 62 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 63 | const char *kSocketNamespaceAbstract = "localabstract"; |
| 64 | const char *kSocketNamespaceFileSystem = "localfilesystem"; |
Oleksiy Vyalov | e7df5f5 | 2015-11-03 01:37:01 +0000 | [diff] [blame] | 65 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 66 | Error ReadAllBytes(Connection &conn, void *buffer, size_t size) { |
| 67 | using namespace std::chrono; |
Oleksiy Vyalov | 6e181cf | 2016-06-30 18:10:27 +0000 | [diff] [blame] | 68 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 69 | Error error; |
| 70 | ConnectionStatus status; |
| 71 | char *read_buffer = static_cast<char *>(buffer); |
Oleksiy Vyalov | 6e181cf | 2016-06-30 18:10:27 +0000 | [diff] [blame] | 72 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 73 | auto now = steady_clock::now(); |
| 74 | const auto deadline = now + kReadTimeout; |
| 75 | size_t total_read_bytes = 0; |
| 76 | while (total_read_bytes < size && now < deadline) { |
| 77 | uint32_t timeout_usec = duration_cast<microseconds>(deadline - now).count(); |
| 78 | auto read_bytes = |
| 79 | conn.Read(read_buffer + total_read_bytes, size - total_read_bytes, |
| 80 | timeout_usec, status, &error); |
Chaoren Lin | 3ea689b | 2015-05-01 16:49:28 +0000 | [diff] [blame] | 81 | if (error.Fail()) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 82 | return error; |
| 83 | total_read_bytes += read_bytes; |
| 84 | if (status != eConnectionStatusSuccess) |
| 85 | break; |
| 86 | now = steady_clock::now(); |
| 87 | } |
| 88 | if (total_read_bytes < size) |
| 89 | error = Error( |
| 90 | "Unable to read requested number of bytes. Connection status: %d.", |
| 91 | status); |
| 92 | return error; |
| 93 | } |
Oleksiy Vyalov | 6f00106 | 2015-03-25 17:58:13 +0000 | [diff] [blame] | 94 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 95 | } // namespace |
Luke Drummond | 3db0491 | 2016-07-07 18:02:44 +0000 | [diff] [blame] | 96 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 97 | Error AdbClient::CreateByDeviceID(const std::string &device_id, |
| 98 | AdbClient &adb) { |
| 99 | DeviceIDList connect_devices; |
| 100 | auto error = adb.GetDevices(connect_devices); |
| 101 | if (error.Fail()) |
Oleksiy Vyalov | 6f00106 | 2015-03-25 17:58:13 +0000 | [diff] [blame] | 102 | return error; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 103 | |
| 104 | std::string android_serial; |
| 105 | if (!device_id.empty()) |
| 106 | android_serial = device_id; |
| 107 | else if (const char *env_serial = std::getenv("ANDROID_SERIAL")) |
| 108 | android_serial = env_serial; |
| 109 | |
| 110 | if (android_serial.empty()) { |
| 111 | if (connect_devices.size() != 1) |
| 112 | return Error("Expected a single connected device, got instead %zu - try " |
| 113 | "setting 'ANDROID_SERIAL'", |
| 114 | connect_devices.size()); |
| 115 | adb.SetDeviceID(connect_devices.front()); |
| 116 | } else { |
| 117 | auto find_it = std::find(connect_devices.begin(), connect_devices.end(), |
| 118 | android_serial); |
| 119 | if (find_it == connect_devices.end()) |
| 120 | return Error("Device \"%s\" not found", android_serial.c_str()); |
| 121 | |
| 122 | adb.SetDeviceID(*find_it); |
| 123 | } |
| 124 | return error; |
Oleksiy Vyalov | 6f00106 | 2015-03-25 17:58:13 +0000 | [diff] [blame] | 125 | } |
| 126 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 127 | AdbClient::AdbClient() {} |
Oleksiy Vyalov | 6e181cf | 2016-06-30 18:10:27 +0000 | [diff] [blame] | 128 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 129 | AdbClient::AdbClient(const std::string &device_id) : m_device_id(device_id) {} |
Oleksiy Vyalov | 05a55de | 2015-03-23 21:03:02 +0000 | [diff] [blame] | 130 | |
Oleksiy Vyalov | 6e181cf | 2016-06-30 18:10:27 +0000 | [diff] [blame] | 131 | AdbClient::~AdbClient() {} |
| 132 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 133 | void AdbClient::SetDeviceID(const std::string &device_id) { |
| 134 | m_device_id = device_id; |
Oleksiy Vyalov | 05a55de | 2015-03-23 21:03:02 +0000 | [diff] [blame] | 135 | } |
| 136 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 137 | const std::string &AdbClient::GetDeviceID() const { return m_device_id; } |
| 138 | |
| 139 | Error AdbClient::Connect() { |
| 140 | Error error; |
| 141 | m_conn.reset(new ConnectionFileDescriptor); |
| 142 | m_conn->Connect("connect://localhost:5037", &error); |
| 143 | |
| 144 | return error; |
Oleksiy Vyalov | 6f00106 | 2015-03-25 17:58:13 +0000 | [diff] [blame] | 145 | } |
| 146 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 147 | Error AdbClient::GetDevices(DeviceIDList &device_list) { |
| 148 | device_list.clear(); |
Oleksiy Vyalov | 05a55de | 2015-03-23 21:03:02 +0000 | [diff] [blame] | 149 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 150 | auto error = SendMessage("host:devices"); |
| 151 | if (error.Fail()) |
Oleksiy Vyalov | 05a55de | 2015-03-23 21:03:02 +0000 | [diff] [blame] | 152 | return error; |
Oleksiy Vyalov | 05a55de | 2015-03-23 21:03:02 +0000 | [diff] [blame] | 153 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 154 | error = ReadResponseStatus(); |
| 155 | if (error.Fail()) |
Oleksiy Vyalov | 05a55de | 2015-03-23 21:03:02 +0000 | [diff] [blame] | 156 | return error; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 157 | |
| 158 | std::vector<char> in_buffer; |
| 159 | error = ReadMessage(in_buffer); |
| 160 | |
| 161 | llvm::StringRef response(&in_buffer[0], in_buffer.size()); |
| 162 | llvm::SmallVector<llvm::StringRef, 4> devices; |
| 163 | response.split(devices, "\n", -1, false); |
| 164 | |
| 165 | for (const auto device : devices) |
| 166 | device_list.push_back(device.split('\t').first); |
| 167 | |
| 168 | // Force disconnect since ADB closes connection after host:devices |
| 169 | // response is sent. |
| 170 | m_conn.reset(); |
| 171 | return error; |
Oleksiy Vyalov | 05a55de | 2015-03-23 21:03:02 +0000 | [diff] [blame] | 172 | } |
| 173 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 174 | Error AdbClient::SetPortForwarding(const uint16_t local_port, |
| 175 | const uint16_t remote_port) { |
| 176 | char message[48]; |
| 177 | snprintf(message, sizeof(message), "forward:tcp:%d;tcp:%d", local_port, |
| 178 | remote_port); |
Oleksiy Vyalov | 05a55de | 2015-03-23 21:03:02 +0000 | [diff] [blame] | 179 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 180 | const auto error = SendDeviceMessage(message); |
| 181 | if (error.Fail()) |
Oleksiy Vyalov | 05a55de | 2015-03-23 21:03:02 +0000 | [diff] [blame] | 182 | return error; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 183 | |
| 184 | return ReadResponseStatus(); |
Oleksiy Vyalov | 05a55de | 2015-03-23 21:03:02 +0000 | [diff] [blame] | 185 | } |
| 186 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 187 | Error AdbClient::SetPortForwarding(const uint16_t local_port, |
Zachary Turner | 245f7fd | 2016-11-17 01:38:02 +0000 | [diff] [blame^] | 188 | llvm::StringRef remote_socket_name, |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 189 | const UnixSocketNamespace socket_namespace) { |
| 190 | char message[PATH_MAX]; |
| 191 | const char *sock_namespace_str = |
| 192 | (socket_namespace == UnixSocketNamespaceAbstract) |
| 193 | ? kSocketNamespaceAbstract |
| 194 | : kSocketNamespaceFileSystem; |
| 195 | snprintf(message, sizeof(message), "forward:tcp:%d;%s:%s", local_port, |
Zachary Turner | 245f7fd | 2016-11-17 01:38:02 +0000 | [diff] [blame^] | 196 | sock_namespace_str, remote_socket_name.str().c_str()); |
Oleksiy Vyalov | 05a55de | 2015-03-23 21:03:02 +0000 | [diff] [blame] | 197 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 198 | const auto error = SendDeviceMessage(message); |
| 199 | if (error.Fail()) |
Oleksiy Vyalov | 05a55de | 2015-03-23 21:03:02 +0000 | [diff] [blame] | 200 | return error; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 201 | |
| 202 | return ReadResponseStatus(); |
Oleksiy Vyalov | 05a55de | 2015-03-23 21:03:02 +0000 | [diff] [blame] | 203 | } |
| 204 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 205 | Error AdbClient::DeletePortForwarding(const uint16_t local_port) { |
| 206 | char message[32]; |
| 207 | snprintf(message, sizeof(message), "killforward:tcp:%d", local_port); |
| 208 | |
| 209 | const auto error = SendDeviceMessage(message); |
| 210 | if (error.Fail()) |
| 211 | return error; |
| 212 | |
| 213 | return ReadResponseStatus(); |
| 214 | } |
| 215 | |
| 216 | Error AdbClient::SendMessage(const std::string &packet, const bool reconnect) { |
| 217 | Error error; |
| 218 | if (!m_conn || reconnect) { |
| 219 | error = Connect(); |
| 220 | if (error.Fail()) |
| 221 | return error; |
| 222 | } |
| 223 | |
| 224 | char length_buffer[5]; |
| 225 | snprintf(length_buffer, sizeof(length_buffer), "%04x", |
| 226 | static_cast<int>(packet.size())); |
| 227 | |
| 228 | ConnectionStatus status; |
| 229 | |
| 230 | m_conn->Write(length_buffer, 4, status, &error); |
| 231 | if (error.Fail()) |
| 232 | return error; |
| 233 | |
| 234 | m_conn->Write(packet.c_str(), packet.size(), status, &error); |
| 235 | return error; |
| 236 | } |
| 237 | |
| 238 | Error AdbClient::SendDeviceMessage(const std::string &packet) { |
| 239 | std::ostringstream msg; |
| 240 | msg << "host-serial:" << m_device_id << ":" << packet; |
| 241 | return SendMessage(msg.str()); |
| 242 | } |
| 243 | |
| 244 | Error AdbClient::ReadMessage(std::vector<char> &message) { |
| 245 | message.clear(); |
| 246 | |
| 247 | char buffer[5]; |
| 248 | buffer[4] = 0; |
| 249 | |
| 250 | auto error = ReadAllBytes(buffer, 4); |
| 251 | if (error.Fail()) |
| 252 | return error; |
| 253 | |
| 254 | unsigned int packet_len = 0; |
| 255 | sscanf(buffer, "%x", &packet_len); |
| 256 | |
| 257 | message.resize(packet_len, 0); |
| 258 | error = ReadAllBytes(&message[0], packet_len); |
| 259 | if (error.Fail()) |
Tamas Berghammer | 9d8dde8 | 2015-09-29 11:04:18 +0000 | [diff] [blame] | 260 | message.clear(); |
| 261 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 262 | return error; |
| 263 | } |
Tamas Berghammer | 9d8dde8 | 2015-09-29 11:04:18 +0000 | [diff] [blame] | 264 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 265 | Error AdbClient::ReadMessageStream(std::vector<char> &message, |
| 266 | uint32_t timeout_ms) { |
| 267 | auto start = std::chrono::steady_clock::now(); |
| 268 | message.clear(); |
| 269 | |
| 270 | Error error; |
| 271 | lldb::ConnectionStatus status = lldb::eConnectionStatusSuccess; |
| 272 | char buffer[1024]; |
| 273 | while (error.Success() && status == lldb::eConnectionStatusSuccess) { |
| 274 | auto end = std::chrono::steady_clock::now(); |
| 275 | uint32_t elapsed_time = |
| 276 | std::chrono::duration_cast<std::chrono::milliseconds>(end - start) |
| 277 | .count(); |
| 278 | if (elapsed_time >= timeout_ms) |
| 279 | return Error("Timed out"); |
| 280 | |
| 281 | size_t n = m_conn->Read(buffer, sizeof(buffer), |
| 282 | 1000 * (timeout_ms - elapsed_time), status, &error); |
| 283 | if (n > 0) |
| 284 | message.insert(message.end(), &buffer[0], &buffer[n]); |
| 285 | } |
| 286 | return error; |
| 287 | } |
| 288 | |
| 289 | Error AdbClient::ReadResponseStatus() { |
| 290 | char response_id[5]; |
| 291 | |
| 292 | static const size_t packet_len = 4; |
| 293 | response_id[packet_len] = 0; |
| 294 | |
| 295 | auto error = ReadAllBytes(response_id, packet_len); |
| 296 | if (error.Fail()) |
Tamas Berghammer | 9d8dde8 | 2015-09-29 11:04:18 +0000 | [diff] [blame] | 297 | return error; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 298 | |
| 299 | if (strncmp(response_id, kOKAY, packet_len) != 0) |
| 300 | return GetResponseError(response_id); |
| 301 | |
| 302 | return error; |
Tamas Berghammer | 9d8dde8 | 2015-09-29 11:04:18 +0000 | [diff] [blame] | 303 | } |
| 304 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 305 | Error AdbClient::GetResponseError(const char *response_id) { |
| 306 | if (strcmp(response_id, kFAIL) != 0) |
| 307 | return Error("Got unexpected response id from adb: \"%s\"", response_id); |
Oleksiy Vyalov | 05a55de | 2015-03-23 21:03:02 +0000 | [diff] [blame] | 308 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 309 | std::vector<char> error_message; |
| 310 | auto error = ReadMessage(error_message); |
| 311 | if (error.Success()) |
| 312 | error.SetErrorString( |
| 313 | std::string(&error_message[0], error_message.size()).c_str()); |
Oleksiy Vyalov | 05a55de | 2015-03-23 21:03:02 +0000 | [diff] [blame] | 314 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 315 | return error; |
| 316 | } |
Oleksiy Vyalov | 05a55de | 2015-03-23 21:03:02 +0000 | [diff] [blame] | 317 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 318 | Error AdbClient::SwitchDeviceTransport() { |
| 319 | std::ostringstream msg; |
| 320 | msg << "host:transport:" << m_device_id; |
Oleksiy Vyalov | 09e9079d | 2015-05-18 23:44:06 +0000 | [diff] [blame] | 321 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 322 | auto error = SendMessage(msg.str()); |
| 323 | if (error.Fail()) |
Oleksiy Vyalov | 09e9079d | 2015-05-18 23:44:06 +0000 | [diff] [blame] | 324 | return error; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 325 | |
| 326 | return ReadResponseStatus(); |
Oleksiy Vyalov | 09e9079d | 2015-05-18 23:44:06 +0000 | [diff] [blame] | 327 | } |
| 328 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 329 | Error AdbClient::StartSync() { |
| 330 | auto error = SwitchDeviceTransport(); |
| 331 | if (error.Fail()) |
| 332 | return Error("Failed to switch to device transport: %s", error.AsCString()); |
Oleksiy Vyalov | 09e9079d | 2015-05-18 23:44:06 +0000 | [diff] [blame] | 333 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 334 | error = Sync(); |
| 335 | if (error.Fail()) |
| 336 | return Error("Sync failed: %s", error.AsCString()); |
Oleksiy Vyalov | 09e9079d | 2015-05-18 23:44:06 +0000 | [diff] [blame] | 337 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 338 | return error; |
| 339 | } |
| 340 | |
| 341 | Error AdbClient::Sync() { |
| 342 | auto error = SendMessage("sync:", false); |
| 343 | if (error.Fail()) |
Oleksiy Vyalov | 09e9079d | 2015-05-18 23:44:06 +0000 | [diff] [blame] | 344 | return error; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 345 | |
| 346 | return ReadResponseStatus(); |
Oleksiy Vyalov | 09e9079d | 2015-05-18 23:44:06 +0000 | [diff] [blame] | 347 | } |
| 348 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 349 | Error AdbClient::ReadAllBytes(void *buffer, size_t size) { |
| 350 | return ::ReadAllBytes(*m_conn, buffer, size); |
Oleksiy Vyalov | 09e9079d | 2015-05-18 23:44:06 +0000 | [diff] [blame] | 351 | } |
| 352 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 353 | Error AdbClient::internalShell(const char *command, uint32_t timeout_ms, |
| 354 | std::vector<char> &output_buf) { |
| 355 | output_buf.clear(); |
Oleksiy Vyalov | 6e181cf | 2016-06-30 18:10:27 +0000 | [diff] [blame] | 356 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 357 | auto error = SwitchDeviceTransport(); |
| 358 | if (error.Fail()) |
| 359 | return Error("Failed to switch to device transport: %s", error.AsCString()); |
Oleksiy Vyalov | 6e181cf | 2016-06-30 18:10:27 +0000 | [diff] [blame] | 360 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 361 | StreamString adb_command; |
| 362 | adb_command.Printf("shell:%s", command); |
Zachary Turner | c156427 | 2016-11-16 21:15:24 +0000 | [diff] [blame] | 363 | error = SendMessage(adb_command.GetString(), false); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 364 | if (error.Fail()) |
Oleksiy Vyalov | 6e181cf | 2016-06-30 18:10:27 +0000 | [diff] [blame] | 365 | return error; |
Oleksiy Vyalov | 6e181cf | 2016-06-30 18:10:27 +0000 | [diff] [blame] | 366 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 367 | error = ReadResponseStatus(); |
| 368 | if (error.Fail()) |
Oleksiy Vyalov | 6e181cf | 2016-06-30 18:10:27 +0000 | [diff] [blame] | 369 | return error; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 370 | |
| 371 | error = ReadMessageStream(output_buf, timeout_ms); |
| 372 | if (error.Fail()) |
| 373 | return error; |
| 374 | |
| 375 | // ADB doesn't propagate return code of shell execution - if |
| 376 | // output starts with /system/bin/sh: most likely command failed. |
| 377 | static const char *kShellPrefix = "/system/bin/sh:"; |
| 378 | if (output_buf.size() > strlen(kShellPrefix)) { |
| 379 | if (!memcmp(&output_buf[0], kShellPrefix, strlen(kShellPrefix))) |
| 380 | return Error("Shell command %s failed: %s", command, |
| 381 | std::string(output_buf.begin(), output_buf.end()).c_str()); |
| 382 | } |
| 383 | |
Mehdi Amini | c1edf56 | 2016-11-11 04:29:25 +0000 | [diff] [blame] | 384 | return Error(); |
Oleksiy Vyalov | 6e181cf | 2016-06-30 18:10:27 +0000 | [diff] [blame] | 385 | } |
| 386 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 387 | Error AdbClient::Shell(const char *command, uint32_t timeout_ms, |
| 388 | std::string *output) { |
| 389 | std::vector<char> output_buffer; |
| 390 | auto error = internalShell(command, timeout_ms, output_buffer); |
| 391 | if (error.Fail()) |
| 392 | return error; |
Oleksiy Vyalov | c6ac2e1 | 2016-07-08 17:45:37 +0000 | [diff] [blame] | 393 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 394 | if (output) |
| 395 | output->assign(output_buffer.begin(), output_buffer.end()); |
| 396 | return error; |
| 397 | } |
Oleksiy Vyalov | c6ac2e1 | 2016-07-08 17:45:37 +0000 | [diff] [blame] | 398 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 399 | Error AdbClient::ShellToFile(const char *command, uint32_t timeout_ms, |
| 400 | const FileSpec &output_file_spec) { |
| 401 | std::vector<char> output_buffer; |
| 402 | auto error = internalShell(command, timeout_ms, output_buffer); |
| 403 | if (error.Fail()) |
| 404 | return error; |
| 405 | |
| 406 | const auto output_filename = output_file_spec.GetPath(); |
| 407 | std::ofstream dst(output_filename, std::ios::out | std::ios::binary); |
| 408 | if (!dst.is_open()) |
| 409 | return Error("Unable to open local file %s", output_filename.c_str()); |
| 410 | |
| 411 | dst.write(&output_buffer[0], output_buffer.size()); |
| 412 | dst.close(); |
| 413 | if (!dst) |
| 414 | return Error("Failed to write file %s", output_filename.c_str()); |
Mehdi Amini | c1edf56 | 2016-11-11 04:29:25 +0000 | [diff] [blame] | 415 | return Error(); |
Oleksiy Vyalov | c6ac2e1 | 2016-07-08 17:45:37 +0000 | [diff] [blame] | 416 | } |
| 417 | |
Oleksiy Vyalov | 6e181cf | 2016-06-30 18:10:27 +0000 | [diff] [blame] | 418 | std::unique_ptr<AdbClient::SyncService> |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 419 | AdbClient::GetSyncService(Error &error) { |
| 420 | std::unique_ptr<SyncService> sync_service; |
| 421 | error = StartSync(); |
| 422 | if (error.Success()) |
| 423 | sync_service.reset(new SyncService(std::move(m_conn))); |
Oleksiy Vyalov | 6e181cf | 2016-06-30 18:10:27 +0000 | [diff] [blame] | 424 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 425 | return sync_service; |
Oleksiy Vyalov | 6e181cf | 2016-06-30 18:10:27 +0000 | [diff] [blame] | 426 | } |
| 427 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 428 | Error AdbClient::SyncService::internalPullFile(const FileSpec &remote_file, |
| 429 | const FileSpec &local_file) { |
| 430 | const auto local_file_path = local_file.GetPath(); |
Malcolm Parsons | 771ef6d | 2016-11-02 20:34:10 +0000 | [diff] [blame] | 431 | llvm::FileRemover local_file_remover(local_file_path); |
Oleksiy Vyalov | 09e9079d | 2015-05-18 23:44:06 +0000 | [diff] [blame] | 432 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 433 | std::ofstream dst(local_file_path, std::ios::out | std::ios::binary); |
| 434 | if (!dst.is_open()) |
| 435 | return Error("Unable to open local file %s", local_file_path.c_str()); |
Oleksiy Vyalov | 09e9079d | 2015-05-18 23:44:06 +0000 | [diff] [blame] | 436 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 437 | const auto remote_file_path = remote_file.GetPath(false); |
| 438 | auto error = SendSyncRequest(kRECV, remote_file_path.length(), |
| 439 | remote_file_path.c_str()); |
| 440 | if (error.Fail()) |
Oleksiy Vyalov | 09e9079d | 2015-05-18 23:44:06 +0000 | [diff] [blame] | 441 | return error; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 442 | |
| 443 | std::vector<char> chunk; |
| 444 | bool eof = false; |
| 445 | while (!eof) { |
| 446 | error = PullFileChunk(chunk, eof); |
| 447 | if (error.Fail()) |
| 448 | return error; |
| 449 | if (!eof) |
| 450 | dst.write(&chunk[0], chunk.size()); |
| 451 | } |
| 452 | |
| 453 | local_file_remover.releaseFile(); |
| 454 | return error; |
Oleksiy Vyalov | 09e9079d | 2015-05-18 23:44:06 +0000 | [diff] [blame] | 455 | } |
| 456 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 457 | Error AdbClient::SyncService::internalPushFile(const FileSpec &local_file, |
| 458 | const FileSpec &remote_file) { |
| 459 | const auto local_file_path(local_file.GetPath()); |
| 460 | std::ifstream src(local_file_path.c_str(), std::ios::in | std::ios::binary); |
| 461 | if (!src.is_open()) |
| 462 | return Error("Unable to open local file %s", local_file_path.c_str()); |
Robert Flack | 62efb1f | 2015-05-27 02:18:50 +0000 | [diff] [blame] | 463 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 464 | std::stringstream file_description; |
| 465 | file_description << remote_file.GetPath(false).c_str() << "," << kDefaultMode; |
| 466 | std::string file_description_str = file_description.str(); |
| 467 | auto error = SendSyncRequest(kSEND, file_description_str.length(), |
| 468 | file_description_str.c_str()); |
| 469 | if (error.Fail()) |
Oleksiy Vyalov | 0b5ebef | 2015-05-28 17:42:48 +0000 | [diff] [blame] | 470 | return error; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 471 | |
| 472 | char chunk[kMaxPushData]; |
| 473 | while (!src.eof() && !src.read(chunk, kMaxPushData).bad()) { |
| 474 | size_t chunk_size = src.gcount(); |
| 475 | error = SendSyncRequest(kDATA, chunk_size, chunk); |
| 476 | if (error.Fail()) |
| 477 | return Error("Failed to send file chunk: %s", error.AsCString()); |
| 478 | } |
Pavel Labath | 1408bf7 | 2016-11-01 16:11:14 +0000 | [diff] [blame] | 479 | error = SendSyncRequest( |
| 480 | kDONE, std::chrono::duration_cast<std::chrono::seconds>( |
| 481 | FileSystem::GetModificationTime(local_file).time_since_epoch()) |
| 482 | .count(), |
| 483 | nullptr); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 484 | if (error.Fail()) |
| 485 | return error; |
| 486 | |
| 487 | std::string response_id; |
| 488 | uint32_t data_len; |
| 489 | error = ReadSyncHeader(response_id, data_len); |
| 490 | if (error.Fail()) |
| 491 | return Error("Failed to read DONE response: %s", error.AsCString()); |
| 492 | if (response_id == kFAIL) { |
| 493 | std::string error_message(data_len, 0); |
| 494 | error = ReadAllBytes(&error_message[0], data_len); |
| 495 | if (error.Fail()) |
| 496 | return Error("Failed to read DONE error message: %s", error.AsCString()); |
| 497 | return Error("Failed to push file: %s", error_message.c_str()); |
| 498 | } else if (response_id != kOKAY) |
| 499 | return Error("Got unexpected DONE response: %s", response_id.c_str()); |
| 500 | |
| 501 | // If there was an error reading the source file, finish the adb file |
| 502 | // transfer first so that adb isn't expecting any more data. |
| 503 | if (src.bad()) |
| 504 | return Error("Failed read on %s", local_file_path.c_str()); |
| 505 | return error; |
Oleksiy Vyalov | 0b5ebef | 2015-05-28 17:42:48 +0000 | [diff] [blame] | 506 | } |
| 507 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 508 | Error AdbClient::SyncService::internalStat(const FileSpec &remote_file, |
| 509 | uint32_t &mode, uint32_t &size, |
| 510 | uint32_t &mtime) { |
| 511 | const std::string remote_file_path(remote_file.GetPath(false)); |
| 512 | auto error = SendSyncRequest(kSTAT, remote_file_path.length(), |
| 513 | remote_file_path.c_str()); |
| 514 | if (error.Fail()) |
| 515 | return Error("Failed to send request: %s", error.AsCString()); |
Oleksiy Vyalov | 0b5ebef | 2015-05-28 17:42:48 +0000 | [diff] [blame] | 516 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 517 | static const size_t stat_len = strlen(kSTAT); |
| 518 | static const size_t response_len = stat_len + (sizeof(uint32_t) * 3); |
Oleksiy Vyalov | 6e181cf | 2016-06-30 18:10:27 +0000 | [diff] [blame] | 519 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 520 | std::vector<char> buffer(response_len); |
| 521 | error = ReadAllBytes(&buffer[0], buffer.size()); |
| 522 | if (error.Fail()) |
| 523 | return Error("Failed to read response: %s", error.AsCString()); |
Oleksiy Vyalov | 6e181cf | 2016-06-30 18:10:27 +0000 | [diff] [blame] | 524 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 525 | DataExtractor extractor(&buffer[0], buffer.size(), eByteOrderLittle, |
| 526 | sizeof(void *)); |
| 527 | offset_t offset = 0; |
| 528 | |
| 529 | const void *command = extractor.GetData(&offset, stat_len); |
| 530 | if (!command) |
| 531 | return Error("Failed to get response command"); |
| 532 | const char *command_str = static_cast<const char *>(command); |
| 533 | if (strncmp(command_str, kSTAT, stat_len)) |
| 534 | return Error("Got invalid stat command: %s", command_str); |
| 535 | |
| 536 | mode = extractor.GetU32(&offset); |
| 537 | size = extractor.GetU32(&offset); |
| 538 | mtime = extractor.GetU32(&offset); |
Mehdi Amini | c1edf56 | 2016-11-11 04:29:25 +0000 | [diff] [blame] | 539 | return Error(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 540 | } |
| 541 | |
| 542 | Error AdbClient::SyncService::PullFile(const FileSpec &remote_file, |
| 543 | const FileSpec &local_file) { |
| 544 | return executeCommand([this, &remote_file, &local_file]() { |
| 545 | return internalPullFile(remote_file, local_file); |
| 546 | }); |
| 547 | } |
| 548 | |
| 549 | Error AdbClient::SyncService::PushFile(const FileSpec &local_file, |
| 550 | const FileSpec &remote_file) { |
| 551 | return executeCommand([this, &local_file, &remote_file]() { |
| 552 | return internalPushFile(local_file, remote_file); |
| 553 | }); |
| 554 | } |
| 555 | |
| 556 | Error AdbClient::SyncService::Stat(const FileSpec &remote_file, uint32_t &mode, |
| 557 | uint32_t &size, uint32_t &mtime) { |
| 558 | return executeCommand([this, &remote_file, &mode, &size, &mtime]() { |
| 559 | return internalStat(remote_file, mode, size, mtime); |
| 560 | }); |
| 561 | } |
| 562 | |
| 563 | bool AdbClient::SyncService::IsConnected() const { |
| 564 | return m_conn && m_conn->IsConnected(); |
| 565 | } |
| 566 | |
| 567 | AdbClient::SyncService::SyncService(std::unique_ptr<Connection> &&conn) |
| 568 | : m_conn(std::move(conn)) {} |
| 569 | |
| 570 | Error AdbClient::SyncService::executeCommand( |
| 571 | const std::function<Error()> &cmd) { |
| 572 | if (!m_conn) |
| 573 | return Error("SyncService is disconnected"); |
| 574 | |
| 575 | const auto error = cmd(); |
| 576 | if (error.Fail()) |
| 577 | m_conn.reset(); |
| 578 | |
| 579 | return error; |
| 580 | } |
| 581 | |
| 582 | AdbClient::SyncService::~SyncService() {} |
| 583 | |
| 584 | Error AdbClient::SyncService::SendSyncRequest(const char *request_id, |
| 585 | const uint32_t data_len, |
| 586 | const void *data) { |
| 587 | const DataBufferSP data_sp(new DataBufferHeap(kSyncPacketLen, 0)); |
| 588 | DataEncoder encoder(data_sp, eByteOrderLittle, sizeof(void *)); |
| 589 | auto offset = encoder.PutData(0, request_id, strlen(request_id)); |
| 590 | encoder.PutU32(offset, data_len); |
| 591 | |
| 592 | Error error; |
| 593 | ConnectionStatus status; |
| 594 | m_conn->Write(data_sp->GetBytes(), kSyncPacketLen, status, &error); |
| 595 | if (error.Fail()) |
| 596 | return error; |
| 597 | |
| 598 | if (data) |
| 599 | m_conn->Write(data, data_len, status, &error); |
| 600 | return error; |
| 601 | } |
| 602 | |
| 603 | Error AdbClient::SyncService::ReadSyncHeader(std::string &response_id, |
| 604 | uint32_t &data_len) { |
| 605 | char buffer[kSyncPacketLen]; |
| 606 | |
| 607 | auto error = ReadAllBytes(buffer, kSyncPacketLen); |
| 608 | if (error.Success()) { |
| 609 | response_id.assign(&buffer[0], 4); |
| 610 | DataExtractor extractor(&buffer[4], 4, eByteOrderLittle, sizeof(void *)); |
Oleksiy Vyalov | 6e181cf | 2016-06-30 18:10:27 +0000 | [diff] [blame] | 611 | offset_t offset = 0; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 612 | data_len = extractor.GetU32(&offset); |
| 613 | } |
Oleksiy Vyalov | 6e181cf | 2016-06-30 18:10:27 +0000 | [diff] [blame] | 614 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 615 | return error; |
Oleksiy Vyalov | 6e181cf | 2016-06-30 18:10:27 +0000 | [diff] [blame] | 616 | } |
| 617 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 618 | Error AdbClient::SyncService::PullFileChunk(std::vector<char> &buffer, |
| 619 | bool &eof) { |
| 620 | buffer.clear(); |
Oleksiy Vyalov | d6a143f | 2016-07-06 17:02:42 +0000 | [diff] [blame] | 621 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 622 | std::string response_id; |
| 623 | uint32_t data_len; |
| 624 | auto error = ReadSyncHeader(response_id, data_len); |
| 625 | if (error.Fail()) |
Oleksiy Vyalov | d6a143f | 2016-07-06 17:02:42 +0000 | [diff] [blame] | 626 | return error; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 627 | |
| 628 | if (response_id == kDATA) { |
| 629 | buffer.resize(data_len, 0); |
| 630 | error = ReadAllBytes(&buffer[0], data_len); |
| 631 | if (error.Fail()) |
| 632 | buffer.clear(); |
| 633 | } else if (response_id == kDONE) { |
| 634 | eof = true; |
| 635 | } else if (response_id == kFAIL) { |
| 636 | std::string error_message(data_len, 0); |
| 637 | error = ReadAllBytes(&error_message[0], data_len); |
| 638 | if (error.Fail()) |
| 639 | return Error("Failed to read pull error message: %s", error.AsCString()); |
| 640 | return Error("Failed to pull file: %s", error_message.c_str()); |
| 641 | } else |
| 642 | return Error("Pull failed with unknown response: %s", response_id.c_str()); |
| 643 | |
Mehdi Amini | c1edf56 | 2016-11-11 04:29:25 +0000 | [diff] [blame] | 644 | return Error(); |
Oleksiy Vyalov | d6a143f | 2016-07-06 17:02:42 +0000 | [diff] [blame] | 645 | } |
| 646 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 647 | Error AdbClient::SyncService::ReadAllBytes(void *buffer, size_t size) { |
| 648 | return ::ReadAllBytes(*m_conn, buffer, size); |
Oleksiy Vyalov | 6e181cf | 2016-06-30 18:10:27 +0000 | [diff] [blame] | 649 | } |