blob: 4ee4ce71a5c51b4680c07b9b35fd9cd01088f34f [file] [log] [blame]
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001/*
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 Lesinski1ab598f2015-08-14 14:26:04 -070020#include <fstream>
21#include <memory>
22#include <string>
23#include <vector>
24
Adam Lesinskid5083f62017-01-16 15:07:21 -080025#include "androidfw/StringPiece.h"
Adam Lesinskice5e56e2016-10-21 17:56:45 -070026#include "google/protobuf/io/zero_copy_stream_impl_lite.h"
27
28#include "Diagnostics.h"
Adam Lesinski06460ef2017-03-14 18:52:13 -070029#include "io/Io.h"
Adam Lesinskice5e56e2016-10-21 17:56:45 -070030#include "util/BigBuffer.h"
31#include "util/Files.h"
Adam Lesinskice5e56e2016-10-21 17:56:45 -070032
Adam Lesinski1ab598f2015-08-14 14:26:04 -070033namespace aapt {
34
35struct ArchiveEntry {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070036 enum : uint32_t {
37 kCompress = 0x01,
38 kAlign = 0x02,
39 };
Adam Lesinski1ab598f2015-08-14 14:26:04 -070040
Adam Lesinskicacb28f2016-10-19 12:18:14 -070041 std::string path;
42 uint32_t flags;
Adam Lesinskice5e56e2016-10-21 17:56:45 -070043 size_t uncompressed_size;
Adam Lesinski1ab598f2015-08-14 14:26:04 -070044};
45
Adam Lesinski06460ef2017-03-14 18:52:13 -070046class IArchiveWriter : public ::google::protobuf::io::CopyingOutputStream {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070047 public:
48 virtual ~IArchiveWriter() = default;
Adam Lesinski1ab598f2015-08-14 14:26:04 -070049
Adam Lesinski06460ef2017-03-14 18:52:13 -070050 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 Lesinskid5083f62017-01-16 15:07:21 -080055 virtual bool StartEntry(const android::StringPiece& path, uint32_t flags) = 0;
Adam Lesinski06460ef2017-03-14 18:52:13 -070056
57 // Called to finish writing an entry previously started by StartEntry.
58 // Prefer WriteFile instead of manually calling StartEntry/FinishEntry.
Adam Lesinskice5e56e2016-10-21 17:56:45 -070059 virtual bool FinishEntry() = 0;
Adam Lesinski59e04c62016-02-04 15:59:23 -080060
Adam Lesinski06460ef2017-03-14 18:52:13 -070061 // 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 Lesinski1ab598f2015-08-14 14:26:04 -070071};
72
Adam Lesinskid5083f62017-01-16 15:07:21 -080073std::unique_ptr<IArchiveWriter> CreateDirectoryArchiveWriter(IDiagnostics* diag,
74 const android::StringPiece& path);
Adam Lesinski1ab598f2015-08-14 14:26:04 -070075
Adam Lesinskid5083f62017-01-16 15:07:21 -080076std::unique_ptr<IArchiveWriter> CreateZipFileArchiveWriter(IDiagnostics* diag,
77 const android::StringPiece& path);
Adam Lesinski1ab598f2015-08-14 14:26:04 -070078
Adam Lesinskicacb28f2016-10-19 12:18:14 -070079} // namespace aapt
Adam Lesinski1ab598f2015-08-14 14:26:04 -070080
81#endif /* AAPT_FLATTEN_ARCHIVE_H */