PostgreSQL Source Code  git master
file_utils.h File Reference
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

int fsync_fname (const char *fname, bool isdir)
 
void fsync_pgdata (const char *pg_data, int serverVersion)
 
void fsync_dir_recurse (const char *dir)
 
int durable_rename (const char *oldfile, const char *newfile)
 
int fsync_parent_path (const char *fname)
 

Function Documentation

◆ durable_rename()

int durable_rename ( const char *  oldfile,
const char *  newfile 
)

Definition at line 343 of file file_utils.c.

References close, EXIT_FAILURE, fd(), fsync, fsync_fname(), fsync_parent_path(), PG_BINARY, pg_log_error, and pg_log_fatal.

344 {
345  int fd;
346 
347  /*
348  * First fsync the old and target path (if it exists), to ensure that they
349  * are properly persistent on disk. Syncing the target file is not
350  * strictly necessary, but it makes it easier to reason about crashes;
351  * because it's then guaranteed that either source or target file exists
352  * after a crash.
353  */
354  if (fsync_fname(oldfile, false) != 0)
355  return -1;
356 
357  fd = open(newfile, PG_BINARY | O_RDWR, 0);
358  if (fd < 0)
359  {
360  if (errno != ENOENT)
361  {
362  pg_log_error("could not open file \"%s\": %m", newfile);
363  return -1;
364  }
365  }
366  else
367  {
368  if (fsync(fd) != 0)
369  {
370  pg_log_fatal("could not fsync file \"%s\": %m", newfile);
371  close(fd);
372  exit(EXIT_FAILURE);
373  }
374  close(fd);
375  }
376 
377  /* Time to do the real deal... */
378  if (rename(oldfile, newfile) != 0)
379  {
380  pg_log_error("could not rename file \"%s\" to \"%s\": %m",
381  oldfile, newfile);
382  return -1;
383  }
384 
385  /*
386  * To guarantee renaming the file is persistent, fsync the file with its
387  * new name, and its containing directory.
388  */
389  if (fsync_fname(newfile, false) != 0)
390  return -1;
391 
392  if (fsync_parent_path(newfile) != 0)
393  return -1;
394 
395  return 0;
396 }
int fsync_fname(const char *fname, bool isdir)
Definition: file_utils.c:260
int fsync_parent_path(const char *fname)
Definition: file_utils.c:316
#define pg_log_error(...)
Definition: logging.h:79
static int fd(const char *x, int i)
Definition: preproc-init.c:105
#define PG_BINARY
Definition: c.h:1233
#define fsync(fd)
Definition: win32_port.h:62
#define EXIT_FAILURE
Definition: settings.h:154
#define close(a)
Definition: win32.h:12
#define pg_log_fatal(...)
Definition: logging.h:75

◆ fsync_dir_recurse()

void fsync_dir_recurse ( const char *  dir)

Definition at line 125 of file file_utils.c.

References fsync_fname(), and walkdir().

Referenced by _CloseArchive(), and BaseBackup().

126 {
127  /*
128  * If possible, hint to the kernel that we're soon going to fsync the data
129  * directory and its contents.
130  */
131 #ifdef PG_FLUSH_DATA_WORKS
132  walkdir(dir, pre_sync_fname, false);
133 #endif
134 
135  walkdir(dir, fsync_fname, false);
136 }
int fsync_fname(const char *fname, bool isdir)
Definition: file_utils.c:260
static void walkdir(const char *path, int(*action)(const char *fname, bool isdir), bool process_symlinks)
Definition: file_utils.c:153

◆ fsync_fname()

int fsync_fname ( const char *  fname,
bool  isdir 
)

Definition at line 632 of file fd.c.

633 {
634  fsync_fname_ext(fname, isdir, false, data_sync_elevel(ERROR));
635 }
#define ERROR
Definition: elog.h:43
int data_sync_elevel(int elevel)
Definition: fd.c:3605
int fsync_fname_ext(const char *fname, bool isdir, bool ignore_perm, int elevel)
Definition: fd.c:3466

◆ fsync_parent_path()

int fsync_parent_path ( const char *  fname)

Definition at line 316 of file file_utils.c.

References fsync_fname(), get_parent_directory(), MAXPGPATH, and strlcpy().

Referenced by durable_rename().

317 {
318  char parentpath[MAXPGPATH];
319 
320  strlcpy(parentpath, fname, MAXPGPATH);
321  get_parent_directory(parentpath);
322 
323  /*
324  * get_parent_directory() returns an empty string if the input argument is
325  * just a file name (see comments in path.c), so handle that as being the
326  * current directory.
327  */
328  if (strlen(parentpath) == 0)
329  strlcpy(parentpath, ".", MAXPGPATH);
330 
331  if (fsync_fname(parentpath, true) != 0)
332  return -1;
333 
334  return 0;
335 }
int fsync_fname(const char *fname, bool isdir)
Definition: file_utils.c:260
#define MAXPGPATH
void get_parent_directory(char *path)
Definition: path.c:854
size_t strlcpy(char *dst, const char *src, size_t siz)
Definition: strlcpy.c:45

◆ fsync_pgdata()

void fsync_pgdata ( const char *  pg_data,
int  serverVersion 
)

Definition at line 61 of file file_utils.c.

References fsync_fname(), lstat, MAXPGPATH, MINIMUM_VERSION_FOR_PG_WAL, pg_log_error, pgwin32_is_junction(), snprintf, stat, and walkdir().

Referenced by BaseBackup(), main(), and syncTargetDirectory().

63 {
64  bool xlog_is_symlink;
65  char pg_wal[MAXPGPATH];
66  char pg_tblspc[MAXPGPATH];
67 
68  /* handle renaming of pg_xlog to pg_wal in post-10 clusters */
69  snprintf(pg_wal, MAXPGPATH, "%s/%s", pg_data,
70  serverVersion < MINIMUM_VERSION_FOR_PG_WAL ? "pg_xlog" : "pg_wal");
71  snprintf(pg_tblspc, MAXPGPATH, "%s/pg_tblspc", pg_data);
72 
73  /*
74  * If pg_wal is a symlink, we'll need to recurse into it separately,
75  * because the first walkdir below will ignore it.
76  */
77  xlog_is_symlink = false;
78 
79 #ifndef WIN32
80  {
81  struct stat st;
82 
83  if (lstat(pg_wal, &st) < 0)
84  pg_log_error("could not stat file \"%s\": %m", pg_wal);
85  else if (S_ISLNK(st.st_mode))
86  xlog_is_symlink = true;
87  }
88 #else
89  if (pgwin32_is_junction(pg_wal))
90  xlog_is_symlink = true;
91 #endif
92 
93  /*
94  * If possible, hint to the kernel that we're soon going to fsync the data
95  * directory and its contents.
96  */
97 #ifdef PG_FLUSH_DATA_WORKS
98  walkdir(pg_data, pre_sync_fname, false);
99  if (xlog_is_symlink)
100  walkdir(pg_wal, pre_sync_fname, false);
101  walkdir(pg_tblspc, pre_sync_fname, true);
102 #endif
103 
104  /*
105  * Now we do the fsync()s in the same order.
106  *
107  * The main call ignores symlinks, so in addition to specially processing
108  * pg_wal if it's a symlink, pg_tblspc has to be visited separately with
109  * process_symlinks = true. Note that if there are any plain directories
110  * in pg_tblspc, they'll get fsync'd twice. That's not an expected case
111  * so we don't worry about optimizing it.
112  */
113  walkdir(pg_data, fsync_fname, false);
114  if (xlog_is_symlink)
115  walkdir(pg_wal, fsync_fname, false);
116  walkdir(pg_tblspc, fsync_fname, true);
117 }
int fsync_fname(const char *fname, bool isdir)
Definition: file_utils.c:260
#define MINIMUM_VERSION_FOR_PG_WAL
Definition: file_utils.c:41
#define pg_log_error(...)
Definition: logging.h:79
static void walkdir(const char *path, int(*action)(const char *fname, bool isdir), bool process_symlinks)
Definition: file_utils.c:153
#define MAXPGPATH
static char * pg_data
Definition: initdb.c:123
#define stat(a, b)
Definition: win32_port.h:255
#define lstat(path, sb)
Definition: win32_port.h:244
#define snprintf
Definition: port.h:193
bool pgwin32_is_junction(const char *path)