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