xref: /haiku/headers/private/package/hpkg/PackageFileHeapWriter.h (revision f73f5d4c42a01ece688cbb57b5d332cc0f68b2c6)
1 /*
2  * Copyright 2013, Ingo Weinhold, ingo_weinhold@gmx.de.
3  * Distributed under the terms of the MIT License.
4  */
5 #ifndef _PACKAGE__HPKG__PRIVATE__PACKAGE_FILE_HEAP_WRITER_H_
6 #define _PACKAGE__HPKG__PRIVATE__PACKAGE_FILE_HEAP_WRITER_H_
7 
8 
9 #include <Array.h>
10 #include <package/hpkg/DataWriters.h>
11 #include <package/hpkg/PackageFileHeapAccessorBase.h>
12 
13 
14 namespace BPrivate {
15 	template<typename Value> class RangeArray;
16 }
17 
18 
19 namespace BPackageKit {
20 
21 namespace BHPKG {
22 
23 
24 class BDataReader;
25 class BErrorOutput;
26 
27 
28 namespace BPrivate {
29 
30 
31 class PackageFileHeapReader;
32 
33 
34 class PackageFileHeapWriter : public PackageFileHeapAccessorBase,
35 	private AbstractDataWriter {
36 public:
37 								PackageFileHeapWriter(BErrorOutput* errorOutput,
38 									int fd, off_t heapOffset,
39 									int32 compressionLevel);
40 								~PackageFileHeapWriter();
41 
42 			void				Init();
43 			void				Reinit(PackageFileHeapReader* heapReader);
44 
45 			AbstractDataWriter* DataWriter()
46 									{ return this; }
47 
48 			status_t			AddData(BDataReader& dataReader, off_t size,
49 									uint64& _offset);
50 			void				RemoveDataRanges(
51 									const ::BPrivate::RangeArray<uint64>&
52 										ranges);
53 									// doesn't truncate the file
54 			status_t			Finish();
55 
56 protected:
57 	virtual	status_t			ReadAndDecompressChunk(size_t chunkIndex,
58 									void* compressedDataBuffer,
59 									void* uncompressedDataBuffer);
60 
61 private:
62 	// AbstractDataWriter
63 	virtual	status_t			WriteDataNoThrow(const void* buffer,
64 									size_t size);
65 
66 private:
67 			struct Chunk;
68 			struct ChunkSegment;
69 			struct ChunkBuffer;
70 
71 			friend struct ChunkBuffer;
72 
73 private:
74 			void				_Uninit();
75 
76 			status_t			_FlushPendingData();
77 			status_t			_WriteChunk(const void* data, size_t size,
78 									bool mayCompress);
79 			status_t			_WriteDataCompressed(const void* data,
80 									size_t size);
81 			status_t			_WriteDataUncompressed(const void* data,
82 									size_t size);
83 
84 			void				_PushChunks(ChunkBuffer& chunkBuffer,
85 									uint64 startOffset, uint64 endOffset);
86 			void				_UnwriteLastPartialChunk();
87 
88 private:
89 			void*				fPendingDataBuffer;
90 			void*				fCompressedDataBuffer;
91 			size_t				fPendingDataSize;
92 			Array<uint64>		fOffsets;
93 			int32				fCompressionLevel;
94 };
95 
96 
97 }	// namespace BPrivate
98 
99 }	// namespace BHPKG
100 
101 }	// namespace BPackageKit
102 
103 
104 #endif	// _PACKAGE__HPKG__PRIVATE__PACKAGE_FILE_HEAP_WRITER_H_
105