PostgreSQL Source Code  git master
compress_io.c File Reference
#include "postgres_fe.h"
#include <sys/stat.h>
#include <unistd.h>
#include "compress_gzip.h"
#include "compress_io.h"
#include "compress_lz4.h"
#include "compress_none.h"
#include "compress_zstd.h"
#include "pg_backup_utils.h"
Include dependency graph for compress_io.c:

Go to the source code of this file.

Functions

char * supports_compression (const pg_compress_specification compression_spec)
 
CompressorStateAllocateCompressor (const pg_compress_specification compression_spec, ReadFunc readF, WriteFunc writeF)
 
void EndCompressor (ArchiveHandle *AH, CompressorState *cs)
 
static int hasSuffix (const char *filename, const char *suffix)
 
static void free_keep_errno (void *p)
 
CompressFileHandleInitCompressFileHandle (const pg_compress_specification compression_spec)
 
static bool check_compressed_file (const char *path, char **fname, char *ext)
 
CompressFileHandleInitDiscoverCompressFileHandle (const char *path, const char *mode)
 
bool EndCompressFileHandle (CompressFileHandle *CFH)
 

Function Documentation

◆ AllocateCompressor()

CompressorState* AllocateCompressor ( const pg_compress_specification  compression_spec,
ReadFunc  readF,
WriteFunc  writeF 
)

Definition at line 124 of file compress_io.c.

126 {
127  CompressorState *cs;
128 
129  cs = (CompressorState *) pg_malloc0(sizeof(CompressorState));
130  cs->readF = readF;
131  cs->writeF = writeF;
132 
133  if (compression_spec.algorithm == PG_COMPRESSION_NONE)
134  InitCompressorNone(cs, compression_spec);
135  else if (compression_spec.algorithm == PG_COMPRESSION_GZIP)
136  InitCompressorGzip(cs, compression_spec);
137  else if (compression_spec.algorithm == PG_COMPRESSION_LZ4)
138  InitCompressorLZ4(cs, compression_spec);
139  else if (compression_spec.algorithm == PG_COMPRESSION_ZSTD)
140  InitCompressorZstd(cs, compression_spec);
141 
142  return cs;
143 }
void InitCompressorGzip(CompressorState *cs, const pg_compress_specification compression_spec)
void InitCompressorLZ4(CompressorState *cs, const pg_compress_specification compression_spec)
Definition: compress_lz4.c:777
void InitCompressorNone(CompressorState *cs, const pg_compress_specification compression_spec)
Definition: compress_none.c:66
void InitCompressorZstd(CompressorState *cs, const pg_compress_specification compression_spec)
Definition: compress_zstd.c:23
@ PG_COMPRESSION_GZIP
Definition: compression.h:24
@ PG_COMPRESSION_LZ4
Definition: compression.h:25
@ PG_COMPRESSION_NONE
Definition: compression.h:23
@ PG_COMPRESSION_ZSTD
Definition: compression.h:26
void * pg_malloc0(size_t size)
Definition: fe_memutils.c:53
ReadFunc readF
Definition: compress_io.h:72
WriteFunc writeF
Definition: compress_io.h:77
pg_compress_algorithm algorithm
Definition: compression.h:34

References pg_compress_specification::algorithm, InitCompressorGzip(), InitCompressorLZ4(), InitCompressorNone(), InitCompressorZstd(), PG_COMPRESSION_GZIP, PG_COMPRESSION_LZ4, PG_COMPRESSION_NONE, PG_COMPRESSION_ZSTD, pg_malloc0(), CompressorState::readF, and CompressorState::writeF.

Referenced by _PrintData(), _StartData(), and _StartLO().

◆ check_compressed_file()

static bool check_compressed_file ( const char *  path,
char **  fname,
char *  ext 
)
static

Definition at line 220 of file compress_io.c.

221 {
222  free_keep_errno(*fname);
223  *fname = psprintf("%s.%s", path, ext);
224  return (access(*fname, F_OK) == 0);
225 }
static void free_keep_errno(void *p)
Definition: compress_io.c:179
short access
Definition: preproc-type.c:36
char * psprintf(const char *fmt,...)
Definition: psprintf.c:46

References free_keep_errno(), and psprintf().

Referenced by InitDiscoverCompressFileHandle().

◆ EndCompressFileHandle()

bool EndCompressFileHandle ( CompressFileHandle CFH)

Definition at line 289 of file compress_io.c.

290 {
291  bool ret = false;
292 
293  if (CFH->private_data)
294  ret = CFH->close_func(CFH);
295 
296  free_keep_errno(CFH);
297 
298  return ret;
299 }
bool(* close_func)(CompressFileHandle *CFH)
Definition: compress_io.h:175

References CompressFileHandle::close_func, free_keep_errno(), and CompressFileHandle::private_data.

Referenced by _CloseArchive(), _EndData(), _EndLO(), _EndLOs(), _LoadLOs(), _PrintFileData(), CloseArchive(), InitArchiveFmt_Directory(), and RestoreOutput().

◆ EndCompressor()

void EndCompressor ( ArchiveHandle AH,
CompressorState cs 
)

Definition at line 149 of file compress_io.c.

150 {
151  cs->end(AH, cs);
152  pg_free(cs);
153 }
void pg_free(void *ptr)
Definition: fe_memutils.c:105
void(* end)(ArchiveHandle *AH, CompressorState *cs)
Definition: compress_io.h:67

References CompressorState::end, and pg_free().

Referenced by _EndData(), _EndLO(), and _PrintData().

◆ free_keep_errno()

static void free_keep_errno ( void *  p)
static

Definition at line 179 of file compress_io.c.

180 {
181  int save_errno = errno;
182 
183  free(p);
184  errno = save_errno;
185 }
#define free(a)
Definition: header.h:65

References free.

Referenced by check_compressed_file(), EndCompressFileHandle(), and InitDiscoverCompressFileHandle().

◆ hasSuffix()

static int hasSuffix ( const char *  filename,
const char *  suffix 
)
static

Definition at line 164 of file compress_io.c.

165 {
166  int filenamelen = strlen(filename);
167  int suffixlen = strlen(suffix);
168 
169  if (filenamelen < suffixlen)
170  return 0;
171 
172  return memcmp(&filename[filenamelen - suffixlen],
173  suffix,
174  suffixlen) == 0;
175 }
static char * filename
Definition: pg_dumpall.c:119

References filename.

Referenced by InitDiscoverCompressFileHandle().

◆ InitCompressFileHandle()

CompressFileHandle* InitCompressFileHandle ( const pg_compress_specification  compression_spec)

Definition at line 195 of file compress_io.c.

196 {
197  CompressFileHandle *CFH;
198 
199  CFH = pg_malloc0(sizeof(CompressFileHandle));
200 
201  if (compression_spec.algorithm == PG_COMPRESSION_NONE)
202  InitCompressFileHandleNone(CFH, compression_spec);
203  else if (compression_spec.algorithm == PG_COMPRESSION_GZIP)
204  InitCompressFileHandleGzip(CFH, compression_spec);
205  else if (compression_spec.algorithm == PG_COMPRESSION_LZ4)
206  InitCompressFileHandleLZ4(CFH, compression_spec);
207  else if (compression_spec.algorithm == PG_COMPRESSION_ZSTD)
208  InitCompressFileHandleZstd(CFH, compression_spec);
209 
210  return CFH;
211 }
void InitCompressFileHandleGzip(CompressFileHandle *CFH, const pg_compress_specification compression_spec)
void InitCompressFileHandleLZ4(CompressFileHandle *CFH, const pg_compress_specification compression_spec)
Definition: compress_lz4.c:784
void InitCompressFileHandleNone(CompressFileHandle *CFH, const pg_compress_specification compression_spec)
void InitCompressFileHandleZstd(CompressFileHandle *CFH, const pg_compress_specification compression_spec)
Definition: compress_zstd.c:29

References pg_compress_specification::algorithm, InitCompressFileHandleGzip(), InitCompressFileHandleLZ4(), InitCompressFileHandleNone(), InitCompressFileHandleZstd(), PG_COMPRESSION_GZIP, PG_COMPRESSION_LZ4, PG_COMPRESSION_NONE, PG_COMPRESSION_ZSTD, and pg_malloc0().

Referenced by _allocAH(), _CloseArchive(), _StartData(), _StartLO(), _StartLOs(), InitDiscoverCompressFileHandle(), and SetOutput().

◆ InitDiscoverCompressFileHandle()

CompressFileHandle* InitDiscoverCompressFileHandle ( const char *  path,
const char *  mode 
)

Definition at line 241 of file compress_io.c.

242 {
243  CompressFileHandle *CFH = NULL;
244  struct stat st;
245  char *fname;
246  pg_compress_specification compression_spec = {0};
247 
248  compression_spec.algorithm = PG_COMPRESSION_NONE;
249 
250  Assert(strcmp(mode, PG_BINARY_R) == 0);
251 
252  fname = pg_strdup(path);
253 
254  if (hasSuffix(fname, ".gz"))
255  compression_spec.algorithm = PG_COMPRESSION_GZIP;
256  else if (hasSuffix(fname, ".lz4"))
257  compression_spec.algorithm = PG_COMPRESSION_LZ4;
258  else if (hasSuffix(fname, ".zst"))
259  compression_spec.algorithm = PG_COMPRESSION_ZSTD;
260  else
261  {
262  if (stat(path, &st) == 0)
263  compression_spec.algorithm = PG_COMPRESSION_NONE;
264  else if (check_compressed_file(path, &fname, "gz"))
265  compression_spec.algorithm = PG_COMPRESSION_GZIP;
266  else if (check_compressed_file(path, &fname, "lz4"))
267  compression_spec.algorithm = PG_COMPRESSION_LZ4;
268  else if (check_compressed_file(path, &fname, "zst"))
269  compression_spec.algorithm = PG_COMPRESSION_ZSTD;
270  }
271 
272  CFH = InitCompressFileHandle(compression_spec);
273  if (!CFH->open_func(fname, -1, mode, CFH))
274  {
275  free_keep_errno(CFH);
276  CFH = NULL;
277  }
278  free_keep_errno(fname);
279 
280  return CFH;
281 }
#define PG_BINARY_R
Definition: c.h:1275
#define Assert(condition)
Definition: c.h:858
static int hasSuffix(const char *filename, const char *suffix)
Definition: compress_io.c:164
CompressFileHandle * InitCompressFileHandle(const pg_compress_specification compression_spec)
Definition: compress_io.c:195
static bool check_compressed_file(const char *path, char **fname, char *ext)
Definition: compress_io.c:220
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
static PgChecksumMode mode
Definition: pg_checksums.c:56
bool(* open_func)(const char *path, int fd, const char *mode, CompressFileHandle *CFH)
Definition: compress_io.h:111
#define stat
Definition: win32_port.h:284

References pg_compress_specification::algorithm, Assert, check_compressed_file(), free_keep_errno(), hasSuffix(), InitCompressFileHandle(), mode, CompressFileHandle::open_func, PG_BINARY_R, PG_COMPRESSION_GZIP, PG_COMPRESSION_LZ4, PG_COMPRESSION_NONE, PG_COMPRESSION_ZSTD, pg_strdup(), and stat.

Referenced by _LoadLOs(), _PrintFileData(), and InitArchiveFmt_Directory().

◆ supports_compression()

char* supports_compression ( const pg_compress_specification  compression_spec)

Definition at line 88 of file compress_io.c.

89 {
90  const pg_compress_algorithm algorithm = compression_spec.algorithm;
91  bool supported = false;
92 
93  if (algorithm == PG_COMPRESSION_NONE)
94  supported = true;
95 #ifdef HAVE_LIBZ
96  if (algorithm == PG_COMPRESSION_GZIP)
97  supported = true;
98 #endif
99 #ifdef USE_LZ4
100  if (algorithm == PG_COMPRESSION_LZ4)
101  supported = true;
102 #endif
103 #ifdef USE_ZSTD
104  if (algorithm == PG_COMPRESSION_ZSTD)
105  supported = true;
106 #endif
107 
108  if (!supported)
109  return psprintf(_("this build does not support compression with %s"),
110  get_compress_algorithm_name(algorithm));
111 
112  return NULL;
113 }
const char * get_compress_algorithm_name(pg_compress_algorithm algorithm)
Definition: compression.c:69
pg_compress_algorithm
Definition: compression.h:22
#define _(x)
Definition: elog.c:90

References _, pg_compress_specification::algorithm, get_compress_algorithm_name(), PG_COMPRESSION_GZIP, PG_COMPRESSION_LZ4, PG_COMPRESSION_NONE, PG_COMPRESSION_ZSTD, and psprintf().

Referenced by main(), ReadHead(), and RestoreArchive().