PostgreSQL Source Code  git master
file_utils.c File Reference
#include "postgres_fe.h"
#include <dirent.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <unistd.h>
#include "common/file_utils.h"
#include "common/logging.h"
Include dependency graph for file_utils.c:

Go to the source code of this file.

Macros

#define MINIMUM_VERSION_FOR_PG_WAL   100000
 

Functions

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

Macro Definition Documentation

◆ MINIMUM_VERSION_FOR_PG_WAL

#define MINIMUM_VERSION_FOR_PG_WAL   100000

Definition at line 36 of file file_utils.c.

Referenced by fsync_pgdata().

Function Documentation

◆ durable_rename()

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

Definition at line 338 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.

339 {
340  int fd;
341 
342  /*
343  * First fsync the old and target path (if it exists), to ensure that they
344  * are properly persistent on disk. Syncing the target file is not
345  * strictly necessary, but it makes it easier to reason about crashes;
346  * because it's then guaranteed that either source or target file exists
347  * after a crash.
348  */
349  if (fsync_fname(oldfile, false) != 0)
350  return -1;
351 
352  fd = open(newfile, PG_BINARY | O_RDWR, 0);
353  if (fd < 0)
354  {
355  if (errno != ENOENT)
356  {
357  pg_log_error("could not open file \"%s\": %m", newfile);
358  return -1;
359  }
360  }
361  else
362  {
363  if (fsync(fd) != 0)
364  {
365  pg_log_fatal("could not fsync file \"%s\": %m", newfile);
366  close(fd);
367  exit(EXIT_FAILURE);
368  }
369  close(fd);
370  }
371 
372  /* Time to do the real deal... */
373  if (rename(oldfile, newfile) != 0)
374  {
375  pg_log_error("could not rename file \"%s\" to \"%s\": %m",
376  oldfile, newfile);
377  return -1;
378  }
379 
380  /*
381  * To guarantee renaming the file is persistent, fsync the file with its
382  * new name, and its containing directory.
383  */
384  if (fsync_fname(newfile, false) != 0)
385  return -1;
386 
387  if (fsync_parent_path(newfile) != 0)
388  return -1;
389 
390  return 0;
391 }
int fsync_fname(const char *fname, bool isdir)
Definition: file_utils.c:255
int fsync_parent_path(const char *fname)
Definition: file_utils.c:311
#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:1234
#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 120 of file file_utils.c.

References fsync_fname(), and walkdir().

Referenced by _CloseArchive(), and BaseBackup().

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

◆ fsync_fname()

int fsync_fname ( const char *  fname,
bool  isdir 
)

Definition at line 255 of file file_utils.c.

References close, data_sync_elevel(), ERROR, EXIT_FAILURE, fd(), fsync, fsync_fname_ext(), PG_BINARY, pg_log_error, and pg_log_fatal.

Referenced by durable_rename(), fsync_dir_recurse(), fsync_parent_path(), and fsync_pgdata().

256 {
257  int fd;
258  int flags;
259  int returncode;
260 
261  /*
262  * Some OSs require directories to be opened read-only whereas other
263  * systems don't allow us to fsync files opened read-only; so we need both
264  * cases here. Using O_RDWR will cause us to fail to fsync files that are
265  * not writable by our userid, but we assume that's OK.
266  */
267  flags = PG_BINARY;
268  if (!isdir)
269  flags |= O_RDWR;
270  else
271  flags |= O_RDONLY;
272 
273  /*
274  * Open the file, silently ignoring errors about unreadable files (or
275  * unsupported operations, e.g. opening a directory under Windows), and
276  * logging others.
277  */
278  fd = open(fname, flags, 0);
279  if (fd < 0)
280  {
281  if (errno == EACCES || (isdir && errno == EISDIR))
282  return 0;
283  pg_log_error("could not open file \"%s\": %m", fname);
284  return -1;
285  }
286 
287  returncode = fsync(fd);
288 
289  /*
290  * Some OSes don't allow us to fsync directories at all, so we can ignore
291  * those errors. Anything else needs to be reported.
292  */
293  if (returncode != 0 && !(isdir && (errno == EBADF || errno == EINVAL)))
294  {
295  pg_log_fatal("could not fsync file \"%s\": %m", fname);
296  (void) close(fd);
297  exit(EXIT_FAILURE);
298  }
299 
300  (void) close(fd);
301  return 0;
302 }
#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:1234
#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_parent_path()

int fsync_parent_path ( const char *  fname)

Definition at line 311 of file file_utils.c.

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

Referenced by durable_rename().

312 {
313  char parentpath[MAXPGPATH];
314 
315  strlcpy(parentpath, fname, MAXPGPATH);
316  get_parent_directory(parentpath);
317 
318  /*
319  * get_parent_directory() returns an empty string if the input argument is
320  * just a file name (see comments in path.c), so handle that as being the
321  * current directory.
322  */
323  if (strlen(parentpath) == 0)
324  strlcpy(parentpath, ".", MAXPGPATH);
325 
326  if (fsync_fname(parentpath, true) != 0)
327  return -1;
328 
329  return 0;
330 }
int fsync_fname(const char *fname, bool isdir)
Definition: file_utils.c:255
#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 56 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().

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

◆ walkdir()

static void walkdir ( const char *  path,
int(*)(const char *fname, bool isdir)  action,
bool  process_symlinks 
)
static

Definition at line 148 of file file_utils.c.

References generate_unaccent_rules::action, close, closedir(), dirent::d_name, fd(), lstat, MAXPGPATH, opendir(), PG_BINARY, pg_log_error, readdir(), S_ISDIR, S_ISREG, snprintf, stat, and subpath().

Referenced by fsync_dir_recurse(), and fsync_pgdata().

151 {
152  DIR *dir;
153  struct dirent *de;
154 
155  dir = opendir(path);
156  if (dir == NULL)
157  {
158  pg_log_error("could not open directory \"%s\": %m", path);
159  return;
160  }
161 
162  while (errno = 0, (de = readdir(dir)) != NULL)
163  {
164  char subpath[MAXPGPATH * 2];
165  struct stat fst;
166  int sret;
167 
168  if (strcmp(de->d_name, ".") == 0 ||
169  strcmp(de->d_name, "..") == 0)
170  continue;
171 
172  snprintf(subpath, sizeof(subpath), "%s/%s", path, de->d_name);
173 
174  if (process_symlinks)
175  sret = stat(subpath, &fst);
176  else
177  sret = lstat(subpath, &fst);
178 
179  if (sret < 0)
180  {
181  pg_log_error("could not stat file \"%s\": %m", subpath);
182  continue;
183  }
184 
185  if (S_ISREG(fst.st_mode))
186  (*action) (subpath, false);
187  else if (S_ISDIR(fst.st_mode))
188  walkdir(subpath, action, false);
189  }
190 
191  if (errno)
192  pg_log_error("could not read directory \"%s\": %m", path);
193 
194  (void) closedir(dir);
195 
196  /*
197  * It's important to fsync the destination directory itself as individual
198  * file fsyncs don't guarantee that the directory entry for the file is
199  * synced. Recent versions of ext4 have made the window much wider but
200  * it's been an issue for ext3 and other filesystems in the past.
201  */
202  (*action) (path, true);
203 }
#define pg_log_error(...)
Definition: logging.h:79
int closedir(DIR *)
Definition: dirent.c:113
static void walkdir(const char *path, int(*action)(const char *fname, bool isdir), bool process_symlinks)
Definition: file_utils.c:148
Definition: dirent.h:9
Definition: dirent.c:25
#define MAXPGPATH
DIR * opendir(const char *)
Definition: dirent.c:33
#define S_ISREG(m)
Definition: win32_port.h:299
#define stat(a, b)
Definition: win32_port.h:255
struct dirent * readdir(DIR *)
Definition: dirent.c:77
#define S_ISDIR(m)
Definition: win32_port.h:296
#define lstat(path, sb)
Definition: win32_port.h:244
char d_name[MAX_PATH]
Definition: dirent.h:14
#define snprintf
Definition: port.h:193
Datum subpath(PG_FUNCTION_ARGS)
Definition: ltree_op.c:241