2010-12-02 20:39:03 +01:00
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
*
|
|
|
|
* compress_io.c
|
2011-04-10 17:42:00 +02:00
|
|
|
* Routines for archivers to write an uncompressed or compressed data
|
|
|
|
* stream.
|
2010-12-02 20:39:03 +01:00
|
|
|
*
|
2019-01-02 18:44:25 +01:00
|
|
|
* Portions Copyright (c) 1996-2019, PostgreSQL Global Development Group
|
2010-12-02 20:39:03 +01:00
|
|
|
* Portions Copyright (c) 1994, Regents of the University of California
|
|
|
|
*
|
2011-01-23 22:10:15 +01:00
|
|
|
* This file includes two APIs for dealing with compressed data. The first
|
|
|
|
* provides more flexibility, using callbacks to read/write data from the
|
|
|
|
* underlying stream. The second API is a wrapper around fopen/gzopen and
|
|
|
|
* friends, providing an interface similar to those, but abstracts away
|
|
|
|
* the possible compression. Both APIs use libz for the compression, but
|
|
|
|
* the second API uses gzip headers, so the resulting files can be easily
|
|
|
|
* manipulated with the gzip utility.
|
|
|
|
*
|
|
|
|
* Compressor API
|
|
|
|
* --------------
|
|
|
|
*
|
2011-04-10 17:42:00 +02:00
|
|
|
* The interface for writing to an archive consists of three functions:
|
|
|
|
* AllocateCompressor, WriteDataToArchive and EndCompressor. First you call
|
|
|
|
* AllocateCompressor, then write all the data by calling WriteDataToArchive
|
|
|
|
* as many times as needed, and finally EndCompressor. WriteDataToArchive
|
|
|
|
* and EndCompressor will call the WriteFunc that was provided to
|
|
|
|
* AllocateCompressor for each chunk of compressed data.
|
2010-12-02 20:39:03 +01:00
|
|
|
*
|
2011-04-10 17:42:00 +02:00
|
|
|
* The interface for reading an archive consists of just one function:
|
|
|
|
* ReadDataFromArchive. ReadDataFromArchive reads the whole compressed input
|
|
|
|
* stream, by repeatedly calling the given ReadFunc. ReadFunc returns the
|
|
|
|
* compressed data chunk at a time, and ReadDataFromArchive decompresses it
|
|
|
|
* and passes the decompressed data to ahwrite(), until ReadFunc returns 0
|
|
|
|
* to signal EOF.
|
2010-12-02 20:39:03 +01:00
|
|
|
*
|
2011-04-10 17:42:00 +02:00
|
|
|
* The interface is the same for compressed and uncompressed streams.
|
2010-12-02 20:39:03 +01:00
|
|
|
*
|
2011-01-23 22:10:15 +01:00
|
|
|
* Compressed stream API
|
|
|
|
* ----------------------
|
|
|
|
*
|
2011-04-10 17:42:00 +02:00
|
|
|
* The compressed stream API is a wrapper around the C standard fopen() and
|
|
|
|
* libz's gzopen() APIs. It allows you to use the same functions for
|
|
|
|
* compressed and uncompressed streams. cfopen_read() first tries to open
|
|
|
|
* the file with given name, and if it fails, it tries to open the same
|
|
|
|
* file with the .gz suffix. cfopen_write() opens a file for writing, an
|
|
|
|
* extra argument specifies if the file should be compressed, and adds the
|
|
|
|
* .gz suffix to the filename if so. This allows you to easily handle both
|
|
|
|
* compressed and uncompressed files.
|
2010-12-02 20:39:03 +01:00
|
|
|
*
|
|
|
|
* IDENTIFICATION
|
2011-04-10 17:42:00 +02:00
|
|
|
* src/bin/pg_dump/compress_io.c
|
2010-12-02 20:39:03 +01:00
|
|
|
*
|
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
*/
|
2014-10-14 20:00:55 +02:00
|
|
|
#include "postgres_fe.h"
|
2010-12-02 20:39:03 +01:00
|
|
|
|
|
|
|
#include "compress_io.h"
|
2014-10-14 20:00:55 +02:00
|
|
|
#include "pg_backup_utils.h"
|
2010-12-02 20:39:03 +01:00
|
|
|
|
2011-01-23 22:10:15 +01:00
|
|
|
/*----------------------
|
|
|
|
* Compressor API
|
|
|
|
*----------------------
|
|
|
|
*/
|
2010-12-02 21:38:49 +01:00
|
|
|
|
|
|
|
/* typedef appears in compress_io.h */
|
|
|
|
struct CompressorState
|
|
|
|
{
|
|
|
|
CompressionAlgorithm comprAlg;
|
2011-04-10 17:42:00 +02:00
|
|
|
WriteFunc writeF;
|
2010-12-02 21:38:49 +01:00
|
|
|
|
|
|
|
#ifdef HAVE_LIBZ
|
2011-04-10 17:42:00 +02:00
|
|
|
z_streamp zp;
|
|
|
|
char *zlibOut;
|
|
|
|
size_t zlibOutSize;
|
2010-12-02 21:38:49 +01:00
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
2010-12-02 20:39:03 +01:00
|
|
|
static void ParseCompressionOption(int compression, CompressionAlgorithm *alg,
|
2019-05-22 19:04:48 +02:00
|
|
|
int *level);
|
2010-12-02 20:39:03 +01:00
|
|
|
|
|
|
|
/* Routines that support zlib compressed data I/O */
|
|
|
|
#ifdef HAVE_LIBZ
|
|
|
|
static void InitCompressorZlib(CompressorState *cs, int level);
|
|
|
|
static void DeflateCompressorZlib(ArchiveHandle *AH, CompressorState *cs,
|
2019-05-22 19:04:48 +02:00
|
|
|
bool flush);
|
2010-12-02 20:39:03 +01:00
|
|
|
static void ReadDataFromArchiveZlib(ArchiveHandle *AH, ReadFunc readF);
|
2014-05-06 02:27:16 +02:00
|
|
|
static void WriteDataToArchiveZlib(ArchiveHandle *AH, CompressorState *cs,
|
2019-05-22 19:04:48 +02:00
|
|
|
const char *data, size_t dLen);
|
2010-12-02 20:39:03 +01:00
|
|
|
static void EndCompressorZlib(ArchiveHandle *AH, CompressorState *cs);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Routines that support uncompressed data I/O */
|
|
|
|
static void ReadDataFromArchiveNone(ArchiveHandle *AH, ReadFunc readF);
|
2014-05-06 02:27:16 +02:00
|
|
|
static void WriteDataToArchiveNone(ArchiveHandle *AH, CompressorState *cs,
|
2019-05-22 19:04:48 +02:00
|
|
|
const char *data, size_t dLen);
|
2010-12-02 20:39:03 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Interprets a numeric 'compression' value. The algorithm implied by the
|
|
|
|
* value (zlib or none at the moment), is returned in *alg, and the
|
|
|
|
* zlib compression level in *level.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
ParseCompressionOption(int compression, CompressionAlgorithm *alg, int *level)
|
|
|
|
{
|
|
|
|
if (compression == Z_DEFAULT_COMPRESSION ||
|
|
|
|
(compression > 0 && compression <= 9))
|
|
|
|
*alg = COMPR_ALG_LIBZ;
|
|
|
|
else if (compression == 0)
|
|
|
|
*alg = COMPR_ALG_NONE;
|
|
|
|
else
|
2010-12-02 21:21:53 +01:00
|
|
|
{
|
Unified logging system for command-line programs
This unifies the various ad hoc logging (message printing, error
printing) systems used throughout the command-line programs.
Features:
- Program name is automatically prefixed.
- Message string does not end with newline. This removes a common
source of inconsistencies and omissions.
- Additionally, a final newline is automatically stripped, simplifying
use of PQerrorMessage() etc., another common source of mistakes.
- I converted error message strings to use %m where possible.
- As a result of the above several points, more translatable message
strings can be shared between different components and between
frontends and backend, without gratuitous punctuation or whitespace
differences.
- There is support for setting a "log level". This is not meant to be
user-facing, but can be used internally to implement debug or
verbose modes.
- Lazy argument evaluation, so no significant overhead if logging at
some level is disabled.
- Some color in the messages, similar to gcc and clang. Set
PG_COLOR=auto to try it out. Some colors are predefined, but can be
customized by setting PG_COLORS.
- Common files (common/, fe_utils/, etc.) can handle logging much more
simply by just using one API without worrying too much about the
context of the calling program, requiring callbacks, or having to
pass "progname" around everywhere.
- Some programs called setvbuf() to make sure that stderr is
unbuffered, even on Windows. But not all programs did that. This
is now done centrally.
Soft goals:
- Reduces vertical space use and visual complexity of error reporting
in the source code.
- Encourages more deliberate classification of messages. For example,
in some cases it wasn't clear without analyzing the surrounding code
whether a message was meant as an error or just an info.
- Concepts and terms are vaguely aligned with popular logging
frameworks such as log4j and Python logging.
This is all just about printing stuff out. Nothing affects program
flow (e.g., fatal exits). The uses are just too varied to do that.
Some existing code had wrappers that do some kind of print-and-exit,
and I adapted those.
I tried to keep the output mostly the same, but there is a lot of
historical baggage to unwind and special cases to consider, and I
might not always have succeeded. One significant change is that
pg_rewind used to write all error messages to stdout. That is now
changed to stderr.
Reviewed-by: Donald Dong <xdong@csumb.edu>
Reviewed-by: Arthur Zakirov <a.zakirov@postgrespro.ru>
Discussion: https://www.postgresql.org/message-id/flat/6a609b43-4f57-7348-6480-bd022f924310@2ndquadrant.com
2019-04-01 14:24:37 +02:00
|
|
|
fatal("invalid compression code: %d", compression);
|
2010-12-02 21:21:53 +01:00
|
|
|
*alg = COMPR_ALG_NONE; /* keep compiler quiet */
|
|
|
|
}
|
2010-12-02 20:39:03 +01:00
|
|
|
|
|
|
|
/* The level is just the passed-in value. */
|
|
|
|
if (level)
|
|
|
|
*level = compression;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Public interface routines */
|
|
|
|
|
|
|
|
/* Allocate a new compressor */
|
|
|
|
CompressorState *
|
|
|
|
AllocateCompressor(int compression, WriteFunc writeF)
|
|
|
|
{
|
|
|
|
CompressorState *cs;
|
|
|
|
CompressionAlgorithm alg;
|
2011-04-10 17:42:00 +02:00
|
|
|
int level;
|
2010-12-02 20:39:03 +01:00
|
|
|
|
|
|
|
ParseCompressionOption(compression, &alg, &level);
|
|
|
|
|
|
|
|
#ifndef HAVE_LIBZ
|
|
|
|
if (alg == COMPR_ALG_LIBZ)
|
Unified logging system for command-line programs
This unifies the various ad hoc logging (message printing, error
printing) systems used throughout the command-line programs.
Features:
- Program name is automatically prefixed.
- Message string does not end with newline. This removes a common
source of inconsistencies and omissions.
- Additionally, a final newline is automatically stripped, simplifying
use of PQerrorMessage() etc., another common source of mistakes.
- I converted error message strings to use %m where possible.
- As a result of the above several points, more translatable message
strings can be shared between different components and between
frontends and backend, without gratuitous punctuation or whitespace
differences.
- There is support for setting a "log level". This is not meant to be
user-facing, but can be used internally to implement debug or
verbose modes.
- Lazy argument evaluation, so no significant overhead if logging at
some level is disabled.
- Some color in the messages, similar to gcc and clang. Set
PG_COLOR=auto to try it out. Some colors are predefined, but can be
customized by setting PG_COLORS.
- Common files (common/, fe_utils/, etc.) can handle logging much more
simply by just using one API without worrying too much about the
context of the calling program, requiring callbacks, or having to
pass "progname" around everywhere.
- Some programs called setvbuf() to make sure that stderr is
unbuffered, even on Windows. But not all programs did that. This
is now done centrally.
Soft goals:
- Reduces vertical space use and visual complexity of error reporting
in the source code.
- Encourages more deliberate classification of messages. For example,
in some cases it wasn't clear without analyzing the surrounding code
whether a message was meant as an error or just an info.
- Concepts and terms are vaguely aligned with popular logging
frameworks such as log4j and Python logging.
This is all just about printing stuff out. Nothing affects program
flow (e.g., fatal exits). The uses are just too varied to do that.
Some existing code had wrappers that do some kind of print-and-exit,
and I adapted those.
I tried to keep the output mostly the same, but there is a lot of
historical baggage to unwind and special cases to consider, and I
might not always have succeeded. One significant change is that
pg_rewind used to write all error messages to stdout. That is now
changed to stderr.
Reviewed-by: Donald Dong <xdong@csumb.edu>
Reviewed-by: Arthur Zakirov <a.zakirov@postgrespro.ru>
Discussion: https://www.postgresql.org/message-id/flat/6a609b43-4f57-7348-6480-bd022f924310@2ndquadrant.com
2019-04-01 14:24:37 +02:00
|
|
|
fatal("not built with zlib support");
|
2010-12-02 20:39:03 +01:00
|
|
|
#endif
|
|
|
|
|
2012-10-02 21:35:10 +02:00
|
|
|
cs = (CompressorState *) pg_malloc0(sizeof(CompressorState));
|
2010-12-02 20:39:03 +01:00
|
|
|
cs->writeF = writeF;
|
|
|
|
cs->comprAlg = alg;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Perform compression algorithm specific initialization.
|
|
|
|
*/
|
|
|
|
#ifdef HAVE_LIBZ
|
|
|
|
if (alg == COMPR_ALG_LIBZ)
|
|
|
|
InitCompressorZlib(cs, level);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return cs;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Read all compressed data from the input stream (via readF) and print it
|
|
|
|
* out with ahwrite().
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
ReadDataFromArchive(ArchiveHandle *AH, int compression, ReadFunc readF)
|
|
|
|
{
|
|
|
|
CompressionAlgorithm alg;
|
|
|
|
|
|
|
|
ParseCompressionOption(compression, &alg, NULL);
|
|
|
|
|
|
|
|
if (alg == COMPR_ALG_NONE)
|
|
|
|
ReadDataFromArchiveNone(AH, readF);
|
|
|
|
if (alg == COMPR_ALG_LIBZ)
|
|
|
|
{
|
|
|
|
#ifdef HAVE_LIBZ
|
|
|
|
ReadDataFromArchiveZlib(AH, readF);
|
|
|
|
#else
|
Unified logging system for command-line programs
This unifies the various ad hoc logging (message printing, error
printing) systems used throughout the command-line programs.
Features:
- Program name is automatically prefixed.
- Message string does not end with newline. This removes a common
source of inconsistencies and omissions.
- Additionally, a final newline is automatically stripped, simplifying
use of PQerrorMessage() etc., another common source of mistakes.
- I converted error message strings to use %m where possible.
- As a result of the above several points, more translatable message
strings can be shared between different components and between
frontends and backend, without gratuitous punctuation or whitespace
differences.
- There is support for setting a "log level". This is not meant to be
user-facing, but can be used internally to implement debug or
verbose modes.
- Lazy argument evaluation, so no significant overhead if logging at
some level is disabled.
- Some color in the messages, similar to gcc and clang. Set
PG_COLOR=auto to try it out. Some colors are predefined, but can be
customized by setting PG_COLORS.
- Common files (common/, fe_utils/, etc.) can handle logging much more
simply by just using one API without worrying too much about the
context of the calling program, requiring callbacks, or having to
pass "progname" around everywhere.
- Some programs called setvbuf() to make sure that stderr is
unbuffered, even on Windows. But not all programs did that. This
is now done centrally.
Soft goals:
- Reduces vertical space use and visual complexity of error reporting
in the source code.
- Encourages more deliberate classification of messages. For example,
in some cases it wasn't clear without analyzing the surrounding code
whether a message was meant as an error or just an info.
- Concepts and terms are vaguely aligned with popular logging
frameworks such as log4j and Python logging.
This is all just about printing stuff out. Nothing affects program
flow (e.g., fatal exits). The uses are just too varied to do that.
Some existing code had wrappers that do some kind of print-and-exit,
and I adapted those.
I tried to keep the output mostly the same, but there is a lot of
historical baggage to unwind and special cases to consider, and I
might not always have succeeded. One significant change is that
pg_rewind used to write all error messages to stdout. That is now
changed to stderr.
Reviewed-by: Donald Dong <xdong@csumb.edu>
Reviewed-by: Arthur Zakirov <a.zakirov@postgrespro.ru>
Discussion: https://www.postgresql.org/message-id/flat/6a609b43-4f57-7348-6480-bd022f924310@2ndquadrant.com
2019-04-01 14:24:37 +02:00
|
|
|
fatal("not built with zlib support");
|
2010-12-02 20:39:03 +01:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Compress and write data to the output stream (via writeF).
|
|
|
|
*/
|
2014-05-06 02:27:16 +02:00
|
|
|
void
|
2010-12-02 20:39:03 +01:00
|
|
|
WriteDataToArchive(ArchiveHandle *AH, CompressorState *cs,
|
|
|
|
const void *data, size_t dLen)
|
|
|
|
{
|
2011-04-10 17:42:00 +02:00
|
|
|
switch (cs->comprAlg)
|
2010-12-02 20:39:03 +01:00
|
|
|
{
|
|
|
|
case COMPR_ALG_LIBZ:
|
|
|
|
#ifdef HAVE_LIBZ
|
2014-05-06 02:27:16 +02:00
|
|
|
WriteDataToArchiveZlib(AH, cs, data, dLen);
|
2010-12-02 20:39:03 +01:00
|
|
|
#else
|
Unified logging system for command-line programs
This unifies the various ad hoc logging (message printing, error
printing) systems used throughout the command-line programs.
Features:
- Program name is automatically prefixed.
- Message string does not end with newline. This removes a common
source of inconsistencies and omissions.
- Additionally, a final newline is automatically stripped, simplifying
use of PQerrorMessage() etc., another common source of mistakes.
- I converted error message strings to use %m where possible.
- As a result of the above several points, more translatable message
strings can be shared between different components and between
frontends and backend, without gratuitous punctuation or whitespace
differences.
- There is support for setting a "log level". This is not meant to be
user-facing, but can be used internally to implement debug or
verbose modes.
- Lazy argument evaluation, so no significant overhead if logging at
some level is disabled.
- Some color in the messages, similar to gcc and clang. Set
PG_COLOR=auto to try it out. Some colors are predefined, but can be
customized by setting PG_COLORS.
- Common files (common/, fe_utils/, etc.) can handle logging much more
simply by just using one API without worrying too much about the
context of the calling program, requiring callbacks, or having to
pass "progname" around everywhere.
- Some programs called setvbuf() to make sure that stderr is
unbuffered, even on Windows. But not all programs did that. This
is now done centrally.
Soft goals:
- Reduces vertical space use and visual complexity of error reporting
in the source code.
- Encourages more deliberate classification of messages. For example,
in some cases it wasn't clear without analyzing the surrounding code
whether a message was meant as an error or just an info.
- Concepts and terms are vaguely aligned with popular logging
frameworks such as log4j and Python logging.
This is all just about printing stuff out. Nothing affects program
flow (e.g., fatal exits). The uses are just too varied to do that.
Some existing code had wrappers that do some kind of print-and-exit,
and I adapted those.
I tried to keep the output mostly the same, but there is a lot of
historical baggage to unwind and special cases to consider, and I
might not always have succeeded. One significant change is that
pg_rewind used to write all error messages to stdout. That is now
changed to stderr.
Reviewed-by: Donald Dong <xdong@csumb.edu>
Reviewed-by: Arthur Zakirov <a.zakirov@postgrespro.ru>
Discussion: https://www.postgresql.org/message-id/flat/6a609b43-4f57-7348-6480-bd022f924310@2ndquadrant.com
2019-04-01 14:24:37 +02:00
|
|
|
fatal("not built with zlib support");
|
2010-12-02 20:39:03 +01:00
|
|
|
#endif
|
2014-05-06 02:27:16 +02:00
|
|
|
break;
|
2010-12-02 20:39:03 +01:00
|
|
|
case COMPR_ALG_NONE:
|
2014-05-06 02:27:16 +02:00
|
|
|
WriteDataToArchiveNone(AH, cs, data, dLen);
|
|
|
|
break;
|
2010-12-02 20:39:03 +01:00
|
|
|
}
|
2014-05-06 02:27:16 +02:00
|
|
|
return;
|
2010-12-02 20:39:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Terminate compression library context and flush its buffers.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
EndCompressor(ArchiveHandle *AH, CompressorState *cs)
|
|
|
|
{
|
|
|
|
#ifdef HAVE_LIBZ
|
|
|
|
if (cs->comprAlg == COMPR_ALG_LIBZ)
|
|
|
|
EndCompressorZlib(AH, cs);
|
|
|
|
#endif
|
|
|
|
free(cs);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Private routines, specific to each compression method. */
|
|
|
|
|
|
|
|
#ifdef HAVE_LIBZ
|
|
|
|
/*
|
|
|
|
* Functions for zlib compressed output.
|
|
|
|
*/
|
|
|
|
|
|
|
|
static void
|
|
|
|
InitCompressorZlib(CompressorState *cs, int level)
|
|
|
|
{
|
2011-04-10 17:42:00 +02:00
|
|
|
z_streamp zp;
|
2010-12-02 20:39:03 +01:00
|
|
|
|
2011-11-25 21:40:51 +01:00
|
|
|
zp = cs->zp = (z_streamp) pg_malloc(sizeof(z_stream));
|
2010-12-02 20:39:03 +01:00
|
|
|
zp->zalloc = Z_NULL;
|
|
|
|
zp->zfree = Z_NULL;
|
|
|
|
zp->opaque = Z_NULL;
|
|
|
|
|
|
|
|
/*
|
2011-04-10 17:42:00 +02:00
|
|
|
* zlibOutSize is the buffer size we tell zlib it can output to. We
|
|
|
|
* actually allocate one extra byte because some routines want to append a
|
|
|
|
* trailing zero byte to the zlib output.
|
2010-12-02 20:39:03 +01:00
|
|
|
*/
|
2011-11-25 21:40:51 +01:00
|
|
|
cs->zlibOut = (char *) pg_malloc(ZLIB_OUT_SIZE + 1);
|
2010-12-02 20:39:03 +01:00
|
|
|
cs->zlibOutSize = ZLIB_OUT_SIZE;
|
|
|
|
|
|
|
|
if (deflateInit(zp, level) != Z_OK)
|
Unified logging system for command-line programs
This unifies the various ad hoc logging (message printing, error
printing) systems used throughout the command-line programs.
Features:
- Program name is automatically prefixed.
- Message string does not end with newline. This removes a common
source of inconsistencies and omissions.
- Additionally, a final newline is automatically stripped, simplifying
use of PQerrorMessage() etc., another common source of mistakes.
- I converted error message strings to use %m where possible.
- As a result of the above several points, more translatable message
strings can be shared between different components and between
frontends and backend, without gratuitous punctuation or whitespace
differences.
- There is support for setting a "log level". This is not meant to be
user-facing, but can be used internally to implement debug or
verbose modes.
- Lazy argument evaluation, so no significant overhead if logging at
some level is disabled.
- Some color in the messages, similar to gcc and clang. Set
PG_COLOR=auto to try it out. Some colors are predefined, but can be
customized by setting PG_COLORS.
- Common files (common/, fe_utils/, etc.) can handle logging much more
simply by just using one API without worrying too much about the
context of the calling program, requiring callbacks, or having to
pass "progname" around everywhere.
- Some programs called setvbuf() to make sure that stderr is
unbuffered, even on Windows. But not all programs did that. This
is now done centrally.
Soft goals:
- Reduces vertical space use and visual complexity of error reporting
in the source code.
- Encourages more deliberate classification of messages. For example,
in some cases it wasn't clear without analyzing the surrounding code
whether a message was meant as an error or just an info.
- Concepts and terms are vaguely aligned with popular logging
frameworks such as log4j and Python logging.
This is all just about printing stuff out. Nothing affects program
flow (e.g., fatal exits). The uses are just too varied to do that.
Some existing code had wrappers that do some kind of print-and-exit,
and I adapted those.
I tried to keep the output mostly the same, but there is a lot of
historical baggage to unwind and special cases to consider, and I
might not always have succeeded. One significant change is that
pg_rewind used to write all error messages to stdout. That is now
changed to stderr.
Reviewed-by: Donald Dong <xdong@csumb.edu>
Reviewed-by: Arthur Zakirov <a.zakirov@postgrespro.ru>
Discussion: https://www.postgresql.org/message-id/flat/6a609b43-4f57-7348-6480-bd022f924310@2ndquadrant.com
2019-04-01 14:24:37 +02:00
|
|
|
fatal("could not initialize compression library: %s",
|
2019-05-22 18:55:34 +02:00
|
|
|
zp->msg);
|
2010-12-02 20:39:03 +01:00
|
|
|
|
|
|
|
/* Just be paranoid - maybe End is called after Start, with no Write */
|
|
|
|
zp->next_out = (void *) cs->zlibOut;
|
|
|
|
zp->avail_out = cs->zlibOutSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
EndCompressorZlib(ArchiveHandle *AH, CompressorState *cs)
|
|
|
|
{
|
2011-04-10 17:42:00 +02:00
|
|
|
z_streamp zp = cs->zp;
|
2010-12-02 20:39:03 +01:00
|
|
|
|
|
|
|
zp->next_in = NULL;
|
|
|
|
zp->avail_in = 0;
|
|
|
|
|
|
|
|
/* Flush any remaining data from zlib buffer */
|
|
|
|
DeflateCompressorZlib(AH, cs, true);
|
|
|
|
|
|
|
|
if (deflateEnd(zp) != Z_OK)
|
Unified logging system for command-line programs
This unifies the various ad hoc logging (message printing, error
printing) systems used throughout the command-line programs.
Features:
- Program name is automatically prefixed.
- Message string does not end with newline. This removes a common
source of inconsistencies and omissions.
- Additionally, a final newline is automatically stripped, simplifying
use of PQerrorMessage() etc., another common source of mistakes.
- I converted error message strings to use %m where possible.
- As a result of the above several points, more translatable message
strings can be shared between different components and between
frontends and backend, without gratuitous punctuation or whitespace
differences.
- There is support for setting a "log level". This is not meant to be
user-facing, but can be used internally to implement debug or
verbose modes.
- Lazy argument evaluation, so no significant overhead if logging at
some level is disabled.
- Some color in the messages, similar to gcc and clang. Set
PG_COLOR=auto to try it out. Some colors are predefined, but can be
customized by setting PG_COLORS.
- Common files (common/, fe_utils/, etc.) can handle logging much more
simply by just using one API without worrying too much about the
context of the calling program, requiring callbacks, or having to
pass "progname" around everywhere.
- Some programs called setvbuf() to make sure that stderr is
unbuffered, even on Windows. But not all programs did that. This
is now done centrally.
Soft goals:
- Reduces vertical space use and visual complexity of error reporting
in the source code.
- Encourages more deliberate classification of messages. For example,
in some cases it wasn't clear without analyzing the surrounding code
whether a message was meant as an error or just an info.
- Concepts and terms are vaguely aligned with popular logging
frameworks such as log4j and Python logging.
This is all just about printing stuff out. Nothing affects program
flow (e.g., fatal exits). The uses are just too varied to do that.
Some existing code had wrappers that do some kind of print-and-exit,
and I adapted those.
I tried to keep the output mostly the same, but there is a lot of
historical baggage to unwind and special cases to consider, and I
might not always have succeeded. One significant change is that
pg_rewind used to write all error messages to stdout. That is now
changed to stderr.
Reviewed-by: Donald Dong <xdong@csumb.edu>
Reviewed-by: Arthur Zakirov <a.zakirov@postgrespro.ru>
Discussion: https://www.postgresql.org/message-id/flat/6a609b43-4f57-7348-6480-bd022f924310@2ndquadrant.com
2019-04-01 14:24:37 +02:00
|
|
|
fatal("could not close compression stream: %s", zp->msg);
|
2010-12-02 20:39:03 +01:00
|
|
|
|
|
|
|
free(cs->zlibOut);
|
|
|
|
free(cs->zp);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
DeflateCompressorZlib(ArchiveHandle *AH, CompressorState *cs, bool flush)
|
|
|
|
{
|
|
|
|
z_streamp zp = cs->zp;
|
|
|
|
char *out = cs->zlibOut;
|
|
|
|
int res = Z_OK;
|
|
|
|
|
|
|
|
while (cs->zp->avail_in != 0 || flush)
|
|
|
|
{
|
|
|
|
res = deflate(zp, flush ? Z_FINISH : Z_NO_FLUSH);
|
|
|
|
if (res == Z_STREAM_ERROR)
|
Unified logging system for command-line programs
This unifies the various ad hoc logging (message printing, error
printing) systems used throughout the command-line programs.
Features:
- Program name is automatically prefixed.
- Message string does not end with newline. This removes a common
source of inconsistencies and omissions.
- Additionally, a final newline is automatically stripped, simplifying
use of PQerrorMessage() etc., another common source of mistakes.
- I converted error message strings to use %m where possible.
- As a result of the above several points, more translatable message
strings can be shared between different components and between
frontends and backend, without gratuitous punctuation or whitespace
differences.
- There is support for setting a "log level". This is not meant to be
user-facing, but can be used internally to implement debug or
verbose modes.
- Lazy argument evaluation, so no significant overhead if logging at
some level is disabled.
- Some color in the messages, similar to gcc and clang. Set
PG_COLOR=auto to try it out. Some colors are predefined, but can be
customized by setting PG_COLORS.
- Common files (common/, fe_utils/, etc.) can handle logging much more
simply by just using one API without worrying too much about the
context of the calling program, requiring callbacks, or having to
pass "progname" around everywhere.
- Some programs called setvbuf() to make sure that stderr is
unbuffered, even on Windows. But not all programs did that. This
is now done centrally.
Soft goals:
- Reduces vertical space use and visual complexity of error reporting
in the source code.
- Encourages more deliberate classification of messages. For example,
in some cases it wasn't clear without analyzing the surrounding code
whether a message was meant as an error or just an info.
- Concepts and terms are vaguely aligned with popular logging
frameworks such as log4j and Python logging.
This is all just about printing stuff out. Nothing affects program
flow (e.g., fatal exits). The uses are just too varied to do that.
Some existing code had wrappers that do some kind of print-and-exit,
and I adapted those.
I tried to keep the output mostly the same, but there is a lot of
historical baggage to unwind and special cases to consider, and I
might not always have succeeded. One significant change is that
pg_rewind used to write all error messages to stdout. That is now
changed to stderr.
Reviewed-by: Donald Dong <xdong@csumb.edu>
Reviewed-by: Arthur Zakirov <a.zakirov@postgrespro.ru>
Discussion: https://www.postgresql.org/message-id/flat/6a609b43-4f57-7348-6480-bd022f924310@2ndquadrant.com
2019-04-01 14:24:37 +02:00
|
|
|
fatal("could not compress data: %s", zp->msg);
|
2010-12-02 20:39:03 +01:00
|
|
|
if ((flush && (zp->avail_out < cs->zlibOutSize))
|
|
|
|
|| (zp->avail_out == 0)
|
|
|
|
|| (zp->avail_in != 0)
|
|
|
|
)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Extra paranoia: avoid zero-length chunks, since a zero length
|
|
|
|
* chunk is the EOF marker in the custom format. This should never
|
|
|
|
* happen but...
|
|
|
|
*/
|
|
|
|
if (zp->avail_out < cs->zlibOutSize)
|
|
|
|
{
|
|
|
|
/*
|
2016-03-15 23:06:11 +01:00
|
|
|
* Any write function should do its own error checking but to
|
2011-04-10 17:42:00 +02:00
|
|
|
* make sure we do a check here as well...
|
2010-12-02 20:39:03 +01:00
|
|
|
*/
|
2011-04-10 17:42:00 +02:00
|
|
|
size_t len = cs->zlibOutSize - zp->avail_out;
|
|
|
|
|
2014-05-06 02:27:16 +02:00
|
|
|
cs->writeF(AH, out, len);
|
2010-12-02 20:39:03 +01:00
|
|
|
}
|
|
|
|
zp->next_out = (void *) out;
|
|
|
|
zp->avail_out = cs->zlibOutSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (res == Z_STREAM_END)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-06 02:27:16 +02:00
|
|
|
static void
|
2010-12-02 20:39:03 +01:00
|
|
|
WriteDataToArchiveZlib(ArchiveHandle *AH, CompressorState *cs,
|
|
|
|
const char *data, size_t dLen)
|
|
|
|
{
|
2019-01-29 01:16:24 +01:00
|
|
|
cs->zp->next_in = (void *) unconstify(char *, data);
|
2010-12-02 20:39:03 +01:00
|
|
|
cs->zp->avail_in = dLen;
|
|
|
|
DeflateCompressorZlib(AH, cs, false);
|
2011-04-10 17:42:00 +02:00
|
|
|
|
2014-05-06 02:27:16 +02:00
|
|
|
return;
|
2010-12-02 20:39:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
ReadDataFromArchiveZlib(ArchiveHandle *AH, ReadFunc readF)
|
|
|
|
{
|
|
|
|
z_streamp zp;
|
|
|
|
char *out;
|
|
|
|
int res = Z_OK;
|
|
|
|
size_t cnt;
|
|
|
|
char *buf;
|
|
|
|
size_t buflen;
|
|
|
|
|
2011-11-25 21:40:51 +01:00
|
|
|
zp = (z_streamp) pg_malloc(sizeof(z_stream));
|
2010-12-02 20:39:03 +01:00
|
|
|
zp->zalloc = Z_NULL;
|
|
|
|
zp->zfree = Z_NULL;
|
|
|
|
zp->opaque = Z_NULL;
|
|
|
|
|
2011-11-25 21:40:51 +01:00
|
|
|
buf = pg_malloc(ZLIB_IN_SIZE);
|
2010-12-02 20:39:03 +01:00
|
|
|
buflen = ZLIB_IN_SIZE;
|
|
|
|
|
2011-11-25 21:40:51 +01:00
|
|
|
out = pg_malloc(ZLIB_OUT_SIZE + 1);
|
2010-12-02 20:39:03 +01:00
|
|
|
|
|
|
|
if (inflateInit(zp) != Z_OK)
|
Unified logging system for command-line programs
This unifies the various ad hoc logging (message printing, error
printing) systems used throughout the command-line programs.
Features:
- Program name is automatically prefixed.
- Message string does not end with newline. This removes a common
source of inconsistencies and omissions.
- Additionally, a final newline is automatically stripped, simplifying
use of PQerrorMessage() etc., another common source of mistakes.
- I converted error message strings to use %m where possible.
- As a result of the above several points, more translatable message
strings can be shared between different components and between
frontends and backend, without gratuitous punctuation or whitespace
differences.
- There is support for setting a "log level". This is not meant to be
user-facing, but can be used internally to implement debug or
verbose modes.
- Lazy argument evaluation, so no significant overhead if logging at
some level is disabled.
- Some color in the messages, similar to gcc and clang. Set
PG_COLOR=auto to try it out. Some colors are predefined, but can be
customized by setting PG_COLORS.
- Common files (common/, fe_utils/, etc.) can handle logging much more
simply by just using one API without worrying too much about the
context of the calling program, requiring callbacks, or having to
pass "progname" around everywhere.
- Some programs called setvbuf() to make sure that stderr is
unbuffered, even on Windows. But not all programs did that. This
is now done centrally.
Soft goals:
- Reduces vertical space use and visual complexity of error reporting
in the source code.
- Encourages more deliberate classification of messages. For example,
in some cases it wasn't clear without analyzing the surrounding code
whether a message was meant as an error or just an info.
- Concepts and terms are vaguely aligned with popular logging
frameworks such as log4j and Python logging.
This is all just about printing stuff out. Nothing affects program
flow (e.g., fatal exits). The uses are just too varied to do that.
Some existing code had wrappers that do some kind of print-and-exit,
and I adapted those.
I tried to keep the output mostly the same, but there is a lot of
historical baggage to unwind and special cases to consider, and I
might not always have succeeded. One significant change is that
pg_rewind used to write all error messages to stdout. That is now
changed to stderr.
Reviewed-by: Donald Dong <xdong@csumb.edu>
Reviewed-by: Arthur Zakirov <a.zakirov@postgrespro.ru>
Discussion: https://www.postgresql.org/message-id/flat/6a609b43-4f57-7348-6480-bd022f924310@2ndquadrant.com
2019-04-01 14:24:37 +02:00
|
|
|
fatal("could not initialize compression library: %s",
|
2019-05-22 18:55:34 +02:00
|
|
|
zp->msg);
|
2010-12-02 20:39:03 +01:00
|
|
|
|
|
|
|
/* no minimal chunk size for zlib */
|
|
|
|
while ((cnt = readF(AH, &buf, &buflen)))
|
|
|
|
{
|
|
|
|
zp->next_in = (void *) buf;
|
|
|
|
zp->avail_in = cnt;
|
|
|
|
|
|
|
|
while (zp->avail_in > 0)
|
|
|
|
{
|
|
|
|
zp->next_out = (void *) out;
|
|
|
|
zp->avail_out = ZLIB_OUT_SIZE;
|
|
|
|
|
|
|
|
res = inflate(zp, 0);
|
|
|
|
if (res != Z_OK && res != Z_STREAM_END)
|
Unified logging system for command-line programs
This unifies the various ad hoc logging (message printing, error
printing) systems used throughout the command-line programs.
Features:
- Program name is automatically prefixed.
- Message string does not end with newline. This removes a common
source of inconsistencies and omissions.
- Additionally, a final newline is automatically stripped, simplifying
use of PQerrorMessage() etc., another common source of mistakes.
- I converted error message strings to use %m where possible.
- As a result of the above several points, more translatable message
strings can be shared between different components and between
frontends and backend, without gratuitous punctuation or whitespace
differences.
- There is support for setting a "log level". This is not meant to be
user-facing, but can be used internally to implement debug or
verbose modes.
- Lazy argument evaluation, so no significant overhead if logging at
some level is disabled.
- Some color in the messages, similar to gcc and clang. Set
PG_COLOR=auto to try it out. Some colors are predefined, but can be
customized by setting PG_COLORS.
- Common files (common/, fe_utils/, etc.) can handle logging much more
simply by just using one API without worrying too much about the
context of the calling program, requiring callbacks, or having to
pass "progname" around everywhere.
- Some programs called setvbuf() to make sure that stderr is
unbuffered, even on Windows. But not all programs did that. This
is now done centrally.
Soft goals:
- Reduces vertical space use and visual complexity of error reporting
in the source code.
- Encourages more deliberate classification of messages. For example,
in some cases it wasn't clear without analyzing the surrounding code
whether a message was meant as an error or just an info.
- Concepts and terms are vaguely aligned with popular logging
frameworks such as log4j and Python logging.
This is all just about printing stuff out. Nothing affects program
flow (e.g., fatal exits). The uses are just too varied to do that.
Some existing code had wrappers that do some kind of print-and-exit,
and I adapted those.
I tried to keep the output mostly the same, but there is a lot of
historical baggage to unwind and special cases to consider, and I
might not always have succeeded. One significant change is that
pg_rewind used to write all error messages to stdout. That is now
changed to stderr.
Reviewed-by: Donald Dong <xdong@csumb.edu>
Reviewed-by: Arthur Zakirov <a.zakirov@postgrespro.ru>
Discussion: https://www.postgresql.org/message-id/flat/6a609b43-4f57-7348-6480-bd022f924310@2ndquadrant.com
2019-04-01 14:24:37 +02:00
|
|
|
fatal("could not uncompress data: %s", zp->msg);
|
2010-12-02 20:39:03 +01:00
|
|
|
|
|
|
|
out[ZLIB_OUT_SIZE - zp->avail_out] = '\0';
|
|
|
|
ahwrite(out, 1, ZLIB_OUT_SIZE - zp->avail_out, AH);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
zp->next_in = NULL;
|
|
|
|
zp->avail_in = 0;
|
|
|
|
while (res != Z_STREAM_END)
|
|
|
|
{
|
|
|
|
zp->next_out = (void *) out;
|
|
|
|
zp->avail_out = ZLIB_OUT_SIZE;
|
|
|
|
res = inflate(zp, 0);
|
|
|
|
if (res != Z_OK && res != Z_STREAM_END)
|
Unified logging system for command-line programs
This unifies the various ad hoc logging (message printing, error
printing) systems used throughout the command-line programs.
Features:
- Program name is automatically prefixed.
- Message string does not end with newline. This removes a common
source of inconsistencies and omissions.
- Additionally, a final newline is automatically stripped, simplifying
use of PQerrorMessage() etc., another common source of mistakes.
- I converted error message strings to use %m where possible.
- As a result of the above several points, more translatable message
strings can be shared between different components and between
frontends and backend, without gratuitous punctuation or whitespace
differences.
- There is support for setting a "log level". This is not meant to be
user-facing, but can be used internally to implement debug or
verbose modes.
- Lazy argument evaluation, so no significant overhead if logging at
some level is disabled.
- Some color in the messages, similar to gcc and clang. Set
PG_COLOR=auto to try it out. Some colors are predefined, but can be
customized by setting PG_COLORS.
- Common files (common/, fe_utils/, etc.) can handle logging much more
simply by just using one API without worrying too much about the
context of the calling program, requiring callbacks, or having to
pass "progname" around everywhere.
- Some programs called setvbuf() to make sure that stderr is
unbuffered, even on Windows. But not all programs did that. This
is now done centrally.
Soft goals:
- Reduces vertical space use and visual complexity of error reporting
in the source code.
- Encourages more deliberate classification of messages. For example,
in some cases it wasn't clear without analyzing the surrounding code
whether a message was meant as an error or just an info.
- Concepts and terms are vaguely aligned with popular logging
frameworks such as log4j and Python logging.
This is all just about printing stuff out. Nothing affects program
flow (e.g., fatal exits). The uses are just too varied to do that.
Some existing code had wrappers that do some kind of print-and-exit,
and I adapted those.
I tried to keep the output mostly the same, but there is a lot of
historical baggage to unwind and special cases to consider, and I
might not always have succeeded. One significant change is that
pg_rewind used to write all error messages to stdout. That is now
changed to stderr.
Reviewed-by: Donald Dong <xdong@csumb.edu>
Reviewed-by: Arthur Zakirov <a.zakirov@postgrespro.ru>
Discussion: https://www.postgresql.org/message-id/flat/6a609b43-4f57-7348-6480-bd022f924310@2ndquadrant.com
2019-04-01 14:24:37 +02:00
|
|
|
fatal("could not uncompress data: %s", zp->msg);
|
2010-12-02 20:39:03 +01:00
|
|
|
|
|
|
|
out[ZLIB_OUT_SIZE - zp->avail_out] = '\0';
|
|
|
|
ahwrite(out, 1, ZLIB_OUT_SIZE - zp->avail_out, AH);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (inflateEnd(zp) != Z_OK)
|
Unified logging system for command-line programs
This unifies the various ad hoc logging (message printing, error
printing) systems used throughout the command-line programs.
Features:
- Program name is automatically prefixed.
- Message string does not end with newline. This removes a common
source of inconsistencies and omissions.
- Additionally, a final newline is automatically stripped, simplifying
use of PQerrorMessage() etc., another common source of mistakes.
- I converted error message strings to use %m where possible.
- As a result of the above several points, more translatable message
strings can be shared between different components and between
frontends and backend, without gratuitous punctuation or whitespace
differences.
- There is support for setting a "log level". This is not meant to be
user-facing, but can be used internally to implement debug or
verbose modes.
- Lazy argument evaluation, so no significant overhead if logging at
some level is disabled.
- Some color in the messages, similar to gcc and clang. Set
PG_COLOR=auto to try it out. Some colors are predefined, but can be
customized by setting PG_COLORS.
- Common files (common/, fe_utils/, etc.) can handle logging much more
simply by just using one API without worrying too much about the
context of the calling program, requiring callbacks, or having to
pass "progname" around everywhere.
- Some programs called setvbuf() to make sure that stderr is
unbuffered, even on Windows. But not all programs did that. This
is now done centrally.
Soft goals:
- Reduces vertical space use and visual complexity of error reporting
in the source code.
- Encourages more deliberate classification of messages. For example,
in some cases it wasn't clear without analyzing the surrounding code
whether a message was meant as an error or just an info.
- Concepts and terms are vaguely aligned with popular logging
frameworks such as log4j and Python logging.
This is all just about printing stuff out. Nothing affects program
flow (e.g., fatal exits). The uses are just too varied to do that.
Some existing code had wrappers that do some kind of print-and-exit,
and I adapted those.
I tried to keep the output mostly the same, but there is a lot of
historical baggage to unwind and special cases to consider, and I
might not always have succeeded. One significant change is that
pg_rewind used to write all error messages to stdout. That is now
changed to stderr.
Reviewed-by: Donald Dong <xdong@csumb.edu>
Reviewed-by: Arthur Zakirov <a.zakirov@postgrespro.ru>
Discussion: https://www.postgresql.org/message-id/flat/6a609b43-4f57-7348-6480-bd022f924310@2ndquadrant.com
2019-04-01 14:24:37 +02:00
|
|
|
fatal("could not close compression library: %s", zp->msg);
|
2010-12-02 20:39:03 +01:00
|
|
|
|
|
|
|
free(buf);
|
|
|
|
free(out);
|
|
|
|
free(zp);
|
|
|
|
}
|
Phase 2 of pgindent updates.
Change pg_bsd_indent to follow upstream rules for placement of comments
to the right of code, and remove pgindent hack that caused comments
following #endif to not obey the general rule.
Commit e3860ffa4dd0dad0dd9eea4be9cc1412373a8c89 wasn't actually using
the published version of pg_bsd_indent, but a hacked-up version that
tried to minimize the amount of movement of comments to the right of
code. The situation of interest is where such a comment has to be
moved to the right of its default placement at column 33 because there's
code there. BSD indent has always moved right in units of tab stops
in such cases --- but in the previous incarnation, indent was working
in 8-space tab stops, while now it knows we use 4-space tabs. So the
net result is that in about half the cases, such comments are placed
one tab stop left of before. This is better all around: it leaves
more room on the line for comment text, and it means that in such
cases the comment uniformly starts at the next 4-space tab stop after
the code, rather than sometimes one and sometimes two tabs after.
Also, ensure that comments following #endif are indented the same
as comments following other preprocessor commands such as #else.
That inconsistency turns out to have been self-inflicted damage
from a poorly-thought-through post-indent "fixup" in pgindent.
This patch is much less interesting than the first round of indent
changes, but also bulkier, so I thought it best to separate the effects.
Discussion: https://postgr.es/m/E1dAmxK-0006EE-1r@gemulon.postgresql.org
Discussion: https://postgr.es/m/30527.1495162840@sss.pgh.pa.us
2017-06-21 21:18:54 +02:00
|
|
|
#endif /* HAVE_LIBZ */
|
2010-12-02 20:39:03 +01:00
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Functions for uncompressed output.
|
|
|
|
*/
|
|
|
|
|
|
|
|
static void
|
|
|
|
ReadDataFromArchiveNone(ArchiveHandle *AH, ReadFunc readF)
|
|
|
|
{
|
|
|
|
size_t cnt;
|
|
|
|
char *buf;
|
|
|
|
size_t buflen;
|
|
|
|
|
2011-11-25 21:40:51 +01:00
|
|
|
buf = pg_malloc(ZLIB_OUT_SIZE);
|
2010-12-02 20:39:03 +01:00
|
|
|
buflen = ZLIB_OUT_SIZE;
|
|
|
|
|
|
|
|
while ((cnt = readF(AH, &buf, &buflen)))
|
|
|
|
{
|
|
|
|
ahwrite(buf, 1, cnt, AH);
|
|
|
|
}
|
|
|
|
|
|
|
|
free(buf);
|
|
|
|
}
|
|
|
|
|
2014-05-06 02:27:16 +02:00
|
|
|
static void
|
2010-12-02 20:39:03 +01:00
|
|
|
WriteDataToArchiveNone(ArchiveHandle *AH, CompressorState *cs,
|
|
|
|
const char *data, size_t dLen)
|
|
|
|
{
|
2014-05-06 02:27:16 +02:00
|
|
|
cs->writeF(AH, data, dLen);
|
|
|
|
return;
|
2010-12-02 20:39:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-01-23 22:10:15 +01:00
|
|
|
/*----------------------
|
|
|
|
* Compressed stream API
|
|
|
|
*----------------------
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* cfp represents an open stream, wrapping the underlying FILE or gzFile
|
|
|
|
* pointer. This is opaque to the callers.
|
|
|
|
*/
|
|
|
|
struct cfp
|
|
|
|
{
|
2011-04-10 17:42:00 +02:00
|
|
|
FILE *uncompressedfp;
|
2011-01-23 22:10:15 +01:00
|
|
|
#ifdef HAVE_LIBZ
|
2011-04-10 17:42:00 +02:00
|
|
|
gzFile compressedfp;
|
2011-01-23 22:10:15 +01:00
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
|
|
|
#ifdef HAVE_LIBZ
|
|
|
|
static int hasSuffix(const char *filename, const char *suffix);
|
|
|
|
#endif
|
|
|
|
|
2015-02-18 17:43:00 +01:00
|
|
|
/* free() without changing errno; useful in several places below */
|
|
|
|
static void
|
|
|
|
free_keep_errno(void *p)
|
|
|
|
{
|
|
|
|
int save_errno = errno;
|
|
|
|
|
|
|
|
free(p);
|
|
|
|
errno = save_errno;
|
|
|
|
}
|
|
|
|
|
2011-01-23 22:10:15 +01:00
|
|
|
/*
|
|
|
|
* Open a file for reading. 'path' is the file to open, and 'mode' should
|
|
|
|
* be either "r" or "rb".
|
|
|
|
*
|
|
|
|
* If the file at 'path' does not exist, we append the ".gz" suffix (if 'path'
|
|
|
|
* doesn't already have it) and try again. So if you pass "foo" as 'path',
|
|
|
|
* this will open either "foo" or "foo.gz".
|
2015-02-18 17:43:00 +01:00
|
|
|
*
|
|
|
|
* On failure, return NULL with an error code in errno.
|
2011-01-23 22:10:15 +01:00
|
|
|
*/
|
|
|
|
cfp *
|
|
|
|
cfopen_read(const char *path, const char *mode)
|
|
|
|
{
|
2011-04-10 17:42:00 +02:00
|
|
|
cfp *fp;
|
2011-01-23 22:10:15 +01:00
|
|
|
|
|
|
|
#ifdef HAVE_LIBZ
|
|
|
|
if (hasSuffix(path, ".gz"))
|
|
|
|
fp = cfopen(path, mode, 1);
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
fp = cfopen(path, mode, 0);
|
|
|
|
#ifdef HAVE_LIBZ
|
|
|
|
if (fp == NULL)
|
|
|
|
{
|
2013-10-13 06:09:18 +02:00
|
|
|
char *fname;
|
2011-01-23 22:10:15 +01:00
|
|
|
|
2013-10-23 01:40:26 +02:00
|
|
|
fname = psprintf("%s.gz", path);
|
2011-01-23 22:10:15 +01:00
|
|
|
fp = cfopen(fname, mode, 1);
|
2015-02-18 17:43:00 +01:00
|
|
|
free_keep_errno(fname);
|
2011-01-23 22:10:15 +01:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
return fp;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Open a file for writing. 'path' indicates the path name, and 'mode' must
|
|
|
|
* be a filemode as accepted by fopen() and gzopen() that indicates writing
|
|
|
|
* ("w", "wb", "a", or "ab").
|
|
|
|
*
|
|
|
|
* If 'compression' is non-zero, a gzip compressed stream is opened, and
|
2015-02-18 17:43:00 +01:00
|
|
|
* 'compression' indicates the compression level used. The ".gz" suffix
|
2011-01-23 22:10:15 +01:00
|
|
|
* is automatically added to 'path' in that case.
|
2015-02-18 17:43:00 +01:00
|
|
|
*
|
|
|
|
* On failure, return NULL with an error code in errno.
|
2011-01-23 22:10:15 +01:00
|
|
|
*/
|
|
|
|
cfp *
|
|
|
|
cfopen_write(const char *path, const char *mode, int compression)
|
|
|
|
{
|
2011-04-10 17:42:00 +02:00
|
|
|
cfp *fp;
|
2011-01-23 22:10:15 +01:00
|
|
|
|
|
|
|
if (compression == 0)
|
|
|
|
fp = cfopen(path, mode, 0);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
#ifdef HAVE_LIBZ
|
2013-10-13 06:09:18 +02:00
|
|
|
char *fname;
|
2011-01-23 22:10:15 +01:00
|
|
|
|
2013-10-23 01:40:26 +02:00
|
|
|
fname = psprintf("%s.gz", path);
|
2015-02-18 17:43:00 +01:00
|
|
|
fp = cfopen(fname, mode, compression);
|
|
|
|
free_keep_errno(fname);
|
2011-01-23 22:10:15 +01:00
|
|
|
#else
|
Unified logging system for command-line programs
This unifies the various ad hoc logging (message printing, error
printing) systems used throughout the command-line programs.
Features:
- Program name is automatically prefixed.
- Message string does not end with newline. This removes a common
source of inconsistencies and omissions.
- Additionally, a final newline is automatically stripped, simplifying
use of PQerrorMessage() etc., another common source of mistakes.
- I converted error message strings to use %m where possible.
- As a result of the above several points, more translatable message
strings can be shared between different components and between
frontends and backend, without gratuitous punctuation or whitespace
differences.
- There is support for setting a "log level". This is not meant to be
user-facing, but can be used internally to implement debug or
verbose modes.
- Lazy argument evaluation, so no significant overhead if logging at
some level is disabled.
- Some color in the messages, similar to gcc and clang. Set
PG_COLOR=auto to try it out. Some colors are predefined, but can be
customized by setting PG_COLORS.
- Common files (common/, fe_utils/, etc.) can handle logging much more
simply by just using one API without worrying too much about the
context of the calling program, requiring callbacks, or having to
pass "progname" around everywhere.
- Some programs called setvbuf() to make sure that stderr is
unbuffered, even on Windows. But not all programs did that. This
is now done centrally.
Soft goals:
- Reduces vertical space use and visual complexity of error reporting
in the source code.
- Encourages more deliberate classification of messages. For example,
in some cases it wasn't clear without analyzing the surrounding code
whether a message was meant as an error or just an info.
- Concepts and terms are vaguely aligned with popular logging
frameworks such as log4j and Python logging.
This is all just about printing stuff out. Nothing affects program
flow (e.g., fatal exits). The uses are just too varied to do that.
Some existing code had wrappers that do some kind of print-and-exit,
and I adapted those.
I tried to keep the output mostly the same, but there is a lot of
historical baggage to unwind and special cases to consider, and I
might not always have succeeded. One significant change is that
pg_rewind used to write all error messages to stdout. That is now
changed to stderr.
Reviewed-by: Donald Dong <xdong@csumb.edu>
Reviewed-by: Arthur Zakirov <a.zakirov@postgrespro.ru>
Discussion: https://www.postgresql.org/message-id/flat/6a609b43-4f57-7348-6480-bd022f924310@2ndquadrant.com
2019-04-01 14:24:37 +02:00
|
|
|
fatal("not built with zlib support");
|
2011-05-29 00:03:38 +02:00
|
|
|
fp = NULL; /* keep compiler quiet */
|
2011-01-23 22:10:15 +01:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
return fp;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Opens file 'path' in 'mode'. If 'compression' is non-zero, the file
|
2015-02-18 17:43:00 +01:00
|
|
|
* is opened with libz gzopen(), otherwise with plain fopen().
|
|
|
|
*
|
|
|
|
* On failure, return NULL with an error code in errno.
|
2011-01-23 22:10:15 +01:00
|
|
|
*/
|
|
|
|
cfp *
|
|
|
|
cfopen(const char *path, const char *mode, int compression)
|
|
|
|
{
|
2011-11-25 21:40:51 +01:00
|
|
|
cfp *fp = pg_malloc(sizeof(cfp));
|
2011-01-23 22:10:15 +01:00
|
|
|
|
|
|
|
if (compression != 0)
|
|
|
|
{
|
|
|
|
#ifdef HAVE_LIBZ
|
2015-07-25 23:14:36 +02:00
|
|
|
if (compression != Z_DEFAULT_COMPRESSION)
|
|
|
|
{
|
|
|
|
/* user has specified a compression level, so tell zlib to use it */
|
|
|
|
char mode_compression[32];
|
|
|
|
|
|
|
|
snprintf(mode_compression, sizeof(mode_compression), "%s%d",
|
|
|
|
mode, compression);
|
|
|
|
fp->compressedfp = gzopen(path, mode_compression);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* don't specify a level, just use the zlib default */
|
|
|
|
fp->compressedfp = gzopen(path, mode);
|
|
|
|
}
|
2015-02-18 17:43:00 +01:00
|
|
|
|
2011-01-23 22:10:15 +01:00
|
|
|
fp->uncompressedfp = NULL;
|
|
|
|
if (fp->compressedfp == NULL)
|
|
|
|
{
|
2015-02-18 17:43:00 +01:00
|
|
|
free_keep_errno(fp);
|
2011-01-23 22:10:15 +01:00
|
|
|
fp = NULL;
|
|
|
|
}
|
|
|
|
#else
|
Unified logging system for command-line programs
This unifies the various ad hoc logging (message printing, error
printing) systems used throughout the command-line programs.
Features:
- Program name is automatically prefixed.
- Message string does not end with newline. This removes a common
source of inconsistencies and omissions.
- Additionally, a final newline is automatically stripped, simplifying
use of PQerrorMessage() etc., another common source of mistakes.
- I converted error message strings to use %m where possible.
- As a result of the above several points, more translatable message
strings can be shared between different components and between
frontends and backend, without gratuitous punctuation or whitespace
differences.
- There is support for setting a "log level". This is not meant to be
user-facing, but can be used internally to implement debug or
verbose modes.
- Lazy argument evaluation, so no significant overhead if logging at
some level is disabled.
- Some color in the messages, similar to gcc and clang. Set
PG_COLOR=auto to try it out. Some colors are predefined, but can be
customized by setting PG_COLORS.
- Common files (common/, fe_utils/, etc.) can handle logging much more
simply by just using one API without worrying too much about the
context of the calling program, requiring callbacks, or having to
pass "progname" around everywhere.
- Some programs called setvbuf() to make sure that stderr is
unbuffered, even on Windows. But not all programs did that. This
is now done centrally.
Soft goals:
- Reduces vertical space use and visual complexity of error reporting
in the source code.
- Encourages more deliberate classification of messages. For example,
in some cases it wasn't clear without analyzing the surrounding code
whether a message was meant as an error or just an info.
- Concepts and terms are vaguely aligned with popular logging
frameworks such as log4j and Python logging.
This is all just about printing stuff out. Nothing affects program
flow (e.g., fatal exits). The uses are just too varied to do that.
Some existing code had wrappers that do some kind of print-and-exit,
and I adapted those.
I tried to keep the output mostly the same, but there is a lot of
historical baggage to unwind and special cases to consider, and I
might not always have succeeded. One significant change is that
pg_rewind used to write all error messages to stdout. That is now
changed to stderr.
Reviewed-by: Donald Dong <xdong@csumb.edu>
Reviewed-by: Arthur Zakirov <a.zakirov@postgrespro.ru>
Discussion: https://www.postgresql.org/message-id/flat/6a609b43-4f57-7348-6480-bd022f924310@2ndquadrant.com
2019-04-01 14:24:37 +02:00
|
|
|
fatal("not built with zlib support");
|
2011-01-23 22:10:15 +01:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
#ifdef HAVE_LIBZ
|
|
|
|
fp->compressedfp = NULL;
|
|
|
|
#endif
|
|
|
|
fp->uncompressedfp = fopen(path, mode);
|
|
|
|
if (fp->uncompressedfp == NULL)
|
|
|
|
{
|
2015-02-18 17:43:00 +01:00
|
|
|
free_keep_errno(fp);
|
2011-01-23 22:10:15 +01:00
|
|
|
fp = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return fp;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
cfread(void *ptr, int size, cfp *fp)
|
|
|
|
{
|
2014-05-06 18:12:18 +02:00
|
|
|
int ret;
|
2014-05-06 02:27:16 +02:00
|
|
|
|
|
|
|
if (size == 0)
|
|
|
|
return 0;
|
|
|
|
|
2011-01-23 22:10:15 +01:00
|
|
|
#ifdef HAVE_LIBZ
|
|
|
|
if (fp->compressedfp)
|
2014-05-06 02:27:16 +02:00
|
|
|
{
|
|
|
|
ret = gzread(fp->compressedfp, ptr, size);
|
|
|
|
if (ret != size && !gzeof(fp->compressedfp))
|
2017-08-03 00:26:26 +02:00
|
|
|
{
|
2017-08-14 23:29:33 +02:00
|
|
|
int errnum;
|
2017-08-03 00:26:26 +02:00
|
|
|
const char *errmsg = gzerror(fp->compressedfp, &errnum);
|
|
|
|
|
Unified logging system for command-line programs
This unifies the various ad hoc logging (message printing, error
printing) systems used throughout the command-line programs.
Features:
- Program name is automatically prefixed.
- Message string does not end with newline. This removes a common
source of inconsistencies and omissions.
- Additionally, a final newline is automatically stripped, simplifying
use of PQerrorMessage() etc., another common source of mistakes.
- I converted error message strings to use %m where possible.
- As a result of the above several points, more translatable message
strings can be shared between different components and between
frontends and backend, without gratuitous punctuation or whitespace
differences.
- There is support for setting a "log level". This is not meant to be
user-facing, but can be used internally to implement debug or
verbose modes.
- Lazy argument evaluation, so no significant overhead if logging at
some level is disabled.
- Some color in the messages, similar to gcc and clang. Set
PG_COLOR=auto to try it out. Some colors are predefined, but can be
customized by setting PG_COLORS.
- Common files (common/, fe_utils/, etc.) can handle logging much more
simply by just using one API without worrying too much about the
context of the calling program, requiring callbacks, or having to
pass "progname" around everywhere.
- Some programs called setvbuf() to make sure that stderr is
unbuffered, even on Windows. But not all programs did that. This
is now done centrally.
Soft goals:
- Reduces vertical space use and visual complexity of error reporting
in the source code.
- Encourages more deliberate classification of messages. For example,
in some cases it wasn't clear without analyzing the surrounding code
whether a message was meant as an error or just an info.
- Concepts and terms are vaguely aligned with popular logging
frameworks such as log4j and Python logging.
This is all just about printing stuff out. Nothing affects program
flow (e.g., fatal exits). The uses are just too varied to do that.
Some existing code had wrappers that do some kind of print-and-exit,
and I adapted those.
I tried to keep the output mostly the same, but there is a lot of
historical baggage to unwind and special cases to consider, and I
might not always have succeeded. One significant change is that
pg_rewind used to write all error messages to stdout. That is now
changed to stderr.
Reviewed-by: Donald Dong <xdong@csumb.edu>
Reviewed-by: Arthur Zakirov <a.zakirov@postgrespro.ru>
Discussion: https://www.postgresql.org/message-id/flat/6a609b43-4f57-7348-6480-bd022f924310@2ndquadrant.com
2019-04-01 14:24:37 +02:00
|
|
|
fatal("could not read from input file: %s",
|
2019-05-22 18:55:34 +02:00
|
|
|
errnum == Z_ERRNO ? strerror(errno) : errmsg);
|
2017-08-03 00:26:26 +02:00
|
|
|
}
|
2014-05-06 02:27:16 +02:00
|
|
|
}
|
2011-01-23 22:10:15 +01:00
|
|
|
else
|
|
|
|
#endif
|
2014-05-06 02:27:16 +02:00
|
|
|
{
|
|
|
|
ret = fread(ptr, 1, size, fp->uncompressedfp);
|
|
|
|
if (ret != size && !feof(fp->uncompressedfp))
|
|
|
|
READ_ERROR_EXIT(fp->uncompressedfp);
|
|
|
|
}
|
|
|
|
return ret;
|
2011-01-23 22:10:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
cfwrite(const void *ptr, int size, cfp *fp)
|
|
|
|
{
|
|
|
|
#ifdef HAVE_LIBZ
|
|
|
|
if (fp->compressedfp)
|
|
|
|
return gzwrite(fp->compressedfp, ptr, size);
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
return fwrite(ptr, 1, size, fp->uncompressedfp);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
cfgetc(cfp *fp)
|
|
|
|
{
|
2014-05-06 18:12:18 +02:00
|
|
|
int ret;
|
2014-05-06 02:27:16 +02:00
|
|
|
|
2011-01-23 22:10:15 +01:00
|
|
|
#ifdef HAVE_LIBZ
|
|
|
|
if (fp->compressedfp)
|
2014-05-06 02:27:16 +02:00
|
|
|
{
|
|
|
|
ret = gzgetc(fp->compressedfp);
|
|
|
|
if (ret == EOF)
|
|
|
|
{
|
|
|
|
if (!gzeof(fp->compressedfp))
|
Unified logging system for command-line programs
This unifies the various ad hoc logging (message printing, error
printing) systems used throughout the command-line programs.
Features:
- Program name is automatically prefixed.
- Message string does not end with newline. This removes a common
source of inconsistencies and omissions.
- Additionally, a final newline is automatically stripped, simplifying
use of PQerrorMessage() etc., another common source of mistakes.
- I converted error message strings to use %m where possible.
- As a result of the above several points, more translatable message
strings can be shared between different components and between
frontends and backend, without gratuitous punctuation or whitespace
differences.
- There is support for setting a "log level". This is not meant to be
user-facing, but can be used internally to implement debug or
verbose modes.
- Lazy argument evaluation, so no significant overhead if logging at
some level is disabled.
- Some color in the messages, similar to gcc and clang. Set
PG_COLOR=auto to try it out. Some colors are predefined, but can be
customized by setting PG_COLORS.
- Common files (common/, fe_utils/, etc.) can handle logging much more
simply by just using one API without worrying too much about the
context of the calling program, requiring callbacks, or having to
pass "progname" around everywhere.
- Some programs called setvbuf() to make sure that stderr is
unbuffered, even on Windows. But not all programs did that. This
is now done centrally.
Soft goals:
- Reduces vertical space use and visual complexity of error reporting
in the source code.
- Encourages more deliberate classification of messages. For example,
in some cases it wasn't clear without analyzing the surrounding code
whether a message was meant as an error or just an info.
- Concepts and terms are vaguely aligned with popular logging
frameworks such as log4j and Python logging.
This is all just about printing stuff out. Nothing affects program
flow (e.g., fatal exits). The uses are just too varied to do that.
Some existing code had wrappers that do some kind of print-and-exit,
and I adapted those.
I tried to keep the output mostly the same, but there is a lot of
historical baggage to unwind and special cases to consider, and I
might not always have succeeded. One significant change is that
pg_rewind used to write all error messages to stdout. That is now
changed to stderr.
Reviewed-by: Donald Dong <xdong@csumb.edu>
Reviewed-by: Arthur Zakirov <a.zakirov@postgrespro.ru>
Discussion: https://www.postgresql.org/message-id/flat/6a609b43-4f57-7348-6480-bd022f924310@2ndquadrant.com
2019-04-01 14:24:37 +02:00
|
|
|
fatal("could not read from input file: %s", strerror(errno));
|
2014-05-06 02:27:16 +02:00
|
|
|
else
|
Unified logging system for command-line programs
This unifies the various ad hoc logging (message printing, error
printing) systems used throughout the command-line programs.
Features:
- Program name is automatically prefixed.
- Message string does not end with newline. This removes a common
source of inconsistencies and omissions.
- Additionally, a final newline is automatically stripped, simplifying
use of PQerrorMessage() etc., another common source of mistakes.
- I converted error message strings to use %m where possible.
- As a result of the above several points, more translatable message
strings can be shared between different components and between
frontends and backend, without gratuitous punctuation or whitespace
differences.
- There is support for setting a "log level". This is not meant to be
user-facing, but can be used internally to implement debug or
verbose modes.
- Lazy argument evaluation, so no significant overhead if logging at
some level is disabled.
- Some color in the messages, similar to gcc and clang. Set
PG_COLOR=auto to try it out. Some colors are predefined, but can be
customized by setting PG_COLORS.
- Common files (common/, fe_utils/, etc.) can handle logging much more
simply by just using one API without worrying too much about the
context of the calling program, requiring callbacks, or having to
pass "progname" around everywhere.
- Some programs called setvbuf() to make sure that stderr is
unbuffered, even on Windows. But not all programs did that. This
is now done centrally.
Soft goals:
- Reduces vertical space use and visual complexity of error reporting
in the source code.
- Encourages more deliberate classification of messages. For example,
in some cases it wasn't clear without analyzing the surrounding code
whether a message was meant as an error or just an info.
- Concepts and terms are vaguely aligned with popular logging
frameworks such as log4j and Python logging.
This is all just about printing stuff out. Nothing affects program
flow (e.g., fatal exits). The uses are just too varied to do that.
Some existing code had wrappers that do some kind of print-and-exit,
and I adapted those.
I tried to keep the output mostly the same, but there is a lot of
historical baggage to unwind and special cases to consider, and I
might not always have succeeded. One significant change is that
pg_rewind used to write all error messages to stdout. That is now
changed to stderr.
Reviewed-by: Donald Dong <xdong@csumb.edu>
Reviewed-by: Arthur Zakirov <a.zakirov@postgrespro.ru>
Discussion: https://www.postgresql.org/message-id/flat/6a609b43-4f57-7348-6480-bd022f924310@2ndquadrant.com
2019-04-01 14:24:37 +02:00
|
|
|
fatal("could not read from input file: end of file");
|
2014-05-06 02:27:16 +02:00
|
|
|
}
|
|
|
|
}
|
2011-01-23 22:10:15 +01:00
|
|
|
else
|
|
|
|
#endif
|
2014-05-06 02:27:16 +02:00
|
|
|
{
|
|
|
|
ret = fgetc(fp->uncompressedfp);
|
|
|
|
if (ret == EOF)
|
|
|
|
READ_ERROR_EXIT(fp->uncompressedfp);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
2011-01-23 22:10:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
char *
|
|
|
|
cfgets(cfp *fp, char *buf, int len)
|
|
|
|
{
|
|
|
|
#ifdef HAVE_LIBZ
|
|
|
|
if (fp->compressedfp)
|
|
|
|
return gzgets(fp->compressedfp, buf, len);
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
return fgets(buf, len, fp->uncompressedfp);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
cfclose(cfp *fp)
|
|
|
|
{
|
2011-04-10 17:42:00 +02:00
|
|
|
int result;
|
2011-01-23 22:10:15 +01:00
|
|
|
|
|
|
|
if (fp == NULL)
|
|
|
|
{
|
|
|
|
errno = EBADF;
|
|
|
|
return EOF;
|
|
|
|
}
|
|
|
|
#ifdef HAVE_LIBZ
|
|
|
|
if (fp->compressedfp)
|
|
|
|
{
|
|
|
|
result = gzclose(fp->compressedfp);
|
|
|
|
fp->compressedfp = NULL;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
result = fclose(fp->uncompressedfp);
|
|
|
|
fp->uncompressedfp = NULL;
|
|
|
|
}
|
2015-02-18 17:43:00 +01:00
|
|
|
free_keep_errno(fp);
|
2011-01-23 22:10:15 +01:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
cfeof(cfp *fp)
|
|
|
|
{
|
|
|
|
#ifdef HAVE_LIBZ
|
|
|
|
if (fp->compressedfp)
|
|
|
|
return gzeof(fp->compressedfp);
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
return feof(fp->uncompressedfp);
|
|
|
|
}
|
|
|
|
|
2017-08-03 00:26:26 +02:00
|
|
|
const char *
|
|
|
|
get_cfp_error(cfp *fp)
|
|
|
|
{
|
|
|
|
#ifdef HAVE_LIBZ
|
|
|
|
if (fp->compressedfp)
|
|
|
|
{
|
|
|
|
int errnum;
|
|
|
|
const char *errmsg = gzerror(fp->compressedfp, &errnum);
|
|
|
|
|
|
|
|
if (errnum != Z_ERRNO)
|
|
|
|
return errmsg;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
return strerror(errno);
|
|
|
|
}
|
|
|
|
|
2011-01-23 22:10:15 +01:00
|
|
|
#ifdef HAVE_LIBZ
|
|
|
|
static int
|
|
|
|
hasSuffix(const char *filename, const char *suffix)
|
|
|
|
{
|
2011-04-10 17:42:00 +02:00
|
|
|
int filenamelen = strlen(filename);
|
|
|
|
int suffixlen = strlen(suffix);
|
2011-01-23 22:10:15 +01:00
|
|
|
|
|
|
|
if (filenamelen < suffixlen)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
return memcmp(&filename[filenamelen - suffixlen],
|
2011-04-10 17:42:00 +02:00
|
|
|
suffix,
|
|
|
|
suffixlen) == 0;
|
2011-01-23 22:10:15 +01:00
|
|
|
}
|
2011-04-10 17:42:00 +02:00
|
|
|
|
2011-01-23 22:10:15 +01:00
|
|
|
#endif
|