blob: 79d8d527fe8b77db86a3a3941953e75322b078d7 [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
Yurii Zubrytskyia5775142022-11-02 17:49:49 -070029bool CopyInputStreamToArchive(IAaptContext* context, InputStream* in, std::string_view out_path,
Adam Lesinskid0f492d2017-04-03 18:12:45 -070030 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()) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +000033 context->GetDiagnostics()->Note(android::DiagMessage()
34 << "writing " << out_path << " to archive");
Adam Lesinskid0f492d2017-04-03 18:12:45 -070035 }
36
37 if (!writer->WriteFile(out_path, compression_flags, in)) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +000038 context->GetDiagnostics()->Error(android::DiagMessage()
39 << "failed to write " << out_path
40 << " to archive: " << writer->GetError());
Adam Lesinskid0f492d2017-04-03 18:12:45 -070041 return false;
42 }
43 return true;
44}
45
Yurii Zubrytskyia5775142022-11-02 17:49:49 -070046bool CopyFileToArchive(IAaptContext* context, io::IFile* file, std::string_view out_path,
Adam Lesinskid0f492d2017-04-03 18:12:45 -070047 uint32_t compression_flags, IArchiveWriter* writer) {
Fabien Sanglard2d34e762019-02-21 15:13:29 -080048 TRACE_CALL();
Adam Lesinskid0f492d2017-04-03 18:12:45 -070049 std::unique_ptr<io::IData> data = file->OpenAsData();
50 if (!data) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +000051 context->GetDiagnostics()->Error(android::DiagMessage(file->GetSource())
52 << "failed to open file");
Adam Lesinskid0f492d2017-04-03 18:12:45 -070053 return false;
54 }
55 return CopyInputStreamToArchive(context, data.get(), out_path, compression_flags, writer);
56}
57
Pierre Lecesned55bef72017-11-10 22:31:01 +000058bool CopyFileToArchivePreserveCompression(IAaptContext* context, io::IFile* file,
Yurii Zubrytskyia5775142022-11-02 17:49:49 -070059 std::string_view out_path, IArchiveWriter* writer) {
Pierre Lecesned55bef72017-11-10 22:31:01 +000060 uint32_t compression_flags = file->WasCompressed() ? ArchiveEntry::kCompress : 0u;
61 return CopyFileToArchive(context, file, out_path, compression_flags, writer);
62}
63
Ryan Mitchell22ead1c2019-05-20 16:54:48 -070064bool CopyProtoToArchive(IAaptContext* context, ::google::protobuf::Message* proto_msg,
Yurii Zubrytskyia5775142022-11-02 17:49:49 -070065 std::string_view out_path, uint32_t compression_flags,
Adam Lesinskid0f492d2017-04-03 18:12:45 -070066 IArchiveWriter* writer) {
Fabien Sanglard2d34e762019-02-21 15:13:29 -080067 TRACE_CALL();
Adam Lesinskid0f492d2017-04-03 18:12:45 -070068 if (context->IsVerbose()) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +000069 context->GetDiagnostics()->Note(android::DiagMessage()
70 << "writing " << out_path << " to archive");
Adam Lesinskid0f492d2017-04-03 18:12:45 -070071 }
72
73 if (writer->StartEntry(out_path, compression_flags)) {
74 // Make sure CopyingOutputStreamAdaptor is deleted before we call writer->FinishEntry().
75 {
76 // Wrap our IArchiveWriter with an adaptor that implements the ZeroCopyOutputStream interface.
77 ::google::protobuf::io::CopyingOutputStreamAdaptor adaptor(writer);
78 if (!proto_msg->SerializeToZeroCopyStream(&adaptor)) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +000079 context->GetDiagnostics()->Error(android::DiagMessage()
80 << "failed to write " << out_path << " to archive");
Adam Lesinskid0f492d2017-04-03 18:12:45 -070081 return false;
82 }
83 }
84
85 if (writer->FinishEntry()) {
86 return true;
87 }
88 }
Jeremy Meyer56f36e82022-05-20 20:35:42 +000089 context->GetDiagnostics()->Error(android::DiagMessage() << "failed to write " << out_path
90 << " to archive: " << writer->GetError());
Adam Lesinskid0f492d2017-04-03 18:12:45 -070091 return false;
92}
93
94bool Copy(OutputStream* out, InputStream* in) {
Fabien Sanglard2d34e762019-02-21 15:13:29 -080095 TRACE_CALL();
Adam Lesinskid0f492d2017-04-03 18:12:45 -070096 const void* in_buffer;
97 size_t in_len;
98 while (in->Next(&in_buffer, &in_len)) {
99 void* out_buffer;
100 size_t out_len;
101 if (!out->Next(&out_buffer, &out_len)) {
102 return !out->HadError();
103 }
104
105 const size_t bytes_to_copy = in_len < out_len ? in_len : out_len;
106 memcpy(out_buffer, in_buffer, bytes_to_copy);
107 out->BackUp(out_len - bytes_to_copy);
108 in->BackUp(in_len - bytes_to_copy);
109 }
110 return !in->HadError();
111}
112
Yurii Zubrytskyia5775142022-11-02 17:49:49 -0700113bool Copy(OutputStream* out, StringPiece in) {
Adam Lesinski93190b72017-11-03 15:20:17 -0700114 const char* in_buffer = in.data();
115 size_t in_len = in.size();
116 while (in_len != 0) {
117 void* out_buffer;
118 size_t out_len;
119 if (!out->Next(&out_buffer, &out_len)) {
120 return false;
121 }
122
123 const size_t bytes_to_copy = in_len < out_len ? in_len : out_len;
124 memcpy(out_buffer, in_buffer, bytes_to_copy);
125 out->BackUp(out_len - bytes_to_copy);
126 in_buffer += bytes_to_copy;
127 in_len -= bytes_to_copy;
128 }
129 return true;
130}
131
Adam Lesinski00451162017-10-03 07:44:08 -0700132bool Copy(ZeroCopyOutputStream* out, InputStream* in) {
133 OutputStreamAdaptor adaptor(out);
134 return Copy(&adaptor, in);
135}
136
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700137} // namespace io
138} // namespace aapt