input/Plugin: migrate open() from class Error to C++ exceptions

This commit is contained in:
Max Kellermann
2016-09-09 15:37:06 +02:00
parent 63ab7767a3
commit fc7d3f64c0
44 changed files with 359 additions and 461 deletions

View File

@@ -36,6 +36,8 @@
#include <bzlib.h>
#include <stdexcept>
#include <stddef.h>
class Bzip2ArchiveFile final : public ArchiveFile {
@@ -74,9 +76,8 @@ public:
visitor.VisitArchiveEntry(name.c_str());
}
virtual InputStream *OpenStream(const char *path,
Mutex &mutex, Cond &cond,
Error &error) override;
InputStream *OpenStream(const char *path,
Mutex &mutex, Cond &cond) override;
};
class Bzip2InputStream final : public InputStream {
@@ -93,13 +94,12 @@ public:
Mutex &mutex, Cond &cond);
~Bzip2InputStream();
bool Open(Error &error);
/* virtual methods from InputStream */
bool IsEOF() override;
size_t Read(void *ptr, size_t size, Error &error) override;
private:
void Open();
bool FillBuffer(Error &error);
};
@@ -107,8 +107,8 @@ static constexpr Domain bz2_domain("bz2");
/* single archive handling allocation helpers */
inline bool
Bzip2InputStream::Open(Error &error)
inline void
Bzip2InputStream::Open()
{
bzstream.bzalloc = nullptr;
bzstream.bzfree = nullptr;
@@ -118,27 +118,20 @@ Bzip2InputStream::Open(Error &error)
bzstream.avail_in = 0;
int ret = BZ2_bzDecompressInit(&bzstream, 0, 0);
if (ret != BZ_OK) {
error.Set(bz2_domain, ret,
"BZ2_bzDecompressInit() has failed");
return false;
}
if (ret != BZ_OK)
throw std::runtime_error("BZ2_bzDecompressInit() has failed");
SetReady();
return true;
}
/* archive open && listing routine */
static ArchiveFile *
bz2_open(Path pathname, Error &error)
bz2_open(Path pathname, gcc_unused Error &error)
{
static Mutex mutex;
static Cond cond;
auto is = OpenLocalInputStream(pathname, mutex, cond, error);
if (is == nullptr)
return nullptr;
auto is = OpenLocalInputStream(pathname, mutex, cond);
return new Bzip2ArchiveFile(pathname, std::move(is));
}
@@ -150,6 +143,7 @@ Bzip2InputStream::Bzip2InputStream(Bzip2ArchiveFile &_context,
:InputStream(_uri, _mutex, _cond),
archive(&_context)
{
Open();
archive->Ref();
}
@@ -161,16 +155,9 @@ Bzip2InputStream::~Bzip2InputStream()
InputStream *
Bzip2ArchiveFile::OpenStream(const char *path,
Mutex &mutex, Cond &cond,
Error &error)
Mutex &mutex, Cond &cond)
{
Bzip2InputStream *bis = new Bzip2InputStream(*this, path, mutex, cond);
if (!bis->Open(error)) {
delete bis;
return nullptr;
}
return bis;
return new Bzip2InputStream(*this, path, mutex, cond);
}
inline bool

View File

@@ -29,6 +29,7 @@
#include "input/InputStream.hxx"
#include "fs/Path.hxx"
#include "util/RefCount.hxx"
#include "util/RuntimeError.hxx"
#include "util/Error.hxx"
#include "util/Domain.hxx"
@@ -77,9 +78,8 @@ public:
virtual void Visit(ArchiveVisitor &visitor) override;
virtual InputStream *OpenStream(const char *path,
Mutex &mutex, Cond &cond,
Error &error) override;
InputStream *OpenStream(const char *path,
Mutex &mutex, Cond &cond) override;
};
static constexpr Domain iso9660_domain("iso9660");
@@ -175,15 +175,12 @@ public:
InputStream *
Iso9660ArchiveFile::OpenStream(const char *pathname,
Mutex &mutex, Cond &cond,
Error &error)
Mutex &mutex, Cond &cond)
{
auto statbuf = iso9660_ifs_stat_translate(iso, pathname);
if (statbuf == nullptr) {
error.Format(iso9660_domain,
"not found in the ISO file: %s", pathname);
return nullptr;
}
if (statbuf == nullptr)
throw FormatRuntimeError("not found in the ISO file: %s",
pathname);
return new Iso9660InputStream(*this, pathname, mutex, cond,
statbuf);

View File

@@ -29,6 +29,7 @@
#include "input/InputStream.hxx"
#include "fs/Path.hxx"
#include "util/RefCount.hxx"
#include "util/RuntimeError.hxx"
#include "util/Error.hxx"
#include "util/Domain.hxx"
@@ -58,9 +59,8 @@ public:
virtual void Visit(ArchiveVisitor &visitor) override;
virtual InputStream *OpenStream(const char *path,
Mutex &mutex, Cond &cond,
Error &error) override;
InputStream *OpenStream(const char *path,
Mutex &mutex, Cond &cond) override;
};
static constexpr Domain zzip_domain("zzip");
@@ -129,15 +129,12 @@ struct ZzipInputStream final : public InputStream {
InputStream *
ZzipArchiveFile::OpenStream(const char *pathname,
Mutex &mutex, Cond &cond,
Error &error)
Mutex &mutex, Cond &cond)
{
ZZIP_FILE *_file = zzip_file_open(dir, pathname, 0);
if (_file == nullptr) {
error.Format(zzip_domain, "not found in the ZIP file: %s",
pathname);
return nullptr;
}
if (_file == nullptr)
throw FormatRuntimeError("not found in the ZIP file: %s",
pathname);
return new ZzipInputStream(*this, pathname,
mutex, cond,