xref: /haiku/headers/private/package/hpkg/PackageWriterImpl.h (revision 7de6af25e9ee8c0279dcf9dcebb241c398f23fae)
1 /*
2  * Copyright 2011, Ingo Weinhold, ingo_weinhold@gmx.de.
3  * Copyright 2011, Oliver Tappe <zooey@hirschkaefer.de>
4  * Distributed under the terms of the MIT License.
5  */
6 #ifndef _PACKAGE__HPKG__PRIVATE__PACKAGE_WRITER_IMPL_H_
7 #define _PACKAGE__HPKG__PRIVATE__PACKAGE_WRITER_IMPL_H_
8 
9 
10 #include <util/DoublyLinkedList.h>
11 #include <util/OpenHashTable.h>
12 
13 #include <String.h>
14 
15 #include <package/hpkg/PackageWriter.h>
16 #include <package/hpkg/Strings.h>
17 #include <package/hpkg/WriterImplBase.h>
18 
19 
20 namespace BPrivate {
21 	template<typename Value> class RangeArray;
22 }
23 
24 
25 namespace BPackageKit {
26 
27 namespace BHPKG {
28 
29 
30 class BDataReader;
31 class BErrorOutput;
32 
33 
34 namespace BPrivate {
35 
36 
37 struct hpkg_header;
38 
39 class PackageWriterImpl : public WriterImplBase {
40 	typedef	WriterImplBase		inherited;
41 
42 public:
43 								PackageWriterImpl(
44 									BPackageWriterListener* listener);
45 								~PackageWriterImpl();
46 
47 			status_t			Init(const char* fileName, uint32 flags);
48 			status_t			SetInstallPath(const char* installPath);
49 			void				SetCheckLicenses(bool checkLicenses);
50 			status_t			AddEntry(const char* fileName, int fd = -1);
51 			status_t			Finish();
52 
53 private:
54 			struct Attribute;
55 			struct PackageContentHandler;
56 			struct Entry;
57 			struct SubPathAdder;
58 			struct HeapAttributeOffsetter;
59 
60 			typedef DoublyLinkedList<Entry> EntryList;
61 
62 private:
63 			status_t			_Init(const char* fileName, uint32 flags);
64 			status_t			_Finish();
65 
66 			status_t			_RegisterEntry(const char* fileName, int fd);
67 			Entry*				_RegisterEntry(Entry* parent,
68 									const char* name, size_t nameLength, int fd,
69 									bool isImplicit);
70 
71 			status_t			_CheckLicenses();
72 			bool				_IsEntryInPackage(const char* fileName);
73 
74 			void				_UpdateReadPackageInfo();
75 			void				_UpdateCheckEntryCollisions();
76 			void				_UpdateCheckEntryCollisions(
77 									Attribute* parentAttribute, int dirFD,
78 									Entry* entry, const char* fileName,
79 									char* pathBuffer);
80 			void				_CompactHeap();
81 			void				_MoveHeapChunk(off_t fromOffset, off_t toOffset,
82 									off_t size);
83 			void				_AttributeRemoved(Attribute* attribute);
84 
85 			void				_WriteTOC(hpkg_header& header);
86 			int32				_WriteTOCCompressed(
87 									uint64& _uncompressedStringsSize,
88 									uint64& _uncompressedMainSize,
89 									uint64& _tocUncompressedSize);
90 			int32				_WriteTOCUncompressed(
91 									uint64& _uncompressedStringsSize,
92 									uint64& _uncompressedMainSize,
93 									uint64& _tocUncompressedSize);
94 			int32				_WriteTOCSections(uint64& _stringsSize,
95 									uint64& _mainSize);
96 			void				_WriteAttributeChildren(Attribute* attribute);
97 
98 			void				_WritePackageAttributes(hpkg_header& header);
99 			uint32				_WritePackageAttributesCompressed(
100 									uint32& _stringsLengthUncompressed,
101 									uint32& _attributesLengthUncompressed);
102 			uint32				_WritePackageAttributesUncompressed(
103 									uint32& _stringsLengthUncompressed,
104 									uint32& _attributesLengthUncompressed);
105 
106 			void				_AddEntry(int dirFD, Entry* entry,
107 									const char* fileName, char* pathBuffer);
108 			void				_AddDirectoryChildren(Entry* entry, int fd,
109 									char* pathBuffer);
110 
111 			Attribute*			_AddAttribute(BHPKGAttributeID attributeID,
112 									const AttributeValue& value);
113 
114 	template<typename Type>
115 	inline	Attribute*			_AddAttribute(BHPKGAttributeID attributeID,
116 									Type value);
117 
118 			Attribute*			_AddStringAttribute(
119 									BHPKGAttributeID attributeID,
120 									const char* value);
121 			Attribute*			_AddDataAttribute(BHPKGAttributeID attributeID,
122 									uint64 dataSize, uint64 dataOffset);
123 			Attribute*			_AddDataAttribute(BHPKGAttributeID attributeID,
124 									uint64 dataSize, const uint8* data);
125 
126 			status_t			_AddData(BDataReader& dataReader, off_t size);
127 
128 			status_t			_WriteUncompressedData(BDataReader& dataReader,
129 									off_t size, uint64 writeOffset);
130 			status_t			_WriteZlibCompressedData(
131 									BDataReader& dataReader,
132 									off_t size, uint64 writeOffset,
133 									uint64& _compressedSize);
134 
135 private:
136 			BPackageWriterListener*	fListener;
137 
138 			off_t				fHeapOffset;
139 			off_t				fHeapEnd;
140 
141 			::BPrivate::RangeArray<off_t>* fHeapRangesToRemove;
142 
143 			void*				fDataBuffer;
144 			const size_t		fDataBufferSize;
145 
146 			Entry*				fRootEntry;
147 
148 			Attribute*			fRootAttribute;
149 			Attribute*			fTopAttribute;
150 
151 			StringCache			fStringCache;
152 
153 			BPackageInfo		fPackageInfo;
154 			BString				fInstallPath;
155 			bool				fCheckLicenses;
156 };
157 
158 
159 }	// namespace BPrivate
160 
161 }	// namespace BHPKG
162 
163 }	// namespace BPackageKit
164 
165 
166 #endif	// _PACKAGE__HPKG__PRIVATE__PACKAGE_WRITER_IMPL_H_
167