LCOV - differential code coverage report
Current view: top level - src/bin/pg_dump - compress_none.c (source / functions) Coverage Total Hit UNC UBC GBC GNC CBC DUB DCB
Current: bed3ffbf9d952be6c7d739d068cdce44c046dfb7 vs 574581b50ac9c63dd9e4abebb731a3b67e5b50f6 Lines: 87.5 % 112 98 4 10 17 31 50 2 2
Current Date: 2026-05-05 10:23:31 +0900 Functions: 92.9 % 14 13 1 1 4 8
Baseline: lcov-20260505-025707-baseline Branches: 61.9 % 42 26 4 12 2 14 10 1 1
Baseline Date: 2026-05-05 10:27:06 +0900 Line coverage date bins:
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
(30,360] days: 84.1 % 44 37 4 3 31 6
(360..) days: 89.7 % 68 61 7 17 44
Function coverage date bins:
(30,360] days: 100.0 % 1 1 1
(360..) days: 92.3 % 13 12 1 1 4 7
Branch coverage date bins:
(30,360] days: 66.7 % 24 16 4 4 14 2
(360..) days: 55.6 % 18 10 8 2 8

 Age         Owner                    Branch data    TLA  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-2026, 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                 :                :  * We buffer outgoing data, just to ensure that data blocks written to the
                                 27                 :                :  * archive file are of reasonable size.  The read side could use this struct,
                                 28                 :                :  * but there's no need because it does not retain data across calls.
                                 29                 :                :  */
                                 30                 :                : typedef struct NoneCompressorState
                                 31                 :                : {
                                 32                 :                :     char       *buffer;         /* buffer for unwritten data */
                                 33                 :                :     size_t      buflen;         /* allocated size of buffer */
                                 34                 :                :     size_t      bufdata;        /* amount of valid data currently in buffer */
                                 35                 :                : } NoneCompressorState;
                                 36                 :                : 
                                 37                 :                : /*
                                 38                 :                :  * Private routines
                                 39                 :                :  */
                                 40                 :                : 
                                 41                 :                : static void
 1167 tomas.vondra@postgre       42                 :GBC           3 : ReadDataFromArchiveNone(ArchiveHandle *AH, CompressorState *cs)
                                 43                 :                : {
                                 44                 :                :     size_t      cnt;
                                 45                 :                :     char       *buf;
                                 46                 :                :     size_t      buflen;
                                 47                 :                : 
 1139                            48                 :              3 :     buflen = DEFAULT_IO_BUFFER_SIZE;
                                 49                 :              3 :     buf = pg_malloc(buflen);
                                 50                 :                : 
 1167                            51         [ +  + ]:              8 :     while ((cnt = cs->readF(AH, &buf, &buflen)))
                                 52                 :                :     {
                                 53                 :              5 :         ahwrite(buf, 1, cnt, AH);
                                 54                 :                :     }
                                 55                 :                : 
                                 56                 :              3 :     free(buf);
                                 57                 :              3 : }
                                 58                 :                : 
                                 59                 :                : 
                                 60                 :                : static void
                                 61                 :              4 : WriteDataToArchiveNone(ArchiveHandle *AH, CompressorState *cs,
                                 62                 :                :                        const void *data, size_t dLen)
                                 63                 :                : {
  201 tgl@sss.pgh.pa.us          64                 :GNC           4 :     NoneCompressorState *nonecs = (NoneCompressorState *) cs->private_data;
                                 65                 :              4 :     size_t      remaining = dLen;
                                 66                 :                : 
                                 67         [ +  + ]:             10 :     while (remaining > 0)
                                 68                 :                :     {
                                 69                 :                :         size_t      chunk;
                                 70                 :                : 
                                 71                 :                :         /* Dump buffer if full */
                                 72         [ +  + ]:              6 :         if (nonecs->bufdata >= nonecs->buflen)
                                 73                 :                :         {
                                 74                 :              2 :             cs->writeF(AH, nonecs->buffer, nonecs->bufdata);
                                 75                 :              2 :             nonecs->bufdata = 0;
                                 76                 :                :         }
                                 77                 :                :         /* And fill it */
                                 78                 :              6 :         chunk = nonecs->buflen - nonecs->bufdata;
                                 79         [ +  + ]:              6 :         if (chunk > remaining)
                                 80                 :              4 :             chunk = remaining;
                                 81                 :              6 :         memcpy(nonecs->buffer + nonecs->bufdata, data, chunk);
                                 82                 :              6 :         nonecs->bufdata += chunk;
                                 83                 :              6 :         data = ((const char *) data) + chunk;
                                 84                 :              6 :         remaining -= chunk;
                                 85                 :                :     }
 1167 tomas.vondra@postgre       86                 :GBC           4 : }
                                 87                 :                : 
                                 88                 :                : static void
                                 89                 :              6 : EndCompressorNone(ArchiveHandle *AH, CompressorState *cs)
                                 90                 :                : {
  201 tgl@sss.pgh.pa.us          91                 :GNC           6 :     NoneCompressorState *nonecs = (NoneCompressorState *) cs->private_data;
                                 92                 :                : 
                                 93         [ +  + ]:              6 :     if (nonecs)
                                 94                 :                :     {
                                 95                 :                :         /* Dump buffer if nonempty */
                                 96         [ +  - ]:              3 :         if (nonecs->bufdata > 0)
                                 97                 :              3 :             cs->writeF(AH, nonecs->buffer, nonecs->bufdata);
                                 98                 :                :         /* Free working state */
                                 99                 :              3 :         pg_free(nonecs->buffer);
                                100                 :              3 :         pg_free(nonecs);
                                101                 :              3 :         cs->private_data = NULL;
                                102                 :                :     }
 1167 tomas.vondra@postgre      103                 :GBC           6 : }
                                104                 :                : 
                                105                 :                : /*
                                106                 :                :  * Public interface
                                107                 :                :  */
                                108                 :                : 
                                109                 :                : void
                                110                 :              6 : InitCompressorNone(CompressorState *cs,
                                111                 :                :                    const pg_compress_specification compression_spec)
                                112                 :                : {
                                113                 :              6 :     cs->readData = ReadDataFromArchiveNone;
                                114                 :              6 :     cs->writeData = WriteDataToArchiveNone;
                                115                 :              6 :     cs->end = EndCompressorNone;
                                116                 :                : 
                                117                 :              6 :     cs->compression_spec = compression_spec;
                                118                 :                : 
                                119                 :                :     /*
                                120                 :                :      * If the caller has defined a write function, prepare the necessary
                                121                 :                :      * buffer.
                                122                 :                :      */
  201 tgl@sss.pgh.pa.us         123         [ +  + ]:GNC           6 :     if (cs->writeF)
                                124                 :                :     {
                                125                 :                :         NoneCompressorState *nonecs;
                                126                 :                : 
   81 michael@paquier.xyz       127                 :              3 :         nonecs = pg_malloc_object(NoneCompressorState);
  201 tgl@sss.pgh.pa.us         128                 :              3 :         nonecs->buflen = DEFAULT_IO_BUFFER_SIZE;
                                129                 :              3 :         nonecs->buffer = pg_malloc(nonecs->buflen);
                                130                 :              3 :         nonecs->bufdata = 0;
                                131                 :                : 
                                132                 :              3 :         cs->private_data = nonecs;
                                133                 :                :     }
 1167 tomas.vondra@postgre      134                 :GBC           6 : }
                                135                 :                : 
                                136                 :                : 
                                137                 :                : /*----------------------
                                138                 :                :  * Compress File API
                                139                 :                :  *----------------------
                                140                 :                :  */
                                141                 :                : 
                                142                 :                : /*
                                143                 :                :  * Private routines
                                144                 :                :  */
                                145                 :                : 
                                146                 :                : static size_t
  249 dgustafsson@postgres      147                 :CBC       20295 : read_none(void *ptr, size_t size, CompressFileHandle *CFH)
                                148                 :                : {
 1167 tomas.vondra@postgre      149                 :          20295 :     FILE       *fp = (FILE *) CFH->private_data;
                                150                 :                :     size_t      ret;
                                151                 :                : 
                                152                 :          20295 :     ret = fread(ptr, 1, size, fp);
  249 dgustafsson@postgres      153         [ -  + ]:          20295 :     if (ferror(fp))
  784 michael@paquier.xyz       154                 :UBC           0 :         pg_fatal("could not read from input file: %m");
                                155                 :                : 
  249 dgustafsson@postgres      156                 :CBC       20295 :     return ret;
                                157                 :                : }
                                158                 :                : 
                                159                 :                : static void
 1167 tomas.vondra@postgre      160                 :        2227010 : write_none(const void *ptr, size_t size, CompressFileHandle *CFH)
                                161                 :                : {
                                162                 :                :     size_t      ret;
                                163                 :                : 
  249 dgustafsson@postgres      164                 :        2227010 :     errno = 0;
 1139 tomas.vondra@postgre      165                 :        2227010 :     ret = fwrite(ptr, 1, size, (FILE *) CFH->private_data);
                                166         [ -  + ]:        2227010 :     if (ret != size)
                                167                 :                :     {
  249 dgustafsson@postgres      168         [ #  # ]:UBC           0 :         errno = (errno) ? errno : ENOSPC;
                                169                 :              0 :         pg_fatal("could not write to file: %m");
                                170                 :                :     }
 1167 tomas.vondra@postgre      171                 :CBC     2227010 : }
                                172                 :                : 
                                173                 :                : static const char *
 1167 tomas.vondra@postgre      174                 :UBC           0 : get_error_none(CompressFileHandle *CFH)
                                175                 :                : {
                                176                 :              0 :     return strerror(errno);
                                177                 :                : }
                                178                 :                : 
                                179                 :                : static char *
 1167 tomas.vondra@postgre      180                 :CBC           8 : gets_none(char *ptr, int size, CompressFileHandle *CFH)
                                181                 :                : {
                                182                 :              8 :     return fgets(ptr, size, (FILE *) CFH->private_data);
                                183                 :                : }
                                184                 :                : 
                                185                 :                : static int
                                186                 :         186721 : getc_none(CompressFileHandle *CFH)
                                187                 :                : {
                                188                 :         186721 :     FILE       *fp = (FILE *) CFH->private_data;
                                189                 :                :     int         ret;
                                190                 :                : 
                                191                 :         186721 :     ret = fgetc(fp);
                                192         [ -  + ]:         186721 :     if (ret == EOF)
                                193                 :                :     {
 1167 tomas.vondra@postgre      194         [ #  # ]:UBC           0 :         if (!feof(fp))
  784 michael@paquier.xyz       195                 :              0 :             pg_fatal("could not read from input file: %m");
                                196                 :                :         else
 1167 tomas.vondra@postgre      197                 :              0 :             pg_fatal("could not read from input file: end of file");
                                198                 :                :     }
                                199                 :                : 
 1167 tomas.vondra@postgre      200                 :CBC      186721 :     return ret;
                                201                 :                : }
                                202                 :                : 
                                203                 :                : static bool
                                204                 :            800 : close_none(CompressFileHandle *CFH)
                                205                 :                : {
                                206                 :            800 :     FILE       *fp = (FILE *) CFH->private_data;
                                207                 :            800 :     int         ret = 0;
                                208                 :                : 
                                209                 :            800 :     CFH->private_data = NULL;
                                210                 :                : 
                                211         [ +  - ]:            800 :     if (fp)
                                212                 :                :     {
  249 dgustafsson@postgres      213                 :            800 :         errno = 0;
 1167 tomas.vondra@postgre      214                 :            800 :         ret = fclose(fp);
  249 dgustafsson@postgres      215         [ -  + ]:            800 :         if (ret != 0)
  249 dgustafsson@postgres      216                 :UBC           0 :             pg_log_error("could not close file: %m");
                                217                 :                :     }
                                218                 :                : 
 1139 tomas.vondra@postgre      219                 :CBC         800 :     return ret == 0;
                                220                 :                : }
                                221                 :                : 
                                222                 :                : static bool
 1167                           223                 :              4 : eof_none(CompressFileHandle *CFH)
                                224                 :                : {
 1139                           225                 :              4 :     return feof((FILE *) CFH->private_data) != 0;
                                226                 :                : }
                                227                 :                : 
                                228                 :                : static bool
 1167                           229                 :            749 : open_none(const char *path, int fd, const char *mode, CompressFileHandle *CFH)
                                230                 :                : {
                                231         [ -  + ]:            749 :     Assert(CFH->private_data == NULL);
                                232                 :                : 
                                233         [ +  + ]:            749 :     if (fd >= 0)
                                234                 :                :     {
   47 tgl@sss.pgh.pa.us         235                 :GNC         446 :         int         dup_fd = dup(fd);
                                236                 :                : 
                                237         [ -  + ]:            446 :         if (dup_fd < 0)
   47 tgl@sss.pgh.pa.us         238                 :UNC           0 :             return false;
   47 tgl@sss.pgh.pa.us         239                 :GNC         446 :         CFH->private_data = fdopen(dup_fd, mode);
                                240         [ -  + ]:            446 :         if (CFH->private_data == NULL)
                                241                 :                :         {
   47 tgl@sss.pgh.pa.us         242                 :UNC           0 :             close(dup_fd);
                                243                 :              0 :             return false;
                                244                 :                :         }
                                245                 :                :     }
                                246                 :                :     else
                                247                 :                :     {
 1167 tomas.vondra@postgre      248                 :CBC         303 :         CFH->private_data = fopen(path, mode);
   47 tgl@sss.pgh.pa.us         249         [ -  + ]:GNC         303 :         if (CFH->private_data == NULL)
   47 tgl@sss.pgh.pa.us         250                 :UNC           0 :             return false;
                                251                 :                :     }
                                252                 :                : 
 1139 tomas.vondra@postgre      253                 :CBC         749 :     return true;
                                254                 :                : }
                                255                 :                : 
                                256                 :                : static bool
 1167                           257                 :             73 : open_write_none(const char *path, const char *mode, CompressFileHandle *CFH)
                                258                 :                : {
                                259         [ -  + ]:             73 :     Assert(CFH->private_data == NULL);
                                260                 :                : 
                                261                 :             73 :     CFH->private_data = fopen(path, mode);
                                262         [ -  + ]:             73 :     if (CFH->private_data == NULL)
 1139 tomas.vondra@postgre      263                 :UBC           0 :         return false;
                                264                 :                : 
 1139 tomas.vondra@postgre      265                 :CBC          73 :     return true;
                                266                 :                : }
                                267                 :                : 
                                268                 :                : /*
                                269                 :                :  * Public interface
                                270                 :                :  */
                                271                 :                : 
                                272                 :                : void
 1167                           273                 :            822 : InitCompressFileHandleNone(CompressFileHandle *CFH,
                                274                 :                :                            const pg_compress_specification compression_spec)
                                275                 :                : {
                                276                 :            822 :     CFH->open_func = open_none;
                                277                 :            822 :     CFH->open_write_func = open_write_none;
                                278                 :            822 :     CFH->read_func = read_none;
                                279                 :            822 :     CFH->write_func = write_none;
                                280                 :            822 :     CFH->gets_func = gets_none;
                                281                 :            822 :     CFH->getc_func = getc_none;
                                282                 :            822 :     CFH->close_func = close_none;
                                283                 :            822 :     CFH->eof_func = eof_none;
                                284                 :            822 :     CFH->get_error_func = get_error_none;
                                285                 :                : 
                                286                 :            822 :     CFH->private_data = NULL;
                                287                 :            822 : }
        

Generated by: LCOV version 2.5.0-beta