Header And Logo

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

Data Structures | Functions | Variables

compress_io.c File Reference

#include "compress_io.h"
#include "pg_backup_utils.h"
#include "parallel.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 ParseCompressionOption (int compression, CompressionAlgorithm *alg, int *level)
static void ReadDataFromArchiveNone (ArchiveHandle *AH, ReadFunc readF)
static size_t WriteDataToArchiveNone (ArchiveHandle *AH, CompressorState *cs, const char *data, size_t dLen)
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_read (const char *path, const char *mode)
cfpcfopen_write (const char *path, const char *mode, int compression)
cfpcfopen (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)

Variables

static const char * modulename = gettext_noop("compress_io")

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);
}

static void ParseCompressionOption ( int  compression,
CompressionAlgorithm alg,
int *  level 
) [static]

Definition at line 105 of file compress_io.c.

References exit_horribly(), modulename, and Z_DEFAULT_COMPRESSION.

Referenced by AllocateCompressor(), and ReadDataFromArchive().

{
    if (compression == Z_DEFAULT_COMPRESSION ||
        (compression > 0 && compression <= 9))
        *alg = COMPR_ALG_LIBZ;
    else if (compression == 0)
        *alg = COMPR_ALG_NONE;
    else
    {
        exit_horribly(modulename, "invalid compression code: %d\n",
                      compression);
        *alg = COMPR_ALG_NONE;  /* keep compiler quiet */
    }

    /* The level is just the passed-in value. */
    if (level)
        *level = compression;
}

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
    }
}

static void ReadDataFromArchiveNone ( ArchiveHandle AH,
ReadFunc  readF 
) [static]

Definition at line 410 of file compress_io.c.

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

Referenced by ReadDataFromArchive().

{
    size_t      cnt;
    char       *buf;
    size_t      buflen;

    buf = pg_malloc(ZLIB_OUT_SIZE);
    buflen = ZLIB_OUT_SIZE;

    while ((cnt = readF(AH, &buf, &buflen)))
    {
        /* Are we aborting? */
        checkAborting(AH);

        ahwrite(buf, 1, cnt, AH);
    }

    free(buf);
}

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 */
}

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

Definition at line 431 of file compress_io.c.

References exit_horribly(), modulename, strerror(), and CompressorState::writeF.

Referenced by WriteDataToArchive().

{
    /*
     * Any write function should do its own error checking but to make sure we
     * do a check here as well...
     */
    if (cs->writeF(AH, data, dLen) != dLen)
        exit_horribly(modulename,
                      "could not write to output file: %s\n",
                      strerror(errno));
    return dLen;
}


Variable Documentation

const char* modulename = gettext_noop("compress_io") [static]