PostgreSQL Source Code  git master
compress_io.c File Reference
#include "postgres_fe.h"
#include "compress_io.h"
#include "pg_backup_utils.h"
Include dependency graph for compress_io.c:

Go to the source code of this file.

Data Structures

struct  CompressorState
 
struct  cfp
 

Functions

static void ReadDataFromArchiveNone (ArchiveHandle *AH, ReadFunc readF)
 
static void WriteDataToArchiveNone (ArchiveHandle *AH, CompressorState *cs, const char *data, size_t dLen)
 
CompressorStateAllocateCompressor (const pg_compress_specification compression_spec, WriteFunc writeF)
 
void ReadDataFromArchive (ArchiveHandle *AH, const pg_compress_specification compression_spec, ReadFunc readF)
 
void WriteDataToArchive (ArchiveHandle *AH, CompressorState *cs, const void *data, size_t dLen)
 
void EndCompressor (ArchiveHandle *AH, CompressorState *cs)
 
static void free_keep_errno (void *p)
 
cfpcfopen_read (const char *path, const char *mode)
 
cfpcfopen_write (const char *path, const char *mode, const pg_compress_specification compression_spec)
 
cfpcfopen (const char *path, const char *mode, const pg_compress_specification compression_spec)
 
int cfread (void *ptr, int size, cfp *fp)
 
int cfwrite (const void *ptr, int size, cfp *fp)
 
int cfgetc (cfp *fp)
 
char * cfgets (cfp *fp, char *buf, int len)
 
int cfclose (cfp *fp)
 
int cfeof (cfp *fp)
 
const char * get_cfp_error (cfp *fp)
 

Function Documentation

◆ AllocateCompressor()

CompressorState* AllocateCompressor ( const pg_compress_specification  compression_spec,
WriteFunc  writeF 
)

Definition at line 97 of file compress_io.c.

99 {
100  CompressorState *cs;
101 
102 #ifndef HAVE_LIBZ
103  if (compression_spec.algorithm == PG_COMPRESSION_GZIP)
104  pg_fatal("not built with zlib support");
105 #endif
106 
107  cs = (CompressorState *) pg_malloc0(sizeof(CompressorState));
108  cs->writeF = writeF;
109  cs->compression_spec = compression_spec;
110 
111  /*
112  * Perform compression algorithm specific initialization.
113  */
114 #ifdef HAVE_LIBZ
116  InitCompressorZlib(cs, cs->compression_spec.level);
117 #endif
118 
119  return cs;
120 }
@ PG_COMPRESSION_GZIP
Definition: compression.h:20
void * pg_malloc0(size_t size)
Definition: fe_memutils.c:53
#define pg_fatal(...)
pg_compress_specification compression_spec
Definition: compress_io.c:67
WriteFunc writeF
Definition: compress_io.c:68
pg_compress_algorithm algorithm
Definition: compression.h:29

References pg_compress_specification::algorithm, CompressorState::compression_spec, pg_compress_specification::level, PG_COMPRESSION_GZIP, pg_fatal, pg_malloc0(), and CompressorState::writeF.

Referenced by _StartData(), and _StartLO().

◆ cfclose()

int cfclose ( cfp fp)

Definition at line 631 of file compress_io.c.

632 {
633  int result;
634 
635  if (fp == NULL)
636  {
637  errno = EBADF;
638  return EOF;
639  }
640 #ifdef HAVE_LIBZ
641  if (fp->compressedfp)
642  {
643  result = gzclose(fp->compressedfp);
644  fp->compressedfp = NULL;
645  }
646  else
647 #endif
648  {
649  result = fclose(fp->uncompressedfp);
650  fp->uncompressedfp = NULL;
651  }
652  free_keep_errno(fp);
653 
654  return result;
655 }
static void free_keep_errno(void *p)
Definition: compress_io.c:406
FILE * uncompressedfp
Definition: compress_io.c:394

References free_keep_errno(), and cfp::uncompressedfp.

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

◆ cfeof()

int cfeof ( cfp fp)

Definition at line 658 of file compress_io.c.

659 {
660 #ifdef HAVE_LIBZ
661  if (fp->compressedfp)
662  return gzeof(fp->compressedfp);
663  else
664 #endif
665  return feof(fp->uncompressedfp);
666 }

References cfp::uncompressedfp.

Referenced by _LoadLOs().

◆ cfgetc()

int cfgetc ( cfp fp)

Definition at line 592 of file compress_io.c.

593 {
594  int ret;
595 
596 #ifdef HAVE_LIBZ
597  if (fp->compressedfp)
598  {
599  ret = gzgetc(fp->compressedfp);
600  if (ret == EOF)
601  {
602  if (!gzeof(fp->compressedfp))
603  pg_fatal("could not read from input file: %s", strerror(errno));
604  else
605  pg_fatal("could not read from input file: end of file");
606  }
607  }
608  else
609 #endif
610  {
611  ret = fgetc(fp->uncompressedfp);
612  if (ret == EOF)
614  }
615 
616  return ret;
617 }
#define READ_ERROR_EXIT(fd)
#define strerror
Definition: port.h:251

References pg_fatal, READ_ERROR_EXIT, strerror, and cfp::uncompressedfp.

Referenced by _ReadByte().

◆ cfgets()

char* cfgets ( cfp fp,
char *  buf,
int  len 
)

Definition at line 620 of file compress_io.c.

621 {
622 #ifdef HAVE_LIBZ
623  if (fp->compressedfp)
624  return gzgets(fp->compressedfp, buf, len);
625  else
626 #endif
627  return fgets(buf, len, fp->uncompressedfp);
628 }
const void size_t len
static char * buf
Definition: pg_test_fsync.c:67

References buf, len, and cfp::uncompressedfp.

Referenced by _LoadLOs().

◆ cfopen()

cfp* cfopen ( const char *  path,
const char *  mode,
const pg_compress_specification  compression_spec 
)

Definition at line 499 of file compress_io.c.

501 {
502  cfp *fp = pg_malloc(sizeof(cfp));
503 
504  if (compression_spec.algorithm == PG_COMPRESSION_GZIP)
505  {
506 #ifdef HAVE_LIBZ
507  if (compression_spec.level != Z_DEFAULT_COMPRESSION)
508  {
509  /* user has specified a compression level, so tell zlib to use it */
510  char mode_compression[32];
511 
512  snprintf(mode_compression, sizeof(mode_compression), "%s%d",
513  mode, compression_spec.level);
514  fp->compressedfp = gzopen(path, mode_compression);
515  }
516  else
517  {
518  /* don't specify a level, just use the zlib default */
519  fp->compressedfp = gzopen(path, mode);
520  }
521 
522  fp->uncompressedfp = NULL;
523  if (fp->compressedfp == NULL)
524  {
525  free_keep_errno(fp);
526  fp = NULL;
527  }
528 #else
529  pg_fatal("not built with zlib support");
530 #endif
531  }
532  else
533  {
534 #ifdef HAVE_LIBZ
535  fp->compressedfp = NULL;
536 #endif
537  fp->uncompressedfp = fopen(path, mode);
538  if (fp->uncompressedfp == NULL)
539  {
540  free_keep_errno(fp);
541  fp = NULL;
542  }
543  }
544 
545  return fp;
546 }
void * pg_malloc(size_t size)
Definition: fe_memutils.c:47
#define Z_DEFAULT_COMPRESSION
static PgChecksumMode mode
Definition: pg_checksums.c:65
#define snprintf
Definition: port.h:238

References pg_compress_specification::algorithm, free_keep_errno(), pg_compress_specification::level, mode, PG_COMPRESSION_GZIP, pg_fatal, pg_malloc(), snprintf, cfp::uncompressedfp, and Z_DEFAULT_COMPRESSION.

Referenced by cfopen_read(), and cfopen_write().

◆ cfopen_read()

cfp* cfopen_read ( const char *  path,
const char *  mode 
)

Definition at line 425 of file compress_io.c.

426 {
427  cfp *fp;
428 
429  pg_compress_specification compression_spec = {0};
430 
431 #ifdef HAVE_LIBZ
432  if (hasSuffix(path, ".gz"))
433  {
434  compression_spec.algorithm = PG_COMPRESSION_GZIP;
435  fp = cfopen(path, mode, compression_spec);
436  }
437  else
438 #endif
439  {
440  compression_spec.algorithm = PG_COMPRESSION_NONE;
441  fp = cfopen(path, mode, compression_spec);
442 #ifdef HAVE_LIBZ
443  if (fp == NULL)
444  {
445  char *fname;
446 
447  fname = psprintf("%s.gz", path);
448  compression_spec.algorithm = PG_COMPRESSION_GZIP;
449  fp = cfopen(fname, mode, compression_spec);
450  free_keep_errno(fname);
451  }
452 #endif
453  }
454  return fp;
455 }
cfp * cfopen(const char *path, const char *mode, const pg_compress_specification compression_spec)
Definition: compress_io.c:499
@ PG_COMPRESSION_NONE
Definition: compression.h:19
char * psprintf(const char *fmt,...)
Definition: psprintf.c:46

References pg_compress_specification::algorithm, cfopen(), free_keep_errno(), mode, PG_COMPRESSION_GZIP, PG_COMPRESSION_NONE, and psprintf().

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

◆ cfopen_write()

cfp* cfopen_write ( const char *  path,
const char *  mode,
const pg_compress_specification  compression_spec 
)

Definition at line 469 of file compress_io.c.

471 {
472  cfp *fp;
473 
474  if (compression_spec.algorithm == PG_COMPRESSION_NONE)
475  fp = cfopen(path, mode, compression_spec);
476  else
477  {
478 #ifdef HAVE_LIBZ
479  char *fname;
480 
481  fname = psprintf("%s.gz", path);
482  fp = cfopen(fname, mode, compression_spec);
483  free_keep_errno(fname);
484 #else
485  pg_fatal("not built with zlib support");
486  fp = NULL; /* keep compiler quiet */
487 #endif
488  }
489  return fp;
490 }

References pg_compress_specification::algorithm, cfopen(), free_keep_errno(), mode, PG_COMPRESSION_NONE, pg_fatal, and psprintf().

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

◆ cfread()

int cfread ( void *  ptr,
int  size,
cfp fp 
)

Definition at line 550 of file compress_io.c.

551 {
552  int ret;
553 
554  if (size == 0)
555  return 0;
556 
557 #ifdef HAVE_LIBZ
558  if (fp->compressedfp)
559  {
560  ret = gzread(fp->compressedfp, ptr, size);
561  if (ret != size && !gzeof(fp->compressedfp))
562  {
563  int errnum;
564  const char *errmsg = gzerror(fp->compressedfp, &errnum);
565 
566  pg_fatal("could not read from input file: %s",
567  errnum == Z_ERRNO ? strerror(errno) : errmsg);
568  }
569  }
570  else
571 #endif
572  {
573  ret = fread(ptr, 1, size, fp->uncompressedfp);
574  if (ret != size && !feof(fp->uncompressedfp))
576  }
577  return ret;
578 }
int errmsg(const char *fmt,...)
Definition: elog.c:946

References errmsg(), pg_fatal, READ_ERROR_EXIT, strerror, and cfp::uncompressedfp.

Referenced by _PrintFileData(), and _ReadBuf().

◆ cfwrite()

int cfwrite ( const void *  ptr,
int  size,
cfp fp 
)

Definition at line 581 of file compress_io.c.

582 {
583 #ifdef HAVE_LIBZ
584  if (fp->compressedfp)
585  return gzwrite(fp->compressedfp, ptr, size);
586  else
587 #endif
588  return fwrite(ptr, 1, size, fp->uncompressedfp);
589 }

References cfp::uncompressedfp.

Referenced by _EndLO(), _WriteBuf(), _WriteByte(), and _WriteData().

◆ EndCompressor()

void EndCompressor ( ArchiveHandle AH,
CompressorState cs 
)

Definition at line 174 of file compress_io.c.

175 {
176 #ifdef HAVE_LIBZ
178  EndCompressorZlib(AH, cs);
179 #endif
180  free(cs);
181 }
#define free(a)
Definition: header.h:65

References pg_compress_specification::algorithm, CompressorState::compression_spec, free, and PG_COMPRESSION_GZIP.

Referenced by _EndData(), and _EndLO().

◆ free_keep_errno()

static void free_keep_errno ( void *  p)
static

Definition at line 406 of file compress_io.c.

407 {
408  int save_errno = errno;
409 
410  free(p);
411  errno = save_errno;
412 }

References free.

Referenced by cfclose(), cfopen(), cfopen_read(), and cfopen_write().

◆ get_cfp_error()

const char* get_cfp_error ( cfp fp)

Definition at line 669 of file compress_io.c.

670 {
671 #ifdef HAVE_LIBZ
672  if (fp->compressedfp)
673  {
674  int errnum;
675  const char *errmsg = gzerror(fp->compressedfp, &errnum);
676 
677  if (errnum != Z_ERRNO)
678  return errmsg;
679  }
680 #endif
681  return strerror(errno);
682 }

References errmsg(), and strerror.

Referenced by _WriteBuf(), _WriteByte(), and _WriteData().

◆ ReadDataFromArchive()

void ReadDataFromArchive ( ArchiveHandle AH,
const pg_compress_specification  compression_spec,
ReadFunc  readF 
)

Definition at line 127 of file compress_io.c.

130 {
131  if (compression_spec.algorithm == PG_COMPRESSION_NONE)
132  ReadDataFromArchiveNone(AH, readF);
133  if (compression_spec.algorithm == PG_COMPRESSION_GZIP)
134  {
135 #ifdef HAVE_LIBZ
136  ReadDataFromArchiveZlib(AH, readF);
137 #else
138  pg_fatal("not built with zlib support");
139 #endif
140  }
141 }
static void ReadDataFromArchiveNone(ArchiveHandle *AH, ReadFunc readF)
Definition: compress_io.c:358

References pg_compress_specification::algorithm, PG_COMPRESSION_GZIP, PG_COMPRESSION_NONE, pg_fatal, and ReadDataFromArchiveNone().

Referenced by _PrintData().

◆ ReadDataFromArchiveNone()

static void ReadDataFromArchiveNone ( ArchiveHandle AH,
ReadFunc  readF 
)
static

Definition at line 358 of file compress_io.c.

359 {
360  size_t cnt;
361  char *buf;
362  size_t buflen;
363 
365  buflen = ZLIB_OUT_SIZE;
366 
367  while ((cnt = readF(AH, &buf, &buflen)))
368  {
369  ahwrite(buf, 1, cnt, AH);
370  }
371 
372  free(buf);
373 }
void ahwrite(const void *ptr, size_t size, size_t nmemb, ArchiveHandle *AH)
#define ZLIB_OUT_SIZE
Definition: walmethods.c:33

References ahwrite(), buf, free, pg_malloc(), and ZLIB_OUT_SIZE.

Referenced by ReadDataFromArchive().

◆ WriteDataToArchive()

void WriteDataToArchive ( ArchiveHandle AH,
CompressorState cs,
const void *  data,
size_t  dLen 
)

Definition at line 147 of file compress_io.c.

149 {
150  switch (cs->compression_spec.algorithm)
151  {
152  case PG_COMPRESSION_GZIP:
153 #ifdef HAVE_LIBZ
154  WriteDataToArchiveZlib(AH, cs, data, dLen);
155 #else
156  pg_fatal("not built with zlib support");
157 #endif
158  break;
159  case PG_COMPRESSION_NONE:
160  WriteDataToArchiveNone(AH, cs, data, dLen);
161  break;
162  case PG_COMPRESSION_LZ4:
163  /* fallthrough */
164  case PG_COMPRESSION_ZSTD:
165  pg_fatal("invalid compression method");
166  break;
167  }
168 }
static void WriteDataToArchiveNone(ArchiveHandle *AH, CompressorState *cs, const char *data, size_t dLen)
Definition: compress_io.c:376
@ PG_COMPRESSION_LZ4
Definition: compression.h:21
@ PG_COMPRESSION_ZSTD
Definition: compression.h:22
const void * data

References pg_compress_specification::algorithm, CompressorState::compression_spec, data, PG_COMPRESSION_GZIP, PG_COMPRESSION_LZ4, PG_COMPRESSION_NONE, PG_COMPRESSION_ZSTD, pg_fatal, and WriteDataToArchiveNone().

Referenced by _WriteData().

◆ WriteDataToArchiveNone()

static void WriteDataToArchiveNone ( ArchiveHandle AH,
CompressorState cs,
const char *  data,
size_t  dLen 
)
static

Definition at line 376 of file compress_io.c.

378 {
379  cs->writeF(AH, data, dLen);
380 }

References data, and CompressorState::writeF.

Referenced by WriteDataToArchive().