|
| 1 | +#include <c.h> |
| 2 | +#include <fcntl.h> |
| 3 | +#include <unistd.h> |
| 4 | + |
| 5 | +#include "logging.h" |
| 6 | +#include "file_compat.h" |
| 7 | +/*vvs*/ |
| 8 | +/* |
| 9 | + * fsync_fname -- Try to fsync a file or directory |
| 10 | + * |
| 11 | + * Ignores errors trying to open unreadable files, or trying to fsync |
| 12 | + * directories on systems where that isn't allowed/required. All other errors |
| 13 | + * are fatal. |
| 14 | + */ |
| 15 | +int |
| 16 | +fsync_fname_compat(const char* fname, bool isdir) |
| 17 | +{ |
| 18 | + int fd; |
| 19 | + int flags; |
| 20 | + int returncode; |
| 21 | + |
| 22 | + /* |
| 23 | + * Some OSs require directories to be opened read-only whereas other |
| 24 | + * systems don't allow us to fsync files opened read-only; so we need both |
| 25 | + * cases here. Using O_RDWR will cause us to fail to fsync files that are |
| 26 | + * not writable by our userid, but we assume that's OK. |
| 27 | + */ |
| 28 | + flags = PG_BINARY; |
| 29 | + if (!isdir) |
| 30 | + flags |= O_RDWR; |
| 31 | + else |
| 32 | + flags |= O_RDONLY; |
| 33 | + |
| 34 | + /* |
| 35 | + * Open the file, silently ignoring errors about unreadable files (or |
| 36 | + * unsupported operations, e.g. opening a directory under Windows), and |
| 37 | + * logging others. |
| 38 | + */ |
| 39 | + fd = open(fname, flags, 0); |
| 40 | + if (fd < 0) |
| 41 | + { |
| 42 | + if (errno == EACCES || (isdir && errno == EISDIR)) |
| 43 | + return 0; |
| 44 | + pg_log_error("could not open file \"%s\": %m", fname); |
| 45 | + return -1; |
| 46 | + } |
| 47 | + |
| 48 | + returncode = fsync(fd); |
| 49 | + |
| 50 | + /* |
| 51 | + * Some OSes don't allow us to fsync directories at all, so we can ignore |
| 52 | + * those errors. Anything else needs to be reported. |
| 53 | + */ |
| 54 | + if (returncode != 0 && !(isdir && (errno == EBADF || errno == EINVAL))) |
| 55 | + { |
| 56 | + pg_log_fatal("could not fsync file \"%s\": %m", fname); |
| 57 | + (void)close(fd); |
| 58 | + exit(EXIT_FAILURE); |
| 59 | + } |
| 60 | + |
| 61 | + (void)close(fd); |
| 62 | + return 0; |
| 63 | +} |
| 64 | + |
| 65 | +/* |
| 66 | + * fsync_parent_path -- fsync the parent path of a file or directory |
| 67 | + * |
| 68 | + * This is aimed at making file operations persistent on disk in case of |
| 69 | + * an OS crash or power failure. |
| 70 | + */ |
| 71 | +int |
| 72 | +fsync_parent_path_compat(const char* fname) |
| 73 | +{ |
| 74 | + char parentpath[MAXPGPATH]; |
| 75 | + |
| 76 | + strlcpy(parentpath, fname, MAXPGPATH); |
| 77 | + get_parent_directory(parentpath); |
| 78 | + |
| 79 | + /* |
| 80 | + * get_parent_directory() returns an empty string if the input argument is |
| 81 | + * just a file name (see comments in path.c), so handle that as being the |
| 82 | + * current directory. |
| 83 | + */ |
| 84 | + if (strlen(parentpath) == 0) |
| 85 | + strlcpy(parentpath, ".", MAXPGPATH); |
| 86 | + |
| 87 | + if (fsync_fname_compat(parentpath, true) != 0) |
| 88 | + return -1; |
| 89 | + |
| 90 | + return 0; |
| 91 | +} |
| 92 | + |
| 93 | +/* |
| 94 | + * durable_rename -- rename(2) wrapper, issuing fsyncs required for durability |
| 95 | + * |
| 96 | + * Wrapper around rename, similar to the backend version. |
| 97 | + */ |
| 98 | +int |
| 99 | +durable_rename_compat(const char* oldfile, const char* newfile) |
| 100 | +{ |
| 101 | + int fd; |
| 102 | + |
| 103 | + /* |
| 104 | + * First fsync the old and target path (if it exists), to ensure that they |
| 105 | + * are properly persistent on disk. Syncing the target file is not |
| 106 | + * strictly necessary, but it makes it easier to reason about crashes; |
| 107 | + * because it's then guaranteed that either source or target file exists |
| 108 | + * after a crash. |
| 109 | + */ |
| 110 | + if (fsync_fname_compat(oldfile, false) != 0) |
| 111 | + return -1; |
| 112 | + |
| 113 | + fd = open(newfile, PG_BINARY | O_RDWR, 0); |
| 114 | + if (fd < 0) |
| 115 | + { |
| 116 | + if (errno != ENOENT) |
| 117 | + { |
| 118 | + pg_log_error("could not open file \"%s\": %m", newfile); |
| 119 | + return -1; |
| 120 | + } |
| 121 | + } |
| 122 | + else |
| 123 | + { |
| 124 | + if (fsync(fd) != 0) |
| 125 | + { |
| 126 | + pg_log_fatal("could not fsync file \"%s\": %m", newfile); |
| 127 | + close(fd); |
| 128 | + exit(EXIT_FAILURE); |
| 129 | + } |
| 130 | + close(fd); |
| 131 | + } |
| 132 | + |
| 133 | + /* Time to do the real deal... */ |
| 134 | + if (rename(oldfile, newfile) != 0) |
| 135 | + { |
| 136 | + pg_log_error("could not rename file \"%s\" to \"%s\": %m", |
| 137 | + oldfile, newfile); |
| 138 | + return -1; |
| 139 | + } |
| 140 | + |
| 141 | + /* |
| 142 | + * To guarantee renaming the file is persistent, fsync the file with its |
| 143 | + * new name, and its containing directory. |
| 144 | + */ |
| 145 | + if (fsync_fname_compat(newfile, false) != 0) |
| 146 | + return -1; |
| 147 | + |
| 148 | + if (fsync_parent_path_compat(newfile) != 0) |
| 149 | + return -1; |
| 150 | + |
| 151 | + return 0; |
| 152 | +} |
0 commit comments