2011-02-08 01:25:16 +03:00
|
|
|
/*
|
|
|
|
* Copyright 2011, Oliver Tappe <zooey@hirschkaefer.de>
|
|
|
|
* Distributed under the terms of the MIT License.
|
|
|
|
*/
|
|
|
|
#ifndef _PACKAGE__HPKG__PRIVATE__WRITER_IMPL_BASE_H_
|
|
|
|
#define _PACKAGE__HPKG__PRIVATE__WRITER_IMPL_BASE_H_
|
|
|
|
|
|
|
|
|
|
|
|
#include <util/DoublyLinkedList.h>
|
|
|
|
|
|
|
|
#include <package/hpkg/haiku_package.h>
|
|
|
|
|
|
|
|
#include <package/hpkg/DataOutput.h>
|
|
|
|
#include <package/hpkg/Strings.h>
|
|
|
|
#include <package/hpkg/ZlibCompressor.h>
|
|
|
|
|
|
|
|
#include <package/PackageInfo.h>
|
|
|
|
|
|
|
|
|
|
|
|
namespace BPackageKit {
|
|
|
|
|
|
|
|
namespace BHPKG {
|
|
|
|
|
|
|
|
|
|
|
|
class BDataReader;
|
|
|
|
class BErrorOutput;
|
|
|
|
|
|
|
|
|
|
|
|
namespace BPrivate {
|
|
|
|
|
|
|
|
|
|
|
|
struct hpkg_header;
|
|
|
|
|
|
|
|
class WriterImplBase {
|
|
|
|
public:
|
|
|
|
WriterImplBase(BErrorOutput* errorOutput);
|
|
|
|
~WriterImplBase();
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
|
|
|
|
|
|
|
struct AttributeValue {
|
|
|
|
union {
|
|
|
|
int64 signedInt;
|
|
|
|
uint64 unsignedInt;
|
|
|
|
CachedString* string;
|
|
|
|
struct {
|
|
|
|
uint64 size;
|
|
|
|
union {
|
|
|
|
uint64 offset;
|
|
|
|
uint8 raw[B_HPKG_MAX_INLINE_DATA_SIZE];
|
|
|
|
};
|
|
|
|
} data;
|
|
|
|
};
|
|
|
|
uint8 type;
|
|
|
|
int8 encoding;
|
|
|
|
|
|
|
|
AttributeValue();
|
|
|
|
~AttributeValue();
|
|
|
|
|
|
|
|
void SetTo(int8 value);
|
|
|
|
void SetTo(uint8 value);
|
|
|
|
void SetTo(int16 value);
|
|
|
|
void SetTo(uint16 value);
|
|
|
|
void SetTo(int32 value);
|
|
|
|
void SetTo(uint32 value);
|
|
|
|
void SetTo(int64 value);
|
|
|
|
void SetTo(uint64 value);
|
|
|
|
void SetTo(CachedString* value);
|
|
|
|
void SetToData(uint64 size, uint64 offset);
|
|
|
|
void SetToData(uint64 size, const void* rawData);
|
|
|
|
uint8 ApplicableEncoding() const;
|
|
|
|
|
|
|
|
private:
|
|
|
|
static uint8 _ApplicableIntEncoding(uint64 value);
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
struct PackageAttribute :
|
|
|
|
public DoublyLinkedListLinkImpl<PackageAttribute>,
|
|
|
|
public AttributeValue {
|
|
|
|
HPKGPackageAttributeID id;
|
|
|
|
DoublyLinkedList<PackageAttribute> children;
|
|
|
|
|
|
|
|
PackageAttribute(HPKGPackageAttributeID id_, uint8 type,
|
|
|
|
uint8 encoding);
|
|
|
|
~PackageAttribute();
|
|
|
|
|
|
|
|
void AddChild(PackageAttribute* child);
|
|
|
|
|
|
|
|
private:
|
|
|
|
void _DeleteChildren();
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2011-02-08 13:59:38 +03:00
|
|
|
struct AbstractDataWriter {
|
|
|
|
AbstractDataWriter();
|
|
|
|
virtual ~AbstractDataWriter();
|
2011-02-08 01:25:16 +03:00
|
|
|
|
|
|
|
uint64 BytesWritten() const;
|
|
|
|
|
|
|
|
virtual status_t WriteDataNoThrow(const void* buffer,
|
|
|
|
size_t size) = 0;
|
|
|
|
|
|
|
|
void WriteDataThrows(const void* buffer, size_t size);
|
|
|
|
|
|
|
|
protected:
|
|
|
|
uint64 fBytesWritten;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2011-02-08 13:59:38 +03:00
|
|
|
struct FDDataWriter : AbstractDataWriter {
|
2011-02-08 01:25:16 +03:00
|
|
|
FDDataWriter(int fd, off_t offset, BErrorOutput* errorOutput);
|
|
|
|
|
|
|
|
virtual status_t WriteDataNoThrow(const void* buffer,
|
|
|
|
size_t size);
|
|
|
|
|
|
|
|
off_t Offset() const;
|
|
|
|
|
|
|
|
private:
|
|
|
|
int fFD;
|
|
|
|
off_t fOffset;
|
|
|
|
BErrorOutput* fErrorOutput;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2011-02-08 13:59:38 +03:00
|
|
|
struct ZlibDataWriter : AbstractDataWriter, private BDataOutput {
|
|
|
|
ZlibDataWriter(AbstractDataWriter* dataWriter);
|
2011-02-08 01:25:16 +03:00
|
|
|
|
|
|
|
void Init();
|
|
|
|
|
|
|
|
void Finish();
|
|
|
|
|
|
|
|
virtual status_t WriteDataNoThrow(const void* buffer,
|
|
|
|
size_t size);
|
|
|
|
|
|
|
|
private:
|
|
|
|
// BDataOutput
|
|
|
|
virtual status_t WriteData(const void* buffer, size_t size);
|
|
|
|
|
|
|
|
private:
|
2011-02-08 13:59:38 +03:00
|
|
|
AbstractDataWriter* fDataWriter;
|
|
|
|
ZlibCompressor fCompressor;
|
2011-02-08 01:25:16 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
typedef DoublyLinkedList<PackageAttribute> PackageAttributeList;
|
|
|
|
|
|
|
|
protected:
|
|
|
|
status_t Init(const char* fileName, const char* type);
|
|
|
|
|
2011-02-08 13:59:38 +03:00
|
|
|
void RegisterPackageInfo(
|
|
|
|
PackageAttributeList& attributeList,
|
|
|
|
const BPackageInfo& packageInfo);
|
|
|
|
void RegisterPackageVersion(
|
|
|
|
PackageAttributeList& attributeList,
|
|
|
|
const BPackageVersion& version);
|
|
|
|
void RegisterPackageResolvableExpressionList(
|
|
|
|
PackageAttributeList& attributeList,
|
|
|
|
const BObjectList<
|
|
|
|
BPackageResolvableExpression>& list,
|
|
|
|
uint8 id);
|
|
|
|
|
2011-02-08 01:25:16 +03:00
|
|
|
int32 WriteCachedStrings(const StringCache& cache,
|
|
|
|
uint32 minUsageCount);
|
|
|
|
|
2011-02-08 13:59:38 +03:00
|
|
|
int32 WritePackageAttributes(
|
|
|
|
const PackageAttributeList& attributes,
|
|
|
|
uint32& _stringsLengthUncompressed);
|
2011-02-08 01:25:16 +03:00
|
|
|
void WritePackageVersion(
|
|
|
|
const BPackageVersion& version);
|
|
|
|
void WritePackageResolvableExpressionList(
|
|
|
|
const BObjectList<
|
|
|
|
BPackageResolvableExpression>& list,
|
|
|
|
uint8 id);
|
|
|
|
|
|
|
|
void WriteAttributeValue(const AttributeValue& value,
|
|
|
|
uint8 encoding);
|
|
|
|
void WriteUnsignedLEB128(uint64 value);
|
|
|
|
inline void WriteString(const char* string);
|
|
|
|
|
|
|
|
template<typename Type>
|
|
|
|
inline void Write(const Type& value);
|
|
|
|
|
|
|
|
void WriteBuffer(const void* buffer, size_t size,
|
|
|
|
off_t offset);
|
|
|
|
|
2011-02-08 13:59:38 +03:00
|
|
|
int FD() const;
|
|
|
|
AbstractDataWriter* DataWriter() const;
|
|
|
|
const PackageAttributeList& PackageAttributes() const;
|
|
|
|
PackageAttributeList& PackageAttributes();
|
|
|
|
|
|
|
|
void SetDataWriter(AbstractDataWriter* dataWriter);
|
|
|
|
void SetFinished(bool finished);
|
|
|
|
|
|
|
|
private:
|
|
|
|
void _WritePackageAttributes(
|
|
|
|
const PackageAttributeList& attributes);
|
|
|
|
|
|
|
|
private:
|
2011-02-08 01:25:16 +03:00
|
|
|
BErrorOutput* fErrorOutput;
|
|
|
|
const char* fFileName;
|
|
|
|
int fFD;
|
|
|
|
bool fFinished;
|
|
|
|
|
2011-02-08 13:59:38 +03:00
|
|
|
AbstractDataWriter* fDataWriter;
|
2011-02-08 01:25:16 +03:00
|
|
|
|
|
|
|
StringCache fPackageStringCache;
|
2011-02-08 13:59:38 +03:00
|
|
|
PackageAttributeList fPackageAttributes;
|
2011-02-08 01:25:16 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
inline uint64
|
2011-02-08 13:59:38 +03:00
|
|
|
WriterImplBase::AbstractDataWriter::BytesWritten() const
|
2011-02-08 01:25:16 +03:00
|
|
|
{
|
|
|
|
return fBytesWritten;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
inline void
|
2011-02-08 18:19:26 +03:00
|
|
|
WriterImplBase::AbstractDataWriter::WriteDataThrows(const void* buffer,
|
|
|
|
size_t size)
|
2011-02-08 01:25:16 +03:00
|
|
|
{
|
|
|
|
status_t error = WriteDataNoThrow(buffer, size);
|
|
|
|
if (error != B_OK)
|
|
|
|
throw status_t(error);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline off_t
|
|
|
|
WriterImplBase::FDDataWriter::Offset() const
|
|
|
|
{
|
|
|
|
return fOffset;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template<typename Type>
|
|
|
|
inline void
|
|
|
|
WriterImplBase::Write(const Type& value)
|
|
|
|
{
|
|
|
|
fDataWriter->WriteDataThrows(&value, sizeof(Type));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
inline void
|
|
|
|
WriterImplBase::WriteString(const char* string)
|
|
|
|
{
|
|
|
|
fDataWriter->WriteDataThrows(string, strlen(string) + 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-02-08 13:59:38 +03:00
|
|
|
inline int
|
|
|
|
WriterImplBase::FD() const
|
|
|
|
{
|
|
|
|
return fFD;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
inline WriterImplBase::AbstractDataWriter*
|
|
|
|
WriterImplBase::DataWriter() const
|
|
|
|
{
|
|
|
|
return fDataWriter;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
inline void
|
|
|
|
WriterImplBase::SetDataWriter(AbstractDataWriter* dataWriter)
|
|
|
|
{
|
|
|
|
fDataWriter = dataWriter;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
inline const WriterImplBase::PackageAttributeList&
|
|
|
|
WriterImplBase::PackageAttributes() const
|
|
|
|
{
|
|
|
|
return fPackageAttributes;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
inline WriterImplBase::PackageAttributeList&
|
|
|
|
WriterImplBase::PackageAttributes()
|
|
|
|
{
|
|
|
|
return fPackageAttributes;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
inline void
|
|
|
|
WriterImplBase::SetFinished(bool finished)
|
|
|
|
{
|
|
|
|
fFinished = finished;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-02-08 01:25:16 +03:00
|
|
|
} // namespace BPrivate
|
|
|
|
|
|
|
|
} // namespace BHPKG
|
|
|
|
|
|
|
|
} // namespace BPackageKit
|
|
|
|
|
|
|
|
|
|
|
|
#endif // _PACKAGE__HPKG__PRIVATE__WRITER_IMPL_BASE_H_
|