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