PostgreSQL Source Code  git master
bbstreamer_gzip.c File Reference
#include "postgres_fe.h"
#include <unistd.h>
#include "bbstreamer.h"
#include "common/logging.h"
#include "common/file_perm.h"
#include "common/string.h"
Include dependency graph for bbstreamer_gzip.c:

Go to the source code of this file.

Functions

bbstreamerbbstreamer_gzip_writer_new (char *pathname, FILE *file, pg_compress_specification *compress)
 
bbstreamerbbstreamer_gzip_decompressor_new (bbstreamer *next)
 

Function Documentation

◆ bbstreamer_gzip_decompressor_new()

bbstreamer* bbstreamer_gzip_decompressor_new ( bbstreamer next)

Definition at line 214 of file bbstreamer_gzip.c.

215 {
216 #ifdef HAVE_LIBZ
217  bbstreamer_gzip_decompressor *streamer;
218  z_stream *zs;
219 
220  Assert(next != NULL);
221 
222  streamer = palloc0(sizeof(bbstreamer_gzip_decompressor));
223  *((const bbstreamer_ops **) &streamer->base.bbs_ops) =
224  &bbstreamer_gzip_decompressor_ops;
225 
226  streamer->base.bbs_next = next;
227  initStringInfo(&streamer->base.bbs_buffer);
228 
229  /* Initialize internal stream state for decompression */
230  zs = &streamer->zstream;
231  zs->zalloc = gzip_palloc;
232  zs->zfree = gzip_pfree;
233  zs->next_out = (uint8 *) streamer->base.bbs_buffer.data;
234  zs->avail_out = streamer->base.bbs_buffer.maxlen;
235 
236  /*
237  * Data compression was initialized using deflateInit2 to request a gzip
238  * header. Similarly, we are using inflateInit2 to initialize data
239  * decompression.
240  *
241  * Per the documentation for inflateInit2, the second argument is
242  * "windowBits" and its value must be greater than or equal to the value
243  * provided while compressing the data, so we are using the maximum
244  * possible value for safety.
245  */
246  if (inflateInit2(zs, 15 + 16) != Z_OK)
247  pg_fatal("could not initialize compression library");
248 
249  return &streamer->base;
250 #else
251  pg_fatal("this build does not support gzip compression");
252  return NULL; /* keep compiler quiet */
253 #endif
254 }
static int32 next
Definition: blutils.c:219
unsigned char uint8
Definition: c.h:439
Assert(fmt[strlen(fmt) - 1] !='\n')
void * palloc0(Size size)
Definition: mcxt.c:1099
#define pg_fatal(...)
void initStringInfo(StringInfo str)
Definition: stringinfo.c:59

References Assert(), _z_stream::avail_out, if(), initStringInfo(), next, _z_stream::next_out, palloc0(), and pg_fatal.

Referenced by CreateBackupStreamer().

◆ bbstreamer_gzip_writer_new()

bbstreamer* bbstreamer_gzip_writer_new ( char *  pathname,
FILE *  file,
pg_compress_specification compress 
)

Definition at line 79 of file bbstreamer_gzip.c.

81 {
82 #ifdef HAVE_LIBZ
83  bbstreamer_gzip_writer *streamer;
84 
85  streamer = palloc0(sizeof(bbstreamer_gzip_writer));
86  *((const bbstreamer_ops **) &streamer->base.bbs_ops) =
87  &bbstreamer_gzip_writer_ops;
88 
89  streamer->pathname = pstrdup(pathname);
90 
91  if (file == NULL)
92  {
93  streamer->gzfile = gzopen(pathname, "wb");
94  if (streamer->gzfile == NULL)
95  pg_fatal("could not create compressed file \"%s\": %m",
96  pathname);
97  }
98  else
99  {
100  int fd = dup(fileno(file));
101 
102  if (fd < 0)
103  pg_fatal("could not duplicate stdout: %m");
104 
105  streamer->gzfile = gzdopen(fd, "wb");
106  if (streamer->gzfile == NULL)
107  pg_fatal("could not open output file: %m");
108  }
109 
110  if ((compress->options & PG_COMPRESSION_OPTION_LEVEL) != 0 &&
111  gzsetparams(streamer->gzfile, compress->level,
112  Z_DEFAULT_STRATEGY) != Z_OK)
113  pg_fatal("could not set compression level %d: %s",
114  compress->level, get_gz_error(streamer->gzfile));
115 
116  return &streamer->base;
117 #else
118  pg_fatal("this build does not support gzip compression");
119  return NULL; /* keep compiler quiet */
120 #endif
121 }
#define PG_COMPRESSION_OPTION_LEVEL
Definition: compression.h:25
char * pstrdup(const char *in)
Definition: mcxt.c:1305
static int fd(const char *x, int i)
Definition: preproc-init.c:105

References fd(), pg_compress_specification::level, pg_compress_specification::options, palloc0(), PG_COMPRESSION_OPTION_LEVEL, pg_fatal, and pstrdup().

Referenced by CreateBackupStreamer().