blob: 6c9a21fdeb5c56ea84b7b7719215ac7b7548ed67 [file] [log] [blame]
Idries Hamadied409ea2018-01-29 16:30:36 +00001/*
2 * Copyright (C) 2018 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define TRACE_TAG ADB
18
19#include <fcntl.h>
20#include <stdio.h>
21#include <stdlib.h>
22#include <sys/stat.h>
23
24#include "client/file_sync_client.h"
25#include "commandline.h"
26#include "sysdeps.h"
27
28#include "fastdeploycallbacks.h"
29
30static void appendBuffer(std::vector<char>* buffer, const char* input, int length) {
31 if (buffer != NULL) {
32 buffer->insert(buffer->end(), input, input + length);
33 }
34}
35
36class DeployAgentBufferCallback : public StandardStreamsCallbackInterface {
37 public:
38 DeployAgentBufferCallback(std::vector<char>* outBuffer, std::vector<char>* errBuffer,
39 int* statusCode);
40
41 virtual void OnStdout(const char* buffer, int length);
42 virtual void OnStderr(const char* buffer, int length);
43 virtual int Done(int status);
44
45 private:
46 std::vector<char>* mpOutBuffer;
47 std::vector<char>* mpErrBuffer;
48 int* mpStatusCode;
49};
50
51int capture_shell_command(const char* command, std::vector<char>* outBuffer,
52 std::vector<char>* errBuffer) {
53 int statusCode;
54 DeployAgentBufferCallback cb(outBuffer, errBuffer, &statusCode);
55 int ret = send_shell_command(command, false, &cb);
56
57 if (ret == 0) {
58 return statusCode;
59 } else {
60 return ret;
61 }
62}
63
64DeployAgentFileCallback::DeployAgentFileCallback(FILE* outputFile, std::vector<char>* errBuffer,
65 int* statusCode) {
66 mpOutFile = outputFile;
67 mpErrBuffer = errBuffer;
68 mpStatusCode = statusCode;
69 mBytesWritten = 0;
70}
71
72void DeployAgentFileCallback::OnStdout(const char* buffer, int length) {
73 if (mpOutFile != NULL) {
74 int bytes_written = fwrite(buffer, 1, length, mpOutFile);
75 if (bytes_written != length) {
76 printf("Write error %d\n", bytes_written);
77 }
78 mBytesWritten += bytes_written;
79 }
80}
81
82void DeployAgentFileCallback::OnStderr(const char* buffer, int length) {
83 appendBuffer(mpErrBuffer, buffer, length);
84}
85
86int DeployAgentFileCallback::Done(int status) {
87 if (mpStatusCode != NULL) {
88 *mpStatusCode = status;
89 }
90 return 0;
91}
92
93int DeployAgentFileCallback::getBytesWritten() {
94 return mBytesWritten;
95}
96
97DeployAgentBufferCallback::DeployAgentBufferCallback(std::vector<char>* outBuffer,
98 std::vector<char>* errBuffer,
99 int* statusCode) {
100 mpOutBuffer = outBuffer;
101 mpErrBuffer = errBuffer;
102 mpStatusCode = statusCode;
103}
104
105void DeployAgentBufferCallback::OnStdout(const char* buffer, int length) {
106 appendBuffer(mpOutBuffer, buffer, length);
107}
108
109void DeployAgentBufferCallback::OnStderr(const char* buffer, int length) {
110 appendBuffer(mpErrBuffer, buffer, length);
111}
112
113int DeployAgentBufferCallback::Done(int status) {
114 if (mpStatusCode != NULL) {
115 *mpStatusCode = status;
116 }
117 return 0;
118}