LCOV - differential code coverage report
Current view: top level - src/bin/pg_basebackup - walmethods.c (source / functions) Coverage Total Hit UBC CBC
Current: 4b3c9325757dfd8f2312e9f1a8decf4431f344ae vs e87b024bfb8dd55e3049b1b6e6ab4e810cf1abc8 Lines: 62.9 % 531 334 197 334
Current Date: 2026-11-28 16:48:44 +0900 Functions: 82.6 % 23 19 4 19
Baseline: lcov-20261128-baseline Branches: 53.8 % 314 169 145 169
Baseline Date: 2026-11-28 16:48:48 +0900 Line coverage date bins:
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
(1,7] days: 40.0 % 5 2 3 2
(7,30] days: 33.3 % 6 2 4 2
(30,360] days: 100.0 % 8 8 8
(360..) days: 62.9 % 512 322 190 322
Function coverage date bins:
(360..) days: 82.6 % 23 19 4 19
Branch coverage date bins:
(1,7] days: 30.0 % 10 3 7 3
(360..) days: 54.6 % 304 166 138 166

 Age         Owner                    Branch data    TLA  Line data    Source code
                                  1                 :                : /*-------------------------------------------------------------------------
                                  2                 :                :  *
                                  3                 :                :  * walmethods.c - implementations of different ways to write received wal
                                  4                 :                :  *
                                  5                 :                :  * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group
                                  6                 :                :  *
                                  7                 :                :  * IDENTIFICATION
                                  8                 :                :  *        src/bin/pg_basebackup/walmethods.c
                                  9                 :                :  *-------------------------------------------------------------------------
                                 10                 :                :  */
                                 11                 :                : 
                                 12                 :                : #include "postgres_fe.h"
                                 13                 :                : 
                                 14                 :                : #include <fcntl.h>
                                 15                 :                : #include <sys/stat.h>
                                 16                 :                : #include <time.h>
                                 17                 :                : #include <unistd.h>
                                 18                 :                : 
                                 19                 :                : #ifdef USE_LZ4
                                 20                 :                : #include <lz4frame.h>
                                 21                 :                : #endif
                                 22                 :                : #ifdef HAVE_LIBZ
                                 23                 :                : #include <zlib.h>
                                 24                 :                : #endif
                                 25                 :                : 
                                 26                 :                : #include "common/file_perm.h"
                                 27                 :                : #include "common/file_utils.h"
                                 28                 :                : #include "common/logging.h"
                                 29                 :                : #include "pgtar.h"
                                 30                 :                : #include "walmethods.h"
                                 31                 :                : 
                                 32                 :                : /* Size of zlib buffer for .tar.gz */
                                 33                 :                : #define ZLIB_OUT_SIZE 4096
                                 34                 :                : 
                                 35                 :                : /* Size of LZ4 input chunk for .lz4 */
                                 36                 :                : #define LZ4_IN_SIZE  4096
                                 37                 :                : 
                                 38                 :                : /*-------------------------------------------------------------------------
                                 39                 :                :  * WalDirectoryMethod - write wal to a directory looking like pg_wal
                                 40                 :                :  *-------------------------------------------------------------------------
                                 41                 :                :  */
                                 42                 :                : 
                                 43                 :                : static Walfile *dir_open_for_write(WalWriteMethod *wwmethod,
                                 44                 :                :                                    const char *pathname,
                                 45                 :                :                                    const char *temp_suffix,
                                 46                 :                :                                    size_t pad_to_size);
                                 47                 :                : static int  dir_close(Walfile *f, WalCloseMethod method);
                                 48                 :                : static bool dir_existsfile(WalWriteMethod *wwmethod, const char *pathname);
                                 49                 :                : static ssize_t dir_get_file_size(WalWriteMethod *wwmethod,
                                 50                 :                :                                  const char *pathname);
                                 51                 :                : static char *dir_get_file_name(WalWriteMethod *wwmethod,
                                 52                 :                :                                const char *pathname, const char *temp_suffix);
                                 53                 :                : static ssize_t dir_write(Walfile *f, const void *buf, size_t count);
                                 54                 :                : static int  dir_sync(Walfile *f);
                                 55                 :                : static bool dir_finish(WalWriteMethod *wwmethod);
                                 56                 :                : static void dir_free(WalWriteMethod *wwmethod);
                                 57                 :                : 
                                 58                 :                : static const WalWriteMethodOps WalDirectoryMethodOps = {
                                 59                 :                :     .open_for_write = dir_open_for_write,
                                 60                 :                :     .close = dir_close,
                                 61                 :                :     .existsfile = dir_existsfile,
                                 62                 :                :     .get_file_size = dir_get_file_size,
                                 63                 :                :     .get_file_name = dir_get_file_name,
                                 64                 :                :     .write = dir_write,
                                 65                 :                :     .sync = dir_sync,
                                 66                 :                :     .finish = dir_finish,
                                 67                 :                :     .free = dir_free
                                 68                 :                : };
                                 69                 :                : 
                                 70                 :                : /*
                                 71                 :                :  * Global static data for this method
                                 72                 :                :  */
                                 73                 :                : typedef struct DirectoryMethodData
                                 74                 :                : {
                                 75                 :                :     WalWriteMethod base;
                                 76                 :                :     char       *basedir;
                                 77                 :                : } DirectoryMethodData;
                                 78                 :                : 
                                 79                 :                : /*
                                 80                 :                :  * Local file handle
                                 81                 :                :  */
                                 82                 :                : typedef struct DirectoryMethodFile
                                 83                 :                : {
                                 84                 :                :     Walfile     base;
                                 85                 :                :     int         fd;
                                 86                 :                :     char       *fullpath;
                                 87                 :                :     char       *temp_suffix;
                                 88                 :                : #ifdef HAVE_LIBZ
                                 89                 :                :     gzFile      gzfp;
                                 90                 :                : #endif
                                 91                 :                : #ifdef USE_LZ4
                                 92                 :                :     LZ4F_compressionContext_t ctx;
                                 93                 :                :     size_t      lz4bufsize;
                                 94                 :                :     void       *lz4buf;
                                 95                 :                : #endif
                                 96                 :                : } DirectoryMethodFile;
                                 97                 :                : 
                                 98                 :                : #define clear_error(wwmethod) \
                                 99                 :                :     ((wwmethod)->lasterrstring = NULL, (wwmethod)->lasterrno = 0)
                                100                 :                : 
                                101                 :                : static char *
 1531 rhaas@postgresql.org      102                 :CBC         539 : dir_get_file_name(WalWriteMethod *wwmethod,
                                103                 :                :                   const char *pathname, const char *temp_suffix)
                                104                 :                : {
  274 michael@paquier.xyz       105                 :            539 :     char       *filename = pg_malloc0_array(char, MAXPGPATH);
                                106                 :                : 
 1957                           107         [ +  + ]:           1078 :     snprintf(filename, MAXPGPATH, "%s%s%s",
                                108                 :                :              pathname,
 1531 rhaas@postgresql.org      109         [ +  + ]:            539 :              wwmethod->compression_algorithm == PG_COMPRESSION_GZIP ? ".gz" :
                                110         [ +  + ]:            531 :              wwmethod->compression_algorithm == PG_COMPRESSION_LZ4 ? ".lz4" : "",
                                111                 :                :              temp_suffix ? temp_suffix : "");
                                112                 :                : 
 1957 michael@paquier.xyz       113                 :            539 :     return filename;
                                114                 :                : }
                                115                 :                : 
                                116                 :                : static Walfile *
 1531 rhaas@postgresql.org      117                 :            183 : dir_open_for_write(WalWriteMethod *wwmethod, const char *pathname,
                                118                 :                :                    const char *temp_suffix, size_t pad_to_size)
                                119                 :                : {
                                120                 :            183 :     DirectoryMethodData *dir_data = (DirectoryMethodData *) wwmethod;
                                121                 :                :     char        tmppath[MAXPGPATH];
                                122                 :                :     char       *filename;
                                123                 :                :     int         fd;
                                124                 :                :     DirectoryMethodFile *f;
                                125                 :                : #ifdef HAVE_LIBZ
 3602 magnus@hagander.net       126                 :            183 :     gzFile      gzfp = NULL;
                                127                 :                : #endif
                                128                 :                : #ifdef USE_LZ4
 1849 michael@paquier.xyz       129                 :            183 :     LZ4F_compressionContext_t ctx = NULL;
                                130                 :            183 :     size_t      lz4bufsize = 0;
                                131                 :            183 :     void       *lz4buf = NULL;
                                132                 :                : #endif
                                133                 :                : 
 1531 rhaas@postgresql.org      134                 :            183 :     clear_error(wwmethod);
                                135                 :                : 
                                136                 :            183 :     filename = dir_get_file_name(wwmethod, pathname, temp_suffix);
 1957 michael@paquier.xyz       137                 :            183 :     snprintf(tmppath, sizeof(tmppath), "%s/%s",
                                138                 :                :              dir_data->basedir, filename);
 1951                           139                 :            183 :     pg_free(filename);
                                140                 :                : 
                                141                 :                :     /*
                                142                 :                :      * Open a file for non-compressed as well as compressed files. Tracking
                                143                 :                :      * the file descriptor is important for dir_sync() method as gzflush()
                                144                 :                :      * does not do any system calls to fsync() to make changes permanent on
                                145                 :                :      * disk.
                                146                 :                :      */
 3157 sfrost@snowman.net        147                 :            183 :     fd = open(tmppath, O_WRONLY | O_CREAT | PG_BINARY, pg_file_create_mode);
 3688 magnus@hagander.net       148         [ -  + ]:            183 :     if (fd < 0)
                                149                 :                :     {
 1531 rhaas@postgresql.org      150                 :UBC           0 :         wwmethod->lasterrno = errno;
 3688 magnus@hagander.net       151                 :              0 :         return NULL;
                                152                 :                :     }
                                153                 :                : 
                                154                 :                : #ifdef HAVE_LIBZ
 1531 rhaas@postgresql.org      155         [ +  + ]:CBC         183 :     if (wwmethod->compression_algorithm == PG_COMPRESSION_GZIP)
                                156                 :                :     {
 3602 magnus@hagander.net       157                 :              2 :         gzfp = gzdopen(fd, "wb");
                                158         [ -  + ]:              2 :         if (gzfp == NULL)
                                159                 :                :         {
 1531 rhaas@postgresql.org      160                 :UBC           0 :             wwmethod->lasterrno = errno;
 3602 magnus@hagander.net       161                 :              0 :             close(fd);
                                162                 :              0 :             return NULL;
                                163                 :                :         }
                                164                 :                : 
 1531 rhaas@postgresql.org      165         [ -  + ]:CBC           2 :         if (gzsetparams(gzfp, wwmethod->compression_level,
                                166                 :                :                         Z_DEFAULT_STRATEGY) != Z_OK)
                                167                 :                :         {
 1531 rhaas@postgresql.org      168                 :UBC           0 :             wwmethod->lasterrno = errno;
 3602 magnus@hagander.net       169                 :              0 :             gzclose(gzfp);
                                170                 :              0 :             return NULL;
                                171                 :                :         }
                                172                 :                :     }
                                173                 :                : #endif
                                174                 :                : #ifdef USE_LZ4
 1531 rhaas@postgresql.org      175         [ +  + ]:CBC         183 :     if (wwmethod->compression_algorithm == PG_COMPRESSION_LZ4)
                                176                 :                :     {
                                177                 :                :         size_t      ctx_out;
                                178                 :                :         size_t      header_size;
                                179                 :                :         LZ4F_preferences_t prefs;
                                180                 :                : 
 1849 michael@paquier.xyz       181                 :              2 :         ctx_out = LZ4F_createCompressionContext(&ctx, LZ4F_VERSION);
                                182         [ -  + ]:              2 :         if (LZ4F_isError(ctx_out))
                                183                 :                :         {
 1531 rhaas@postgresql.org      184                 :UBC           0 :             wwmethod->lasterrstring = LZ4F_getErrorName(ctx_out);
 1849 michael@paquier.xyz       185                 :              0 :             close(fd);
                                186                 :              0 :             return NULL;
                                187                 :                :         }
                                188                 :                : 
 1849 michael@paquier.xyz       189                 :CBC           2 :         lz4bufsize = LZ4F_compressBound(LZ4_IN_SIZE, NULL);
                                190                 :              2 :         lz4buf = pg_malloc0(lz4bufsize);
                                191                 :                : 
                                192                 :                :         /* assign the compression level, default is 0 */
 1685                           193                 :              2 :         memset(&prefs, 0, sizeof(prefs));
 1531 rhaas@postgresql.org      194                 :              2 :         prefs.compressionLevel = wwmethod->compression_level;
                                195                 :                : 
                                196                 :                :         /* add the header */
 1685 michael@paquier.xyz       197                 :              2 :         header_size = LZ4F_compressBegin(ctx, lz4buf, lz4bufsize, &prefs);
 1849                           198         [ -  + ]:              2 :         if (LZ4F_isError(header_size))
                                199                 :                :         {
 1531 rhaas@postgresql.org      200                 :UBC           0 :             wwmethod->lasterrstring = LZ4F_getErrorName(header_size);
 1849 michael@paquier.xyz       201                 :              0 :             (void) LZ4F_freeCompressionContext(ctx);
                                202                 :              0 :             pg_free(lz4buf);
                                203                 :              0 :             close(fd);
                                204                 :              0 :             return NULL;
                                205                 :                :         }
                                206                 :                : 
 1849 michael@paquier.xyz       207                 :CBC           2 :         errno = 0;
                                208         [ -  + ]:              2 :         if (write(fd, lz4buf, header_size) != header_size)
                                209                 :                :         {
                                210                 :                :             /* If write didn't set errno, assume problem is no disk space */
 1531 rhaas@postgresql.org      211         [ #  # ]:UBC           0 :             wwmethod->lasterrno = errno ? errno : ENOSPC;
 1849 michael@paquier.xyz       212                 :              0 :             (void) LZ4F_freeCompressionContext(ctx);
                                213                 :              0 :             pg_free(lz4buf);
                                214                 :              0 :             close(fd);
                                215                 :              0 :             return NULL;
                                216                 :                :         }
                                217                 :                :     }
                                218                 :                : #endif
                                219                 :                : 
                                220                 :                :     /* Do pre-padding on non-compressed files */
 1531 rhaas@postgresql.org      221   [ +  +  +  + ]:CBC         183 :     if (pad_to_size && wwmethod->compression_algorithm == PG_COMPRESSION_NONE)
                                222                 :                :     {
                                223                 :                :         ssize_t     rc;
                                224                 :                : 
 1363 michael@paquier.xyz       225                 :            163 :         rc = pg_pwrite_zeros(fd, pad_to_size, 0);
                                226                 :                : 
 1481                           227         [ -  + ]:            163 :         if (rc < 0)
                                228                 :                :         {
 1481 michael@paquier.xyz       229                 :UBC           0 :             wwmethod->lasterrno = errno;
                                230                 :              0 :             close(fd);
                                231                 :              0 :             return NULL;
                                232                 :                :         }
                                233                 :                : 
                                234                 :                :         /*
                                235                 :                :          * pg_pwrite() (called via pg_pwrite_zeros()) may have moved the file
                                236                 :                :          * position, so reset it (see win32pwrite.c).
                                237                 :                :          */
 3688 magnus@hagander.net       238         [ -  + ]:CBC         163 :         if (lseek(fd, 0, SEEK_SET) != 0)
                                239                 :                :         {
 1531 rhaas@postgresql.org      240                 :UBC           0 :             wwmethod->lasterrno = errno;
 3688 magnus@hagander.net       241                 :              0 :             close(fd);
                                242                 :              0 :             return NULL;
                                243                 :                :         }
                                244                 :                :     }
                                245                 :                : 
                                246                 :                :     /*
                                247                 :                :      * fsync WAL file and containing directory, to ensure the file is
                                248                 :                :      * persistently created and zeroed (if padded). That's particularly
                                249                 :                :      * important when using synchronous mode, where the file is modified and
                                250                 :                :      * fsynced in-place, without a directory fsync.
                                251                 :                :      */
 1531 rhaas@postgresql.org      252         [ +  + ]:CBC         183 :     if (wwmethod->sync)
                                253                 :                :     {
 2798 peter@eisentraut.org      254   [ +  -  -  + ]:             14 :         if (fsync_fname(tmppath, false) != 0 ||
                                255                 :              7 :             fsync_parent_path(tmppath) != 0)
                                256                 :                :         {
 1531 rhaas@postgresql.org      257                 :UBC           0 :             wwmethod->lasterrno = errno;
                                258                 :                : #ifdef HAVE_LIBZ
                                259         [ #  # ]:              0 :             if (wwmethod->compression_algorithm == PG_COMPRESSION_GZIP)
 3602 magnus@hagander.net       260                 :              0 :                 gzclose(gzfp);
                                261                 :                :             else
                                262                 :                : #endif
                                263                 :                : #ifdef USE_LZ4
 1531 rhaas@postgresql.org      264         [ #  # ]:              0 :             if (wwmethod->compression_algorithm == PG_COMPRESSION_LZ4)
                                265                 :                :             {
 1849 michael@paquier.xyz       266                 :              0 :                 (void) LZ4F_compressEnd(ctx, lz4buf, lz4bufsize, NULL);
                                267                 :              0 :                 (void) LZ4F_freeCompressionContext(ctx);
                                268                 :              0 :                 pg_free(lz4buf);
                                269                 :              0 :                 close(fd);
                                270                 :                :             }
                                271                 :                :             else
                                272                 :                : #endif
 3602 magnus@hagander.net       273                 :              0 :                 close(fd);
 3688                           274                 :              0 :             return NULL;
                                275                 :                :         }
                                276                 :                :     }
                                277                 :                : 
  274 michael@paquier.xyz       278                 :CBC         183 :     f = pg_malloc0_object(DirectoryMethodFile);
                                279                 :                : #ifdef HAVE_LIBZ
 1531 rhaas@postgresql.org      280         [ +  + ]:            183 :     if (wwmethod->compression_algorithm == PG_COMPRESSION_GZIP)
 3602 magnus@hagander.net       281                 :              2 :         f->gzfp = gzfp;
                                282                 :                : #endif
                                283                 :                : #ifdef USE_LZ4
 1531 rhaas@postgresql.org      284         [ +  + ]:            183 :     if (wwmethod->compression_algorithm == PG_COMPRESSION_LZ4)
                                285                 :                :     {
 1849 michael@paquier.xyz       286                 :              2 :         f->ctx = ctx;
                                287                 :              2 :         f->lz4buf = lz4buf;
                                288                 :              2 :         f->lz4bufsize = lz4bufsize;
                                289                 :                :     }
                                290                 :                : #endif
                                291                 :                : 
 1531 rhaas@postgresql.org      292                 :            183 :     f->base.wwmethod = wwmethod;
                                293                 :            183 :     f->base.currpos = 0;
                                294                 :            183 :     f->base.pathname = pg_strdup(pathname);
 3688 magnus@hagander.net       295                 :            183 :     f->fd = fd;
                                296                 :            183 :     f->fullpath = pg_strdup(tmppath);
                                297         [ +  + ]:            183 :     if (temp_suffix)
                                298                 :             17 :         f->temp_suffix = pg_strdup(temp_suffix);
                                299                 :                : 
 1531 rhaas@postgresql.org      300                 :            183 :     return &f->base;
                                301                 :                : }
                                302                 :                : 
                                303                 :                : static ssize_t
                                304                 :           1825 : dir_write(Walfile *f, const void *buf, size_t count)
                                305                 :                : {
                                306                 :                :     ssize_t     r;
 3688 magnus@hagander.net       307                 :           1825 :     DirectoryMethodFile *df = (DirectoryMethodFile *) f;
                                308                 :                : 
                                309         [ -  + ]:           1825 :     Assert(f != NULL);
 1531 rhaas@postgresql.org      310                 :           1825 :     clear_error(f->wwmethod);
                                311                 :                : 
                                312                 :                : #ifdef HAVE_LIBZ
                                313         [ +  + ]:           1825 :     if (f->wwmethod->compression_algorithm == PG_COMPRESSION_GZIP)
                                314                 :                :     {
 1837 tgl@sss.pgh.pa.us         315                 :              9 :         errno = 0;
 3602 magnus@hagander.net       316                 :              9 :         r = (ssize_t) gzwrite(df->gzfp, buf, count);
 1837 tgl@sss.pgh.pa.us         317         [ -  + ]:              9 :         if (r != count)
                                318                 :                :         {
                                319                 :                :             /* If write didn't set errno, assume problem is no disk space */
 1531 rhaas@postgresql.org      320         [ #  # ]:UBC           0 :             f->wwmethod->lasterrno = errno ? errno : ENOSPC;
                                321                 :                :         }
                                322                 :                :     }
                                323                 :                :     else
                                324                 :                : #endif
                                325                 :                : #ifdef USE_LZ4
 1531 rhaas@postgresql.org      326         [ +  + ]:CBC        1816 :     if (f->wwmethod->compression_algorithm == PG_COMPRESSION_LZ4)
                                327                 :                :     {
                                328                 :                :         size_t      chunk;
                                329                 :                :         size_t      remaining;
 1849 michael@paquier.xyz       330                 :              9 :         const void *inbuf = buf;
                                331                 :                : 
                                332                 :              9 :         remaining = count;
                                333         [ +  + ]:            266 :         while (remaining > 0)
                                334                 :                :         {
                                335                 :                :             size_t      compressed;
                                336                 :                : 
                                337         [ +  + ]:            257 :             if (remaining > LZ4_IN_SIZE)
                                338                 :            248 :                 chunk = LZ4_IN_SIZE;
                                339                 :                :             else
                                340                 :              9 :                 chunk = remaining;
                                341                 :                : 
                                342                 :            257 :             remaining -= chunk;
                                343                 :            257 :             compressed = LZ4F_compressUpdate(df->ctx,
                                344                 :                :                                              df->lz4buf, df->lz4bufsize,
                                345                 :                :                                              inbuf, chunk,
                                346                 :                :                                              NULL);
                                347                 :                : 
                                348         [ -  + ]:            257 :             if (LZ4F_isError(compressed))
                                349                 :                :             {
 1531 rhaas@postgresql.org      350                 :UBC           0 :                 f->wwmethod->lasterrstring = LZ4F_getErrorName(compressed);
 1849 michael@paquier.xyz       351                 :              0 :                 return -1;
                                352                 :                :             }
                                353                 :                : 
 1837 tgl@sss.pgh.pa.us         354                 :CBC         257 :             errno = 0;
 1849 michael@paquier.xyz       355         [ -  + ]:            257 :             if (write(df->fd, df->lz4buf, compressed) != compressed)
                                356                 :                :             {
                                357                 :                :                 /* If write didn't set errno, assume problem is no disk space */
 1531 rhaas@postgresql.org      358         [ #  # ]:UBC           0 :                 f->wwmethod->lasterrno = errno ? errno : ENOSPC;
 1849 michael@paquier.xyz       359                 :              0 :                 return -1;
                                360                 :                :             }
                                361                 :                : 
  306 peter@eisentraut.org      362                 :CBC         257 :             inbuf = ((const char *) inbuf) + chunk;
                                363                 :                :         }
                                364                 :                : 
                                365                 :                :         /* Our caller keeps track of the uncompressed size. */
 1849 michael@paquier.xyz       366                 :              9 :         r = (ssize_t) count;
                                367                 :                :     }
                                368                 :                :     else
                                369                 :                : #endif
                                370                 :                :     {
 1837 tgl@sss.pgh.pa.us         371                 :           1807 :         errno = 0;
 3602 magnus@hagander.net       372                 :           1807 :         r = write(df->fd, buf, count);
 1837 tgl@sss.pgh.pa.us         373         [ -  + ]:           1807 :         if (r != count)
                                374                 :                :         {
                                375                 :                :             /* If write didn't set errno, assume problem is no disk space */
 1531 rhaas@postgresql.org      376         [ #  # ]:UBC           0 :             f->wwmethod->lasterrno = errno ? errno : ENOSPC;
                                377                 :                :         }
                                378                 :                :     }
 3688 magnus@hagander.net       379         [ +  - ]:CBC        1825 :     if (r > 0)
 1531 rhaas@postgresql.org      380                 :           1825 :         df->base.currpos += r;
 3688 magnus@hagander.net       381                 :           1825 :     return r;
                                382                 :                : }
                                383                 :                : 
                                384                 :                : static int
 1531 rhaas@postgresql.org      385                 :            183 : dir_close(Walfile *f, WalCloseMethod method)
                                386                 :                : {
    8 fujii@postgresql.org      387                 :            183 :     int         r = -1;
 3688 magnus@hagander.net       388                 :            183 :     DirectoryMethodFile *df = (DirectoryMethodFile *) f;
 1531 rhaas@postgresql.org      389                 :            183 :     DirectoryMethodData *dir_data = (DirectoryMethodData *) f->wwmethod;
                                390                 :                :     char        tmppath[MAXPGPATH];
                                391                 :                :     char        tmppath2[MAXPGPATH];
                                392                 :                : 
 3688 magnus@hagander.net       393         [ -  + ]:            183 :     Assert(f != NULL);
 1531 rhaas@postgresql.org      394                 :            183 :     clear_error(f->wwmethod);
                                395                 :                : 
                                396                 :                : #ifdef HAVE_LIBZ
                                397         [ +  + ]:            183 :     if (f->wwmethod->compression_algorithm == PG_COMPRESSION_GZIP)
                                398                 :                :     {
 1837 tgl@sss.pgh.pa.us         399                 :              2 :         errno = 0;              /* in case gzclose() doesn't set it */
 3602 magnus@hagander.net       400                 :              2 :         r = gzclose(df->gzfp);
                                401                 :                :     }
                                402                 :                :     else
                                403                 :                : #endif
                                404                 :                : #ifdef USE_LZ4
 1531 rhaas@postgresql.org      405         [ +  + ]:            181 :     if (f->wwmethod->compression_algorithm == PG_COMPRESSION_LZ4)
                                406                 :                :     {
                                407                 :                :         size_t      compressed;
                                408                 :                : 
 1849 michael@paquier.xyz       409                 :              2 :         compressed = LZ4F_compressEnd(df->ctx,
                                410                 :                :                                       df->lz4buf, df->lz4bufsize,
                                411                 :                :                                       NULL);
                                412                 :                : 
                                413         [ -  + ]:              2 :         if (LZ4F_isError(compressed))
                                414                 :                :         {
 1531 rhaas@postgresql.org      415                 :UBC           0 :             f->wwmethod->lasterrstring = LZ4F_getErrorName(compressed);
    8 fujii@postgresql.org      416                 :              0 :             (void) close(df->fd);
                                417                 :              0 :             goto cleanup;
                                418                 :                :         }
                                419                 :                : 
 1837 tgl@sss.pgh.pa.us         420                 :CBC           2 :         errno = 0;
 1849 michael@paquier.xyz       421         [ -  + ]:              2 :         if (write(df->fd, df->lz4buf, compressed) != compressed)
                                422                 :                :         {
                                423                 :                :             /* If write didn't set errno, assume problem is no disk space */
 1531 rhaas@postgresql.org      424         [ #  # ]:UBC           0 :             f->wwmethod->lasterrno = errno ? errno : ENOSPC;
    8 fujii@postgresql.org      425                 :              0 :             (void) close(df->fd);
                                426                 :              0 :             goto cleanup;
                                427                 :                :         }
                                428                 :                : 
 1849 michael@paquier.xyz       429                 :CBC           2 :         r = close(df->fd);
                                430                 :                :     }
                                431                 :                :     else
                                432                 :                : #endif
 3602 magnus@hagander.net       433                 :            179 :         r = close(df->fd);
                                434                 :                : 
    3 fujii@postgresql.org      435   [ +  -  +  - ]:            183 :     if (r == 0 && method != CLOSE_UNLINK)
                                436                 :                :     {
                                437                 :                :         /* Build path to the current version of the file */
 3688 magnus@hagander.net       438   [ +  +  +  + ]:            183 :         if (method == CLOSE_NORMAL && df->temp_suffix)
                                439                 :             11 :         {
                                440                 :                :             char       *filename;
                                441                 :                :             char       *filename2;
                                442                 :                : 
                                443                 :                :             /*
                                444                 :                :              * If we have a temp prefix, normal operation is to rename the
                                445                 :                :              * file.
                                446                 :                :              */
 1531 rhaas@postgresql.org      447                 :             11 :             filename = dir_get_file_name(f->wwmethod, df->base.pathname,
                                448                 :             11 :                                          df->temp_suffix);
 1957 michael@paquier.xyz       449                 :             11 :             snprintf(tmppath, sizeof(tmppath), "%s/%s",
                                450                 :                :                      dir_data->basedir, filename);
 1951                           451                 :             11 :             pg_free(filename);
                                452                 :                : 
                                453                 :                :             /* permanent name, so no need for the prefix */
 1531 rhaas@postgresql.org      454                 :             11 :             filename2 = dir_get_file_name(f->wwmethod, df->base.pathname, NULL);
 1957 michael@paquier.xyz       455                 :             11 :             snprintf(tmppath2, sizeof(tmppath2), "%s/%s",
                                456                 :                :                      dir_data->basedir, filename2);
 1951                           457                 :             11 :             pg_free(filename2);
 1531 rhaas@postgresql.org      458         [ +  + ]:             11 :             if (f->wwmethod->sync)
 1770 andres@anarazel.de        459                 :              3 :                 r = durable_rename(tmppath, tmppath2);
                                460                 :                :             else
                                461                 :                :             {
                                462         [ -  + ]:              8 :                 if (rename(tmppath, tmppath2) != 0)
                                463                 :                :                 {
 1770 andres@anarazel.de        464                 :UBC           0 :                     pg_log_error("could not rename file \"%s\" to \"%s\": %m",
                                465                 :                :                                  tmppath, tmppath2);
                                466                 :              0 :                     r = -1;
                                467                 :                :                 }
                                468                 :                :             }
                                469                 :                :         }
                                470                 :                :         else
                                471                 :                :         {
                                472                 :                :             /*
                                473                 :                :              * Else either CLOSE_NORMAL and no temp suffix, or
                                474                 :                :              * CLOSE_NO_RENAME. In this case, fsync the file and containing
                                475                 :                :              * directory if sync mode is requested.
                                476                 :                :              */
 1531 rhaas@postgresql.org      477         [ +  + ]:CBC         172 :             if (f->wwmethod->sync)
                                478                 :                :             {
 2798 peter@eisentraut.org      479                 :              4 :                 r = fsync_fname(df->fullpath, false);
 3688 magnus@hagander.net       480         [ +  - ]:              4 :                 if (r == 0)
 2798 peter@eisentraut.org      481                 :              4 :                     r = fsync_parent_path(df->fullpath);
                                482                 :                :             }
                                483                 :                :         }
                                484                 :                :     }
                                485                 :                : 
 1837 tgl@sss.pgh.pa.us         486         [ +  - ]:            183 :     if (r != 0)
 1531 rhaas@postgresql.org      487                 :UBC           0 :         f->wwmethod->lasterrno = errno;
                                488                 :                : 
                                489                 :                : #ifdef USE_LZ4
    8 fujii@postgresql.org      490                 :CBC         183 : cleanup:
 1849 michael@paquier.xyz       491                 :            183 :     pg_free(df->lz4buf);
                                492                 :                :     /* supports free on NULL */
                                493                 :            183 :     LZ4F_freeCompressionContext(df->ctx);
                                494                 :                : #endif
                                495                 :                : 
    3 fujii@postgresql.org      496         [ -  + ]:            183 :     if (method == CLOSE_UNLINK)
                                497                 :                :     {
                                498                 :                :         /*
                                499                 :                :          * If unlink fails, report its error if the preceding operations
                                500                 :                :          * succeeded; otherwise, preserve the earlier error.
                                501                 :                :          */
    3 fujii@postgresql.org      502   [ #  #  #  # ]:UBC           0 :         if (unlink(df->fullpath) != 0 && r == 0)
                                503                 :                :         {
                                504                 :              0 :             f->wwmethod->lasterrno = errno;
                                505                 :              0 :             r = -1;
                                506                 :                :         }
                                507                 :                :     }
                                508                 :                : 
 1531 rhaas@postgresql.org      509                 :CBC         183 :     pg_free(df->base.pathname);
 3688 magnus@hagander.net       510                 :            183 :     pg_free(df->fullpath);
 1625 peter@eisentraut.org      511                 :            183 :     pg_free(df->temp_suffix);
 3688 magnus@hagander.net       512                 :            183 :     pg_free(df);
                                513                 :                : 
                                514                 :            183 :     return r;
                                515                 :                : }
                                516                 :                : 
                                517                 :                : static int
 1531 rhaas@postgresql.org      518                 :UBC           0 : dir_sync(Walfile *f)
                                519                 :                : {
                                520                 :                :     int         r;
                                521                 :                : 
 3688 magnus@hagander.net       522         [ #  # ]:              0 :     Assert(f != NULL);
 1531 rhaas@postgresql.org      523                 :              0 :     clear_error(f->wwmethod);
                                524                 :                : 
                                525         [ #  # ]:              0 :     if (!f->wwmethod->sync)
 3688 magnus@hagander.net       526                 :              0 :         return 0;
                                527                 :                : 
                                528                 :                : #ifdef HAVE_LIBZ
 1531 rhaas@postgresql.org      529         [ #  # ]:              0 :     if (f->wwmethod->compression_algorithm == PG_COMPRESSION_GZIP)
                                530                 :                :     {
 3602 magnus@hagander.net       531         [ #  # ]:              0 :         if (gzflush(((DirectoryMethodFile *) f)->gzfp, Z_SYNC_FLUSH) != Z_OK)
                                532                 :                :         {
 1531 rhaas@postgresql.org      533                 :              0 :             f->wwmethod->lasterrno = errno;
 3602 magnus@hagander.net       534                 :              0 :             return -1;
                                535                 :                :         }
                                536                 :                :     }
                                537                 :                : #endif
                                538                 :                : #ifdef USE_LZ4
 1531 rhaas@postgresql.org      539         [ #  # ]:              0 :     if (f->wwmethod->compression_algorithm == PG_COMPRESSION_LZ4)
                                540                 :                :     {
 1849 michael@paquier.xyz       541                 :              0 :         DirectoryMethodFile *df = (DirectoryMethodFile *) f;
                                542                 :                :         size_t      compressed;
                                543                 :                : 
                                544                 :                :         /* Flush any internal buffers */
                                545                 :              0 :         compressed = LZ4F_flush(df->ctx, df->lz4buf, df->lz4bufsize, NULL);
                                546         [ #  # ]:              0 :         if (LZ4F_isError(compressed))
                                547                 :                :         {
 1531 rhaas@postgresql.org      548                 :              0 :             f->wwmethod->lasterrstring = LZ4F_getErrorName(compressed);
 1849 michael@paquier.xyz       549                 :              0 :             return -1;
                                550                 :                :         }
                                551                 :                : 
 1837 tgl@sss.pgh.pa.us         552                 :              0 :         errno = 0;
 1849 michael@paquier.xyz       553         [ #  # ]:              0 :         if (write(df->fd, df->lz4buf, compressed) != compressed)
                                554                 :                :         {
                                555                 :                :             /* If write didn't set errno, assume problem is no disk space */
 1531 rhaas@postgresql.org      556         [ #  # ]:              0 :             f->wwmethod->lasterrno = errno ? errno : ENOSPC;
 1849 michael@paquier.xyz       557                 :              0 :             return -1;
                                558                 :                :         }
                                559                 :                :     }
                                560                 :                : #endif
                                561                 :                : 
 1837 tgl@sss.pgh.pa.us         562                 :              0 :     r = fsync(((DirectoryMethodFile *) f)->fd);
                                563         [ #  # ]:              0 :     if (r < 0)
 1531 rhaas@postgresql.org      564                 :              0 :         f->wwmethod->lasterrno = errno;
 1837 tgl@sss.pgh.pa.us         565                 :              0 :     return r;
                                566                 :                : }
                                567                 :                : 
                                568                 :                : static ssize_t
 1531 rhaas@postgresql.org      569                 :              0 : dir_get_file_size(WalWriteMethod *wwmethod, const char *pathname)
                                570                 :                : {
                                571                 :              0 :     DirectoryMethodData *dir_data = (DirectoryMethodData *) wwmethod;
                                572                 :                :     struct stat statbuf;
                                573                 :                :     char        tmppath[MAXPGPATH];
                                574                 :                : 
 3688 magnus@hagander.net       575                 :              0 :     snprintf(tmppath, sizeof(tmppath), "%s/%s",
                                576                 :                :              dir_data->basedir, pathname);
                                577                 :                : 
                                578         [ #  # ]:              0 :     if (stat(tmppath, &statbuf) != 0)
                                579                 :                :     {
 1531 rhaas@postgresql.org      580                 :              0 :         wwmethod->lasterrno = errno;
 3688 magnus@hagander.net       581                 :              0 :         return -1;
                                582                 :                :     }
                                583                 :                : 
                                584                 :              0 :     return statbuf.st_size;
                                585                 :                : }
                                586                 :                : 
                                587                 :                : static bool
 1531 rhaas@postgresql.org      588                 :CBC         168 : dir_existsfile(WalWriteMethod *wwmethod, const char *pathname)
                                589                 :                : {
                                590                 :            168 :     DirectoryMethodData *dir_data = (DirectoryMethodData *) wwmethod;
                                591                 :                :     char        tmppath[MAXPGPATH];
                                592                 :                :     int         fd;
                                593                 :                : 
                                594                 :            168 :     clear_error(wwmethod);
                                595                 :                : 
 3688 magnus@hagander.net       596                 :            168 :     snprintf(tmppath, sizeof(tmppath), "%s/%s",
                                597                 :                :              dir_data->basedir, pathname);
                                598                 :                : 
                                599                 :            168 :     fd = open(tmppath, O_RDONLY | PG_BINARY, 0);
                                600         [ +  - ]:            168 :     if (fd < 0)
                                601                 :                : 
                                602                 :                :         /*
                                603                 :                :          * Skip setting dir_data->lasterrno here because we are only checking
                                604                 :                :          * for existence.
                                605                 :                :          */
                                606                 :            168 :         return false;
 3688 magnus@hagander.net       607                 :UBC           0 :     close(fd);
                                608                 :              0 :     return true;
                                609                 :                : }
                                610                 :                : 
                                611                 :                : static bool
 1531 rhaas@postgresql.org      612                 :CBC         159 : dir_finish(WalWriteMethod *wwmethod)
                                613                 :                : {
                                614                 :            159 :     clear_error(wwmethod);
                                615                 :                : 
                                616         [ +  + ]:            159 :     if (wwmethod->sync)
                                617                 :                :     {
                                618                 :              4 :         DirectoryMethodData *dir_data = (DirectoryMethodData *) wwmethod;
                                619                 :                : 
                                620                 :                :         /*
                                621                 :                :          * Files are fsynced when they are closed, but we need to fsync the
                                622                 :                :          * directory entry here as well.
                                623                 :                :          */
 2798 peter@eisentraut.org      624         [ -  + ]:              4 :         if (fsync_fname(dir_data->basedir, true) != 0)
                                625                 :                :         {
 1531 rhaas@postgresql.org      626                 :UBC           0 :             wwmethod->lasterrno = errno;
 3688 magnus@hagander.net       627                 :              0 :             return false;
                                628                 :                :         }
                                629                 :                :     }
 3688 magnus@hagander.net       630                 :CBC         159 :     return true;
                                631                 :                : }
                                632                 :                : 
                                633                 :                : static void
 1531 rhaas@postgresql.org      634                 :            159 : dir_free(WalWriteMethod *wwmethod)
                                635                 :                : {
                                636                 :            159 :     DirectoryMethodData *dir_data = (DirectoryMethodData *) wwmethod;
                                637                 :                : 
                                638                 :            159 :     pg_free(dir_data->basedir);
                                639                 :            159 :     pg_free(wwmethod);
                                640                 :            159 : }
                                641                 :                : 
                                642                 :                : 
                                643                 :                : WalWriteMethod *
 1850 michael@paquier.xyz       644                 :            161 : CreateWalDirectoryMethod(const char *basedir,
                                645                 :                :                          pg_compress_algorithm compression_algorithm,
                                646                 :                :                          int compression_level, bool sync)
                                647                 :                : {
                                648                 :                :     DirectoryMethodData *wwmethod;
                                649                 :                : 
  274                           650                 :            161 :     wwmethod = pg_malloc0_object(DirectoryMethodData);
 1531 rhaas@postgresql.org      651                 :            161 :     *((const WalWriteMethodOps **) &wwmethod->base.ops) =
                                652                 :                :         &WalDirectoryMethodOps;
                                653                 :            161 :     wwmethod->base.compression_algorithm = compression_algorithm;
                                654                 :            161 :     wwmethod->base.compression_level = compression_level;
                                655                 :            161 :     wwmethod->base.sync = sync;
                                656                 :            161 :     clear_error(&wwmethod->base);
                                657                 :            161 :     wwmethod->basedir = pg_strdup(basedir);
                                658                 :                : 
                                659                 :            161 :     return &wwmethod->base;
                                660                 :                : }
                                661                 :                : 
                                662                 :                : 
                                663                 :                : /*-------------------------------------------------------------------------
                                664                 :                :  * WalTarMethod - write wal to a tar file containing pg_wal contents
                                665                 :                :  *-------------------------------------------------------------------------
                                666                 :                :  */
                                667                 :                : 
                                668                 :                : static Walfile *tar_open_for_write(WalWriteMethod *wwmethod,
                                669                 :                :                                    const char *pathname,
                                670                 :                :                                    const char *temp_suffix,
                                671                 :                :                                    size_t pad_to_size);
                                672                 :                : static int  tar_close(Walfile *f, WalCloseMethod method);
                                673                 :                : static bool tar_existsfile(WalWriteMethod *wwmethod, const char *pathname);
                                674                 :                : static ssize_t tar_get_file_size(WalWriteMethod *wwmethod,
                                675                 :                :                                  const char *pathname);
                                676                 :                : static char *tar_get_file_name(WalWriteMethod *wwmethod,
                                677                 :                :                                const char *pathname, const char *temp_suffix);
                                678                 :                : static ssize_t tar_write(Walfile *f, const void *buf, size_t count);
                                679                 :                : static int  tar_sync(Walfile *f);
                                680                 :                : static bool tar_finish(WalWriteMethod *wwmethod);
                                681                 :                : static void tar_free(WalWriteMethod *wwmethod);
                                682                 :                : 
                                683                 :                : static const WalWriteMethodOps WalTarMethodOps = {
                                684                 :                :     .open_for_write = tar_open_for_write,
                                685                 :                :     .close = tar_close,
                                686                 :                :     .existsfile = tar_existsfile,
                                687                 :                :     .get_file_size = tar_get_file_size,
                                688                 :                :     .get_file_name = tar_get_file_name,
                                689                 :                :     .write = tar_write,
                                690                 :                :     .sync = tar_sync,
                                691                 :                :     .finish = tar_finish,
                                692                 :                :     .free = tar_free
                                693                 :                : };
                                694                 :                : 
                                695                 :                : typedef struct TarMethodFile
                                696                 :                : {
                                697                 :                :     Walfile     base;
                                698                 :                :     pgoff_t     ofs_start;      /* Where does the *header* for this file start */
                                699                 :                :     char        header[TAR_BLOCK_SIZE];
                                700                 :                :     size_t      pad_to_size;
                                701                 :                : } TarMethodFile;
                                702                 :                : 
                                703                 :                : typedef struct TarMethodData
                                704                 :                : {
                                705                 :                :     WalWriteMethod base;
                                706                 :                :     char       *tarfilename;
                                707                 :                :     int         fd;
                                708                 :                :     TarMethodFile *currentfile;
                                709                 :                : #ifdef HAVE_LIBZ
                                710                 :                :     z_streamp   zp;
                                711                 :                :     void       *zlibOut;
                                712                 :                : #endif
                                713                 :                : } TarMethodData;
                                714                 :                : 
                                715                 :                : #ifdef HAVE_LIBZ
                                716                 :                : static bool
 1209 peter@eisentraut.org      717                 :           8106 : tar_write_compressed_data(TarMethodData *tar_data, const void *buf, size_t count,
                                718                 :                :                           bool flush)
                                719                 :                : {
 3688 magnus@hagander.net       720                 :           8106 :     tar_data->zp->next_in = buf;
                                721                 :           8106 :     tar_data->zp->avail_in = count;
                                722                 :                : 
                                723   [ +  +  +  + ]:          16214 :     while (tar_data->zp->avail_in || flush)
                                724                 :                :     {
                                725                 :                :         int         r;
                                726                 :                : 
                                727         [ +  + ]:           8132 :         r = deflate(tar_data->zp, flush ? Z_FINISH : Z_NO_FLUSH);
                                728         [ -  + ]:           8132 :         if (r == Z_STREAM_ERROR)
                                729                 :                :         {
 1300 peter@eisentraut.org      730                 :UBC           0 :             tar_data->base.lasterrstring = _("could not compress data");
 3688 magnus@hagander.net       731                 :              0 :             return false;
                                732                 :                :         }
                                733                 :                : 
 3688 magnus@hagander.net       734         [ +  + ]:CBC        8132 :         if (tar_data->zp->avail_out < ZLIB_OUT_SIZE)
                                735                 :                :         {
                                736                 :             83 :             size_t      len = ZLIB_OUT_SIZE - tar_data->zp->avail_out;
                                737                 :                : 
 3037 michael@paquier.xyz       738                 :             83 :             errno = 0;
 3688 magnus@hagander.net       739         [ -  + ]:             83 :             if (write(tar_data->fd, tar_data->zlibOut, len) != len)
                                740                 :                :             {
                                741                 :                :                 /* If write didn't set errno, assume problem is no disk space */
 1531 rhaas@postgresql.org      742         [ #  # ]:UBC           0 :                 tar_data->base.lasterrno = errno ? errno : ENOSPC;
 3688 magnus@hagander.net       743                 :              0 :                 return false;
                                744                 :                :             }
                                745                 :                : 
 3688 magnus@hagander.net       746                 :CBC          83 :             tar_data->zp->next_out = tar_data->zlibOut;
                                747                 :             83 :             tar_data->zp->avail_out = ZLIB_OUT_SIZE;
                                748                 :                :         }
                                749                 :                : 
                                750         [ +  + ]:           8132 :         if (r == Z_STREAM_END)
                                751                 :             24 :             break;
                                752                 :                :     }
                                753                 :                : 
                                754         [ +  + ]:           8106 :     if (flush)
                                755                 :                :     {
                                756                 :                :         /* Reset the stream for writing */
                                757         [ -  + ]:             24 :         if (deflateReset(tar_data->zp) != Z_OK)
                                758                 :                :         {
 1300 peter@eisentraut.org      759                 :UBC           0 :             tar_data->base.lasterrstring = _("could not reset compression stream");
 3688 magnus@hagander.net       760                 :              0 :             return false;
                                761                 :                :         }
                                762                 :                :     }
                                763                 :                : 
 3688 magnus@hagander.net       764                 :CBC        8106 :     return true;
                                765                 :                : }
                                766                 :                : #endif
                                767                 :                : 
                                768                 :                : static ssize_t
 1531 rhaas@postgresql.org      769                 :          30631 : tar_write(Walfile *f, const void *buf, size_t count)
                                770                 :                : {
                                771                 :          30631 :     TarMethodData *tar_data = (TarMethodData *) f->wwmethod;
                                772                 :                :     ssize_t     r;
                                773                 :                : 
 3688 magnus@hagander.net       774         [ -  + ]:          30631 :     Assert(f != NULL);
 1531 rhaas@postgresql.org      775                 :          30631 :     clear_error(f->wwmethod);
                                776                 :                : 
                                777                 :                :     /* Tarfile will always be positioned at the end */
                                778         [ +  + ]:          30631 :     if (f->wwmethod->compression_algorithm == PG_COMPRESSION_NONE)
                                779                 :                :     {
 1837 tgl@sss.pgh.pa.us         780                 :          22553 :         errno = 0;
 3688 magnus@hagander.net       781                 :          22553 :         r = write(tar_data->fd, buf, count);
 1837 tgl@sss.pgh.pa.us         782         [ -  + ]:          22553 :         if (r != count)
                                783                 :                :         {
                                784                 :                :             /* If write didn't set errno, assume problem is no disk space */
 1531 rhaas@postgresql.org      785         [ #  # ]:UBC           0 :             f->wwmethod->lasterrno = errno ? errno : ENOSPC;
 1837 tgl@sss.pgh.pa.us         786                 :              0 :             return -1;
                                787                 :                :         }
 1531 rhaas@postgresql.org      788                 :CBC       22553 :         f->currpos += r;
 3688 magnus@hagander.net       789                 :          22553 :         return r;
                                790                 :                :     }
                                791                 :                : #ifdef HAVE_LIBZ
 1531 rhaas@postgresql.org      792         [ +  - ]:           8078 :     else if (f->wwmethod->compression_algorithm == PG_COMPRESSION_GZIP)
                                793                 :                :     {
 1209 peter@eisentraut.org      794         [ -  + ]:           8078 :         if (!tar_write_compressed_data(tar_data, buf, count, false))
 3688 magnus@hagander.net       795                 :UBC           0 :             return -1;
 1531 rhaas@postgresql.org      796                 :CBC        8078 :         f->currpos += count;
 3688 magnus@hagander.net       797                 :           8078 :         return count;
                                798                 :                :     }
                                799                 :                : #endif
                                800                 :                :     else
                                801                 :                :     {
                                802                 :                :         /* Can't happen - compression enabled with no method set */
 1531 rhaas@postgresql.org      803                 :UBC           0 :         f->wwmethod->lasterrno = ENOSYS;
 3688 magnus@hagander.net       804                 :              0 :         return -1;
                                805                 :                :     }
                                806                 :                : }
                                807                 :                : 
                                808                 :                : static bool
 3482 bruce@momjian.us          809                 :CBC          15 : tar_write_padding_data(TarMethodFile *f, size_t bytes)
                                810                 :                : {
                                811                 :                :     PGAlignedXLogBlock zerobuf;
 3688 magnus@hagander.net       812                 :             15 :     size_t      bytesleft = bytes;
                                813                 :                : 
 3010 tgl@sss.pgh.pa.us         814                 :             15 :     memset(zerobuf.data, 0, XLOG_BLCKSZ);
 3688 magnus@hagander.net       815         [ +  + ]:          30607 :     while (bytesleft)
                                816                 :                :     {
 3010 tgl@sss.pgh.pa.us         817                 :          30592 :         size_t      bytestowrite = Min(bytesleft, XLOG_BLCKSZ);
 1531 rhaas@postgresql.org      818                 :          30592 :         ssize_t     r = tar_write(&f->base, zerobuf.data, bytestowrite);
                                819                 :                : 
 3688 magnus@hagander.net       820         [ -  + ]:          30592 :         if (r < 0)
 3688 magnus@hagander.net       821                 :UBC           0 :             return false;
 3688 magnus@hagander.net       822                 :CBC       30592 :         bytesleft -= r;
                                823                 :                :     }
                                824                 :                : 
                                825                 :             15 :     return true;
                                826                 :                : }
                                827                 :                : 
                                828                 :                : static char *
 1531 rhaas@postgresql.org      829                 :             49 : tar_get_file_name(WalWriteMethod *wwmethod, const char *pathname,
                                830                 :                :                   const char *temp_suffix)
                                831                 :                : {
  274 michael@paquier.xyz       832                 :             49 :     char       *filename = pg_malloc0_array(char, MAXPGPATH);
                                833                 :                : 
 1957                           834         [ +  + ]:             49 :     snprintf(filename, MAXPGPATH, "%s%s",
                                835                 :                :              pathname, temp_suffix ? temp_suffix : "");
                                836                 :                : 
                                837                 :             49 :     return filename;
                                838                 :                : }
                                839                 :                : 
                                840                 :                : static Walfile *
 1531 rhaas@postgresql.org      841                 :             19 : tar_open_for_write(WalWriteMethod *wwmethod, const char *pathname,
                                842                 :                :                    const char *temp_suffix, size_t pad_to_size)
                                843                 :                : {
                                844                 :             19 :     TarMethodData *tar_data = (TarMethodData *) wwmethod;
                                845                 :                :     char       *tmppath;
                                846                 :                : 
                                847                 :             19 :     clear_error(wwmethod);
                                848                 :                : 
 3688 magnus@hagander.net       849         [ +  + ]:             19 :     if (tar_data->fd < 0)
                                850                 :                :     {
                                851                 :                :         /*
                                852                 :                :          * We open the tar file only when we first try to write to it.
                                853                 :                :          */
                                854                 :             15 :         tar_data->fd = open(tar_data->tarfilename,
                                855                 :                :                             O_WRONLY | O_CREAT | PG_BINARY,
                                856                 :                :                             pg_file_create_mode);
                                857         [ -  + ]:             15 :         if (tar_data->fd < 0)
                                858                 :                :         {
 1531 rhaas@postgresql.org      859                 :UBC           0 :             wwmethod->lasterrno = errno;
 3688 magnus@hagander.net       860                 :              0 :             return NULL;
                                861                 :                :         }
                                862                 :                : 
                                863                 :                : #ifdef HAVE_LIBZ
 1531 rhaas@postgresql.org      864         [ +  + ]:CBC          15 :         if (wwmethod->compression_algorithm == PG_COMPRESSION_GZIP)
                                865                 :                :         {
  274 michael@paquier.xyz       866                 :              4 :             tar_data->zp = pg_malloc_object(z_stream);
 3688 magnus@hagander.net       867                 :              4 :             tar_data->zp->zalloc = Z_NULL;
                                868                 :              4 :             tar_data->zp->zfree = Z_NULL;
                                869                 :              4 :             tar_data->zp->opaque = Z_NULL;
                                870                 :              4 :             tar_data->zp->next_out = tar_data->zlibOut;
                                871                 :              4 :             tar_data->zp->avail_out = ZLIB_OUT_SIZE;
                                872                 :                : 
                                873                 :                :             /*
                                874                 :                :              * Initialize deflation library. Adding the magic value 16 to the
                                875                 :                :              * default 15 for the windowBits parameter makes the output be
                                876                 :                :              * gzip instead of zlib.
                                877                 :                :              */
 1531 rhaas@postgresql.org      878         [ -  + ]:              4 :             if (deflateInit2(tar_data->zp, wwmethod->compression_level,
                                879                 :                :                              Z_DEFLATED, 15 + 16, 8, Z_DEFAULT_STRATEGY) != Z_OK)
                                880                 :                :             {
 3688 magnus@hagander.net       881                 :UBC           0 :                 pg_free(tar_data->zp);
                                882                 :              0 :                 tar_data->zp = NULL;
 1531 rhaas@postgresql.org      883                 :              0 :                 wwmethod->lasterrstring =
 1300 peter@eisentraut.org      884                 :              0 :                     _("could not initialize compression library");
 3688 magnus@hagander.net       885                 :              0 :                 return NULL;
                                886                 :                :             }
                                887                 :                :         }
                                888                 :                : #endif
                                889                 :                : 
                                890                 :                :         /* There's no tar header itself, the file starts with regular files */
                                891                 :                :     }
                                892                 :                : 
 3688 magnus@hagander.net       893         [ -  + ]:CBC          19 :     if (tar_data->currentfile != NULL)
                                894                 :                :     {
 1531 rhaas@postgresql.org      895                 :UBC           0 :         wwmethod->lasterrstring =
 1300 peter@eisentraut.org      896                 :              0 :             _("implementation error: tar files can't have more than one open file");
 3688 magnus@hagander.net       897                 :              0 :         return NULL;
                                898                 :                :     }
                                899                 :                : 
  274 michael@paquier.xyz       900                 :CBC          19 :     tar_data->currentfile = pg_malloc0_object(TarMethodFile);
 1531 rhaas@postgresql.org      901                 :             19 :     tar_data->currentfile->base.wwmethod = wwmethod;
                                902                 :                : 
                                903                 :             19 :     tmppath = tar_get_file_name(wwmethod, pathname, temp_suffix);
                                904                 :                : 
                                905                 :                :     /* Create a header with size set to 0 - we will fill out the size on close */
 3688 magnus@hagander.net       906         [ -  + ]:             19 :     if (tarCreateHeader(tar_data->currentfile->header, tmppath, NULL, 0, S_IRUSR | S_IWUSR, 0, 0, time(NULL)) != TAR_OK)
                                907                 :                :     {
 3688 magnus@hagander.net       908                 :UBC           0 :         pg_free(tar_data->currentfile);
 1951 michael@paquier.xyz       909                 :              0 :         pg_free(tmppath);
 3688 magnus@hagander.net       910                 :              0 :         tar_data->currentfile = NULL;
 1300 peter@eisentraut.org      911                 :              0 :         wwmethod->lasterrstring = _("could not create tar header");
 3688 magnus@hagander.net       912                 :              0 :         return NULL;
                                913                 :                :     }
                                914                 :                : 
 1951 michael@paquier.xyz       915                 :CBC          19 :     pg_free(tmppath);
                                916                 :                : 
                                917                 :                : #ifdef HAVE_LIBZ
 1531 rhaas@postgresql.org      918         [ +  + ]:             19 :     if (wwmethod->compression_algorithm == PG_COMPRESSION_GZIP)
                                919                 :                :     {
                                920                 :                :         /* Flush existing data */
                                921         [ -  + ]:              6 :         if (!tar_write_compressed_data(tar_data, NULL, 0, true))
 3688 magnus@hagander.net       922                 :UBC           0 :             return NULL;
                                923                 :                : 
                                924                 :                :         /* Turn off compression for header */
 1536 michael@paquier.xyz       925         [ -  + ]:CBC           6 :         if (deflateParams(tar_data->zp, 0, Z_DEFAULT_STRATEGY) != Z_OK)
                                926                 :                :         {
 1531 rhaas@postgresql.org      927                 :UBC           0 :             wwmethod->lasterrstring =
 1300 peter@eisentraut.org      928                 :              0 :                 _("could not change compression parameters");
 3688 magnus@hagander.net       929                 :              0 :             return NULL;
                                930                 :                :         }
                                931                 :                :     }
                                932                 :                : #endif
                                933                 :                : 
 3688 magnus@hagander.net       934                 :CBC          19 :     tar_data->currentfile->ofs_start = lseek(tar_data->fd, 0, SEEK_CUR);
                                935         [ -  + ]:             19 :     if (tar_data->currentfile->ofs_start == -1)
                                936                 :                :     {
 1531 rhaas@postgresql.org      937                 :UBC           0 :         wwmethod->lasterrno = errno;
 3688 magnus@hagander.net       938                 :              0 :         pg_free(tar_data->currentfile);
                                939                 :              0 :         tar_data->currentfile = NULL;
                                940                 :              0 :         return NULL;
                                941                 :                :     }
 1531 rhaas@postgresql.org      942                 :CBC          19 :     tar_data->currentfile->base.currpos = 0;
                                943                 :                : 
                                944         [ +  + ]:             19 :     if (wwmethod->compression_algorithm == PG_COMPRESSION_NONE)
                                945                 :                :     {
 3037 michael@paquier.xyz       946                 :             13 :         errno = 0;
 2409 rhaas@postgresql.org      947         [ -  + ]:             13 :         if (write(tar_data->fd, tar_data->currentfile->header,
                                948                 :                :                   TAR_BLOCK_SIZE) != TAR_BLOCK_SIZE)
                                949                 :                :         {
                                950                 :                :             /* If write didn't set errno, assume problem is no disk space */
 1531 rhaas@postgresql.org      951         [ #  # ]:UBC           0 :             wwmethod->lasterrno = errno ? errno : ENOSPC;
 3688 magnus@hagander.net       952                 :              0 :             pg_free(tar_data->currentfile);
                                953                 :              0 :             tar_data->currentfile = NULL;
                                954                 :              0 :             return NULL;
                                955                 :                :         }
                                956                 :                :     }
                                957                 :                : #ifdef HAVE_LIBZ
 1531 rhaas@postgresql.org      958         [ +  - ]:CBC           6 :     else if (wwmethod->compression_algorithm == PG_COMPRESSION_GZIP)
                                959                 :                :     {
                                960                 :                :         /* Write header through the zlib APIs but with no compression */
                                961         [ -  + ]:              6 :         if (!tar_write_compressed_data(tar_data, tar_data->currentfile->header,
                                962                 :                :                                        TAR_BLOCK_SIZE, true))
 3688 magnus@hagander.net       963                 :UBC           0 :             return NULL;
                                964                 :                : 
                                965                 :                :         /* Re-enable compression for the rest of the file */
 1531 rhaas@postgresql.org      966         [ -  + ]:CBC           6 :         if (deflateParams(tar_data->zp, wwmethod->compression_level,
                                967                 :                :                           Z_DEFAULT_STRATEGY) != Z_OK)
                                968                 :                :         {
 1300 peter@eisentraut.org      969                 :UBC           0 :             wwmethod->lasterrstring = _("could not change compression parameters");
 3688 magnus@hagander.net       970                 :              0 :             return NULL;
                                971                 :                :         }
                                972                 :                :     }
                                973                 :                : #endif
                                974                 :                :     else
                                975                 :                :     {
                                976                 :                :         /* not reachable */
 1786 michael@paquier.xyz       977                 :              0 :         Assert(false);
                                978                 :                :     }
                                979                 :                : 
 1531 rhaas@postgresql.org      980                 :CBC          19 :     tar_data->currentfile->base.pathname = pg_strdup(pathname);
                                981                 :                : 
                                982                 :                :     /*
                                983                 :                :      * Uncompressed files are padded on creation, but for compression we can't
                                984                 :                :      * do that
                                985                 :                :      */
 3688 magnus@hagander.net       986         [ +  + ]:             19 :     if (pad_to_size)
                                987                 :                :     {
                                988                 :             15 :         tar_data->currentfile->pad_to_size = pad_to_size;
 1531 rhaas@postgresql.org      989         [ +  + ]:             15 :         if (wwmethod->compression_algorithm == PG_COMPRESSION_NONE)
                                990                 :                :         {
                                991                 :                :             /* Uncompressed, so pad now */
 1837 tgl@sss.pgh.pa.us         992         [ -  + ]:             11 :             if (!tar_write_padding_data(tar_data->currentfile, pad_to_size))
 1837 tgl@sss.pgh.pa.us         993                 :UBC           0 :                 return NULL;
                                994                 :                :             /* Seek back to start */
 2409 rhaas@postgresql.org      995                 :CBC          22 :             if (lseek(tar_data->fd,
                                996                 :             11 :                       tar_data->currentfile->ofs_start + TAR_BLOCK_SIZE,
                                997         [ -  + ]:             11 :                       SEEK_SET) != tar_data->currentfile->ofs_start + TAR_BLOCK_SIZE)
                                998                 :                :             {
 1531 rhaas@postgresql.org      999                 :UBC           0 :                 wwmethod->lasterrno = errno;
 3688 magnus@hagander.net      1000                 :              0 :                 return NULL;
                               1001                 :                :             }
                               1002                 :                : 
 1531 rhaas@postgresql.org     1003                 :CBC          11 :             tar_data->currentfile->base.currpos = 0;
                               1004                 :                :         }
                               1005                 :                :     }
                               1006                 :                : 
                               1007                 :             19 :     return &tar_data->currentfile->base;
                               1008                 :                : }
                               1009                 :                : 
                               1010                 :                : static ssize_t
 1531 rhaas@postgresql.org     1011                 :UBC           0 : tar_get_file_size(WalWriteMethod *wwmethod, const char *pathname)
                               1012                 :                : {
                               1013                 :              0 :     clear_error(wwmethod);
                               1014                 :                : 
                               1015                 :                :     /* Currently not used, so not supported */
                               1016                 :              0 :     wwmethod->lasterrno = ENOSYS;
 3688 magnus@hagander.net      1017                 :              0 :     return -1;
                               1018                 :                : }
                               1019                 :                : 
                               1020                 :                : static int
 1531 rhaas@postgresql.org     1021                 :CBC          19 : tar_sync(Walfile *f)
                               1022                 :                : {
                               1023                 :             19 :     TarMethodData *tar_data = (TarMethodData *) f->wwmethod;
                               1024                 :                :     int         r;
                               1025                 :                : 
 3688 magnus@hagander.net      1026         [ -  + ]:             19 :     Assert(f != NULL);
 1531 rhaas@postgresql.org     1027                 :             19 :     clear_error(f->wwmethod);
                               1028                 :                : 
                               1029         [ +  - ]:             19 :     if (!f->wwmethod->sync)
 3686 magnus@hagander.net      1030                 :             19 :         return 0;
                               1031                 :                : 
                               1032                 :                :     /*
                               1033                 :                :      * Always sync the whole tarfile, because that's all we can do. This makes
                               1034                 :                :      * no sense on compressed files, so just ignore those.
                               1035                 :                :      */
 1531 rhaas@postgresql.org     1036         [ #  # ]:UBC           0 :     if (f->wwmethod->compression_algorithm != PG_COMPRESSION_NONE)
 3688 magnus@hagander.net      1037                 :              0 :         return 0;
                               1038                 :                : 
 1837 tgl@sss.pgh.pa.us        1039                 :              0 :     r = fsync(tar_data->fd);
                               1040         [ #  # ]:              0 :     if (r < 0)
 1531 rhaas@postgresql.org     1041                 :              0 :         f->wwmethod->lasterrno = errno;
 1837 tgl@sss.pgh.pa.us        1042                 :              0 :     return r;
                               1043                 :                : }
                               1044                 :                : 
                               1045                 :                : static int
 1531 rhaas@postgresql.org     1046                 :CBC          19 : tar_close(Walfile *f, WalCloseMethod method)
                               1047                 :                : {
                               1048                 :                :     ssize_t     filesize;
                               1049                 :                :     int         padding;
                               1050                 :             19 :     TarMethodData *tar_data = (TarMethodData *) f->wwmethod;
 3688 magnus@hagander.net      1051                 :             19 :     TarMethodFile *tf = (TarMethodFile *) f;
                               1052                 :                : 
                               1053         [ -  + ]:             19 :     Assert(f != NULL);
 1531 rhaas@postgresql.org     1054                 :             19 :     clear_error(f->wwmethod);
                               1055                 :                : 
 3688 magnus@hagander.net      1056         [ -  + ]:             19 :     if (method == CLOSE_UNLINK)
                               1057                 :                :     {
 1531 rhaas@postgresql.org     1058         [ #  # ]:UBC           0 :         if (f->wwmethod->compression_algorithm != PG_COMPRESSION_NONE)
                               1059                 :                :         {
 1300 peter@eisentraut.org     1060                 :              0 :             f->wwmethod->lasterrstring = _("unlink not supported with compression");
 3688 magnus@hagander.net      1061                 :              0 :             return -1;
                               1062                 :                :         }
                               1063                 :                : 
                               1064                 :                :         /*
                               1065                 :                :          * Unlink the file that we just wrote to the tar. We do this by
                               1066                 :                :          * truncating it to the start of the header. This is safe as we only
                               1067                 :                :          * allow writing of the very last file.
                               1068                 :                :          */
                               1069         [ #  # ]:              0 :         if (ftruncate(tar_data->fd, tf->ofs_start) != 0)
                               1070                 :                :         {
 1531 rhaas@postgresql.org     1071                 :              0 :             f->wwmethod->lasterrno = errno;
 3688 magnus@hagander.net      1072                 :              0 :             return -1;
                               1073                 :                :         }
                               1074                 :                : 
 1531 rhaas@postgresql.org     1075                 :              0 :         pg_free(tf->base.pathname);
 3688 magnus@hagander.net      1076                 :              0 :         pg_free(tf);
                               1077                 :              0 :         tar_data->currentfile = NULL;
                               1078                 :                : 
                               1079                 :              0 :         return 0;
                               1080                 :                :     }
                               1081                 :                : 
                               1082                 :                :     /*
                               1083                 :                :      * Pad the file itself with zeroes if necessary. Note that this is
                               1084                 :                :      * different from the tar format padding -- this is the padding we asked
                               1085                 :                :      * for when the file was opened.
                               1086                 :                :      */
 3688 magnus@hagander.net      1087         [ +  + ]:CBC          19 :     if (tf->pad_to_size)
                               1088                 :                :     {
 1531 rhaas@postgresql.org     1089         [ +  + ]:             15 :         if (f->wwmethod->compression_algorithm == PG_COMPRESSION_GZIP)
                               1090                 :                :         {
                               1091                 :                :             /*
                               1092                 :                :              * A compressed tarfile is padded on close since we cannot know
                               1093                 :                :              * the size of the compressed output until the end.
                               1094                 :                :              */
                               1095                 :              4 :             size_t      sizeleft = tf->pad_to_size - tf->base.currpos;
                               1096                 :                : 
 3688 magnus@hagander.net      1097         [ +  - ]:              4 :             if (sizeleft)
                               1098                 :                :             {
                               1099         [ -  + ]:              4 :                 if (!tar_write_padding_data(tf, sizeleft))
 3688 magnus@hagander.net      1100                 :UBC           0 :                     return -1;
                               1101                 :                :             }
                               1102                 :                :         }
                               1103                 :                :         else
                               1104                 :                :         {
                               1105                 :                :             /*
                               1106                 :                :              * An uncompressed tarfile was padded on creation, so just adjust
                               1107                 :                :              * the current position as if we seeked to the end.
                               1108                 :                :              */
 1531 rhaas@postgresql.org     1109                 :CBC          11 :             tf->base.currpos = tf->pad_to_size;
                               1110                 :                :         }
                               1111                 :                :     }
                               1112                 :                : 
                               1113                 :                :     /*
                               1114                 :                :      * Get the size of the file, and pad out to a multiple of the tar block
                               1115                 :                :      * size.
                               1116                 :                :      */
                               1117                 :             19 :     filesize = f->currpos;
 2409                          1118                 :             19 :     padding = tarPaddingBytesRequired(filesize);
 3688 magnus@hagander.net      1119         [ +  + ]:             19 :     if (padding)
                               1120                 :                :     {
 1596 peter@eisentraut.org     1121                 :              2 :         char        zerobuf[TAR_BLOCK_SIZE] = {0};
                               1122                 :                : 
 3688 magnus@hagander.net      1123         [ -  + ]:              2 :         if (tar_write(f, zerobuf, padding) != padding)
 3688 magnus@hagander.net      1124                 :UBC           0 :             return -1;
                               1125                 :                :     }
                               1126                 :                : 
                               1127                 :                : 
                               1128                 :                : #ifdef HAVE_LIBZ
 1531 rhaas@postgresql.org     1129         [ +  + ]:CBC          19 :     if (f->wwmethod->compression_algorithm == PG_COMPRESSION_GZIP)
                               1130                 :                :     {
                               1131                 :                :         /* Flush the current buffer */
                               1132         [ -  + ]:              6 :         if (!tar_write_compressed_data(tar_data, NULL, 0, true))
 3688 magnus@hagander.net      1133                 :UBC           0 :             return -1;
                               1134                 :                :     }
                               1135                 :                : #endif
                               1136                 :                : 
                               1137                 :                :     /*
                               1138                 :                :      * Now go back and update the header with the correct filesize and
                               1139                 :                :      * possibly also renaming the file. We overwrite the entire current header
                               1140                 :                :      * when done, including the checksum.
                               1141                 :                :      */
 1215 rhaas@postgresql.org     1142                 :CBC          19 :     print_tar_number(&(tf->header[TAR_OFFSET_SIZE]), 12, filesize);
                               1143                 :                : 
 3688 magnus@hagander.net      1144         [ +  - ]:             19 :     if (method == CLOSE_NORMAL)
                               1145                 :                : 
                               1146                 :                :         /*
                               1147                 :                :          * We overwrite it with what it was before if we have no tempname,
                               1148                 :                :          * since we're going to write the buffer anyway.
                               1149                 :                :          */
 1215 rhaas@postgresql.org     1150                 :             19 :         strlcpy(&(tf->header[TAR_OFFSET_NAME]), tf->base.pathname, 100);
                               1151                 :                : 
                               1152                 :             19 :     print_tar_number(&(tf->header[TAR_OFFSET_CHECKSUM]), 8,
                               1153                 :             19 :                      tarChecksum(((TarMethodFile *) f)->header));
 3688 magnus@hagander.net      1154         [ -  + ]:             19 :     if (lseek(tar_data->fd, tf->ofs_start, SEEK_SET) != ((TarMethodFile *) f)->ofs_start)
                               1155                 :                :     {
 1531 rhaas@postgresql.org     1156                 :UBC           0 :         f->wwmethod->lasterrno = errno;
 3688 magnus@hagander.net      1157                 :              0 :         return -1;
                               1158                 :                :     }
 1531 rhaas@postgresql.org     1159         [ +  + ]:CBC          19 :     if (f->wwmethod->compression_algorithm == PG_COMPRESSION_NONE)
                               1160                 :                :     {
 3037 michael@paquier.xyz      1161                 :             13 :         errno = 0;
 2409 rhaas@postgresql.org     1162         [ -  + ]:             13 :         if (write(tar_data->fd, tf->header, TAR_BLOCK_SIZE) != TAR_BLOCK_SIZE)
                               1163                 :                :         {
                               1164                 :                :             /* If write didn't set errno, assume problem is no disk space */
 1531 rhaas@postgresql.org     1165         [ #  # ]:UBC           0 :             f->wwmethod->lasterrno = errno ? errno : ENOSPC;
 3688 magnus@hagander.net      1166                 :              0 :             return -1;
                               1167                 :                :         }
                               1168                 :                :     }
                               1169                 :                : #ifdef HAVE_LIBZ
 1531 rhaas@postgresql.org     1170         [ +  - ]:CBC           6 :     else if (f->wwmethod->compression_algorithm == PG_COMPRESSION_GZIP)
                               1171                 :                :     {
                               1172                 :                :         /* Turn off compression */
 1536 michael@paquier.xyz      1173         [ -  + ]:              6 :         if (deflateParams(tar_data->zp, 0, Z_DEFAULT_STRATEGY) != Z_OK)
                               1174                 :                :         {
 1300 peter@eisentraut.org     1175                 :UBC           0 :             f->wwmethod->lasterrstring = _("could not change compression parameters");
 3688 magnus@hagander.net      1176                 :              0 :             return -1;
                               1177                 :                :         }
                               1178                 :                : 
                               1179                 :                :         /* Overwrite the header, assuming the size will be the same */
 1531 rhaas@postgresql.org     1180         [ -  + ]:CBC           6 :         if (!tar_write_compressed_data(tar_data, tar_data->currentfile->header,
                               1181                 :                :                                        TAR_BLOCK_SIZE, true))
 3688 magnus@hagander.net      1182                 :UBC           0 :             return -1;
                               1183                 :                : 
                               1184                 :                :         /* Turn compression back on */
 1531 rhaas@postgresql.org     1185         [ -  + ]:CBC           6 :         if (deflateParams(tar_data->zp, f->wwmethod->compression_level,
                               1186                 :                :                           Z_DEFAULT_STRATEGY) != Z_OK)
                               1187                 :                :         {
 1300 peter@eisentraut.org     1188                 :UBC           0 :             f->wwmethod->lasterrstring = _("could not change compression parameters");
 3688 magnus@hagander.net      1189                 :              0 :             return -1;
                               1190                 :                :         }
                               1191                 :                :     }
                               1192                 :                : #endif
                               1193                 :                :     else
                               1194                 :                :     {
                               1195                 :                :         /* not reachable */
 1786 michael@paquier.xyz      1196                 :              0 :         Assert(false);
                               1197                 :                :     }
                               1198                 :                : 
                               1199                 :                :     /* Move file pointer back down to end, so we can write the next file */
 3688 magnus@hagander.net      1200         [ -  + ]:CBC          19 :     if (lseek(tar_data->fd, 0, SEEK_END) < 0)
                               1201                 :                :     {
 1531 rhaas@postgresql.org     1202                 :UBC           0 :         f->wwmethod->lasterrno = errno;
 3688 magnus@hagander.net      1203                 :              0 :         return -1;
                               1204                 :                :     }
                               1205                 :                : 
                               1206                 :                :     /* Always fsync on close, so the padding gets fsynced */
 3077 michael@paquier.xyz      1207         [ -  + ]:CBC          19 :     if (tar_sync(f) < 0)
                               1208                 :                :     {
                               1209                 :                :         /* XXX this seems pretty bogus; why is only this case fatal? */
 1695 tgl@sss.pgh.pa.us        1210                 :UBC           0 :         pg_fatal("could not fsync file \"%s\": %s",
                               1211                 :                :                  tf->base.pathname, GetLastWalMethodError(f->wwmethod));
                               1212                 :                :     }
                               1213                 :                : 
                               1214                 :                :     /* Clean up and done */
 1531 rhaas@postgresql.org     1215                 :CBC          19 :     pg_free(tf->base.pathname);
 3688 magnus@hagander.net      1216                 :             19 :     pg_free(tf);
                               1217                 :             19 :     tar_data->currentfile = NULL;
                               1218                 :                : 
                               1219                 :             19 :     return 0;
                               1220                 :                : }
                               1221                 :                : 
                               1222                 :                : static bool
 1531 rhaas@postgresql.org     1223                 :             13 : tar_existsfile(WalWriteMethod *wwmethod, const char *pathname)
                               1224                 :                : {
                               1225                 :             13 :     clear_error(wwmethod);
                               1226                 :                :     /* We only deal with new tarfiles, so nothing externally created exists */
 3688 magnus@hagander.net      1227                 :             13 :     return false;
                               1228                 :                : }
                               1229                 :                : 
                               1230                 :                : static bool
 1531 rhaas@postgresql.org     1231                 :             15 : tar_finish(WalWriteMethod *wwmethod)
                               1232                 :                : {
                               1233                 :             15 :     TarMethodData *tar_data = (TarMethodData *) wwmethod;
 1596 peter@eisentraut.org     1234                 :             15 :     char        zerobuf[1024] = {0};
                               1235                 :                : 
 1531 rhaas@postgresql.org     1236                 :             15 :     clear_error(wwmethod);
                               1237                 :                : 
 3688 magnus@hagander.net      1238         [ -  + ]:             15 :     if (tar_data->currentfile)
                               1239                 :                :     {
 1531 rhaas@postgresql.org     1240         [ #  # ]:UBC           0 :         if (tar_close(&tar_data->currentfile->base, CLOSE_NORMAL) != 0)
 3688 magnus@hagander.net      1241                 :              0 :             return false;
                               1242                 :                :     }
                               1243                 :                : 
                               1244                 :                :     /* A tarfile always ends with two empty blocks */
 1531 rhaas@postgresql.org     1245         [ +  + ]:CBC          15 :     if (wwmethod->compression_algorithm == PG_COMPRESSION_NONE)
                               1246                 :                :     {
 3037 michael@paquier.xyz      1247                 :             11 :         errno = 0;
 3688 magnus@hagander.net      1248         [ -  + ]:             11 :         if (write(tar_data->fd, zerobuf, sizeof(zerobuf)) != sizeof(zerobuf))
                               1249                 :                :         {
                               1250                 :                :             /* If write didn't set errno, assume problem is no disk space */
 1531 rhaas@postgresql.org     1251         [ #  # ]:UBC           0 :             wwmethod->lasterrno = errno ? errno : ENOSPC;
 3688 magnus@hagander.net      1252                 :              0 :             return false;
                               1253                 :                :         }
                               1254                 :                :     }
                               1255                 :                : #ifdef HAVE_LIBZ
 1531 rhaas@postgresql.org     1256         [ +  - ]:CBC           4 :     else if (wwmethod->compression_algorithm == PG_COMPRESSION_GZIP)
                               1257                 :                :     {
                               1258         [ -  + ]:              4 :         if (!tar_write_compressed_data(tar_data, zerobuf, sizeof(zerobuf),
                               1259                 :                :                                        false))
 3688 magnus@hagander.net      1260                 :UBC           0 :             return false;
                               1261                 :                : 
                               1262                 :                :         /* Also flush all data to make sure the gzip stream is finished */
 3688 magnus@hagander.net      1263                 :CBC           4 :         tar_data->zp->next_in = NULL;
                               1264                 :              4 :         tar_data->zp->avail_in = 0;
                               1265                 :                :         while (true)
 3688 magnus@hagander.net      1266                 :UBC           0 :         {
                               1267                 :                :             int         r;
                               1268                 :                : 
 3688 magnus@hagander.net      1269                 :CBC           4 :             r = deflate(tar_data->zp, Z_FINISH);
                               1270                 :                : 
                               1271         [ -  + ]:              4 :             if (r == Z_STREAM_ERROR)
                               1272                 :                :             {
 1300 peter@eisentraut.org     1273                 :UBC           0 :                 wwmethod->lasterrstring = _("could not compress data");
 3688 magnus@hagander.net      1274                 :              0 :                 return false;
                               1275                 :                :             }
 3688 magnus@hagander.net      1276         [ +  - ]:CBC           4 :             if (tar_data->zp->avail_out < ZLIB_OUT_SIZE)
                               1277                 :                :             {
                               1278                 :              4 :                 size_t      len = ZLIB_OUT_SIZE - tar_data->zp->avail_out;
                               1279                 :                : 
 3037 michael@paquier.xyz      1280                 :              4 :                 errno = 0;
 3688 magnus@hagander.net      1281         [ -  + ]:              4 :                 if (write(tar_data->fd, tar_data->zlibOut, len) != len)
                               1282                 :                :                 {
                               1283                 :                :                     /*
                               1284                 :                :                      * If write didn't set errno, assume problem is no disk
                               1285                 :                :                      * space.
                               1286                 :                :                      */
 1531 rhaas@postgresql.org     1287         [ #  # ]:UBC           0 :                     wwmethod->lasterrno = errno ? errno : ENOSPC;
 3688 magnus@hagander.net      1288                 :              0 :                     return false;
                               1289                 :                :                 }
                               1290                 :                :             }
 3688 magnus@hagander.net      1291         [ +  - ]:CBC           4 :             if (r == Z_STREAM_END)
                               1292                 :              4 :                 break;
                               1293                 :                :         }
                               1294                 :                : 
                               1295         [ -  + ]:              4 :         if (deflateEnd(tar_data->zp) != Z_OK)
                               1296                 :                :         {
 1300 peter@eisentraut.org     1297                 :UBC           0 :             wwmethod->lasterrstring = _("could not close compression stream");
 3688 magnus@hagander.net      1298                 :              0 :             return false;
                               1299                 :                :         }
                               1300                 :                :     }
                               1301                 :                : #endif
                               1302                 :                :     else
                               1303                 :                :     {
                               1304                 :                :         /* not reachable */
 1786 michael@paquier.xyz      1305                 :              0 :         Assert(false);
                               1306                 :                :     }
                               1307                 :                : 
                               1308                 :                :     /* sync the empty blocks as well, since they're after the last file */
 1531 rhaas@postgresql.org     1309         [ -  + ]:CBC          15 :     if (wwmethod->sync)
                               1310                 :                :     {
 3077 michael@paquier.xyz      1311         [ #  # ]:UBC           0 :         if (fsync(tar_data->fd) != 0)
                               1312                 :                :         {
 1531 rhaas@postgresql.org     1313                 :              0 :             wwmethod->lasterrno = errno;
 3077 michael@paquier.xyz      1314                 :              0 :             return false;
                               1315                 :                :         }
                               1316                 :                :     }
                               1317                 :                : 
 3688 magnus@hagander.net      1318         [ -  + ]:CBC          15 :     if (close(tar_data->fd) != 0)
                               1319                 :                :     {
 1531 rhaas@postgresql.org     1320                 :UBC           0 :         wwmethod->lasterrno = errno;
 3688 magnus@hagander.net      1321                 :              0 :         return false;
                               1322                 :                :     }
                               1323                 :                : 
 3688 magnus@hagander.net      1324                 :CBC          15 :     tar_data->fd = -1;
                               1325                 :                : 
 1531 rhaas@postgresql.org     1326         [ -  + ]:             15 :     if (wwmethod->sync)
                               1327                 :                :     {
 1837 tgl@sss.pgh.pa.us        1328   [ #  #  #  # ]:UBC           0 :         if (fsync_fname(tar_data->tarfilename, false) != 0 ||
                               1329                 :              0 :             fsync_parent_path(tar_data->tarfilename) != 0)
                               1330                 :                :         {
 1531 rhaas@postgresql.org     1331                 :              0 :             wwmethod->lasterrno = errno;
 3688 magnus@hagander.net      1332                 :              0 :             return false;
                               1333                 :                :         }
                               1334                 :                :     }
                               1335                 :                : 
 3688 magnus@hagander.net      1336                 :CBC          15 :     return true;
                               1337                 :                : }
                               1338                 :                : 
                               1339                 :                : static void
 1531 rhaas@postgresql.org     1340                 :             15 : tar_free(WalWriteMethod *wwmethod)
                               1341                 :                : {
                               1342                 :             15 :     TarMethodData *tar_data = (TarMethodData *) wwmethod;
                               1343                 :                : 
                               1344                 :             15 :     pg_free(tar_data->tarfilename);
                               1345                 :                : #ifdef HAVE_LIBZ
                               1346         [ +  + ]:             15 :     if (wwmethod->compression_algorithm == PG_COMPRESSION_GZIP)
                               1347                 :              4 :         pg_free(tar_data->zlibOut);
                               1348                 :                : #endif
                               1349                 :             15 :     pg_free(wwmethod);
                               1350                 :             15 : }
                               1351                 :                : 
                               1352                 :                : /*
                               1353                 :                :  * The argument compression_algorithm is currently ignored. It is in place for
                               1354                 :                :  * symmetry with CreateWalDirectoryMethod which uses it for distinguishing
                               1355                 :                :  * between the different compression methods. CreateWalTarMethod and its family
                               1356                 :                :  * of functions handle only zlib compression.
                               1357                 :                :  */
                               1358                 :                : WalWriteMethod *
 1850 michael@paquier.xyz      1359                 :             15 : CreateWalTarMethod(const char *tarbase,
                               1360                 :                :                    pg_compress_algorithm compression_algorithm,
                               1361                 :                :                    int compression_level, bool sync)
                               1362                 :                : {
                               1363                 :                :     TarMethodData *wwmethod;
 1691                          1364                 :             15 :     const char *suffix = (compression_algorithm == PG_COMPRESSION_GZIP) ?
 1289 tgl@sss.pgh.pa.us        1365         [ +  + ]:             15 :         ".tar.gz" : ".tar";
                               1366                 :                : 
  274 michael@paquier.xyz      1367                 :             15 :     wwmethod = pg_malloc0_object(TarMethodData);
 1531 rhaas@postgresql.org     1368                 :             15 :     *((const WalWriteMethodOps **) &wwmethod->base.ops) =
                               1369                 :                :         &WalTarMethodOps;
                               1370                 :             15 :     wwmethod->base.compression_algorithm = compression_algorithm;
                               1371                 :             15 :     wwmethod->base.compression_level = compression_level;
                               1372                 :             15 :     wwmethod->base.sync = sync;
                               1373                 :             15 :     clear_error(&wwmethod->base);
                               1374                 :                : 
                               1375                 :             15 :     wwmethod->tarfilename = pg_malloc0(strlen(tarbase) + strlen(suffix) + 1);
                               1376                 :             15 :     sprintf(wwmethod->tarfilename, "%s%s", tarbase, suffix);
                               1377                 :             15 :     wwmethod->fd = -1;
                               1378                 :                : #ifdef HAVE_LIBZ
 1691 michael@paquier.xyz      1379         [ +  + ]:             15 :     if (compression_algorithm == PG_COMPRESSION_GZIP)
 1531 rhaas@postgresql.org     1380                 :              4 :         wwmethod->zlibOut = (char *) pg_malloc(ZLIB_OUT_SIZE + 1);
                               1381                 :                : #endif
                               1382                 :                : 
                               1383                 :             15 :     return &wwmethod->base;
                               1384                 :                : }
                               1385                 :                : 
                               1386                 :                : const char *
 1531 rhaas@postgresql.org     1387                 :UBC           0 : GetLastWalMethodError(WalWriteMethod *wwmethod)
                               1388                 :                : {
                               1389         [ #  # ]:              0 :     if (wwmethod->lasterrstring)
                               1390                 :              0 :         return wwmethod->lasterrstring;
                               1391                 :              0 :     return strerror(wwmethod->lasterrno);
                               1392                 :                : }
        

Generated by: LCOV version 2.0-1