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/string.h"
#include "datapagemap.h"
#include "filemap.h"
#include "pg_rewind.h"
#include "storage/fd.h"
Include dependency graph for filemap.c:

Go to the source code of this file.

Data Structures

struct  exclude_list_item
 

Functions

static bool isRelDataFile (const char *path)
 
static char * datasegpath (RelFileNode rnode, ForkNumber forknum, BlockNumber segno)
 
static int path_cmp (const void *a, const void *b)
 
static int final_filemap_cmp (const void *a, const void *b)
 
static void filemap_list_to_array (filemap_t *map)
 
static bool check_file_excluded (const char *path, bool is_source)
 
void filemap_create (void)
 
void process_source_file (const char *path, file_type_t type, size_t newsize, const char *link_target)
 
void process_target_file (const char *path, file_type_t type, size_t oldsize, const char *link_target)
 
void process_block_change (ForkNumber forknum, RelFileNode rnode, BlockNumber blkno)
 
void filemap_finalize (void)
 
static const char * action_to_str (file_action_t action)
 
void calculate_totals (void)
 
void print_filemap (void)
 

Variables

filemap_tfilemap = NULL
 
static const char * excludeDirContents []
 
static const struct exclude_list_item excludeFiles []
 

Function Documentation

◆ action_to_str()

static const char* action_to_str ( file_action_t  action)
static

Definition at line 599 of file filemap.c.

References FILE_ACTION_COPY, FILE_ACTION_COPY_TAIL, FILE_ACTION_CREATE, FILE_ACTION_NONE, FILE_ACTION_REMOVE, and FILE_ACTION_TRUNCATE.

Referenced by print_filemap().

600 {
601  switch (action)
602  {
603  case FILE_ACTION_NONE:
604  return "NONE";
605  case FILE_ACTION_COPY:
606  return "COPY";
608  return "TRUNCATE";
610  return "COPY_TAIL";
611  case FILE_ACTION_CREATE:
612  return "CREATE";
613  case FILE_ACTION_REMOVE:
614  return "REMOVE";
615 
616  default:
617  return "unknown";
618  }
619 }

◆ calculate_totals()

void calculate_totals ( void  )

Definition at line 625 of file filemap.c.

References file_entry_t::action, filemap_t::array, datapagemap::bitmapsize, datapagemap_iterate(), datapagemap_next(), filemap_t::fetch_size, FILE_ACTION_COPY, FILE_ACTION_COPY_TAIL, FILE_TYPE_REGULAR, filemap, i, filemap_t::narray, file_entry_t::newsize, file_entry_t::oldsize, file_entry_t::pagemap, pg_free(), filemap_t::total_size, and file_entry_t::type.

Referenced by main().

626 {
627  file_entry_t *entry;
628  int i;
629  filemap_t *map = filemap;
630 
631  map->total_size = 0;
632  map->fetch_size = 0;
633 
634  for (i = 0; i < map->narray; i++)
635  {
636  entry = map->array[i];
637 
638  if (entry->type != FILE_TYPE_REGULAR)
639  continue;
640 
641  map->total_size += entry->newsize;
642 
643  if (entry->action == FILE_ACTION_COPY)
644  {
645  map->fetch_size += entry->newsize;
646  continue;
647  }
648 
649  if (entry->action == FILE_ACTION_COPY_TAIL)
650  map->fetch_size += (entry->newsize - entry->oldsize);
651 
652  if (entry->pagemap.bitmapsize > 0)
653  {
655  BlockNumber blk;
656 
657  iter = datapagemap_iterate(&entry->pagemap);
658  while (datapagemap_next(iter, &blk))
659  map->fetch_size += BLCKSZ;
660 
661  pg_free(iter);
662  }
663  }
664 }
file_entry_t ** array
Definition: filemap.h:78
size_t newsize
Definition: filemap.h:50
uint32 BlockNumber
Definition: block.h:31
datapagemap_t pagemap
Definition: filemap.h:53
int narray
Definition: filemap.h:79
uint64 fetch_size
Definition: filemap.h:86
file_type_t type
Definition: filemap.h:44
bool datapagemap_next(datapagemap_iterator_t *iter, BlockNumber *blkno)
Definition: datapagemap.c:87
file_action_t action
Definition: filemap.h:46
filemap_t * filemap
Definition: filemap.c:23
size_t oldsize
Definition: filemap.h:49
uint64 total_size
Definition: filemap.h:85
int bitmapsize
Definition: datapagemap.h:18
datapagemap_iterator_t * datapagemap_iterate(datapagemap_t *map)
Definition: datapagemap.c:75
void pg_free(void *ptr)
Definition: fe_memutils.c:105
int i
Definition: filemap.h:41

◆ check_file_excluded()

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

Definition at line 506 of file filemap.c.

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

Referenced by process_source_file().

507 {
508  char localpath[MAXPGPATH];
509  int excludeIdx;
510  const char *filename;
511 
512  /* check individual files... */
513  for (excludeIdx = 0; excludeFiles[excludeIdx].name != NULL; excludeIdx++)
514  {
515  int cmplen = strlen(excludeFiles[excludeIdx].name);
516 
517  filename = last_dir_separator(path);
518  if (filename == NULL)
519  filename = path;
520  else
521  filename++;
522 
523  if (!excludeFiles[excludeIdx].match_prefix)
524  cmplen++;
525  if (strncmp(filename, excludeFiles[excludeIdx].name, cmplen) == 0)
526  {
527  if (is_source)
528  pg_log_debug("entry \"%s\" excluded from source file list",
529  path);
530  else
531  pg_log_debug("entry \"%s\" excluded from target file list",
532  path);
533  return true;
534  }
535  }
536 
537  /*
538  * ... And check some directories. Note that this includes any contents
539  * within the directories themselves.
540  */
541  for (excludeIdx = 0; excludeDirContents[excludeIdx] != NULL; excludeIdx++)
542  {
543  snprintf(localpath, sizeof(localpath), "%s/",
544  excludeDirContents[excludeIdx]);
545  if (strstr(path, localpath) == path)
546  {
547  if (is_source)
548  pg_log_debug("entry \"%s\" excluded from source file list",
549  path);
550  else
551  pg_log_debug("entry \"%s\" excluded from target file list",
552  path);
553  return true;
554  }
555  }
556 
557  return false;
558 }
#define pg_log_debug(...)
Definition: logging.h:91
#define MAXPGPATH
static const struct exclude_list_item excludeFiles[]
Definition: filemap.c:96
char * last_dir_separator(const char *filename)
Definition: path.c:138
const char * name
Definition: encode.c:521
const char * name
Definition: basebackup.c:143
static char * filename
Definition: pg_dumpall.c:90
static const char * excludeDirContents[]
Definition: filemap.c:54
#define snprintf
Definition: port.h:192

◆ datasegpath()

static char * datasegpath ( RelFileNode  rnode,
ForkNumber  forknum,
BlockNumber  segno 
)
static

Definition at line 781 of file filemap.c.

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

Referenced by isRelDataFile(), and process_block_change().

782 {
783  char *path;
784  char *segpath;
785 
786  path = relpathperm(rnode, forknum);
787  if (segno > 0)
788  {
789  segpath = psprintf("%s.%u", path, segno);
790  pfree(path);
791  return segpath;
792  }
793  else
794  return path;
795 }
#define relpathperm(rnode, forknum)
Definition: relpath.h:83
char * psprintf(const char *fmt,...)
Definition: psprintf.c:46
void pfree(void *pointer)
Definition: mcxt.c:1056

◆ filemap_create()

void filemap_create ( void  )

Definition at line 127 of file filemap.c.

References filemap_t::array, Assert, filemap_t::first, filemap_t::last, filemap_t::narray, filemap_t::nlist, and pg_malloc().

Referenced by main().

128 {
129  filemap_t *map;
130 
131  map = pg_malloc(sizeof(filemap_t));
132  map->first = map->last = NULL;
133  map->nlist = 0;
134  map->array = NULL;
135  map->narray = 0;
136 
137  Assert(filemap == NULL);
138  filemap = map;
139 }
file_entry_t * first
Definition: filemap.h:67
file_entry_t ** array
Definition: filemap.h:78
void * pg_malloc(size_t size)
Definition: fe_memutils.c:47
int narray
Definition: filemap.h:79
int nlist
Definition: filemap.h:69
filemap_t * filemap
Definition: filemap.c:23
#define Assert(condition)
Definition: c.h:738
file_entry_t * last
Definition: filemap.h:68

◆ filemap_finalize()

void filemap_finalize ( void  )

Definition at line 589 of file filemap.c.

References filemap_t::array, filemap, filemap_list_to_array(), final_filemap_cmp(), filemap_t::narray, and qsort.

Referenced by main().

590 {
591  filemap_t *map = filemap;
592 
594  qsort(map->array, map->narray, sizeof(file_entry_t *),
596 }
static int final_filemap_cmp(const void *a, const void *b)
Definition: filemap.c:817
file_entry_t ** array
Definition: filemap.h:78
int narray
Definition: filemap.h:79
filemap_t * filemap
Definition: filemap.c:23
static void filemap_list_to_array(filemap_t *map)
Definition: filemap.c:565
Definition: filemap.h:41
#define qsort(a, b, c, d)
Definition: port.h:478

◆ filemap_list_to_array()

static void filemap_list_to_array ( filemap_t map)
static

Definition at line 565 of file filemap.c.

References filemap_t::array, Assert, filemap_t::first, filemap_t::last, filemap_t::narray, file_entry_t::next, next, filemap_t::nlist, and pg_realloc().

Referenced by filemap_finalize(), and process_target_file().

566 {
567  int narray;
568  file_entry_t *entry,
569  *next;
570 
571  map->array = (file_entry_t **)
572  pg_realloc(map->array,
573  (map->nlist + map->narray) * sizeof(file_entry_t *));
574 
575  narray = map->narray;
576  for (entry = map->first; entry != NULL; entry = next)
577  {
578  map->array[narray++] = entry;
579  next = entry->next;
580  entry->next = NULL;
581  }
582  Assert(narray == map->nlist + map->narray);
583  map->narray = narray;
584  map->nlist = 0;
585  map->first = map->last = NULL;
586 }
static int32 next
Definition: blutils.c:218
file_entry_t * first
Definition: filemap.h:67
file_entry_t ** array
Definition: filemap.h:78
int narray
Definition: filemap.h:79
int nlist
Definition: filemap.h:69
struct file_entry_t * next
Definition: filemap.h:58
void * pg_realloc(void *ptr, size_t size)
Definition: fe_memutils.c:65
#define Assert(condition)
Definition: c.h:738
file_entry_t * last
Definition: filemap.h:68
Definition: filemap.h:41

◆ final_filemap_cmp()

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

Definition at line 817 of file filemap.c.

References file_entry_t::action, fa(), fb(), FILE_ACTION_REMOVE, and file_entry_t::path.

Referenced by filemap_finalize().

818 {
819  file_entry_t *fa = *((file_entry_t **) a);
820  file_entry_t *fb = *((file_entry_t **) b);
821 
822  if (fa->action > fb->action)
823  return 1;
824  if (fa->action < fb->action)
825  return -1;
826 
827  if (fa->action == FILE_ACTION_REMOVE)
828  return strcmp(fb->path, fa->path);
829  else
830  return strcmp(fa->path, fb->path);
831 }
static int fa(void)
Definition: preproc-init.c:85
static int fb(int x)
Definition: preproc-init.c:92
file_action_t action
Definition: filemap.h:46
char * path
Definition: filemap.h:43
Definition: filemap.h:41

◆ isRelDataFile()

static bool isRelDataFile ( const char *  path)
static

Definition at line 698 of file filemap.c.

References datasegpath(), RelFileNode::dbNode, InvalidOid, MAIN_FORKNUM, pfree(), RelFileNode::relNode, RelFileNode::spcNode, and TABLESPACE_VERSION_DIRECTORY.

Referenced by process_source_file(), and process_target_file().

699 {
700  RelFileNode rnode;
701  unsigned int segNo;
702  int nmatch;
703  bool matched;
704 
705  /*----
706  * Relation data files can be in one of the following directories:
707  *
708  * global/
709  * shared relations
710  *
711  * base/<db oid>/
712  * regular relations, default tablespace
713  *
714  * pg_tblspc/<tblspc oid>/<tblspc version>/
715  * within a non-default tablespace (the name of the directory
716  * depends on version)
717  *
718  * And the relation data files themselves have a filename like:
719  *
720  * <oid>.<segment number>
721  *
722  *----
723  */
724  rnode.spcNode = InvalidOid;
725  rnode.dbNode = InvalidOid;
726  rnode.relNode = InvalidOid;
727  segNo = 0;
728  matched = false;
729 
730  nmatch = sscanf(path, "global/%u.%u", &rnode.relNode, &segNo);
731  if (nmatch == 1 || nmatch == 2)
732  {
733  rnode.spcNode = GLOBALTABLESPACE_OID;
734  rnode.dbNode = 0;
735  matched = true;
736  }
737  else
738  {
739  nmatch = sscanf(path, "base/%u/%u.%u",
740  &rnode.dbNode, &rnode.relNode, &segNo);
741  if (nmatch == 2 || nmatch == 3)
742  {
743  rnode.spcNode = DEFAULTTABLESPACE_OID;
744  matched = true;
745  }
746  else
747  {
748  nmatch = sscanf(path, "pg_tblspc/%u/" TABLESPACE_VERSION_DIRECTORY "/%u/%u.%u",
749  &rnode.spcNode, &rnode.dbNode, &rnode.relNode,
750  &segNo);
751  if (nmatch == 3 || nmatch == 4)
752  matched = true;
753  }
754  }
755 
756  /*
757  * The sscanf tests above can match files that have extra characters at
758  * the end. To eliminate such cases, cross-check that GetRelationPath
759  * creates the exact same filename, when passed the RelFileNode
760  * information we extracted from the filename.
761  */
762  if (matched)
763  {
764  char *check_path = datasegpath(rnode, MAIN_FORKNUM, segNo);
765 
766  if (strcmp(check_path, path) != 0)
767  matched = false;
768 
769  pfree(check_path);
770  }
771 
772  return matched;
773 }
static char * datasegpath(RelFileNode rnode, ForkNumber forknum, BlockNumber segno)
Definition: filemap.c:781
void pfree(void *pointer)
Definition: mcxt.c:1056
#define TABLESPACE_VERSION_DIRECTORY
Definition: relpath.h:26
#define InvalidOid
Definition: postgres_ext.h:36

◆ path_cmp()

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

Definition at line 798 of file filemap.c.

References fa(), fb(), and file_entry_t::path.

Referenced by process_block_change(), and process_target_file().

799 {
800  file_entry_t *fa = *((file_entry_t **) a);
801  file_entry_t *fb = *((file_entry_t **) b);
802 
803  return strcmp(fa->path, fb->path);
804 }
static int fa(void)
Definition: preproc-init.c:85
static int fb(int x)
Definition: preproc-init.c:92
char * path
Definition: filemap.h:43
Definition: filemap.h:41

◆ print_filemap()

void print_filemap ( void  )

Definition at line 667 of file filemap.c.

References file_entry_t::action, action_to_str(), filemap_t::array, datapagemap::bitmapsize, datapagemap_print(), FILE_ACTION_NONE, filemap, i, filemap_t::narray, file_entry_t::pagemap, file_entry_t::path, pg_log_debug, and generate_unaccent_rules::stdout.

Referenced by main().

668 {
669  filemap_t *map = filemap;
670  file_entry_t *entry;
671  int i;
672 
673  for (i = 0; i < map->narray; i++)
674  {
675  entry = map->array[i];
676  if (entry->action != FILE_ACTION_NONE ||
677  entry->pagemap.bitmapsize > 0)
678  {
679  pg_log_debug("%s (%s)", entry->path,
680  action_to_str(entry->action));
681 
682  if (entry->pagemap.bitmapsize > 0)
683  datapagemap_print(&entry->pagemap);
684  }
685  }
686  fflush(stdout);
687 }
file_entry_t ** array
Definition: filemap.h:78
datapagemap_t pagemap
Definition: filemap.h:53
int narray
Definition: filemap.h:79
#define pg_log_debug(...)
Definition: logging.h:91
file_action_t action
Definition: filemap.h:46
filemap_t * filemap
Definition: filemap.c:23
static const char * action_to_str(file_action_t action)
Definition: filemap.c:599
void datapagemap_print(datapagemap_t *map)
Definition: datapagemap.c:117
int bitmapsize
Definition: datapagemap.h:18
char * path
Definition: filemap.h:43
int i
Definition: filemap.h:41

◆ process_block_change()

void process_block_change ( ForkNumber  forknum,
RelFileNode  rnode,
BlockNumber  blkno 
)

Definition at line 431 of file filemap.c.

References file_entry_t::action, filemap_t::array, Assert, datapagemap_add(), datasegpath(), FILE_ACTION_COPY, FILE_ACTION_COPY_TAIL, FILE_ACTION_CREATE, FILE_ACTION_NONE, FILE_ACTION_REMOVE, FILE_ACTION_TRUNCATE, filemap, file_entry_t::isrelfile, sort-test::key, filemap_t::narray, file_entry_t::pagemap, file_entry_t::path, path_cmp(), pfree(), and pg_fatal.

Referenced by extractPageInfo().

432 {
433  char *path;
435  file_entry_t *key_ptr;
436  file_entry_t *entry;
437  BlockNumber blkno_inseg;
438  int segno;
439  filemap_t *map = filemap;
440  file_entry_t **e;
441 
442  Assert(map->array);
443 
444  segno = blkno / RELSEG_SIZE;
445  blkno_inseg = blkno % RELSEG_SIZE;
446 
447  path = datasegpath(rnode, forknum, segno);
448 
449  key.path = (char *) path;
450  key_ptr = &key;
451 
452  e = bsearch(&key_ptr, map->array, map->narray, sizeof(file_entry_t *),
453  path_cmp);
454  if (e)
455  entry = *e;
456  else
457  entry = NULL;
458  pfree(path);
459 
460  if (entry)
461  {
462  Assert(entry->isrelfile);
463 
464  switch (entry->action)
465  {
466  case FILE_ACTION_NONE:
468  /* skip if we're truncating away the modified block anyway */
469  if ((blkno_inseg + 1) * BLCKSZ <= entry->newsize)
470  datapagemap_add(&entry->pagemap, blkno_inseg);
471  break;
472 
474 
475  /*
476  * skip the modified block if it is part of the "tail" that
477  * we're copying anyway.
478  */
479  if ((blkno_inseg + 1) * BLCKSZ <= entry->oldsize)
480  datapagemap_add(&entry->pagemap, blkno_inseg);
481  break;
482 
483  case FILE_ACTION_COPY:
484  case FILE_ACTION_REMOVE:
485  break;
486 
487  case FILE_ACTION_CREATE:
488  pg_fatal("unexpected page modification for directory or symbolic link \"%s\"", entry->path);
489  }
490  }
491  else
492  {
493  /*
494  * If we don't have any record of this file in the file map, it means
495  * that it's a relation that doesn't exist in the source system, and
496  * it was subsequently removed in the target system, too. We can
497  * safely ignore it.
498  */
499  }
500 }
void datapagemap_add(datapagemap_t *map, BlockNumber blkno)
Definition: datapagemap.c:32
file_entry_t ** array
Definition: filemap.h:78
#define pg_fatal(...)
Definition: pg_rewind.h:41
uint32 BlockNumber
Definition: block.h:31
datapagemap_t pagemap
Definition: filemap.h:53
int narray
Definition: filemap.h:79
static char * datasegpath(RelFileNode rnode, ForkNumber forknum, BlockNumber segno)
Definition: filemap.c:781
bool isrelfile
Definition: filemap.h:51
void pfree(void *pointer)
Definition: mcxt.c:1056
file_action_t action
Definition: filemap.h:46
filemap_t * filemap
Definition: filemap.c:23
#define Assert(condition)
Definition: c.h:738
char * path
Definition: filemap.h:43
e
Definition: preproc-init.c:82
Definition: filemap.h:41
static int path_cmp(const void *a, const void *b)
Definition: filemap.c:798

◆ process_source_file()

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

Definition at line 149 of file filemap.c.

References file_entry_t::action, generate_unaccent_rules::action, Assert, datapagemap::bitmap, datapagemap::bitmapsize, check_file_excluded(), datadir_target, FILE_ACTION_COPY, FILE_ACTION_COPY_TAIL, FILE_ACTION_CREATE, FILE_ACTION_NONE, FILE_ACTION_TRUNCATE, FILE_TYPE_DIRECTORY, FILE_TYPE_REGULAR, FILE_TYPE_SYMLINK, filemap, isRelDataFile(), file_entry_t::isrelfile, file_entry_t::link_target, lstat, MAXPGPATH, file_entry_t::newsize, file_entry_t::next, file_entry_t::oldsize, file_entry_t::pagemap, file_entry_t::path, pg_fatal, pg_malloc(), pg_str_endswith(), pg_strdup(), PG_TEMP_FILE_PREFIX, PG_TEMP_FILES_DIR, pgwin32_is_junction(), S_ISDIR, S_ISREG, snprintf, stat, file_entry_t::type, and generate_unaccent_rules::type.

Referenced by fetchSourceFileList(), and libpqProcessFileList().

151 {
152  bool exists;
153  char localpath[MAXPGPATH];
154  struct stat statbuf;
155  filemap_t *map = filemap;
157  size_t oldsize = 0;
158  file_entry_t *entry;
159 
160  Assert(map->array == NULL);
161 
162  /*
163  * Skip any files matching the exclusion filters. This has the effect to
164  * remove all those files on the target.
165  */
166  if (check_file_excluded(path, true))
167  return;
168 
169  /*
170  * Pretend that pg_wal is a directory, even if it's really a symlink. We
171  * don't want to mess with the symlink itself, nor complain if it's a
172  * symlink in source but not in target or vice versa.
173  */
174  if (strcmp(path, "pg_wal") == 0 && type == FILE_TYPE_SYMLINK)
176 
177  /*
178  * Skip temporary files, .../pgsql_tmp/... and .../pgsql_tmp.* in source.
179  * This has the effect that all temporary files in the destination will be
180  * removed.
181  */
182  if (strstr(path, "/" PG_TEMP_FILE_PREFIX) != NULL)
183  return;
184  if (strstr(path, "/" PG_TEMP_FILES_DIR "/") != NULL)
185  return;
186 
187  /*
188  * sanity check: a filename that looks like a data file better be a
189  * regular file
190  */
191  if (type != FILE_TYPE_REGULAR && isRelDataFile(path))
192  pg_fatal("data file \"%s\" in source is not a regular file", path);
193 
194  snprintf(localpath, sizeof(localpath), "%s/%s", datadir_target, path);
195 
196  /* Does the corresponding file exist in the target data dir? */
197  if (lstat(localpath, &statbuf) < 0)
198  {
199  if (errno != ENOENT)
200  pg_fatal("could not stat file \"%s\": %m",
201  localpath);
202 
203  exists = false;
204  }
205  else
206  exists = true;
207 
208  switch (type)
209  {
210  case FILE_TYPE_DIRECTORY:
211  if (exists && !S_ISDIR(statbuf.st_mode) && strcmp(path, "pg_wal") != 0)
212  {
213  /* it's a directory in source, but not in target. Strange.. */
214  pg_fatal("\"%s\" is not a directory", localpath);
215  }
216 
217  if (!exists)
218  action = FILE_ACTION_CREATE;
219  else
220  action = FILE_ACTION_NONE;
221  oldsize = 0;
222  break;
223 
224  case FILE_TYPE_SYMLINK:
225  if (exists &&
226 #ifndef WIN32
227  !S_ISLNK(statbuf.st_mode)
228 #else
229  !pgwin32_is_junction(localpath)
230 #endif
231  )
232  {
233  /*
234  * It's a symbolic link in source, but not in target.
235  * Strange..
236  */
237  pg_fatal("\"%s\" is not a symbolic link", localpath);
238  }
239 
240  if (!exists)
241  action = FILE_ACTION_CREATE;
242  else
243  action = FILE_ACTION_NONE;
244  oldsize = 0;
245  break;
246 
247  case FILE_TYPE_REGULAR:
248  if (exists && !S_ISREG(statbuf.st_mode))
249  pg_fatal("\"%s\" is not a regular file", localpath);
250 
251  if (!exists || !isRelDataFile(path))
252  {
253  /*
254  * File exists in source, but not in target. Or it's a
255  * non-data file that we have no special processing for. Copy
256  * it in toto.
257  *
258  * An exception: PG_VERSIONs should be identical, but avoid
259  * overwriting it for paranoia.
260  */
261  if (pg_str_endswith(path, "PG_VERSION"))
262  {
263  action = FILE_ACTION_NONE;
264  oldsize = statbuf.st_size;
265  }
266  else
267  {
268  action = FILE_ACTION_COPY;
269  oldsize = 0;
270  }
271  }
272  else
273  {
274  /*
275  * It's a data file that exists in both.
276  *
277  * If it's larger in target, we can truncate it. There will
278  * also be a WAL record of the truncation in the source
279  * system, so WAL replay would eventually truncate the target
280  * too, but we might as well do it now.
281  *
282  * If it's smaller in the target, it means that it has been
283  * truncated in the target, or enlarged in the source, or
284  * both. If it was truncated in the target, we need to copy
285  * the missing tail from the source system. If it was enlarged
286  * in the source system, there will be WAL records in the
287  * source system for the new blocks, so we wouldn't need to
288  * copy them here. But we don't know which scenario we're
289  * dealing with, and there's no harm in copying the missing
290  * blocks now, so do it now.
291  *
292  * If it's the same size, do nothing here. Any blocks modified
293  * in the target will be copied based on parsing the target
294  * system's WAL, and any blocks modified in the source will be
295  * updated after rewinding, when the source system's WAL is
296  * replayed.
297  */
298  oldsize = statbuf.st_size;
299  if (oldsize < newsize)
300  action = FILE_ACTION_COPY_TAIL;
301  else if (oldsize > newsize)
302  action = FILE_ACTION_TRUNCATE;
303  else
304  action = FILE_ACTION_NONE;
305  }
306  break;
307  }
308 
309  /* Create a new entry for this file */
310  entry = pg_malloc(sizeof(file_entry_t));
311  entry->path = pg_strdup(path);
312  entry->type = type;
313  entry->action = action;
314  entry->oldsize = oldsize;
315  entry->newsize = newsize;
316  entry->link_target = link_target ? pg_strdup(link_target) : NULL;
317  entry->next = NULL;
318  entry->pagemap.bitmap = NULL;
319  entry->pagemap.bitmapsize = 0;
320  entry->isrelfile = isRelDataFile(path);
321 
322  if (map->last)
323  {
324  map->last->next = entry;
325  map->last = entry;
326  }
327  else
328  map->first = map->last = entry;
329  map->nlist++;
330 }
char * datadir_target
Definition: pg_rewind.c:53
bool pg_str_endswith(const char *str, const char *end)
Definition: string.c:31
void * pg_malloc(size_t size)
Definition: fe_memutils.c:47
size_t newsize
Definition: filemap.h:50
#define PG_TEMP_FILES_DIR
Definition: pg_checksums.c:58
#define pg_fatal(...)
Definition: pg_rewind.h:41
char * bitmap
Definition: datapagemap.h:17
static bool check_file_excluded(const char *path, bool is_source)
Definition: filemap.c:506
datapagemap_t pagemap
Definition: filemap.h:53
file_type_t type
Definition: filemap.h:44
bool isrelfile
Definition: filemap.h:51
#define PG_TEMP_FILE_PREFIX
Definition: pg_checksums.c:59
#define MAXPGPATH
file_action_t action
Definition: filemap.h:46
char * link_target
Definition: filemap.h:56
struct file_entry_t * next
Definition: filemap.h:58
filemap_t * filemap
Definition: filemap.c:23
size_t oldsize
Definition: filemap.h:49
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
#define S_ISREG(m)
Definition: win32_port.h:299
#define stat(a, b)
Definition: win32_port.h:255
int bitmapsize
Definition: datapagemap.h:18
#define Assert(condition)
Definition: c.h:738
file_action_t
Definition: filemap.h:23
static bool isRelDataFile(const char *path)
Definition: filemap.c:698
char * path
Definition: filemap.h:43
#define S_ISDIR(m)
Definition: win32_port.h:296
#define lstat(path, sb)
Definition: win32_port.h:244
Definition: filemap.h:41
#define snprintf
Definition: port.h:192
bool pgwin32_is_junction(const char *path)

◆ process_target_file()

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

Definition at line 340 of file filemap.c.

References file_entry_t::action, filemap_t::array, Assert, datapagemap::bitmap, datapagemap::bitmapsize, datadir_target, FILE_ACTION_REMOVE, FILE_TYPE_DIRECTORY, FILE_TYPE_SYMLINK, filemap, filemap_list_to_array(), filemap_t::first, isRelDataFile(), file_entry_t::isrelfile, sort-test::key, filemap_t::last, file_entry_t::link_target, lstat, MAXPGPATH, filemap_t::narray, file_entry_t::newsize, file_entry_t::next, filemap_t::nlist, file_entry_t::oldsize, file_entry_t::pagemap, file_entry_t::path, path_cmp(), pg_fatal, pg_malloc(), pg_strdup(), qsort, snprintf, stat, file_entry_t::type, and generate_unaccent_rules::type.

Referenced by main().

342 {
343  bool exists;
344  char localpath[MAXPGPATH];
345  struct stat statbuf;
347  file_entry_t *key_ptr;
348  filemap_t *map = filemap;
349  file_entry_t *entry;
350 
351  /*
352  * Do not apply any exclusion filters here. This has advantage to remove
353  * from the target data folder all paths which have been filtered out from
354  * the source data folder when processing the source files.
355  */
356 
357  snprintf(localpath, sizeof(localpath), "%s/%s", datadir_target, path);
358  if (lstat(localpath, &statbuf) < 0)
359  {
360  if (errno != ENOENT)
361  pg_fatal("could not stat file \"%s\": %m",
362  localpath);
363 
364  exists = false;
365  }
366 
367  if (map->array == NULL)
368  {
369  /* on first call, initialize lookup array */
370  if (map->nlist == 0)
371  {
372  /* should not happen */
373  pg_fatal("source file list is empty");
374  }
375 
377 
378  Assert(map->array != NULL);
379 
380  qsort(map->array, map->narray, sizeof(file_entry_t *), path_cmp);
381  }
382 
383  /*
384  * Like in process_source_file, pretend that xlog is always a directory.
385  */
386  if (strcmp(path, "pg_wal") == 0 && type == FILE_TYPE_SYMLINK)
388 
389  key.path = (char *) path;
390  key_ptr = &key;
391  exists = (bsearch(&key_ptr, map->array, map->narray, sizeof(file_entry_t *),
392  path_cmp) != NULL);
393 
394  /* Remove any file or folder that doesn't exist in the source system. */
395  if (!exists)
396  {
397  entry = pg_malloc(sizeof(file_entry_t));
398  entry->path = pg_strdup(path);
399  entry->type = type;
400  entry->action = FILE_ACTION_REMOVE;
401  entry->oldsize = oldsize;
402  entry->newsize = 0;
403  entry->link_target = link_target ? pg_strdup(link_target) : NULL;
404  entry->next = NULL;
405  entry->pagemap.bitmap = NULL;
406  entry->pagemap.bitmapsize = 0;
407  entry->isrelfile = isRelDataFile(path);
408 
409  if (map->last == NULL)
410  map->first = entry;
411  else
412  map->last->next = entry;
413  map->last = entry;
414  map->nlist++;
415  }
416  else
417  {
418  /*
419  * We already handled all files that exist in the source system in
420  * process_source_file().
421  */
422  }
423 }
char * datadir_target
Definition: pg_rewind.c:53
file_entry_t * first
Definition: filemap.h:67
file_entry_t ** array
Definition: filemap.h:78
void * pg_malloc(size_t size)
Definition: fe_memutils.c:47
size_t newsize
Definition: filemap.h:50
#define pg_fatal(...)
Definition: pg_rewind.h:41
char * bitmap
Definition: datapagemap.h:17
datapagemap_t pagemap
Definition: filemap.h:53
int narray
Definition: filemap.h:79
file_type_t type
Definition: filemap.h:44
bool isrelfile
Definition: filemap.h:51
int nlist
Definition: filemap.h:69
#define MAXPGPATH
file_action_t action
Definition: filemap.h:46
char * link_target
Definition: filemap.h:56
struct file_entry_t * next
Definition: filemap.h:58
filemap_t * filemap
Definition: filemap.c:23
size_t oldsize
Definition: filemap.h:49
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
#define stat(a, b)
Definition: win32_port.h:255
int bitmapsize
Definition: datapagemap.h:18
#define Assert(condition)
Definition: c.h:738
static bool isRelDataFile(const char *path)
Definition: filemap.c:698
char * path
Definition: filemap.h:43
static void filemap_list_to_array(filemap_t *map)
Definition: filemap.c:565
#define lstat(path, sb)
Definition: win32_port.h:244
file_entry_t * last
Definition: filemap.h:68
Definition: filemap.h:41
static int path_cmp(const void *a, const void *b)
Definition: filemap.c:798
#define qsort(a, b, c, d)
Definition: port.h:478
#define snprintf
Definition: port.h:192

Variable Documentation

◆ excludeDirContents

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

Definition at line 54 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},
{"postmaster.pid", false},
{"postmaster.opts", false},
{NULL, false}
}

Definition at line 96 of file filemap.c.

◆ filemap