blob: fbf6dee87bf1e1b2da019e47489877ce3870c953 [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/Core/DataBuffer.h"
19#include "lldb/Core/DataBufferHeap.h"
20#include "lldb/Core/DataEncoder.h"
21#include "lldb/Core/DataExtractor.h"
22#include "lldb/Core/StreamString.h"
23#include "lldb/Host/ConnectionFileDescriptor.h"
24#include "lldb/Host/FileSpec.h"
Pavel Labath1408bf72016-11-01 16:11:14 +000025#include "lldb/Host/FileSystem.h"
Zachary Turnerf343968f2016-08-09 23:06:08 +000026#include "lldb/Host/PosixApi.h"
Oleksiy Vyalov05a55de2015-03-23 21:03:02 +000027
Oleksiy Vyalov09e9079d2015-05-18 23:44:06 +000028#include <limits.h>
29
Oleksiy Vyalov6f001062015-03-25 17:58:13 +000030#include <algorithm>
Oleksiy Vyalovc6ac2e12016-07-08 17:45:37 +000031#include <cstdlib>
Oleksiy Vyalov09e9079d2015-05-18 23:44:06 +000032#include <fstream>
Oleksiy Vyalov05a55de2015-03-23 21:03:02 +000033#include <sstream>
34
Adrian McCarthy1341d4c2016-06-30 20:55:50 +000035// 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 Vyalov05a55de2015-03-23 21:03:02 +000041using namespace lldb;
42using namespace lldb_private;
Tamas Berghammerdb264a62015-03-31 09:52:22 +000043using namespace lldb_private::platform_android;
Oleksiy Vyalov05a55de2015-03-23 21:03:02 +000044
45namespace {
46
Pavel Labath204ef662016-05-16 11:41:36 +000047const std::chrono::seconds kReadTimeout(8);
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
Kate Stoneb9c1b512016-09-06 20:57:50 +000066Error ReadAllBytes(Connection &conn, void *buffer, size_t size) {
67 using namespace std::chrono;
Oleksiy Vyalov6e181cf2016-06-30 18:10:27 +000068
Kate Stoneb9c1b512016-09-06 20:57:50 +000069 Error error;
70 ConnectionStatus status;
71 char *read_buffer = static_cast<char *>(buffer);
Oleksiy Vyalov6e181cf2016-06-30 18:10:27 +000072
Kate Stoneb9c1b512016-09-06 20:57:50 +000073 auto now = steady_clock::now();
74 const auto deadline = now + kReadTimeout;
75 size_t total_read_bytes = 0;
76 while (total_read_bytes < size && now < deadline) {
77 uint32_t timeout_usec = duration_cast<microseconds>(deadline - now).count();
78 auto read_bytes =
79 conn.Read(read_buffer + total_read_bytes, size - total_read_bytes,
80 timeout_usec, status, &error);
Chaoren 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)
89 error = Error(
90 "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
Kate Stoneb9c1b512016-09-06 20:57:50 +000097Error AdbClient::CreateByDeviceID(const std::string &device_id,
98 AdbClient &adb) {
99 DeviceIDList connect_devices;
100 auto error = adb.GetDevices(connect_devices);
101 if (error.Fail())
Oleksiy 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)
112 return Error("Expected a single connected device, got instead %zu - try "
113 "setting 'ANDROID_SERIAL'",
114 connect_devices.size());
115 adb.SetDeviceID(connect_devices.front());
116 } else {
117 auto find_it = std::find(connect_devices.begin(), connect_devices.end(),
118 android_serial);
119 if (find_it == connect_devices.end())
120 return Error("Device \"%s\" not found", android_serial.c_str());
121
122 adb.SetDeviceID(*find_it);
123 }
124 return error;
Oleksiy 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
139Error AdbClient::Connect() {
140 Error error;
141 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
Kate Stoneb9c1b512016-09-06 20:57:50 +0000147Error AdbClient::GetDevices(DeviceIDList &device_list) {
148 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
168 // Force disconnect since ADB closes connection after host:devices
169 // response is sent.
170 m_conn.reset();
171 return error;
Oleksiy Vyalov05a55de2015-03-23 21:03:02 +0000172}
173
Kate Stoneb9c1b512016-09-06 20:57:50 +0000174Error AdbClient::SetPortForwarding(const uint16_t local_port,
175 const uint16_t remote_port) {
176 char message[48];
177 snprintf(message, sizeof(message), "forward:tcp:%d;tcp:%d", local_port,
178 remote_port);
Oleksiy 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
Kate Stoneb9c1b512016-09-06 20:57:50 +0000187Error AdbClient::SetPortForwarding(const uint16_t local_port,
Zachary Turner245f7fd2016-11-17 01:38:02 +0000188 llvm::StringRef remote_socket_name,
Kate Stoneb9c1b512016-09-06 20:57:50 +0000189 const UnixSocketNamespace socket_namespace) {
190 char message[PATH_MAX];
191 const char *sock_namespace_str =
192 (socket_namespace == UnixSocketNamespaceAbstract)
193 ? kSocketNamespaceAbstract
194 : kSocketNamespaceFileSystem;
195 snprintf(message, sizeof(message), "forward:tcp:%d;%s:%s", local_port,
Zachary Turner245f7fd2016-11-17 01:38:02 +0000196 sock_namespace_str, remote_socket_name.str().c_str());
Oleksiy Vyalov05a55de2015-03-23 21:03:02 +0000197
Kate Stoneb9c1b512016-09-06 20:57:50 +0000198 const auto error = SendDeviceMessage(message);
199 if (error.Fail())
Oleksiy Vyalov05a55de2015-03-23 21:03:02 +0000200 return error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000201
202 return ReadResponseStatus();
Oleksiy Vyalov05a55de2015-03-23 21:03:02 +0000203}
204
Kate Stoneb9c1b512016-09-06 20:57:50 +0000205Error AdbClient::DeletePortForwarding(const uint16_t local_port) {
206 char message[32];
207 snprintf(message, sizeof(message), "killforward:tcp:%d", local_port);
208
209 const auto error = SendDeviceMessage(message);
210 if (error.Fail())
211 return error;
212
213 return ReadResponseStatus();
214}
215
216Error AdbClient::SendMessage(const std::string &packet, const bool reconnect) {
217 Error error;
218 if (!m_conn || reconnect) {
219 error = Connect();
220 if (error.Fail())
221 return error;
222 }
223
224 char length_buffer[5];
225 snprintf(length_buffer, sizeof(length_buffer), "%04x",
226 static_cast<int>(packet.size()));
227
228 ConnectionStatus status;
229
230 m_conn->Write(length_buffer, 4, status, &error);
231 if (error.Fail())
232 return error;
233
234 m_conn->Write(packet.c_str(), packet.size(), status, &error);
235 return error;
236}
237
238Error AdbClient::SendDeviceMessage(const std::string &packet) {
239 std::ostringstream msg;
240 msg << "host-serial:" << m_device_id << ":" << packet;
241 return SendMessage(msg.str());
242}
243
244Error AdbClient::ReadMessage(std::vector<char> &message) {
245 message.clear();
246
247 char buffer[5];
248 buffer[4] = 0;
249
250 auto error = ReadAllBytes(buffer, 4);
251 if (error.Fail())
252 return error;
253
254 unsigned int packet_len = 0;
255 sscanf(buffer, "%x", &packet_len);
256
257 message.resize(packet_len, 0);
258 error = ReadAllBytes(&message[0], packet_len);
259 if (error.Fail())
Tamas Berghammer9d8dde82015-09-29 11:04:18 +0000260 message.clear();
261
Kate Stoneb9c1b512016-09-06 20:57:50 +0000262 return error;
263}
Tamas Berghammer9d8dde82015-09-29 11:04:18 +0000264
Kate Stoneb9c1b512016-09-06 20:57:50 +0000265Error AdbClient::ReadMessageStream(std::vector<char> &message,
266 uint32_t timeout_ms) {
267 auto start = std::chrono::steady_clock::now();
268 message.clear();
269
270 Error error;
271 lldb::ConnectionStatus status = lldb::eConnectionStatusSuccess;
272 char buffer[1024];
273 while (error.Success() && status == lldb::eConnectionStatusSuccess) {
274 auto end = std::chrono::steady_clock::now();
275 uint32_t elapsed_time =
276 std::chrono::duration_cast<std::chrono::milliseconds>(end - start)
277 .count();
278 if (elapsed_time >= timeout_ms)
279 return Error("Timed out");
280
281 size_t n = m_conn->Read(buffer, sizeof(buffer),
282 1000 * (timeout_ms - elapsed_time), status, &error);
283 if (n > 0)
284 message.insert(message.end(), &buffer[0], &buffer[n]);
285 }
286 return error;
287}
288
289Error AdbClient::ReadResponseStatus() {
290 char response_id[5];
291
292 static const size_t packet_len = 4;
293 response_id[packet_len] = 0;
294
295 auto error = ReadAllBytes(response_id, packet_len);
296 if (error.Fail())
Tamas 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
Kate Stoneb9c1b512016-09-06 20:57:50 +0000305Error AdbClient::GetResponseError(const char *response_id) {
306 if (strcmp(response_id, kFAIL) != 0)
307 return Error("Got unexpected response id from adb: \"%s\"", response_id);
Oleksiy 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
Kate Stoneb9c1b512016-09-06 20:57:50 +0000318Error AdbClient::SwitchDeviceTransport() {
319 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
Kate Stoneb9c1b512016-09-06 20:57:50 +0000329Error AdbClient::StartSync() {
330 auto error = SwitchDeviceTransport();
331 if (error.Fail())
332 return Error("Failed to switch to device transport: %s", error.AsCString());
Oleksiy Vyalov09e9079d2015-05-18 23:44:06 +0000333
Kate Stoneb9c1b512016-09-06 20:57:50 +0000334 error = Sync();
335 if (error.Fail())
336 return Error("Sync failed: %s", error.AsCString());
Oleksiy Vyalov09e9079d2015-05-18 23:44:06 +0000337
Kate Stoneb9c1b512016-09-06 20:57:50 +0000338 return error;
339}
340
341Error AdbClient::Sync() {
342 auto error = SendMessage("sync:", false);
343 if (error.Fail())
Oleksiy Vyalov09e9079d2015-05-18 23:44:06 +0000344 return error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000345
346 return ReadResponseStatus();
Oleksiy Vyalov09e9079d2015-05-18 23:44:06 +0000347}
348
Kate Stoneb9c1b512016-09-06 20:57:50 +0000349Error AdbClient::ReadAllBytes(void *buffer, size_t size) {
350 return ::ReadAllBytes(*m_conn, buffer, size);
Oleksiy Vyalov09e9079d2015-05-18 23:44:06 +0000351}
352
Kate Stoneb9c1b512016-09-06 20:57:50 +0000353Error AdbClient::internalShell(const char *command, uint32_t timeout_ms,
354 std::vector<char> &output_buf) {
355 output_buf.clear();
Oleksiy Vyalov6e181cf2016-06-30 18:10:27 +0000356
Kate Stoneb9c1b512016-09-06 20:57:50 +0000357 auto error = SwitchDeviceTransport();
358 if (error.Fail())
359 return Error("Failed to switch to device transport: %s", 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
371 error = ReadMessageStream(output_buf, timeout_ms);
372 if (error.Fail())
373 return error;
374
375 // ADB doesn't propagate return code of shell execution - if
376 // output starts with /system/bin/sh: most likely command failed.
377 static const char *kShellPrefix = "/system/bin/sh:";
378 if (output_buf.size() > strlen(kShellPrefix)) {
379 if (!memcmp(&output_buf[0], kShellPrefix, strlen(kShellPrefix)))
380 return Error("Shell command %s failed: %s", command,
381 std::string(output_buf.begin(), output_buf.end()).c_str());
382 }
383
Mehdi Aminic1edf562016-11-11 04:29:25 +0000384 return Error();
Oleksiy Vyalov6e181cf2016-06-30 18:10:27 +0000385}
386
Kate Stoneb9c1b512016-09-06 20:57:50 +0000387Error AdbClient::Shell(const char *command, uint32_t timeout_ms,
388 std::string *output) {
389 std::vector<char> output_buffer;
390 auto error = internalShell(command, timeout_ms, output_buffer);
391 if (error.Fail())
392 return error;
Oleksiy 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
Kate Stoneb9c1b512016-09-06 20:57:50 +0000399Error AdbClient::ShellToFile(const char *command, uint32_t timeout_ms,
400 const FileSpec &output_file_spec) {
401 std::vector<char> output_buffer;
402 auto error = internalShell(command, timeout_ms, output_buffer);
403 if (error.Fail())
404 return error;
405
406 const auto output_filename = output_file_spec.GetPath();
407 std::ofstream dst(output_filename, std::ios::out | std::ios::binary);
408 if (!dst.is_open())
409 return Error("Unable to open local file %s", output_filename.c_str());
410
411 dst.write(&output_buffer[0], output_buffer.size());
412 dst.close();
413 if (!dst)
414 return Error("Failed to write file %s", output_filename.c_str());
Mehdi Aminic1edf562016-11-11 04:29:25 +0000415 return Error();
Oleksiy Vyalovc6ac2e12016-07-08 17:45:37 +0000416}
417
Oleksiy Vyalov6e181cf2016-06-30 18:10:27 +0000418std::unique_ptr<AdbClient::SyncService>
Kate Stoneb9c1b512016-09-06 20:57:50 +0000419AdbClient::GetSyncService(Error &error) {
420 std::unique_ptr<SyncService> sync_service;
421 error = StartSync();
422 if (error.Success())
423 sync_service.reset(new SyncService(std::move(m_conn)));
Oleksiy Vyalov6e181cf2016-06-30 18:10:27 +0000424
Kate Stoneb9c1b512016-09-06 20:57:50 +0000425 return sync_service;
Oleksiy Vyalov6e181cf2016-06-30 18:10:27 +0000426}
427
Kate Stoneb9c1b512016-09-06 20:57:50 +0000428Error AdbClient::SyncService::internalPullFile(const FileSpec &remote_file,
429 const FileSpec &local_file) {
430 const auto local_file_path = local_file.GetPath();
Malcolm Parsons771ef6d2016-11-02 20:34:10 +0000431 llvm::FileRemover local_file_remover(local_file_path);
Oleksiy Vyalov09e9079d2015-05-18 23:44:06 +0000432
Kate Stoneb9c1b512016-09-06 20:57:50 +0000433 std::ofstream dst(local_file_path, std::ios::out | std::ios::binary);
434 if (!dst.is_open())
435 return Error("Unable to open local file %s", local_file_path.c_str());
Oleksiy Vyalov09e9079d2015-05-18 23:44:06 +0000436
Kate Stoneb9c1b512016-09-06 20:57:50 +0000437 const auto remote_file_path = remote_file.GetPath(false);
438 auto error = SendSyncRequest(kRECV, remote_file_path.length(),
439 remote_file_path.c_str());
440 if (error.Fail())
Oleksiy Vyalov09e9079d2015-05-18 23:44:06 +0000441 return error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000442
443 std::vector<char> chunk;
444 bool eof = false;
445 while (!eof) {
446 error = PullFileChunk(chunk, eof);
447 if (error.Fail())
448 return error;
449 if (!eof)
450 dst.write(&chunk[0], chunk.size());
451 }
452
453 local_file_remover.releaseFile();
454 return error;
Oleksiy Vyalov09e9079d2015-05-18 23:44:06 +0000455}
456
Kate Stoneb9c1b512016-09-06 20:57:50 +0000457Error AdbClient::SyncService::internalPushFile(const FileSpec &local_file,
458 const FileSpec &remote_file) {
459 const auto local_file_path(local_file.GetPath());
460 std::ifstream src(local_file_path.c_str(), std::ios::in | std::ios::binary);
461 if (!src.is_open())
462 return Error("Unable to open local file %s", local_file_path.c_str());
Robert Flack62efb1f2015-05-27 02:18:50 +0000463
Kate Stoneb9c1b512016-09-06 20:57:50 +0000464 std::stringstream file_description;
465 file_description << remote_file.GetPath(false).c_str() << "," << kDefaultMode;
466 std::string file_description_str = file_description.str();
467 auto error = SendSyncRequest(kSEND, file_description_str.length(),
468 file_description_str.c_str());
469 if (error.Fail())
Oleksiy Vyalov0b5ebef2015-05-28 17:42:48 +0000470 return error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000471
472 char chunk[kMaxPushData];
473 while (!src.eof() && !src.read(chunk, kMaxPushData).bad()) {
474 size_t chunk_size = src.gcount();
475 error = SendSyncRequest(kDATA, chunk_size, chunk);
476 if (error.Fail())
477 return Error("Failed to send file chunk: %s", error.AsCString());
478 }
Pavel Labath1408bf72016-11-01 16:11:14 +0000479 error = SendSyncRequest(
480 kDONE, std::chrono::duration_cast<std::chrono::seconds>(
481 FileSystem::GetModificationTime(local_file).time_since_epoch())
482 .count(),
483 nullptr);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000484 if (error.Fail())
485 return error;
486
487 std::string response_id;
488 uint32_t data_len;
489 error = ReadSyncHeader(response_id, data_len);
490 if (error.Fail())
491 return Error("Failed to read DONE response: %s", error.AsCString());
492 if (response_id == kFAIL) {
493 std::string error_message(data_len, 0);
494 error = ReadAllBytes(&error_message[0], data_len);
495 if (error.Fail())
496 return Error("Failed to read DONE error message: %s", error.AsCString());
497 return Error("Failed to push file: %s", error_message.c_str());
498 } else if (response_id != kOKAY)
499 return Error("Got unexpected DONE response: %s", response_id.c_str());
500
501 // If there was an error reading the source file, finish the adb file
502 // transfer first so that adb isn't expecting any more data.
503 if (src.bad())
504 return Error("Failed read on %s", local_file_path.c_str());
505 return error;
Oleksiy Vyalov0b5ebef2015-05-28 17:42:48 +0000506}
507
Kate Stoneb9c1b512016-09-06 20:57:50 +0000508Error AdbClient::SyncService::internalStat(const FileSpec &remote_file,
509 uint32_t &mode, uint32_t &size,
510 uint32_t &mtime) {
511 const std::string remote_file_path(remote_file.GetPath(false));
512 auto error = SendSyncRequest(kSTAT, remote_file_path.length(),
513 remote_file_path.c_str());
514 if (error.Fail())
515 return Error("Failed to send request: %s", error.AsCString());
Oleksiy Vyalov0b5ebef2015-05-28 17:42:48 +0000516
Kate Stoneb9c1b512016-09-06 20:57:50 +0000517 static const size_t stat_len = strlen(kSTAT);
518 static const size_t response_len = stat_len + (sizeof(uint32_t) * 3);
Oleksiy Vyalov6e181cf2016-06-30 18:10:27 +0000519
Kate Stoneb9c1b512016-09-06 20:57:50 +0000520 std::vector<char> buffer(response_len);
521 error = ReadAllBytes(&buffer[0], buffer.size());
522 if (error.Fail())
523 return Error("Failed to read response: %s", error.AsCString());
Oleksiy Vyalov6e181cf2016-06-30 18:10:27 +0000524
Kate Stoneb9c1b512016-09-06 20:57:50 +0000525 DataExtractor extractor(&buffer[0], buffer.size(), eByteOrderLittle,
526 sizeof(void *));
527 offset_t offset = 0;
528
529 const void *command = extractor.GetData(&offset, stat_len);
530 if (!command)
531 return Error("Failed to get response command");
532 const char *command_str = static_cast<const char *>(command);
533 if (strncmp(command_str, kSTAT, stat_len))
534 return Error("Got invalid stat command: %s", command_str);
535
536 mode = extractor.GetU32(&offset);
537 size = extractor.GetU32(&offset);
538 mtime = extractor.GetU32(&offset);
Mehdi Aminic1edf562016-11-11 04:29:25 +0000539 return Error();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000540}
541
542Error AdbClient::SyncService::PullFile(const FileSpec &remote_file,
543 const FileSpec &local_file) {
544 return executeCommand([this, &remote_file, &local_file]() {
545 return internalPullFile(remote_file, local_file);
546 });
547}
548
549Error AdbClient::SyncService::PushFile(const FileSpec &local_file,
550 const FileSpec &remote_file) {
551 return executeCommand([this, &local_file, &remote_file]() {
552 return internalPushFile(local_file, remote_file);
553 });
554}
555
556Error AdbClient::SyncService::Stat(const FileSpec &remote_file, uint32_t &mode,
557 uint32_t &size, uint32_t &mtime) {
558 return executeCommand([this, &remote_file, &mode, &size, &mtime]() {
559 return internalStat(remote_file, mode, size, mtime);
560 });
561}
562
563bool AdbClient::SyncService::IsConnected() const {
564 return m_conn && m_conn->IsConnected();
565}
566
567AdbClient::SyncService::SyncService(std::unique_ptr<Connection> &&conn)
568 : m_conn(std::move(conn)) {}
569
570Error AdbClient::SyncService::executeCommand(
571 const std::function<Error()> &cmd) {
572 if (!m_conn)
573 return Error("SyncService is disconnected");
574
575 const auto error = cmd();
576 if (error.Fail())
577 m_conn.reset();
578
579 return error;
580}
581
582AdbClient::SyncService::~SyncService() {}
583
584Error AdbClient::SyncService::SendSyncRequest(const char *request_id,
585 const uint32_t data_len,
586 const void *data) {
587 const DataBufferSP data_sp(new DataBufferHeap(kSyncPacketLen, 0));
588 DataEncoder encoder(data_sp, eByteOrderLittle, sizeof(void *));
589 auto offset = encoder.PutData(0, request_id, strlen(request_id));
590 encoder.PutU32(offset, data_len);
591
592 Error error;
593 ConnectionStatus status;
594 m_conn->Write(data_sp->GetBytes(), kSyncPacketLen, status, &error);
595 if (error.Fail())
596 return error;
597
598 if (data)
599 m_conn->Write(data, data_len, status, &error);
600 return error;
601}
602
603Error AdbClient::SyncService::ReadSyncHeader(std::string &response_id,
604 uint32_t &data_len) {
605 char buffer[kSyncPacketLen];
606
607 auto error = ReadAllBytes(buffer, kSyncPacketLen);
608 if (error.Success()) {
609 response_id.assign(&buffer[0], 4);
610 DataExtractor extractor(&buffer[4], 4, eByteOrderLittle, sizeof(void *));
Oleksiy Vyalov6e181cf2016-06-30 18:10:27 +0000611 offset_t offset = 0;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000612 data_len = extractor.GetU32(&offset);
613 }
Oleksiy Vyalov6e181cf2016-06-30 18:10:27 +0000614
Kate Stoneb9c1b512016-09-06 20:57:50 +0000615 return error;
Oleksiy Vyalov6e181cf2016-06-30 18:10:27 +0000616}
617
Kate Stoneb9c1b512016-09-06 20:57:50 +0000618Error AdbClient::SyncService::PullFileChunk(std::vector<char> &buffer,
619 bool &eof) {
620 buffer.clear();
Oleksiy Vyalovd6a143f2016-07-06 17:02:42 +0000621
Kate Stoneb9c1b512016-09-06 20:57:50 +0000622 std::string response_id;
623 uint32_t data_len;
624 auto error = ReadSyncHeader(response_id, data_len);
625 if (error.Fail())
Oleksiy Vyalovd6a143f2016-07-06 17:02:42 +0000626 return error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000627
628 if (response_id == kDATA) {
629 buffer.resize(data_len, 0);
630 error = ReadAllBytes(&buffer[0], data_len);
631 if (error.Fail())
632 buffer.clear();
633 } else if (response_id == kDONE) {
634 eof = true;
635 } else if (response_id == kFAIL) {
636 std::string error_message(data_len, 0);
637 error = ReadAllBytes(&error_message[0], data_len);
638 if (error.Fail())
639 return Error("Failed to read pull error message: %s", error.AsCString());
640 return Error("Failed to pull file: %s", error_message.c_str());
641 } else
642 return Error("Pull failed with unknown response: %s", response_id.c_str());
643
Mehdi Aminic1edf562016-11-11 04:29:25 +0000644 return Error();
Oleksiy Vyalovd6a143f2016-07-06 17:02:42 +0000645}
646
Kate Stoneb9c1b512016-09-06 20:57:50 +0000647Error AdbClient::SyncService::ReadAllBytes(void *buffer, size_t size) {
648 return ::ReadAllBytes(*m_conn, buffer, size);
Oleksiy Vyalov6e181cf2016-06-30 18:10:27 +0000649}