LCOV - code coverage report
Current view: top level - src/bin/pg_combinebackup - write_manifest.c (source / functions) Coverage Total Hit
Test: PostgreSQL 20devel Lines: 73.2 % 123 90
Test Date: 2026-07-03 19:57:34 Functions: 100.0 % 6 6
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
Branches: 63.0 % 54 34

             Branch data     Line data    Source code
       1                 :             : /*-------------------------------------------------------------------------
       2                 :             :  *
       3                 :             :  * Write a new backup manifest.
       4                 :             :  *
       5                 :             :  * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group
       6                 :             :  * Portions Copyright (c) 1994, Regents of the University of California
       7                 :             :  *
       8                 :             :  * src/bin/pg_combinebackup/write_manifest.c
       9                 :             :  *
      10                 :             :  *-------------------------------------------------------------------------
      11                 :             :  */
      12                 :             : 
      13                 :             : #include "postgres_fe.h"
      14                 :             : 
      15                 :             : #include <fcntl.h>
      16                 :             : #include <time.h>
      17                 :             : #include <unistd.h>
      18                 :             : 
      19                 :             : #include "common/checksum_helper.h"
      20                 :             : #include "common/file_perm.h"
      21                 :             : #include "common/logging.h"
      22                 :             : #include "lib/stringinfo.h"
      23                 :             : #include "load_manifest.h"
      24                 :             : #include "mb/pg_wchar.h"
      25                 :             : #include "write_manifest.h"
      26                 :             : 
      27                 :             : struct manifest_writer
      28                 :             : {
      29                 :             :     char        pathname[MAXPGPATH];
      30                 :             :     int         fd;
      31                 :             :     StringInfoData buf;
      32                 :             :     bool        first_file;
      33                 :             :     bool        still_checksumming;
      34                 :             :     pg_checksum_context manifest_ctx;
      35                 :             : };
      36                 :             : 
      37                 :             : static void escape_json(StringInfo buf, const char *str);
      38                 :             : static void flush_manifest(manifest_writer *mwriter);
      39                 :             : static size_t hex_encode(const uint8 *src, size_t len, char *dst);
      40                 :             : 
      41                 :             : /*
      42                 :             :  * Create a new backup manifest writer.
      43                 :             :  *
      44                 :             :  * The backup manifest will be written into a file named backup_manifest
      45                 :             :  * in the specified directory.
      46                 :             :  */
      47                 :             : manifest_writer *
      48                 :          13 : create_manifest_writer(char *directory, uint64 system_identifier)
      49                 :             : {
      50                 :          13 :     manifest_writer *mwriter = pg_malloc_object(manifest_writer);
      51                 :             : 
      52                 :          13 :     snprintf(mwriter->pathname, MAXPGPATH, "%s/backup_manifest", directory);
      53                 :          13 :     mwriter->fd = -1;
      54                 :          13 :     initStringInfo(&mwriter->buf);
      55                 :          13 :     mwriter->first_file = true;
      56                 :          13 :     mwriter->still_checksumming = true;
      57                 :          13 :     pg_checksum_init(&mwriter->manifest_ctx, CHECKSUM_TYPE_SHA256);
      58                 :             : 
      59                 :          13 :     appendStringInfo(&mwriter->buf,
      60                 :             :                      "{ \"PostgreSQL-Backup-Manifest-Version\": 2,\n"
      61                 :             :                      "\"System-Identifier\": " UINT64_FORMAT ",\n"
      62                 :             :                      "\"Files\": [",
      63                 :             :                      system_identifier);
      64                 :             : 
      65                 :          13 :     return mwriter;
      66                 :             : }
      67                 :             : 
      68                 :             : /*
      69                 :             :  * Add an entry for a file to a backup manifest.
      70                 :             :  *
      71                 :             :  * This is very similar to the backend's AddFileToBackupManifest, but
      72                 :             :  * various adjustments are required due to frontend/backend differences
      73                 :             :  * and other details.
      74                 :             :  */
      75                 :             : void
      76                 :       13764 : add_file_to_manifest(manifest_writer *mwriter, const char *manifest_path,
      77                 :             :                      uint64 size, time_t mtime,
      78                 :             :                      pg_checksum_type checksum_type,
      79                 :             :                      int checksum_length,
      80                 :             :                      uint8 *checksum_payload)
      81                 :             : {
      82                 :       13764 :     int         pathlen = strlen(manifest_path);
      83                 :             : 
      84         [ +  + ]:       13764 :     if (mwriter->first_file)
      85                 :             :     {
      86                 :          13 :         appendStringInfoChar(&mwriter->buf, '\n');
      87                 :          13 :         mwriter->first_file = false;
      88                 :             :     }
      89                 :             :     else
      90                 :       13751 :         appendStringInfoString(&mwriter->buf, ",\n");
      91                 :             : 
      92         [ +  - ]:       13764 :     if (pg_encoding_verifymbstr(PG_UTF8, manifest_path, pathlen) == pathlen)
      93                 :             :     {
      94                 :       13764 :         appendStringInfoString(&mwriter->buf, "{ \"Path\": ");
      95                 :       13764 :         escape_json(&mwriter->buf, manifest_path);
      96                 :       13764 :         appendStringInfoString(&mwriter->buf, ", ");
      97                 :             :     }
      98                 :             :     else
      99                 :             :     {
     100                 :           0 :         appendStringInfoString(&mwriter->buf, "{ \"Encoded-Path\": \"");
     101                 :           0 :         enlargeStringInfo(&mwriter->buf, 2 * pathlen);
     102                 :           0 :         mwriter->buf.len += hex_encode((const uint8 *) manifest_path, pathlen,
     103                 :           0 :                                        &mwriter->buf.data[mwriter->buf.len]);
     104                 :           0 :         appendStringInfoString(&mwriter->buf, "\", ");
     105                 :             :     }
     106                 :             : 
     107                 :       13764 :     appendStringInfo(&mwriter->buf, "\"Size\": %" PRIu64 ", ", size);
     108                 :             : 
     109                 :       13764 :     appendStringInfoString(&mwriter->buf, "\"Last-Modified\": \"");
     110                 :       13764 :     enlargeStringInfo(&mwriter->buf, 128);
     111                 :       13764 :     mwriter->buf.len += strftime(&mwriter->buf.data[mwriter->buf.len], 128,
     112                 :             :                                  "%Y-%m-%d %H:%M:%S %Z",
     113                 :       13764 :                                  gmtime(&mtime));
     114                 :       13764 :     appendStringInfoChar(&mwriter->buf, '"');
     115                 :             : 
     116         [ +  + ]:       13764 :     if (mwriter->buf.len > 128 * 1024)
     117                 :           5 :         flush_manifest(mwriter);
     118                 :             : 
     119         [ +  + ]:       13764 :     if (checksum_length > 0)
     120                 :             :     {
     121                 :       12721 :         appendStringInfo(&mwriter->buf,
     122                 :             :                          ", \"Checksum-Algorithm\": \"%s\", \"Checksum\": \"",
     123                 :             :                          pg_checksum_type_name(checksum_type));
     124                 :             : 
     125                 :       12721 :         enlargeStringInfo(&mwriter->buf, 2 * checksum_length);
     126                 :       25442 :         mwriter->buf.len += hex_encode(checksum_payload, checksum_length,
     127                 :       12721 :                                        &mwriter->buf.data[mwriter->buf.len]);
     128                 :             : 
     129                 :       12721 :         appendStringInfoChar(&mwriter->buf, '"');
     130                 :             :     }
     131                 :             : 
     132                 :       13764 :     appendStringInfoString(&mwriter->buf, " }");
     133                 :             : 
     134         [ +  + ]:       13764 :     if (mwriter->buf.len > 128 * 1024)
     135                 :           6 :         flush_manifest(mwriter);
     136                 :       13764 : }
     137                 :             : 
     138                 :             : /*
     139                 :             :  * Finalize the backup_manifest.
     140                 :             :  */
     141                 :             : void
     142                 :          12 : finalize_manifest(manifest_writer *mwriter,
     143                 :             :                   manifest_wal_range *first_wal_range)
     144                 :             : {
     145                 :             :     uint8       checksumbuf[PG_SHA256_DIGEST_LENGTH];
     146                 :             :     int         len;
     147                 :             :     manifest_wal_range *wal_range;
     148                 :             : 
     149                 :             :     /* Terminate the list of files. */
     150                 :          12 :     appendStringInfoString(&mwriter->buf, "\n],\n");
     151                 :             : 
     152                 :             :     /* Start a list of LSN ranges. */
     153                 :          12 :     appendStringInfoString(&mwriter->buf, "\"WAL-Ranges\": [\n");
     154                 :             : 
     155         [ +  + ]:          24 :     for (wal_range = first_wal_range; wal_range != NULL;
     156                 :          12 :          wal_range = wal_range->next)
     157                 :          12 :         appendStringInfo(&mwriter->buf,
     158                 :             :                          "%s{ \"Timeline\": %u, \"Start-LSN\": \"%X/%08X\", \"End-LSN\": \"%X/%08X\" }",
     159                 :             :                          wal_range == first_wal_range ? "" : ",\n",
     160                 :             :                          wal_range->tli,
     161                 :          12 :                          LSN_FORMAT_ARGS(wal_range->start_lsn),
     162         [ +  - ]:          12 :                          LSN_FORMAT_ARGS(wal_range->end_lsn));
     163                 :             : 
     164                 :             :     /* Terminate the list of WAL ranges. */
     165                 :          12 :     appendStringInfoString(&mwriter->buf, "\n],\n");
     166                 :             : 
     167                 :             :     /* Flush accumulated data and update checksum calculation. */
     168                 :          12 :     flush_manifest(mwriter);
     169                 :             : 
     170                 :             :     /* Checksum only includes data up to this point. */
     171                 :          12 :     mwriter->still_checksumming = false;
     172                 :             : 
     173                 :             :     /* Compute and insert manifest checksum. */
     174                 :          12 :     appendStringInfoString(&mwriter->buf, "\"Manifest-Checksum\": \"");
     175                 :          12 :     enlargeStringInfo(&mwriter->buf, 2 * PG_SHA256_DIGEST_STRING_LENGTH);
     176                 :          12 :     len = pg_checksum_final(&mwriter->manifest_ctx, checksumbuf);
     177                 :             :     Assert(len == PG_SHA256_DIGEST_LENGTH);
     178                 :          12 :     mwriter->buf.len +=
     179                 :          12 :         hex_encode(checksumbuf, len, &mwriter->buf.data[mwriter->buf.len]);
     180                 :          12 :     appendStringInfoString(&mwriter->buf, "\"}\n");
     181                 :             : 
     182                 :             :     /* Flush the last manifest checksum itself. */
     183                 :          12 :     flush_manifest(mwriter);
     184                 :             : 
     185                 :             :     /* Close the file. */
     186         [ -  + ]:          12 :     if (close(mwriter->fd) != 0)
     187                 :           0 :         pg_fatal("could not close file \"%s\": %m", mwriter->pathname);
     188                 :          12 :     mwriter->fd = -1;
     189                 :          12 : }
     190                 :             : 
     191                 :             : /*
     192                 :             :  * Produce a JSON string literal, properly escaping characters in the text.
     193                 :             :  */
     194                 :             : static void
     195                 :       13764 : escape_json(StringInfo buf, const char *str)
     196                 :             : {
     197                 :             :     const char *p;
     198                 :             : 
     199         [ -  + ]:       13764 :     appendStringInfoCharMacro(buf, '"');
     200         [ +  + ]:      181847 :     for (p = str; *p; p++)
     201                 :             :     {
     202   [ -  -  -  -  :      168083 :         switch (*p)
             -  -  -  + ]
     203                 :             :         {
     204                 :           0 :             case '\b':
     205                 :           0 :                 appendStringInfoString(buf, "\\b");
     206                 :           0 :                 break;
     207                 :           0 :             case '\f':
     208                 :           0 :                 appendStringInfoString(buf, "\\f");
     209                 :           0 :                 break;
     210                 :           0 :             case '\n':
     211                 :           0 :                 appendStringInfoString(buf, "\\n");
     212                 :           0 :                 break;
     213                 :           0 :             case '\r':
     214                 :           0 :                 appendStringInfoString(buf, "\\r");
     215                 :           0 :                 break;
     216                 :           0 :             case '\t':
     217                 :           0 :                 appendStringInfoString(buf, "\\t");
     218                 :           0 :                 break;
     219                 :           0 :             case '"':
     220                 :           0 :                 appendStringInfoString(buf, "\\\"");
     221                 :           0 :                 break;
     222                 :           0 :             case '\\':
     223                 :           0 :                 appendStringInfoString(buf, "\\\\");
     224                 :           0 :                 break;
     225                 :      168083 :             default:
     226         [ -  + ]:      168083 :                 if ((unsigned char) *p < ' ')
     227                 :           0 :                     appendStringInfo(buf, "\\u%04x", (int) *p);
     228                 :             :                 else
     229         [ -  + ]:      168083 :                     appendStringInfoCharMacro(buf, *p);
     230                 :      168083 :                 break;
     231                 :             :         }
     232                 :             :     }
     233         [ -  + ]:       13764 :     appendStringInfoCharMacro(buf, '"');
     234                 :       13764 : }
     235                 :             : 
     236                 :             : /*
     237                 :             :  * Flush whatever portion of the backup manifest we have generated and
     238                 :             :  * buffered in memory out to a file on disk.
     239                 :             :  *
     240                 :             :  * The first call to this function will create the file. After that, we
     241                 :             :  * keep it open and just append more data.
     242                 :             :  */
     243                 :             : static void
     244                 :          35 : flush_manifest(manifest_writer *mwriter)
     245                 :             : {
     246         [ +  + ]:          35 :     if (mwriter->fd == -1 &&
     247         [ -  + ]:          12 :         (mwriter->fd = open(mwriter->pathname,
     248                 :             :                             O_WRONLY | O_CREAT | O_EXCL | PG_BINARY,
     249                 :             :                             pg_file_create_mode)) < 0)
     250                 :           0 :         pg_fatal("could not open file \"%s\": %m", mwriter->pathname);
     251                 :             : 
     252         [ +  - ]:          35 :     if (mwriter->buf.len > 0)
     253                 :             :     {
     254                 :             :         ssize_t     wb;
     255                 :             : 
     256                 :          35 :         wb = write(mwriter->fd, mwriter->buf.data, mwriter->buf.len);
     257         [ -  + ]:          35 :         if (wb != mwriter->buf.len)
     258                 :             :         {
     259         [ #  # ]:           0 :             if (wb < 0)
     260                 :           0 :                 pg_fatal("could not write file \"%s\": %m", mwriter->pathname);
     261                 :             :             else
     262                 :           0 :                 pg_fatal("could not write file \"%s\": wrote %zd of %d",
     263                 :             :                          mwriter->pathname, wb, mwriter->buf.len);
     264                 :             :         }
     265                 :             : 
     266   [ +  +  -  + ]:          58 :         if (mwriter->still_checksumming &&
     267                 :          23 :             pg_checksum_update(&mwriter->manifest_ctx,
     268                 :          23 :                                (uint8 *) mwriter->buf.data,
     269                 :          23 :                                mwriter->buf.len) < 0)
     270                 :           0 :             pg_fatal("could not update checksum of file \"%s\"",
     271                 :             :                      mwriter->pathname);
     272                 :          35 :         resetStringInfo(&mwriter->buf);
     273                 :             :     }
     274                 :          35 : }
     275                 :             : 
     276                 :             : /*
     277                 :             :  * Encode bytes using two hexadecimal digits for each one.
     278                 :             :  */
     279                 :             : static size_t
     280                 :       12733 : hex_encode(const uint8 *src, size_t len, char *dst)
     281                 :             : {
     282                 :       12733 :     const uint8 *end = src + len;
     283                 :             : 
     284         [ +  + ]:       88697 :     while (src < end)
     285                 :             :     {
     286                 :       75964 :         unsigned    n1 = (*src >> 4) & 0xF;
     287                 :       75964 :         unsigned    n2 = *src & 0xF;
     288                 :             : 
     289         [ +  + ]:       75964 :         *dst++ = n1 < 10 ? '0' + n1 : 'a' + n1 - 10;
     290         [ +  + ]:       75964 :         *dst++ = n2 < 10 ? '0' + n2 : 'a' + n2 - 10;
     291                 :       75964 :         ++src;
     292                 :             :     }
     293                 :             : 
     294                 :       12733 :     return len * 2;
     295                 :             : }
        

Generated by: LCOV version 2.0-1