Switch package file accessor classes to use BPositionIO

* PackageFileHeap{Reader,Writer} as well as Package{Reader,Writer} and
  their implementation and super classes do now internally use a
  BPositionIO instead of a FD to access the package file. This provides
  more flexibility needed for features to come.
* BPackageReader has already grown a new Init() version with a
  BPositionIO* parameter.
This commit is contained in:
Ingo Weinhold
2014-07-12 15:40:22 +02:00
parent 01e6d687c0
commit e527b79631
21 changed files with 205 additions and 141 deletions
+6 -1
View File
@@ -9,6 +9,9 @@
#include <SupportDefs.h>
class BPositionIO;
namespace BPackageKit {
namespace BHPKG {
@@ -34,12 +37,14 @@ public:
status_t Init(const char* fileName, uint32 flags = 0);
status_t Init(int fd, bool keepFD, uint32 flags = 0);
status_t Init(BPositionIO* file, bool keepFile,
uint32 flags = 0);
status_t ParseContent(
BPackageContentHandler* contentHandler);
status_t ParseContent(BLowLevelPackageContentHandler*
contentHandler);
int PackageFileFD();
BPositionIO* PackageFile() const;
BAbstractBufferedDataReader* HeapReader() const;
// Only valid as long as the reader lives.
@@ -71,8 +71,8 @@ public:
public:
PackageFileHeapAccessorBase(
BErrorOutput* errorOutput, int fd,
off_t heapOffset,
BErrorOutput* errorOutput,
BPositionIO* file, off_t heapOffset,
DecompressionAlgorithmOwner*
decompressionAlgorithm);
virtual ~PackageFileHeapAccessorBase();
@@ -89,8 +89,8 @@ public:
// normally used after cloning a PackageFileHeapReader only
void SetErrorOutput(BErrorOutput* errorOutput)
{ fErrorOutput = errorOutput; }
void SetFD(int fd)
{ fFD = fd; }
void SetFile(BPositionIO* file)
{ fFile = file; }
uint64 HeapOverhead(uint64 uncompressedSize) const;
// additional bytes needed when storing
@@ -122,7 +122,7 @@ protected:
protected:
BErrorOutput* fErrorOutput;
int fFD;
BPositionIO* fFile;
off_t fHeapOffset;
uint64 fCompressedHeapSize;
uint64 fUncompressedHeapSize;
@@ -25,7 +25,7 @@ namespace BPrivate {
class PackageFileHeapReader : public PackageFileHeapAccessorBase {
public:
PackageFileHeapReader(BErrorOutput* errorOutput,
int fd, off_t heapOffset,
BPositionIO* file, off_t heapOffset,
off_t compressedHeapSize,
uint64 uncompressedHeapSize,
DecompressionAlgorithmOwner*
@@ -35,7 +35,7 @@ class PackageFileHeapReader;
class PackageFileHeapWriter : public PackageFileHeapAccessorBase {
public:
PackageFileHeapWriter(BErrorOutput* errorOutput,
int fd, off_t heapOffset,
BPositionIO* file, off_t heapOffset,
CompressionAlgorithmOwner*
compressionAlgorithm,
DecompressionAlgorithmOwner*
@@ -33,12 +33,14 @@ public:
status_t Init(const char* fileName, uint32 flags);
status_t Init(int fd, bool keepFD, uint32 flags);
status_t Init(BPositionIO* file, bool keepFile,
uint32 flags);
status_t ParseContent(
BPackageContentHandler* contentHandler);
status_t ParseContent(BLowLevelPackageContentHandler*
contentHandler);
int PackageFileFD() const;
BPositionIO* PackageFile() const;
uint64 HeapOffset() const;
uint64 HeapSize() const;
@@ -77,10 +79,10 @@ private:
};
inline int
PackageReaderImpl::PackageFileFD() const
inline BPositionIO*
PackageReaderImpl::PackageFile() const
{
return FD();
return File();
}
+27 -26
View File
@@ -1,5 +1,5 @@
/*
* Copyright 2009-2013, Ingo Weinhold, [email protected].
* Copyright 2009-2014, Ingo Weinhold, [email protected].
* Copyright 2011, Oliver Tappe <[email protected]>
* Distributed under the terms of the MIT License.
*/
@@ -11,7 +11,7 @@
#include <sys/stat.h>
#include <ByteOrder.h>
#include <SupportDefs.h>
#include <DataIO.h>
#include <Array.h>
#include <util/SinglyLinkedList.h>
@@ -77,7 +77,7 @@ protected:
BErrorOutput* errorOutput);
virtual ~ReaderImplBase();
int FD() const;
BPositionIO* File() const;
BErrorOutput* ErrorOutput() const;
@@ -93,14 +93,14 @@ protected:
// equals RawHeapReader(), if uncached
BAbstractBufferedDataReader* DetachHeapReader(
PackageFileHeapReader** _rawHeapReader
= NULL);
PackageFileHeapReader*& _rawHeapReader);
// Detaches both raw and (if applicable)
// cached heap reader. The called gains
// ownership. The FD may need to be set on
// the raw heap reader, if it shall be used
// after destroying this object and Init()
// has been called with keepFD == true.
// ownership of both. The file may need to
// be set on the raw heap reader, if it
// shall be used after destroying this
// object and Init() has been called with
// keepFile == true.
protected:
class AttributeHandlerContext;
@@ -122,8 +122,8 @@ protected:
protected:
template<typename Header, uint32 kMagic, uint16 kVersion,
uint16 kMinorVersion>
status_t Init(int fd, bool keepFD, Header& header,
uint32 flags);
status_t Init(BPositionIO* file, bool keepFile,
Header& header, uint32 flags);
status_t InitHeapReader(uint32 compression,
uint32 chunkSize, off_t offset,
uint64 compressedSize,
@@ -169,7 +169,7 @@ protected:
PackageFileSection fPackageAttributesSection;
private:
status_t _Init(int fd, bool keepFD);
status_t _Init(BPositionIO* file, bool keepFile);
status_t _ParseAttributeTree(
AttributeHandlerContext* context);
@@ -190,8 +190,8 @@ private:
private:
const char* fFileType;
BErrorOutput* fErrorOutput;
int fFD;
bool fOwnsFD;
BPositionIO* fFile;
bool fOwnsFile;
uint16 fMinorFormatVersion;
uint16 fCurrentMinorFormatVersion;
@@ -429,17 +429,18 @@ private:
template<typename Header, uint32 kMagic, uint16 kVersion, uint16 kMinorVersion>
status_t
ReaderImplBase::Init(int fd, bool keepFD, Header& header, uint32 flags)
ReaderImplBase::Init(BPositionIO* file, bool keepFile, Header& header, uint32 flags)
{
status_t error = _Init(fd, keepFD);
status_t error = _Init(file, keepFile);
if (error != B_OK)
return error;
// stat the file
struct stat st;
if (fstat(FD(), &st) < 0) {
ErrorOutput()->PrintError("Error: Failed to access %s file: %s\n",
fFileType, strerror(errno));
// get the file size
off_t fileSize;
error = fFile->GetSize(&fileSize);
if (error != B_OK) {
ErrorOutput()->PrintError("Error: Failed to get size of %s file: %s\n",
fFileType, strerror(error));
return errno;
}
@@ -479,10 +480,10 @@ ReaderImplBase::Init(int fd, bool keepFD, Header& header, uint32 flags)
// total size
uint64 totalSize = B_BENDIAN_TO_HOST_INT64(header.total_size);
if (totalSize != (uint64)st.st_size) {
if (totalSize != (uint64)fileSize) {
ErrorOutput()->PrintError("Error: Invalid %s file: Total size in "
"header (%" B_PRIu64 ") doesn't agree with total file size (%"
B_PRIdOFF ")\n", fFileType, totalSize, st.st_size);
B_PRIdOFF ")\n", fFileType, totalSize, fileSize);
return B_BAD_DATA;
}
@@ -510,10 +511,10 @@ ReaderImplBase::Init(int fd, bool keepFD, Header& header, uint32 flags)
}
inline int
ReaderImplBase::FD() const
inline BPositionIO*
ReaderImplBase::File() const
{
return fFD;
return fFile;
}
@@ -30,6 +30,7 @@ public:
status_t Init(const char* fileName);
status_t Init(int fd, bool keepFD);
status_t Init(BPositionIO* file, bool keepFile);
status_t GetRepositoryInfo(
BRepositoryInfo* _repositoryInfo) const;
@@ -149,7 +149,7 @@ protected:
off_t offset);
// writes to the file directly
inline int FD() const;
inline BPositionIO* File() const;
inline uint32 Flags() const;
inline const BPackageWriterParameters& Parameters() const;
@@ -187,7 +187,7 @@ private:
BErrorOutput* fErrorOutput;
const char* fFileName;
BPackageWriterParameters fParameters;
int fFD;
BPositionIO* fFile;
bool fFinished;
StringCache fPackageStringCache;
@@ -217,10 +217,10 @@ WriterImplBase::WriteBuffer(const void* data, size_t size)
}
inline int
WriterImplBase::FD() const
inline BPositionIO*
WriterImplBase::File() const
{
return fFD;
return fFile;
}