PostgreSQL Source Code git master
Loading...
Searching...
No Matches
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
240 z_stream *zs;
241
242 Assert(next != NULL);
243
245 *((const astreamer_ops **) &streamer->base.bbs_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:225
uint8_t uint8
Definition c.h:544
#define Assert(condition)
Definition c.h:873
#define palloc0_object(type)
Definition fe_memutils.h:75
#define pg_fatal(...)
static int fb(int x)
void initStringInfo(StringInfo str)
Definition stringinfo.c:97

References Assert, fb(), initStringInfo(), next, palloc0_object, 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
106 *((const astreamer_ops **) &streamer->base.bbs_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:1781
static int fd(const char *x, int i)

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

Referenced by CreateBackupStreamer().