blob: ce6d9352180d5b3dadd88594fb015fbb59779db6 [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
Fabien Sanglard2d34e762019-02-21 15:13:29 -080021#include "trace/TraceBuffer.h"
22
Adam Lesinski93190b72017-11-03 15:20:17 -070023using ::android::StringPiece;
Adam Lesinski00451162017-10-03 07:44:08 -070024using ::google::protobuf::io::ZeroCopyOutputStream;
25
Adam Lesinskid0f492d2017-04-03 18:12:45 -070026namespace aapt {
27namespace io {
28
29bool CopyInputStreamToArchive(IAaptContext* context, InputStream* in, const std::string& out_path,
30 uint32_t compression_flags, IArchiveWriter* writer) {
Fabien Sanglard2d34e762019-02-21 15:13:29 -080031 TRACE_CALL();
Adam Lesinskid0f492d2017-04-03 18:12:45 -070032 if (context->IsVerbose()) {
33 context->GetDiagnostics()->Note(DiagMessage() << "writing " << out_path << " to archive");
34 }
35
36 if (!writer->WriteFile(out_path, compression_flags, in)) {
37 context->GetDiagnostics()->Error(DiagMessage() << "failed to write " << out_path
38 << " to archive: " << writer->GetError());
39 return false;
40 }
41 return true;
42}
43
44bool CopyFileToArchive(IAaptContext* context, io::IFile* file, const std::string& out_path,
45 uint32_t compression_flags, IArchiveWriter* writer) {
Fabien Sanglard2d34e762019-02-21 15:13:29 -080046 TRACE_CALL();
Adam Lesinskid0f492d2017-04-03 18:12:45 -070047 std::unique_ptr<io::IData> data = file->OpenAsData();
48 if (!data) {
49 context->GetDiagnostics()->Error(DiagMessage(file->GetSource()) << "failed to open file");
50 return false;
51 }
52 return CopyInputStreamToArchive(context, data.get(), out_path, compression_flags, writer);
53}
54
Pierre Lecesned55bef72017-11-10 22:31:01 +000055bool CopyFileToArchivePreserveCompression(IAaptContext* context, io::IFile* file,
56 const std::string& out_path, IArchiveWriter* writer) {
57 uint32_t compression_flags = file->WasCompressed() ? ArchiveEntry::kCompress : 0u;
58 return CopyFileToArchive(context, file, out_path, compression_flags, writer);
59}
60
Adam Lesinskid0f492d2017-04-03 18:12:45 -070061bool CopyProtoToArchive(IAaptContext* context, ::google::protobuf::MessageLite* proto_msg,
62 const std::string& out_path, uint32_t compression_flags,
63 IArchiveWriter* writer) {
Fabien Sanglard2d34e762019-02-21 15:13:29 -080064 TRACE_CALL();
Adam Lesinskid0f492d2017-04-03 18:12:45 -070065 if (context->IsVerbose()) {
66 context->GetDiagnostics()->Note(DiagMessage() << "writing " << out_path << " to archive");
67 }
68
69 if (writer->StartEntry(out_path, compression_flags)) {
70 // Make sure CopyingOutputStreamAdaptor is deleted before we call writer->FinishEntry().
71 {
72 // Wrap our IArchiveWriter with an adaptor that implements the ZeroCopyOutputStream interface.
73 ::google::protobuf::io::CopyingOutputStreamAdaptor adaptor(writer);
74 if (!proto_msg->SerializeToZeroCopyStream(&adaptor)) {
75 context->GetDiagnostics()->Error(DiagMessage() << "failed to write " << out_path
76 << " to archive");
77 return false;
78 }
79 }
80
81 if (writer->FinishEntry()) {
82 return true;
83 }
84 }
85 context->GetDiagnostics()->Error(DiagMessage() << "failed to write " << out_path
86 << " to archive: " << writer->GetError());
87 return false;
88}
89
90bool Copy(OutputStream* out, InputStream* in) {
Fabien Sanglard2d34e762019-02-21 15:13:29 -080091 TRACE_CALL();
Adam Lesinskid0f492d2017-04-03 18:12:45 -070092 const void* in_buffer;
93 size_t in_len;
94 while (in->Next(&in_buffer, &in_len)) {
95 void* out_buffer;
96 size_t out_len;
97 if (!out->Next(&out_buffer, &out_len)) {
98 return !out->HadError();
99 }
100
101 const size_t bytes_to_copy = in_len < out_len ? in_len : out_len;
102 memcpy(out_buffer, in_buffer, bytes_to_copy);
103 out->BackUp(out_len - bytes_to_copy);
104 in->BackUp(in_len - bytes_to_copy);
105 }
106 return !in->HadError();
107}
108
Adam Lesinski93190b72017-11-03 15:20:17 -0700109bool Copy(OutputStream* out, const StringPiece& in) {
110 const char* in_buffer = in.data();
111 size_t in_len = in.size();
112 while (in_len != 0) {
113 void* out_buffer;
114 size_t out_len;
115 if (!out->Next(&out_buffer, &out_len)) {
116 return false;
117 }
118
119 const size_t bytes_to_copy = in_len < out_len ? in_len : out_len;
120 memcpy(out_buffer, in_buffer, bytes_to_copy);
121 out->BackUp(out_len - bytes_to_copy);
122 in_buffer += bytes_to_copy;
123 in_len -= bytes_to_copy;
124 }
125 return true;
126}
127
Adam Lesinski00451162017-10-03 07:44:08 -0700128bool Copy(ZeroCopyOutputStream* out, InputStream* in) {
129 OutputStreamAdaptor adaptor(out);
130 return Copy(&adaptor, in);
131}
132
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700133} // namespace io
134} // namespace aapt