Idries Hamadi | 1ecee44 | 2018-01-29 16:30:36 +0000 | [diff] [blame] | 1 | /* |
| 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 | |
| 30 | static 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 | |
| 36 | class DeployAgentBufferCallback : public StandardStreamsCallbackInterface { |
| 37 | public: |
Henry Daitx | 61bf748 | 2018-11-30 18:02:43 +0000 | [diff] [blame] | 38 | DeployAgentBufferCallback(std::vector<char>* outBuffer, std::vector<char>* errBuffer); |
Idries Hamadi | 1ecee44 | 2018-01-29 16:30:36 +0000 | [diff] [blame] | 39 | |
| 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 Hamadi | 1ecee44 | 2018-01-29 16:30:36 +0000 | [diff] [blame] | 47 | }; |
| 48 | |
| 49 | int capture_shell_command(const char* command, std::vector<char>* outBuffer, |
| 50 | std::vector<char>* errBuffer) { |
Henry Daitx | 61bf748 | 2018-11-30 18:02:43 +0000 | [diff] [blame] | 51 | DeployAgentBufferCallback cb(outBuffer, errBuffer); |
Alex Buynytskyy | 1af550e | 2019-09-16 12:10:54 -0700 | [diff] [blame] | 52 | return send_shell_command(command, /*disable_shell_protocol=*/false, &cb); |
Idries Hamadi | 1ecee44 | 2018-01-29 16:30:36 +0000 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | DeployAgentBufferCallback::DeployAgentBufferCallback(std::vector<char>* outBuffer, |
Henry Daitx | 61bf748 | 2018-11-30 18:02:43 +0000 | [diff] [blame] | 56 | std::vector<char>* errBuffer) { |
Idries Hamadi | 1ecee44 | 2018-01-29 16:30:36 +0000 | [diff] [blame] | 57 | mpOutBuffer = outBuffer; |
| 58 | mpErrBuffer = errBuffer; |
Idries Hamadi | 1ecee44 | 2018-01-29 16:30:36 +0000 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | void DeployAgentBufferCallback::OnStdout(const char* buffer, int length) { |
| 62 | appendBuffer(mpOutBuffer, buffer, length); |
| 63 | } |
| 64 | |
| 65 | void DeployAgentBufferCallback::OnStderr(const char* buffer, int length) { |
| 66 | appendBuffer(mpErrBuffer, buffer, length); |
| 67 | } |
| 68 | |
| 69 | int DeployAgentBufferCallback::Done(int status) { |
Henry Daitx | 61bf748 | 2018-11-30 18:02:43 +0000 | [diff] [blame] | 70 | return status; |
Idries Hamadi | 1ecee44 | 2018-01-29 16:30:36 +0000 | [diff] [blame] | 71 | } |