Home | History | Annotate | Download | only in io
      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 
     21 namespace aapt {
     22 namespace io {
     23 
     24 bool CopyInputStreamToArchive(IAaptContext* context, InputStream* in, const std::string& out_path,
     25                               uint32_t compression_flags, IArchiveWriter* writer) {
     26   if (context->IsVerbose()) {
     27     context->GetDiagnostics()->Note(DiagMessage() << "writing " << out_path << " to archive");
     28   }
     29 
     30   if (!writer->WriteFile(out_path, compression_flags, in)) {
     31     context->GetDiagnostics()->Error(DiagMessage() << "failed to write " << out_path
     32                                                    << " to archive: " << writer->GetError());
     33     return false;
     34   }
     35   return true;
     36 }
     37 
     38 bool CopyFileToArchive(IAaptContext* context, io::IFile* file, const std::string& out_path,
     39                        uint32_t compression_flags, IArchiveWriter* writer) {
     40   std::unique_ptr<io::IData> data = file->OpenAsData();
     41   if (!data) {
     42     context->GetDiagnostics()->Error(DiagMessage(file->GetSource()) << "failed to open file");
     43     return false;
     44   }
     45   return CopyInputStreamToArchive(context, data.get(), out_path, compression_flags, writer);
     46 }
     47 
     48 bool CopyProtoToArchive(IAaptContext* context, ::google::protobuf::MessageLite* proto_msg,
     49                         const std::string& out_path, uint32_t compression_flags,
     50                         IArchiveWriter* writer) {
     51   if (context->IsVerbose()) {
     52     context->GetDiagnostics()->Note(DiagMessage() << "writing " << out_path << " to archive");
     53   }
     54 
     55   if (writer->StartEntry(out_path, compression_flags)) {
     56     // Make sure CopyingOutputStreamAdaptor is deleted before we call writer->FinishEntry().
     57     {
     58       // Wrap our IArchiveWriter with an adaptor that implements the ZeroCopyOutputStream interface.
     59       ::google::protobuf::io::CopyingOutputStreamAdaptor adaptor(writer);
     60       if (!proto_msg->SerializeToZeroCopyStream(&adaptor)) {
     61         context->GetDiagnostics()->Error(DiagMessage() << "failed to write " << out_path
     62                                                        << " to archive");
     63         return false;
     64       }
     65     }
     66 
     67     if (writer->FinishEntry()) {
     68       return true;
     69     }
     70   }
     71   context->GetDiagnostics()->Error(DiagMessage() << "failed to write " << out_path
     72                                                  << " to archive: " << writer->GetError());
     73   return false;
     74 }
     75 
     76 bool Copy(OutputStream* out, InputStream* in) {
     77   const void* in_buffer;
     78   size_t in_len;
     79   while (in->Next(&in_buffer, &in_len)) {
     80     void* out_buffer;
     81     size_t out_len;
     82     if (!out->Next(&out_buffer, &out_len)) {
     83       return !out->HadError();
     84     }
     85 
     86     const size_t bytes_to_copy = in_len < out_len ? in_len : out_len;
     87     memcpy(out_buffer, in_buffer, bytes_to_copy);
     88     out->BackUp(out_len - bytes_to_copy);
     89     in->BackUp(in_len - bytes_to_copy);
     90   }
     91   return !in->HadError();
     92 }
     93 
     94 }  // namespace io
     95 }  // namespace aapt
     96