blob: 9616e479eda0dcf6cfe818a9ecca9788ccdcef7d [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
Jeremy Meyerb4f83ff2023-11-30 19:29:50 +000029bool CopyInputStreamToArchive(IAaptContext* context, android::InputStream* in,
30 std::string_view out_path, uint32_t compression_flags,
31 IArchiveWriter* writer) {
Fabien Sanglard2d34e762019-02-21 15:13:29 -080032 TRACE_CALL();
Adam Lesinskid0f492d2017-04-03 18:12:45 -070033 if (context->IsVerbose()) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +000034 context->GetDiagnostics()->Note(android::DiagMessage()
35 << "writing " << out_path << " to archive");
Adam Lesinskid0f492d2017-04-03 18:12:45 -070036 }
37
38 if (!writer->WriteFile(out_path, compression_flags, in)) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +000039 context->GetDiagnostics()->Error(android::DiagMessage()
40 << "failed to write " << out_path
41 << " to archive: " << writer->GetError());
Adam Lesinskid0f492d2017-04-03 18:12:45 -070042 return false;
43 }
44 return true;
45}
46
Yurii Zubrytskyia5775142022-11-02 17:49:49 -070047bool CopyFileToArchive(IAaptContext* context, io::IFile* file, std::string_view out_path,
Adam Lesinskid0f492d2017-04-03 18:12:45 -070048 uint32_t compression_flags, IArchiveWriter* writer) {
Fabien Sanglard2d34e762019-02-21 15:13:29 -080049 TRACE_CALL();
Adam Lesinskid0f492d2017-04-03 18:12:45 -070050 std::unique_ptr<io::IData> data = file->OpenAsData();
51 if (!data) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +000052 context->GetDiagnostics()->Error(android::DiagMessage(file->GetSource())
53 << "failed to open file");
Adam Lesinskid0f492d2017-04-03 18:12:45 -070054 return false;
55 }
56 return CopyInputStreamToArchive(context, data.get(), out_path, compression_flags, writer);
57}
58
Pierre Lecesned55bef72017-11-10 22:31:01 +000059bool CopyFileToArchivePreserveCompression(IAaptContext* context, io::IFile* file,
Yurii Zubrytskyia5775142022-11-02 17:49:49 -070060 std::string_view out_path, IArchiveWriter* writer) {
Pierre Lecesned55bef72017-11-10 22:31:01 +000061 uint32_t compression_flags = file->WasCompressed() ? ArchiveEntry::kCompress : 0u;
62 return CopyFileToArchive(context, file, out_path, compression_flags, writer);
63}
64
Ryan Mitchell22ead1c2019-05-20 16:54:48 -070065bool CopyProtoToArchive(IAaptContext* context, ::google::protobuf::Message* proto_msg,
Yurii Zubrytskyia5775142022-11-02 17:49:49 -070066 std::string_view out_path, uint32_t compression_flags,
Adam Lesinskid0f492d2017-04-03 18:12:45 -070067 IArchiveWriter* writer) {
Fabien Sanglard2d34e762019-02-21 15:13:29 -080068 TRACE_CALL();
Adam Lesinskid0f492d2017-04-03 18:12:45 -070069 if (context->IsVerbose()) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +000070 context->GetDiagnostics()->Note(android::DiagMessage()
71 << "writing " << out_path << " to archive");
Adam Lesinskid0f492d2017-04-03 18:12:45 -070072 }
73
74 if (writer->StartEntry(out_path, compression_flags)) {
75 // Make sure CopyingOutputStreamAdaptor is deleted before we call writer->FinishEntry().
76 {
77 // Wrap our IArchiveWriter with an adaptor that implements the ZeroCopyOutputStream interface.
78 ::google::protobuf::io::CopyingOutputStreamAdaptor adaptor(writer);
79 if (!proto_msg->SerializeToZeroCopyStream(&adaptor)) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +000080 context->GetDiagnostics()->Error(android::DiagMessage()
81 << "failed to write " << out_path << " to archive");
Adam Lesinskid0f492d2017-04-03 18:12:45 -070082 return false;
83 }
84 }
85
86 if (writer->FinishEntry()) {
87 return true;
88 }
89 }
Jeremy Meyer56f36e82022-05-20 20:35:42 +000090 context->GetDiagnostics()->Error(android::DiagMessage() << "failed to write " << out_path
91 << " to archive: " << writer->GetError());
Adam Lesinskid0f492d2017-04-03 18:12:45 -070092 return false;
93}
94
Jeremy Meyerb4f83ff2023-11-30 19:29:50 +000095bool Copy(android::OutputStream* out, android::InputStream* in) {
Fabien Sanglard2d34e762019-02-21 15:13:29 -080096 TRACE_CALL();
Adam Lesinskid0f492d2017-04-03 18:12:45 -070097 const void* in_buffer;
98 size_t in_len;
99 while (in->Next(&in_buffer, &in_len)) {
100 void* out_buffer;
101 size_t out_len;
102 if (!out->Next(&out_buffer, &out_len)) {
103 return !out->HadError();
104 }
105
106 const size_t bytes_to_copy = in_len < out_len ? in_len : out_len;
107 memcpy(out_buffer, in_buffer, bytes_to_copy);
108 out->BackUp(out_len - bytes_to_copy);
109 in->BackUp(in_len - bytes_to_copy);
110 }
111 return !in->HadError();
112}
113
Jeremy Meyerb4f83ff2023-11-30 19:29:50 +0000114bool Copy(android::OutputStream* out, StringPiece in) {
Adam Lesinski93190b72017-11-03 15:20:17 -0700115 const char* in_buffer = in.data();
116 size_t in_len = in.size();
117 while (in_len != 0) {
118 void* out_buffer;
119 size_t out_len;
120 if (!out->Next(&out_buffer, &out_len)) {
121 return false;
122 }
123
124 const size_t bytes_to_copy = in_len < out_len ? in_len : out_len;
125 memcpy(out_buffer, in_buffer, bytes_to_copy);
126 out->BackUp(out_len - bytes_to_copy);
127 in_buffer += bytes_to_copy;
128 in_len -= bytes_to_copy;
129 }
130 return true;
131}
132
Jeremy Meyerb4f83ff2023-11-30 19:29:50 +0000133bool Copy(ZeroCopyOutputStream* out, android::InputStream* in) {
Adam Lesinski00451162017-10-03 07:44:08 -0700134 OutputStreamAdaptor adaptor(out);
135 return Copy(&adaptor, in);
136}
137
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700138} // namespace io
139} // namespace aapt