PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
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

#define ZLIB_IN_SIZE   4096

Definition at line 22 of file compress_io.h.

#define ZLIB_OUT_SIZE   4096

Definition at line 21 of file compress_io.h.

Typedef Documentation

typedef struct cfp cfp

Definition at line 57 of file compress_io.h.

Definition at line 47 of file compress_io.h.

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

Definition at line 44 of file compress_io.h.

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

Definition at line 31 of file compress_io.h.

Enumeration Type Documentation

Enumerator
COMPR_ALG_NONE 
COMPR_ALG_LIBZ 

Definition at line 24 of file compress_io.h.

Function Documentation

CompressorState* AllocateCompressor ( int  compression,
WriteFunc  writeF 
)

Definition at line 128 of file compress_io.c.

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

Referenced by _StartBlob(), and _StartData().

129 {
130  CompressorState *cs;
132  int level;
133 
134  ParseCompressionOption(compression, &alg, &level);
135 
136 #ifndef HAVE_LIBZ
137  if (alg == COMPR_ALG_LIBZ)
138  exit_horribly(modulename, "not built with zlib support\n");
139 #endif
140 
141  cs = (CompressorState *) pg_malloc0(sizeof(CompressorState));
142  cs->writeF = writeF;
143  cs->comprAlg = alg;
144 
145  /*
146  * Perform compression algorithm specific initialization.
147  */
148 #ifdef HAVE_LIBZ
149  if (alg == COMPR_ALG_LIBZ)
150  InitCompressorZlib(cs, level);
151 #endif
152 
153  return cs;
154 }
static const char * modulename
Definition: compress_io.c:78
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:105
void * pg_malloc0(size_t size)
Definition: fe_memutils.c:53
void exit_horribly(const char *modulename, const char *fmt,...)
int cfclose ( cfp fp)

Definition at line 667 of file compress_io.c.

References free_keep_errno(), NULL, result, and cfp::uncompressedfp.

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

668 {
669  int result;
670 
671  if (fp == NULL)
672  {
673  errno = EBADF;
674  return EOF;
675  }
676 #ifdef HAVE_LIBZ
677  if (fp->compressedfp)
678  {
679  result = gzclose(fp->compressedfp);
680  fp->compressedfp = NULL;
681  }
682  else
683 #endif
684  {
685  result = fclose(fp->uncompressedfp);
686  fp->uncompressedfp = NULL;
687  }
688  free_keep_errno(fp);
689 
690  return result;
691 }
static void free_keep_errno(void *p)
Definition: compress_io.c:448
FILE * uncompressedfp
Definition: compress_io.c:436
return result
Definition: formatting.c:1633
#define NULL
Definition: c.h:229
int cfeof ( cfp fp)

Definition at line 694 of file compress_io.c.

References cfp::uncompressedfp.

Referenced by _LoadBlobs().

695 {
696 #ifdef HAVE_LIBZ
697  if (fp->compressedfp)
698  return gzeof(fp->compressedfp);
699  else
700 #endif
701  return feof(fp->uncompressedfp);
702 }
FILE * uncompressedfp
Definition: compress_io.c:436
int cfgetc ( cfp fp)

Definition at line 626 of file compress_io.c.

References exit_horribly(), modulename, READ_ERROR_EXIT, strerror(), and cfp::uncompressedfp.

Referenced by _ReadByte().

627 {
628  int ret;
629 
630 #ifdef HAVE_LIBZ
631  if (fp->compressedfp)
632  {
633  ret = gzgetc(fp->compressedfp);
634  if (ret == EOF)
635  {
636  if (!gzeof(fp->compressedfp))
638  "could not read from input file: %s\n", strerror(errno));
639  else
641  "could not read from input file: end of file\n");
642  }
643  }
644  else
645 #endif
646  {
647  ret = fgetc(fp->uncompressedfp);
648  if (ret == EOF)
650  }
651 
652  return ret;
653 }
static const char * modulename
Definition: compress_io.c:78
FILE * uncompressedfp
Definition: compress_io.c:436
void exit_horribly(const char *modulename, const char *fmt,...)
const char * strerror(int errnum)
Definition: strerror.c:19
#define READ_ERROR_EXIT(fd)
char* cfgets ( cfp fp,
char *  buf,
int  len 
)

Definition at line 656 of file compress_io.c.

References cfp::uncompressedfp.

Referenced by _LoadBlobs().

657 {
658 #ifdef HAVE_LIBZ
659  if (fp->compressedfp)
660  return gzgets(fp->compressedfp, buf, len);
661  else
662 #endif
663  return fgets(buf, len, fp->uncompressedfp);
664 }
FILE * uncompressedfp
Definition: compress_io.c:436
static char * buf
Definition: pg_test_fsync.c:66
cfp* cfopen ( const char *  path,
const char *  mode,
int  compression 
)

Definition at line 533 of file compress_io.c.

References exit_horribly(), free_keep_errno(), modulename, NULL, pg_malloc(), snprintf(), cfp::uncompressedfp, and Z_DEFAULT_COMPRESSION.

Referenced by cfopen_read(), and cfopen_write().

534 {
535  cfp *fp = pg_malloc(sizeof(cfp));
536 
537  if (compression != 0)
538  {
539 #ifdef HAVE_LIBZ
540  if (compression != Z_DEFAULT_COMPRESSION)
541  {
542  /* user has specified a compression level, so tell zlib to use it */
543  char mode_compression[32];
544 
545  snprintf(mode_compression, sizeof(mode_compression), "%s%d",
546  mode, compression);
547  fp->compressedfp = gzopen(path, mode_compression);
548  }
549  else
550  {
551  /* don't specify a level, just use the zlib default */
552  fp->compressedfp = gzopen(path, mode);
553  }
554 
555  fp->uncompressedfp = NULL;
556  if (fp->compressedfp == NULL)
557  {
558  free_keep_errno(fp);
559  fp = NULL;
560  }
561 #else
562  exit_horribly(modulename, "not built with zlib support\n");
563 #endif
564  }
565  else
566  {
567 #ifdef HAVE_LIBZ
568  fp->compressedfp = NULL;
569 #endif
570  fp->uncompressedfp = fopen(path, mode);
571  if (fp->uncompressedfp == NULL)
572  {
573  free_keep_errno(fp);
574  fp = NULL;
575  }
576  }
577 
578  return fp;
579 }
#define Z_DEFAULT_COMPRESSION
static const char * modulename
Definition: compress_io.c:78
void * pg_malloc(size_t size)
Definition: fe_memutils.c:47
static void free_keep_errno(void *p)
Definition: compress_io.c:448
FILE * uncompressedfp
Definition: compress_io.c:436
int snprintf(char *str, size_t count, const char *fmt,...) pg_attribute_printf(3
#define NULL
Definition: c.h:229
void exit_horribly(const char *modulename, const char *fmt,...)
cfp* cfopen_read ( const char *  path,
const char *  mode 
)

Definition at line 467 of file compress_io.c.

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

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

468 {
469  cfp *fp;
470 
471 #ifdef HAVE_LIBZ
472  if (hasSuffix(path, ".gz"))
473  fp = cfopen(path, mode, 1);
474  else
475 #endif
476  {
477  fp = cfopen(path, mode, 0);
478 #ifdef HAVE_LIBZ
479  if (fp == NULL)
480  {
481  char *fname;
482 
483  fname = psprintf("%s.gz", path);
484  fp = cfopen(fname, mode, 1);
485  free_keep_errno(fname);
486  }
487 #endif
488  }
489  return fp;
490 }
char * psprintf(const char *fmt,...)
Definition: psprintf.c:46
static void free_keep_errno(void *p)
Definition: compress_io.c:448
cfp * cfopen(const char *path, const char *mode, int compression)
Definition: compress_io.c:533
#define NULL
Definition: c.h:229
cfp* cfopen_write ( const char *  path,
const char *  mode,
int  compression 
)

Definition at line 504 of file compress_io.c.

References cfopen(), exit_horribly(), free_keep_errno(), modulename, NULL, and psprintf().

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

505 {
506  cfp *fp;
507 
508  if (compression == 0)
509  fp = cfopen(path, mode, 0);
510  else
511  {
512 #ifdef HAVE_LIBZ
513  char *fname;
514 
515  fname = psprintf("%s.gz", path);
516  fp = cfopen(fname, mode, compression);
517  free_keep_errno(fname);
518 #else
519  exit_horribly(modulename, "not built with zlib support\n");
520  fp = NULL; /* keep compiler quiet */
521 #endif
522  }
523  return fp;
524 }
static const char * modulename
Definition: compress_io.c:78
char * psprintf(const char *fmt,...)
Definition: psprintf.c:46
static void free_keep_errno(void *p)
Definition: compress_io.c:448
cfp * cfopen(const char *path, const char *mode, int compression)
Definition: compress_io.c:533
#define NULL
Definition: c.h:229
void exit_horribly(const char *modulename, const char *fmt,...)
int cfread ( void *  ptr,
int  size,
cfp fp 
)

Definition at line 583 of file compress_io.c.

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

Referenced by _PrintFileData(), and _ReadBuf().

584 {
585  int ret;
586 
587  if (size == 0)
588  return 0;
589 
590 #ifdef HAVE_LIBZ
591  if (fp->compressedfp)
592  {
593  ret = gzread(fp->compressedfp, ptr, size);
594  if (ret != size && !gzeof(fp->compressedfp))
595  {
596  int errnum;
597  const char *errmsg = gzerror(fp->compressedfp, &errnum);
598 
600  "could not read from input file: %s\n",
601  errnum == Z_ERRNO ? strerror(errno) : errmsg);
602  }
603  }
604  else
605 #endif
606  {
607  ret = fread(ptr, 1, size, fp->uncompressedfp);
608  if (ret != size && !feof(fp->uncompressedfp))
610  }
611  return ret;
612 }
static const char * modulename
Definition: compress_io.c:78
FILE * uncompressedfp
Definition: compress_io.c:436
void exit_horribly(const char *modulename, const char *fmt,...)
int errmsg(const char *fmt,...)
Definition: elog.c:797
const char * strerror(int errnum)
Definition: strerror.c:19
#define READ_ERROR_EXIT(fd)
int cfwrite ( const void *  ptr,
int  size,
cfp fp 
)

Definition at line 615 of file compress_io.c.

References cfp::uncompressedfp.

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

616 {
617 #ifdef HAVE_LIBZ
618  if (fp->compressedfp)
619  return gzwrite(fp->compressedfp, ptr, size);
620  else
621 #endif
622  return fwrite(ptr, 1, size, fp->uncompressedfp);
623 }
FILE * uncompressedfp
Definition: compress_io.c:436
void EndCompressor ( ArchiveHandle AH,
CompressorState cs 
)

Definition at line 206 of file compress_io.c.

References COMPR_ALG_LIBZ, CompressorState::comprAlg, and free.

Referenced by _EndBlob(), and _EndData().

207 {
208 #ifdef HAVE_LIBZ
209  if (cs->comprAlg == COMPR_ALG_LIBZ)
210  EndCompressorZlib(AH, cs);
211 #endif
212  free(cs);
213 }
CompressionAlgorithm comprAlg
Definition: compress_io.c:67
#define free(a)
Definition: header.h:65
const char* get_cfp_error ( cfp fp)

Definition at line 705 of file compress_io.c.

References errmsg(), and strerror().

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

706 {
707 #ifdef HAVE_LIBZ
708  if (fp->compressedfp)
709  {
710  int errnum;
711  const char *errmsg = gzerror(fp->compressedfp, &errnum);
712 
713  if (errnum != Z_ERRNO)
714  return errmsg;
715  }
716 #endif
717  return strerror(errno);
718 }
int errmsg(const char *fmt,...)
Definition: elog.c:797
const char * strerror(int errnum)
Definition: strerror.c:19
void ReadDataFromArchive ( ArchiveHandle AH,
int  compression,
ReadFunc  readF 
)

Definition at line 161 of file compress_io.c.

References COMPR_ALG_LIBZ, COMPR_ALG_NONE, exit_horribly(), modulename, NULL, ParseCompressionOption(), and ReadDataFromArchiveNone().

Referenced by _PrintData().

162 {
164 
165  ParseCompressionOption(compression, &alg, NULL);
166 
167  if (alg == COMPR_ALG_NONE)
168  ReadDataFromArchiveNone(AH, readF);
169  if (alg == COMPR_ALG_LIBZ)
170  {
171 #ifdef HAVE_LIBZ
172  ReadDataFromArchiveZlib(AH, readF);
173 #else
174  exit_horribly(modulename, "not built with zlib support\n");
175 #endif
176  }
177 }
static const char * modulename
Definition: compress_io.c:78
CompressionAlgorithm
Definition: compress_io.h:24
static void ParseCompressionOption(int compression, CompressionAlgorithm *alg, int *level)
Definition: compress_io.c:105
static void ReadDataFromArchiveNone(ArchiveHandle *AH, ReadFunc readF)
Definition: compress_io.c:399
#define NULL
Definition: c.h:229
void exit_horribly(const char *modulename, const char *fmt,...)
void WriteDataToArchive ( ArchiveHandle AH,
CompressorState cs,
const void *  data,
size_t  dLen 
)

Definition at line 183 of file compress_io.c.

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

Referenced by _WriteData().

185 {
186  switch (cs->comprAlg)
187  {
188  case COMPR_ALG_LIBZ:
189 #ifdef HAVE_LIBZ
190  WriteDataToArchiveZlib(AH, cs, data, dLen);
191 #else
192  exit_horribly(modulename, "not built with zlib support\n");
193 #endif
194  break;
195  case COMPR_ALG_NONE:
196  WriteDataToArchiveNone(AH, cs, data, dLen);
197  break;
198  }
199  return;
200 }
static const char * modulename
Definition: compress_io.c:78
CompressionAlgorithm comprAlg
Definition: compress_io.c:67
static void WriteDataToArchiveNone(ArchiveHandle *AH, CompressorState *cs, const char *data, size_t dLen)
Definition: compress_io.c:417
void exit_horribly(const char *modulename, const char *fmt,...)