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

Go to the source code of this file.

Functions

astreamerastreamer_gzip_writer_new (char *pathname, FILE *file, pg_compress_specification *compress)
 
astreamerastreamer_gzip_decompressor_new (astreamer *next)
 

Function Documentation

◆ astreamer_gzip_decompressor_new()

astreamer* astreamer_gzip_decompressor_new ( astreamer next)

Definition at line 238 of file astreamer_gzip.c.

239 {
240 #ifdef HAVE_LIBZ
241  astreamer_gzip_decompressor *streamer;
242  z_stream *zs;
243 
244  Assert(next != NULL);
245 
246  streamer = palloc0(sizeof(astreamer_gzip_decompressor));
247  *((const astreamer_ops **) &streamer->base.bbs_ops) =
248  &astreamer_gzip_decompressor_ops;
249 
250  streamer->base.bbs_next = next;
251  initStringInfo(&streamer->base.bbs_buffer);
252 
253  /* Initialize internal stream state for decompression */
254  zs = &streamer->zstream;
255  zs->zalloc = gzip_palloc;
256  zs->zfree = gzip_pfree;
257  zs->next_out = (uint8 *) streamer->base.bbs_buffer.data;
258  zs->avail_out = streamer->base.bbs_buffer.maxlen;
259 
260  /*
261  * Data compression was initialized using deflateInit2 to request a gzip
262  * header. Similarly, we are using inflateInit2 to initialize data
263  * decompression.
264  *
265  * Per the documentation for inflateInit2, the second argument is
266  * "windowBits" and its value must be greater than or equal to the value
267  * provided while compressing the data, so we are using the maximum
268  * possible value for safety.
269  */
270  if (inflateInit2(zs, 15 + 16) != Z_OK)
271  pg_fatal("could not initialize compression library");
272 
273  return &streamer->base;
274 #else
275  pg_fatal("this build does not support compression with %s", "gzip");
276  return NULL; /* keep compiler quiet */
277 #endif
278 }
static int32 next
Definition: blutils.c:222
#define Assert(condition)
Definition: c.h:849
unsigned char uint8
Definition: c.h:504
if(TABLE==NULL||TABLE_index==NULL)
Definition: isn.c:77
void * palloc0(Size size)
Definition: mcxt.c:1347
#define pg_fatal(...)
void initStringInfo(StringInfo str)
Definition: stringinfo.c:59

References Assert, if(), initStringInfo(), next, palloc0(), and pg_fatal.

Referenced by create_archive_verifier(), and CreateBackupStreamer().

◆ astreamer_gzip_writer_new()

astreamer* astreamer_gzip_writer_new ( char *  pathname,
FILE *  file,
pg_compress_specification compress 
)

Definition at line 101 of file astreamer_gzip.c.

103 {
104 #ifdef HAVE_LIBZ
105  astreamer_gzip_writer *streamer;
106 
107  streamer = palloc0(sizeof(astreamer_gzip_writer));
108  *((const astreamer_ops **) &streamer->base.bbs_ops) =
109  &astreamer_gzip_writer_ops;
110 
111  streamer->pathname = pstrdup(pathname);
112 
113  if (file == NULL)
114  {
115  streamer->gzfile = gzopen(pathname, "wb");
116  if (streamer->gzfile == NULL)
117  pg_fatal("could not create compressed file \"%s\": %m",
118  pathname);
119  }
120  else
121  {
122  /*
123  * We must dup the file handle so that gzclose doesn't break the
124  * caller's FILE. See comment for astreamer_gzip_writer_finalize.
125  */
126  int fd = dup(fileno(file));
127 
128  if (fd < 0)
129  pg_fatal("could not duplicate stdout: %m");
130 
131  streamer->gzfile = gzdopen(fd, "wb");
132  if (streamer->gzfile == NULL)
133  pg_fatal("could not open output file: %m");
134  }
135 
136  if (gzsetparams(streamer->gzfile, compress->level, Z_DEFAULT_STRATEGY) != Z_OK)
137  pg_fatal("could not set compression level %d: %s",
138  compress->level, get_gz_error(streamer->gzfile));
139 
140  return &streamer->base;
141 #else
142  pg_fatal("this build does not support compression with %s", "gzip");
143  return NULL; /* keep compiler quiet */
144 #endif
145 }
char * pstrdup(const char *in)
Definition: mcxt.c:1696
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().