blob: d270340a180c7b546e22eb37ccd37957a43e589c [file] [log] [blame]
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001/*
2 * Copyright (C) 2017 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 "io/Util.h"
18
19#include "google/protobuf/io/zero_copy_stream_impl_lite.h"
20
Adam Lesinski00451162017-10-03 07:44:08 -070021using ::google::protobuf::io::ZeroCopyOutputStream;
22
Adam Lesinskid0f492d2017-04-03 18:12:45 -070023namespace aapt {
24namespace io {
25
26bool CopyInputStreamToArchive(IAaptContext* context, InputStream* in, const std::string& out_path,
27 uint32_t compression_flags, IArchiveWriter* writer) {
28 if (context->IsVerbose()) {
29 context->GetDiagnostics()->Note(DiagMessage() << "writing " << out_path << " to archive");
30 }
31
32 if (!writer->WriteFile(out_path, compression_flags, in)) {
33 context->GetDiagnostics()->Error(DiagMessage() << "failed to write " << out_path
34 << " to archive: " << writer->GetError());
35 return false;
36 }
37 return true;
38}
39
40bool CopyFileToArchive(IAaptContext* context, io::IFile* file, const std::string& out_path,
41 uint32_t compression_flags, IArchiveWriter* writer) {
42 std::unique_ptr<io::IData> data = file->OpenAsData();
43 if (!data) {
44 context->GetDiagnostics()->Error(DiagMessage(file->GetSource()) << "failed to open file");
45 return false;
46 }
47 return CopyInputStreamToArchive(context, data.get(), out_path, compression_flags, writer);
48}
49
50bool CopyProtoToArchive(IAaptContext* context, ::google::protobuf::MessageLite* proto_msg,
51 const std::string& out_path, uint32_t compression_flags,
52 IArchiveWriter* writer) {
53 if (context->IsVerbose()) {
54 context->GetDiagnostics()->Note(DiagMessage() << "writing " << out_path << " to archive");
55 }
56
57 if (writer->StartEntry(out_path, compression_flags)) {
58 // Make sure CopyingOutputStreamAdaptor is deleted before we call writer->FinishEntry().
59 {
60 // Wrap our IArchiveWriter with an adaptor that implements the ZeroCopyOutputStream interface.
61 ::google::protobuf::io::CopyingOutputStreamAdaptor adaptor(writer);
62 if (!proto_msg->SerializeToZeroCopyStream(&adaptor)) {
63 context->GetDiagnostics()->Error(DiagMessage() << "failed to write " << out_path
64 << " to archive");
65 return false;
66 }
67 }
68
69 if (writer->FinishEntry()) {
70 return true;
71 }
72 }
73 context->GetDiagnostics()->Error(DiagMessage() << "failed to write " << out_path
74 << " to archive: " << writer->GetError());
75 return false;
76}
77
78bool Copy(OutputStream* out, InputStream* in) {
79 const void* in_buffer;
80 size_t in_len;
81 while (in->Next(&in_buffer, &in_len)) {
82 void* out_buffer;
83 size_t out_len;
84 if (!out->Next(&out_buffer, &out_len)) {
85 return !out->HadError();
86 }
87
88 const size_t bytes_to_copy = in_len < out_len ? in_len : out_len;
89 memcpy(out_buffer, in_buffer, bytes_to_copy);
90 out->BackUp(out_len - bytes_to_copy);
91 in->BackUp(in_len - bytes_to_copy);
92 }
93 return !in->HadError();
94}
95
Adam Lesinski00451162017-10-03 07:44:08 -070096bool Copy(ZeroCopyOutputStream* out, InputStream* in) {
97 OutputStreamAdaptor adaptor(out);
98 return Copy(&adaptor, in);
99}
100
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700101} // namespace io
102} // namespace aapt