PostgreSQL Source Code  git master
filemap.c File Reference
#include "postgres_fe.h"
#include <sys/stat.h>
#include <unistd.h>
#include "catalog/pg_tablespace_d.h"
#include "common/file_utils.h"
#include "common/hashfn_unstable.h"
#include "common/string.h"
#include "datapagemap.h"
#include "filemap.h"
#include "pg_rewind.h"
#include "lib/simplehash.h"
Include dependency graph for filemap.c:

Go to the source code of this file.

Data Structures

struct  exclude_list_item
 

Macros

#define SH_PREFIX   filehash
 
#define SH_ELEMENT_TYPE   file_entry_t
 
#define SH_KEY_TYPE   const char *
 
#define SH_KEY   path
 
#define SH_HASH_KEY(tb, key)   hash_string(key)
 
#define SH_EQUAL(tb, a, b)   (strcmp(a, b) == 0)
 
#define SH_SCOPE   static inline
 
#define SH_RAW_ALLOCATOR   pg_malloc0
 
#define SH_DECLARE
 
#define SH_DEFINE
 
#define FILEHASH_INITIAL_SIZE   1000
 

Functions

static bool isRelDataFile (const char *path)
 
static char * datasegpath (RelFileLocator rlocator, ForkNumber forknum, BlockNumber segno)
 
static file_entry_tinsert_filehash_entry (const char *path)
 
static file_entry_tlookup_filehash_entry (const char *path)
 
static int final_filemap_cmp (const void *a, const void *b)
 
static bool check_file_excluded (const char *path, bool is_source)
 
void filehash_init (void)
 
void process_source_file (const char *path, file_type_t type, size_t size, const char *link_target)
 
void process_target_file (const char *path, file_type_t type, size_t size, const char *link_target)
 
void process_target_wal_block_change (ForkNumber forknum, RelFileLocator rlocator, BlockNumber blkno)
 
static const char * action_to_str (file_action_t action)
 
void calculate_totals (filemap_t *filemap)
 
void print_filemap (filemap_t *filemap)
 
static file_action_t decide_file_action (file_entry_t *entry)
 
filemap_tdecide_file_actions (void)
 

Variables

static filehash_hash * filehash
 
static const char *const excludeDirContents []
 
static const struct exclude_list_item excludeFiles []
 

Macro Definition Documentation

◆ FILEHASH_INITIAL_SIZE

#define FILEHASH_INITIAL_SIZE   1000

Definition at line 53 of file filemap.c.

◆ SH_DECLARE

#define SH_DECLARE

Definition at line 49 of file filemap.c.

◆ SH_DEFINE

#define SH_DEFINE

Definition at line 50 of file filemap.c.

◆ SH_ELEMENT_TYPE

#define SH_ELEMENT_TYPE   file_entry_t

Definition at line 42 of file filemap.c.

◆ SH_EQUAL

#define SH_EQUAL (   tb,
  a,
  b 
)    (strcmp(a, b) == 0)

Definition at line 46 of file filemap.c.

◆ SH_HASH_KEY

#define SH_HASH_KEY (   tb,
  key 
)    hash_string(key)

Definition at line 45 of file filemap.c.

◆ SH_KEY

#define SH_KEY   path

Definition at line 44 of file filemap.c.

◆ SH_KEY_TYPE

#define SH_KEY_TYPE   const char *

Definition at line 43 of file filemap.c.

◆ SH_PREFIX

#define SH_PREFIX   filehash

Definition at line 41 of file filemap.c.

◆ SH_RAW_ALLOCATOR

#define SH_RAW_ALLOCATOR   pg_malloc0

Definition at line 48 of file filemap.c.

◆ SH_SCOPE

#define SH_SCOPE   static inline

Definition at line 47 of file filemap.c.

Function Documentation

◆ action_to_str()

static const char* action_to_str ( file_action_t  action)
static

Definition at line 411 of file filemap.c.

412 {
413  switch (action)
414  {
415  case FILE_ACTION_NONE:
416  return "NONE";
417  case FILE_ACTION_COPY:
418  return "COPY";
420  return "TRUNCATE";
422  return "COPY_TAIL";
423  case FILE_ACTION_CREATE:
424  return "CREATE";
425  case FILE_ACTION_REMOVE:
426  return "REMOVE";
427 
428  default:
429  return "unknown";
430  }
431 }
@ FILE_ACTION_REMOVE
Definition: filemap.h:27
@ FILE_ACTION_COPY
Definition: filemap.h:21
@ FILE_ACTION_NONE
Definition: filemap.h:24
@ FILE_ACTION_COPY_TAIL
Definition: filemap.h:22
@ FILE_ACTION_TRUNCATE
Definition: filemap.h:26
@ FILE_ACTION_CREATE
Definition: filemap.h:20

References generate_unaccent_rules::action, FILE_ACTION_COPY, FILE_ACTION_COPY_TAIL, FILE_ACTION_CREATE, FILE_ACTION_NONE, FILE_ACTION_REMOVE, and FILE_ACTION_TRUNCATE.

Referenced by print_filemap().

◆ calculate_totals()

void calculate_totals ( filemap_t filemap)

Definition at line 437 of file filemap.c.

438 {
439  file_entry_t *entry;
440  int i;
441 
442  filemap->total_size = 0;
443  filemap->fetch_size = 0;
444 
445  for (i = 0; i < filemap->nentries; i++)
446  {
447  entry = filemap->entries[i];
448 
449  if (entry->source_type != FILE_TYPE_REGULAR)
450  continue;
451 
452  filemap->total_size += entry->source_size;
453 
454  if (entry->action == FILE_ACTION_COPY)
455  {
456  filemap->fetch_size += entry->source_size;
457  continue;
458  }
459 
460  if (entry->action == FILE_ACTION_COPY_TAIL)
461  filemap->fetch_size += (entry->source_size - entry->target_size);
462 
463  if (entry->target_pages_to_overwrite.bitmapsize > 0)
464  {
466  BlockNumber blk;
467 
469  while (datapagemap_next(iter, &blk))
470  filemap->fetch_size += BLCKSZ;
471 
472  pg_free(iter);
473  }
474  }
475 }
uint32 BlockNumber
Definition: block.h:31
bool datapagemap_next(datapagemap_iterator_t *iter, BlockNumber *blkno)
Definition: datapagemap.c:87
datapagemap_iterator_t * datapagemap_iterate(datapagemap_t *map)
Definition: datapagemap.c:75
void pg_free(void *ptr)
Definition: fe_memutils.c:105
@ FILE_TYPE_REGULAR
Definition: filemap.h:34
int i
Definition: isn.c:73
int bitmapsize
Definition: datapagemap.h:18
Definition: filemap.h:50
datapagemap_t target_pages_to_overwrite
Definition: filemap.h:68
size_t source_size
Definition: filemap.h:75
file_type_t source_type
Definition: filemap.h:74
size_t target_size
Definition: filemap.h:61
file_action_t action
Definition: filemap.h:81
file_entry_t * entries[FLEXIBLE_ARRAY_MEMBER]
Definition: filemap.h:96
int nentries
Definition: filemap.h:95
uint64 total_size
Definition: filemap.h:92
uint64 fetch_size
Definition: filemap.h:93

References file_entry_t::action, datapagemap::bitmapsize, datapagemap_iterate(), datapagemap_next(), filemap_t::entries, filemap_t::fetch_size, FILE_ACTION_COPY, FILE_ACTION_COPY_TAIL, FILE_TYPE_REGULAR, i, filemap_t::nentries, pg_free(), file_entry_t::source_size, file_entry_t::source_type, file_entry_t::target_pages_to_overwrite, file_entry_t::target_size, and filemap_t::total_size.

Referenced by main().

◆ check_file_excluded()

static bool check_file_excluded ( const char *  path,
bool  is_source 
)
static

Definition at line 347 of file filemap.c.

348 {
349  char localpath[MAXPGPATH];
350  int excludeIdx;
351  const char *filename;
352 
353  /*
354  * Skip all temporary files, .../pgsql_tmp/... and .../pgsql_tmp.*
355  */
356  if (strstr(path, "/" PG_TEMP_FILE_PREFIX) != NULL ||
357  strstr(path, "/" PG_TEMP_FILES_DIR "/") != NULL)
358  {
359  return true;
360  }
361 
362  /* check individual files... */
363  for (excludeIdx = 0; excludeFiles[excludeIdx].name != NULL; excludeIdx++)
364  {
365  int cmplen = strlen(excludeFiles[excludeIdx].name);
366 
368  if (filename == NULL)
369  filename = path;
370  else
371  filename++;
372 
373  if (!excludeFiles[excludeIdx].match_prefix)
374  cmplen++;
375  if (strncmp(filename, excludeFiles[excludeIdx].name, cmplen) == 0)
376  {
377  if (is_source)
378  pg_log_debug("entry \"%s\" excluded from source file list",
379  path);
380  else
381  pg_log_debug("entry \"%s\" excluded from target file list",
382  path);
383  return true;
384  }
385  }
386 
387  /*
388  * ... And check some directories. Note that this includes any contents
389  * within the directories themselves.
390  */
391  for (excludeIdx = 0; excludeDirContents[excludeIdx] != NULL; excludeIdx++)
392  {
393  snprintf(localpath, sizeof(localpath), "%s/",
394  excludeDirContents[excludeIdx]);
395  if (strstr(path, localpath) == path)
396  {
397  if (is_source)
398  pg_log_debug("entry \"%s\" excluded from source file list",
399  path);
400  else
401  pg_log_debug("entry \"%s\" excluded from target file list",
402  path);
403  return true;
404  }
405  }
406 
407  return false;
408 }
#define PG_TEMP_FILES_DIR
Definition: file_utils.h:62
#define PG_TEMP_FILE_PREFIX
Definition: file_utils.h:63
static const struct exclude_list_item excludeFiles[]
Definition: filemap.c:128
static const char *const excludeDirContents[]
Definition: filemap.c:87
#define pg_log_debug(...)
Definition: logging.h:133
#define MAXPGPATH
static char * filename
Definition: pg_dumpall.c:119
char * last_dir_separator(const char *filename)
Definition: path.c:139
#define snprintf
Definition: port.h:238
const char * name
Definition: basebackup.c:139
const char * name

References excludeDirContents, excludeFiles, filename, last_dir_separator(), exclude_list_item::match_prefix, MAXPGPATH, exclude_list_item::name, name, pg_log_debug, PG_TEMP_FILE_PREFIX, PG_TEMP_FILES_DIR, and snprintf.

Referenced by decide_file_action().

◆ datasegpath()

static char * datasegpath ( RelFileLocator  rlocator,
ForkNumber  forknum,
BlockNumber  segno 
)
static

Definition at line 591 of file filemap.c.

592 {
593  char *path;
594  char *segpath;
595 
596  path = relpathperm(rlocator, forknum);
597  if (segno > 0)
598  {
599  segpath = psprintf("%s.%u", path, segno);
600  pfree(path);
601  return segpath;
602  }
603  else
604  return path;
605 }
void pfree(void *pointer)
Definition: mcxt.c:1520
char * psprintf(const char *fmt,...)
Definition: psprintf.c:46
#define relpathperm(rlocator, forknum)
Definition: relpath.h:90

References pfree(), psprintf(), and relpathperm.

Referenced by isRelDataFile(), and process_target_wal_block_change().

◆ decide_file_action()

static file_action_t decide_file_action ( file_entry_t entry)
static

Definition at line 638 of file filemap.c.

639 {
640  const char *path = entry->path;
641 
642  /*
643  * Don't touch the control file. It is handled specially, after copying
644  * all the other files.
645  */
646  if (strcmp(path, "global/pg_control") == 0)
647  return FILE_ACTION_NONE;
648 
649  /* Skip macOS system files */
650  if (strstr(path, ".DS_Store") != NULL)
651  return FILE_ACTION_NONE;
652 
653  /*
654  * Remove all files matching the exclusion filters in the target.
655  */
656  if (check_file_excluded(path, true))
657  {
658  if (entry->target_exists)
659  return FILE_ACTION_REMOVE;
660  else
661  return FILE_ACTION_NONE;
662  }
663 
664  /*
665  * Handle cases where the file is missing from one of the systems.
666  */
667  if (!entry->target_exists && entry->source_exists)
668  {
669  /*
670  * File exists in source, but not in target. Copy it in toto. (If it's
671  * a relation data file, WAL replay after rewinding should re-create
672  * it anyway. But there's no harm in copying it now.)
673  */
674  switch (entry->source_type)
675  {
676  case FILE_TYPE_DIRECTORY:
677  case FILE_TYPE_SYMLINK:
678  return FILE_ACTION_CREATE;
679  case FILE_TYPE_REGULAR:
680  return FILE_ACTION_COPY;
681  case FILE_TYPE_UNDEFINED:
682  pg_fatal("unknown file type for \"%s\"", entry->path);
683  break;
684  }
685  }
686  else if (entry->target_exists && !entry->source_exists)
687  {
688  /* File exists in target, but not source. Remove it. */
689  return FILE_ACTION_REMOVE;
690  }
691  else if (!entry->target_exists && !entry->source_exists)
692  {
693  /*
694  * Doesn't exist in either server. Why does it have an entry in the
695  * first place??
696  */
697  Assert(false);
698  return FILE_ACTION_NONE;
699  }
700 
701  /*
702  * Otherwise, the file exists on both systems
703  */
704  Assert(entry->target_exists && entry->source_exists);
705 
706  if (entry->source_type != entry->target_type)
707  {
708  /* But it's a different kind of object. Strange.. */
709  pg_fatal("file \"%s\" is of different type in source and target", entry->path);
710  }
711 
712  /*
713  * PG_VERSION files should be identical on both systems, but avoid
714  * overwriting them for paranoia.
715  */
716  if (pg_str_endswith(entry->path, "PG_VERSION"))
717  return FILE_ACTION_NONE;
718 
719  switch (entry->source_type)
720  {
721  case FILE_TYPE_DIRECTORY:
722  return FILE_ACTION_NONE;
723 
724  case FILE_TYPE_SYMLINK:
725 
726  /*
727  * XXX: Should we check if it points to the same target?
728  */
729  return FILE_ACTION_NONE;
730 
731  case FILE_TYPE_REGULAR:
732  if (!entry->isrelfile)
733  {
734  /*
735  * It's a non-data file that we have no special processing
736  * for. Copy it in toto.
737  */
738  return FILE_ACTION_COPY;
739  }
740  else
741  {
742  /*
743  * It's a data file that exists in both systems.
744  *
745  * If it's larger in target, we can truncate it. There will
746  * also be a WAL record of the truncation in the source
747  * system, so WAL replay would eventually truncate the target
748  * too, but we might as well do it now.
749  *
750  * If it's smaller in the target, it means that it has been
751  * truncated in the target, or enlarged in the source, or
752  * both. If it was truncated in the target, we need to copy
753  * the missing tail from the source system. If it was enlarged
754  * in the source system, there will be WAL records in the
755  * source system for the new blocks, so we wouldn't need to
756  * copy them here. But we don't know which scenario we're
757  * dealing with, and there's no harm in copying the missing
758  * blocks now, so do it now.
759  *
760  * If it's the same size, do nothing here. Any blocks modified
761  * in the target will be copied based on parsing the target
762  * system's WAL, and any blocks modified in the source will be
763  * updated after rewinding, when the source system's WAL is
764  * replayed.
765  */
766  if (entry->target_size < entry->source_size)
767  return FILE_ACTION_COPY_TAIL;
768  else if (entry->target_size > entry->source_size)
769  return FILE_ACTION_TRUNCATE;
770  else
771  return FILE_ACTION_NONE;
772  }
773  break;
774 
775  case FILE_TYPE_UNDEFINED:
776  pg_fatal("unknown file type for \"%s\"", path);
777  break;
778  }
779 
780  /* unreachable */
781  pg_fatal("could not decide what to do with file \"%s\"", path);
782 }
#define Assert(condition)
Definition: c.h:858
static bool check_file_excluded(const char *path, bool is_source)
Definition: filemap.c:347
@ FILE_TYPE_UNDEFINED
Definition: filemap.h:32
@ FILE_TYPE_SYMLINK
Definition: filemap.h:36
@ FILE_TYPE_DIRECTORY
Definition: filemap.h:35
#define pg_fatal(...)
bool pg_str_endswith(const char *str, const char *end)
Definition: string.c:32
const char * path
Definition: filemap.h:53
bool source_exists
Definition: filemap.h:73
bool target_exists
Definition: filemap.h:59
file_type_t target_type
Definition: filemap.h:60
bool isrelfile
Definition: filemap.h:54

References Assert, check_file_excluded(), FILE_ACTION_COPY, FILE_ACTION_COPY_TAIL, FILE_ACTION_CREATE, FILE_ACTION_NONE, FILE_ACTION_REMOVE, FILE_ACTION_TRUNCATE, FILE_TYPE_DIRECTORY, FILE_TYPE_REGULAR, FILE_TYPE_SYMLINK, FILE_TYPE_UNDEFINED, file_entry_t::isrelfile, file_entry_t::path, pg_fatal, pg_str_endswith(), file_entry_t::source_exists, file_entry_t::source_size, file_entry_t::source_type, file_entry_t::target_exists, file_entry_t::target_size, and file_entry_t::target_type.

Referenced by decide_file_actions().

◆ decide_file_actions()

filemap_t* decide_file_actions ( void  )

Definition at line 791 of file filemap.c.

792 {
793  int i;
794  filehash_iterator it;
795  file_entry_t *entry;
796  filemap_t *filemap;
797 
798  filehash_start_iterate(filehash, &it);
799  while ((entry = filehash_iterate(filehash, &it)) != NULL)
800  {
801  entry->action = decide_file_action(entry);
802  }
803 
804  /*
805  * Turn the hash table into an array, and sort in the order that the
806  * actions should be performed.
807  */
808  filemap = pg_malloc(offsetof(filemap_t, entries) +
809  filehash->members * sizeof(file_entry_t *));
810  filemap->nentries = filehash->members;
811  filehash_start_iterate(filehash, &it);
812  i = 0;
813  while ((entry = filehash_iterate(filehash, &it)) != NULL)
814  {
815  filemap->entries[i++] = entry;
816  }
817 
818  qsort(&filemap->entries, filemap->nentries, sizeof(file_entry_t *),
820 
821  return filemap;
822 }
void * pg_malloc(size_t size)
Definition: fe_memutils.c:47
static filehash_hash * filehash
Definition: filemap.c:55
static file_action_t decide_file_action(file_entry_t *entry)
Definition: filemap.c:638
static int final_filemap_cmp(const void *a, const void *b)
Definition: filemap.c:618
#define qsort(a, b, c, d)
Definition: port.h:449

References file_entry_t::action, decide_file_action(), filemap_t::entries, filehash, final_filemap_cmp(), i, filemap_t::nentries, pg_malloc(), and qsort.

Referenced by main().

◆ filehash_init()

void filehash_init ( void  )

Definition at line 167 of file filemap.c.

168 {
169  filehash = filehash_create(FILEHASH_INITIAL_SIZE, NULL);
170 }
#define FILEHASH_INITIAL_SIZE
Definition: filemap.c:53

References filehash, and FILEHASH_INITIAL_SIZE.

Referenced by main().

◆ final_filemap_cmp()

static int final_filemap_cmp ( const void *  a,
const void *  b 
)
static

Definition at line 618 of file filemap.c.

619 {
620  file_entry_t *fa = *((file_entry_t **) a);
621  file_entry_t *fb = *((file_entry_t **) b);
622 
623  if (fa->action > fb->action)
624  return 1;
625  if (fa->action < fb->action)
626  return -1;
627 
628  if (fa->action == FILE_ACTION_REMOVE)
629  return strcmp(fb->path, fa->path);
630  else
631  return strcmp(fa->path, fb->path);
632 }
int b
Definition: isn.c:70
int a
Definition: isn.c:69
static int fa(void)
Definition: preproc-init.c:85
static int fb(int x)
Definition: preproc-init.c:92

References a, b, fa(), fb(), and FILE_ACTION_REMOVE.

Referenced by decide_file_actions().

◆ insert_filehash_entry()

static file_entry_t * insert_filehash_entry ( const char *  path)
static

Definition at line 174 of file filemap.c.

175 {
176  file_entry_t *entry;
177  bool found;
178 
179  entry = filehash_insert(filehash, path, &found);
180  if (!found)
181  {
182  entry->path = pg_strdup(path);
183  entry->isrelfile = isRelDataFile(path);
184 
185  entry->target_exists = false;
187  entry->target_size = 0;
188  entry->target_link_target = NULL;
189  entry->target_pages_to_overwrite.bitmap = NULL;
191 
192  entry->source_exists = false;
194  entry->source_size = 0;
195  entry->source_link_target = NULL;
196 
197  entry->action = FILE_ACTION_UNDECIDED;
198  }
199 
200  return entry;
201 }
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
static bool isRelDataFile(const char *path)
Definition: filemap.c:508
@ FILE_ACTION_UNDECIDED
Definition: filemap.h:18
char * bitmap
Definition: datapagemap.h:17
char * source_link_target
Definition: filemap.h:76
char * target_link_target
Definition: filemap.h:62

References file_entry_t::action, datapagemap::bitmap, datapagemap::bitmapsize, FILE_ACTION_UNDECIDED, FILE_TYPE_UNDEFINED, filehash, isRelDataFile(), file_entry_t::isrelfile, file_entry_t::path, pg_strdup(), file_entry_t::source_exists, file_entry_t::source_link_target, file_entry_t::source_size, file_entry_t::source_type, file_entry_t::target_exists, file_entry_t::target_link_target, file_entry_t::target_pages_to_overwrite, file_entry_t::target_size, and file_entry_t::target_type.

Referenced by process_source_file(), and process_target_file().

◆ isRelDataFile()

static bool isRelDataFile ( const char *  path)
static

Definition at line 508 of file filemap.c.

509 {
510  RelFileLocator rlocator;
511  unsigned int segNo;
512  int nmatch;
513  bool matched;
514 
515  /*----
516  * Relation data files can be in one of the following directories:
517  *
518  * global/
519  * shared relations
520  *
521  * base/<db oid>/
522  * regular relations, default tablespace
523  *
524  * pg_tblspc/<tblspc oid>/<tblspc version>/
525  * within a non-default tablespace (the name of the directory
526  * depends on version)
527  *
528  * And the relation data files themselves have a filename like:
529  *
530  * <oid>.<segment number>
531  *
532  *----
533  */
534  rlocator.spcOid = InvalidOid;
535  rlocator.dbOid = InvalidOid;
536  rlocator.relNumber = InvalidRelFileNumber;
537  segNo = 0;
538  matched = false;
539 
540  nmatch = sscanf(path, "global/%u.%u", &rlocator.relNumber, &segNo);
541  if (nmatch == 1 || nmatch == 2)
542  {
543  rlocator.spcOid = GLOBALTABLESPACE_OID;
544  rlocator.dbOid = 0;
545  matched = true;
546  }
547  else
548  {
549  nmatch = sscanf(path, "base/%u/%u.%u",
550  &rlocator.dbOid, &rlocator.relNumber, &segNo);
551  if (nmatch == 2 || nmatch == 3)
552  {
553  rlocator.spcOid = DEFAULTTABLESPACE_OID;
554  matched = true;
555  }
556  else
557  {
558  nmatch = sscanf(path, "pg_tblspc/%u/" TABLESPACE_VERSION_DIRECTORY "/%u/%u.%u",
559  &rlocator.spcOid, &rlocator.dbOid, &rlocator.relNumber,
560  &segNo);
561  if (nmatch == 3 || nmatch == 4)
562  matched = true;
563  }
564  }
565 
566  /*
567  * The sscanf tests above can match files that have extra characters at
568  * the end. To eliminate such cases, cross-check that GetRelationPath
569  * creates the exact same filename, when passed the RelFileLocator
570  * information we extracted from the filename.
571  */
572  if (matched)
573  {
574  char *check_path = datasegpath(rlocator, MAIN_FORKNUM, segNo);
575 
576  if (strcmp(check_path, path) != 0)
577  matched = false;
578 
579  pfree(check_path);
580  }
581 
582  return matched;
583 }
static char * datasegpath(RelFileLocator rlocator, ForkNumber forknum, BlockNumber segno)
Definition: filemap.c:591
#define InvalidOid
Definition: postgres_ext.h:36
@ MAIN_FORKNUM
Definition: relpath.h:50
#define InvalidRelFileNumber
Definition: relpath.h:26
#define TABLESPACE_VERSION_DIRECTORY
Definition: relpath.h:33
RelFileNumber relNumber

References datasegpath(), RelFileLocator::dbOid, InvalidOid, InvalidRelFileNumber, MAIN_FORKNUM, pfree(), RelFileLocator::relNumber, RelFileLocator::spcOid, and TABLESPACE_VERSION_DIRECTORY.

Referenced by insert_filehash_entry(), and process_source_file().

◆ lookup_filehash_entry()

static file_entry_t * lookup_filehash_entry ( const char *  path)
static

Definition at line 204 of file filemap.c.

205 {
206  return filehash_lookup(filehash, path);
207 }

References filehash.

Referenced by process_target_wal_block_change().

◆ print_filemap()

void print_filemap ( filemap_t filemap)

Definition at line 478 of file filemap.c.

479 {
480  file_entry_t *entry;
481  int i;
482 
483  for (i = 0; i < filemap->nentries; i++)
484  {
485  entry = filemap->entries[i];
486  if (entry->action != FILE_ACTION_NONE ||
488  {
489  pg_log_debug("%s (%s)", entry->path,
490  action_to_str(entry->action));
491 
492  if (entry->target_pages_to_overwrite.bitmapsize > 0)
494  }
495  }
496  fflush(stdout);
497 }
void datapagemap_print(datapagemap_t *map)
Definition: datapagemap.c:117
static const char * action_to_str(file_action_t action)
Definition: filemap.c:411
static void const char fflush(stdout)

References file_entry_t::action, action_to_str(), datapagemap::bitmapsize, datapagemap_print(), filemap_t::entries, fflush(), FILE_ACTION_NONE, i, filemap_t::nentries, file_entry_t::path, pg_log_debug, generate_unaccent_rules::stdout, and file_entry_t::target_pages_to_overwrite.

Referenced by main().

◆ process_source_file()

void process_source_file ( const char *  path,
file_type_t  type,
size_t  size,
const char *  link_target 
)

Definition at line 217 of file filemap.c.

219 {
220  file_entry_t *entry;
221 
222  /*
223  * Pretend that pg_wal is a directory, even if it's really a symlink. We
224  * don't want to mess with the symlink itself, nor complain if it's a
225  * symlink in source but not in target or vice versa.
226  */
227  if (strcmp(path, "pg_wal") == 0 && type == FILE_TYPE_SYMLINK)
229 
230  /*
231  * sanity check: a filename that looks like a data file better be a
232  * regular file
233  */
234  if (type != FILE_TYPE_REGULAR && isRelDataFile(path))
235  pg_fatal("data file \"%s\" in source is not a regular file", path);
236 
237  /* Remember this source file */
238  entry = insert_filehash_entry(path);
239  if (entry->source_exists)
240  pg_fatal("duplicate source file \"%s\"", path);
241  entry->source_exists = true;
242  entry->source_type = type;
243  entry->source_size = size;
244  entry->source_link_target = link_target ? pg_strdup(link_target) : NULL;
245 }
static file_entry_t * insert_filehash_entry(const char *path)
Definition: filemap.c:174
static pg_noinline void Size size
Definition: slab.c:607
const char * type

References FILE_TYPE_DIRECTORY, FILE_TYPE_REGULAR, FILE_TYPE_SYMLINK, insert_filehash_entry(), isRelDataFile(), pg_fatal, pg_strdup(), size, file_entry_t::source_exists, file_entry_t::source_link_target, file_entry_t::source_size, file_entry_t::source_type, and type.

Referenced by main().

◆ process_target_file()

void process_target_file ( const char *  path,
file_type_t  type,
size_t  size,
const char *  link_target 
)

Definition at line 253 of file filemap.c.

255 {
256  file_entry_t *entry;
257 
258  /*
259  * Do not apply any exclusion filters here. This has advantage to remove
260  * from the target data folder all paths which have been filtered out from
261  * the source data folder when processing the source files.
262  */
263 
264  /*
265  * Like in process_source_file, pretend that pg_wal is always a directory.
266  */
267  if (strcmp(path, "pg_wal") == 0 && type == FILE_TYPE_SYMLINK)
269 
270  /* Remember this target file */
271  entry = insert_filehash_entry(path);
272  if (entry->target_exists)
273  pg_fatal("duplicate source file \"%s\"", path);
274  entry->target_exists = true;
275  entry->target_type = type;
276  entry->target_size = size;
277  entry->target_link_target = link_target ? pg_strdup(link_target) : NULL;
278 }

References FILE_TYPE_DIRECTORY, FILE_TYPE_SYMLINK, insert_filehash_entry(), pg_fatal, pg_strdup(), size, file_entry_t::target_exists, file_entry_t::target_link_target, file_entry_t::target_size, file_entry_t::target_type, and type.

Referenced by main().

◆ process_target_wal_block_change()

void process_target_wal_block_change ( ForkNumber  forknum,
RelFileLocator  rlocator,
BlockNumber  blkno 
)

Definition at line 290 of file filemap.c.

292 {
293  char *path;
294  file_entry_t *entry;
295  BlockNumber blkno_inseg;
296  int segno;
297 
298  segno = blkno / RELSEG_SIZE;
299  blkno_inseg = blkno % RELSEG_SIZE;
300 
301  path = datasegpath(rlocator, forknum, segno);
302  entry = lookup_filehash_entry(path);
303  pfree(path);
304 
305  /*
306  * If the block still exists in both systems, remember it. Otherwise we
307  * can safely ignore it.
308  *
309  * If the block is beyond the EOF in the source system, or the file
310  * doesn't exist in the source at all, we're going to truncate/remove it
311  * away from the target anyway. Likewise, if it doesn't exist in the
312  * target anymore, we will copy it over with the "tail" from the source
313  * system, anyway.
314  *
315  * It is possible to find WAL for a file that doesn't exist on either
316  * system anymore. It means that the relation was dropped later in the
317  * target system, and independently on the source system too, or that it
318  * was created and dropped in the target system and it never existed in
319  * the source. Either way, we can safely ignore it.
320  */
321  if (entry)
322  {
323  Assert(entry->isrelfile);
324 
325  if (entry->target_exists)
326  {
327  if (entry->target_type != FILE_TYPE_REGULAR)
328  pg_fatal("unexpected page modification for non-regular file \"%s\"",
329  entry->path);
330 
331  if (entry->source_exists)
332  {
333  off_t end_offset;
334 
335  end_offset = (blkno_inseg + 1) * BLCKSZ;
336  if (end_offset <= entry->source_size && end_offset <= entry->target_size)
337  datapagemap_add(&entry->target_pages_to_overwrite, blkno_inseg);
338  }
339  }
340  }
341 }
void datapagemap_add(datapagemap_t *map, BlockNumber blkno)
Definition: datapagemap.c:32
static file_entry_t * lookup_filehash_entry(const char *path)
Definition: filemap.c:204

References Assert, datapagemap_add(), datasegpath(), FILE_TYPE_REGULAR, file_entry_t::isrelfile, lookup_filehash_entry(), file_entry_t::path, pfree(), pg_fatal, file_entry_t::source_exists, file_entry_t::target_exists, file_entry_t::target_pages_to_overwrite, and file_entry_t::target_type.

Referenced by extractPageInfo().

Variable Documentation

◆ excludeDirContents

const char* const excludeDirContents[]
static
Initial value:
=
{
"pg_stat_tmp",
"pg_replslot",
"pg_dynshmem",
"pg_notify",
"pg_serial",
"pg_snapshots",
"pg_subtrans",
NULL
}

Definition at line 87 of file filemap.c.

Referenced by check_file_excluded().

◆ excludeFiles

const struct exclude_list_item excludeFiles[]
static
Initial value:
=
{
{"postgresql.auto.conf.tmp", false},
{"current_logfiles.tmp", false},
{"pg_internal.init", true},
{"backup_label", false},
{"tablespace_map", false},
{"backup_manifest", false},
{"postmaster.pid", false},
{"postmaster.opts", false},
{NULL, false}
}

Definition at line 87 of file filemap.c.

Referenced by check_file_excluded().

◆ filehash

filehash_hash* filehash
static