Pierre Lecesne | ff759e6 | 2017-02-01 00:29:25 +0000 | [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 "LoadedApk.h" |
| 18 | |
Pierre Lecesne | 2599aa4 | 2017-02-01 22:47:03 +0000 | [diff] [blame] | 19 | #include "ResourceValues.h" |
| 20 | #include "ValueVisitor.h" |
| 21 | #include "flatten/Archive.h" |
| 22 | #include "flatten/TableFlattener.h" |
Adam Lesinski | 06460ef | 2017-03-14 18:52:13 -0700 | [diff] [blame] | 23 | #include "io/BigBufferInputStream.h" |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 24 | #include "io/Util.h" |
Pierre Lecesne | 2599aa4 | 2017-02-01 22:47:03 +0000 | [diff] [blame] | 25 | |
Pierre Lecesne | ff759e6 | 2017-02-01 00:29:25 +0000 | [diff] [blame] | 26 | namespace aapt { |
| 27 | |
Pierre Lecesne | 2599aa4 | 2017-02-01 22:47:03 +0000 | [diff] [blame] | 28 | std::unique_ptr<LoadedApk> LoadedApk::LoadApkFromPath(IAaptContext* context, |
| 29 | const android::StringPiece& path) { |
Pierre Lecesne | ff759e6 | 2017-02-01 00:29:25 +0000 | [diff] [blame] | 30 | Source source(path); |
| 31 | std::string error; |
Adam Lesinski | 06460ef | 2017-03-14 18:52:13 -0700 | [diff] [blame] | 32 | std::unique_ptr<io::ZipFileCollection> apk = io::ZipFileCollection::Create(path, &error); |
Pierre Lecesne | ff759e6 | 2017-02-01 00:29:25 +0000 | [diff] [blame] | 33 | if (!apk) { |
| 34 | context->GetDiagnostics()->Error(DiagMessage(source) << error); |
| 35 | return {}; |
| 36 | } |
| 37 | |
| 38 | io::IFile* file = apk->FindFile("resources.arsc"); |
| 39 | if (!file) { |
Adam Lesinski | 06460ef | 2017-03-14 18:52:13 -0700 | [diff] [blame] | 40 | context->GetDiagnostics()->Error(DiagMessage(source) << "no resources.arsc found"); |
Pierre Lecesne | ff759e6 | 2017-02-01 00:29:25 +0000 | [diff] [blame] | 41 | return {}; |
| 42 | } |
| 43 | |
| 44 | std::unique_ptr<io::IData> data = file->OpenAsData(); |
| 45 | if (!data) { |
Adam Lesinski | 06460ef | 2017-03-14 18:52:13 -0700 | [diff] [blame] | 46 | context->GetDiagnostics()->Error(DiagMessage(source) << "could not open resources.arsc"); |
Pierre Lecesne | ff759e6 | 2017-02-01 00:29:25 +0000 | [diff] [blame] | 47 | return {}; |
| 48 | } |
| 49 | |
| 50 | std::unique_ptr<ResourceTable> table = util::make_unique<ResourceTable>(); |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 51 | BinaryResourceParser parser(context, table.get(), source, data->data(), data->size(), apk.get()); |
Pierre Lecesne | ff759e6 | 2017-02-01 00:29:25 +0000 | [diff] [blame] | 52 | if (!parser.Parse()) { |
| 53 | return {}; |
| 54 | } |
Pierre Lecesne | ff759e6 | 2017-02-01 00:29:25 +0000 | [diff] [blame] | 55 | return util::make_unique<LoadedApk>(source, std::move(apk), std::move(table)); |
| 56 | } |
| 57 | |
Adam Lesinski | d48944a | 2017-02-21 14:22:30 -0800 | [diff] [blame] | 58 | bool LoadedApk::WriteToArchive(IAaptContext* context, const TableFlattenerOptions& options, |
| 59 | IArchiveWriter* writer) { |
Shane Farmer | 5766943 | 2017-06-19 12:52:04 -0700 | [diff] [blame] | 60 | FilterChain empty; |
Shane Farmer | 0a5b201 | 2017-06-22 12:24:12 -0700 | [diff] [blame] | 61 | return WriteToArchive(context, table_.get(), options, &empty, writer); |
Shane Farmer | 5766943 | 2017-06-19 12:52:04 -0700 | [diff] [blame] | 62 | } |
| 63 | |
Shane Farmer | 0a5b201 | 2017-06-22 12:24:12 -0700 | [diff] [blame] | 64 | bool LoadedApk::WriteToArchive(IAaptContext* context, ResourceTable* split_table, |
| 65 | const TableFlattenerOptions& options, FilterChain* filters, |
| 66 | IArchiveWriter* writer) { |
Pierre Lecesne | 2599aa4 | 2017-02-01 22:47:03 +0000 | [diff] [blame] | 67 | std::set<std::string> referenced_resources; |
| 68 | // List the files being referenced in the resource table. |
Shane Farmer | 0a5b201 | 2017-06-22 12:24:12 -0700 | [diff] [blame] | 69 | for (auto& pkg : split_table->packages) { |
Pierre Lecesne | 2599aa4 | 2017-02-01 22:47:03 +0000 | [diff] [blame] | 70 | for (auto& type : pkg->types) { |
| 71 | for (auto& entry : type->entries) { |
| 72 | for (auto& config_value : entry->values) { |
| 73 | FileReference* file_ref = ValueCast<FileReference>(config_value->value.get()); |
| 74 | if (file_ref) { |
| 75 | referenced_resources.insert(*file_ref->path); |
| 76 | } |
| 77 | } |
| 78 | } |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | std::unique_ptr<io::IFileCollectionIterator> iterator = apk_->Iterator(); |
| 83 | while (iterator->HasNext()) { |
| 84 | io::IFile* file = iterator->Next(); |
| 85 | |
| 86 | std::string path = file->GetSource().path; |
| 87 | // The name of the path has the format "<zip-file-name>@<path-to-file>". |
Chih-Hung Hsieh | 4dc5812 | 2017-08-03 16:28:10 -0700 | [diff] [blame] | 88 | path = path.substr(path.find('@') + 1); |
Pierre Lecesne | 2599aa4 | 2017-02-01 22:47:03 +0000 | [diff] [blame] | 89 | |
| 90 | // Skip resources that are not referenced if requested. |
| 91 | if (path.find("res/") == 0 && referenced_resources.find(path) == referenced_resources.end()) { |
| 92 | if (context->IsVerbose()) { |
| 93 | context->GetDiagnostics()->Note(DiagMessage() |
Pierre Lecesne | fa131d5 | 2017-02-03 19:15:03 +0000 | [diff] [blame] | 94 | << "Removing resource '" << path << "' from APK."); |
Pierre Lecesne | 2599aa4 | 2017-02-01 22:47:03 +0000 | [diff] [blame] | 95 | } |
| 96 | continue; |
| 97 | } |
| 98 | |
Shane Farmer | 5766943 | 2017-06-19 12:52:04 -0700 | [diff] [blame] | 99 | if (!filters->Keep(path)) { |
| 100 | if (context->IsVerbose()) { |
| 101 | context->GetDiagnostics()->Note(DiagMessage() << "Filtered '" << path << "' from APK."); |
| 102 | } |
| 103 | continue; |
| 104 | } |
| 105 | |
Adam Lesinski | 06460ef | 2017-03-14 18:52:13 -0700 | [diff] [blame] | 106 | // The resource table needs to be re-serialized since it might have changed. |
Pierre Lecesne | 2599aa4 | 2017-02-01 22:47:03 +0000 | [diff] [blame] | 107 | if (path == "resources.arsc") { |
Adam Lesinski | 06460ef | 2017-03-14 18:52:13 -0700 | [diff] [blame] | 108 | BigBuffer buffer(4096); |
Adam Lesinski | c8f71aa | 2017-02-08 07:03:50 -0800 | [diff] [blame] | 109 | // TODO(adamlesinski): How to determine if there were sparse entries (and if to encode |
| 110 | // with sparse entries) b/35389232. |
Adam Lesinski | d48944a | 2017-02-21 14:22:30 -0800 | [diff] [blame] | 111 | TableFlattener flattener(options, &buffer); |
Shane Farmer | 0a5b201 | 2017-06-22 12:24:12 -0700 | [diff] [blame] | 112 | if (!flattener.Consume(context, split_table)) { |
Pierre Lecesne | 2599aa4 | 2017-02-01 22:47:03 +0000 | [diff] [blame] | 113 | return false; |
| 114 | } |
| 115 | |
Adam Lesinski | 06460ef | 2017-03-14 18:52:13 -0700 | [diff] [blame] | 116 | io::BigBufferInputStream input_stream(&buffer); |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 117 | if (!io::CopyInputStreamToArchive(context, &input_stream, path, ArchiveEntry::kAlign, |
| 118 | writer)) { |
Pierre Lecesne | 2599aa4 | 2017-02-01 22:47:03 +0000 | [diff] [blame] | 119 | return false; |
| 120 | } |
Pierre Lecesne | 2599aa4 | 2017-02-01 22:47:03 +0000 | [diff] [blame] | 121 | |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 122 | } else { |
| 123 | uint32_t compression_flags = file->WasCompressed() ? ArchiveEntry::kCompress : 0u; |
| 124 | if (!io::CopyFileToArchive(context, file, path, compression_flags, writer)) { |
| 125 | return false; |
| 126 | } |
Pierre Lecesne | 2599aa4 | 2017-02-01 22:47:03 +0000 | [diff] [blame] | 127 | } |
| 128 | } |
Pierre Lecesne | 2599aa4 | 2017-02-01 22:47:03 +0000 | [diff] [blame] | 129 | return true; |
| 130 | } |
| 131 | |
Pierre Lecesne | ff759e6 | 2017-02-01 00:29:25 +0000 | [diff] [blame] | 132 | } // namespace aapt |