PostgreSQL Source Code git master
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
astreamer_gzip.c File Reference
#include "postgres_fe.h"
#include <unistd.h>
#include "common/logging.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 236 of file astreamer_gzip.c.

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

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 99 of file astreamer_gzip.c.

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