LCOV - code coverage report
Current view: top level - src/bin/pg_rewind - local_source.c (source / functions) Coverage Total Hit
Test: PostgreSQL 19devel Lines: 88.1 % 67 59
Test Date: 2026-03-04 16:14:47 Functions: 100.0 % 7 7
Legend: Lines:     hit not hit

            Line data    Source code
       1              : /*-------------------------------------------------------------------------
       2              :  *
       3              :  * local_source.c
       4              :  *    Functions for using a local data directory as the source.
       5              :  *
       6              :  * Portions Copyright (c) 2013-2026, PostgreSQL Global Development Group
       7              :  *
       8              :  *-------------------------------------------------------------------------
       9              :  */
      10              : #include "postgres_fe.h"
      11              : 
      12              : #include <fcntl.h>
      13              : #include <unistd.h>
      14              : 
      15              : #include "common/logging.h"
      16              : #include "file_ops.h"
      17              : #include "rewind_source.h"
      18              : 
      19              : typedef struct
      20              : {
      21              :     rewind_source common;       /* common interface functions */
      22              : 
      23              :     const char *datadir;        /* path to the source data directory */
      24              : } local_source;
      25              : 
      26              : static void local_traverse_files(rewind_source *source,
      27              :                                  process_file_callback_t callback);
      28              : static char *local_fetch_file(rewind_source *source, const char *path,
      29              :                               size_t *filesize);
      30              : static void local_queue_fetch_file(rewind_source *source, const char *path,
      31              :                                    size_t len);
      32              : static void local_queue_fetch_range(rewind_source *source, const char *path,
      33              :                                     off_t off, size_t len);
      34              : static void local_finish_fetch(rewind_source *source);
      35              : static void local_destroy(rewind_source *source);
      36              : 
      37              : rewind_source *
      38           13 : init_local_source(const char *datadir)
      39              : {
      40              :     local_source *src;
      41              : 
      42           13 :     src = pg_malloc0_object(local_source);
      43              : 
      44           13 :     src->common.traverse_files = local_traverse_files;
      45           13 :     src->common.fetch_file = local_fetch_file;
      46           13 :     src->common.queue_fetch_file = local_queue_fetch_file;
      47           13 :     src->common.queue_fetch_range = local_queue_fetch_range;
      48           13 :     src->common.finish_fetch = local_finish_fetch;
      49           13 :     src->common.get_current_wal_insert_lsn = NULL;
      50           13 :     src->common.destroy = local_destroy;
      51              : 
      52           13 :     src->datadir = datadir;
      53              : 
      54           13 :     return &src->common;
      55              : }
      56              : 
      57              : static void
      58            9 : local_traverse_files(rewind_source *source, process_file_callback_t callback)
      59              : {
      60            9 :     traverse_datadir(((local_source *) source)->datadir, callback);
      61            9 : }
      62              : 
      63              : static char *
      64           29 : local_fetch_file(rewind_source *source, const char *path, size_t *filesize)
      65              : {
      66           29 :     return slurpFile(((local_source *) source)->datadir, path, filesize);
      67              : }
      68              : 
      69              : /*
      70              :  * Copy a file from source to target.
      71              :  *
      72              :  * 'len' is the expected length of the file.
      73              :  */
      74              : static void
      75         2722 : local_queue_fetch_file(rewind_source *source, const char *path, size_t len)
      76              : {
      77         2722 :     const char *datadir = ((local_source *) source)->datadir;
      78              :     PGIOAlignedBlock buf;
      79              :     char        srcpath[MAXPGPATH];
      80              :     int         srcfd;
      81              :     size_t      written_len;
      82              : 
      83         2722 :     snprintf(srcpath, sizeof(srcpath), "%s/%s", datadir, path);
      84              : 
      85              :     /* Open source file for reading */
      86         2722 :     srcfd = open(srcpath, O_RDONLY | PG_BINARY, 0);
      87         2722 :     if (srcfd < 0)
      88            0 :         pg_fatal("could not open source file \"%s\": %m",
      89              :                  srcpath);
      90              : 
      91              :     /* Truncate and open the target file for writing */
      92         2722 :     open_target_file(path, true);
      93              : 
      94         2722 :     written_len = 0;
      95              :     for (;;)
      96        48687 :     {
      97              :         ssize_t     read_len;
      98              : 
      99        51409 :         read_len = read(srcfd, buf.data, sizeof(buf));
     100              : 
     101        51409 :         if (read_len < 0)
     102            0 :             pg_fatal("could not read file \"%s\": %m", srcpath);
     103        51409 :         else if (read_len == 0)
     104         2722 :             break;              /* EOF reached */
     105              : 
     106        48687 :         write_target_range(buf.data, written_len, read_len);
     107        48687 :         written_len += read_len;
     108              :     }
     109              : 
     110              :     /*
     111              :      * A local source is not expected to change while we're rewinding, so
     112              :      * check that the size of the file matches our earlier expectation.
     113              :      */
     114         2722 :     if (written_len != len)
     115            1 :         pg_fatal("size of source file \"%s\" changed concurrently: %zu bytes expected, %zu copied",
     116              :                  srcpath, len, written_len);
     117              : 
     118         2721 :     if (close(srcfd) != 0)
     119            0 :         pg_fatal("could not close file \"%s\": %m", srcpath);
     120         2721 : }
     121              : 
     122              : /*
     123              :  * Copy a file from source to target, starting at 'off', for 'len' bytes.
     124              :  */
     125              : static void
     126          857 : local_queue_fetch_range(rewind_source *source, const char *path, off_t off,
     127              :                         size_t len)
     128              : {
     129          857 :     const char *datadir = ((local_source *) source)->datadir;
     130              :     PGIOAlignedBlock buf;
     131              :     char        srcpath[MAXPGPATH];
     132              :     int         srcfd;
     133          857 :     off_t       begin = off;
     134          857 :     off_t       end = off + len;
     135              : 
     136          857 :     snprintf(srcpath, sizeof(srcpath), "%s/%s", datadir, path);
     137              : 
     138          857 :     srcfd = open(srcpath, O_RDONLY | PG_BINARY, 0);
     139          857 :     if (srcfd < 0)
     140            0 :         pg_fatal("could not open source file \"%s\": %m",
     141              :                  srcpath);
     142              : 
     143          857 :     if (lseek(srcfd, begin, SEEK_SET) == -1)
     144            0 :         pg_fatal("could not seek in source file: %m");
     145              : 
     146          857 :     open_target_file(path, false);
     147              : 
     148         1960 :     while (end - begin > 0)
     149              :     {
     150              :         ssize_t     readlen;
     151              :         size_t      thislen;
     152              : 
     153         1103 :         if (end - begin > sizeof(buf))
     154          246 :             thislen = sizeof(buf);
     155              :         else
     156          857 :             thislen = end - begin;
     157              : 
     158         1103 :         readlen = read(srcfd, buf.data, thislen);
     159              : 
     160         1103 :         if (readlen < 0)
     161            0 :             pg_fatal("could not read file \"%s\": %m", srcpath);
     162         1103 :         else if (readlen == 0)
     163            0 :             pg_fatal("unexpected EOF while reading file \"%s\"", srcpath);
     164              : 
     165         1103 :         write_target_range(buf.data, begin, readlen);
     166         1103 :         begin += readlen;
     167              :     }
     168              : 
     169          857 :     if (close(srcfd) != 0)
     170            0 :         pg_fatal("could not close file \"%s\": %m", srcpath);
     171          857 : }
     172              : 
     173              : static void
     174            8 : local_finish_fetch(rewind_source *source)
     175              : {
     176              :     /*
     177              :      * Nothing to do, local_queue_fetch_range() copies the ranges immediately.
     178              :      */
     179            8 : }
     180              : 
     181              : static void
     182            8 : local_destroy(rewind_source *source)
     183              : {
     184            8 :     pfree(source);
     185            8 : }
        

Generated by: LCOV version 2.0-1