PostgreSQL Source Code  git master
bbstreamer_gzip.c File Reference
#include "postgres_fe.h"
#include <unistd.h>
#include "bbstreamer.h"
#include "common/file_perm.h"
#include "common/logging.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 212 of file bbstreamer_gzip.c.

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

References Assert, if(), initStringInfo(), next, 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 (gzsetparams(streamer->gzfile, compress->level, Z_DEFAULT_STRATEGY) != Z_OK)
111  pg_fatal("could not set compression level %d: %s",
112  compress->level, get_gz_error(streamer->gzfile));
113 
114  return &streamer->base;
115 #else
116  pg_fatal("this build does not support compression with %s", "gzip");
117  return NULL; /* keep compiler quiet */
118 #endif
119 }
char * pstrdup(const char *in)
Definition: mcxt.c:1695
static int fd(const char *x, int i)
Definition: preproc-init.c:105

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

Referenced by CreateBackupStreamer().