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 "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)
 
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 118 of file compress_io.c.

120 {
121  CompressorState *cs;
122 
123  cs = (CompressorState *) pg_malloc0(sizeof(CompressorState));
124  cs->readF = readF;
125  cs->writeF = writeF;
126 
127  if (compression_spec.algorithm == PG_COMPRESSION_NONE)
128  InitCompressorNone(cs, compression_spec);
129  else if (compression_spec.algorithm == PG_COMPRESSION_GZIP)
130  InitCompressorGzip(cs, compression_spec);
131  else if (compression_spec.algorithm == PG_COMPRESSION_LZ4)
132  InitCompressorLZ4(cs, compression_spec);
133 
134  return cs;
135 }
void InitCompressorGzip(CompressorState *cs, const pg_compress_specification compression_spec)
void InitCompressorLZ4(CompressorState *cs, const pg_compress_specification compression_spec)
Definition: compress_lz4.c:765
void InitCompressorNone(CompressorState *cs, const pg_compress_specification compression_spec)
Definition: compress_none.c:66
@ PG_COMPRESSION_GZIP
Definition: compression.h:24
@ PG_COMPRESSION_LZ4
Definition: compression.h:25
@ PG_COMPRESSION_NONE
Definition: compression.h:23
void * pg_malloc0(size_t size)
Definition: fe_memutils.c:53
ReadFunc readF
Definition: compress_io.h:66
WriteFunc writeF
Definition: compress_io.h:71
pg_compress_algorithm algorithm
Definition: compression.h:33

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

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

◆ EndCompressFileHandle()

bool EndCompressFileHandle ( CompressFileHandle CFH)

Definition at line 281 of file compress_io.c.

282 {
283  bool ret = false;
284 
285  if (CFH->private_data)
286  ret = CFH->close_func(CFH);
287 
288  free_keep_errno(CFH);
289 
290  return ret;
291 }
static void free_keep_errno(void *p)
Definition: compress_io.c:171
bool(* close_func)(CompressFileHandle *CFH)
Definition: compress_io.h:169

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 141 of file compress_io.c.

142 {
143  cs->end(AH, cs);
144  pg_free(cs);
145 }
void pg_free(void *ptr)
Definition: fe_memutils.c:105
void(* end)(ArchiveHandle *AH, CompressorState *cs)
Definition: compress_io.h:61

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 171 of file compress_io.c.

172 {
173  int save_errno = errno;
174 
175  free(p);
176  errno = save_errno;
177 }
#define free(a)
Definition: header.h:65

References free.

Referenced by EndCompressFileHandle(), and InitDiscoverCompressFileHandle().

◆ hasSuffix()

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

Definition at line 156 of file compress_io.c.

157 {
158  int filenamelen = strlen(filename);
159  int suffixlen = strlen(suffix);
160 
161  if (filenamelen < suffixlen)
162  return 0;
163 
164  return memcmp(&filename[filenamelen - suffixlen],
165  suffix,
166  suffixlen) == 0;
167 }
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 187 of file compress_io.c.

188 {
189  CompressFileHandle *CFH;
190 
191  CFH = pg_malloc0(sizeof(CompressFileHandle));
192 
193  if (compression_spec.algorithm == PG_COMPRESSION_NONE)
194  InitCompressFileHandleNone(CFH, compression_spec);
195  else if (compression_spec.algorithm == PG_COMPRESSION_GZIP)
196  InitCompressFileHandleGzip(CFH, compression_spec);
197  else if (compression_spec.algorithm == PG_COMPRESSION_LZ4)
198  InitCompressFileHandleLZ4(CFH, compression_spec);
199 
200  return CFH;
201 }
void InitCompressFileHandleGzip(CompressFileHandle *CFH, const pg_compress_specification compression_spec)
void InitCompressFileHandleLZ4(CompressFileHandle *CFH, const pg_compress_specification compression_spec)
Definition: compress_lz4.c:772
void InitCompressFileHandleNone(CompressFileHandle *CFH, const pg_compress_specification compression_spec)

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

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

◆ InitDiscoverCompressFileHandle()

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

Definition at line 217 of file compress_io.c.

218 {
219  CompressFileHandle *CFH = NULL;
220  struct stat st;
221  char *fname;
222  pg_compress_specification compression_spec = {0};
223 
224  compression_spec.algorithm = PG_COMPRESSION_NONE;
225 
226  Assert(strcmp(mode, PG_BINARY_R) == 0);
227 
228  fname = strdup(path);
229 
230  if (hasSuffix(fname, ".gz"))
231  compression_spec.algorithm = PG_COMPRESSION_GZIP;
232  else
233  {
234  bool exists;
235 
236  exists = (stat(path, &st) == 0);
237  /* avoid unused warning if it is not built with compression */
238  if (exists)
239  compression_spec.algorithm = PG_COMPRESSION_NONE;
240 #ifdef HAVE_LIBZ
241  if (!exists)
242  {
243  free_keep_errno(fname);
244  fname = psprintf("%s.gz", path);
245  exists = (stat(fname, &st) == 0);
246 
247  if (exists)
248  compression_spec.algorithm = PG_COMPRESSION_GZIP;
249  }
250 #endif
251 #ifdef USE_LZ4
252  if (!exists)
253  {
254  free_keep_errno(fname);
255  fname = psprintf("%s.lz4", path);
256  exists = (stat(fname, &st) == 0);
257 
258  if (exists)
259  compression_spec.algorithm = PG_COMPRESSION_LZ4;
260  }
261 #endif
262  }
263 
264  CFH = InitCompressFileHandle(compression_spec);
265  if (!CFH->open_func(fname, -1, mode, CFH))
266  {
267  free_keep_errno(CFH);
268  CFH = NULL;
269  }
270  free_keep_errno(fname);
271 
272  return CFH;
273 }
#define PG_BINARY_R
Definition: c.h:1262
static int hasSuffix(const char *filename, const char *suffix)
Definition: compress_io.c:156
CompressFileHandle * InitCompressFileHandle(const pg_compress_specification compression_spec)
Definition: compress_io.c:187
Assert(fmt[strlen(fmt) - 1] !='\n')
static PgChecksumMode mode
Definition: pg_checksums.c:65
char * psprintf(const char *fmt,...)
Definition: psprintf.c:46
bool(* open_func)(const char *path, int fd, const char *mode, CompressFileHandle *CFH)
Definition: compress_io.h:105
#define stat
Definition: win32_port.h:286

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

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

◆ supports_compression()

char* supports_compression ( const pg_compress_specification  compression_spec)

Definition at line 86 of file compress_io.c.

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

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

Referenced by ReadHead(), and RestoreArchive().