blob: 86ceaa1378992e28d82156ac7d82a951ce389dac [file] [log] [blame]
Idries Hamadi1ecee442018-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:
Henry Daitx61bf7482018-11-30 18:02:43 +000038 DeployAgentBufferCallback(std::vector<char>* outBuffer, std::vector<char>* errBuffer);
Idries Hamadi1ecee442018-01-29 16:30:36 +000039
40 virtual void OnStdout(const char* buffer, int length);
41 virtual void OnStderr(const char* buffer, int length);
42 virtual int Done(int status);
43
44 private:
45 std::vector<char>* mpOutBuffer;
46 std::vector<char>* mpErrBuffer;
Idries Hamadi1ecee442018-01-29 16:30:36 +000047};
48
49int capture_shell_command(const char* command, std::vector<char>* outBuffer,
50 std::vector<char>* errBuffer) {
Henry Daitx61bf7482018-11-30 18:02:43 +000051 DeployAgentBufferCallback cb(outBuffer, errBuffer);
Alex Buynytskyy1af550e2019-09-16 12:10:54 -070052 return send_shell_command(command, /*disable_shell_protocol=*/false, &cb);
Idries Hamadi1ecee442018-01-29 16:30:36 +000053}
54
55DeployAgentBufferCallback::DeployAgentBufferCallback(std::vector<char>* outBuffer,
Henry Daitx61bf7482018-11-30 18:02:43 +000056 std::vector<char>* errBuffer) {
Idries Hamadi1ecee442018-01-29 16:30:36 +000057 mpOutBuffer = outBuffer;
58 mpErrBuffer = errBuffer;
Idries Hamadi1ecee442018-01-29 16:30:36 +000059}
60
61void DeployAgentBufferCallback::OnStdout(const char* buffer, int length) {
62 appendBuffer(mpOutBuffer, buffer, length);
63}
64
65void DeployAgentBufferCallback::OnStderr(const char* buffer, int length) {
66 appendBuffer(mpErrBuffer, buffer, length);
67}
68
69int DeployAgentBufferCallback::Done(int status) {
Henry Daitx61bf7482018-11-30 18:02:43 +000070 return status;
Idries Hamadi1ecee442018-01-29 16:30:36 +000071}