blob: e99018d88cb2a93f15632dc6fc5d38d776e47348 [file] [log] [blame]
Oleksiy Vyalov05a55de2015-03-23 21:03:02 +00001//===-- AdbClient.cpp -------------------------------------------*- C++ -*-===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Oleksiy Vyalov05a55de2015-03-23 21:03:02 +00006//
7//===----------------------------------------------------------------------===//
8
Oleksiy Vyalov6e181cf2016-06-30 18:10:27 +00009#include "AdbClient.h"
10
Kate Stoneb9c1b512016-09-06 20:57:50 +000011#include "llvm/ADT/STLExtras.h"
Oleksiy Vyalov05a55de2015-03-23 21:03:02 +000012#include "llvm/ADT/SmallVector.h"
13#include "llvm/ADT/StringRef.h"
Oleksiy Vyalov09e9079d2015-05-18 23:44:06 +000014#include "llvm/Support/FileUtilities.h"
Oleksiy Vyalov05a55de2015-03-23 21:03:02 +000015
Oleksiy Vyalov6e181cf2016-06-30 18:10:27 +000016#include "lldb/Host/ConnectionFileDescriptor.h"
Pavel Labath1408bf72016-11-01 16:11:14 +000017#include "lldb/Host/FileSystem.h"
Zachary Turnerf343968f2016-08-09 23:06:08 +000018#include "lldb/Host/PosixApi.h"
Zachary Turner666cc0b2017-03-04 01:30:05 +000019#include "lldb/Utility/DataBuffer.h"
20#include "lldb/Utility/DataBufferHeap.h"
21#include "lldb/Utility/DataEncoder.h"
22#include "lldb/Utility/DataExtractor.h"
Zachary Turner5713a052017-03-22 18:40:07 +000023#include "lldb/Utility/FileSpec.h"
Zachary Turnerbf9a7732017-02-02 21:39:50 +000024#include "lldb/Utility/StreamString.h"
Zachary Turner2f3df612017-04-06 21:28:29 +000025#include "lldb/Utility/Timeout.h"
Oleksiy Vyalov05a55de2015-03-23 21:03:02 +000026
Oleksiy Vyalov09e9079d2015-05-18 23:44:06 +000027#include <limits.h>
28
Oleksiy Vyalov6f001062015-03-25 17:58:13 +000029#include <algorithm>
Oleksiy Vyalovc6ac2e12016-07-08 17:45:37 +000030#include <cstdlib>
Oleksiy Vyalov09e9079d2015-05-18 23:44:06 +000031#include <fstream>
Oleksiy Vyalov05a55de2015-03-23 21:03:02 +000032#include <sstream>
33
Adrian McCarthy1341d4c2016-06-30 20:55:50 +000034// On Windows, transitive dependencies pull in <Windows.h>, which defines a
35// macro that clashes with a method name.
36#ifdef SendMessage
37#undef SendMessage
38#endif
39
Oleksiy Vyalov05a55de2015-03-23 21:03:02 +000040using namespace lldb;
41using namespace lldb_private;
Tamas Berghammerdb264a62015-03-31 09:52:22 +000042using namespace lldb_private::platform_android;
Pavel Labath818dd512016-11-24 14:03:57 +000043using namespace std::chrono;
Oleksiy Vyalov05a55de2015-03-23 21:03:02 +000044
45namespace {
46
Pavel Labathaee3e262017-10-31 12:27:46 +000047const seconds kReadTimeout(20);
Kate Stoneb9c1b512016-09-06 20:57:50 +000048const char *kOKAY = "OKAY";
49const char *kFAIL = "FAIL";
50const char *kDATA = "DATA";
51const char *kDONE = "DONE";
Oleksiy Vyalov6002a312015-06-05 01:32:45 +000052
Kate Stoneb9c1b512016-09-06 20:57:50 +000053const char *kSEND = "SEND";
54const char *kRECV = "RECV";
55const char *kSTAT = "STAT";
Oleksiy Vyalov6002a312015-06-05 01:32:45 +000056
Oleksiy Vyalov09e9079d2015-05-18 23:44:06 +000057const size_t kSyncPacketLen = 8;
Robert Flack62efb1f2015-05-27 02:18:50 +000058// Maximum size of a filesync DATA packet.
Kate Stoneb9c1b512016-09-06 20:57:50 +000059const size_t kMaxPushData = 2 * 1024;
Robert Flack62efb1f2015-05-27 02:18:50 +000060// Default mode for pushed files.
61const uint32_t kDefaultMode = 0100770; // S_IFREG | S_IRWXU | S_IRWXG
Oleksiy Vyalov05a55de2015-03-23 21:03:02 +000062
Kate Stoneb9c1b512016-09-06 20:57:50 +000063const char *kSocketNamespaceAbstract = "localabstract";
64const char *kSocketNamespaceFileSystem = "localfilesystem";
Oleksiy Vyalove7df5f52015-11-03 01:37:01 +000065
Zachary Turner97206d52017-05-12 04:51:55 +000066Status ReadAllBytes(Connection &conn, void *buffer, size_t size) {
Oleksiy Vyalov6e181cf2016-06-30 18:10:27 +000067
Zachary Turner97206d52017-05-12 04:51:55 +000068 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +000069 ConnectionStatus status;
70 char *read_buffer = static_cast<char *>(buffer);
Oleksiy Vyalov6e181cf2016-06-30 18:10:27 +000071
Kate Stoneb9c1b512016-09-06 20:57:50 +000072 auto now = steady_clock::now();
73 const auto deadline = now + kReadTimeout;
74 size_t total_read_bytes = 0;
75 while (total_read_bytes < size && now < deadline) {
Kate Stoneb9c1b512016-09-06 20:57:50 +000076 auto read_bytes =
77 conn.Read(read_buffer + total_read_bytes, size - total_read_bytes,
Pavel Labath2f159a52016-11-25 12:22:32 +000078 duration_cast<microseconds>(deadline - now), status, &error);
Chaoren Lin3ea689b2015-05-01 16:49:28 +000079 if (error.Fail())
Kate Stoneb9c1b512016-09-06 20:57:50 +000080 return error;
81 total_read_bytes += read_bytes;
82 if (status != eConnectionStatusSuccess)
83 break;
84 now = steady_clock::now();
85 }
86 if (total_read_bytes < size)
Zachary Turner97206d52017-05-12 04:51:55 +000087 error = Status(
Kate Stoneb9c1b512016-09-06 20:57:50 +000088 "Unable to read requested number of bytes. Connection status: %d.",
89 status);
90 return error;
91}
Oleksiy Vyalov6f001062015-03-25 17:58:13 +000092
Kate Stoneb9c1b512016-09-06 20:57:50 +000093} // namespace
Luke Drummond3db04912016-07-07 18:02:44 +000094
Zachary Turner97206d52017-05-12 04:51:55 +000095Status AdbClient::CreateByDeviceID(const std::string &device_id,
96 AdbClient &adb) {
Kate Stoneb9c1b512016-09-06 20:57:50 +000097 DeviceIDList connect_devices;
98 auto error = adb.GetDevices(connect_devices);
99 if (error.Fail())
Oleksiy Vyalov6f001062015-03-25 17:58:13 +0000100 return error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000101
102 std::string android_serial;
103 if (!device_id.empty())
104 android_serial = device_id;
105 else if (const char *env_serial = std::getenv("ANDROID_SERIAL"))
106 android_serial = env_serial;
107
108 if (android_serial.empty()) {
109 if (connect_devices.size() != 1)
Zachary Turner97206d52017-05-12 04:51:55 +0000110 return Status("Expected a single connected device, got instead %zu - try "
111 "setting 'ANDROID_SERIAL'",
112 connect_devices.size());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000113 adb.SetDeviceID(connect_devices.front());
114 } else {
115 auto find_it = std::find(connect_devices.begin(), connect_devices.end(),
116 android_serial);
117 if (find_it == connect_devices.end())
Zachary Turner97206d52017-05-12 04:51:55 +0000118 return Status("Device \"%s\" not found", android_serial.c_str());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000119
120 adb.SetDeviceID(*find_it);
121 }
122 return error;
Oleksiy Vyalov6f001062015-03-25 17:58:13 +0000123}
124
Kate Stoneb9c1b512016-09-06 20:57:50 +0000125AdbClient::AdbClient() {}
Oleksiy Vyalov6e181cf2016-06-30 18:10:27 +0000126
Kate Stoneb9c1b512016-09-06 20:57:50 +0000127AdbClient::AdbClient(const std::string &device_id) : m_device_id(device_id) {}
Oleksiy Vyalov05a55de2015-03-23 21:03:02 +0000128
Oleksiy Vyalov6e181cf2016-06-30 18:10:27 +0000129AdbClient::~AdbClient() {}
130
Kate Stoneb9c1b512016-09-06 20:57:50 +0000131void AdbClient::SetDeviceID(const std::string &device_id) {
132 m_device_id = device_id;
Oleksiy Vyalov05a55de2015-03-23 21:03:02 +0000133}
134
Kate Stoneb9c1b512016-09-06 20:57:50 +0000135const std::string &AdbClient::GetDeviceID() const { return m_device_id; }
136
Zachary Turner97206d52017-05-12 04:51:55 +0000137Status AdbClient::Connect() {
138 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000139 m_conn.reset(new ConnectionFileDescriptor);
140 m_conn->Connect("connect://localhost:5037", &error);
141
142 return error;
Oleksiy Vyalov6f001062015-03-25 17:58:13 +0000143}
144
Zachary Turner97206d52017-05-12 04:51:55 +0000145Status AdbClient::GetDevices(DeviceIDList &device_list) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000146 device_list.clear();
Oleksiy Vyalov05a55de2015-03-23 21:03:02 +0000147
Kate Stoneb9c1b512016-09-06 20:57:50 +0000148 auto error = SendMessage("host:devices");
149 if (error.Fail())
Oleksiy Vyalov05a55de2015-03-23 21:03:02 +0000150 return error;
Oleksiy Vyalov05a55de2015-03-23 21:03:02 +0000151
Kate Stoneb9c1b512016-09-06 20:57:50 +0000152 error = ReadResponseStatus();
153 if (error.Fail())
Oleksiy Vyalov05a55de2015-03-23 21:03:02 +0000154 return error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000155
156 std::vector<char> in_buffer;
157 error = ReadMessage(in_buffer);
158
159 llvm::StringRef response(&in_buffer[0], in_buffer.size());
160 llvm::SmallVector<llvm::StringRef, 4> devices;
161 response.split(devices, "\n", -1, false);
162
163 for (const auto device : devices)
164 device_list.push_back(device.split('\t').first);
165
Adrian Prantl05097242018-04-30 16:49:04 +0000166 // Force disconnect since ADB closes connection after host:devices response
167 // is sent.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000168 m_conn.reset();
169 return error;
Oleksiy Vyalov05a55de2015-03-23 21:03:02 +0000170}
171
Zachary Turner97206d52017-05-12 04:51:55 +0000172Status AdbClient::SetPortForwarding(const uint16_t local_port,
173 const uint16_t remote_port) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000174 char message[48];
175 snprintf(message, sizeof(message), "forward:tcp:%d;tcp:%d", local_port,
176 remote_port);
Oleksiy Vyalov05a55de2015-03-23 21:03:02 +0000177
Kate Stoneb9c1b512016-09-06 20:57:50 +0000178 const auto error = SendDeviceMessage(message);
179 if (error.Fail())
Oleksiy Vyalov05a55de2015-03-23 21:03:02 +0000180 return error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000181
182 return ReadResponseStatus();
Oleksiy Vyalov05a55de2015-03-23 21:03:02 +0000183}
184
Zachary Turner97206d52017-05-12 04:51:55 +0000185Status
186AdbClient::SetPortForwarding(const uint16_t local_port,
187 llvm::StringRef remote_socket_name,
188 const UnixSocketNamespace socket_namespace) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000189 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 Turner245f7fd2016-11-17 01:38:02 +0000195 sock_namespace_str, remote_socket_name.str().c_str());
Oleksiy Vyalov05a55de2015-03-23 21:03:02 +0000196
Kate Stoneb9c1b512016-09-06 20:57:50 +0000197 const auto error = SendDeviceMessage(message);
198 if (error.Fail())
Oleksiy Vyalov05a55de2015-03-23 21:03:02 +0000199 return error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000200
201 return ReadResponseStatus();
Oleksiy Vyalov05a55de2015-03-23 21:03:02 +0000202}
203
Zachary Turner97206d52017-05-12 04:51:55 +0000204Status AdbClient::DeletePortForwarding(const uint16_t local_port) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000205 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
Zachary Turner97206d52017-05-12 04:51:55 +0000215Status AdbClient::SendMessage(const std::string &packet, const bool reconnect) {
216 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000217 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
Zachary Turner97206d52017-05-12 04:51:55 +0000237Status AdbClient::SendDeviceMessage(const std::string &packet) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000238 std::ostringstream msg;
239 msg << "host-serial:" << m_device_id << ":" << packet;
240 return SendMessage(msg.str());
241}
242
Zachary Turner97206d52017-05-12 04:51:55 +0000243Status AdbClient::ReadMessage(std::vector<char> &message) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000244 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 Berghammer9d8dde82015-09-29 11:04:18 +0000259 message.clear();
260
Kate Stoneb9c1b512016-09-06 20:57:50 +0000261 return error;
262}
Tamas Berghammer9d8dde82015-09-29 11:04:18 +0000263
Zachary Turner97206d52017-05-12 04:51:55 +0000264Status AdbClient::ReadMessageStream(std::vector<char> &message,
265 milliseconds timeout) {
Pavel Labath818dd512016-11-24 14:03:57 +0000266 auto start = steady_clock::now();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000267 message.clear();
268
Zachary Turner97206d52017-05-12 04:51:55 +0000269 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000270 lldb::ConnectionStatus status = lldb::eConnectionStatusSuccess;
271 char buffer[1024];
272 while (error.Success() && status == lldb::eConnectionStatusSuccess) {
Pavel Labath818dd512016-11-24 14:03:57 +0000273 auto end = steady_clock::now();
274 auto elapsed = end - start;
275 if (elapsed >= timeout)
Zachary Turner97206d52017-05-12 04:51:55 +0000276 return Status("Timed out");
Kate Stoneb9c1b512016-09-06 20:57:50 +0000277
Pavel Labath2f159a52016-11-25 12:22:32 +0000278 size_t n = m_conn->Read(buffer, sizeof(buffer),
279 duration_cast<microseconds>(timeout - elapsed),
280 status, &error);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000281 if (n > 0)
282 message.insert(message.end(), &buffer[0], &buffer[n]);
283 }
284 return error;
285}
286
Zachary Turner97206d52017-05-12 04:51:55 +0000287Status AdbClient::ReadResponseStatus() {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000288 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 Berghammer9d8dde82015-09-29 11:04:18 +0000295 return error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000296
297 if (strncmp(response_id, kOKAY, packet_len) != 0)
298 return GetResponseError(response_id);
299
300 return error;
Tamas Berghammer9d8dde82015-09-29 11:04:18 +0000301}
302
Zachary Turner97206d52017-05-12 04:51:55 +0000303Status AdbClient::GetResponseError(const char *response_id) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000304 if (strcmp(response_id, kFAIL) != 0)
Zachary Turner97206d52017-05-12 04:51:55 +0000305 return Status("Got unexpected response id from adb: \"%s\"", response_id);
Oleksiy Vyalov05a55de2015-03-23 21:03:02 +0000306
Kate Stoneb9c1b512016-09-06 20:57:50 +0000307 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 Vyalov05a55de2015-03-23 21:03:02 +0000312
Kate Stoneb9c1b512016-09-06 20:57:50 +0000313 return error;
314}
Oleksiy Vyalov05a55de2015-03-23 21:03:02 +0000315
Zachary Turner97206d52017-05-12 04:51:55 +0000316Status AdbClient::SwitchDeviceTransport() {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000317 std::ostringstream msg;
318 msg << "host:transport:" << m_device_id;
Oleksiy Vyalov09e9079d2015-05-18 23:44:06 +0000319
Kate Stoneb9c1b512016-09-06 20:57:50 +0000320 auto error = SendMessage(msg.str());
321 if (error.Fail())
Oleksiy Vyalov09e9079d2015-05-18 23:44:06 +0000322 return error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000323
324 return ReadResponseStatus();
Oleksiy Vyalov09e9079d2015-05-18 23:44:06 +0000325}
326
Zachary Turner97206d52017-05-12 04:51:55 +0000327Status AdbClient::StartSync() {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000328 auto error = SwitchDeviceTransport();
329 if (error.Fail())
Zachary Turner97206d52017-05-12 04:51:55 +0000330 return Status("Failed to switch to device transport: %s",
331 error.AsCString());
Oleksiy Vyalov09e9079d2015-05-18 23:44:06 +0000332
Kate Stoneb9c1b512016-09-06 20:57:50 +0000333 error = Sync();
334 if (error.Fail())
Zachary Turner97206d52017-05-12 04:51:55 +0000335 return Status("Sync failed: %s", error.AsCString());
Oleksiy Vyalov09e9079d2015-05-18 23:44:06 +0000336
Kate Stoneb9c1b512016-09-06 20:57:50 +0000337 return error;
338}
339
Zachary Turner97206d52017-05-12 04:51:55 +0000340Status AdbClient::Sync() {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000341 auto error = SendMessage("sync:", false);
342 if (error.Fail())
Oleksiy Vyalov09e9079d2015-05-18 23:44:06 +0000343 return error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000344
345 return ReadResponseStatus();
Oleksiy Vyalov09e9079d2015-05-18 23:44:06 +0000346}
347
Zachary Turner97206d52017-05-12 04:51:55 +0000348Status AdbClient::ReadAllBytes(void *buffer, size_t size) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000349 return ::ReadAllBytes(*m_conn, buffer, size);
Oleksiy Vyalov09e9079d2015-05-18 23:44:06 +0000350}
351
Zachary Turner97206d52017-05-12 04:51:55 +0000352Status AdbClient::internalShell(const char *command, milliseconds timeout,
353 std::vector<char> &output_buf) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000354 output_buf.clear();
Oleksiy Vyalov6e181cf2016-06-30 18:10:27 +0000355
Kate Stoneb9c1b512016-09-06 20:57:50 +0000356 auto error = SwitchDeviceTransport();
357 if (error.Fail())
Zachary Turner97206d52017-05-12 04:51:55 +0000358 return Status("Failed to switch to device transport: %s",
359 error.AsCString());
Oleksiy Vyalov6e181cf2016-06-30 18:10:27 +0000360
Kate Stoneb9c1b512016-09-06 20:57:50 +0000361 StreamString adb_command;
362 adb_command.Printf("shell:%s", command);
Zachary Turnerc1564272016-11-16 21:15:24 +0000363 error = SendMessage(adb_command.GetString(), false);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000364 if (error.Fail())
Oleksiy Vyalov6e181cf2016-06-30 18:10:27 +0000365 return error;
Oleksiy Vyalov6e181cf2016-06-30 18:10:27 +0000366
Kate Stoneb9c1b512016-09-06 20:57:50 +0000367 error = ReadResponseStatus();
368 if (error.Fail())
Oleksiy Vyalov6e181cf2016-06-30 18:10:27 +0000369 return error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000370
Pavel Labath818dd512016-11-24 14:03:57 +0000371 error = ReadMessageStream(output_buf, timeout);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000372 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)))
Zachary Turner97206d52017-05-12 04:51:55 +0000380 return Status("Shell command %s failed: %s", command,
381 std::string(output_buf.begin(), output_buf.end()).c_str());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000382 }
383
Zachary Turner97206d52017-05-12 04:51:55 +0000384 return Status();
Oleksiy Vyalov6e181cf2016-06-30 18:10:27 +0000385}
386
Zachary Turner97206d52017-05-12 04:51:55 +0000387Status AdbClient::Shell(const char *command, milliseconds timeout,
388 std::string *output) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000389 std::vector<char> output_buffer;
Pavel Labathce255ff2016-11-24 14:11:47 +0000390 auto error = internalShell(command, timeout, output_buffer);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000391 if (error.Fail())
392 return error;
Oleksiy Vyalovc6ac2e12016-07-08 17:45:37 +0000393
Kate Stoneb9c1b512016-09-06 20:57:50 +0000394 if (output)
395 output->assign(output_buffer.begin(), output_buffer.end());
396 return error;
397}
Oleksiy Vyalovc6ac2e12016-07-08 17:45:37 +0000398
Zachary Turner97206d52017-05-12 04:51:55 +0000399Status AdbClient::ShellToFile(const char *command, milliseconds timeout,
400 const FileSpec &output_file_spec) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000401 std::vector<char> output_buffer;
Pavel Labathce255ff2016-11-24 14:11:47 +0000402 auto error = internalShell(command, timeout, output_buffer);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000403 if (error.Fail())
404 return error;
405
406 const auto output_filename = output_file_spec.GetPath();
Pavel Labathe3ad2e22017-03-21 13:49:50 +0000407 std::error_code EC;
408 llvm::raw_fd_ostream dst(output_filename, EC, llvm::sys::fs::F_None);
409 if (EC)
Zachary Turner97206d52017-05-12 04:51:55 +0000410 return Status("Unable to open local file %s", output_filename.c_str());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000411
412 dst.write(&output_buffer[0], output_buffer.size());
413 dst.close();
Pavel Labathe3ad2e22017-03-21 13:49:50 +0000414 if (dst.has_error())
Zachary Turner97206d52017-05-12 04:51:55 +0000415 return Status("Failed to write file %s", output_filename.c_str());
416 return Status();
Oleksiy Vyalovc6ac2e12016-07-08 17:45:37 +0000417}
418
Oleksiy Vyalov6e181cf2016-06-30 18:10:27 +0000419std::unique_ptr<AdbClient::SyncService>
Zachary Turner97206d52017-05-12 04:51:55 +0000420AdbClient::GetSyncService(Status &error) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000421 std::unique_ptr<SyncService> sync_service;
422 error = StartSync();
423 if (error.Success())
424 sync_service.reset(new SyncService(std::move(m_conn)));
Oleksiy Vyalov6e181cf2016-06-30 18:10:27 +0000425
Kate Stoneb9c1b512016-09-06 20:57:50 +0000426 return sync_service;
Oleksiy Vyalov6e181cf2016-06-30 18:10:27 +0000427}
428
Zachary Turner97206d52017-05-12 04:51:55 +0000429Status AdbClient::SyncService::internalPullFile(const FileSpec &remote_file,
430 const FileSpec &local_file) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000431 const auto local_file_path = local_file.GetPath();
Malcolm Parsons771ef6d2016-11-02 20:34:10 +0000432 llvm::FileRemover local_file_remover(local_file_path);
Oleksiy Vyalov09e9079d2015-05-18 23:44:06 +0000433
Pavel Labathe3ad2e22017-03-21 13:49:50 +0000434 std::error_code EC;
435 llvm::raw_fd_ostream dst(local_file_path, EC, llvm::sys::fs::F_None);
436 if (EC)
Zachary Turner97206d52017-05-12 04:51:55 +0000437 return Status("Unable to open local file %s", local_file_path.c_str());
Oleksiy Vyalov09e9079d2015-05-18 23:44:06 +0000438
Kate Stoneb9c1b512016-09-06 20:57:50 +0000439 const auto remote_file_path = remote_file.GetPath(false);
440 auto error = SendSyncRequest(kRECV, remote_file_path.length(),
441 remote_file_path.c_str());
442 if (error.Fail())
Oleksiy Vyalov09e9079d2015-05-18 23:44:06 +0000443 return error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000444
445 std::vector<char> chunk;
446 bool eof = false;
447 while (!eof) {
448 error = PullFileChunk(chunk, eof);
449 if (error.Fail())
450 return error;
451 if (!eof)
452 dst.write(&chunk[0], chunk.size());
453 }
Pavel Labathe3ad2e22017-03-21 13:49:50 +0000454 dst.close();
455 if (dst.has_error())
Zachary Turner97206d52017-05-12 04:51:55 +0000456 return Status("Failed to write file %s", local_file_path.c_str());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000457
458 local_file_remover.releaseFile();
459 return error;
Oleksiy Vyalov09e9079d2015-05-18 23:44:06 +0000460}
461
Zachary Turner97206d52017-05-12 04:51:55 +0000462Status AdbClient::SyncService::internalPushFile(const FileSpec &local_file,
463 const FileSpec &remote_file) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000464 const auto local_file_path(local_file.GetPath());
465 std::ifstream src(local_file_path.c_str(), std::ios::in | std::ios::binary);
466 if (!src.is_open())
Zachary Turner97206d52017-05-12 04:51:55 +0000467 return Status("Unable to open local file %s", local_file_path.c_str());
Robert Flack62efb1f2015-05-27 02:18:50 +0000468
Kate Stoneb9c1b512016-09-06 20:57:50 +0000469 std::stringstream file_description;
470 file_description << remote_file.GetPath(false).c_str() << "," << kDefaultMode;
471 std::string file_description_str = file_description.str();
472 auto error = SendSyncRequest(kSEND, file_description_str.length(),
473 file_description_str.c_str());
474 if (error.Fail())
Oleksiy Vyalov0b5ebef2015-05-28 17:42:48 +0000475 return error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000476
477 char chunk[kMaxPushData];
478 while (!src.eof() && !src.read(chunk, kMaxPushData).bad()) {
479 size_t chunk_size = src.gcount();
480 error = SendSyncRequest(kDATA, chunk_size, chunk);
481 if (error.Fail())
Zachary Turner97206d52017-05-12 04:51:55 +0000482 return Status("Failed to send file chunk: %s", error.AsCString());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000483 }
Pavel Labath1408bf72016-11-01 16:11:14 +0000484 error = SendSyncRequest(
Jonas Devlieghere46376962018-10-31 21:49:27 +0000485 kDONE, llvm::sys::toTimeT(FileSystem::Instance().GetModificationTime(local_file)),
Pavel Labath1408bf72016-11-01 16:11:14 +0000486 nullptr);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000487 if (error.Fail())
488 return error;
489
490 std::string response_id;
491 uint32_t data_len;
492 error = ReadSyncHeader(response_id, data_len);
493 if (error.Fail())
Zachary Turner97206d52017-05-12 04:51:55 +0000494 return Status("Failed to read DONE response: %s", error.AsCString());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000495 if (response_id == kFAIL) {
496 std::string error_message(data_len, 0);
497 error = ReadAllBytes(&error_message[0], data_len);
498 if (error.Fail())
Zachary Turner97206d52017-05-12 04:51:55 +0000499 return Status("Failed to read DONE error message: %s", error.AsCString());
500 return Status("Failed to push file: %s", error_message.c_str());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000501 } else if (response_id != kOKAY)
Zachary Turner97206d52017-05-12 04:51:55 +0000502 return Status("Got unexpected DONE response: %s", response_id.c_str());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000503
504 // If there was an error reading the source file, finish the adb file
505 // transfer first so that adb isn't expecting any more data.
506 if (src.bad())
Zachary Turner97206d52017-05-12 04:51:55 +0000507 return Status("Failed read on %s", local_file_path.c_str());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000508 return error;
Oleksiy Vyalov0b5ebef2015-05-28 17:42:48 +0000509}
510
Zachary Turner97206d52017-05-12 04:51:55 +0000511Status AdbClient::SyncService::internalStat(const FileSpec &remote_file,
512 uint32_t &mode, uint32_t &size,
513 uint32_t &mtime) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000514 const std::string remote_file_path(remote_file.GetPath(false));
515 auto error = SendSyncRequest(kSTAT, remote_file_path.length(),
516 remote_file_path.c_str());
517 if (error.Fail())
Zachary Turner97206d52017-05-12 04:51:55 +0000518 return Status("Failed to send request: %s", error.AsCString());
Oleksiy Vyalov0b5ebef2015-05-28 17:42:48 +0000519
Kate Stoneb9c1b512016-09-06 20:57:50 +0000520 static const size_t stat_len = strlen(kSTAT);
521 static const size_t response_len = stat_len + (sizeof(uint32_t) * 3);
Oleksiy Vyalov6e181cf2016-06-30 18:10:27 +0000522
Kate Stoneb9c1b512016-09-06 20:57:50 +0000523 std::vector<char> buffer(response_len);
524 error = ReadAllBytes(&buffer[0], buffer.size());
525 if (error.Fail())
Zachary Turner97206d52017-05-12 04:51:55 +0000526 return Status("Failed to read response: %s", error.AsCString());
Oleksiy Vyalov6e181cf2016-06-30 18:10:27 +0000527
Kate Stoneb9c1b512016-09-06 20:57:50 +0000528 DataExtractor extractor(&buffer[0], buffer.size(), eByteOrderLittle,
529 sizeof(void *));
530 offset_t offset = 0;
531
532 const void *command = extractor.GetData(&offset, stat_len);
533 if (!command)
Zachary Turner97206d52017-05-12 04:51:55 +0000534 return Status("Failed to get response command");
Kate Stoneb9c1b512016-09-06 20:57:50 +0000535 const char *command_str = static_cast<const char *>(command);
536 if (strncmp(command_str, kSTAT, stat_len))
Zachary Turner97206d52017-05-12 04:51:55 +0000537 return Status("Got invalid stat command: %s", command_str);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000538
539 mode = extractor.GetU32(&offset);
540 size = extractor.GetU32(&offset);
541 mtime = extractor.GetU32(&offset);
Zachary Turner97206d52017-05-12 04:51:55 +0000542 return Status();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000543}
544
Zachary Turner97206d52017-05-12 04:51:55 +0000545Status AdbClient::SyncService::PullFile(const FileSpec &remote_file,
546 const FileSpec &local_file) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000547 return executeCommand([this, &remote_file, &local_file]() {
548 return internalPullFile(remote_file, local_file);
549 });
550}
551
Zachary Turner97206d52017-05-12 04:51:55 +0000552Status AdbClient::SyncService::PushFile(const FileSpec &local_file,
553 const FileSpec &remote_file) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000554 return executeCommand([this, &local_file, &remote_file]() {
555 return internalPushFile(local_file, remote_file);
556 });
557}
558
Zachary Turner97206d52017-05-12 04:51:55 +0000559Status AdbClient::SyncService::Stat(const FileSpec &remote_file, uint32_t &mode,
560 uint32_t &size, uint32_t &mtime) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000561 return executeCommand([this, &remote_file, &mode, &size, &mtime]() {
562 return internalStat(remote_file, mode, size, mtime);
563 });
564}
565
566bool AdbClient::SyncService::IsConnected() const {
567 return m_conn && m_conn->IsConnected();
568}
569
570AdbClient::SyncService::SyncService(std::unique_ptr<Connection> &&conn)
571 : m_conn(std::move(conn)) {}
572
Zachary Turner97206d52017-05-12 04:51:55 +0000573Status
574AdbClient::SyncService::executeCommand(const std::function<Status()> &cmd) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000575 if (!m_conn)
Zachary Turner97206d52017-05-12 04:51:55 +0000576 return Status("SyncService is disconnected");
Kate Stoneb9c1b512016-09-06 20:57:50 +0000577
578 const auto error = cmd();
579 if (error.Fail())
580 m_conn.reset();
581
582 return error;
583}
584
585AdbClient::SyncService::~SyncService() {}
586
Zachary Turner97206d52017-05-12 04:51:55 +0000587Status AdbClient::SyncService::SendSyncRequest(const char *request_id,
588 const uint32_t data_len,
589 const void *data) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000590 const DataBufferSP data_sp(new DataBufferHeap(kSyncPacketLen, 0));
591 DataEncoder encoder(data_sp, eByteOrderLittle, sizeof(void *));
592 auto offset = encoder.PutData(0, request_id, strlen(request_id));
593 encoder.PutU32(offset, data_len);
594
Zachary Turner97206d52017-05-12 04:51:55 +0000595 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000596 ConnectionStatus status;
597 m_conn->Write(data_sp->GetBytes(), kSyncPacketLen, status, &error);
598 if (error.Fail())
599 return error;
600
601 if (data)
602 m_conn->Write(data, data_len, status, &error);
603 return error;
604}
605
Zachary Turner97206d52017-05-12 04:51:55 +0000606Status AdbClient::SyncService::ReadSyncHeader(std::string &response_id,
607 uint32_t &data_len) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000608 char buffer[kSyncPacketLen];
609
610 auto error = ReadAllBytes(buffer, kSyncPacketLen);
611 if (error.Success()) {
612 response_id.assign(&buffer[0], 4);
613 DataExtractor extractor(&buffer[4], 4, eByteOrderLittle, sizeof(void *));
Oleksiy Vyalov6e181cf2016-06-30 18:10:27 +0000614 offset_t offset = 0;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000615 data_len = extractor.GetU32(&offset);
616 }
Oleksiy Vyalov6e181cf2016-06-30 18:10:27 +0000617
Kate Stoneb9c1b512016-09-06 20:57:50 +0000618 return error;
Oleksiy Vyalov6e181cf2016-06-30 18:10:27 +0000619}
620
Zachary Turner97206d52017-05-12 04:51:55 +0000621Status AdbClient::SyncService::PullFileChunk(std::vector<char> &buffer,
622 bool &eof) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000623 buffer.clear();
Oleksiy Vyalovd6a143f2016-07-06 17:02:42 +0000624
Kate Stoneb9c1b512016-09-06 20:57:50 +0000625 std::string response_id;
626 uint32_t data_len;
627 auto error = ReadSyncHeader(response_id, data_len);
628 if (error.Fail())
Oleksiy Vyalovd6a143f2016-07-06 17:02:42 +0000629 return error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000630
631 if (response_id == kDATA) {
632 buffer.resize(data_len, 0);
633 error = ReadAllBytes(&buffer[0], data_len);
634 if (error.Fail())
635 buffer.clear();
636 } else if (response_id == kDONE) {
637 eof = true;
638 } else if (response_id == kFAIL) {
639 std::string error_message(data_len, 0);
640 error = ReadAllBytes(&error_message[0], data_len);
641 if (error.Fail())
Zachary Turner97206d52017-05-12 04:51:55 +0000642 return Status("Failed to read pull error message: %s", error.AsCString());
643 return Status("Failed to pull file: %s", error_message.c_str());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000644 } else
Zachary Turner97206d52017-05-12 04:51:55 +0000645 return Status("Pull failed with unknown response: %s", response_id.c_str());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000646
Zachary Turner97206d52017-05-12 04:51:55 +0000647 return Status();
Oleksiy Vyalovd6a143f2016-07-06 17:02:42 +0000648}
649
Zachary Turner97206d52017-05-12 04:51:55 +0000650Status AdbClient::SyncService::ReadAllBytes(void *buffer, size_t size) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000651 return ::ReadAllBytes(*m_conn, buffer, size);
Oleksiy Vyalov6e181cf2016-06-30 18:10:27 +0000652}