blob: d7492285c977c1cdb0607493e4464ce32c258963 [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"
Zachary Turnerf343968f2016-08-09 23:06:08 +000025#include "lldb/Host/PosixApi.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;
Oleksiy Vyalov05a55de2015-03-23 21:03:02 +000043
44namespace {
45
Pavel Labath204ef662016-05-16 11:41:36 +000046const std::chrono::seconds kReadTimeout(8);
Kate Stoneb9c1b512016-09-06 20:57:50 +000047const char *kOKAY = "OKAY";
48const char *kFAIL = "FAIL";
49const char *kDATA = "DATA";
50const char *kDONE = "DONE";
Oleksiy Vyalov6002a312015-06-05 01:32:45 +000051
Kate Stoneb9c1b512016-09-06 20:57:50 +000052const char *kSEND = "SEND";
53const char *kRECV = "RECV";
54const char *kSTAT = "STAT";
Oleksiy Vyalov6002a312015-06-05 01:32:45 +000055
Oleksiy Vyalov09e9079d2015-05-18 23:44:06 +000056const size_t kSyncPacketLen = 8;
Robert Flack62efb1f2015-05-27 02:18:50 +000057// Maximum size of a filesync DATA packet.
Kate Stoneb9c1b512016-09-06 20:57:50 +000058const size_t kMaxPushData = 2 * 1024;
Robert Flack62efb1f2015-05-27 02:18:50 +000059// Default mode for pushed files.
60const uint32_t kDefaultMode = 0100770; // S_IFREG | S_IRWXU | S_IRWXG
Oleksiy Vyalov05a55de2015-03-23 21:03:02 +000061
Kate Stoneb9c1b512016-09-06 20:57:50 +000062const char *kSocketNamespaceAbstract = "localabstract";
63const char *kSocketNamespaceFileSystem = "localfilesystem";
Oleksiy Vyalove7df5f52015-11-03 01:37:01 +000064
Kate Stoneb9c1b512016-09-06 20:57:50 +000065Error ReadAllBytes(Connection &conn, void *buffer, size_t size) {
66 using namespace std::chrono;
Oleksiy Vyalov6e181cf2016-06-30 18:10:27 +000067
Kate Stoneb9c1b512016-09-06 20:57:50 +000068 Error error;
69 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) {
76 uint32_t timeout_usec = duration_cast<microseconds>(deadline - now).count();
77 auto read_bytes =
78 conn.Read(read_buffer + total_read_bytes, size - total_read_bytes,
79 timeout_usec, status, &error);
Chaoren Lin3ea689b2015-05-01 16:49:28 +000080 if (error.Fail())
Kate Stoneb9c1b512016-09-06 20:57:50 +000081 return error;
82 total_read_bytes += read_bytes;
83 if (status != eConnectionStatusSuccess)
84 break;
85 now = steady_clock::now();
86 }
87 if (total_read_bytes < size)
88 error = Error(
89 "Unable to read requested number of bytes. Connection status: %d.",
90 status);
91 return error;
92}
Oleksiy Vyalov6f001062015-03-25 17:58:13 +000093
Kate Stoneb9c1b512016-09-06 20:57:50 +000094} // namespace
Luke Drummond3db04912016-07-07 18:02:44 +000095
Kate Stoneb9c1b512016-09-06 20:57:50 +000096Error AdbClient::CreateByDeviceID(const std::string &device_id,
97 AdbClient &adb) {
98 DeviceIDList connect_devices;
99 auto error = adb.GetDevices(connect_devices);
100 if (error.Fail())
Oleksiy Vyalov6f001062015-03-25 17:58:13 +0000101 return error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000102
103 std::string android_serial;
104 if (!device_id.empty())
105 android_serial = device_id;
106 else if (const char *env_serial = std::getenv("ANDROID_SERIAL"))
107 android_serial = env_serial;
108
109 if (android_serial.empty()) {
110 if (connect_devices.size() != 1)
111 return Error("Expected a single connected device, got instead %zu - try "
112 "setting 'ANDROID_SERIAL'",
113 connect_devices.size());
114 adb.SetDeviceID(connect_devices.front());
115 } else {
116 auto find_it = std::find(connect_devices.begin(), connect_devices.end(),
117 android_serial);
118 if (find_it == connect_devices.end())
119 return Error("Device \"%s\" not found", android_serial.c_str());
120
121 adb.SetDeviceID(*find_it);
122 }
123 return error;
Oleksiy Vyalov6f001062015-03-25 17:58:13 +0000124}
125
Kate Stoneb9c1b512016-09-06 20:57:50 +0000126AdbClient::AdbClient() {}
Oleksiy Vyalov6e181cf2016-06-30 18:10:27 +0000127
Kate Stoneb9c1b512016-09-06 20:57:50 +0000128AdbClient::AdbClient(const std::string &device_id) : m_device_id(device_id) {}
Oleksiy Vyalov05a55de2015-03-23 21:03:02 +0000129
Oleksiy Vyalov6e181cf2016-06-30 18:10:27 +0000130AdbClient::~AdbClient() {}
131
Kate Stoneb9c1b512016-09-06 20:57:50 +0000132void AdbClient::SetDeviceID(const std::string &device_id) {
133 m_device_id = device_id;
Oleksiy Vyalov05a55de2015-03-23 21:03:02 +0000134}
135
Kate Stoneb9c1b512016-09-06 20:57:50 +0000136const std::string &AdbClient::GetDeviceID() const { return m_device_id; }
137
138Error AdbClient::Connect() {
139 Error error;
140 m_conn.reset(new ConnectionFileDescriptor);
141 m_conn->Connect("connect://localhost:5037", &error);
142
143 return error;
Oleksiy Vyalov6f001062015-03-25 17:58:13 +0000144}
145
Kate Stoneb9c1b512016-09-06 20:57:50 +0000146Error AdbClient::GetDevices(DeviceIDList &device_list) {
147 device_list.clear();
Oleksiy Vyalov05a55de2015-03-23 21:03:02 +0000148
Kate Stoneb9c1b512016-09-06 20:57:50 +0000149 auto error = SendMessage("host:devices");
150 if (error.Fail())
Oleksiy Vyalov05a55de2015-03-23 21:03:02 +0000151 return error;
Oleksiy Vyalov05a55de2015-03-23 21:03:02 +0000152
Kate Stoneb9c1b512016-09-06 20:57:50 +0000153 error = ReadResponseStatus();
154 if (error.Fail())
Oleksiy Vyalov05a55de2015-03-23 21:03:02 +0000155 return error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000156
157 std::vector<char> in_buffer;
158 error = ReadMessage(in_buffer);
159
160 llvm::StringRef response(&in_buffer[0], in_buffer.size());
161 llvm::SmallVector<llvm::StringRef, 4> devices;
162 response.split(devices, "\n", -1, false);
163
164 for (const auto device : devices)
165 device_list.push_back(device.split('\t').first);
166
167 // Force disconnect since ADB closes connection after host:devices
168 // response is sent.
169 m_conn.reset();
170 return error;
Oleksiy Vyalov05a55de2015-03-23 21:03:02 +0000171}
172
Kate Stoneb9c1b512016-09-06 20:57:50 +0000173Error AdbClient::SetPortForwarding(const uint16_t local_port,
174 const uint16_t remote_port) {
175 char message[48];
176 snprintf(message, sizeof(message), "forward:tcp:%d;tcp:%d", local_port,
177 remote_port);
Oleksiy Vyalov05a55de2015-03-23 21:03:02 +0000178
Kate Stoneb9c1b512016-09-06 20:57:50 +0000179 const auto error = SendDeviceMessage(message);
180 if (error.Fail())
Oleksiy Vyalov05a55de2015-03-23 21:03:02 +0000181 return error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000182
183 return ReadResponseStatus();
Oleksiy Vyalov05a55de2015-03-23 21:03:02 +0000184}
185
Kate Stoneb9c1b512016-09-06 20:57:50 +0000186Error AdbClient::SetPortForwarding(const uint16_t local_port,
187 const char *remote_socket_name,
188 const UnixSocketNamespace socket_namespace) {
189 char message[PATH_MAX];
190 const char *sock_namespace_str =
191 (socket_namespace == UnixSocketNamespaceAbstract)
192 ? kSocketNamespaceAbstract
193 : kSocketNamespaceFileSystem;
194 snprintf(message, sizeof(message), "forward:tcp:%d;%s:%s", local_port,
195 sock_namespace_str, remote_socket_name);
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
Kate Stoneb9c1b512016-09-06 20:57:50 +0000204Error AdbClient::DeletePortForwarding(const uint16_t local_port) {
205 char message[32];
206 snprintf(message, sizeof(message), "killforward:tcp:%d", local_port);
207
208 const auto error = SendDeviceMessage(message);
209 if (error.Fail())
210 return error;
211
212 return ReadResponseStatus();
213}
214
215Error AdbClient::SendMessage(const std::string &packet, const bool reconnect) {
216 Error error;
217 if (!m_conn || reconnect) {
218 error = Connect();
219 if (error.Fail())
220 return error;
221 }
222
223 char length_buffer[5];
224 snprintf(length_buffer, sizeof(length_buffer), "%04x",
225 static_cast<int>(packet.size()));
226
227 ConnectionStatus status;
228
229 m_conn->Write(length_buffer, 4, status, &error);
230 if (error.Fail())
231 return error;
232
233 m_conn->Write(packet.c_str(), packet.size(), status, &error);
234 return error;
235}
236
237Error AdbClient::SendDeviceMessage(const std::string &packet) {
238 std::ostringstream msg;
239 msg << "host-serial:" << m_device_id << ":" << packet;
240 return SendMessage(msg.str());
241}
242
243Error AdbClient::ReadMessage(std::vector<char> &message) {
244 message.clear();
245
246 char buffer[5];
247 buffer[4] = 0;
248
249 auto error = ReadAllBytes(buffer, 4);
250 if (error.Fail())
251 return error;
252
253 unsigned int packet_len = 0;
254 sscanf(buffer, "%x", &packet_len);
255
256 message.resize(packet_len, 0);
257 error = ReadAllBytes(&message[0], packet_len);
258 if (error.Fail())
Tamas 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
Kate Stoneb9c1b512016-09-06 20:57:50 +0000264Error AdbClient::ReadMessageStream(std::vector<char> &message,
265 uint32_t timeout_ms) {
266 auto start = std::chrono::steady_clock::now();
267 message.clear();
268
269 Error error;
270 lldb::ConnectionStatus status = lldb::eConnectionStatusSuccess;
271 char buffer[1024];
272 while (error.Success() && status == lldb::eConnectionStatusSuccess) {
273 auto end = std::chrono::steady_clock::now();
274 uint32_t elapsed_time =
275 std::chrono::duration_cast<std::chrono::milliseconds>(end - start)
276 .count();
277 if (elapsed_time >= timeout_ms)
278 return Error("Timed out");
279
280 size_t n = m_conn->Read(buffer, sizeof(buffer),
281 1000 * (timeout_ms - elapsed_time), status, &error);
282 if (n > 0)
283 message.insert(message.end(), &buffer[0], &buffer[n]);
284 }
285 return error;
286}
287
288Error AdbClient::ReadResponseStatus() {
289 char response_id[5];
290
291 static const size_t packet_len = 4;
292 response_id[packet_len] = 0;
293
294 auto error = ReadAllBytes(response_id, packet_len);
295 if (error.Fail())
Tamas Berghammer9d8dde82015-09-29 11:04:18 +0000296 return error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000297
298 if (strncmp(response_id, kOKAY, packet_len) != 0)
299 return GetResponseError(response_id);
300
301 return error;
Tamas Berghammer9d8dde82015-09-29 11:04:18 +0000302}
303
Kate Stoneb9c1b512016-09-06 20:57:50 +0000304Error AdbClient::GetResponseError(const char *response_id) {
305 if (strcmp(response_id, kFAIL) != 0)
306 return Error("Got unexpected response id from adb: \"%s\"", response_id);
Oleksiy Vyalov05a55de2015-03-23 21:03:02 +0000307
Kate Stoneb9c1b512016-09-06 20:57:50 +0000308 std::vector<char> error_message;
309 auto error = ReadMessage(error_message);
310 if (error.Success())
311 error.SetErrorString(
312 std::string(&error_message[0], error_message.size()).c_str());
Oleksiy Vyalov05a55de2015-03-23 21:03:02 +0000313
Kate Stoneb9c1b512016-09-06 20:57:50 +0000314 return error;
315}
Oleksiy Vyalov05a55de2015-03-23 21:03:02 +0000316
Kate Stoneb9c1b512016-09-06 20:57:50 +0000317Error AdbClient::SwitchDeviceTransport() {
318 std::ostringstream msg;
319 msg << "host:transport:" << m_device_id;
Oleksiy Vyalov09e9079d2015-05-18 23:44:06 +0000320
Kate Stoneb9c1b512016-09-06 20:57:50 +0000321 auto error = SendMessage(msg.str());
322 if (error.Fail())
Oleksiy Vyalov09e9079d2015-05-18 23:44:06 +0000323 return error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000324
325 return ReadResponseStatus();
Oleksiy Vyalov09e9079d2015-05-18 23:44:06 +0000326}
327
Kate Stoneb9c1b512016-09-06 20:57:50 +0000328Error AdbClient::StartSync() {
329 auto error = SwitchDeviceTransport();
330 if (error.Fail())
331 return Error("Failed to switch to device transport: %s", error.AsCString());
Oleksiy Vyalov09e9079d2015-05-18 23:44:06 +0000332
Kate Stoneb9c1b512016-09-06 20:57:50 +0000333 error = Sync();
334 if (error.Fail())
335 return Error("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
340Error AdbClient::Sync() {
341 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
Kate Stoneb9c1b512016-09-06 20:57:50 +0000348Error AdbClient::ReadAllBytes(void *buffer, size_t size) {
349 return ::ReadAllBytes(*m_conn, buffer, size);
Oleksiy Vyalov09e9079d2015-05-18 23:44:06 +0000350}
351
Kate Stoneb9c1b512016-09-06 20:57:50 +0000352Error AdbClient::internalShell(const char *command, uint32_t timeout_ms,
353 std::vector<char> &output_buf) {
354 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())
358 return Error("Failed to switch to device transport: %s", error.AsCString());
Oleksiy Vyalov6e181cf2016-06-30 18:10:27 +0000359
Kate Stoneb9c1b512016-09-06 20:57:50 +0000360 StreamString adb_command;
361 adb_command.Printf("shell:%s", command);
362 error = SendMessage(adb_command.GetData(), false);
363 if (error.Fail())
Oleksiy Vyalov6e181cf2016-06-30 18:10:27 +0000364 return error;
Oleksiy Vyalov6e181cf2016-06-30 18:10:27 +0000365
Kate Stoneb9c1b512016-09-06 20:57:50 +0000366 error = ReadResponseStatus();
367 if (error.Fail())
Oleksiy Vyalov6e181cf2016-06-30 18:10:27 +0000368 return error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000369
370 error = ReadMessageStream(output_buf, timeout_ms);
371 if (error.Fail())
372 return error;
373
374 // ADB doesn't propagate return code of shell execution - if
375 // output starts with /system/bin/sh: most likely command failed.
376 static const char *kShellPrefix = "/system/bin/sh:";
377 if (output_buf.size() > strlen(kShellPrefix)) {
378 if (!memcmp(&output_buf[0], kShellPrefix, strlen(kShellPrefix)))
379 return Error("Shell command %s failed: %s", command,
380 std::string(output_buf.begin(), output_buf.end()).c_str());
381 }
382
383 return Error();
Oleksiy Vyalov6e181cf2016-06-30 18:10:27 +0000384}
385
Kate Stoneb9c1b512016-09-06 20:57:50 +0000386Error AdbClient::Shell(const char *command, uint32_t timeout_ms,
387 std::string *output) {
388 std::vector<char> output_buffer;
389 auto error = internalShell(command, timeout_ms, output_buffer);
390 if (error.Fail())
391 return error;
Oleksiy Vyalovc6ac2e12016-07-08 17:45:37 +0000392
Kate Stoneb9c1b512016-09-06 20:57:50 +0000393 if (output)
394 output->assign(output_buffer.begin(), output_buffer.end());
395 return error;
396}
Oleksiy Vyalovc6ac2e12016-07-08 17:45:37 +0000397
Kate Stoneb9c1b512016-09-06 20:57:50 +0000398Error AdbClient::ShellToFile(const char *command, uint32_t timeout_ms,
399 const FileSpec &output_file_spec) {
400 std::vector<char> output_buffer;
401 auto error = internalShell(command, timeout_ms, output_buffer);
402 if (error.Fail())
403 return error;
404
405 const auto output_filename = output_file_spec.GetPath();
406 std::ofstream dst(output_filename, std::ios::out | std::ios::binary);
407 if (!dst.is_open())
408 return Error("Unable to open local file %s", output_filename.c_str());
409
410 dst.write(&output_buffer[0], output_buffer.size());
411 dst.close();
412 if (!dst)
413 return Error("Failed to write file %s", output_filename.c_str());
414 return Error();
Oleksiy Vyalovc6ac2e12016-07-08 17:45:37 +0000415}
416
Oleksiy Vyalov6e181cf2016-06-30 18:10:27 +0000417std::unique_ptr<AdbClient::SyncService>
Kate Stoneb9c1b512016-09-06 20:57:50 +0000418AdbClient::GetSyncService(Error &error) {
419 std::unique_ptr<SyncService> sync_service;
420 error = StartSync();
421 if (error.Success())
422 sync_service.reset(new SyncService(std::move(m_conn)));
Oleksiy Vyalov6e181cf2016-06-30 18:10:27 +0000423
Kate Stoneb9c1b512016-09-06 20:57:50 +0000424 return sync_service;
Oleksiy Vyalov6e181cf2016-06-30 18:10:27 +0000425}
426
Kate Stoneb9c1b512016-09-06 20:57:50 +0000427Error AdbClient::SyncService::internalPullFile(const FileSpec &remote_file,
428 const FileSpec &local_file) {
429 const auto local_file_path = local_file.GetPath();
430 llvm::FileRemover local_file_remover(local_file_path.c_str());
Oleksiy Vyalov09e9079d2015-05-18 23:44:06 +0000431
Kate Stoneb9c1b512016-09-06 20:57:50 +0000432 std::ofstream dst(local_file_path, std::ios::out | std::ios::binary);
433 if (!dst.is_open())
434 return Error("Unable to open local file %s", local_file_path.c_str());
Oleksiy Vyalov09e9079d2015-05-18 23:44:06 +0000435
Kate Stoneb9c1b512016-09-06 20:57:50 +0000436 const auto remote_file_path = remote_file.GetPath(false);
437 auto error = SendSyncRequest(kRECV, remote_file_path.length(),
438 remote_file_path.c_str());
439 if (error.Fail())
Oleksiy Vyalov09e9079d2015-05-18 23:44:06 +0000440 return error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000441
442 std::vector<char> chunk;
443 bool eof = false;
444 while (!eof) {
445 error = PullFileChunk(chunk, eof);
446 if (error.Fail())
447 return error;
448 if (!eof)
449 dst.write(&chunk[0], chunk.size());
450 }
451
452 local_file_remover.releaseFile();
453 return error;
Oleksiy Vyalov09e9079d2015-05-18 23:44:06 +0000454}
455
Kate Stoneb9c1b512016-09-06 20:57:50 +0000456Error AdbClient::SyncService::internalPushFile(const FileSpec &local_file,
457 const FileSpec &remote_file) {
458 const auto local_file_path(local_file.GetPath());
459 std::ifstream src(local_file_path.c_str(), std::ios::in | std::ios::binary);
460 if (!src.is_open())
461 return Error("Unable to open local file %s", local_file_path.c_str());
Robert Flack62efb1f2015-05-27 02:18:50 +0000462
Kate Stoneb9c1b512016-09-06 20:57:50 +0000463 std::stringstream file_description;
464 file_description << remote_file.GetPath(false).c_str() << "," << kDefaultMode;
465 std::string file_description_str = file_description.str();
466 auto error = SendSyncRequest(kSEND, file_description_str.length(),
467 file_description_str.c_str());
468 if (error.Fail())
Oleksiy Vyalov0b5ebef2015-05-28 17:42:48 +0000469 return error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000470
471 char chunk[kMaxPushData];
472 while (!src.eof() && !src.read(chunk, kMaxPushData).bad()) {
473 size_t chunk_size = src.gcount();
474 error = SendSyncRequest(kDATA, chunk_size, chunk);
475 if (error.Fail())
476 return Error("Failed to send file chunk: %s", error.AsCString());
477 }
478 error = SendSyncRequest(kDONE, local_file.GetModificationTime().seconds(),
479 nullptr);
480 if (error.Fail())
481 return error;
482
483 std::string response_id;
484 uint32_t data_len;
485 error = ReadSyncHeader(response_id, data_len);
486 if (error.Fail())
487 return Error("Failed to read DONE response: %s", error.AsCString());
488 if (response_id == kFAIL) {
489 std::string error_message(data_len, 0);
490 error = ReadAllBytes(&error_message[0], data_len);
491 if (error.Fail())
492 return Error("Failed to read DONE error message: %s", error.AsCString());
493 return Error("Failed to push file: %s", error_message.c_str());
494 } else if (response_id != kOKAY)
495 return Error("Got unexpected DONE response: %s", response_id.c_str());
496
497 // If there was an error reading the source file, finish the adb file
498 // transfer first so that adb isn't expecting any more data.
499 if (src.bad())
500 return Error("Failed read on %s", local_file_path.c_str());
501 return error;
Oleksiy Vyalov0b5ebef2015-05-28 17:42:48 +0000502}
503
Kate Stoneb9c1b512016-09-06 20:57:50 +0000504Error AdbClient::SyncService::internalStat(const FileSpec &remote_file,
505 uint32_t &mode, uint32_t &size,
506 uint32_t &mtime) {
507 const std::string remote_file_path(remote_file.GetPath(false));
508 auto error = SendSyncRequest(kSTAT, remote_file_path.length(),
509 remote_file_path.c_str());
510 if (error.Fail())
511 return Error("Failed to send request: %s", error.AsCString());
Oleksiy Vyalov0b5ebef2015-05-28 17:42:48 +0000512
Kate Stoneb9c1b512016-09-06 20:57:50 +0000513 static const size_t stat_len = strlen(kSTAT);
514 static const size_t response_len = stat_len + (sizeof(uint32_t) * 3);
Oleksiy Vyalov6e181cf2016-06-30 18:10:27 +0000515
Kate Stoneb9c1b512016-09-06 20:57:50 +0000516 std::vector<char> buffer(response_len);
517 error = ReadAllBytes(&buffer[0], buffer.size());
518 if (error.Fail())
519 return Error("Failed to read response: %s", error.AsCString());
Oleksiy Vyalov6e181cf2016-06-30 18:10:27 +0000520
Kate Stoneb9c1b512016-09-06 20:57:50 +0000521 DataExtractor extractor(&buffer[0], buffer.size(), eByteOrderLittle,
522 sizeof(void *));
523 offset_t offset = 0;
524
525 const void *command = extractor.GetData(&offset, stat_len);
526 if (!command)
527 return Error("Failed to get response command");
528 const char *command_str = static_cast<const char *>(command);
529 if (strncmp(command_str, kSTAT, stat_len))
530 return Error("Got invalid stat command: %s", command_str);
531
532 mode = extractor.GetU32(&offset);
533 size = extractor.GetU32(&offset);
534 mtime = extractor.GetU32(&offset);
535 return Error();
536}
537
538Error AdbClient::SyncService::PullFile(const FileSpec &remote_file,
539 const FileSpec &local_file) {
540 return executeCommand([this, &remote_file, &local_file]() {
541 return internalPullFile(remote_file, local_file);
542 });
543}
544
545Error AdbClient::SyncService::PushFile(const FileSpec &local_file,
546 const FileSpec &remote_file) {
547 return executeCommand([this, &local_file, &remote_file]() {
548 return internalPushFile(local_file, remote_file);
549 });
550}
551
552Error AdbClient::SyncService::Stat(const FileSpec &remote_file, uint32_t &mode,
553 uint32_t &size, uint32_t &mtime) {
554 return executeCommand([this, &remote_file, &mode, &size, &mtime]() {
555 return internalStat(remote_file, mode, size, mtime);
556 });
557}
558
559bool AdbClient::SyncService::IsConnected() const {
560 return m_conn && m_conn->IsConnected();
561}
562
563AdbClient::SyncService::SyncService(std::unique_ptr<Connection> &&conn)
564 : m_conn(std::move(conn)) {}
565
566Error AdbClient::SyncService::executeCommand(
567 const std::function<Error()> &cmd) {
568 if (!m_conn)
569 return Error("SyncService is disconnected");
570
571 const auto error = cmd();
572 if (error.Fail())
573 m_conn.reset();
574
575 return error;
576}
577
578AdbClient::SyncService::~SyncService() {}
579
580Error AdbClient::SyncService::SendSyncRequest(const char *request_id,
581 const uint32_t data_len,
582 const void *data) {
583 const DataBufferSP data_sp(new DataBufferHeap(kSyncPacketLen, 0));
584 DataEncoder encoder(data_sp, eByteOrderLittle, sizeof(void *));
585 auto offset = encoder.PutData(0, request_id, strlen(request_id));
586 encoder.PutU32(offset, data_len);
587
588 Error error;
589 ConnectionStatus status;
590 m_conn->Write(data_sp->GetBytes(), kSyncPacketLen, status, &error);
591 if (error.Fail())
592 return error;
593
594 if (data)
595 m_conn->Write(data, data_len, status, &error);
596 return error;
597}
598
599Error AdbClient::SyncService::ReadSyncHeader(std::string &response_id,
600 uint32_t &data_len) {
601 char buffer[kSyncPacketLen];
602
603 auto error = ReadAllBytes(buffer, kSyncPacketLen);
604 if (error.Success()) {
605 response_id.assign(&buffer[0], 4);
606 DataExtractor extractor(&buffer[4], 4, eByteOrderLittle, sizeof(void *));
Oleksiy Vyalov6e181cf2016-06-30 18:10:27 +0000607 offset_t offset = 0;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000608 data_len = extractor.GetU32(&offset);
609 }
Oleksiy Vyalov6e181cf2016-06-30 18:10:27 +0000610
Kate Stoneb9c1b512016-09-06 20:57:50 +0000611 return error;
Oleksiy Vyalov6e181cf2016-06-30 18:10:27 +0000612}
613
Kate Stoneb9c1b512016-09-06 20:57:50 +0000614Error AdbClient::SyncService::PullFileChunk(std::vector<char> &buffer,
615 bool &eof) {
616 buffer.clear();
Oleksiy Vyalovd6a143f2016-07-06 17:02:42 +0000617
Kate Stoneb9c1b512016-09-06 20:57:50 +0000618 std::string response_id;
619 uint32_t data_len;
620 auto error = ReadSyncHeader(response_id, data_len);
621 if (error.Fail())
Oleksiy Vyalovd6a143f2016-07-06 17:02:42 +0000622 return error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000623
624 if (response_id == kDATA) {
625 buffer.resize(data_len, 0);
626 error = ReadAllBytes(&buffer[0], data_len);
627 if (error.Fail())
628 buffer.clear();
629 } else if (response_id == kDONE) {
630 eof = true;
631 } else if (response_id == kFAIL) {
632 std::string error_message(data_len, 0);
633 error = ReadAllBytes(&error_message[0], data_len);
634 if (error.Fail())
635 return Error("Failed to read pull error message: %s", error.AsCString());
636 return Error("Failed to pull file: %s", error_message.c_str());
637 } else
638 return Error("Pull failed with unknown response: %s", response_id.c_str());
639
640 return Error();
Oleksiy Vyalovd6a143f2016-07-06 17:02:42 +0000641}
642
Kate Stoneb9c1b512016-09-06 20:57:50 +0000643Error AdbClient::SyncService::ReadAllBytes(void *buffer, size_t size) {
644 return ::ReadAllBytes(*m_conn, buffer, size);
Oleksiy Vyalov6e181cf2016-06-30 18:10:27 +0000645}