From 06460ef0d7072114ea3280e1650f77f55e7223f4 Mon Sep 17 00:00:00 2001 From: Adam Lesinski Date: Tue, 14 Mar 2017 18:52:13 -0700 Subject: AAPT2: Fix up file IO This also enables an AAPT behavior that CTS tests have come to depend on. Small files that compress negatively (get larger) are stored uncompressed. Some CTS tests assume this and try to open these files by mmapping them, which is only possible if they are uncompressed. Bug: 35461578 Test: make aapt2_tests Change-Id: Id622a6150fe72477ad65d67d1bad897a8ee2ffb9 --- tools/aapt2/LoadedApk.cpp | 25 ++++++++++--------------- 1 file changed, 10 insertions(+), 15 deletions(-) (limited to 'tools/aapt2/LoadedApk.cpp') diff --git a/tools/aapt2/LoadedApk.cpp b/tools/aapt2/LoadedApk.cpp index 1d04b357131a..b855f8f80c58 100644 --- a/tools/aapt2/LoadedApk.cpp +++ b/tools/aapt2/LoadedApk.cpp @@ -20,6 +20,7 @@ #include "ValueVisitor.h" #include "flatten/Archive.h" #include "flatten/TableFlattener.h" +#include "io/BigBufferInputStream.h" namespace aapt { @@ -27,8 +28,7 @@ std::unique_ptr LoadedApk::LoadApkFromPath(IAaptContext* context, const android::StringPiece& path) { Source source(path); std::string error; - std::unique_ptr apk = - io::ZipFileCollection::Create(path, &error); + std::unique_ptr apk = io::ZipFileCollection::Create(path, &error); if (!apk) { context->GetDiagnostics()->Error(DiagMessage(source) << error); return {}; @@ -36,21 +36,18 @@ std::unique_ptr LoadedApk::LoadApkFromPath(IAaptContext* context, io::IFile* file = apk->FindFile("resources.arsc"); if (!file) { - context->GetDiagnostics()->Error(DiagMessage(source) - << "no resources.arsc found"); + context->GetDiagnostics()->Error(DiagMessage(source) << "no resources.arsc found"); return {}; } std::unique_ptr data = file->OpenAsData(); if (!data) { - context->GetDiagnostics()->Error(DiagMessage(source) - << "could not open resources.arsc"); + context->GetDiagnostics()->Error(DiagMessage(source) << "could not open resources.arsc"); return {}; } std::unique_ptr table = util::make_unique(); - BinaryResourceParser parser(context, table.get(), source, data->data(), - data->size()); + BinaryResourceParser parser(context, table.get(), source, data->data(), data->size()); if (!parser.Parse()) { return {}; } @@ -92,9 +89,9 @@ bool LoadedApk::WriteToArchive(IAaptContext* context, const TableFlattenerOption continue; } - // The resource table needs to be reserialized since it might have changed. + // The resource table needs to be re-serialized since it might have changed. if (path == "resources.arsc") { - BigBuffer buffer = BigBuffer(1024); + BigBuffer buffer(4096); // TODO(adamlesinski): How to determine if there were sparse entries (and if to encode // with sparse entries) b/35389232. TableFlattener flattener(options, &buffer); @@ -102,8 +99,8 @@ bool LoadedApk::WriteToArchive(IAaptContext* context, const TableFlattenerOption return false; } - if (!writer->StartEntry(path, ArchiveEntry::kAlign) || !writer->WriteEntry(buffer) || - !writer->FinishEntry()) { + io::BigBufferInputStream input_stream(&buffer); + if (!writer->WriteFile(path, ArchiveEntry::kAlign, &input_stream)) { context->GetDiagnostics()->Error(DiagMessage() << "Error when writing file '" << path << "' in APK."); return false; @@ -113,14 +110,12 @@ bool LoadedApk::WriteToArchive(IAaptContext* context, const TableFlattenerOption std::unique_ptr data = file->OpenAsData(); uint32_t compression_flags = file->WasCompressed() ? ArchiveEntry::kCompress : 0u; - if (!writer->StartEntry(path, compression_flags) || - !writer->WriteEntry(data->data(), data->size()) || !writer->FinishEntry()) { + if (!writer->WriteFile(path, compression_flags, data.get())) { context->GetDiagnostics()->Error(DiagMessage() << "Error when writing file '" << path << "' in APK."); return false; } } - return true; } -- cgit v1.2.3-59-g8ed1b