fs/io/GzipOutputStream: use C++ exceptions in constructor
This commit is contained in:
parent
7eae3bc8c5
commit
36d6ead65c
|
@ -605,6 +605,7 @@ libfs_a_CPPFLAGS = $(AM_CPPFLAGS) $(ZLIB_CFLAGS)
|
||||||
if ENABLE_ZLIB
|
if ENABLE_ZLIB
|
||||||
libfs_a_SOURCES += \
|
libfs_a_SOURCES += \
|
||||||
src/lib/zlib/Domain.cxx src/lib/zlib/Domain.hxx \
|
src/lib/zlib/Domain.cxx src/lib/zlib/Domain.hxx \
|
||||||
|
src/lib/zlib/Error.cxx src/lib/zlib/Error.hxx \
|
||||||
src/fs/io/GunzipReader.cxx src/fs/io/GunzipReader.hxx \
|
src/fs/io/GunzipReader.cxx src/fs/io/GunzipReader.hxx \
|
||||||
src/fs/io/AutoGunzipReader.cxx src/fs/io/AutoGunzipReader.hxx \
|
src/fs/io/AutoGunzipReader.cxx src/fs/io/AutoGunzipReader.hxx \
|
||||||
src/fs/io/GzipOutputStream.cxx src/fs/io/GzipOutputStream.hxx
|
src/fs/io/GzipOutputStream.cxx src/fs/io/GzipOutputStream.hxx
|
||||||
|
@ -1759,7 +1760,8 @@ noinst_PROGRAMS += test/run_gzip test/run_gunzip
|
||||||
test_run_gzip_LDADD = \
|
test_run_gzip_LDADD = \
|
||||||
libutil.a \
|
libutil.a \
|
||||||
$(FS_LIBS)
|
$(FS_LIBS)
|
||||||
test_run_gzip_SOURCES = test/run_gzip.cxx
|
test_run_gzip_SOURCES = test/run_gzip.cxx \
|
||||||
|
src/Log.cxx src/LogBackend.cxx
|
||||||
|
|
||||||
test_run_gunzip_SOURCES = test/run_gunzip.cxx \
|
test_run_gunzip_SOURCES = test/run_gunzip.cxx \
|
||||||
src/Log.cxx src/LogBackend.cxx
|
src/Log.cxx src/LogBackend.cxx
|
||||||
|
|
|
@ -386,10 +386,7 @@ SimpleDatabase::Save(Error &error)
|
||||||
#ifdef ENABLE_ZLIB
|
#ifdef ENABLE_ZLIB
|
||||||
std::unique_ptr<GzipOutputStream> gzip;
|
std::unique_ptr<GzipOutputStream> gzip;
|
||||||
if (compress) {
|
if (compress) {
|
||||||
gzip.reset(new GzipOutputStream(*os, error));
|
gzip.reset(new GzipOutputStream(*os));
|
||||||
if (!gzip->IsDefined())
|
|
||||||
return false;
|
|
||||||
|
|
||||||
os = gzip.get();
|
os = gzip.get();
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -20,10 +20,10 @@
|
||||||
#include "config.h"
|
#include "config.h"
|
||||||
#include "GzipOutputStream.hxx"
|
#include "GzipOutputStream.hxx"
|
||||||
#include "lib/zlib/Domain.hxx"
|
#include "lib/zlib/Domain.hxx"
|
||||||
|
#include "lib/zlib/Error.hxx"
|
||||||
#include "util/Error.hxx"
|
#include "util/Error.hxx"
|
||||||
#include "util/Domain.hxx"
|
|
||||||
|
|
||||||
GzipOutputStream::GzipOutputStream(OutputStream &_next, Error &error)
|
GzipOutputStream::GzipOutputStream(OutputStream &_next) throw(ZlibError)
|
||||||
:next(_next)
|
:next(_next)
|
||||||
{
|
{
|
||||||
z.next_in = nullptr;
|
z.next_in = nullptr;
|
||||||
|
@ -38,15 +38,12 @@ GzipOutputStream::GzipOutputStream(OutputStream &_next, Error &error)
|
||||||
int result = deflateInit2(&z, Z_DEFAULT_COMPRESSION, Z_DEFLATED,
|
int result = deflateInit2(&z, Z_DEFAULT_COMPRESSION, Z_DEFLATED,
|
||||||
windowBits | gzip_encoding,
|
windowBits | gzip_encoding,
|
||||||
8, Z_DEFAULT_STRATEGY);
|
8, Z_DEFAULT_STRATEGY);
|
||||||
if (result != Z_OK) {
|
if (result != Z_OK)
|
||||||
z.opaque = this;
|
throw ZlibError(result);
|
||||||
error.Set(zlib_domain, result, zError(result));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
GzipOutputStream::~GzipOutputStream()
|
GzipOutputStream::~GzipOutputStream()
|
||||||
{
|
{
|
||||||
if (IsDefined())
|
|
||||||
deflateEnd(&z);
|
deflateEnd(&z);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -22,13 +22,13 @@
|
||||||
|
|
||||||
#include "check.h"
|
#include "check.h"
|
||||||
#include "OutputStream.hxx"
|
#include "OutputStream.hxx"
|
||||||
|
#include "lib/zlib/Error.hxx"
|
||||||
#include "Compiler.h"
|
#include "Compiler.h"
|
||||||
|
|
||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
#include <zlib.h>
|
#include <zlib.h>
|
||||||
|
|
||||||
class Error;
|
class Error;
|
||||||
class Domain;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A filter that compresses data written to it using zlib, forwarding
|
* A filter that compresses data written to it using zlib, forwarding
|
||||||
|
@ -43,20 +43,11 @@ class GzipOutputStream final : public OutputStream {
|
||||||
|
|
||||||
public:
|
public:
|
||||||
/**
|
/**
|
||||||
* Construct the filter. Call IsDefined() to check whether
|
* Construct the filter.
|
||||||
* the constructor has succeeded. If not, #error will hold
|
|
||||||
* information about the failure.
|
|
||||||
*/
|
*/
|
||||||
GzipOutputStream(OutputStream &_next, Error &error);
|
GzipOutputStream(OutputStream &_next) throw(ZlibError);
|
||||||
~GzipOutputStream();
|
~GzipOutputStream();
|
||||||
|
|
||||||
/**
|
|
||||||
* Check whether the constructor has succeeded.
|
|
||||||
*/
|
|
||||||
bool IsDefined() const {
|
|
||||||
return z.opaque == nullptr;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Finish the file and write all data remaining in zlib's
|
* Finish the file and write all data remaining in zlib's
|
||||||
* output buffer.
|
* output buffer.
|
||||||
|
|
|
@ -0,0 +1,29 @@
|
||||||
|
/*
|
||||||
|
* Copyright (C) 2003-2015 The Music Player Daemon Project
|
||||||
|
* http://www.musicpd.org
|
||||||
|
*
|
||||||
|
* This program is free software; you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU General Public License as published by
|
||||||
|
* the Free Software Foundation; either version 2 of the License, or
|
||||||
|
* (at your option) any later version.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License along
|
||||||
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
||||||
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "config.h"
|
||||||
|
#include "Error.hxx"
|
||||||
|
|
||||||
|
#include <zlib.h>
|
||||||
|
|
||||||
|
const char *
|
||||||
|
ZlibError::what() const noexcept
|
||||||
|
{
|
||||||
|
return zError(code);
|
||||||
|
}
|
|
@ -0,0 +1,38 @@
|
||||||
|
/*
|
||||||
|
* Copyright (C) 2003-2015 The Music Player Daemon Project
|
||||||
|
* http://www.musicpd.org
|
||||||
|
*
|
||||||
|
* This program is free software; you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU General Public License as published by
|
||||||
|
* the Free Software Foundation; either version 2 of the License, or
|
||||||
|
* (at your option) any later version.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License along
|
||||||
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
||||||
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef MPD_ZLIB_ERROR_HXX
|
||||||
|
#define MPD_ZLIB_ERROR_HXX
|
||||||
|
|
||||||
|
#include <stdexcept>
|
||||||
|
|
||||||
|
class ZlibError final : public std::exception {
|
||||||
|
int code;
|
||||||
|
|
||||||
|
public:
|
||||||
|
explicit ZlibError(int _code):code(_code) {}
|
||||||
|
|
||||||
|
int GetCode() const {
|
||||||
|
return code;
|
||||||
|
}
|
||||||
|
|
||||||
|
const char *what() const noexcept override;
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
|
@ -20,6 +20,7 @@
|
||||||
#include "config.h"
|
#include "config.h"
|
||||||
#include "fs/io/GzipOutputStream.hxx"
|
#include "fs/io/GzipOutputStream.hxx"
|
||||||
#include "fs/io/StdioOutputStream.hxx"
|
#include "fs/io/StdioOutputStream.hxx"
|
||||||
|
#include "Log.hxx"
|
||||||
#include "util/Error.hxx"
|
#include "util/Error.hxx"
|
||||||
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
@ -48,9 +49,8 @@ Copy(OutputStream &dest, int src, Error &error)
|
||||||
static bool
|
static bool
|
||||||
CopyGzip(OutputStream &_dest, int src, Error &error)
|
CopyGzip(OutputStream &_dest, int src, Error &error)
|
||||||
{
|
{
|
||||||
GzipOutputStream dest(_dest, error);
|
GzipOutputStream dest(_dest);
|
||||||
return dest.IsDefined() &&
|
return Copy(dest, src, error) &&
|
||||||
Copy(dest, src, error) &&
|
|
||||||
dest.Flush(error);
|
dest.Flush(error);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -69,6 +69,7 @@ main(int argc, gcc_unused char **argv)
|
||||||
return EXIT_FAILURE;
|
return EXIT_FAILURE;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
Error error;
|
Error error;
|
||||||
if (!CopyGzip(stdout, STDIN_FILENO, error)) {
|
if (!CopyGzip(stdout, STDIN_FILENO, error)) {
|
||||||
fprintf(stderr, "%s\n", error.GetMessage());
|
fprintf(stderr, "%s\n", error.GetMessage());
|
||||||
|
@ -76,4 +77,8 @@ main(int argc, gcc_unused char **argv)
|
||||||
}
|
}
|
||||||
|
|
||||||
return EXIT_SUCCESS;
|
return EXIT_SUCCESS;
|
||||||
|
} catch (const std::exception &e) {
|
||||||
|
LogError(e);
|
||||||
|
return EXIT_FAILURE;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue