blob: b855f8f80c58169f1082eaf4af44dbfb45744744 [file] [log] [blame]
Pierre Lecesneff759e62017-02-01 00:29:25 +00001/*
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 Lecesne2599aa42017-02-01 22:47:03 +000019#include "ResourceValues.h"
20#include "ValueVisitor.h"
21#include "flatten/Archive.h"
22#include "flatten/TableFlattener.h"
Adam Lesinski06460ef2017-03-14 18:52:13 -070023#include "io/BigBufferInputStream.h"
Pierre Lecesne2599aa42017-02-01 22:47:03 +000024
Pierre Lecesneff759e62017-02-01 00:29:25 +000025namespace aapt {
26
Pierre Lecesne2599aa42017-02-01 22:47:03 +000027std::unique_ptr<LoadedApk> LoadedApk::LoadApkFromPath(IAaptContext* context,
28 const android::StringPiece& path) {
Pierre Lecesneff759e62017-02-01 00:29:25 +000029 Source source(path);
30 std::string error;
Adam Lesinski06460ef2017-03-14 18:52:13 -070031 std::unique_ptr<io::ZipFileCollection> apk = io::ZipFileCollection::Create(path, &error);
Pierre Lecesneff759e62017-02-01 00:29:25 +000032 if (!apk) {
33 context->GetDiagnostics()->Error(DiagMessage(source) << error);
34 return {};
35 }
36
37 io::IFile* file = apk->FindFile("resources.arsc");
38 if (!file) {
Adam Lesinski06460ef2017-03-14 18:52:13 -070039 context->GetDiagnostics()->Error(DiagMessage(source) << "no resources.arsc found");
Pierre Lecesneff759e62017-02-01 00:29:25 +000040 return {};
41 }
42
43 std::unique_ptr<io::IData> data = file->OpenAsData();
44 if (!data) {
Adam Lesinski06460ef2017-03-14 18:52:13 -070045 context->GetDiagnostics()->Error(DiagMessage(source) << "could not open resources.arsc");
Pierre Lecesneff759e62017-02-01 00:29:25 +000046 return {};
47 }
48
49 std::unique_ptr<ResourceTable> table = util::make_unique<ResourceTable>();
Adam Lesinski06460ef2017-03-14 18:52:13 -070050 BinaryResourceParser parser(context, table.get(), source, data->data(), data->size());
Pierre Lecesneff759e62017-02-01 00:29:25 +000051 if (!parser.Parse()) {
52 return {};
53 }
54
55 return util::make_unique<LoadedApk>(source, std::move(apk), std::move(table));
56}
57
Adam Lesinskid48944a2017-02-21 14:22:30 -080058bool LoadedApk::WriteToArchive(IAaptContext* context, const TableFlattenerOptions& options,
59 IArchiveWriter* writer) {
Pierre Lecesne2599aa42017-02-01 22:47:03 +000060 std::set<std::string> referenced_resources;
61 // List the files being referenced in the resource table.
62 for (auto& pkg : table_->packages) {
63 for (auto& type : pkg->types) {
64 for (auto& entry : type->entries) {
65 for (auto& config_value : entry->values) {
66 FileReference* file_ref = ValueCast<FileReference>(config_value->value.get());
67 if (file_ref) {
68 referenced_resources.insert(*file_ref->path);
69 }
70 }
71 }
72 }
73 }
74
75 std::unique_ptr<io::IFileCollectionIterator> iterator = apk_->Iterator();
76 while (iterator->HasNext()) {
77 io::IFile* file = iterator->Next();
78
79 std::string path = file->GetSource().path;
80 // The name of the path has the format "<zip-file-name>@<path-to-file>".
81 path = path.substr(path.find("@") + 1);
82
83 // Skip resources that are not referenced if requested.
84 if (path.find("res/") == 0 && referenced_resources.find(path) == referenced_resources.end()) {
85 if (context->IsVerbose()) {
86 context->GetDiagnostics()->Note(DiagMessage()
Pierre Lecesnefa131d52017-02-03 19:15:03 +000087 << "Removing resource '" << path << "' from APK.");
Pierre Lecesne2599aa42017-02-01 22:47:03 +000088 }
89 continue;
90 }
91
Adam Lesinski06460ef2017-03-14 18:52:13 -070092 // The resource table needs to be re-serialized since it might have changed.
Pierre Lecesne2599aa42017-02-01 22:47:03 +000093 if (path == "resources.arsc") {
Adam Lesinski06460ef2017-03-14 18:52:13 -070094 BigBuffer buffer(4096);
Adam Lesinskic8f71aa2017-02-08 07:03:50 -080095 // TODO(adamlesinski): How to determine if there were sparse entries (and if to encode
96 // with sparse entries) b/35389232.
Adam Lesinskid48944a2017-02-21 14:22:30 -080097 TableFlattener flattener(options, &buffer);
Pierre Lecesne2599aa42017-02-01 22:47:03 +000098 if (!flattener.Consume(context, table_.get())) {
99 return false;
100 }
101
Adam Lesinski06460ef2017-03-14 18:52:13 -0700102 io::BigBufferInputStream input_stream(&buffer);
103 if (!writer->WriteFile(path, ArchiveEntry::kAlign, &input_stream)) {
Pierre Lecesne2599aa42017-02-01 22:47:03 +0000104 context->GetDiagnostics()->Error(DiagMessage()
105 << "Error when writing file '" << path << "' in APK.");
106 return false;
107 }
108 continue;
109 }
110
111 std::unique_ptr<io::IData> data = file->OpenAsData();
Pierre Lecesnefa131d52017-02-03 19:15:03 +0000112 uint32_t compression_flags = file->WasCompressed() ? ArchiveEntry::kCompress : 0u;
Adam Lesinski06460ef2017-03-14 18:52:13 -0700113 if (!writer->WriteFile(path, compression_flags, data.get())) {
Pierre Lecesne2599aa42017-02-01 22:47:03 +0000114 context->GetDiagnostics()->Error(DiagMessage()
115 << "Error when writing file '" << path << "' in APK.");
116 return false;
117 }
118 }
Pierre Lecesne2599aa42017-02-01 22:47:03 +0000119 return true;
120}
121
Pierre Lecesneff759e62017-02-01 00:29:25 +0000122} // namespace aapt