blob: 400a894652fc7b321ffa4753417d2e0ce2d02d48 [file] [log] [blame]
Oleksiy Vyalov05a55de2015-03-23 21:03:02 +00001//===-- 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 Vyalov6e181cf2016-06-30 18:10:27 +000011#include "AdbClient.h"
12
Kate Stoneb9c1b512016-09-06 20:57:50 +000013#include "llvm/ADT/STLExtras.h"
Oleksiy Vyalov05a55de2015-03-23 21:03:02 +000014#include "llvm/ADT/SmallVector.h"
15#include "llvm/ADT/StringRef.h"
Oleksiy Vyalov09e9079d2015-05-18 23:44:06 +000016#include "llvm/Support/FileUtilities.h"
Oleksiy Vyalov05a55de2015-03-23 21:03:02 +000017
Oleksiy Vyalov6e181cf2016-06-30 18:10:27 +000018#include "lldb/Host/ConnectionFileDescriptor.h"
Pavel Labath1408bf72016-11-01 16:11:14 +000019#include "lldb/Host/FileSystem.h"
Zachary Turnerf343968f2016-08-09 23:06:08 +000020#include "lldb/Host/PosixApi.h"
Zachary Turner666cc0b2017-03-04 01:30:05 +000021#include "lldb/Utility/DataBuffer.h"
22#include "lldb/Utility/DataBufferHeap.h"
23#include "lldb/Utility/DataEncoder.h"
24#include "lldb/Utility/DataExtractor.h"
Zachary Turner5713a052017-03-22 18:40:07 +000025#include "lldb/Utility/FileSpec.h"
Zachary Turnerbf9a7732017-02-02 21:39:50 +000026#include "lldb/Utility/StreamString.h"
Zachary Turner2f3df612017-04-06 21:28:29 +000027#include "lldb/Utility/Timeout.h"
Oleksiy Vyalov05a55de2015-03-23 21:03:02 +000028
Oleksiy Vyalov09e9079d2015-05-18 23:44:06 +000029#include <limits.h>
30
Oleksiy Vyalov6f001062015-03-25 17:58:13 +000031#include <algorithm>
Oleksiy Vyalovc6ac2e12016-07-08 17:45:37 +000032#include <cstdlib>
Oleksiy Vyalov09e9079d2015-05-18 23:44:06 +000033#include <fstream>
Oleksiy Vyalov05a55de2015-03-23 21:03:02 +000034#include <sstream>
35
Adrian McCarthy1341d4c2016-06-30 20:55:50 +000036// On Windows, transitive dependencies pull in <Windows.h>, which defines a
37// macro that clashes with a method name.
38#ifdef SendMessage
39#undef SendMessage
40#endif
41
Oleksiy Vyalov05a55de2015-03-23 21:03:02 +000042using namespace lldb;
43using namespace lldb_private;
Tamas Berghammerdb264a62015-03-31 09:52:22 +000044using namespace lldb_private::platform_android;
Pavel Labath818dd512016-11-24 14:03:57 +000045using namespace std::chrono;
Oleksiy Vyalov05a55de2015-03-23 21:03:02 +000046
47namespace {
48
Pavel Labathaee3e262017-10-31 12:27:46 +000049const seconds kReadTimeout(20);
Kate Stoneb9c1b512016-09-06 20:57:50 +000050const char *kOKAY = "OKAY";
51const char *kFAIL = "FAIL";
52const char *kDATA = "DATA";
53const char *kDONE = "DONE";
Oleksiy Vyalov6002a312015-06-05 01:32:45 +000054
Kate Stoneb9c1b512016-09-06 20:57:50 +000055const char *kSEND = "SEND";
56const char *kRECV = "RECV";
57const char *kSTAT = "STAT";
Oleksiy Vyalov6002a312015-06-05 01:32:45 +000058
Oleksiy Vyalov09e9079d2015-05-18 23:44:06 +000059const size_t kSyncPacketLen = 8;
Robert Flack62efb1f2015-05-27 02:18:50 +000060// Maximum size of a filesync DATA packet.
Kate Stoneb9c1b512016-09-06 20:57:50 +000061const size_t kMaxPushData = 2 * 1024;
Robert Flack62efb1f2015-05-27 02:18:50 +000062// Default mode for pushed files.
63const uint32_t kDefaultMode = 0100770; // S_IFREG | S_IRWXU | S_IRWXG
Oleksiy Vyalov05a55de2015-03-23 21:03:02 +000064
Kate Stoneb9c1b512016-09-06 20:57:50 +000065const char *kSocketNamespaceAbstract = "localabstract";
66const char *kSocketNamespaceFileSystem = "localfilesystem";
Oleksiy Vyalove7df5f52015-11-03 01:37:01 +000067
Zachary Turner97206d52017-05-12 04:51:55 +000068Status ReadAllBytes(Connection &conn, void *buffer, size_t size) {
Oleksiy Vyalov6e181cf2016-06-30 18:10:27 +000069
Zachary Turner97206d52017-05-12 04:51:55 +000070 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +000071 ConnectionStatus status;
72 char *read_buffer = static_cast<char *>(buffer);
Oleksiy Vyalov6e181cf2016-06-30 18:10:27 +000073
Kate Stoneb9c1b512016-09-06 20:57:50 +000074 auto now = steady_clock::now();
75 const auto deadline = now + kReadTimeout;
76 size_t total_read_bytes = 0;
77 while (total_read_bytes < size && now < deadline) {
Kate Stoneb9c1b512016-09-06 20:57:50 +000078 auto read_bytes =
79 conn.Read(read_buffer + total_read_bytes, size - total_read_bytes,
Pavel Labath2f159a52016-11-25 12:22:32 +000080 duration_cast<microseconds>(deadline - now), status, &error);
Chaoren Lin3ea689b2015-05-01 16:49:28 +000081 if (error.Fail())
Kate Stoneb9c1b512016-09-06 20:57:50 +000082 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)
Zachary Turner97206d52017-05-12 04:51:55 +000089 error = Status(
Kate Stoneb9c1b512016-09-06 20:57:50 +000090 "Unable to read requested number of bytes. Connection status: %d.",
91 status);
92 return error;
93}
Oleksiy Vyalov6f001062015-03-25 17:58:13 +000094
Kate Stoneb9c1b512016-09-06 20:57:50 +000095} // namespace
Luke Drummond3db04912016-07-07 18:02:44 +000096
Zachary Turner97206d52017-05-12 04:51:55 +000097Status AdbClient::CreateByDeviceID(const std::string &device_id,
98 AdbClient &adb) {
Kate Stoneb9c1b512016-09-06 20:57:50 +000099 DeviceIDList connect_devices;
100 auto error = adb.GetDevices(connect_devices);
101 if (error.Fail())
Oleksiy Vyalov6f001062015-03-25 17:58:13 +0000102 return error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000103
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)
Zachary Turner97206d52017-05-12 04:51:55 +0000112 return Status("Expected a single connected device, got instead %zu - try "
113 "setting 'ANDROID_SERIAL'",
114 connect_devices.size());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000115 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())
Zachary Turner97206d52017-05-12 04:51:55 +0000120 return Status("Device \"%s\" not found", android_serial.c_str());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000121
122 adb.SetDeviceID(*find_it);
123 }
124 return error;
Oleksiy Vyalov6f001062015-03-25 17:58:13 +0000125}
126
Kate Stoneb9c1b512016-09-06 20:57:50 +0000127AdbClient::AdbClient() {}
Oleksiy Vyalov6e181cf2016-06-30 18:10:27 +0000128
Kate Stoneb9c1b512016-09-06 20:57:50 +0000129AdbClient::AdbClient(const std::string &device_id) : m_device_id(device_id) {}
Oleksiy Vyalov05a55de2015-03-23 21:03:02 +0000130
Oleksiy Vyalov6e181cf2016-06-30 18:10:27 +0000131AdbClient::~AdbClient() {}
132
Kate Stoneb9c1b512016-09-06 20:57:50 +0000133void AdbClient::SetDeviceID(const std::string &device_id) {
134 m_device_id = device_id;
Oleksiy Vyalov05a55de2015-03-23 21:03:02 +0000135}
136
Kate Stoneb9c1b512016-09-06 20:57:50 +0000137const std::string &AdbClient::GetDeviceID() const { return m_device_id; }
138
Zachary Turner97206d52017-05-12 04:51:55 +0000139Status AdbClient::Connect() {
140 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000141 m_conn.reset(new ConnectionFileDescriptor);
142 m_conn->Connect("connect://localhost:5037", &error);
143
144 return error;
Oleksiy Vyalov6f001062015-03-25 17:58:13 +0000145}
146
Zachary Turner97206d52017-05-12 04:51:55 +0000147Status AdbClient::GetDevices(DeviceIDList &device_list) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000148 device_list.clear();
Oleksiy Vyalov05a55de2015-03-23 21:03:02 +0000149
Kate Stoneb9c1b512016-09-06 20:57:50 +0000150 auto error = SendMessage("host:devices");
151 if (error.Fail())
Oleksiy Vyalov05a55de2015-03-23 21:03:02 +0000152 return error;
Oleksiy Vyalov05a55de2015-03-23 21:03:02 +0000153
Kate Stoneb9c1b512016-09-06 20:57:50 +0000154 error = ReadResponseStatus();
155 if (error.Fail())
Oleksiy Vyalov05a55de2015-03-23 21:03:02 +0000156 return error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000157
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
Adrian Prantl05097242018-04-30 16:49:04 +0000168 // Force disconnect since ADB closes connection after host:devices response
169 // is sent.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000170 m_conn.reset();
171 return error;
Oleksiy Vyalov05a55de2015-03-23 21:03:02 +0000172}
173
Zachary Turner97206d52017-05-12 04:51:55 +0000174Status AdbClient::SetPortForwarding(const uint16_t local_port,
175 const uint16_t remote_port) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000176 char message[48];
177 snprintf(message, sizeof(message), "forward:tcp:%d;tcp:%d", local_port,
178 remote_port);
Oleksiy Vyalov05a55de2015-03-23 21:03:02 +0000179
Kate Stoneb9c1b512016-09-06 20:57:50 +0000180 const auto error = SendDeviceMessage(message);
181 if (error.Fail())
Oleksiy Vyalov05a55de2015-03-23 21:03:02 +0000182 return error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000183
184 return ReadResponseStatus();
Oleksiy Vyalov05a55de2015-03-23 21:03:02 +0000185}
186
Zachary Turner97206d52017-05-12 04:51:55 +0000187Status
188AdbClient::SetPortForwarding(const uint16_t local_port,
189 llvm::StringRef remote_socket_name,
190 const UnixSocketNamespace socket_namespace) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000191 char message[PATH_MAX];
192 const char *sock_namespace_str =
193 (socket_namespace == UnixSocketNamespaceAbstract)
194 ? kSocketNamespaceAbstract
195 : kSocketNamespaceFileSystem;
196 snprintf(message, sizeof(message), "forward:tcp:%d;%s:%s", local_port,
Zachary Turner245f7fd2016-11-17 01:38:02 +0000197 sock_namespace_str, remote_socket_name.str().c_str());
Oleksiy Vyalov05a55de2015-03-23 21:03:02 +0000198
Kate Stoneb9c1b512016-09-06 20:57:50 +0000199 const auto error = SendDeviceMessage(message);
200 if (error.Fail())
Oleksiy Vyalov05a55de2015-03-23 21:03:02 +0000201 return error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000202
203 return ReadResponseStatus();
Oleksiy Vyalov05a55de2015-03-23 21:03:02 +0000204}
205
Zachary Turner97206d52017-05-12 04:51:55 +0000206Status AdbClient::DeletePortForwarding(const uint16_t local_port) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000207 char message[32];
208 snprintf(message, sizeof(message), "killforward:tcp:%d", local_port);
209
210 const auto error = SendDeviceMessage(message);
211 if (error.Fail())
212 return error;
213
214 return ReadResponseStatus();
215}
216
Zachary Turner97206d52017-05-12 04:51:55 +0000217Status AdbClient::SendMessage(const std::string &packet, const bool reconnect) {
218 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000219 if (!m_conn || reconnect) {
220 error = Connect();
221 if (error.Fail())
222 return error;
223 }
224
225 char length_buffer[5];
226 snprintf(length_buffer, sizeof(length_buffer), "%04x",
227 static_cast<int>(packet.size()));
228
229 ConnectionStatus status;
230
231 m_conn->Write(length_buffer, 4, status, &error);
232 if (error.Fail())
233 return error;
234
235 m_conn->Write(packet.c_str(), packet.size(), status, &error);
236 return error;
237}
238
Zachary Turner97206d52017-05-12 04:51:55 +0000239Status AdbClient::SendDeviceMessage(const std::string &packet) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000240 std::ostringstream msg;
241 msg << "host-serial:" << m_device_id << ":" << packet;
242 return SendMessage(msg.str());
243}
244
Zachary Turner97206d52017-05-12 04:51:55 +0000245Status AdbClient::ReadMessage(std::vector<char> &message) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000246 message.clear();
247
248 char buffer[5];
249 buffer[4] = 0;
250
251 auto error = ReadAllBytes(buffer, 4);
252 if (error.Fail())
253 return error;
254
255 unsigned int packet_len = 0;
256 sscanf(buffer, "%x", &packet_len);
257
258 message.resize(packet_len, 0);
259 error = ReadAllBytes(&message[0], packet_len);
260 if (error.Fail())
Tamas Berghammer9d8dde82015-09-29 11:04:18 +0000261 message.clear();
262
Kate Stoneb9c1b512016-09-06 20:57:50 +0000263 return error;
264}
Tamas Berghammer9d8dde82015-09-29 11:04:18 +0000265
Zachary Turner97206d52017-05-12 04:51:55 +0000266Status AdbClient::ReadMessageStream(std::vector<char> &message,
267 milliseconds timeout) {
Pavel Labath818dd512016-11-24 14:03:57 +0000268 auto start = steady_clock::now();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000269 message.clear();
270
Zachary Turner97206d52017-05-12 04:51:55 +0000271 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000272 lldb::ConnectionStatus status = lldb::eConnectionStatusSuccess;
273 char buffer[1024];
274 while (error.Success() && status == lldb::eConnectionStatusSuccess) {
Pavel Labath818dd512016-11-24 14:03:57 +0000275 auto end = steady_clock::now();
276 auto elapsed = end - start;
277 if (elapsed >= timeout)
Zachary Turner97206d52017-05-12 04:51:55 +0000278 return Status("Timed out");
Kate Stoneb9c1b512016-09-06 20:57:50 +0000279
Pavel Labath2f159a52016-11-25 12:22:32 +0000280 size_t n = m_conn->Read(buffer, sizeof(buffer),
281 duration_cast<microseconds>(timeout - elapsed),
282 status, &error);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000283 if (n > 0)
284 message.insert(message.end(), &buffer[0], &buffer[n]);
285 }
286 return error;
287}
288
Zachary Turner97206d52017-05-12 04:51:55 +0000289Status AdbClient::ReadResponseStatus() {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000290 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 Berghammer9d8dde82015-09-29 11:04:18 +0000297 return error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000298
299 if (strncmp(response_id, kOKAY, packet_len) != 0)
300 return GetResponseError(response_id);
301
302 return error;
Tamas Berghammer9d8dde82015-09-29 11:04:18 +0000303}
304
Zachary Turner97206d52017-05-12 04:51:55 +0000305Status AdbClient::GetResponseError(const char *response_id) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000306 if (strcmp(response_id, kFAIL) != 0)
Zachary Turner97206d52017-05-12 04:51:55 +0000307 return Status("Got unexpected response id from adb: \"%s\"", response_id);
Oleksiy Vyalov05a55de2015-03-23 21:03:02 +0000308
Kate Stoneb9c1b512016-09-06 20:57:50 +0000309 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 Vyalov05a55de2015-03-23 21:03:02 +0000314
Kate Stoneb9c1b512016-09-06 20:57:50 +0000315 return error;
316}
Oleksiy Vyalov05a55de2015-03-23 21:03:02 +0000317
Zachary Turner97206d52017-05-12 04:51:55 +0000318Status AdbClient::SwitchDeviceTransport() {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000319 std::ostringstream msg;
320 msg << "host:transport:" << m_device_id;
Oleksiy Vyalov09e9079d2015-05-18 23:44:06 +0000321
Kate Stoneb9c1b512016-09-06 20:57:50 +0000322 auto error = SendMessage(msg.str());
323 if (error.Fail())
Oleksiy Vyalov09e9079d2015-05-18 23:44:06 +0000324 return error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000325
326 return ReadResponseStatus();
Oleksiy Vyalov09e9079d2015-05-18 23:44:06 +0000327}
328
Zachary Turner97206d52017-05-12 04:51:55 +0000329Status AdbClient::StartSync() {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000330 auto error = SwitchDeviceTransport();
331 if (error.Fail())
Zachary Turner97206d52017-05-12 04:51:55 +0000332 return Status("Failed to switch to device transport: %s",
333 error.AsCString());
Oleksiy Vyalov09e9079d2015-05-18 23:44:06 +0000334
Kate Stoneb9c1b512016-09-06 20:57:50 +0000335 error = Sync();
336 if (error.Fail())
Zachary Turner97206d52017-05-12 04:51:55 +0000337 return Status("Sync failed: %s", error.AsCString());
Oleksiy Vyalov09e9079d2015-05-18 23:44:06 +0000338
Kate Stoneb9c1b512016-09-06 20:57:50 +0000339 return error;
340}
341
Zachary Turner97206d52017-05-12 04:51:55 +0000342Status AdbClient::Sync() {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000343 auto error = SendMessage("sync:", false);
344 if (error.Fail())
Oleksiy Vyalov09e9079d2015-05-18 23:44:06 +0000345 return error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000346
347 return ReadResponseStatus();
Oleksiy Vyalov09e9079d2015-05-18 23:44:06 +0000348}
349
Zachary Turner97206d52017-05-12 04:51:55 +0000350Status AdbClient::ReadAllBytes(void *buffer, size_t size) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000351 return ::ReadAllBytes(*m_conn, buffer, size);
Oleksiy Vyalov09e9079d2015-05-18 23:44:06 +0000352}
353
Zachary Turner97206d52017-05-12 04:51:55 +0000354Status AdbClient::internalShell(const char *command, milliseconds timeout,
355 std::vector<char> &output_buf) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000356 output_buf.clear();
Oleksiy Vyalov6e181cf2016-06-30 18:10:27 +0000357
Kate Stoneb9c1b512016-09-06 20:57:50 +0000358 auto error = SwitchDeviceTransport();
359 if (error.Fail())
Zachary Turner97206d52017-05-12 04:51:55 +0000360 return Status("Failed to switch to device transport: %s",
361 error.AsCString());
Oleksiy Vyalov6e181cf2016-06-30 18:10:27 +0000362
Kate Stoneb9c1b512016-09-06 20:57:50 +0000363 StreamString adb_command;
364 adb_command.Printf("shell:%s", command);
Zachary Turnerc1564272016-11-16 21:15:24 +0000365 error = SendMessage(adb_command.GetString(), false);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000366 if (error.Fail())
Oleksiy Vyalov6e181cf2016-06-30 18:10:27 +0000367 return error;
Oleksiy Vyalov6e181cf2016-06-30 18:10:27 +0000368
Kate Stoneb9c1b512016-09-06 20:57:50 +0000369 error = ReadResponseStatus();
370 if (error.Fail())
Oleksiy Vyalov6e181cf2016-06-30 18:10:27 +0000371 return error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000372
Pavel Labath818dd512016-11-24 14:03:57 +0000373 error = ReadMessageStream(output_buf, timeout);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000374 if (error.Fail())
375 return error;
376
377 // ADB doesn't propagate return code of shell execution - if
378 // output starts with /system/bin/sh: most likely command failed.
379 static const char *kShellPrefix = "/system/bin/sh:";
380 if (output_buf.size() > strlen(kShellPrefix)) {
381 if (!memcmp(&output_buf[0], kShellPrefix, strlen(kShellPrefix)))
Zachary Turner97206d52017-05-12 04:51:55 +0000382 return Status("Shell command %s failed: %s", command,
383 std::string(output_buf.begin(), output_buf.end()).c_str());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000384 }
385
Zachary Turner97206d52017-05-12 04:51:55 +0000386 return Status();
Oleksiy Vyalov6e181cf2016-06-30 18:10:27 +0000387}
388
Zachary Turner97206d52017-05-12 04:51:55 +0000389Status AdbClient::Shell(const char *command, milliseconds timeout,
390 std::string *output) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000391 std::vector<char> output_buffer;
Pavel Labathce255ff2016-11-24 14:11:47 +0000392 auto error = internalShell(command, timeout, output_buffer);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000393 if (error.Fail())
394 return error;
Oleksiy Vyalovc6ac2e12016-07-08 17:45:37 +0000395
Kate Stoneb9c1b512016-09-06 20:57:50 +0000396 if (output)
397 output->assign(output_buffer.begin(), output_buffer.end());
398 return error;
399}
Oleksiy Vyalovc6ac2e12016-07-08 17:45:37 +0000400
Zachary Turner97206d52017-05-12 04:51:55 +0000401Status AdbClient::ShellToFile(const char *command, milliseconds timeout,
402 const FileSpec &output_file_spec) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000403 std::vector<char> output_buffer;
Pavel Labathce255ff2016-11-24 14:11:47 +0000404 auto error = internalShell(command, timeout, output_buffer);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000405 if (error.Fail())
406 return error;
407
408 const auto output_filename = output_file_spec.GetPath();
Pavel Labathe3ad2e22017-03-21 13:49:50 +0000409 std::error_code EC;
410 llvm::raw_fd_ostream dst(output_filename, EC, llvm::sys::fs::F_None);
411 if (EC)
Zachary Turner97206d52017-05-12 04:51:55 +0000412 return Status("Unable to open local file %s", output_filename.c_str());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000413
414 dst.write(&output_buffer[0], output_buffer.size());
415 dst.close();
Pavel Labathe3ad2e22017-03-21 13:49:50 +0000416 if (dst.has_error())
Zachary Turner97206d52017-05-12 04:51:55 +0000417 return Status("Failed to write file %s", output_filename.c_str());
418 return Status();
Oleksiy Vyalovc6ac2e12016-07-08 17:45:37 +0000419}
420
Oleksiy Vyalov6e181cf2016-06-30 18:10:27 +0000421std::unique_ptr<AdbClient::SyncService>
Zachary Turner97206d52017-05-12 04:51:55 +0000422AdbClient::GetSyncService(Status &error) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000423 std::unique_ptr<SyncService> sync_service;
424 error = StartSync();
425 if (error.Success())
426 sync_service.reset(new SyncService(std::move(m_conn)));
Oleksiy Vyalov6e181cf2016-06-30 18:10:27 +0000427
Kate Stoneb9c1b512016-09-06 20:57:50 +0000428 return sync_service;
Oleksiy Vyalov6e181cf2016-06-30 18:10:27 +0000429}
430
Zachary Turner97206d52017-05-12 04:51:55 +0000431Status AdbClient::SyncService::internalPullFile(const FileSpec &remote_file,
432 const FileSpec &local_file) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000433 const auto local_file_path = local_file.GetPath();
Malcolm Parsons771ef6d2016-11-02 20:34:10 +0000434 llvm::FileRemover local_file_remover(local_file_path);
Oleksiy Vyalov09e9079d2015-05-18 23:44:06 +0000435
Pavel Labathe3ad2e22017-03-21 13:49:50 +0000436 std::error_code EC;
437 llvm::raw_fd_ostream dst(local_file_path, EC, llvm::sys::fs::F_None);
438 if (EC)
Zachary Turner97206d52017-05-12 04:51:55 +0000439 return Status("Unable to open local file %s", local_file_path.c_str());
Oleksiy Vyalov09e9079d2015-05-18 23:44:06 +0000440
Kate Stoneb9c1b512016-09-06 20:57:50 +0000441 const auto remote_file_path = remote_file.GetPath(false);
442 auto error = SendSyncRequest(kRECV, remote_file_path.length(),
443 remote_file_path.c_str());
444 if (error.Fail())
Oleksiy Vyalov09e9079d2015-05-18 23:44:06 +0000445 return error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000446
447 std::vector<char> chunk;
448 bool eof = false;
449 while (!eof) {
450 error = PullFileChunk(chunk, eof);
451 if (error.Fail())
452 return error;
453 if (!eof)
454 dst.write(&chunk[0], chunk.size());
455 }
Pavel Labathe3ad2e22017-03-21 13:49:50 +0000456 dst.close();
457 if (dst.has_error())
Zachary Turner97206d52017-05-12 04:51:55 +0000458 return Status("Failed to write file %s", local_file_path.c_str());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000459
460 local_file_remover.releaseFile();
461 return error;
Oleksiy Vyalov09e9079d2015-05-18 23:44:06 +0000462}
463
Zachary Turner97206d52017-05-12 04:51:55 +0000464Status AdbClient::SyncService::internalPushFile(const FileSpec &local_file,
465 const FileSpec &remote_file) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000466 const auto local_file_path(local_file.GetPath());
467 std::ifstream src(local_file_path.c_str(), std::ios::in | std::ios::binary);
468 if (!src.is_open())
Zachary Turner97206d52017-05-12 04:51:55 +0000469 return Status("Unable to open local file %s", local_file_path.c_str());
Robert Flack62efb1f2015-05-27 02:18:50 +0000470
Kate Stoneb9c1b512016-09-06 20:57:50 +0000471 std::stringstream file_description;
472 file_description << remote_file.GetPath(false).c_str() << "," << kDefaultMode;
473 std::string file_description_str = file_description.str();
474 auto error = SendSyncRequest(kSEND, file_description_str.length(),
475 file_description_str.c_str());
476 if (error.Fail())
Oleksiy Vyalov0b5ebef2015-05-28 17:42:48 +0000477 return error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000478
479 char chunk[kMaxPushData];
480 while (!src.eof() && !src.read(chunk, kMaxPushData).bad()) {
481 size_t chunk_size = src.gcount();
482 error = SendSyncRequest(kDATA, chunk_size, chunk);
483 if (error.Fail())
Zachary Turner97206d52017-05-12 04:51:55 +0000484 return Status("Failed to send file chunk: %s", error.AsCString());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000485 }
Pavel Labath1408bf72016-11-01 16:11:14 +0000486 error = SendSyncRequest(
Jonas Devlieghere46376962018-10-31 21:49:27 +0000487 kDONE, llvm::sys::toTimeT(FileSystem::Instance().GetModificationTime(local_file)),
Pavel Labath1408bf72016-11-01 16:11:14 +0000488 nullptr);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000489 if (error.Fail())
490 return error;
491
492 std::string response_id;
493 uint32_t data_len;
494 error = ReadSyncHeader(response_id, data_len);
495 if (error.Fail())
Zachary Turner97206d52017-05-12 04:51:55 +0000496 return Status("Failed to read DONE response: %s", error.AsCString());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000497 if (response_id == kFAIL) {
498 std::string error_message(data_len, 0);
499 error = ReadAllBytes(&error_message[0], data_len);
500 if (error.Fail())
Zachary Turner97206d52017-05-12 04:51:55 +0000501 return Status("Failed to read DONE error message: %s", error.AsCString());
502 return Status("Failed to push file: %s", error_message.c_str());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000503 } else if (response_id != kOKAY)
Zachary Turner97206d52017-05-12 04:51:55 +0000504 return Status("Got unexpected DONE response: %s", response_id.c_str());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000505
506 // If there was an error reading the source file, finish the adb file
507 // transfer first so that adb isn't expecting any more data.
508 if (src.bad())
Zachary Turner97206d52017-05-12 04:51:55 +0000509 return Status("Failed read on %s", local_file_path.c_str());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000510 return error;
Oleksiy Vyalov0b5ebef2015-05-28 17:42:48 +0000511}
512
Zachary Turner97206d52017-05-12 04:51:55 +0000513Status AdbClient::SyncService::internalStat(const FileSpec &remote_file,
514 uint32_t &mode, uint32_t &size,
515 uint32_t &mtime) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000516 const std::string remote_file_path(remote_file.GetPath(false));
517 auto error = SendSyncRequest(kSTAT, remote_file_path.length(),
518 remote_file_path.c_str());
519 if (error.Fail())
Zachary Turner97206d52017-05-12 04:51:55 +0000520 return Status("Failed to send request: %s", error.AsCString());
Oleksiy Vyalov0b5ebef2015-05-28 17:42:48 +0000521
Kate Stoneb9c1b512016-09-06 20:57:50 +0000522 static const size_t stat_len = strlen(kSTAT);
523 static const size_t response_len = stat_len + (sizeof(uint32_t) * 3);
Oleksiy Vyalov6e181cf2016-06-30 18:10:27 +0000524
Kate Stoneb9c1b512016-09-06 20:57:50 +0000525 std::vector<char> buffer(response_len);
526 error = ReadAllBytes(&buffer[0], buffer.size());
527 if (error.Fail())
Zachary Turner97206d52017-05-12 04:51:55 +0000528 return Status("Failed to read response: %s", error.AsCString());
Oleksiy Vyalov6e181cf2016-06-30 18:10:27 +0000529
Kate Stoneb9c1b512016-09-06 20:57:50 +0000530 DataExtractor extractor(&buffer[0], buffer.size(), eByteOrderLittle,
531 sizeof(void *));
532 offset_t offset = 0;
533
534 const void *command = extractor.GetData(&offset, stat_len);
535 if (!command)
Zachary Turner97206d52017-05-12 04:51:55 +0000536 return Status("Failed to get response command");
Kate Stoneb9c1b512016-09-06 20:57:50 +0000537 const char *command_str = static_cast<const char *>(command);
538 if (strncmp(command_str, kSTAT, stat_len))
Zachary Turner97206d52017-05-12 04:51:55 +0000539 return Status("Got invalid stat command: %s", command_str);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000540
541 mode = extractor.GetU32(&offset);
542 size = extractor.GetU32(&offset);
543 mtime = extractor.GetU32(&offset);
Zachary Turner97206d52017-05-12 04:51:55 +0000544 return Status();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000545}
546
Zachary Turner97206d52017-05-12 04:51:55 +0000547Status AdbClient::SyncService::PullFile(const FileSpec &remote_file,
548 const FileSpec &local_file) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000549 return executeCommand([this, &remote_file, &local_file]() {
550 return internalPullFile(remote_file, local_file);
551 });
552}
553
Zachary Turner97206d52017-05-12 04:51:55 +0000554Status AdbClient::SyncService::PushFile(const FileSpec &local_file,
555 const FileSpec &remote_file) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000556 return executeCommand([this, &local_file, &remote_file]() {
557 return internalPushFile(local_file, remote_file);
558 });
559}
560
Zachary Turner97206d52017-05-12 04:51:55 +0000561Status AdbClient::SyncService::Stat(const FileSpec &remote_file, uint32_t &mode,
562 uint32_t &size, uint32_t &mtime) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000563 return executeCommand([this, &remote_file, &mode, &size, &mtime]() {
564 return internalStat(remote_file, mode, size, mtime);
565 });
566}
567
568bool AdbClient::SyncService::IsConnected() const {
569 return m_conn && m_conn->IsConnected();
570}
571
572AdbClient::SyncService::SyncService(std::unique_ptr<Connection> &&conn)
573 : m_conn(std::move(conn)) {}
574
Zachary Turner97206d52017-05-12 04:51:55 +0000575Status
576AdbClient::SyncService::executeCommand(const std::function<Status()> &cmd) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000577 if (!m_conn)
Zachary Turner97206d52017-05-12 04:51:55 +0000578 return Status("SyncService is disconnected");
Kate Stoneb9c1b512016-09-06 20:57:50 +0000579
580 const auto error = cmd();
581 if (error.Fail())
582 m_conn.reset();
583
584 return error;
585}
586
587AdbClient::SyncService::~SyncService() {}
588
Zachary Turner97206d52017-05-12 04:51:55 +0000589Status AdbClient::SyncService::SendSyncRequest(const char *request_id,
590 const uint32_t data_len,
591 const void *data) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000592 const DataBufferSP data_sp(new DataBufferHeap(kSyncPacketLen, 0));
593 DataEncoder encoder(data_sp, eByteOrderLittle, sizeof(void *));
594 auto offset = encoder.PutData(0, request_id, strlen(request_id));
595 encoder.PutU32(offset, data_len);
596
Zachary Turner97206d52017-05-12 04:51:55 +0000597 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000598 ConnectionStatus status;
599 m_conn->Write(data_sp->GetBytes(), kSyncPacketLen, status, &error);
600 if (error.Fail())
601 return error;
602
603 if (data)
604 m_conn->Write(data, data_len, status, &error);
605 return error;
606}
607
Zachary Turner97206d52017-05-12 04:51:55 +0000608Status AdbClient::SyncService::ReadSyncHeader(std::string &response_id,
609 uint32_t &data_len) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000610 char buffer[kSyncPacketLen];
611
612 auto error = ReadAllBytes(buffer, kSyncPacketLen);
613 if (error.Success()) {
614 response_id.assign(&buffer[0], 4);
615 DataExtractor extractor(&buffer[4], 4, eByteOrderLittle, sizeof(void *));
Oleksiy Vyalov6e181cf2016-06-30 18:10:27 +0000616 offset_t offset = 0;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000617 data_len = extractor.GetU32(&offset);
618 }
Oleksiy Vyalov6e181cf2016-06-30 18:10:27 +0000619
Kate Stoneb9c1b512016-09-06 20:57:50 +0000620 return error;
Oleksiy Vyalov6e181cf2016-06-30 18:10:27 +0000621}
622
Zachary Turner97206d52017-05-12 04:51:55 +0000623Status AdbClient::SyncService::PullFileChunk(std::vector<char> &buffer,
624 bool &eof) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000625 buffer.clear();
Oleksiy Vyalovd6a143f2016-07-06 17:02:42 +0000626
Kate Stoneb9c1b512016-09-06 20:57:50 +0000627 std::string response_id;
628 uint32_t data_len;
629 auto error = ReadSyncHeader(response_id, data_len);
630 if (error.Fail())
Oleksiy Vyalovd6a143f2016-07-06 17:02:42 +0000631 return error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000632
633 if (response_id == kDATA) {
634 buffer.resize(data_len, 0);
635 error = ReadAllBytes(&buffer[0], data_len);
636 if (error.Fail())
637 buffer.clear();
638 } else if (response_id == kDONE) {
639 eof = true;
640 } else if (response_id == kFAIL) {
641 std::string error_message(data_len, 0);
642 error = ReadAllBytes(&error_message[0], data_len);
643 if (error.Fail())
Zachary Turner97206d52017-05-12 04:51:55 +0000644 return Status("Failed to read pull error message: %s", error.AsCString());
645 return Status("Failed to pull file: %s", error_message.c_str());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000646 } else
Zachary Turner97206d52017-05-12 04:51:55 +0000647 return Status("Pull failed with unknown response: %s", response_id.c_str());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000648
Zachary Turner97206d52017-05-12 04:51:55 +0000649 return Status();
Oleksiy Vyalovd6a143f2016-07-06 17:02:42 +0000650}
651
Zachary Turner97206d52017-05-12 04:51:55 +0000652Status AdbClient::SyncService::ReadAllBytes(void *buffer, size_t size) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000653 return ::ReadAllBytes(*m_conn, buffer, size);
Oleksiy Vyalov6e181cf2016-06-30 18:10:27 +0000654}