diff options
author | Andres Freund <andres@anarazel.de> | 2014-11-15 01:09:05 +0100 |
---|---|---|
committer | Andres Freund <andres@anarazel.de> | 2014-11-15 01:22:32 +0100 |
commit | b0a48e996bd7ff336ea26344d3d97ad32b22a61a (patch) | |
tree | 6a7f99878cb65a91528814719fb306a2e11eddc1 | |
parent | 79b2fa5bd2f7d7721651e177c824eb103870d8f5 (diff) | |
download | postgresql-b0a48e996bd7ff336ea26344d3d97ad32b22a61a.tar.gz postgresql-b0a48e996bd7ff336ea26344d3d97ad32b22a61a.zip |
Backport "Expose fsync_fname as a public API".
Backport commit cc52d5b33ff5df29de57dcae9322214cfe9c8464 back to 9.1
to allow backpatching some unlogged table fixes that use fsync_fname.
-rw-r--r-- | src/backend/storage/file/copydir.c | 59 | ||||
-rw-r--r-- | src/backend/storage/file/fd.c | 56 | ||||
-rw-r--r-- | src/include/storage/fd.h | 1 |
3 files changed, 57 insertions, 59 deletions
diff --git a/src/backend/storage/file/copydir.c b/src/backend/storage/file/copydir.c index 6cfb8164f69..51e29662ac7 100644 --- a/src/backend/storage/file/copydir.c +++ b/src/backend/storage/file/copydir.c @@ -38,9 +38,6 @@ #endif -static void fsync_fname(char *fname, bool isdir); - - /* * copydir: copy a directory * @@ -214,59 +211,3 @@ copy_file(char *fromfile, char *tofile) pfree(buffer); } - - -/* - * fsync a file - * - * Try to fsync directories but ignore errors that indicate the OS - * just doesn't allow/require fsyncing directories. - */ -static void -fsync_fname(char *fname, bool isdir) -{ - int fd; - int returncode; - - /* - * Some OSs require directories to be opened read-only whereas other - * systems don't allow us to fsync files opened read-only; so we need both - * cases here - */ - if (!isdir) - fd = BasicOpenFile(fname, - O_RDWR | PG_BINARY, - S_IRUSR | S_IWUSR); - else - fd = BasicOpenFile(fname, - O_RDONLY | PG_BINARY, - S_IRUSR | S_IWUSR); - - /* - * Some OSs don't allow us to open directories at all (Windows returns - * EACCES) - */ - if (fd < 0 && isdir && (errno == EISDIR || errno == EACCES)) - return; - - else if (fd < 0) - ereport(ERROR, - (errcode_for_file_access(), - errmsg("could not open file \"%s\": %m", fname))); - - returncode = pg_fsync(fd); - - /* Some OSs don't allow us to fsync directories at all */ - if (returncode != 0 && isdir && errno == EBADF) - { - close(fd); - return; - } - - if (returncode != 0) - ereport(ERROR, - (errcode_for_file_access(), - errmsg("could not fsync file \"%s\": %m", fname))); - - close(fd); -} diff --git a/src/backend/storage/file/fd.c b/src/backend/storage/file/fd.c index ed76272fbcf..727952616d7 100644 --- a/src/backend/storage/file/fd.c +++ b/src/backend/storage/file/fd.c @@ -346,6 +346,62 @@ pg_flush_data(int fd, off_t offset, off_t amount) /* + * fsync_fname -- fsync a file or directory, handling errors properly + * + * Try to fsync a file or directory. When doing the latter, ignore errors that + * indicate the OS just doesn't allow/require fsyncing directories. + */ +void +fsync_fname(char *fname, bool isdir) +{ + int fd; + int returncode; + + /* + * Some OSs require directories to be opened read-only whereas other + * systems don't allow us to fsync files opened read-only; so we need both + * cases here + */ + if (!isdir) + fd = BasicOpenFile(fname, + O_RDWR | PG_BINARY, + S_IRUSR | S_IWUSR); + else + fd = BasicOpenFile(fname, + O_RDONLY | PG_BINARY, + S_IRUSR | S_IWUSR); + + /* + * Some OSs don't allow us to open directories at all (Windows returns + * EACCES) + */ + if (fd < 0 && isdir && (errno == EISDIR || errno == EACCES)) + return; + + else if (fd < 0) + ereport(ERROR, + (errcode_for_file_access(), + errmsg("could not open file \"%s\": %m", fname))); + + returncode = pg_fsync(fd); + + /* Some OSs don't allow us to fsync directories at all */ + if (returncode != 0 && isdir && errno == EBADF) + { + close(fd); + return; + } + + if (returncode != 0) + ereport(ERROR, + (errcode_for_file_access(), + errmsg("could not fsync file \"%s\": %m", fname))); + + close(fd); +} + + +/* * InitFileAccess --- initialize this module during backend startup * * This is called during either normal or standalone backend start. diff --git a/src/include/storage/fd.h b/src/include/storage/fd.h index dc0aada35bc..4cff9c13c8b 100644 --- a/src/include/storage/fd.h +++ b/src/include/storage/fd.h @@ -99,6 +99,7 @@ extern int pg_fsync_no_writethrough(int fd); extern int pg_fsync_writethrough(int fd); extern int pg_fdatasync(int fd); extern int pg_flush_data(int fd, off_t offset, off_t amount); +extern void fsync_fname(char *fname, bool isdir); /* Filename components for OpenTemporaryFile */ #define PG_TEMP_FILES_DIR "pgsql_tmp" |