Header And Logo

PostgreSQL
| The world's most advanced open source database.

Defines | Typedefs | Enumerations | Functions

compress_io.h File Reference

#include "postgres_fe.h"
#include "pg_backup_archiver.h"
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.

Defines

#define ZLIB_OUT_SIZE   4096
#define ZLIB_IN_SIZE   4096

Typedefs

typedef size_t(* 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)
size_t 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)

Define Documentation

#define ZLIB_IN_SIZE   4096

Definition at line 23 of file compress_io.h.

#define ZLIB_OUT_SIZE   4096

Definition at line 22 of file compress_io.h.

Referenced by _PrintFileData(), and ReadDataFromArchiveNone().


Typedef Documentation

typedef struct cfp cfp

Definition at line 58 of file compress_io.h.

Definition at line 48 of file compress_io.h.

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

Definition at line 45 of file compress_io.h.

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

Definition at line 32 of file compress_io.h.


Enumeration Type Documentation

Enumerator:
COMPR_ALG_NONE 
COMPR_ALG_LIBZ 

Definition at line 25 of file compress_io.h.

{
    COMPR_ALG_NONE,
    COMPR_ALG_LIBZ
} CompressionAlgorithm;


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().

{
    CompressorState *cs;
    CompressionAlgorithm alg;
    int         level;

    ParseCompressionOption(compression, &alg, &level);

#ifndef HAVE_LIBZ
    if (alg == COMPR_ALG_LIBZ)
        exit_horribly(modulename, "not built with zlib support\n");
#endif

    cs = (CompressorState *) pg_malloc0(sizeof(CompressorState));
    cs->writeF = writeF;
    cs->comprAlg = alg;

    /*
     * Perform compression algorithm specific initialization.
     */
#ifdef HAVE_LIBZ
    if (alg == COMPR_ALG_LIBZ)
        InitCompressorZlib(cs, level);
#endif

    return cs;
}

int cfclose ( cfp fp  ) 

Definition at line 620 of file compress_io.c.

References free, NULL, and cfp::uncompressedfp.

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

{
    int         result;

    if (fp == NULL)
    {
        errno = EBADF;
        return EOF;
    }
#ifdef HAVE_LIBZ
    if (fp->compressedfp)
    {
        result = gzclose(fp->compressedfp);
        fp->compressedfp = NULL;
    }
    else
#endif
    {
        result = fclose(fp->uncompressedfp);
        fp->uncompressedfp = NULL;
    }
    free(fp);

    return result;
}

int cfeof ( cfp fp  ) 

Definition at line 647 of file compress_io.c.

References cfp::uncompressedfp.

Referenced by _LoadBlobs().

{
#ifdef HAVE_LIBZ
    if (fp->compressedfp)
        return gzeof(fp->compressedfp);
    else
#endif
        return feof(fp->uncompressedfp);
}

int cfgetc ( cfp fp  ) 

Definition at line 598 of file compress_io.c.

References cfp::uncompressedfp.

Referenced by _ReadByte().

{
#ifdef HAVE_LIBZ
    if (fp->compressedfp)
        return gzgetc(fp->compressedfp);
    else
#endif
        return fgetc(fp->uncompressedfp);
}

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

Definition at line 609 of file compress_io.c.

References cfp::uncompressedfp.

Referenced by _LoadBlobs().

{
#ifdef HAVE_LIBZ
    if (fp->compressedfp)
        return gzgets(fp->compressedfp, buf, len);
    else
#endif
        return fgets(buf, len, fp->uncompressedfp);
}

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

Definition at line 540 of file compress_io.c.

References exit_horribly(), free, modulename, NULL, pg_malloc(), and cfp::uncompressedfp.

Referenced by cfopen_read(), and cfopen_write().

{
    cfp        *fp = pg_malloc(sizeof(cfp));

    if (compression != 0)
    {
#ifdef HAVE_LIBZ
        fp->compressedfp = gzopen(path, mode);
        fp->uncompressedfp = NULL;
        if (fp->compressedfp == NULL)
        {
            free(fp);
            fp = NULL;
        }
#else
        exit_horribly(modulename, "not built with zlib support\n");
#endif
    }
    else
    {
#ifdef HAVE_LIBZ
        fp->compressedfp = NULL;
#endif
        fp->uncompressedfp = fopen(path, mode);
        if (fp->uncompressedfp == NULL)
        {
            free(fp);
            fp = NULL;
        }
    }

    return fp;
}

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

Definition at line 476 of file compress_io.c.

References cfopen(), free, NULL, pg_malloc(), and snprintf().

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

{
    cfp        *fp;

#ifdef HAVE_LIBZ
    if (hasSuffix(path, ".gz"))
        fp = cfopen(path, mode, 1);
    else
#endif
    {
        fp = cfopen(path, mode, 0);
#ifdef HAVE_LIBZ
        if (fp == NULL)
        {
            int         fnamelen = strlen(path) + 4;
            char       *fname = pg_malloc(fnamelen);

            snprintf(fname, fnamelen, "%s%s", path, ".gz");
            fp = cfopen(fname, mode, 1);
            free(fname);
        }
#endif
    }
    return fp;
}

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

Definition at line 512 of file compress_io.c.

References cfopen(), exit_horribly(), free, modulename, pg_malloc(), and snprintf().

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

{
    cfp        *fp;

    if (compression == 0)
        fp = cfopen(path, mode, 0);
    else
    {
#ifdef HAVE_LIBZ
        int         fnamelen = strlen(path) + 4;
        char       *fname = pg_malloc(fnamelen);

        snprintf(fname, fnamelen, "%s%s", path, ".gz");
        fp = cfopen(fname, mode, 1);
        free(fname);
#else
        exit_horribly(modulename, "not built with zlib support\n");
        fp = NULL;              /* keep compiler quiet */
#endif
    }
    return fp;
}

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

Definition at line 576 of file compress_io.c.

References cfp::uncompressedfp.

Referenced by _PrintFileData(), and _ReadBuf().

{
#ifdef HAVE_LIBZ
    if (fp->compressedfp)
        return gzread(fp->compressedfp, ptr, size);
    else
#endif
        return fread(ptr, 1, size, fp->uncompressedfp);
}

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

Definition at line 587 of file compress_io.c.

References cfp::uncompressedfp.

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

{
#ifdef HAVE_LIBZ
    if (fp->compressedfp)
        return gzwrite(fp->compressedfp, ptr, size);
    else
#endif
        return fwrite(ptr, 1, size, fp->uncompressedfp);
}

void EndCompressor ( ArchiveHandle AH,
CompressorState cs 
)

Definition at line 207 of file compress_io.c.

References COMPR_ALG_LIBZ, CompressorState::comprAlg, and free.

Referenced by _EndBlob(), and _EndData().

{
#ifdef HAVE_LIBZ
    if (cs->comprAlg == COMPR_ALG_LIBZ)
        EndCompressorZlib(AH, cs);
#endif
    free(cs);
}

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().

{
    CompressionAlgorithm alg;

    ParseCompressionOption(compression, &alg, NULL);

    if (alg == COMPR_ALG_NONE)
        ReadDataFromArchiveNone(AH, readF);
    if (alg == COMPR_ALG_LIBZ)
    {
#ifdef HAVE_LIBZ
        ReadDataFromArchiveZlib(AH, readF);
#else
        exit_horribly(modulename, "not built with zlib support\n");
#endif
    }
}

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

Definition at line 183 of file compress_io.c.

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

Referenced by _WriteData().

{
    /* Are we aborting? */
    checkAborting(AH);

    switch (cs->comprAlg)
    {
        case COMPR_ALG_LIBZ:
#ifdef HAVE_LIBZ
            return WriteDataToArchiveZlib(AH, cs, data, dLen);
#else
            exit_horribly(modulename, "not built with zlib support\n");
#endif
        case COMPR_ALG_NONE:
            return WriteDataToArchiveNone(AH, cs, data, dLen);
    }
    return 0;                   /* keep compiler quiet */
}