Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 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 | #ifndef AAPT_FLATTEN_ARCHIVE_H |
| 18 | #define AAPT_FLATTEN_ARCHIVE_H |
| 19 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 20 | #include <fstream> |
| 21 | #include <memory> |
| 22 | #include <string> |
| 23 | #include <vector> |
| 24 | |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 25 | #include "androidfw/StringPiece.h" |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 26 | #include "google/protobuf/io/zero_copy_stream_impl_lite.h" |
| 27 | |
| 28 | #include "Diagnostics.h" |
Adam Lesinski | 06460ef | 2017-03-14 18:52:13 -0700 | [diff] [blame^] | 29 | #include "io/Io.h" |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 30 | #include "util/BigBuffer.h" |
| 31 | #include "util/Files.h" |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 32 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 33 | namespace aapt { |
| 34 | |
| 35 | struct ArchiveEntry { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 36 | enum : uint32_t { |
| 37 | kCompress = 0x01, |
| 38 | kAlign = 0x02, |
| 39 | }; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 40 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 41 | std::string path; |
| 42 | uint32_t flags; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 43 | size_t uncompressed_size; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 44 | }; |
| 45 | |
Adam Lesinski | 06460ef | 2017-03-14 18:52:13 -0700 | [diff] [blame^] | 46 | class IArchiveWriter : public ::google::protobuf::io::CopyingOutputStream { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 47 | public: |
| 48 | virtual ~IArchiveWriter() = default; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 49 | |
Adam Lesinski | 06460ef | 2017-03-14 18:52:13 -0700 | [diff] [blame^] | 50 | virtual bool WriteFile(const android::StringPiece& path, uint32_t flags, io::InputStream* in) = 0; |
| 51 | |
| 52 | // Starts a new entry and allows caller to write bytes to it sequentially. |
| 53 | // Only use StartEntry if code you do not control needs to write to a CopyingOutputStream. |
| 54 | // Prefer WriteFile instead of manually calling StartEntry/FinishEntry. |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 55 | virtual bool StartEntry(const android::StringPiece& path, uint32_t flags) = 0; |
Adam Lesinski | 06460ef | 2017-03-14 18:52:13 -0700 | [diff] [blame^] | 56 | |
| 57 | // Called to finish writing an entry previously started by StartEntry. |
| 58 | // Prefer WriteFile instead of manually calling StartEntry/FinishEntry. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 59 | virtual bool FinishEntry() = 0; |
Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame] | 60 | |
Adam Lesinski | 06460ef | 2017-03-14 18:52:13 -0700 | [diff] [blame^] | 61 | // CopyingOutputStream implementation that allows sequential writes to this archive. Only |
| 62 | // valid between calls to StartEntry and FinishEntry. |
| 63 | virtual bool Write(const void* buffer, int size) = 0; |
| 64 | |
| 65 | // Returns true if there was an error writing to the archive. |
| 66 | // The resulting error message can be retrieved from GetError(). |
| 67 | virtual bool HadError() const = 0; |
| 68 | |
| 69 | // Returns the error message if HadError() returns true. |
| 70 | virtual std::string GetError() const = 0; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 71 | }; |
| 72 | |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 73 | std::unique_ptr<IArchiveWriter> CreateDirectoryArchiveWriter(IDiagnostics* diag, |
| 74 | const android::StringPiece& path); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 75 | |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 76 | std::unique_ptr<IArchiveWriter> CreateZipFileArchiveWriter(IDiagnostics* diag, |
| 77 | const android::StringPiece& path); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 78 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 79 | } // namespace aapt |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 80 | |
| 81 | #endif /* AAPT_FLATTEN_ARCHIVE_H */ |