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

References free_keep_errno(), and cfp::uncompressedfp.

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

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

◆ cfeof()

int cfeof ( cfp fp)

Definition at line 680 of file compress_io.c.

References cfp::uncompressedfp.

Referenced by _LoadBlobs().

681 {
682 #ifdef HAVE_LIBZ
683  if (fp->compressedfp)
684  return gzeof(fp->compressedfp);
685  else
686 #endif
687  return feof(fp->uncompressedfp);
688 }
FILE * uncompressedfp
Definition: compress_io.c:425

◆ cfgetc()

int cfgetc ( cfp fp)

Definition at line 614 of file compress_io.c.

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

Referenced by _ReadByte().

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

◆ cfgets()

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

Definition at line 642 of file compress_io.c.

References cfp::uncompressedfp.

Referenced by _LoadBlobs().

643 {
644 #ifdef HAVE_LIBZ
645  if (fp->compressedfp)
646  return gzgets(fp->compressedfp, buf, len);
647  else
648 #endif
649  return fgets(buf, len, fp->uncompressedfp);
650 }
FILE * uncompressedfp
Definition: compress_io.c:425
static char * buf
Definition: pg_test_fsync.c:67

◆ cfopen()

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

Definition at line 522 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().

523 {
524  cfp *fp = pg_malloc(sizeof(cfp));
525 
526  if (compression != 0)
527  {
528 #ifdef HAVE_LIBZ
529  if (compression != Z_DEFAULT_COMPRESSION)
530  {
531  /* user has specified a compression level, so tell zlib to use it */
532  char mode_compression[32];
533 
534  snprintf(mode_compression, sizeof(mode_compression), "%s%d",
535  mode, compression);
536  fp->compressedfp = gzopen(path, mode_compression);
537  }
538  else
539  {
540  /* don't specify a level, just use the zlib default */
541  fp->compressedfp = gzopen(path, mode);
542  }
543 
544  fp->uncompressedfp = NULL;
545  if (fp->compressedfp == NULL)
546  {
547  free_keep_errno(fp);
548  fp = NULL;
549  }
550 #else
551  fatal("not built with zlib support");
552 #endif
553  }
554  else
555  {
556 #ifdef HAVE_LIBZ
557  fp->compressedfp = NULL;
558 #endif
559  fp->uncompressedfp = fopen(path, mode);
560  if (fp->uncompressedfp == NULL)
561  {
562  free_keep_errno(fp);
563  fp = NULL;
564  }
565  }
566 
567  return fp;
568 }
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:437
FILE * uncompressedfp
Definition: compress_io.c:425
#define fatal(...)
#define snprintf
Definition: port.h:192

◆ cfopen_read()

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

Definition at line 456 of file compress_io.c.

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

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

457 {
458  cfp *fp;
459 
460 #ifdef HAVE_LIBZ
461  if (hasSuffix(path, ".gz"))
462  fp = cfopen(path, mode, 1);
463  else
464 #endif
465  {
466  fp = cfopen(path, mode, 0);
467 #ifdef HAVE_LIBZ
468  if (fp == NULL)
469  {
470  char *fname;
471 
472  fname = psprintf("%s.gz", path);
473  fp = cfopen(fname, mode, 1);
474  free_keep_errno(fname);
475  }
476 #endif
477  }
478  return fp;
479 }
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:437
cfp * cfopen(const char *path, const char *mode, int compression)
Definition: compress_io.c:522

◆ cfopen_write()

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

Definition at line 493 of file compress_io.c.

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

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

494 {
495  cfp *fp;
496 
497  if (compression == 0)
498  fp = cfopen(path, mode, 0);
499  else
500  {
501 #ifdef HAVE_LIBZ
502  char *fname;
503 
504  fname = psprintf("%s.gz", path);
505  fp = cfopen(fname, mode, compression);
506  free_keep_errno(fname);
507 #else
508  fatal("not built with zlib support");
509  fp = NULL; /* keep compiler quiet */
510 #endif
511  }
512  return fp;
513 }
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:437
cfp * cfopen(const char *path, const char *mode, int compression)
Definition: compress_io.c:522
#define fatal(...)

◆ cfread()

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

Definition at line 572 of file compress_io.c.

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

Referenced by _PrintFileData(), and _ReadBuf().

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

◆ cfwrite()

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

Definition at line 603 of file compress_io.c.

References cfp::uncompressedfp.

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

604 {
605 #ifdef HAVE_LIBZ
606  if (fp->compressedfp)
607  return gzwrite(fp->compressedfp, ptr, size);
608  else
609 #endif
610  return fwrite(ptr, 1, size, fp->uncompressedfp);
611 }
FILE * uncompressedfp
Definition: compress_io.c:425

◆ EndCompressor()

void EndCompressor ( ArchiveHandle AH,
CompressorState cs 
)

Definition at line 202 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().

203 {
204 #ifdef HAVE_LIBZ
205  if (cs->comprAlg == COMPR_ALG_LIBZ)
206  EndCompressorZlib(AH, cs);
207 #endif
208  free(cs);
209 }
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 691 of file compress_io.c.

References errmsg(), filename, and strerror.

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

692 {
693 #ifdef HAVE_LIBZ
694  if (fp->compressedfp)
695  {
696  int errnum;
697  const char *errmsg = gzerror(fp->compressedfp, &errnum);
698 
699  if (errnum != Z_ERRNO)
700  return errmsg;
701  }
702 #endif
703  return strerror(errno);
704 }
#define strerror
Definition: port.h:205
int errmsg(const char *fmt,...)
Definition: elog.c:822

◆ 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:388
#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  return;
196 }
CompressionAlgorithm comprAlg
Definition: compress_io.c:67
static void WriteDataToArchiveNone(ArchiveHandle *AH, CompressorState *cs, const char *data, size_t dLen)
Definition: compress_io.c:406
#define fatal(...)