Re: [PATCH v2 13/13] libdpkg: compression: check for output errors closing files
On Sat, 2009-10-24 at 17:49:49 -0500, Jonathan Nieder wrote:
> A gzclose call has the same potential for errors as a write,
> since the compressor needs to flush its buffers before closing
> its output file. The same applies to BZ2_bzclose, but
> unfortunately libbz2's gzio-style API does not expose the error
> code. Luckily, the only possible errors are I/O errors, which
> can be detected through errno.
> Similarly, explicitly closing a file descriptor can reveal errors
> writing out buffered data.
> Closing input handles, on the other hand, would be a waste of
> time: all it would accomplish is to free some resources held by a
> process that is about to exit anyway.
Applied this one with few modifications. Adapt to no COMPRESS and
DECOMPRESS macro usage.
> diff --git a/lib/dpkg/compression-backend.c b/lib/dpkg/compression-backend.c
> index 9bfbcee..101018a 100644
> --- a/lib/dpkg/compression-backend.c
> +++ b/lib/dpkg/compression-backend.c
> @@ -151,6 +164,34 @@ compress_gzip(int fd_in, int fd_out, char compression, const char *desc)
> #ifdef WITH_BZ2
> + * BZ2_bzclose does not pass on the error code to the caller.
> + * To work around this, we could
> + * - set errno to 0 and compare the value afterwards, hoping
> + * that libbz2 did not recover from any errors itself
> + * or
> + * - modify a copy of BZ2_bzclose to expose the error
> + * This function follows the former course, so it can continue
> + * to work even if the layout of BZFILE objects should change.
> + */
> +static int
> +bzclose(BZFILE *b)
> + errno = 0;
> + BZ2_bzclose(b);
> + if (errno)
> + return BZ_IO_ERROR;
> + return 0;
> +static const char * DPKG_ATTR_CONST
This function cannot be const as it's calling gettext (masked as “_()”).
> +bzcloseerror(int err)
> + /* bzclose only returns BZ_OK and BZ_IO_ERROR. */
> + return _("Unexpected error (bug)");
Instead of this I used BZ2_bzWriteClose which returns an error code in
one of the arguments, and the closed the descriptor.