Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 1 | /* |
| 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 Sanglard | 2d34e76 | 2019-02-21 15:13:29 -0800 | [diff] [blame] | 21 | #include "trace/TraceBuffer.h" |
| 22 | |
Adam Lesinski | 93190b7 | 2017-11-03 15:20:17 -0700 | [diff] [blame] | 23 | using ::android::StringPiece; |
Adam Lesinski | 0045116 | 2017-10-03 07:44:08 -0700 | [diff] [blame] | 24 | using ::google::protobuf::io::ZeroCopyOutputStream; |
| 25 | |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 26 | namespace aapt { |
| 27 | namespace io { |
| 28 | |
Yurii Zubrytskyi | a577514 | 2022-11-02 17:49:49 -0700 | [diff] [blame^] | 29 | bool CopyInputStreamToArchive(IAaptContext* context, InputStream* in, std::string_view out_path, |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 30 | uint32_t compression_flags, IArchiveWriter* writer) { |
Fabien Sanglard | 2d34e76 | 2019-02-21 15:13:29 -0800 | [diff] [blame] | 31 | TRACE_CALL(); |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 32 | if (context->IsVerbose()) { |
Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame] | 33 | context->GetDiagnostics()->Note(android::DiagMessage() |
| 34 | << "writing " << out_path << " to archive"); |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 35 | } |
| 36 | |
| 37 | if (!writer->WriteFile(out_path, compression_flags, in)) { |
Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame] | 38 | context->GetDiagnostics()->Error(android::DiagMessage() |
| 39 | << "failed to write " << out_path |
| 40 | << " to archive: " << writer->GetError()); |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 41 | return false; |
| 42 | } |
| 43 | return true; |
| 44 | } |
| 45 | |
Yurii Zubrytskyi | a577514 | 2022-11-02 17:49:49 -0700 | [diff] [blame^] | 46 | bool CopyFileToArchive(IAaptContext* context, io::IFile* file, std::string_view out_path, |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 47 | uint32_t compression_flags, IArchiveWriter* writer) { |
Fabien Sanglard | 2d34e76 | 2019-02-21 15:13:29 -0800 | [diff] [blame] | 48 | TRACE_CALL(); |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 49 | std::unique_ptr<io::IData> data = file->OpenAsData(); |
| 50 | if (!data) { |
Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame] | 51 | context->GetDiagnostics()->Error(android::DiagMessage(file->GetSource()) |
| 52 | << "failed to open file"); |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 53 | return false; |
| 54 | } |
| 55 | return CopyInputStreamToArchive(context, data.get(), out_path, compression_flags, writer); |
| 56 | } |
| 57 | |
Pierre Lecesne | d55bef7 | 2017-11-10 22:31:01 +0000 | [diff] [blame] | 58 | bool CopyFileToArchivePreserveCompression(IAaptContext* context, io::IFile* file, |
Yurii Zubrytskyi | a577514 | 2022-11-02 17:49:49 -0700 | [diff] [blame^] | 59 | std::string_view out_path, IArchiveWriter* writer) { |
Pierre Lecesne | d55bef7 | 2017-11-10 22:31:01 +0000 | [diff] [blame] | 60 | uint32_t compression_flags = file->WasCompressed() ? ArchiveEntry::kCompress : 0u; |
| 61 | return CopyFileToArchive(context, file, out_path, compression_flags, writer); |
| 62 | } |
| 63 | |
Ryan Mitchell | 22ead1c | 2019-05-20 16:54:48 -0700 | [diff] [blame] | 64 | bool CopyProtoToArchive(IAaptContext* context, ::google::protobuf::Message* proto_msg, |
Yurii Zubrytskyi | a577514 | 2022-11-02 17:49:49 -0700 | [diff] [blame^] | 65 | std::string_view out_path, uint32_t compression_flags, |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 66 | IArchiveWriter* writer) { |
Fabien Sanglard | 2d34e76 | 2019-02-21 15:13:29 -0800 | [diff] [blame] | 67 | TRACE_CALL(); |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 68 | if (context->IsVerbose()) { |
Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame] | 69 | context->GetDiagnostics()->Note(android::DiagMessage() |
| 70 | << "writing " << out_path << " to archive"); |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 71 | } |
| 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 Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame] | 79 | context->GetDiagnostics()->Error(android::DiagMessage() |
| 80 | << "failed to write " << out_path << " to archive"); |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 81 | return false; |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | if (writer->FinishEntry()) { |
| 86 | return true; |
| 87 | } |
| 88 | } |
Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame] | 89 | context->GetDiagnostics()->Error(android::DiagMessage() << "failed to write " << out_path |
| 90 | << " to archive: " << writer->GetError()); |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 91 | return false; |
| 92 | } |
| 93 | |
| 94 | bool Copy(OutputStream* out, InputStream* in) { |
Fabien Sanglard | 2d34e76 | 2019-02-21 15:13:29 -0800 | [diff] [blame] | 95 | TRACE_CALL(); |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 96 | 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 Zubrytskyi | a577514 | 2022-11-02 17:49:49 -0700 | [diff] [blame^] | 113 | bool Copy(OutputStream* out, StringPiece in) { |
Adam Lesinski | 93190b7 | 2017-11-03 15:20:17 -0700 | [diff] [blame] | 114 | 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 Lesinski | 0045116 | 2017-10-03 07:44:08 -0700 | [diff] [blame] | 132 | bool Copy(ZeroCopyOutputStream* out, InputStream* in) { |
| 133 | OutputStreamAdaptor adaptor(out); |
| 134 | return Copy(&adaptor, in); |
| 135 | } |
| 136 | |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 137 | } // namespace io |
| 138 | } // namespace aapt |