219
227
static char **readfile(const char *path);
220
228
static void writefile(char *path, char **lines);
221
static void walkdir(char *path, void (*action) (char *fname, bool isdir));
222
static void walktblspc_links(char *path, void (*action) (char *fname, bool isdir));
223
static void pre_sync_fname(char *fname, bool isdir);
224
static void fsync_fname(char *fname, bool isdir);
229
static void walkdir(const char *path,
230
void (*action) (const char *fname, bool isdir),
231
bool process_symlinks);
232
#ifdef PG_FLUSH_DATA_WORKS
233
static void pre_sync_fname(const char *fname, bool isdir);
235
static void fsync_fname_ext(const char *fname, bool isdir);
225
236
static FILE *popen_check(const char *command, const char *mode);
226
237
static void exit_nicely(void);
227
238
static char *get_id(void);
526
537
* walkdir: recursively walk a directory, applying the action to each
527
538
* regular file and directory (including the named directory itself).
529
* Adapted from copydir() in copydir.c.
540
* If process_symlinks is true, the action and recursion are also applied
541
* to regular files and directories that are pointed to by symlinks in the
542
* given directory; otherwise symlinks are ignored. Symlinks are always
543
* ignored in subdirectories, ie we intentionally don't pass down the
544
* process_symlinks flag to recursive calls.
546
* Errors are reported but not considered fatal.
548
* See also walkdir in fd.c, which is a backend version of this logic.
532
walkdir(char *path, void (*action) (char *fname, bool isdir))
551
walkdir(const char *path,
552
void (*action) (const char *fname, bool isdir),
553
bool process_symlinks)
535
struct dirent *direntry;
536
char subpath[MAXPGPATH];
538
558
dir = opendir(path);
541
561
fprintf(stderr, _("%s: could not open directory \"%s\": %s\n"),
542
562
progname, path, strerror(errno));
546
while (errno = 0, (direntry = readdir(dir)) != NULL)
566
while (errno = 0, (de = readdir(dir)) != NULL)
568
char subpath[MAXPGPATH];
550
if (strcmp(direntry->d_name, ".") == 0 ||
551
strcmp(direntry->d_name, "..") == 0)
572
if (strcmp(de->d_name, ".") == 0 ||
573
strcmp(de->d_name, "..") == 0)
554
snprintf(subpath, MAXPGPATH, "%s/%s", path, direntry->d_name);
556
if (lstat(subpath, &fst) < 0)
576
snprintf(subpath, MAXPGPATH, "%s/%s", path, de->d_name);
578
if (process_symlinks)
579
sret = stat(subpath, &fst);
581
sret = lstat(subpath, &fst);
558
585
fprintf(stderr, _("%s: could not stat file \"%s\": %s\n"),
559
586
progname, subpath, strerror(errno));
563
if (S_ISDIR(fst.st_mode))
564
walkdir(subpath, action);
565
else if (S_ISREG(fst.st_mode))
590
if (S_ISREG(fst.st_mode))
566
591
(*action) (subpath, false);
592
else if (S_ISDIR(fst.st_mode))
593
walkdir(subpath, action, false);
571
597
fprintf(stderr, _("%s: could not read directory \"%s\": %s\n"),
572
598
progname, path, strerror(errno));
578
fprintf(stderr, _("%s: could not close directory \"%s\": %s\n"),
579
progname, path, strerror(errno));
600
(void) closedir(dir);
584
603
* It's important to fsync the destination directory itself as individual
593
* walktblspc_links: call walkdir on each entry under the given
594
* pg_tblspc directory, or do nothing if pg_tblspc doesn't exist.
597
walktblspc_links(char *path, void (*action) (char *fname, bool isdir))
600
struct dirent *direntry;
601
char subpath[MAXPGPATH];
608
fprintf(stderr, _("%s: could not open directory \"%s\": %s\n"),
609
progname, path, strerror(errno));
613
while (errno = 0, (direntry = readdir(dir)) != NULL)
615
if (strcmp(direntry->d_name, ".") == 0 ||
616
strcmp(direntry->d_name, "..") == 0)
619
/* fsync the version specific tablespace subdirectory */
620
snprintf(subpath, sizeof(subpath), "%s/%s/%s",
621
path, direntry->d_name, TABLESPACE_VERSION_DIRECTORY);
623
walkdir(subpath, action);
628
fprintf(stderr, _("%s: could not read directory \"%s\": %s\n"),
629
progname, path, strerror(errno));
635
fprintf(stderr, _("%s: could not close directory \"%s\": %s\n"),
636
progname, path, strerror(errno));
642
612
* Hint to the OS that it should get ready to fsync() this file.
614
* Ignores errors trying to open unreadable files, and reports other errors
617
#ifdef PG_FLUSH_DATA_WORKS
645
pre_sync_fname(char *fname, bool isdir)
620
pre_sync_fname(const char *fname, bool isdir)
647
#if defined(HAVE_SYNC_FILE_RANGE) || \
648
(defined(USE_POSIX_FADVISE) && defined(POSIX_FADV_DONTNEED))
651
624
fd = open(fname, O_RDONLY | PG_BINARY);
654
* Some OSs don't allow us to open directories at all (Windows returns
657
if (fd < 0 && isdir && (errno == EISDIR || errno == EACCES))
628
if (errno == EACCES || (isdir && errno == EISDIR))
662
630
fprintf(stderr, _("%s: could not open file \"%s\": %s\n"),
663
631
progname, fname, strerror(errno));
668
* Prefer sync_file_range, else use posix_fadvise. We ignore any error
669
* here since this operation is only a hint anyway.
636
* We do what pg_flush_data() would do in the backend: prefer to use
637
* sync_file_range, but fall back to posix_fadvise. We ignore errors
638
* because this is only a hint.
671
640
#if defined(HAVE_SYNC_FILE_RANGE)
672
sync_file_range(fd, 0, 0, SYNC_FILE_RANGE_WRITE);
641
(void) sync_file_range(fd, 0, 0, SYNC_FILE_RANGE_WRITE);
673
642
#elif defined(USE_POSIX_FADVISE) && defined(POSIX_FADV_DONTNEED)
674
posix_fadvise(fd, 0, 0, POSIX_FADV_DONTNEED);
643
(void) posix_fadvise(fd, 0, 0, POSIX_FADV_DONTNEED);
645
#error PG_FLUSH_DATA_WORKS should not have been defined
651
#endif /* PG_FLUSH_DATA_WORKS */
682
* fsync a file or directory
684
* Try to fsync directories but ignore errors that indicate the OS
685
* just doesn't allow/require fsyncing directories.
687
* Adapted from fsync_fname() in copydir.c.
654
* fsync_fname_ext -- Try to fsync a file or directory
656
* Ignores errors trying to open unreadable files, or trying to fsync
657
* directories on systems where that isn't allowed/required. Reports
658
* other errors non-fatally.
690
fsync_fname(char *fname, bool isdir)
661
fsync_fname_ext(const char *fname, bool isdir)
696
668
* Some OSs require directories to be opened read-only whereas other
697
669
* systems don't allow us to fsync files opened read-only; so we need both
670
* cases here. Using O_RDWR will cause us to fail to fsync files that are
671
* not writable by our userid, but we assume that's OK.
701
fd = open(fname, O_RDWR | PG_BINARY);
703
fd = open(fname, O_RDONLY | PG_BINARY);
706
* Some OSs don't allow us to open directories at all (Windows returns
680
* Open the file, silently ignoring errors about unreadable files (or
681
* unsupported operations, e.g. opening a directory under Windows), and
709
if (fd < 0 && isdir && (errno == EISDIR || errno == EACCES))
684
fd = open(fname, flags);
687
if (errno == EACCES || (isdir && errno == EISDIR))
714
689
fprintf(stderr, _("%s: could not open file \"%s\": %s\n"),
715
690
progname, fname, strerror(errno));
719
694
returncode = fsync(fd);
721
/* Some OSs don't allow us to fsync directories at all */
722
if (returncode != 0 && isdir && errno == EBADF)
697
* Some OSes don't allow us to fsync directories at all, so we can ignore
698
* those errors. Anything else needs to be reported.
700
if (returncode != 0 && !(isdir && errno == EBADF))
730
701
fprintf(stderr, _("%s: could not fsync file \"%s\": %s\n"),
731
702
progname, fname, strerror(errno));
2427
* fsync everything down to disk
2396
* Issue fsync recursively on PGDATA and all its contents.
2398
* We fsync regular files and directories wherever they are, but we
2399
* follow symlinks only for pg_xlog and immediately under pg_tblspc.
2400
* Other symlinks are presumed to point at files we're not responsible
2401
* for fsyncing, and might not have privileges to write at all.
2403
* Errors are reported but not considered fatal.
2432
char pdir[MAXPGPATH];
2408
bool xlog_is_symlink;
2409
char pg_xlog[MAXPGPATH];
2433
2410
char pg_tblspc[MAXPGPATH];
2435
2412
fputs(_("syncing data to disk ... "), stdout);
2436
2413
fflush(stdout);
2439
* We need to name the parent of PGDATA. get_parent_directory() isn't
2440
* enough here, because it can result in an empty string.
2442
snprintf(pdir, MAXPGPATH, "%s/..", pg_data);
2443
canonicalize_path(pdir);
2446
* Hint to the OS so that we're going to fsync each of these files soon.
2449
/* first the parent of the PGDATA directory */
2450
pre_sync_fname(pdir, true);
2452
/* then recursively through the data directory */
2453
walkdir(pg_data, pre_sync_fname);
2455
/* now do the same thing for everything under pg_tblspc */
2415
snprintf(pg_xlog, MAXPGPATH, "%s/pg_xlog", pg_data);
2456
2416
snprintf(pg_tblspc, MAXPGPATH, "%s/pg_tblspc", pg_data);
2457
walktblspc_links(pg_tblspc, pre_sync_fname);
2460
* Now, do the fsync()s in the same order.
2463
/* first the parent of the PGDATA directory */
2464
fsync_fname(pdir, true);
2466
/* then recursively through the data directory */
2467
walkdir(pg_data, fsync_fname);
2469
/* and now the same for all tablespaces */
2470
walktblspc_links(pg_tblspc, fsync_fname);
2419
* If pg_xlog is a symlink, we'll need to recurse into it separately,
2420
* because the first walkdir below will ignore it.
2422
xlog_is_symlink = false;
2428
if (lstat(pg_xlog, &st) < 0)
2429
fprintf(stderr, _("%s: could not stat file \"%s\": %s\n"),
2430
progname, pg_xlog, strerror(errno));
2431
else if (S_ISLNK(st.st_mode))
2432
xlog_is_symlink = true;
2435
if (pgwin32_is_junction(pg_xlog))
2436
xlog_is_symlink = true;
2440
* If possible, hint to the kernel that we're soon going to fsync the data
2441
* directory and its contents.
2443
#ifdef PG_FLUSH_DATA_WORKS
2444
walkdir(pg_data, pre_sync_fname, false);
2445
if (xlog_is_symlink)
2446
walkdir(pg_xlog, pre_sync_fname, false);
2447
walkdir(pg_tblspc, pre_sync_fname, true);
2451
* Now we do the fsync()s in the same order.
2453
* The main call ignores symlinks, so in addition to specially processing
2454
* pg_xlog if it's a symlink, pg_tblspc has to be visited separately with
2455
* process_symlinks = true. Note that if there are any plain directories
2456
* in pg_tblspc, they'll get fsync'd twice. That's not an expected case
2457
* so we don't worry about optimizing it.
2459
walkdir(pg_data, fsync_fname_ext, false);
2460
if (xlog_is_symlink)
2461
walkdir(pg_xlog, fsync_fname_ext, false);
2462
walkdir(pg_tblspc, fsync_fname_ext, true);