LCOV - differential code coverage report
Current view: top level - src/bin/pg_rewind - filemap.c (source / functions) Coverage Total Hit UNC UBC GNC CBC DCB
Current: a2387c32f2f8a1643c7d71b951587e6bcb2d4744 vs 371a302eecdc82274b0ae2967d18fd726a0aa6a1 Lines: 84.7 % 261 221 1 39 32 189 15
Current Date: 2025-10-26 12:31:50 -0700 Functions: 94.7 % 19 18 1 8 10 3
Baseline: lcov-20251027-010456-baseline Branches: 72.6 % 186 135 3 48 21 114
Baseline Date: 2025-10-26 11:01:32 +1300 Line coverage date bins:
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
(1,7] days: 97.0 % 33 32 1 32
(30,360] days: 94.1 % 17 16 1 16
(360..) days: 82.0 % 211 173 38 173
Function coverage date bins:
(1,7] days: 100.0 % 4 4 4
(30,360] days: 100.0 % 3 3 3
(360..) days: 91.7 % 12 11 1 4 7
Branch coverage date bins:
(1,7] days: 87.5 % 24 21 3 21
(30,360] days: 70.0 % 10 7 3 7
(360..) days: 70.4 % 152 107 45 107

 Age         Owner                    Branch data    TLA  Line data    Source code
                                  1                 :                : /*-------------------------------------------------------------------------
                                  2                 :                :  *
                                  3                 :                :  * filemap.c
                                  4                 :                :  *    A data structure for keeping track of files that have changed.
                                  5                 :                :  *
                                  6                 :                :  * This source file contains the logic to decide what to do with different
                                  7                 :                :  * kinds of files, and the data structure to support it.  Before modifying
                                  8                 :                :  * anything, pg_rewind collects information about all the files and their
                                  9                 :                :  * attributes in the target and source data directories.  It also scans the
                                 10                 :                :  * WAL log in the target, and collects information about data blocks that
                                 11                 :                :  * were changed.  All this information is stored in a hash table, using the
                                 12                 :                :  * file path relative to the root of the data directory as the key.
                                 13                 :                :  *
                                 14                 :                :  * After collecting all the information required, the decide_file_actions()
                                 15                 :                :  * function scans the hash table and decides what action needs to be taken
                                 16                 :                :  * for each file.  Finally, it sorts the array to the final order that the
                                 17                 :                :  * actions should be executed in.
                                 18                 :                :  *
                                 19                 :                :  * Copyright (c) 2013-2025, PostgreSQL Global Development Group
                                 20                 :                :  *
                                 21                 :                :  *-------------------------------------------------------------------------
                                 22                 :                :  */
                                 23                 :                : 
                                 24                 :                : #include "postgres_fe.h"
                                 25                 :                : 
                                 26                 :                : #include <sys/stat.h>
                                 27                 :                : #include <unistd.h>
                                 28                 :                : 
                                 29                 :                : #include "access/xlog_internal.h"
                                 30                 :                : #include "catalog/pg_tablespace_d.h"
                                 31                 :                : #include "common/file_utils.h"
                                 32                 :                : #include "common/hashfn_unstable.h"
                                 33                 :                : #include "common/string.h"
                                 34                 :                : #include "datapagemap.h"
                                 35                 :                : #include "filemap.h"
                                 36                 :                : #include "pg_rewind.h"
                                 37                 :                : 
                                 38                 :                : /*
                                 39                 :                :  * Define a hash table which we can use to store information about the files
                                 40                 :                :  * appearing in source and target systems.
                                 41                 :                :  */
                                 42                 :                : #define SH_PREFIX               filehash
                                 43                 :                : #define SH_ELEMENT_TYPE         file_entry_t
                                 44                 :                : #define SH_KEY_TYPE             const char *
                                 45                 :                : #define SH_KEY                  path
                                 46                 :                : #define SH_HASH_KEY(tb, key)    hash_string(key)
                                 47                 :                : #define SH_EQUAL(tb, a, b)      (strcmp(a, b) == 0)
                                 48                 :                : #define SH_SCOPE                static inline
                                 49                 :                : #define SH_RAW_ALLOCATOR        pg_malloc0
                                 50                 :                : #define SH_DECLARE
                                 51                 :                : #define SH_DEFINE
                                 52                 :                : #include "lib/simplehash.h"
                                 53                 :                : 
                                 54                 :                : #define FILEHASH_INITIAL_SIZE   1000
                                 55                 :                : 
                                 56                 :                : static filehash_hash *filehash;
                                 57                 :                : 
                                 58                 :                : static file_content_type_t getFileContentType(const char *path);
                                 59                 :                : static char *datasegpath(RelFileLocator rlocator, ForkNumber forknum,
                                 60                 :                :                          BlockNumber segno);
                                 61                 :                : 
                                 62                 :                : static file_entry_t *insert_filehash_entry(const char *path);
                                 63                 :                : static file_entry_t *lookup_filehash_entry(const char *path);
                                 64                 :                : 
                                 65                 :                : /*
                                 66                 :                :  * A separate hash table which tracks WAL files that must not be deleted.
                                 67                 :                :  */
                                 68                 :                : typedef struct keepwal_entry
                                 69                 :                : {
                                 70                 :                :     const char *path;
                                 71                 :                :     uint32      status;
                                 72                 :                : } keepwal_entry;
                                 73                 :                : 
                                 74                 :                : #define SH_PREFIX               keepwal
                                 75                 :                : #define SH_ELEMENT_TYPE         keepwal_entry
                                 76                 :                : #define SH_KEY_TYPE             const char *
                                 77                 :                : #define SH_KEY                  path
                                 78                 :                : #define SH_HASH_KEY(tb, key)    hash_string(key)
                                 79                 :                : #define SH_EQUAL(tb, a, b)      (strcmp(a, b) == 0)
                                 80                 :                : #define SH_SCOPE                static inline
                                 81                 :                : #define SH_RAW_ALLOCATOR        pg_malloc0
                                 82                 :                : #define SH_DECLARE
                                 83                 :                : #define SH_DEFINE
                                 84                 :                : #include "lib/simplehash.h"
                                 85                 :                : 
                                 86                 :                : #define KEEPWAL_INITIAL_SIZE    1000
                                 87                 :                : 
                                 88                 :                : 
                                 89                 :                : static keepwal_hash *keepwal = NULL;
                                 90                 :                : static bool keepwal_entry_exists(const char *path);
                                 91                 :                : 
                                 92                 :                : static int  final_filemap_cmp(const void *a, const void *b);
                                 93                 :                : 
                                 94                 :                : static bool check_file_excluded(const char *path, bool is_source);
                                 95                 :                : 
                                 96                 :                : /*
                                 97                 :                :  * Definition of one element part of an exclusion list, used to exclude
                                 98                 :                :  * contents when rewinding.  "name" is the name of the file or path to
                                 99                 :                :  * check for exclusion.  If "match_prefix" is true, any items matching
                                100                 :                :  * the name as prefix are excluded.
                                101                 :                :  */
                                102                 :                : struct exclude_list_item
                                103                 :                : {
                                104                 :                :     const char *name;
                                105                 :                :     bool        match_prefix;
                                106                 :                : };
                                107                 :                : 
                                108                 :                : /*
                                109                 :                :  * The contents of these directories are removed or recreated during server
                                110                 :                :  * start so they are not included in data processed by pg_rewind.
                                111                 :                :  *
                                112                 :                :  * Note: those lists should be kept in sync with what basebackup.c provides.
                                113                 :                :  * Some of the values, contrary to what basebackup.c uses, are hardcoded as
                                114                 :                :  * they are defined in backend-only headers.  So this list is maintained
                                115                 :                :  * with a best effort in mind.
                                116                 :                :  */
                                117                 :                : static const char *const excludeDirContents[] =
                                118                 :                : {
                                119                 :                :     /*
                                120                 :                :      * Skip temporary statistics files. PG_STAT_TMP_DIR must be skipped
                                121                 :                :      * because extensions like pg_stat_statements store data there.
                                122                 :                :      */
                                123                 :                :     "pg_stat_tmp",                /* defined as PG_STAT_TMP_DIR */
                                124                 :                : 
                                125                 :                :     /*
                                126                 :                :      * It is generally not useful to backup the contents of this directory
                                127                 :                :      * even if the intention is to restore to another primary. See backup.sgml
                                128                 :                :      * for a more detailed description.
                                129                 :                :      */
                                130                 :                :     "pg_replslot",                /* defined as PG_REPLSLOT_DIR */
                                131                 :                : 
                                132                 :                :     /* Contents removed on startup, see dsm_cleanup_for_mmap(). */
                                133                 :                :     "pg_dynshmem",                /* defined as PG_DYNSHMEM_DIR */
                                134                 :                : 
                                135                 :                :     /* Contents removed on startup, see AsyncShmemInit(). */
                                136                 :                :     "pg_notify",
                                137                 :                : 
                                138                 :                :     /*
                                139                 :                :      * Old contents are loaded for possible debugging but are not required for
                                140                 :                :      * normal operation, see SerialInit().
                                141                 :                :      */
                                142                 :                :     "pg_serial",
                                143                 :                : 
                                144                 :                :     /* Contents removed on startup, see DeleteAllExportedSnapshotFiles(). */
                                145                 :                :     "pg_snapshots",
                                146                 :                : 
                                147                 :                :     /* Contents zeroed on startup, see StartupSUBTRANS(). */
                                148                 :                :     "pg_subtrans",
                                149                 :                : 
                                150                 :                :     /* end of list */
                                151                 :                :     NULL
                                152                 :                : };
                                153                 :                : 
                                154                 :                : /*
                                155                 :                :  * List of files excluded from filemap processing.   Files are excluded
                                156                 :                :  * if their prefix match.
                                157                 :                :  */
                                158                 :                : static const struct exclude_list_item excludeFiles[] =
                                159                 :                : {
                                160                 :                :     /* Skip auto conf temporary file. */
                                161                 :                :     {"postgresql.auto.conf.tmp", false},  /* defined as PG_AUTOCONF_FILENAME */
                                162                 :                : 
                                163                 :                :     /* Skip current log file temporary file */
                                164                 :                :     {"current_logfiles.tmp", false},  /* defined as
                                165                 :                :                                          * LOG_METAINFO_DATAFILE_TMP */
                                166                 :                : 
                                167                 :                :     /* Skip relation cache because it is rebuilt on startup */
                                168                 :                :     {"pg_internal.init", true}, /* defined as RELCACHE_INIT_FILENAME */
                                169                 :                : 
                                170                 :                :     /*
                                171                 :                :      * If there is a backup_label or tablespace_map file, it indicates that a
                                172                 :                :      * recovery failed and this cluster probably can't be rewound, but exclude
                                173                 :                :      * them anyway if they are found.
                                174                 :                :      */
                                175                 :                :     {"backup_label", false},  /* defined as BACKUP_LABEL_FILE */
                                176                 :                :     {"tablespace_map", false},    /* defined as TABLESPACE_MAP */
                                177                 :                : 
                                178                 :                :     /*
                                179                 :                :      * If there's a backup_manifest, it belongs to a backup that was used to
                                180                 :                :      * start this server. It is *not* correct for this backup. Our
                                181                 :                :      * backup_manifest is injected into the backup separately if users want
                                182                 :                :      * it.
                                183                 :                :      */
                                184                 :                :     {"backup_manifest", false},
                                185                 :                : 
                                186                 :                :     {"postmaster.pid", false},
                                187                 :                :     {"postmaster.opts", false},
                                188                 :                : 
                                189                 :                :     /* end of list */
                                190                 :                :     {NULL, false}
                                191                 :                : };
                                192                 :                : 
                                193                 :                : /*
                                194                 :                :  * Initialize the hash table for the file map.
                                195                 :                :  */
                                196                 :                : void
 1818 heikki.linnakangas@i      197                 :CBC          15 : filehash_init(void)
                                198                 :                : {
                                199                 :             15 :     filehash = filehash_create(FILEHASH_INITIAL_SIZE, NULL);
 3871                           200                 :             15 : }
                                201                 :                : 
                                202                 :                : /* Look up entry for 'path', creating a new one if it doesn't exist */
                                203                 :                : static file_entry_t *
 1818                           204                 :          33977 : insert_filehash_entry(const char *path)
                                205                 :                : {
                                206                 :                :     file_entry_t *entry;
                                207                 :                :     bool        found;
                                208                 :                : 
                                209                 :          33977 :     entry = filehash_insert(filehash, path, &found);
                                210         [ +  + ]:          33977 :     if (!found)
                                211                 :                :     {
                                212                 :          17687 :         entry->path = pg_strdup(path);
    4 michael@paquier.xyz       213                 :GNC       17687 :         entry->content_type = getFileContentType(path);
                                214                 :                : 
 1818 heikki.linnakangas@i      215                 :CBC       17687 :         entry->target_exists = false;
                                216                 :          17687 :         entry->target_type = FILE_TYPE_UNDEFINED;
                                217                 :          17687 :         entry->target_size = 0;
                                218                 :          17687 :         entry->target_link_target = NULL;
                                219                 :          17687 :         entry->target_pages_to_overwrite.bitmap = NULL;
                                220                 :          17687 :         entry->target_pages_to_overwrite.bitmapsize = 0;
                                221                 :                : 
                                222                 :          17687 :         entry->source_exists = false;
                                223                 :          17687 :         entry->source_type = FILE_TYPE_UNDEFINED;
                                224                 :          17687 :         entry->source_size = 0;
                                225                 :          17687 :         entry->source_link_target = NULL;
                                226                 :                : 
                                227                 :          17687 :         entry->action = FILE_ACTION_UNDECIDED;
                                228                 :                :     }
                                229                 :                : 
                                230                 :          33977 :     return entry;
                                231                 :                : }
                                232                 :                : 
                                233                 :                : static file_entry_t *
                                234                 :          84621 : lookup_filehash_entry(const char *path)
                                235                 :                : {
                                236                 :          84621 :     return filehash_lookup(filehash, path);
                                237                 :                : }
                                238                 :                : 
                                239                 :                : /*
                                240                 :                :  * Initialize a hash table to store WAL file names that must be kept.
                                241                 :                :  */
                                242                 :                : void
  346 alvherre@alvh.no-ip.      243                 :             15 : keepwal_init(void)
                                244                 :                : {
                                245                 :                :     /* An initial hash size out of thin air */
                                246                 :             15 :     keepwal = keepwal_create(KEEPWAL_INITIAL_SIZE, NULL);
                                247                 :             15 : }
                                248                 :                : 
                                249                 :                : /* Mark the given file to prevent its removal */
                                250                 :                : void
                                251                 :             20 : keepwal_add_entry(const char *path)
                                252                 :                : {
                                253                 :                :     keepwal_entry *entry;
                                254                 :                :     bool        found;
                                255                 :                : 
                                256                 :                :     /* Should only be called with keepwal initialized */
                                257         [ -  + ]:             20 :     Assert(keepwal != NULL);
                                258                 :                : 
                                259                 :             20 :     entry = keepwal_insert(keepwal, path, &found);
                                260                 :                : 
                                261         [ +  - ]:             20 :     if (!found)
                                262                 :             20 :         entry->path = pg_strdup(path);
                                263                 :             20 : }
                                264                 :                : 
                                265                 :                : /* Return true if file is marked as not to be removed, false otherwise */
                                266                 :                : static bool
                                267                 :            654 : keepwal_entry_exists(const char *path)
                                268                 :                : {
                                269                 :            654 :     return keepwal_lookup(keepwal, path) != NULL;
                                270                 :                : }
                                271                 :                : 
                                272                 :                : /*
                                273                 :                :  * Callback for processing source file list.
                                274                 :                :  *
                                275                 :                :  * This is called once for every file in the source server.  We record the
                                276                 :                :  * type and size of the file, so that decide_file_action() can later decide what
                                277                 :                :  * to do with it.
                                278                 :                :  */
                                279                 :                : void
 1818 heikki.linnakangas@i      280                 :          17008 : process_source_file(const char *path, file_type_t type, size_t size,
                                281                 :                :                     const char *link_target)
                                282                 :                : {
                                283                 :                :     file_entry_t *entry;
                                284                 :                : 
                                285                 :                :     /*
                                286                 :                :      * Pretend that pg_wal is a directory, even if it's really a symlink. We
                                287                 :                :      * don't want to mess with the symlink itself, nor complain if it's a
                                288                 :                :      * symlink in source but not in target or vice versa.
                                289                 :                :      */
 3294 rhaas@postgresql.org      290   [ +  +  -  + ]:          17008 :     if (strcmp(path, "pg_wal") == 0 && type == FILE_TYPE_SYMLINK)
 3738 heikki.linnakangas@i      291                 :UBC           0 :         type = FILE_TYPE_DIRECTORY;
                                292                 :                : 
                                293                 :                :     /*
                                294                 :                :      * sanity check: a filename that looks like a data file better be a
                                295                 :                :      * regular file
                                296                 :                :      */
    4 michael@paquier.xyz       297   [ +  +  -  + ]:GNC       17008 :     if (type != FILE_TYPE_REGULAR && getFileContentType(path) == FILE_CONTENT_TYPE_RELATION)
 2401 peter@eisentraut.org      298                 :UBC           0 :         pg_fatal("data file \"%s\" in source is not a regular file", path);
                                299                 :                : 
                                300                 :                :     /* Remember this source file */
 1818 heikki.linnakangas@i      301                 :CBC       17008 :     entry = insert_filehash_entry(path);
                                302         [ -  + ]:          17008 :     if (entry->source_exists)
 1818 heikki.linnakangas@i      303                 :UBC           0 :         pg_fatal("duplicate source file \"%s\"", path);
 1818 heikki.linnakangas@i      304                 :CBC       17008 :     entry->source_exists = true;
                                305                 :          17008 :     entry->source_type = type;
                                306                 :          17008 :     entry->source_size = size;
                                307         [ +  + ]:          17008 :     entry->source_link_target = link_target ? pg_strdup(link_target) : NULL;
 3871                           308                 :          17008 : }
                                309                 :                : 
                                310                 :                : /*
                                311                 :                :  * Callback for processing target file list.
                                312                 :                :  *
                                313                 :                :  * Record the type and size of the file, like process_source_file() does.
                                314                 :                :  */
                                315                 :                : void
 1818                           316                 :          16969 : process_target_file(const char *path, file_type_t type, size_t size,
                                317                 :                :                     const char *link_target)
                                318                 :                : {
                                319                 :                :     file_entry_t *entry;
                                320                 :                : 
                                321                 :                :     /*
                                322                 :                :      * Do not apply any exclusion filters here.  This has advantage to remove
                                323                 :                :      * from the target data folder all paths which have been filtered out from
                                324                 :                :      * the source data folder when processing the source files.
                                325                 :                :      */
                                326                 :                : 
                                327                 :                :     /*
                                328                 :                :      * Like in process_source_file, pretend that pg_wal is always a directory.
                                329                 :                :      */
 3294 rhaas@postgresql.org      330   [ +  +  +  + ]:          16969 :     if (strcmp(path, "pg_wal") == 0 && type == FILE_TYPE_SYMLINK)
 3738 heikki.linnakangas@i      331                 :              2 :         type = FILE_TYPE_DIRECTORY;
                                332                 :                : 
                                333                 :                :     /* Remember this target file */
 1818                           334                 :          16969 :     entry = insert_filehash_entry(path);
                                335         [ -  + ]:          16969 :     if (entry->target_exists)
 1818 heikki.linnakangas@i      336                 :UBC           0 :         pg_fatal("duplicate source file \"%s\"", path);
 1818 heikki.linnakangas@i      337                 :CBC       16969 :     entry->target_exists = true;
                                338                 :          16969 :     entry->target_type = type;
                                339                 :          16969 :     entry->target_size = size;
                                340         [ +  + ]:          16969 :     entry->target_link_target = link_target ? pg_strdup(link_target) : NULL;
 3871                           341                 :          16969 : }
                                342                 :                : 
                                343                 :                : /*
                                344                 :                :  * This callback gets called while we read the WAL in the target, for every
                                345                 :                :  * block that has changed in the target system.  It decides if the given
                                346                 :                :  * 'blkno' in the target relfile needs to be overwritten from the source, and
                                347                 :                :  * if so, records it in 'target_pages_to_overwrite' bitmap.
                                348                 :                :  *
                                349                 :                :  * NOTE: All the files on both systems must have already been added to the
                                350                 :                :  * hash table!
                                351                 :                :  */
                                352                 :                : void
 1209 rhaas@postgresql.org      353                 :          84621 : process_target_wal_block_change(ForkNumber forknum, RelFileLocator rlocator,
                                354                 :                :                                 BlockNumber blkno)
                                355                 :                : {
                                356                 :                :     char       *path;
                                357                 :                :     file_entry_t *entry;
                                358                 :                :     BlockNumber blkno_inseg;
                                359                 :                :     int         segno;
                                360                 :                : 
 3871 heikki.linnakangas@i      361                 :          84621 :     segno = blkno / RELSEG_SIZE;
                                362                 :          84621 :     blkno_inseg = blkno % RELSEG_SIZE;
                                363                 :                : 
 1209 rhaas@postgresql.org      364                 :          84621 :     path = datasegpath(rlocator, forknum, segno);
 1818 heikki.linnakangas@i      365                 :          84621 :     entry = lookup_filehash_entry(path);
 3791 fujii@postgresql.org      366                 :          84621 :     pfree(path);
                                367                 :                : 
                                368                 :                :     /*
                                369                 :                :      * If the block still exists in both systems, remember it. Otherwise we
                                370                 :                :      * can safely ignore it.
                                371                 :                :      *
                                372                 :                :      * If the block is beyond the EOF in the source system, or the file
                                373                 :                :      * doesn't exist in the source at all, we're going to truncate/remove it
                                374                 :                :      * away from the target anyway. Likewise, if it doesn't exist in the
                                375                 :                :      * target anymore, we will copy it over with the "tail" from the source
                                376                 :                :      * system, anyway.
                                377                 :                :      *
                                378                 :                :      * It is possible to find WAL for a file that doesn't exist on either
                                379                 :                :      * system anymore. It means that the relation was dropped later in the
                                380                 :                :      * target system, and independently on the source system too, or that it
                                381                 :                :      * was created and dropped in the target system and it never existed in
                                382                 :                :      * the source. Either way, we can safely ignore it.
                                383                 :                :      */
 3871 heikki.linnakangas@i      384         [ +  - ]:          84621 :     if (entry)
                                385                 :                :     {
    4 michael@paquier.xyz       386         [ -  + ]:GNC       84621 :         Assert(entry->content_type == FILE_CONTENT_TYPE_RELATION);
                                387                 :                : 
 1812 heikki.linnakangas@i      388         [ +  + ]:CBC       84621 :         if (entry->target_exists)
                                389                 :                :         {
                                390         [ -  + ]:          84617 :             if (entry->target_type != FILE_TYPE_REGULAR)
 1812 heikki.linnakangas@i      391                 :UBC           0 :                 pg_fatal("unexpected page modification for non-regular file \"%s\"",
                                392                 :                :                          entry->path);
                                393                 :                : 
 1812 heikki.linnakangas@i      394         [ +  + ]:CBC       84617 :             if (entry->source_exists)
                                395                 :                :             {
                                396                 :                :                 off_t       end_offset;
                                397                 :                : 
                                398                 :          82921 :                 end_offset = (blkno_inseg + 1) * BLCKSZ;
                                399   [ +  +  +  + ]:          82921 :                 if (end_offset <= entry->source_size && end_offset <= entry->target_size)
                                400                 :           2921 :                     datapagemap_add(&entry->target_pages_to_overwrite, blkno_inseg);
                                401                 :                :             }
                                402                 :                :         }
                                403                 :                :     }
 3871                           404                 :          84621 : }
                                405                 :                : 
                                406                 :                : /*
                                407                 :                :  * Is this the path of file that pg_rewind can skip copying?
                                408                 :                :  */
                                409                 :                : static bool
 2601 peter_e@gmx.net           410                 :          17670 : check_file_excluded(const char *path, bool is_source)
                                411                 :                : {
                                412                 :                :     char        localpath[MAXPGPATH];
                                413                 :                :     int         excludeIdx;
                                414                 :                :     const char *filename;
                                415                 :                : 
                                416                 :                :     /*
                                417                 :                :      * Skip all temporary files, .../pgsql_tmp/... and .../pgsql_tmp.*
                                418                 :                :      */
 1818 heikki.linnakangas@i      419         [ +  + ]:          17670 :     if (strstr(path, "/" PG_TEMP_FILE_PREFIX) != NULL ||
                                420         [ -  + ]:          17655 :         strstr(path, "/" PG_TEMP_FILES_DIR "/") != NULL)
                                421                 :                :     {
                                422                 :             15 :         return true;
                                423                 :                :     }
                                424                 :                : 
                                425                 :                :     /* check individual files... */
 2072 michael@paquier.xyz       426         [ +  + ]:         158595 :     for (excludeIdx = 0; excludeFiles[excludeIdx].name != NULL; excludeIdx++)
                                427                 :                :     {
                                428                 :         141014 :         int         cmplen = strlen(excludeFiles[excludeIdx].name);
                                429                 :                : 
 2769 fujii@postgresql.org      430                 :         141014 :         filename = last_dir_separator(path);
                                431         [ +  + ]:         141014 :         if (filename == NULL)
                                432                 :           3100 :             filename = path;
                                433                 :                :         else
                                434                 :         137914 :             filename++;
                                435                 :                : 
 2072 michael@paquier.xyz       436         [ +  + ]:         141014 :         if (!excludeFiles[excludeIdx].match_prefix)
                                437                 :         123359 :             cmplen++;
                                438         [ +  + ]:         141014 :         if (strncmp(filename, excludeFiles[excludeIdx].name, cmplen) == 0)
                                439                 :                :         {
 2601 peter_e@gmx.net           440         [ +  - ]:             74 :             if (is_source)
 2401 peter@eisentraut.org      441         [ +  - ]:             74 :                 pg_log_debug("entry \"%s\" excluded from source file list",
                                442                 :                :                              path);
                                443                 :                :             else
 2401 peter@eisentraut.org      444         [ #  # ]:UBC           0 :                 pg_log_debug("entry \"%s\" excluded from target file list",
                                445                 :                :                              path);
 2769 fujii@postgresql.org      446                 :CBC          74 :             return true;
                                447                 :                :         }
                                448                 :                :     }
                                449                 :                : 
                                450                 :                :     /*
                                451                 :                :      * ... And check some directories.  Note that this includes any contents
                                452                 :                :      * within the directories themselves.
                                453                 :                :      */
                                454         [ +  + ]:         140633 :     for (excludeIdx = 0; excludeDirContents[excludeIdx] != NULL; excludeIdx++)
                                455                 :                :     {
                                456                 :         123067 :         snprintf(localpath, sizeof(localpath), "%s/",
                                457                 :         123067 :                  excludeDirContents[excludeIdx]);
                                458         [ +  + ]:         123067 :         if (strstr(path, localpath) == path)
                                459                 :                :         {
 2601 peter_e@gmx.net           460         [ +  - ]:             15 :             if (is_source)
 2401 peter@eisentraut.org      461         [ +  - ]:             15 :                 pg_log_debug("entry \"%s\" excluded from source file list",
                                462                 :                :                              path);
                                463                 :                :             else
 2401 peter@eisentraut.org      464         [ #  # ]:UBC           0 :                 pg_log_debug("entry \"%s\" excluded from target file list",
                                465                 :                :                              path);
 2769 fujii@postgresql.org      466                 :CBC          15 :             return true;
                                467                 :                :         }
                                468                 :                :     }
                                469                 :                : 
                                470                 :          17566 :     return false;
                                471                 :                : }
                                472                 :                : 
                                473                 :                : static const char *
 3871 heikki.linnakangas@i      474                 :           5855 : action_to_str(file_action_t action)
                                475                 :                : {
                                476   [ +  +  +  +  :           5855 :     switch (action)
                                           +  +  - ]
                                477                 :                :     {
                                478                 :            433 :         case FILE_ACTION_NONE:
                                479                 :            433 :             return "NONE";
                                480                 :           4677 :         case FILE_ACTION_COPY:
                                481                 :           4677 :             return "COPY";
                                482                 :              4 :         case FILE_ACTION_TRUNCATE:
                                483                 :              4 :             return "TRUNCATE";
                                484                 :              5 :         case FILE_ACTION_COPY_TAIL:
                                485                 :              5 :             return "COPY_TAIL";
                                486                 :              9 :         case FILE_ACTION_CREATE:
                                487                 :              9 :             return "CREATE";
                                488                 :            727 :         case FILE_ACTION_REMOVE:
                                489                 :            727 :             return "REMOVE";
                                490                 :                : 
 3871 heikki.linnakangas@i      491                 :UBC           0 :         default:
                                492                 :              0 :             return "unknown";
                                493                 :                :     }
                                494                 :                : }
                                495                 :                : 
                                496                 :                : /*
                                497                 :                :  * Calculate the totals needed for progress reports.
                                498                 :                :  */
                                499                 :                : void
 1818                           500                 :              0 : calculate_totals(filemap_t *filemap)
                                501                 :                : {
                                502                 :                :     file_entry_t *entry;
                                503                 :                :     int         i;
                                504                 :                : 
                                505                 :              0 :     filemap->total_size = 0;
                                506                 :              0 :     filemap->fetch_size = 0;
                                507                 :                : 
                                508         [ #  # ]:              0 :     for (i = 0; i < filemap->nentries; i++)
                                509                 :                :     {
                                510                 :              0 :         entry = filemap->entries[i];
                                511                 :                : 
                                512         [ #  # ]:              0 :         if (entry->source_type != FILE_TYPE_REGULAR)
 3871                           513                 :              0 :             continue;
                                514                 :                : 
 1818                           515                 :              0 :         filemap->total_size += entry->source_size;
                                516                 :                : 
 3871                           517         [ #  # ]:              0 :         if (entry->action == FILE_ACTION_COPY)
                                518                 :                :         {
 1818                           519                 :              0 :             filemap->fetch_size += entry->source_size;
 3871                           520                 :              0 :             continue;
                                521                 :                :         }
                                522                 :                : 
                                523         [ #  # ]:              0 :         if (entry->action == FILE_ACTION_COPY_TAIL)
 1818                           524                 :              0 :             filemap->fetch_size += (entry->source_size - entry->target_size);
                                525                 :                : 
                                526         [ #  # ]:              0 :         if (entry->target_pages_to_overwrite.bitmapsize > 0)
                                527                 :                :         {
                                528                 :                :             datapagemap_iterator_t *iter;
                                529                 :                :             BlockNumber blk;
                                530                 :                : 
                                531                 :              0 :             iter = datapagemap_iterate(&entry->target_pages_to_overwrite);
 3871                           532         [ #  # ]:              0 :             while (datapagemap_next(iter, &blk))
 1818                           533                 :              0 :                 filemap->fetch_size += BLCKSZ;
                                534                 :                : 
 3871                           535                 :              0 :             pg_free(iter);
                                536                 :                :         }
                                537                 :                :     }
                                538                 :              0 : }
                                539                 :                : 
                                540                 :                : void
 1818 heikki.linnakangas@i      541                 :CBC          15 : print_filemap(filemap_t *filemap)
                                542                 :                : {
                                543                 :                :     file_entry_t *entry;
                                544                 :                :     int         i;
                                545                 :                : 
                                546         [ +  + ]:          17702 :     for (i = 0; i < filemap->nentries; i++)
                                547                 :                :     {
                                548                 :          17687 :         entry = filemap->entries[i];
                                549                 :                : 
 3871                           550         [ +  + ]:          17687 :         if (entry->action != FILE_ACTION_NONE ||
    2 michael@paquier.xyz       551         [ +  + ]:GNC       12265 :             entry->content_type == FILE_CONTENT_TYPE_WAL ||
 1818 heikki.linnakangas@i      552         [ +  + ]:CBC       12247 :             entry->target_pages_to_overwrite.bitmapsize > 0)
                                553                 :                :         {
 2401 peter@eisentraut.org      554         [ +  - ]:           5855 :             pg_log_debug("%s (%s)", entry->path,
                                555                 :                :                          action_to_str(entry->action));
                                556                 :                : 
 1818 heikki.linnakangas@i      557         [ +  + ]:           5855 :             if (entry->target_pages_to_overwrite.bitmapsize > 0)
                                558                 :            423 :                 datapagemap_print(&entry->target_pages_to_overwrite);
                                559                 :                :         }
                                560                 :                :     }
 3871                           561                 :             15 :     fflush(stdout);
                                562                 :             15 : }
                                563                 :                : 
                                564                 :                : /*
                                565                 :                :  * Determine what kind of file this one looks like.
                                566                 :                :  */
                                567                 :                : static file_content_type_t
    4 michael@paquier.xyz       568                 :GNC       18106 : getFileContentType(const char *path)
                                569                 :                : {
                                570                 :                :     RelFileLocator rlocator;
                                571                 :                :     unsigned int segNo;
                                572                 :                :     int         nmatch;
                                573                 :          18106 :     file_content_type_t result = FILE_CONTENT_TYPE_OTHER;
                                574                 :                : 
                                575                 :                :     /* Check if it is a WAL file. */
                                576         [ +  + ]:          18106 :     if (strncmp("pg_wal/", path, 7) == 0)
                                577                 :                :     {
                                578                 :            158 :         const char *filename = path + 7;    /* Skip "pg_wal/" */
                                579                 :                : 
                                580         [ +  + ]:            158 :         if (IsXLogFileName(filename))
                                581                 :             65 :             return FILE_CONTENT_TYPE_WAL;
                                582                 :                :         else
                                583                 :             93 :             return FILE_CONTENT_TYPE_OTHER;
                                584                 :                :     }
                                585                 :                : 
                                586                 :                :     /*----
                                587                 :                :      * Does it look like a relation data file?
                                588                 :                :      *
                                589                 :                :      * For our purposes, only files belonging to the main fork are considered
                                590                 :                :      * relation files. Other forks are always copied in toto, because we
                                591                 :                :      * cannot reliably track changes to them, because WAL only contains block
                                592                 :                :      * references for the main fork.
                                593                 :                :      *
                                594                 :                :      * Relation data files can be in one of the following directories:
                                595                 :                :      *
                                596                 :                :      * global/
                                597                 :                :      *      shared relations
                                598                 :                :      *
                                599                 :                :      * base/<db oid>/
                                600                 :                :      *      regular relations, default tablespace
                                601                 :                :      *
                                602                 :                :      * pg_tblspc/<tblspc oid>/<tblspc version>/
                                603                 :                :      *      within a non-default tablespace (the name of the directory
                                604                 :                :      *      depends on version)
                                605                 :                :      *
                                606                 :                :      * And the relation data files themselves have a filename like:
                                607                 :                :      *
                                608                 :                :      * <oid>.<segment number>
                                609                 :                :      *
                                610                 :                :      *----
                                611                 :                :      */
 1209 rhaas@postgresql.org      612                 :CBC       17948 :     rlocator.spcOid = InvalidOid;
                                613                 :          17948 :     rlocator.dbOid = InvalidOid;
                                614                 :          17948 :     rlocator.relNumber = InvalidRelFileNumber;
 3871 heikki.linnakangas@i      615                 :          17948 :     segNo = 0;
    4 michael@paquier.xyz       616                 :GNC       17948 :     result = FILE_CONTENT_TYPE_OTHER;
                                617                 :                : 
 1125 rhaas@postgresql.org      618                 :CBC       17948 :     nmatch = sscanf(path, "global/%u.%u", &rlocator.relNumber, &segNo);
 3871 heikki.linnakangas@i      619   [ +  +  -  + ]:          17948 :     if (nmatch == 1 || nmatch == 2)
                                620                 :                :     {
 1209 rhaas@postgresql.org      621                 :            840 :         rlocator.spcOid = GLOBALTABLESPACE_OID;
                                622                 :            840 :         rlocator.dbOid = 0;
    4 michael@paquier.xyz       623                 :GNC         840 :         result = FILE_CONTENT_TYPE_RELATION;
                                624                 :                :     }
                                625                 :                :     else
                                626                 :                :     {
 1125 rhaas@postgresql.org      627                 :CBC       17108 :         nmatch = sscanf(path, "base/%u/%u.%u",
                                628                 :                :                         &rlocator.dbOid, &rlocator.relNumber, &segNo);
 3871 heikki.linnakangas@i      629   [ +  +  -  + ]:          17108 :         if (nmatch == 2 || nmatch == 3)
                                630                 :                :         {
 1209 rhaas@postgresql.org      631                 :          15882 :             rlocator.spcOid = DEFAULTTABLESPACE_OID;
    4 michael@paquier.xyz       632                 :GNC       15882 :             result = FILE_CONTENT_TYPE_RELATION;
                                633                 :                :         }
                                634                 :                :         else
                                635                 :                :         {
 1125 rhaas@postgresql.org      636                 :CBC        1226 :             nmatch = sscanf(path, "pg_tblspc/%u/" TABLESPACE_VERSION_DIRECTORY "/%u/%u.%u",
                                637                 :                :                             &rlocator.spcOid, &rlocator.dbOid, &rlocator.relNumber,
                                638                 :                :                             &segNo);
 2792 fujii@postgresql.org      639   [ +  +  -  + ]:           1226 :             if (nmatch == 3 || nmatch == 4)
    4 michael@paquier.xyz       640                 :GNC          12 :                 result = FILE_CONTENT_TYPE_RELATION;
                                641                 :                :         }
                                642                 :                :     }
                                643                 :                : 
                                644                 :                :     /*
                                645                 :                :      * The sscanf tests above can match files that have extra characters at
                                646                 :                :      * the end. To eliminate such cases, cross-check that GetRelationPath
                                647                 :                :      * creates the exact same filename, when passed the RelFileLocator
                                648                 :                :      * information we extracted from the filename.
                                649                 :                :      */
                                650         [ +  + ]:          17948 :     if (result == FILE_CONTENT_TYPE_RELATION)
                                651                 :                :     {
 1209 rhaas@postgresql.org      652                 :CBC       16734 :         char       *check_path = datasegpath(rlocator, MAIN_FORKNUM, segNo);
                                653                 :                : 
 3871 heikki.linnakangas@i      654         [ +  + ]:          16734 :         if (strcmp(check_path, path) != 0)
    4 michael@paquier.xyz       655                 :GNC        4084 :             result = FILE_CONTENT_TYPE_OTHER;
                                656                 :                : 
 3871 heikki.linnakangas@i      657                 :CBC       16734 :         pfree(check_path);
                                658                 :                :     }
                                659                 :                : 
    4 michael@paquier.xyz       660                 :GNC       17948 :     return result;
                                661                 :                : }
                                662                 :                : 
                                663                 :                : /*
                                664                 :                :  * A helper function to create the path of a relation file and segment.
                                665                 :                :  *
                                666                 :                :  * The returned path is palloc'd
                                667                 :                :  */
                                668                 :                : static char *
 1209 rhaas@postgresql.org      669                 :CBC      101355 : datasegpath(RelFileLocator rlocator, ForkNumber forknum, BlockNumber segno)
                                670                 :                : {
                                671                 :                :     RelPathStr  path;
                                672                 :                :     char       *segpath;
                                673                 :                : 
                                674                 :         101355 :     path = relpathperm(rlocator, forknum);
 3871 heikki.linnakangas@i      675         [ -  + ]:         101355 :     if (segno > 0)
                                676                 :                :     {
  244 andres@anarazel.de        677                 :UBC           0 :         segpath = psprintf("%s.%u", path.str, segno);
 3871 heikki.linnakangas@i      678                 :              0 :         return segpath;
                                679                 :                :     }
                                680                 :                :     else
  244 andres@anarazel.de        681                 :CBC      101355 :         return pstrdup(path.str);
                                682                 :                : }
                                683                 :                : 
                                684                 :                : /*
                                685                 :                :  * In the final stage, the filemap is sorted so that removals come last.
                                686                 :                :  * From disk space usage point of view, it would be better to do removals
                                687                 :                :  * first, but for now, safety first. If a whole directory is deleted, all
                                688                 :                :  * files and subdirectories inside it need to removed first. On creation,
                                689                 :                :  * parent directory needs to be created before files and directories inside
                                690                 :                :  * it. To achieve that, the file_action_t enum is ordered so that we can
                                691                 :                :  * just sort on that first. Furthermore, sort REMOVE entries in reverse
                                692                 :                :  * path order, so that "foo/bar" subdirectory is removed before "foo".
                                693                 :                :  */
                                694                 :                : static int
 3871 heikki.linnakangas@i      695                 :         189055 : final_filemap_cmp(const void *a, const void *b)
                                696                 :                : {
                                697                 :         189055 :     file_entry_t *fa = *((file_entry_t **) a);
                                698                 :         189055 :     file_entry_t *fb = *((file_entry_t **) b);
                                699                 :                : 
                                700         [ +  + ]:         189055 :     if (fa->action > fb->action)
                                701                 :           5304 :         return 1;
                                702         [ +  + ]:         183751 :     if (fa->action < fb->action)
                                703                 :          10386 :         return -1;
                                704                 :                : 
                                705         [ +  + ]:         173365 :     if (fa->action == FILE_ACTION_REMOVE)
 2579 tgl@sss.pgh.pa.us         706                 :           5909 :         return strcmp(fb->path, fa->path);
                                707                 :                :     else
 3871 heikki.linnakangas@i      708                 :         167456 :         return strcmp(fa->path, fb->path);
                                709                 :                : }
                                710                 :                : 
                                711                 :                : /*
                                712                 :                :  * Decide what to do with a WAL segment file based on its position
                                713                 :                :  * relative to the point of divergence.
                                714                 :                :  *
                                715                 :                :  * Caller is responsible for ensuring that the file exists on both
                                716                 :                :  * source and target servers.
                                717                 :                :  */
                                718                 :                : static file_action_t
    2 michael@paquier.xyz       719                 :GNC          32 : decide_wal_file_action(const char *fname, XLogSegNo last_common_segno,
                                720                 :                :                        size_t source_size, size_t target_size)
                                721                 :                : {
                                722                 :                :     TimeLineID  file_tli;
                                723                 :                :     XLogSegNo   file_segno;
                                724                 :                : 
                                725                 :                :     /* Get current WAL segment number given current segment file name */
                                726                 :             32 :     XLogFromFileName(fname, &file_tli, &file_segno, WalSegSz);
                                727                 :                : 
                                728                 :                :     /*
                                729                 :                :      * Avoid copying files before the last common segment.
                                730                 :                :      *
                                731                 :                :      * These files exist on the source and the target servers, so they should
                                732                 :                :      * be identical and located strictly before the segment that contains the
                                733                 :                :      * LSN where target and source servers have diverged.
                                734                 :                :      *
                                735                 :                :      * While we are on it, double-check the size of each file and copy the
                                736                 :                :      * file if they do not match, in case.
                                737                 :                :      */
                                738   [ +  +  +  + ]:             32 :     if (file_segno < last_common_segno &&
                                739                 :                :         source_size == target_size)
                                740                 :             16 :         return FILE_ACTION_NONE;
                                741                 :                : 
                                742                 :             16 :     return FILE_ACTION_COPY;
                                743                 :                : }
                                744                 :                : 
                                745                 :                : /*
                                746                 :                :  * Decide what action to perform to a file.
                                747                 :                :  */
                                748                 :                : static file_action_t
                                749                 :          17687 : decide_file_action(file_entry_t *entry, XLogSegNo last_common_segno)
                                750                 :                : {
 1818 heikki.linnakangas@i      751                 :CBC       17687 :     const char *path = entry->path;
                                752                 :                : 
                                753                 :                :     /*
                                754                 :                :      * Don't touch the control file. It is handled specially, after copying
                                755                 :                :      * all the other files.
                                756                 :                :      */
  203 fujii@postgresql.org      757         [ +  + ]:          17687 :     if (strcmp(path, XLOG_CONTROL_FILE) == 0)
 1818 heikki.linnakangas@i      758                 :             15 :         return FILE_ACTION_NONE;
                                759                 :                : 
                                760                 :                :     /* Skip macOS system files */
  622 dgustafsson@postgres      761         [ +  + ]:          17672 :     if (strstr(path, ".DS_Store") != NULL)
                                762                 :              2 :         return FILE_ACTION_NONE;
                                763                 :                : 
                                764                 :                :     /*
                                765                 :                :      * Remove all files matching the exclusion filters in the target.
                                766                 :                :      */
 1818 heikki.linnakangas@i      767         [ +  + ]:          17670 :     if (check_file_excluded(path, true))
                                768                 :                :     {
                                769         [ +  + ]:            104 :         if (entry->target_exists)
                                770                 :             75 :             return FILE_ACTION_REMOVE;
                                771                 :                :         else
                                772                 :             29 :             return FILE_ACTION_NONE;
                                773                 :                :     }
                                774                 :                : 
                                775                 :                :     /*
                                776                 :                :      * Handle cases where the file is missing from one of the systems.
                                777                 :                :      */
                                778   [ +  +  +  - ]:          17566 :     if (!entry->target_exists && entry->source_exists)
                                779                 :                :     {
                                780                 :                :         /*
                                781                 :                :          * File exists in source, but not in target. Copy it in toto. (If it's
                                782                 :                :          * a relation data file, WAL replay after rewinding should re-create
                                783                 :                :          * it anyway. But there's no harm in copying it now.)
                                784                 :                :          */
                                785   [ +  +  -  - ]:            687 :         switch (entry->source_type)
                                786                 :                :         {
                                787                 :              9 :             case FILE_TYPE_DIRECTORY:
                                788                 :                :             case FILE_TYPE_SYMLINK:
                                789                 :              9 :                 return FILE_ACTION_CREATE;
                                790                 :            678 :             case FILE_TYPE_REGULAR:
                                791                 :            678 :                 return FILE_ACTION_COPY;
 1818 heikki.linnakangas@i      792                 :UBC           0 :             case FILE_TYPE_UNDEFINED:
                                793                 :              0 :                 pg_fatal("unknown file type for \"%s\"", entry->path);
                                794                 :                :                 break;
                                795                 :                :         }
                                796                 :                :     }
 1818 heikki.linnakangas@i      797   [ +  -  +  + ]:CBC       16879 :     else if (entry->target_exists && !entry->source_exists)
                                798                 :                :     {
                                799                 :                :         /*
                                800                 :                :          * For files that exist in target but not in source, we check the
                                801                 :                :          * keepwal hash table; any files listed therein must not be removed.
                                802                 :                :          */
  346 alvherre@alvh.no-ip.      803         [ +  + ]:            654 :         if (keepwal_entry_exists(path))
                                804                 :                :         {
                                805         [ +  - ]:              2 :             pg_log_debug("Not removing file \"%s\" because it is required for recovery", path);
                                806                 :              2 :             return FILE_ACTION_NONE;
                                807                 :                :         }
 1818 heikki.linnakangas@i      808                 :            652 :         return FILE_ACTION_REMOVE;
                                809                 :                :     }
                                810   [ -  +  -  - ]:          16225 :     else if (!entry->target_exists && !entry->source_exists)
                                811                 :                :     {
                                812                 :                :         /*
                                813                 :                :          * Doesn't exist in either server. Why does it have an entry in the
                                814                 :                :          * first place??
                                815                 :                :          */
 1818 heikki.linnakangas@i      816                 :UBC           0 :         Assert(false);
                                817                 :                :         return FILE_ACTION_NONE;
                                818                 :                :     }
                                819                 :                : 
                                820                 :                :     /*
                                821                 :                :      * Otherwise, the file exists on both systems
                                822                 :                :      */
 1818 heikki.linnakangas@i      823   [ +  -  +  - ]:CBC       16225 :     Assert(entry->target_exists && entry->source_exists);
                                824                 :                : 
                                825         [ -  + ]:          16225 :     if (entry->source_type != entry->target_type)
                                826                 :                :     {
                                827                 :                :         /* But it's a different kind of object. Strange.. */
 1818 heikki.linnakangas@i      828                 :UBC           0 :         pg_fatal("file \"%s\" is of different type in source and target", entry->path);
                                829                 :                :     }
                                830                 :                : 
                                831                 :                :     /*
                                832                 :                :      * PG_VERSION files should be identical on both systems, but avoid
                                833                 :                :      * overwriting them for paranoia.
                                834                 :                :      */
 1818 heikki.linnakangas@i      835         [ +  + ]:CBC       16225 :     if (pg_str_endswith(entry->path, "PG_VERSION"))
                                836                 :             64 :         return FILE_ACTION_NONE;
                                837                 :                : 
                                838   [ +  -  +  -  :          16161 :     switch (entry->source_type)
                                                 - ]
                                839                 :                :     {
                                840                 :            408 :         case FILE_TYPE_DIRECTORY:
                                841                 :            408 :             return FILE_ACTION_NONE;
                                842                 :                : 
 1818 heikki.linnakangas@i      843                 :UBC           0 :         case FILE_TYPE_SYMLINK:
                                844                 :                : 
                                845                 :                :             /*
                                846                 :                :              * XXX: Should we check if it points to the same target?
                                847                 :                :              */
                                848                 :              0 :             return FILE_ACTION_NONE;
                                849                 :                : 
 1818 heikki.linnakangas@i      850                 :CBC       15753 :         case FILE_TYPE_REGULAR:
    4 michael@paquier.xyz       851         [ +  + ]:GNC       15753 :             if (entry->content_type == FILE_CONTENT_TYPE_WAL)
                                852                 :                :             {
                                853                 :                :                 /* Handle WAL segment file */
    2                           854                 :             32 :                 const char *filename = last_dir_separator(entry->path);
                                855                 :                : 
                                856         [ -  + ]:             32 :                 if (filename == NULL)
    2 michael@paquier.xyz       857                 :UNC           0 :                     filename = entry->path;
                                858                 :                :                 else
    2 michael@paquier.xyz       859                 :GNC          32 :                     filename++; /* Skip the separator */
                                860                 :                : 
                                861                 :             32 :                 return decide_wal_file_action(filename, last_common_segno,
                                862                 :                :                                               entry->source_size,
                                863                 :                :                                               entry->target_size);
                                864                 :                :             }
    4                           865         [ +  + ]:          15721 :             else if (entry->content_type != FILE_CONTENT_TYPE_RELATION)
                                866                 :                :             {
                                867                 :                :                 /*
                                868                 :                :                  * It's a non-data file that we have no special processing
                                869                 :                :                  * for. Copy it in toto.
                                870                 :                :                  */
 1818 heikki.linnakangas@i      871                 :CBC        3983 :                 return FILE_ACTION_COPY;
                                872                 :                :             }
                                873                 :                :             else
                                874                 :                :             {
                                875                 :                :                 /*
                                876                 :                :                  * It's a data file that exists in both systems.
                                877                 :                :                  *
                                878                 :                :                  * If it's larger in target, we can truncate it. There will
                                879                 :                :                  * also be a WAL record of the truncation in the source
                                880                 :                :                  * system, so WAL replay would eventually truncate the target
                                881                 :                :                  * too, but we might as well do it now.
                                882                 :                :                  *
                                883                 :                :                  * If it's smaller in the target, it means that it has been
                                884                 :                :                  * truncated in the target, or enlarged in the source, or
                                885                 :                :                  * both. If it was truncated in the target, we need to copy
                                886                 :                :                  * the missing tail from the source system. If it was enlarged
                                887                 :                :                  * in the source system, there will be WAL records in the
                                888                 :                :                  * source system for the new blocks, so we wouldn't need to
                                889                 :                :                  * copy them here. But we don't know which scenario we're
                                890                 :                :                  * dealing with, and there's no harm in copying the missing
                                891                 :                :                  * blocks now, so do it now.
                                892                 :                :                  *
                                893                 :                :                  * If it's the same size, do nothing here. Any blocks modified
                                894                 :                :                  * in the target will be copied based on parsing the target
                                895                 :                :                  * system's WAL, and any blocks modified in the source will be
                                896                 :                :                  * updated after rewinding, when the source system's WAL is
                                897                 :                :                  * replayed.
                                898                 :                :                  */
                                899         [ +  + ]:          11738 :                 if (entry->target_size < entry->source_size)
                                900                 :              5 :                     return FILE_ACTION_COPY_TAIL;
                                901         [ +  + ]:          11733 :                 else if (entry->target_size > entry->source_size)
                                902                 :              4 :                     return FILE_ACTION_TRUNCATE;
                                903                 :                :                 else
                                904                 :          11729 :                     return FILE_ACTION_NONE;
                                905                 :                :             }
                                906                 :                :             break;
                                907                 :                : 
 1818 heikki.linnakangas@i      908                 :UBC           0 :         case FILE_TYPE_UNDEFINED:
                                909                 :              0 :             pg_fatal("unknown file type for \"%s\"", path);
                                910                 :                :             break;
                                911                 :                :     }
                                912                 :                : 
                                913                 :                :     /* unreachable */
                                914                 :              0 :     pg_fatal("could not decide what to do with file \"%s\"", path);
                                915                 :                : }
                                916                 :                : 
                                917                 :                : /*
                                918                 :                :  * Decide what to do with each file.
                                919                 :                :  *
                                920                 :                :  * Returns a 'filemap' with the entries in the order that their actions
                                921                 :                :  * should be executed.
                                922                 :                :  */
                                923                 :                : filemap_t *
    2 michael@paquier.xyz       924                 :GNC          15 : decide_file_actions(XLogSegNo last_common_segno)
                                925                 :                : {
                                926                 :                :     int         i;
                                927                 :                :     filehash_iterator it;
                                928                 :                :     file_entry_t *entry;
                                929                 :                :     filemap_t  *filemap;
                                930                 :                : 
 1818 heikki.linnakangas@i      931                 :CBC          15 :     filehash_start_iterate(filehash, &it);
                                932         [ +  + ]:          17702 :     while ((entry = filehash_iterate(filehash, &it)) != NULL)
                                933                 :                :     {
    2 michael@paquier.xyz       934                 :GNC       17687 :         entry->action = decide_file_action(entry, last_common_segno);
                                935                 :                :     }
                                936                 :                : 
                                937                 :                :     /*
                                938                 :                :      * Turn the hash table into an array, and sort in the order that the
                                939                 :                :      * actions should be performed.
                                940                 :                :      */
 1818 heikki.linnakangas@i      941                 :CBC          15 :     filemap = pg_malloc(offsetof(filemap_t, entries) +
                                942                 :             15 :                         filehash->members * sizeof(file_entry_t *));
                                943                 :             15 :     filemap->nentries = filehash->members;
                                944                 :             15 :     filehash_start_iterate(filehash, &it);
                                945                 :             15 :     i = 0;
                                946         [ +  + ]:          17702 :     while ((entry = filehash_iterate(filehash, &it)) != NULL)
                                947                 :                :     {
                                948                 :          17687 :         filemap->entries[i++] = entry;
                                949                 :                :     }
                                950                 :                : 
                                951                 :             15 :     qsort(&filemap->entries, filemap->nentries, sizeof(file_entry_t *),
                                952                 :                :           final_filemap_cmp);
                                953                 :                : 
                                954                 :             15 :     return filemap;
                                955                 :                : }
        

Generated by: LCOV version 2.4-beta