From dd0b33cb6330ac9c9e8430d2a8f94396474c8128 Mon Sep 17 00:00:00 2001 From: Ingo Weinhold Date: Mon, 13 May 2013 02:40:24 +0200 Subject: [PATCH] BPackageInfo: Support reading both HPKG format versions --- headers/os/package/PackageInfo.h | 4 ++ src/kits/package/PackageInfo.cpp | 87 ++++++++++++++++++++++++++------ 2 files changed, 75 insertions(+), 16 deletions(-) diff --git a/headers/os/package/PackageInfo.h b/headers/os/package/PackageInfo.h index 5b0260c6cb..9833a9047d 100644 --- a/headers/os/package/PackageInfo.h +++ b/headers/os/package/PackageInfo.h @@ -167,12 +167,16 @@ private: friend class Parser; struct StringBuilder; struct FieldName; + struct PackageFileLocation; typedef BObjectList ResolvableList; typedef BObjectList ResolvableExpressionList; private: + status_t _ReadFromPackageFile( + const PackageFileLocation& fileLocation); + static status_t _AddVersion(BMessage* archive, const char* field, const BPackageVersion& version); diff --git a/src/kits/package/PackageInfo.cpp b/src/kits/package/PackageInfo.cpp index 7175b684a4..e9075b1c2c 100644 --- a/src/kits/package/PackageInfo.cpp +++ b/src/kits/package/PackageInfo.cpp @@ -18,6 +18,8 @@ #include #include #include +#include +#include #include @@ -1267,6 +1269,40 @@ private: }; +// #pragma mark - PackageFileLocation + + +struct BPackageInfo::PackageFileLocation { + PackageFileLocation(const char* path) + : + fPath(path), + fFD(-1) + { + } + + PackageFileLocation(int fd) + : + fPath(NULL), + fFD(fd) + { + } + + const char* Path() const + { + return fPath; + } + + int FD() const + { + return fFD; + } + +private: + const char* fPath; + int fFD; +}; + + // #pragma mark - BPackageInfo @@ -1409,28 +1445,14 @@ BPackageInfo::ReadFromConfigString(const BString& packageInfoString, status_t BPackageInfo::ReadFromPackageFile(const char* path) { - BHPKG::BNoErrorOutput errorOutput; - BHPKG::BPackageReader packageReader(&errorOutput); - status_t error = packageReader.Init(path); - if (error != B_OK) - return error; - - BPackageInfoContentHandler handler(*this); - return packageReader.ParseContent(&handler); + return _ReadFromPackageFile(PackageFileLocation(path)); } status_t BPackageInfo::ReadFromPackageFile(int fd) { - BHPKG::BNoErrorOutput errorOutput; - BHPKG::BPackageReader packageReader(&errorOutput); - status_t error = packageReader.Init(fd, false); - if (error != B_OK) - return error; - - BPackageInfoContentHandler handler(*this); - return packageReader.ParseContent(&handler); + return _ReadFromPackageFile(PackageFileLocation(fd)); } @@ -1971,6 +1993,39 @@ BPackageInfo::ParseVersionString(const BString& string, bool revisionIsOptional, } +status_t +BPackageInfo::_ReadFromPackageFile(const PackageFileLocation& fileLocation) +{ + BHPKG::BNoErrorOutput errorOutput; + + // try current package file format version + { + BHPKG::BPackageReader packageReader(&errorOutput); + status_t error = fileLocation.Path() != NULL + ? packageReader.Init(fileLocation.Path()) + : packageReader.Init(fileLocation.FD(), false); + if (error == B_OK) { + BPackageInfoContentHandler handler(*this); + return packageReader.ParseContent(&handler); + } + + if (error != B_MISMATCHED_VALUES) + return error; + } + + // try package file format version 1 + BHPKG::V1::BPackageReader packageReader(&errorOutput); + status_t error = fileLocation.Path() != NULL + ? packageReader.Init(fileLocation.Path()) + : packageReader.Init(fileLocation.FD(), false); + if (error != B_OK) + return error; + + BHPKG::V1::BPackageInfoContentHandler handler(*this); + return packageReader.ParseContent(&handler); +} + + /*static*/ status_t BPackageInfo::_AddVersion(BMessage* archive, const char* field, const BPackageVersion& version)