• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_FORMAT_ARCHIVE_H
18 #define AAPT_FORMAT_ARCHIVE_H
19 
20 #include <fstream>
21 #include <memory>
22 #include <string>
23 #include <vector>
24 
25 #include "androidfw/BigBuffer.h"
26 #include "androidfw/IDiagnostics.h"
27 #include "androidfw/Streams.h"
28 #include "androidfw/StringPiece.h"
29 #include "google/protobuf/io/zero_copy_stream_impl_lite.h"
30 #include "util/Files.h"
31 
32 namespace aapt {
33 
34 struct ArchiveEntry {
35   enum : uint32_t {
36     kCompress = 0x01,
37     kAlign = 0x02,
38   };
39 
40   std::string path;
41   uint32_t flags;
42   size_t uncompressed_size;
43 };
44 
45 class IArchiveWriter : public ::google::protobuf::io::CopyingOutputStream {
46  public:
47   virtual ~IArchiveWriter() = default;
48 
49   virtual bool WriteFile(android::StringPiece path, uint32_t flags, android::InputStream* in) = 0;
50 
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.
54   virtual bool StartEntry(android::StringPiece path, uint32_t flags) = 0;
55 
56   // Called to finish writing an entry previously started by StartEntry.
57   // Prefer WriteFile instead of manually calling StartEntry/FinishEntry.
58   virtual bool FinishEntry() = 0;
59 
60   // 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;
70 };
71 
72 std::unique_ptr<IArchiveWriter> CreateDirectoryArchiveWriter(android::IDiagnostics* diag,
73                                                              android::StringPiece path);
74 
75 std::unique_ptr<IArchiveWriter> CreateZipFileArchiveWriter(android::IDiagnostics* diag,
76                                                            android::StringPiece path);
77 
78 }  // namespace aapt
79 
80 #endif /* AAPT_FORMAT_ARCHIVE_H */
81