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