LCOV - code coverage report
Current view: top level - src/bin/pg_dump - compress_none.c (source / functions) Hit Total Coverage
Test: PostgreSQL 17devel Lines: 50 78 64.1 %
Date: 2024-04-19 05:11:10 Functions: 9 14 64.3 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*-------------------------------------------------------------------------
       2             :  *
       3             :  * compress_none.c
       4             :  *   Routines for archivers to read or write an uncompressed stream.
       5             :  *
       6             :  * Portions Copyright (c) 1996-2024, PostgreSQL Global Development Group
       7             :  * Portions Copyright (c) 1994, Regents of the University of California
       8             :  *
       9             :  * IDENTIFICATION
      10             :  *     src/bin/pg_dump/compress_none.c
      11             :  *
      12             :  *-------------------------------------------------------------------------
      13             :  */
      14             : #include "postgres_fe.h"
      15             : #include <unistd.h>
      16             : 
      17             : #include "compress_none.h"
      18             : #include "pg_backup_utils.h"
      19             : 
      20             : /*----------------------
      21             :  * Compressor API
      22             :  *----------------------
      23             :  */
      24             : 
      25             : /*
      26             :  * Private routines
      27             :  */
      28             : 
      29             : static void
      30           0 : ReadDataFromArchiveNone(ArchiveHandle *AH, CompressorState *cs)
      31             : {
      32             :     size_t      cnt;
      33             :     char       *buf;
      34             :     size_t      buflen;
      35             : 
      36           0 :     buflen = DEFAULT_IO_BUFFER_SIZE;
      37           0 :     buf = pg_malloc(buflen);
      38             : 
      39           0 :     while ((cnt = cs->readF(AH, &buf, &buflen)))
      40             :     {
      41           0 :         ahwrite(buf, 1, cnt, AH);
      42             :     }
      43             : 
      44           0 :     free(buf);
      45           0 : }
      46             : 
      47             : 
      48             : static void
      49           0 : WriteDataToArchiveNone(ArchiveHandle *AH, CompressorState *cs,
      50             :                        const void *data, size_t dLen)
      51             : {
      52           0 :     cs->writeF(AH, data, dLen);
      53           0 : }
      54             : 
      55             : static void
      56           0 : EndCompressorNone(ArchiveHandle *AH, CompressorState *cs)
      57             : {
      58             :     /* no op */
      59           0 : }
      60             : 
      61             : /*
      62             :  * Public interface
      63             :  */
      64             : 
      65             : void
      66           0 : InitCompressorNone(CompressorState *cs,
      67             :                    const pg_compress_specification compression_spec)
      68             : {
      69           0 :     cs->readData = ReadDataFromArchiveNone;
      70           0 :     cs->writeData = WriteDataToArchiveNone;
      71           0 :     cs->end = EndCompressorNone;
      72             : 
      73           0 :     cs->compression_spec = compression_spec;
      74           0 : }
      75             : 
      76             : 
      77             : /*----------------------
      78             :  * Compress File API
      79             :  *----------------------
      80             :  */
      81             : 
      82             : /*
      83             :  * Private routines
      84             :  */
      85             : 
      86             : static bool
      87       36130 : read_none(void *ptr, size_t size, size_t *rsize, CompressFileHandle *CFH)
      88             : {
      89       36130 :     FILE       *fp = (FILE *) CFH->private_data;
      90             :     size_t      ret;
      91             : 
      92       36130 :     if (size == 0)
      93        3616 :         return true;
      94             : 
      95       32514 :     ret = fread(ptr, 1, size, fp);
      96       32514 :     if (ret != size && !feof(fp))
      97           0 :         pg_fatal("could not read from input file: %m");
      98             : 
      99       32514 :     if (rsize)
     100           0 :         *rsize = ret;
     101             : 
     102       32514 :     return true;
     103             : }
     104             : 
     105             : static bool
     106     3961344 : write_none(const void *ptr, size_t size, CompressFileHandle *CFH)
     107             : {
     108             :     size_t      ret;
     109             : 
     110     3961344 :     ret = fwrite(ptr, 1, size, (FILE *) CFH->private_data);
     111     3961344 :     if (ret != size)
     112           0 :         return false;
     113             : 
     114     3961344 :     return true;
     115             : }
     116             : 
     117             : static const char *
     118           0 : get_error_none(CompressFileHandle *CFH)
     119             : {
     120           0 :     return strerror(errno);
     121             : }
     122             : 
     123             : static char *
     124          24 : gets_none(char *ptr, int size, CompressFileHandle *CFH)
     125             : {
     126          24 :     return fgets(ptr, size, (FILE *) CFH->private_data);
     127             : }
     128             : 
     129             : static int
     130      299558 : getc_none(CompressFileHandle *CFH)
     131             : {
     132      299558 :     FILE       *fp = (FILE *) CFH->private_data;
     133             :     int         ret;
     134             : 
     135      299558 :     ret = fgetc(fp);
     136      299558 :     if (ret == EOF)
     137             :     {
     138           0 :         if (!feof(fp))
     139           0 :             pg_fatal("could not read from input file: %m");
     140             :         else
     141           0 :             pg_fatal("could not read from input file: end of file");
     142             :     }
     143             : 
     144      299558 :     return ret;
     145             : }
     146             : 
     147             : static bool
     148         710 : close_none(CompressFileHandle *CFH)
     149             : {
     150         710 :     FILE       *fp = (FILE *) CFH->private_data;
     151         710 :     int         ret = 0;
     152             : 
     153         710 :     CFH->private_data = NULL;
     154             : 
     155         710 :     if (fp)
     156         710 :         ret = fclose(fp);
     157             : 
     158         710 :     return ret == 0;
     159             : }
     160             : 
     161             : static bool
     162          12 : eof_none(CompressFileHandle *CFH)
     163             : {
     164          12 :     return feof((FILE *) CFH->private_data) != 0;
     165             : }
     166             : 
     167             : static bool
     168         716 : open_none(const char *path, int fd, const char *mode, CompressFileHandle *CFH)
     169             : {
     170             :     Assert(CFH->private_data == NULL);
     171             : 
     172         716 :     if (fd >= 0)
     173         420 :         CFH->private_data = fdopen(dup(fd), mode);
     174             :     else
     175         296 :         CFH->private_data = fopen(path, mode);
     176             : 
     177         716 :     if (CFH->private_data == NULL)
     178           0 :         return false;
     179             : 
     180         716 :     return true;
     181             : }
     182             : 
     183             : static bool
     184          36 : open_write_none(const char *path, const char *mode, CompressFileHandle *CFH)
     185             : {
     186             :     Assert(CFH->private_data == NULL);
     187             : 
     188          36 :     CFH->private_data = fopen(path, mode);
     189          36 :     if (CFH->private_data == NULL)
     190           0 :         return false;
     191             : 
     192          36 :     return true;
     193             : }
     194             : 
     195             : /*
     196             :  * Public interface
     197             :  */
     198             : 
     199             : void
     200         752 : InitCompressFileHandleNone(CompressFileHandle *CFH,
     201             :                            const pg_compress_specification compression_spec)
     202             : {
     203         752 :     CFH->open_func = open_none;
     204         752 :     CFH->open_write_func = open_write_none;
     205         752 :     CFH->read_func = read_none;
     206         752 :     CFH->write_func = write_none;
     207         752 :     CFH->gets_func = gets_none;
     208         752 :     CFH->getc_func = getc_none;
     209         752 :     CFH->close_func = close_none;
     210         752 :     CFH->eof_func = eof_none;
     211         752 :     CFH->get_error_func = get_error_none;
     212             : 
     213         752 :     CFH->private_data = NULL;
     214         752 : }

Generated by: LCOV version 1.14