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

Go to the source code of this file.

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, const char *type)
 
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 char * excludeFiles []
 

Function Documentation

◆ action_to_str()

static const char* action_to_str ( file_action_t  action)
static

Definition at line 573 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().

574 {
575  switch (action)
576  {
577  case FILE_ACTION_NONE:
578  return "NONE";
579  case FILE_ACTION_COPY:
580  return "COPY";
582  return "TRUNCATE";
584  return "COPY_TAIL";
585  case FILE_ACTION_CREATE:
586  return "CREATE";
587  case FILE_ACTION_REMOVE:
588  return "REMOVE";
589 
590  default:
591  return "unknown";
592  }
593 }

◆ calculate_totals()

void calculate_totals ( void  )

Definition at line 599 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().

600 {
601  file_entry_t *entry;
602  int i;
603  filemap_t *map = filemap;
604 
605  map->total_size = 0;
606  map->fetch_size = 0;
607 
608  for (i = 0; i < map->narray; i++)
609  {
610  entry = map->array[i];
611 
612  if (entry->type != FILE_TYPE_REGULAR)
613  continue;
614 
615  map->total_size += entry->newsize;
616 
617  if (entry->action == FILE_ACTION_COPY)
618  {
619  map->fetch_size += entry->newsize;
620  continue;
621  }
622 
623  if (entry->action == FILE_ACTION_COPY_TAIL)
624  map->fetch_size += (entry->newsize - entry->oldsize);
625 
626  if (entry->pagemap.bitmapsize > 0)
627  {
629  BlockNumber blk;
630 
631  iter = datapagemap_iterate(&entry->pagemap);
632  while (datapagemap_next(iter, &blk))
633  map->fetch_size += BLCKSZ;
634 
635  pg_free(iter);
636  }
637  }
638 }
file_entry_t ** array
Definition: filemap.h:79
size_t newsize
Definition: filemap.h:51
uint32 BlockNumber
Definition: block.h:31
datapagemap_t pagemap
Definition: filemap.h:54
int narray
Definition: filemap.h:80
uint64 fetch_size
Definition: filemap.h:87
file_type_t type
Definition: filemap.h:45
bool datapagemap_next(datapagemap_iterator_t *iter, BlockNumber *blkno)
Definition: datapagemap.c:87
file_action_t action
Definition: filemap.h:47
filemap_t * filemap
Definition: filemap.c:25
size_t oldsize
Definition: filemap.h:50
uint64 total_size
Definition: filemap.h:86
int bitmapsize
Definition: datapagemap.h:19
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:42

◆ check_file_excluded()

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

Definition at line 493 of file filemap.c.

References excludeDirContents, excludeFiles, filename, last_dir_separator(), MAXPGPATH, PG_DEBUG, pg_log(), and snprintf().

Referenced by process_source_file(), and process_target_file().

494 {
495  char localpath[MAXPGPATH];
496  int excludeIdx;
497  const char *filename;
498 
499  /* check individual files... */
500  for (excludeIdx = 0; excludeFiles[excludeIdx] != NULL; excludeIdx++)
501  {
502  filename = last_dir_separator(path);
503  if (filename == NULL)
504  filename = path;
505  else
506  filename++;
507  if (strcmp(filename, excludeFiles[excludeIdx]) == 0)
508  {
509  pg_log(PG_DEBUG, "entry \"%s\" excluded from %s file list\n",
510  path, type);
511  return true;
512  }
513  }
514 
515  /*
516  * ... And check some directories. Note that this includes any contents
517  * within the directories themselves.
518  */
519  for (excludeIdx = 0; excludeDirContents[excludeIdx] != NULL; excludeIdx++)
520  {
521  snprintf(localpath, sizeof(localpath), "%s/",
522  excludeDirContents[excludeIdx]);
523  if (strstr(path, localpath) == path)
524  {
525  pg_log(PG_DEBUG, "entry \"%s\" excluded from %s file list\n",
526  path, type);
527  return true;
528  }
529  }
530 
531  return false;
532 }
int snprintf(char *str, size_t count, const char *fmt,...) pg_attribute_printf(3
#define MAXPGPATH
void pg_log(eLogType type, const char *fmt,...)
Definition: logging.c:69
static const char * excludeFiles[]
Definition: filemap.c:85
char * last_dir_separator(const char *filename)
Definition: path.c:138
static char * filename
Definition: pg_dumpall.c:87
static const char * excludeDirContents[]
Definition: filemap.c:44

◆ datasegpath()

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

Definition at line 758 of file filemap.c.

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

Referenced by isRelDataFile(), and process_block_change().

759 {
760  char *path;
761  char *segpath;
762 
763  path = relpathperm(rnode, forknum);
764  if (segno > 0)
765  {
766  segpath = psprintf("%s.%u", path, segno);
767  pfree(path);
768  return segpath;
769  }
770  else
771  return path;
772 }
#define relpathperm(rnode, forknum)
Definition: relpath.h:83
char * psprintf(const char *fmt,...)
Definition: psprintf.c:46
void pfree(void *pointer)
Definition: mcxt.c:1031

◆ filemap_create()

void filemap_create ( void  )

Definition at line 115 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().

116 {
117  filemap_t *map;
118 
119  map = pg_malloc(sizeof(filemap_t));
120  map->first = map->last = NULL;
121  map->nlist = 0;
122  map->array = NULL;
123  map->narray = 0;
124 
125  Assert(filemap == NULL);
126  filemap = map;
127 }
file_entry_t * first
Definition: filemap.h:68
file_entry_t ** array
Definition: filemap.h:79
void * pg_malloc(size_t size)
Definition: fe_memutils.c:47
int narray
Definition: filemap.h:80
int nlist
Definition: filemap.h:70
filemap_t * filemap
Definition: filemap.c:25
#define Assert(condition)
Definition: c.h:699
file_entry_t * last
Definition: filemap.h:69

◆ filemap_finalize()

void filemap_finalize ( void  )

Definition at line 563 of file filemap.c.

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

Referenced by main().

564 {
565  filemap_t *map = filemap;
566 
568  qsort(map->array, map->narray, sizeof(file_entry_t *),
570 }
static int final_filemap_cmp(const void *a, const void *b)
Definition: filemap.c:794
file_entry_t ** array
Definition: filemap.h:79
int narray
Definition: filemap.h:80
filemap_t * filemap
Definition: filemap.c:25
static void filemap_list_to_array(filemap_t *map)
Definition: filemap.c:539
Definition: filemap.h:42
#define qsort(a, b, c, d)
Definition: port.h:421

◆ filemap_list_to_array()

static void filemap_list_to_array ( filemap_t map)
static

Definition at line 539 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().

540 {
541  int narray;
542  file_entry_t *entry,
543  *next;
544 
545  map->array = (file_entry_t **)
546  pg_realloc(map->array,
547  (map->nlist + map->narray) * sizeof(file_entry_t *));
548 
549  narray = map->narray;
550  for (entry = map->first; entry != NULL; entry = next)
551  {
552  map->array[narray++] = entry;
553  next = entry->next;
554  entry->next = NULL;
555  }
556  Assert(narray == map->nlist + map->narray);
557  map->narray = narray;
558  map->nlist = 0;
559  map->first = map->last = NULL;
560 }
static int32 next
Definition: blutils.c:211
file_entry_t * first
Definition: filemap.h:68
file_entry_t ** array
Definition: filemap.h:79
int narray
Definition: filemap.h:80
int nlist
Definition: filemap.h:70
struct file_entry_t * next
Definition: filemap.h:59
void * pg_realloc(void *ptr, size_t size)
Definition: fe_memutils.c:65
#define Assert(condition)
Definition: c.h:699
file_entry_t * last
Definition: filemap.h:69
Definition: filemap.h:42

◆ final_filemap_cmp()

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

Definition at line 794 of file filemap.c.

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

Referenced by filemap_finalize().

795 {
796  file_entry_t *fa = *((file_entry_t **) a);
797  file_entry_t *fb = *((file_entry_t **) b);
798 
799  if (fa->action > fb->action)
800  return 1;
801  if (fa->action < fb->action)
802  return -1;
803 
804  if (fa->action == FILE_ACTION_REMOVE)
805  return -strcmp(fa->path, fb->path);
806  else
807  return strcmp(fa->path, fb->path);
808 }
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:47
char * path
Definition: filemap.h:44
Definition: filemap.h:42

◆ isRelDataFile()

static bool isRelDataFile ( const char *  path)
static

Definition at line 675 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().

676 {
677  RelFileNode rnode;
678  unsigned int segNo;
679  int nmatch;
680  bool matched;
681 
682  /*----
683  * Relation data files can be in one of the following directories:
684  *
685  * global/
686  * shared relations
687  *
688  * base/<db oid>/
689  * regular relations, default tablespace
690  *
691  * pg_tblspc/<tblspc oid>/<tblspc version>/
692  * within a non-default tablespace (the name of the directory
693  * depends on version)
694  *
695  * And the relation data files themselves have a filename like:
696  *
697  * <oid>.<segment number>
698  *
699  *----
700  */
701  rnode.spcNode = InvalidOid;
702  rnode.dbNode = InvalidOid;
703  rnode.relNode = InvalidOid;
704  segNo = 0;
705  matched = false;
706 
707  nmatch = sscanf(path, "global/%u.%u", &rnode.relNode, &segNo);
708  if (nmatch == 1 || nmatch == 2)
709  {
710  rnode.spcNode = GLOBALTABLESPACE_OID;
711  rnode.dbNode = 0;
712  matched = true;
713  }
714  else
715  {
716  nmatch = sscanf(path, "base/%u/%u.%u",
717  &rnode.dbNode, &rnode.relNode, &segNo);
718  if (nmatch == 2 || nmatch == 3)
719  {
720  rnode.spcNode = DEFAULTTABLESPACE_OID;
721  matched = true;
722  }
723  else
724  {
725  nmatch = sscanf(path, "pg_tblspc/%u/" TABLESPACE_VERSION_DIRECTORY "/%u/%u.%u",
726  &rnode.spcNode, &rnode.dbNode, &rnode.relNode,
727  &segNo);
728  if (nmatch == 3 || nmatch == 4)
729  matched = true;
730  }
731  }
732 
733  /*
734  * The sscanf tests above can match files that have extra characters at
735  * the end. To eliminate such cases, cross-check that GetRelationPath
736  * creates the exact same filename, when passed the RelFileNode
737  * information we extracted from the filename.
738  */
739  if (matched)
740  {
741  char *check_path = datasegpath(rnode, MAIN_FORKNUM, segNo);
742 
743  if (strcmp(check_path, path) != 0)
744  matched = false;
745 
746  pfree(check_path);
747  }
748 
749  return matched;
750 }
static char * datasegpath(RelFileNode rnode, ForkNumber forknum, BlockNumber segno)
Definition: filemap.c:758
void pfree(void *pointer)
Definition: mcxt.c:1031
#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 775 of file filemap.c.

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

Referenced by process_block_change(), and process_target_file().

776 {
777  file_entry_t *fa = *((file_entry_t **) a);
778  file_entry_t *fb = *((file_entry_t **) b);
779 
780  return strcmp(fa->path, fb->path);
781 }
static int fa(void)
Definition: preproc-init.c:85
static int fb(int x)
Definition: preproc-init.c:92
char * path
Definition: filemap.h:44
Definition: filemap.h:42

◆ print_filemap()

void print_filemap ( void  )

Definition at line 641 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_DEBUG, and pg_log().

Referenced by main().

642 {
643  filemap_t *map = filemap;
644  file_entry_t *entry;
645  int i;
646 
647  for (i = 0; i < map->narray; i++)
648  {
649  entry = map->array[i];
650  if (entry->action != FILE_ACTION_NONE ||
651  entry->pagemap.bitmapsize > 0)
652  {
654  /*------
655  translator: first %s is a file path, second is a keyword such as COPY */
656  "%s (%s)\n", entry->path,
657  action_to_str(entry->action));
658 
659  if (entry->pagemap.bitmapsize > 0)
660  datapagemap_print(&entry->pagemap);
661  }
662  }
663  fflush(stdout);
664 }
file_entry_t ** array
Definition: filemap.h:79
datapagemap_t pagemap
Definition: filemap.h:54
int narray
Definition: filemap.h:80
file_action_t action
Definition: filemap.h:47
filemap_t * filemap
Definition: filemap.c:25
static const char * action_to_str(file_action_t action)
Definition: filemap.c:573
void pg_log(eLogType type, const char *fmt,...)
Definition: logging.c:69
void datapagemap_print(datapagemap_t *map)
Definition: datapagemap.c:117
int bitmapsize
Definition: datapagemap.h:19
char * path
Definition: filemap.h:44
int i
Definition: filemap.h:42

◆ process_block_change()

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

Definition at line 418 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, filemap_t::narray, file_entry_t::pagemap, file_entry_t::path, path_cmp(), pfree(), and pg_fatal().

Referenced by extractPageInfo().

419 {
420  char *path;
421  file_entry_t key;
422  file_entry_t *key_ptr;
423  file_entry_t *entry;
424  BlockNumber blkno_inseg;
425  int segno;
426  filemap_t *map = filemap;
427  file_entry_t **e;
428 
429  Assert(map->array);
430 
431  segno = blkno / RELSEG_SIZE;
432  blkno_inseg = blkno % RELSEG_SIZE;
433 
434  path = datasegpath(rnode, forknum, segno);
435 
436  key.path = (char *) path;
437  key_ptr = &key;
438 
439  e = bsearch(&key_ptr, map->array, map->narray, sizeof(file_entry_t *),
440  path_cmp);
441  if (e)
442  entry = *e;
443  else
444  entry = NULL;
445  pfree(path);
446 
447  if (entry)
448  {
449  Assert(entry->isrelfile);
450 
451  switch (entry->action)
452  {
453  case FILE_ACTION_NONE:
455  /* skip if we're truncating away the modified block anyway */
456  if ((blkno_inseg + 1) * BLCKSZ <= entry->newsize)
457  datapagemap_add(&entry->pagemap, blkno_inseg);
458  break;
459 
461 
462  /*
463  * skip the modified block if it is part of the "tail" that
464  * we're copying anyway.
465  */
466  if ((blkno_inseg + 1) * BLCKSZ <= entry->oldsize)
467  datapagemap_add(&entry->pagemap, blkno_inseg);
468  break;
469 
470  case FILE_ACTION_COPY:
471  case FILE_ACTION_REMOVE:
472  break;
473 
474  case FILE_ACTION_CREATE:
475  pg_fatal("unexpected page modification for directory or symbolic link \"%s\"\n", entry->path);
476  }
477  }
478  else
479  {
480  /*
481  * If we don't have any record of this file in the file map, it means
482  * that it's a relation that doesn't exist in the source system, and
483  * it was subsequently removed in the target system, too. We can
484  * safely ignore it.
485  */
486  }
487 }
void datapagemap_add(datapagemap_t *map, BlockNumber blkno)
Definition: datapagemap.c:32
file_entry_t ** array
Definition: filemap.h:79
uint32 BlockNumber
Definition: block.h:31
void pg_fatal(const char *fmt,...)
Definition: logging.c:83
datapagemap_t pagemap
Definition: filemap.h:54
int narray
Definition: filemap.h:80
static char * datasegpath(RelFileNode rnode, ForkNumber forknum, BlockNumber segno)
Definition: filemap.c:758
bool isrelfile
Definition: filemap.h:52
void pfree(void *pointer)
Definition: mcxt.c:1031
file_action_t action
Definition: filemap.h:47
filemap_t * filemap
Definition: filemap.c:25
#define Assert(condition)
Definition: c.h:699
char * path
Definition: filemap.h:44
e
Definition: preproc-init.c:82
Definition: filemap.h:42
static int path_cmp(const void *a, const void *b)
Definition: filemap.c:775

◆ process_source_file()

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

Definition at line 137 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, strerror(), file_entry_t::type, and generate_unaccent_rules::type.

Referenced by fetchSourceFileList(), and libpqProcessFileList().

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

References file_entry_t::action, filemap_t::array, Assert, datapagemap::bitmap, datapagemap::bitmapsize, check_file_excluded(), datadir_target, FILE_ACTION_REMOVE, FILE_TYPE_DIRECTORY, FILE_TYPE_SYMLINK, filemap, filemap_list_to_array(), filemap_t::first, isRelDataFile(), file_entry_t::isrelfile, 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, strerror(), file_entry_t::type, and generate_unaccent_rules::type.

Referenced by main().

327 {
328  bool exists;
329  char localpath[MAXPGPATH];
330  struct stat statbuf;
331  file_entry_t key;
332  file_entry_t *key_ptr;
333  filemap_t *map = filemap;
334  file_entry_t *entry;
335 
336  /*
337  * Ignore any path matching the exclusion filters. This is not actually
338  * mandatory for target files, but this does not hurt and let's be
339  * consistent with the source processing.
340  */
341  if (check_file_excluded(path, "target"))
342  return;
343 
344  snprintf(localpath, sizeof(localpath), "%s/%s", datadir_target, path);
345  if (lstat(localpath, &statbuf) < 0)
346  {
347  if (errno != ENOENT)
348  pg_fatal("could not stat file \"%s\": %s\n",
349  localpath, strerror(errno));
350 
351  exists = false;
352  }
353 
354  if (map->array == NULL)
355  {
356  /* on first call, initialize lookup array */
357  if (map->nlist == 0)
358  {
359  /* should not happen */
360  pg_fatal("source file list is empty\n");
361  }
362 
364 
365  Assert(map->array != NULL);
366 
367  qsort(map->array, map->narray, sizeof(file_entry_t *), path_cmp);
368  }
369 
370  /*
371  * Like in process_source_file, pretend that xlog is always a directory.
372  */
373  if (strcmp(path, "pg_wal") == 0 && type == FILE_TYPE_SYMLINK)
375 
376  key.path = (char *) path;
377  key_ptr = &key;
378  exists = (bsearch(&key_ptr, map->array, map->narray, sizeof(file_entry_t *),
379  path_cmp) != NULL);
380 
381  /* Remove any file or folder that doesn't exist in the source system. */
382  if (!exists)
383  {
384  entry = pg_malloc(sizeof(file_entry_t));
385  entry->path = pg_strdup(path);
386  entry->type = type;
387  entry->action = FILE_ACTION_REMOVE;
388  entry->oldsize = oldsize;
389  entry->newsize = 0;
390  entry->link_target = link_target ? pg_strdup(link_target) : NULL;
391  entry->next = NULL;
392  entry->pagemap.bitmap = NULL;
393  entry->pagemap.bitmapsize = 0;
394  entry->isrelfile = isRelDataFile(path);
395 
396  if (map->last == NULL)
397  map->first = entry;
398  else
399  map->last->next = entry;
400  map->last = entry;
401  map->nlist++;
402  }
403  else
404  {
405  /*
406  * We already handled all files that exist in the source system in
407  * process_source_file().
408  */
409  }
410 }
char * datadir_target
Definition: pg_rewind.c:51
file_entry_t * first
Definition: filemap.h:68
file_entry_t ** array
Definition: filemap.h:79
void * pg_malloc(size_t size)
Definition: fe_memutils.c:47
size_t newsize
Definition: filemap.h:51
int snprintf(char *str, size_t count, const char *fmt,...) pg_attribute_printf(3
char * bitmap
Definition: datapagemap.h:18
void pg_fatal(const char *fmt,...)
Definition: logging.c:83
datapagemap_t pagemap
Definition: filemap.h:54
int narray
Definition: filemap.h:80
file_type_t type
Definition: filemap.h:45
bool isrelfile
Definition: filemap.h:52
int nlist
Definition: filemap.h:70
#define MAXPGPATH
file_action_t action
Definition: filemap.h:47
char * link_target
Definition: filemap.h:57
struct file_entry_t * next
Definition: filemap.h:59
filemap_t * filemap
Definition: filemap.c:25
size_t oldsize
Definition: filemap.h:50
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
#define stat(a, b)
Definition: win32_port.h:266
int bitmapsize
Definition: datapagemap.h:19
#define Assert(condition)
Definition: c.h:699
static bool isRelDataFile(const char *path)
Definition: filemap.c:675
char * path
Definition: filemap.h:44
static void filemap_list_to_array(filemap_t *map)
Definition: filemap.c:539
#define lstat(path, sb)
Definition: win32_port.h:255
file_entry_t * last
Definition: filemap.h:69
const char * strerror(int errnum)
Definition: strerror.c:19
Definition: filemap.h:42
static int path_cmp(const void *a, const void *b)
Definition: filemap.c:775
static bool check_file_excluded(const char *path, const char *type)
Definition: filemap.c:493
#define qsort(a, b, c, d)
Definition: port.h:421

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 44 of file filemap.c.

Referenced by check_file_excluded().

◆ excludeFiles

const char* excludeFiles[]
static
Initial value:
=
{
"postgresql.auto.conf.tmp",
"current_logfiles.tmp",
"pg_internal.init",
"backup_label",
"tablespace_map",
"postmaster.pid",
"postmaster.opts",
NULL
}

Definition at line 85 of file filemap.c.

Referenced by check_file_excluded().

◆ filemap