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
20namespace BPrivate {
21	template<typename Value> class RangeArray;
22}
23
24
25namespace BPackageKit {
26
27namespace BHPKG {
28
29
30class BDataReader;
31class BErrorOutput;
32class BPackageWriterParameters;
33
34
35namespace BPrivate {
36
37
38struct hpkg_header;
39
40
41class PackageWriterImpl : public WriterImplBase {
42	typedef	WriterImplBase		inherited;
43
44public:
45								PackageWriterImpl(
46									BPackageWriterListener* listener);
47								~PackageWriterImpl();
48
49			status_t			Init(const char* fileName,
50									const BPackageWriterParameters& parameters);
51			status_t			Init(BPositionIO* file, bool keepFile,
52									const BPackageWriterParameters& parameters);
53			status_t			SetInstallPath(const char* installPath);
54			void				SetCheckLicenses(bool checkLicenses);
55			status_t			AddEntry(const char* fileName, int fd = -1);
56			status_t			Finish();
57
58			status_t			Recompress(BPositionIO* inputFile);
59									// to be called after Init(); no Finish()
60
61private:
62			struct Attribute;
63			struct PackageContentHandler;
64			struct Entry;
65			struct SubPathAdder;
66			struct HeapAttributeOffsetter;
67
68			typedef DoublyLinkedList<Entry> EntryList;
69
70private:
71			status_t			_Init(BPositionIO* file, bool keepFile,
72									const char* fileName,
73									const BPackageWriterParameters& parameters);
74			status_t			_Finish();
75
76			status_t			_Recompress(BPositionIO* inputFile);
77
78			status_t			_RegisterEntry(const char* fileName, int fd);
79			Entry*				_RegisterEntry(Entry* parent,
80									const char* name, size_t nameLength, int fd,
81									bool isImplicit);
82
83			status_t			_CheckLicenses();
84			bool				_IsEntryInPackage(const char* fileName);
85
86			void				_UpdateReadPackageInfo();
87			void				_UpdateCheckEntryCollisions();
88			void				_UpdateCheckEntryCollisions(
89									Attribute* parentAttribute, int dirFD,
90									Entry* entry, const char* fileName,
91									char* pathBuffer);
92			void				_CompactHeap();
93			void				_AttributeRemoved(Attribute* attribute);
94
95			void				_WriteTOC(hpkg_header& header, uint64& _length);
96			void				_WriteAttributeChildren(Attribute* attribute);
97
98			void				_WritePackageAttributes(hpkg_header& header,
99									uint64& _length);
100			uint32				_WritePackageAttributesCompressed(
101									uint32& _stringsLengthUncompressed,
102									uint32& _attributesLengthUncompressed);
103
104			void				_AddEntry(int dirFD, Entry* entry,
105									const char* fileName, char* pathBuffer);
106			void				_AddDirectoryChildren(Entry* entry, int fd,
107									char* pathBuffer);
108
109			Attribute*			_AddAttribute(BHPKGAttributeID attributeID,
110									const AttributeValue& value);
111
112	template<typename Type>
113	inline	Attribute*			_AddAttribute(BHPKGAttributeID attributeID,
114									Type value);
115
116			Attribute*			_AddStringAttribute(
117									BHPKGAttributeID attributeID,
118									const char* value);
119			Attribute*			_AddDataAttribute(BHPKGAttributeID attributeID,
120									uint64 dataSize, uint64 dataOffset);
121			Attribute*			_AddDataAttribute(BHPKGAttributeID attributeID,
122									uint64 dataSize, const uint8* data);
123
124			status_t			_AddData(BDataReader& dataReader, off_t size);
125
126private:
127			BPackageWriterListener*	fListener;
128
129			off_t				fHeapOffset;
130			uint16				fHeaderSize;
131
132			::BPrivate::RangeArray<uint64>* fHeapRangesToRemove;
133
134			Entry*				fRootEntry;
135
136			Attribute*			fRootAttribute;
137			Attribute*			fTopAttribute;
138
139			StringCache			fStringCache;
140
141			BPackageInfo		fPackageInfo;
142			BString				fInstallPath;
143			bool				fCheckLicenses;
144};
145
146
147}	// namespace BPrivate
148
149}	// namespace BHPKG
150
151}	// namespace BPackageKit
152
153
154#endif	// _PACKAGE__HPKG__PRIVATE__PACKAGE_WRITER_IMPL_H_
155