| Iurii Makhno | f248074 | 2022-04-26 14:51:24 +0000 | [diff] [blame] | 1 | /* | 
|  | 2 | * Copyright (C) 2022 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 "ApkInfo.h" | 
|  | 18 |  | 
|  | 19 | #include <fcntl.h> | 
|  | 20 |  | 
|  | 21 | #include <iostream> | 
|  | 22 | #include <memory> | 
|  | 23 |  | 
| Iurii Makhno | f248074 | 2022-04-26 14:51:24 +0000 | [diff] [blame] | 24 | #include "LoadedApk.h" | 
|  | 25 | #include "android-base/file.h"  // for O_BINARY | 
|  | 26 | #include "android-base/utf8.h" | 
| Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame] | 27 | #include "androidfw/IDiagnostics.h" | 
| Iurii Makhno | f248074 | 2022-04-26 14:51:24 +0000 | [diff] [blame] | 28 | #include "androidfw/StringPiece.h" | 
|  | 29 | #include "dump/DumpManifest.h" | 
|  | 30 | #include "format/proto/ProtoSerialize.h" | 
|  | 31 |  | 
|  | 32 | using ::android::StringPiece; | 
|  | 33 |  | 
|  | 34 | namespace aapt { | 
|  | 35 |  | 
|  | 36 | int ExportApkInfo(LoadedApk* apk, bool include_resource_table, | 
|  | 37 | const std::unordered_set<std::string>& xml_resources, pb::ApkInfo* out_apk_info, | 
| Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame] | 38 | android::IDiagnostics* diag) { | 
| Iurii Makhno | f248074 | 2022-04-26 14:51:24 +0000 | [diff] [blame] | 39 | auto result = DumpBadgingProto(apk, out_apk_info->mutable_badging(), diag); | 
|  | 40 | if (result != 0) { | 
|  | 41 | return result; | 
|  | 42 | } | 
|  | 43 |  | 
|  | 44 | if (include_resource_table) { | 
|  | 45 | SerializeTableToPb(*apk->GetResourceTable(), out_apk_info->mutable_resource_table(), diag); | 
|  | 46 | } | 
|  | 47 |  | 
|  | 48 | for (auto& xml_resource : xml_resources) { | 
|  | 49 | auto xml = apk->LoadXml(xml_resource, diag); | 
|  | 50 | if (xml) { | 
|  | 51 | auto out_xml = out_apk_info->add_xml_files(); | 
|  | 52 | out_xml->set_path(xml_resource); | 
|  | 53 | SerializeXmlResourceToPb(*xml, out_xml->mutable_root(), | 
|  | 54 | {/* remove_empty_text_nodes= */ true}); | 
|  | 55 | } | 
|  | 56 | } | 
|  | 57 |  | 
|  | 58 | return 0; | 
|  | 59 | } | 
|  | 60 |  | 
|  | 61 | int ApkInfoCommand::Action(const std::vector<std::string>& args) { | 
|  | 62 | if (args.size() != 1) { | 
|  | 63 | std::cerr << "must supply a single APK\n"; | 
|  | 64 | Usage(&std::cerr); | 
|  | 65 | return 1; | 
|  | 66 | } | 
|  | 67 | const StringPiece& path = args[0]; | 
|  | 68 | std::unique_ptr<LoadedApk> apk = LoadedApk::LoadApkFromPath(path, diag_); | 
|  | 69 | if (!apk) { | 
|  | 70 | return 1; | 
|  | 71 | } | 
|  | 72 |  | 
|  | 73 | pb::ApkInfo out_apk_info; | 
|  | 74 | int result = | 
|  | 75 | ExportApkInfo(apk.get(), include_resource_table_, xml_resources_, &out_apk_info, diag_); | 
|  | 76 | if (result != 0) { | 
| Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame] | 77 | diag_->Error(android::DiagMessage() << "Failed to serialize ApkInfo into proto."); | 
| Iurii Makhno | f248074 | 2022-04-26 14:51:24 +0000 | [diff] [blame] | 78 | return result; | 
|  | 79 | } | 
|  | 80 |  | 
|  | 81 | int mode = O_WRONLY | O_CREAT | O_TRUNC | O_BINARY; | 
|  | 82 | int outfd = ::android::base::utf8::open(output_path_.c_str(), mode, 0666); | 
|  | 83 | if (outfd == -1) { | 
| Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame] | 84 | diag_->Error(android::DiagMessage() << "Failed to open output file."); | 
| Iurii Makhno | f248074 | 2022-04-26 14:51:24 +0000 | [diff] [blame] | 85 | return 1; | 
|  | 86 | } | 
|  | 87 |  | 
|  | 88 | bool is_serialized = out_apk_info.SerializeToFileDescriptor(outfd); | 
|  | 89 | close(outfd); | 
|  | 90 |  | 
|  | 91 | return is_serialized ? 0 : 1; | 
|  | 92 | } | 
|  | 93 |  | 
|  | 94 | }  // namespace aapt |