blob: e267f0f87d6979607438d0d84a941e19beb35514 [file] [log] [blame]
Felipe Leme218e1ff2016-07-19 17:07:22 -07001/*
2 * Copyright (C) 2016 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#include <string>
18
19#include <android-base/strings.h>
20
21#include "bugreport.h"
Felipe Leme218e1ff2016-07-19 17:07:22 -070022#include "file_sync_service.h"
23
24static constexpr char BUGZ_OK_PREFIX[] = "OK:";
25static constexpr char BUGZ_FAIL_PREFIX[] = "FAIL:";
26
Felipe Lemed1885422016-07-26 12:14:39 -070027// Custom callback used to handle the output of zipped bugreports.
28class BugreportStandardStreamsCallback : public StandardStreamsCallbackInterface {
29 public:
30 BugreportStandardStreamsCallback(const std::string& dest_file, Bugreport* br)
31 : br_(br), dest_file_(dest_file), stdout_str_() {
32 }
33
34 void OnStdout(const char* buffer, int length) {
35 std::string output;
36 OnStream(&output, stdout, buffer, length);
37 stdout_str_.append(output);
38 }
39
40 void OnStderr(const char* buffer, int length) {
41 OnStream(nullptr, stderr, buffer, length);
42 }
43
44 int Done(int unused_) {
45 int status = -1;
46 std::string output = android::base::Trim(stdout_str_);
47 if (android::base::StartsWith(output, BUGZ_OK_PREFIX)) {
48 const char* zip_file = &output[strlen(BUGZ_OK_PREFIX)];
49 std::vector<const char*> srcs{zip_file};
50 status = br_->DoSyncPull(srcs, dest_file_.c_str(), true, dest_file_.c_str()) ? 0 : 1;
51 if (status != 0) {
52 fprintf(stderr, "Could not copy file '%s' to '%s'\n", zip_file, dest_file_.c_str());
53 }
54 } else if (android::base::StartsWith(output, BUGZ_FAIL_PREFIX)) {
55 const char* error_message = &output[strlen(BUGZ_FAIL_PREFIX)];
56 fprintf(stderr, "Device failed to take a zipped bugreport: %s\n", error_message);
57 } else {
58 fprintf(stderr,
59 "Unexpected string (%s) returned by bugreportz, "
60 "device probably does not support it\n",
61 output.c_str());
62 }
63
64 return status;
65 }
66
67 private:
68 Bugreport* br_;
69 const std::string dest_file_;
70 std::string stdout_str_;
71
72 DISALLOW_COPY_AND_ASSIGN(BugreportStandardStreamsCallback);
73};
74
Felipe Leme218e1ff2016-07-19 17:07:22 -070075int Bugreport::DoIt(TransportType transport_type, const char* serial, int argc, const char** argv) {
76 if (argc == 1) return SendShellCommand(transport_type, serial, "bugreport", false);
77 if (argc != 2) return usage();
78
79 // Zipped bugreport option - will call 'bugreportz', which prints the location
80 // of the generated
81 // file, then pull it to the destination file provided by the user.
82 std::string dest_file = argv[1];
83 if (!android::base::EndsWith(argv[1], ".zip")) {
84 // TODO: use a case-insensitive comparison (like EndsWithIgnoreCase
85 dest_file += ".zip";
86 }
Felipe Leme218e1ff2016-07-19 17:07:22 -070087 fprintf(stderr,
88 "Bugreport is in progress and it could take minutes to complete.\n"
89 "Please be patient and do not cancel or disconnect your device until "
90 "it completes.\n");
Felipe Lemed1885422016-07-26 12:14:39 -070091 BugreportStandardStreamsCallback bugz_callback(dest_file, this);
92 return SendShellCommand(transport_type, serial, "bugreportz", false, &bugz_callback);
Felipe Leme218e1ff2016-07-19 17:07:22 -070093}
94
95int Bugreport::SendShellCommand(TransportType transport_type, const char* serial,
96 const std::string& command, bool disable_shell_protocol,
Felipe Lemed1885422016-07-26 12:14:39 -070097 StandardStreamsCallbackInterface* callback) {
98 return send_shell_command(transport_type, serial, command, disable_shell_protocol, callback);
Felipe Leme218e1ff2016-07-19 17:07:22 -070099}
100
101bool Bugreport::DoSyncPull(const std::vector<const char*>& srcs, const char* dst, bool copy_attrs,
102 const char* name) {
103 return do_sync_pull(srcs, dst, copy_attrs, name);
104}