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