| Idries Hamadi | ed409ea | 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 | f21edf3 | 2018-11-30 18:02:43 +0000 | [diff] [blame] | 38 | DeployAgentBufferCallback(std::vector<char>* outBuffer, std::vector<char>* errBuffer); | 
| Idries Hamadi | ed409ea | 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 | ed409ea | 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 | f21edf3 | 2018-11-30 18:02:43 +0000 | [diff] [blame] | 51 | DeployAgentBufferCallback cb(outBuffer, errBuffer); | 
|  | 52 | return send_shell_command(command, false, &cb); | 
| Idries Hamadi | ed409ea | 2018-01-29 16:30:36 +0000 | [diff] [blame] | 53 | } | 
|  | 54 |  | 
| Henry Daitx | f21edf3 | 2018-11-30 18:02:43 +0000 | [diff] [blame] | 55 | DeployAgentFileCallback::DeployAgentFileCallback(FILE* outputFile, std::vector<char>* errBuffer) { | 
| Idries Hamadi | ed409ea | 2018-01-29 16:30:36 +0000 | [diff] [blame] | 56 | mpOutFile = outputFile; | 
|  | 57 | mpErrBuffer = errBuffer; | 
| Idries Hamadi | ed409ea | 2018-01-29 16:30:36 +0000 | [diff] [blame] | 58 | mBytesWritten = 0; | 
|  | 59 | } | 
|  | 60 |  | 
|  | 61 | void DeployAgentFileCallback::OnStdout(const char* buffer, int length) { | 
|  | 62 | if (mpOutFile != NULL) { | 
|  | 63 | int bytes_written = fwrite(buffer, 1, length, mpOutFile); | 
|  | 64 | if (bytes_written != length) { | 
|  | 65 | printf("Write error %d\n", bytes_written); | 
|  | 66 | } | 
|  | 67 | mBytesWritten += bytes_written; | 
|  | 68 | } | 
|  | 69 | } | 
|  | 70 |  | 
|  | 71 | void DeployAgentFileCallback::OnStderr(const char* buffer, int length) { | 
|  | 72 | appendBuffer(mpErrBuffer, buffer, length); | 
|  | 73 | } | 
|  | 74 |  | 
|  | 75 | int DeployAgentFileCallback::Done(int status) { | 
| Henry Daitx | f21edf3 | 2018-11-30 18:02:43 +0000 | [diff] [blame] | 76 | return status; | 
| Idries Hamadi | ed409ea | 2018-01-29 16:30:36 +0000 | [diff] [blame] | 77 | } | 
|  | 78 |  | 
|  | 79 | int DeployAgentFileCallback::getBytesWritten() { | 
|  | 80 | return mBytesWritten; | 
|  | 81 | } | 
|  | 82 |  | 
|  | 83 | DeployAgentBufferCallback::DeployAgentBufferCallback(std::vector<char>* outBuffer, | 
| Henry Daitx | f21edf3 | 2018-11-30 18:02:43 +0000 | [diff] [blame] | 84 | std::vector<char>* errBuffer) { | 
| Idries Hamadi | ed409ea | 2018-01-29 16:30:36 +0000 | [diff] [blame] | 85 | mpOutBuffer = outBuffer; | 
|  | 86 | mpErrBuffer = errBuffer; | 
| Idries Hamadi | ed409ea | 2018-01-29 16:30:36 +0000 | [diff] [blame] | 87 | } | 
|  | 88 |  | 
|  | 89 | void DeployAgentBufferCallback::OnStdout(const char* buffer, int length) { | 
|  | 90 | appendBuffer(mpOutBuffer, buffer, length); | 
|  | 91 | } | 
|  | 92 |  | 
|  | 93 | void DeployAgentBufferCallback::OnStderr(const char* buffer, int length) { | 
|  | 94 | appendBuffer(mpErrBuffer, buffer, length); | 
|  | 95 | } | 
|  | 96 |  | 
|  | 97 | int DeployAgentBufferCallback::Done(int status) { | 
| Henry Daitx | f21edf3 | 2018-11-30 18:02:43 +0000 | [diff] [blame] | 98 | return status; | 
| Idries Hamadi | ed409ea | 2018-01-29 16:30:36 +0000 | [diff] [blame] | 99 | } |