• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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, false, &cb);
53 }
54 
DeployAgentFileCallback(FILE * outputFile,std::vector<char> * errBuffer)55 DeployAgentFileCallback::DeployAgentFileCallback(FILE* outputFile, std::vector<char>* errBuffer) {
56     mpOutFile = outputFile;
57     mpErrBuffer = errBuffer;
58     mBytesWritten = 0;
59 }
60 
OnStdout(const char * buffer,int length)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 
OnStderr(const char * buffer,int length)71 void DeployAgentFileCallback::OnStderr(const char* buffer, int length) {
72     appendBuffer(mpErrBuffer, buffer, length);
73 }
74 
Done(int status)75 int DeployAgentFileCallback::Done(int status) {
76     return status;
77 }
78 
getBytesWritten()79 int DeployAgentFileCallback::getBytesWritten() {
80     return mBytesWritten;
81 }
82 
DeployAgentBufferCallback(std::vector<char> * outBuffer,std::vector<char> * errBuffer)83 DeployAgentBufferCallback::DeployAgentBufferCallback(std::vector<char>* outBuffer,
84                                                      std::vector<char>* errBuffer) {
85     mpOutBuffer = outBuffer;
86     mpErrBuffer = errBuffer;
87 }
88 
OnStdout(const char * buffer,int length)89 void DeployAgentBufferCallback::OnStdout(const char* buffer, int length) {
90     appendBuffer(mpOutBuffer, buffer, length);
91 }
92 
OnStderr(const char * buffer,int length)93 void DeployAgentBufferCallback::OnStderr(const char* buffer, int length) {
94     appendBuffer(mpErrBuffer, buffer, length);
95 }
96 
Done(int status)97 int DeployAgentBufferCallback::Done(int status) {
98     return status;
99 }
100