]> granicus.if.org Git - postgresql/commitdiff
Backport "Expose fsync_fname as a public API".
authorAndres Freund <andres@anarazel.de>
Sat, 15 Nov 2014 00:09:05 +0000 (01:09 +0100)
committerAndres Freund <andres@anarazel.de>
Sat, 15 Nov 2014 00:21:30 +0000 (01:21 +0100)
Backport commit cc52d5b33ff5df29de57dcae9322214cfe9c8464 back to 9.1
to allow backpatching some unlogged table fixes that use fsync_fname.

src/backend/storage/file/copydir.c
src/backend/storage/file/fd.c
src/include/storage/fd.h

index dc89442041a1e2968c7c1626e5faa4264c9d8a28..26de1c8d0833f904341ff40cadc04081cc3293e0 100644 (file)
@@ -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);
-}
index a74a0f9c07348c97998c206ee1e2309531087dab..6b97cad4419275716e3e21786ca5919f978cd782 100644 (file)
@@ -355,6 +355,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
  *
index 849bb1025d9d483916784378e7beb8370e647e51..dbeee31334b2295db1b4badbfd29e4e940a86bd5 100644 (file)
@@ -104,6 +104,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"