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 
appendBuffer(std::vector<char> * buffer,const char * input,int length)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:
38     DeployAgentBufferCallback(std::vector<char>* outBuffer, std::vector<char>* errBuffer);
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;
47 };
48 
capture_shell_command(const char * command,std::vector<char> * outBuffer,std::vector<char> * errBuffer)49 int capture_shell_command(const char* command, std::vector<char>* outBuffer,
50                           std::vector<char>* errBuffer) {
51     DeployAgentBufferCallback cb(outBuffer, errBuffer);
52     return send_shell_command(command, /*disable_shell_protocol=*/false, &cb);
53 }
54 
DeployAgentBufferCallback(std::vector<char> * outBuffer,std::vector<char> * errBuffer)55 DeployAgentBufferCallback::DeployAgentBufferCallback(std::vector<char>* outBuffer,
56                                                      std::vector<char>* errBuffer) {
57     mpOutBuffer = outBuffer;
58     mpErrBuffer = errBuffer;
59 }
60 
OnStdout(const char * buffer,int length)61 void DeployAgentBufferCallback::OnStdout(const char* buffer, int length) {
62     appendBuffer(mpOutBuffer, buffer, length);
63 }
64 
OnStderr(const char * buffer,int length)65 void DeployAgentBufferCallback::OnStderr(const char* buffer, int length) {
66     appendBuffer(mpErrBuffer, buffer, length);
67 }
68 
Done(int status)69 int DeployAgentBufferCallback::Done(int status) {
70     return status;
71 }
72