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