blob: 3c3d0ab74c5200651d4a842bdc164bb8fde13138 [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
Adam Lesinski46708052017-09-29 14:49:15 -070017#ifndef AAPT_FORMAT_ARCHIVE_H
18#define AAPT_FORMAT_ARCHIVE_H
Adam Lesinski1ab598f2015-08-14 14:26:04 -070019
Adam Lesinski1ab598f2015-08-14 14:26:04 -070020#include <fstream>
21#include <memory>
22#include <string>
23#include <vector>
24
Jeremy Meyer56f36e82022-05-20 20:35:42 +000025#include "androidfw/BigBuffer.h"
26#include "androidfw/IDiagnostics.h"
Jeremy Meyerb4f83ff2023-11-30 19:29:50 +000027#include "androidfw/Streams.h"
Adam Lesinskid5083f62017-01-16 15:07:21 -080028#include "androidfw/StringPiece.h"
Adam Lesinskice5e56e2016-10-21 17:56:45 -070029#include "google/protobuf/io/zero_copy_stream_impl_lite.h"
Adam Lesinskice5e56e2016-10-21 17:56:45 -070030#include "util/Files.h"
Adam Lesinskice5e56e2016-10-21 17:56:45 -070031
Adam Lesinski1ab598f2015-08-14 14:26:04 -070032namespace aapt {
33
34struct ArchiveEntry {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070035 enum : uint32_t {
36 kCompress = 0x01,
37 kAlign = 0x02,
38 };
Adam Lesinski1ab598f2015-08-14 14:26:04 -070039
Adam Lesinskicacb28f2016-10-19 12:18:14 -070040 std::string path;
41 uint32_t flags;
Adam Lesinskice5e56e2016-10-21 17:56:45 -070042 size_t uncompressed_size;
Adam Lesinski1ab598f2015-08-14 14:26:04 -070043};
44
Adam Lesinski06460ef2017-03-14 18:52:13 -070045class IArchiveWriter : public ::google::protobuf::io::CopyingOutputStream {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070046 public:
47 virtual ~IArchiveWriter() = default;
Adam Lesinski1ab598f2015-08-14 14:26:04 -070048
Jeremy Meyerb4f83ff2023-11-30 19:29:50 +000049 virtual bool WriteFile(android::StringPiece path, uint32_t flags, android::InputStream* in) = 0;
Adam Lesinski06460ef2017-03-14 18:52:13 -070050
51 // Starts a new entry and allows caller to write bytes to it sequentially.
52 // Only use StartEntry if code you do not control needs to write to a CopyingOutputStream.
53 // Prefer WriteFile instead of manually calling StartEntry/FinishEntry.
Yurii Zubrytskyia5775142022-11-02 17:49:49 -070054 virtual bool StartEntry(android::StringPiece path, uint32_t flags) = 0;
Adam Lesinski06460ef2017-03-14 18:52:13 -070055
56 // Called to finish writing an entry previously started by StartEntry.
57 // Prefer WriteFile instead of manually calling StartEntry/FinishEntry.
Adam Lesinskice5e56e2016-10-21 17:56:45 -070058 virtual bool FinishEntry() = 0;
Adam Lesinski59e04c62016-02-04 15:59:23 -080059
Adam Lesinski06460ef2017-03-14 18:52:13 -070060 // CopyingOutputStream implementation that allows sequential writes to this archive. Only
61 // valid between calls to StartEntry and FinishEntry.
62 virtual bool Write(const void* buffer, int size) = 0;
63
64 // Returns true if there was an error writing to the archive.
65 // The resulting error message can be retrieved from GetError().
66 virtual bool HadError() const = 0;
67
68 // Returns the error message if HadError() returns true.
69 virtual std::string GetError() const = 0;
Adam Lesinski1ab598f2015-08-14 14:26:04 -070070};
71
Jeremy Meyer56f36e82022-05-20 20:35:42 +000072std::unique_ptr<IArchiveWriter> CreateDirectoryArchiveWriter(android::IDiagnostics* diag,
Yurii Zubrytskyia5775142022-11-02 17:49:49 -070073 android::StringPiece path);
Adam Lesinski1ab598f2015-08-14 14:26:04 -070074
Jeremy Meyer56f36e82022-05-20 20:35:42 +000075std::unique_ptr<IArchiveWriter> CreateZipFileArchiveWriter(android::IDiagnostics* diag,
Yurii Zubrytskyia5775142022-11-02 17:49:49 -070076 android::StringPiece path);
Adam Lesinski1ab598f2015-08-14 14:26:04 -070077
Adam Lesinskicacb28f2016-10-19 12:18:14 -070078} // namespace aapt
Adam Lesinski1ab598f2015-08-14 14:26:04 -070079
Adam Lesinski46708052017-09-29 14:49:15 -070080#endif /* AAPT_FORMAT_ARCHIVE_H */