PostgreSQL Source Code  git master
compress_io.h File Reference
Include dependency graph for compress_io.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Macros

#define ZLIB_OUT_SIZE   4096
 
#define ZLIB_IN_SIZE   4096
 

Typedefs

typedef void(* WriteFunc) (ArchiveHandle *AH, const char *buf, size_t len)
 
typedef size_t(* ReadFunc) (ArchiveHandle *AH, char **buf, size_t *buflen)
 
typedef struct CompressorState CompressorState
 
typedef struct cfp cfp
 

Enumerations

enum  CompressionAlgorithm { COMPR_ALG_NONE, COMPR_ALG_LIBZ }
 

Functions

CompressorStateAllocateCompressor (int compression, WriteFunc writeF)
 
void ReadDataFromArchive (ArchiveHandle *AH, int compression, ReadFunc readF)
 
void WriteDataToArchive (ArchiveHandle *AH, CompressorState *cs, const void *data, size_t dLen)
 
void EndCompressor (ArchiveHandle *AH, CompressorState *cs)
 
cfpcfopen (const char *path, const char *mode, int compression)
 
cfpcfopen_read (const char *path, const char *mode)
 
cfpcfopen_write (const char *path, const char *mode, int compression)
 
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)
 

Macro Definition Documentation

◆ ZLIB_IN_SIZE

#define ZLIB_IN_SIZE   4096

Definition at line 22 of file compress_io.h.

Referenced by EndCompressor().

◆ ZLIB_OUT_SIZE

#define ZLIB_OUT_SIZE   4096

Definition at line 21 of file compress_io.h.

Typedef Documentation

◆ cfp

typedef struct cfp cfp

Definition at line 57 of file compress_io.h.

◆ CompressorState

Definition at line 47 of file compress_io.h.

◆ ReadFunc

typedef size_t(* ReadFunc) (ArchiveHandle *AH, char **buf, size_t *buflen)

Definition at line 44 of file compress_io.h.

◆ WriteFunc

typedef void(* WriteFunc) (ArchiveHandle *AH, const char *buf, size_t len)

Definition at line 31 of file compress_io.h.

Enumeration Type Documentation

◆ CompressionAlgorithm

Enumerator
COMPR_ALG_NONE 
COMPR_ALG_LIBZ 

Definition at line 24 of file compress_io.h.

Function Documentation

◆ AllocateCompressor()

CompressorState* AllocateCompressor ( int  compression,
WriteFunc  writeF 
)

Definition at line 124 of file compress_io.c.

References COMPR_ALG_LIBZ, CompressorState::comprAlg, fatal, ParseCompressionOption(), pg_malloc0(), and CompressorState::writeF.

Referenced by _StartBlob(), and _StartData().

125 {
126  CompressorState *cs;
128  int level;
129 
130  ParseCompressionOption(compression, &alg, &level);
131 
132 #ifndef HAVE_LIBZ
133  if (alg == COMPR_ALG_LIBZ)
134  fatal("not built with zlib support");
135 #endif
136 
137  cs = (CompressorState *) pg_malloc0(sizeof(CompressorState));
138  cs->writeF = writeF;
139  cs->comprAlg = alg;
140 
141  /*
142  * Perform compression algorithm specific initialization.
143  */
144 #ifdef HAVE_LIBZ
145  if (alg == COMPR_ALG_LIBZ)
146  InitCompressorZlib(cs, level);
147 #endif
148 
149  return cs;
150 }
CompressionAlgorithm
Definition: compress_io.h:24
CompressionAlgorithm comprAlg
Definition: compress_io.c:67
WriteFunc writeF
Definition: compress_io.c:68
static void ParseCompressionOption(int compression, CompressionAlgorithm *alg, int *level)
Definition: compress_io.c:102
void * pg_malloc0(size_t size)
Definition: fe_memutils.c:53
#define fatal(...)

◆ cfclose()

int cfclose ( cfp fp)

Definition at line 649 of file compress_io.c.

References free_keep_errno(), and cfp::uncompressedfp.

Referenced by _CloseArchive(), _EndBlob(), _EndBlobs(), _EndData(), _LoadBlobs(), _PrintFileData(), and InitArchiveFmt_Directory().

650 {
651  int result;
652 
653  if (fp == NULL)
654  {
655  errno = EBADF;
656  return EOF;
657  }
658 #ifdef HAVE_LIBZ
659  if (fp->compressedfp)
660  {
661  result = gzclose(fp->compressedfp);
662  fp->compressedfp = NULL;
663  }
664  else
665 #endif
666  {
667  result = fclose(fp->uncompressedfp);
668  fp->uncompressedfp = NULL;
669  }
670  free_keep_errno(fp);
671 
672  return result;
673 }
static void free_keep_errno(void *p)
Definition: compress_io.c:433
FILE * uncompressedfp
Definition: compress_io.c:421

◆ cfeof()

int cfeof ( cfp fp)

Definition at line 676 of file compress_io.c.

References cfp::uncompressedfp.

Referenced by _LoadBlobs().

677 {
678 #ifdef HAVE_LIBZ
679  if (fp->compressedfp)
680  return gzeof(fp->compressedfp);
681  else
682 #endif
683  return feof(fp->uncompressedfp);
684 }
FILE * uncompressedfp
Definition: compress_io.c:421

◆ cfgetc()

int cfgetc ( cfp fp)

Definition at line 610 of file compress_io.c.

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

Referenced by _ReadByte().

611 {
612  int ret;
613 
614 #ifdef HAVE_LIBZ
615  if (fp->compressedfp)
616  {
617  ret = gzgetc(fp->compressedfp);
618  if (ret == EOF)
619  {
620  if (!gzeof(fp->compressedfp))
621  fatal("could not read from input file: %s", strerror(errno));
622  else
623  fatal("could not read from input file: end of file");
624  }
625  }
626  else
627 #endif
628  {
629  ret = fgetc(fp->uncompressedfp);
630  if (ret == EOF)
632  }
633 
634  return ret;
635 }
FILE * uncompressedfp
Definition: compress_io.c:421
#define strerror
Definition: port.h:206
#define fatal(...)
#define READ_ERROR_EXIT(fd)

◆ cfgets()

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

Definition at line 638 of file compress_io.c.

References cfp::uncompressedfp.

Referenced by _LoadBlobs().

639 {
640 #ifdef HAVE_LIBZ
641  if (fp->compressedfp)
642  return gzgets(fp->compressedfp, buf, len);
643  else
644 #endif
645  return fgets(buf, len, fp->uncompressedfp);
646 }
FILE * uncompressedfp
Definition: compress_io.c:421
static char * buf
Definition: pg_test_fsync.c:67

◆ cfopen()

cfp* cfopen ( const char *  path,
const char *  mode,
int  compression 
)

Definition at line 518 of file compress_io.c.

References fatal, free_keep_errno(), pg_malloc(), snprintf, cfp::uncompressedfp, and Z_DEFAULT_COMPRESSION.

Referenced by cfopen_read(), and cfopen_write().

519 {
520  cfp *fp = pg_malloc(sizeof(cfp));
521 
522  if (compression != 0)
523  {
524 #ifdef HAVE_LIBZ
525  if (compression != Z_DEFAULT_COMPRESSION)
526  {
527  /* user has specified a compression level, so tell zlib to use it */
528  char mode_compression[32];
529 
530  snprintf(mode_compression, sizeof(mode_compression), "%s%d",
531  mode, compression);
532  fp->compressedfp = gzopen(path, mode_compression);
533  }
534  else
535  {
536  /* don't specify a level, just use the zlib default */
537  fp->compressedfp = gzopen(path, mode);
538  }
539 
540  fp->uncompressedfp = NULL;
541  if (fp->compressedfp == NULL)
542  {
543  free_keep_errno(fp);
544  fp = NULL;
545  }
546 #else
547  fatal("not built with zlib support");
548 #endif
549  }
550  else
551  {
552 #ifdef HAVE_LIBZ
553  fp->compressedfp = NULL;
554 #endif
555  fp->uncompressedfp = fopen(path, mode);
556  if (fp->uncompressedfp == NULL)
557  {
558  free_keep_errno(fp);
559  fp = NULL;
560  }
561  }
562 
563  return fp;
564 }
static PgChecksumMode mode
Definition: pg_checksums.c:61
#define Z_DEFAULT_COMPRESSION
void * pg_malloc(size_t size)
Definition: fe_memutils.c:47
static void free_keep_errno(void *p)
Definition: compress_io.c:433
FILE * uncompressedfp
Definition: compress_io.c:421
#define fatal(...)
#define snprintf
Definition: port.h:193

◆ cfopen_read()

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

Definition at line 452 of file compress_io.c.

References cfopen(), free_keep_errno(), and psprintf().

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

453 {
454  cfp *fp;
455 
456 #ifdef HAVE_LIBZ
457  if (hasSuffix(path, ".gz"))
458  fp = cfopen(path, mode, 1);
459  else
460 #endif
461  {
462  fp = cfopen(path, mode, 0);
463 #ifdef HAVE_LIBZ
464  if (fp == NULL)
465  {
466  char *fname;
467 
468  fname = psprintf("%s.gz", path);
469  fp = cfopen(fname, mode, 1);
470  free_keep_errno(fname);
471  }
472 #endif
473  }
474  return fp;
475 }
static PgChecksumMode mode
Definition: pg_checksums.c:61
char * psprintf(const char *fmt,...)
Definition: psprintf.c:46
static void free_keep_errno(void *p)
Definition: compress_io.c:433
cfp * cfopen(const char *path, const char *mode, int compression)
Definition: compress_io.c:518

◆ cfopen_write()

cfp* cfopen_write ( const char *  path,
const char *  mode,
int  compression 
)

Definition at line 489 of file compress_io.c.

References cfopen(), fatal, free_keep_errno(), and psprintf().

Referenced by _CloseArchive(), _StartBlob(), _StartBlobs(), and _StartData().

490 {
491  cfp *fp;
492 
493  if (compression == 0)
494  fp = cfopen(path, mode, 0);
495  else
496  {
497 #ifdef HAVE_LIBZ
498  char *fname;
499 
500  fname = psprintf("%s.gz", path);
501  fp = cfopen(fname, mode, compression);
502  free_keep_errno(fname);
503 #else
504  fatal("not built with zlib support");
505  fp = NULL; /* keep compiler quiet */
506 #endif
507  }
508  return fp;
509 }
static PgChecksumMode mode
Definition: pg_checksums.c:61
char * psprintf(const char *fmt,...)
Definition: psprintf.c:46
static void free_keep_errno(void *p)
Definition: compress_io.c:433
cfp * cfopen(const char *path, const char *mode, int compression)
Definition: compress_io.c:518
#define fatal(...)

◆ cfread()

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

Definition at line 568 of file compress_io.c.

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

Referenced by _PrintFileData(), and _ReadBuf().

569 {
570  int ret;
571 
572  if (size == 0)
573  return 0;
574 
575 #ifdef HAVE_LIBZ
576  if (fp->compressedfp)
577  {
578  ret = gzread(fp->compressedfp, ptr, size);
579  if (ret != size && !gzeof(fp->compressedfp))
580  {
581  int errnum;
582  const char *errmsg = gzerror(fp->compressedfp, &errnum);
583 
584  fatal("could not read from input file: %s",
585  errnum == Z_ERRNO ? strerror(errno) : errmsg);
586  }
587  }
588  else
589 #endif
590  {
591  ret = fread(ptr, 1, size, fp->uncompressedfp);
592  if (ret != size && !feof(fp->uncompressedfp))
594  }
595  return ret;
596 }
FILE * uncompressedfp
Definition: compress_io.c:421
#define strerror
Definition: port.h:206
#define fatal(...)
int errmsg(const char *fmt,...)
Definition: elog.c:824
#define READ_ERROR_EXIT(fd)

◆ cfwrite()

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

Definition at line 599 of file compress_io.c.

References cfp::uncompressedfp.

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

600 {
601 #ifdef HAVE_LIBZ
602  if (fp->compressedfp)
603  return gzwrite(fp->compressedfp, ptr, size);
604  else
605 #endif
606  return fwrite(ptr, 1, size, fp->uncompressedfp);
607 }
FILE * uncompressedfp
Definition: compress_io.c:421

◆ EndCompressor()

void EndCompressor ( ArchiveHandle AH,
CompressorState cs 
)

Definition at line 201 of file compress_io.c.

References ahwrite(), _z_stream::avail_in, _z_stream::avail_out, buf, COMPR_ALG_LIBZ, CompressorState::comprAlg, fatal, free, _z_stream::next_in, _z_stream::next_out, pg_malloc(), unconstify, CompressorState::writeF, ZLIB_IN_SIZE, and ZLIB_OUT_SIZE.

Referenced by _EndBlob(), and _EndData().

202 {
203 #ifdef HAVE_LIBZ
204  if (cs->comprAlg == COMPR_ALG_LIBZ)
205  EndCompressorZlib(AH, cs);
206 #endif
207  free(cs);
208 }
CompressionAlgorithm comprAlg
Definition: compress_io.c:67
#define free(a)
Definition: header.h:65

◆ get_cfp_error()

const char* get_cfp_error ( cfp fp)

Definition at line 687 of file compress_io.c.

References errmsg(), filename, and strerror.

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

688 {
689 #ifdef HAVE_LIBZ
690  if (fp->compressedfp)
691  {
692  int errnum;
693  const char *errmsg = gzerror(fp->compressedfp, &errnum);
694 
695  if (errnum != Z_ERRNO)
696  return errmsg;
697  }
698 #endif
699  return strerror(errno);
700 }
#define strerror
Definition: port.h:206
int errmsg(const char *fmt,...)
Definition: elog.c:824

◆ ReadDataFromArchive()

void ReadDataFromArchive ( ArchiveHandle AH,
int  compression,
ReadFunc  readF 
)

Definition at line 157 of file compress_io.c.

References COMPR_ALG_LIBZ, COMPR_ALG_NONE, fatal, ParseCompressionOption(), and ReadDataFromArchiveNone().

Referenced by _PrintData().

158 {
160 
161  ParseCompressionOption(compression, &alg, NULL);
162 
163  if (alg == COMPR_ALG_NONE)
164  ReadDataFromArchiveNone(AH, readF);
165  if (alg == COMPR_ALG_LIBZ)
166  {
167 #ifdef HAVE_LIBZ
168  ReadDataFromArchiveZlib(AH, readF);
169 #else
170  fatal("not built with zlib support");
171 #endif
172  }
173 }
CompressionAlgorithm
Definition: compress_io.h:24
static void ParseCompressionOption(int compression, CompressionAlgorithm *alg, int *level)
Definition: compress_io.c:102
static void ReadDataFromArchiveNone(ArchiveHandle *AH, ReadFunc readF)
Definition: compress_io.c:385
#define fatal(...)

◆ WriteDataToArchive()

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

Definition at line 179 of file compress_io.c.

References COMPR_ALG_LIBZ, COMPR_ALG_NONE, CompressorState::comprAlg, fatal, and WriteDataToArchiveNone().

Referenced by _WriteData().

181 {
182  switch (cs->comprAlg)
183  {
184  case COMPR_ALG_LIBZ:
185 #ifdef HAVE_LIBZ
186  WriteDataToArchiveZlib(AH, cs, data, dLen);
187 #else
188  fatal("not built with zlib support");
189 #endif
190  break;
191  case COMPR_ALG_NONE:
192  WriteDataToArchiveNone(AH, cs, data, dLen);
193  break;
194  }
195 }
CompressionAlgorithm comprAlg
Definition: compress_io.c:67
static void WriteDataToArchiveNone(ArchiveHandle *AH, CompressorState *cs, const char *data, size_t dLen)
Definition: compress_io.c:403
#define fatal(...)