Felipe Leme | 698e065 | 2016-07-19 17:07:22 -0700 | [diff] [blame^] | 1 | /* |
| 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" |
| 22 | #include "commandline.h" |
| 23 | #include "file_sync_service.h" |
| 24 | |
| 25 | static constexpr char BUGZ_OK_PREFIX[] = "OK:"; |
| 26 | static constexpr char BUGZ_FAIL_PREFIX[] = "FAIL:"; |
| 27 | |
| 28 | int Bugreport::DoIt(TransportType transport_type, const char* serial, int argc, const char** argv) { |
| 29 | if (argc == 1) return SendShellCommand(transport_type, serial, "bugreport", false); |
| 30 | if (argc != 2) return usage(); |
| 31 | |
| 32 | // Zipped bugreport option - will call 'bugreportz', which prints the location |
| 33 | // of the generated |
| 34 | // file, then pull it to the destination file provided by the user. |
| 35 | std::string dest_file = argv[1]; |
| 36 | if (!android::base::EndsWith(argv[1], ".zip")) { |
| 37 | // TODO: use a case-insensitive comparison (like EndsWithIgnoreCase |
| 38 | dest_file += ".zip"; |
| 39 | } |
| 40 | std::string output; |
| 41 | |
| 42 | fprintf(stderr, |
| 43 | "Bugreport is in progress and it could take minutes to complete.\n" |
| 44 | "Please be patient and do not cancel or disconnect your device until " |
| 45 | "it completes.\n"); |
| 46 | int status = SendShellCommand(transport_type, serial, "bugreportz", false, &output, nullptr); |
| 47 | if (status != 0 || output.empty()) return status; |
| 48 | output = android::base::Trim(output); |
| 49 | |
| 50 | if (android::base::StartsWith(output, BUGZ_OK_PREFIX)) { |
| 51 | const char* zip_file = &output[strlen(BUGZ_OK_PREFIX)]; |
| 52 | std::vector<const char*> srcs{zip_file}; |
| 53 | status = DoSyncPull(srcs, dest_file.c_str(), true, dest_file.c_str()) ? 0 : 1; |
| 54 | if (status != 0) { |
| 55 | fprintf(stderr, "Could not copy file '%s' to '%s'\n", zip_file, dest_file.c_str()); |
| 56 | } |
| 57 | return status; |
| 58 | } |
| 59 | if (android::base::StartsWith(output, BUGZ_FAIL_PREFIX)) { |
| 60 | const char* error_message = &output[strlen(BUGZ_FAIL_PREFIX)]; |
| 61 | fprintf(stderr, "Device failed to take a zipped bugreport: %s\n", error_message); |
| 62 | return -1; |
| 63 | } |
| 64 | fprintf(stderr, |
| 65 | "Unexpected string (%s) returned by bugreportz, " |
| 66 | "device probably does not support it\n", |
| 67 | output.c_str()); |
| 68 | return -1; |
| 69 | } |
| 70 | |
| 71 | int Bugreport::SendShellCommand(TransportType transport_type, const char* serial, |
| 72 | const std::string& command, bool disable_shell_protocol, |
| 73 | std::string* output, std::string* err) { |
| 74 | return send_shell_command(transport_type, serial, command, disable_shell_protocol, output, err); |
| 75 | } |
| 76 | |
| 77 | bool Bugreport::DoSyncPull(const std::vector<const char*>& srcs, const char* dst, bool copy_attrs, |
| 78 | const char* name) { |
| 79 | return do_sync_pull(srcs, dst, copy_attrs, name); |
| 80 | } |