]> granicus.if.org Git - postgresql/commitdiff
Provide pg_pread() and pg_pwrite() for random I/O.
authorThomas Munro <tmunro@postgresql.org>
Tue, 6 Nov 2018 20:50:01 +0000 (09:50 +1300)
committerThomas Munro <tmunro@postgresql.org>
Tue, 6 Nov 2018 20:50:01 +0000 (09:50 +1300)
Forward to POSIX pread() and pwrite(), or emulate them if unavailable.
The emulation is not perfect as the file position is changed, so
we'll put pg_ prefixes on the names to minimize the risk of confusion
in future patches that might inadvertently try to mix pread() and read()
on the same file descriptor.

Author: Thomas Munro
Reviewed-by: Tom Lane, Jesper Pedersen
Discussion: https://postgr.es/m/CAEepm=02rapCpPR3ZGF2vW=SBHSdFYO_bz_f-wwWJonmA3APgw@mail.gmail.com

configure
configure.in
src/include/pg_config.h.in
src/include/pg_config.h.win32
src/include/port.h
src/port/pread.c [new file with mode: 0644]
src/port/pwrite.c [new file with mode: 0644]
src/tools/msvc/Mkvcbuild.pm

index 748abaffc2b07b3ebc6396683c2f88e8fdad7fb3..a600fdfac3814a66421a92f086cd4cfc5d7b869d 100755 (executable)
--- a/configure
+++ b/configure
@@ -15539,6 +15539,32 @@ esac
 
 fi
 
+ac_fn_c_check_func "$LINENO" "pread" "ac_cv_func_pread"
+if test "x$ac_cv_func_pread" = xyes; then :
+  $as_echo "#define HAVE_PREAD 1" >>confdefs.h
+
+else
+  case " $LIBOBJS " in
+  *" pread.$ac_objext "* ) ;;
+  *) LIBOBJS="$LIBOBJS pread.$ac_objext"
+ ;;
+esac
+
+fi
+
+ac_fn_c_check_func "$LINENO" "pwrite" "ac_cv_func_pwrite"
+if test "x$ac_cv_func_pwrite" = xyes; then :
+  $as_echo "#define HAVE_PWRITE 1" >>confdefs.h
+
+else
+  case " $LIBOBJS " in
+  *" pwrite.$ac_objext "* ) ;;
+  *) LIBOBJS="$LIBOBJS pwrite.$ac_objext"
+ ;;
+esac
+
+fi
+
 ac_fn_c_check_func "$LINENO" "random" "ac_cv_func_random"
 if test "x$ac_cv_func_random" = xyes; then :
   $as_echo "#define HAVE_RANDOM 1" >>confdefs.h
index 09b23e9821d16aac3aff9aa29483306d263caf26..75dbe8e734bd96679a0a176da045e0d0f1d648cd 100644 (file)
@@ -1698,6 +1698,8 @@ AC_REPLACE_FUNCS(m4_normalize([
        getrusage
        inet_aton
        mkdtemp
+       pread
+       pwrite
        random
        rint
        srandom
index 9798bd24b447661db7d2e32efb7a7142acbf8114..5a996e75572203de7b73d240c3a8937631553b0c 100644 (file)
 /* Define to 1 if you have the `ppoll' function. */
 #undef HAVE_PPOLL
 
+/* Define to 1 if you have the `pread' function. */
+#undef HAVE_PREAD
+
 /* Define to 1 if you have the `pstat' function. */
 #undef HAVE_PSTAT
 
 /* Have PTHREAD_PRIO_INHERIT. */
 #undef HAVE_PTHREAD_PRIO_INHERIT
 
+/* Define to 1 if you have the `pwrite' function. */
+#undef HAVE_PWRITE
+
 /* Define to 1 if you have the `random' function. */
 #undef HAVE_RANDOM
 
index f7a051d11270f60e9cae8118cc86901cf71f17db..894d658a204e3a817e854e7dd915aba6417e40f9 100644 (file)
 /* Define to 1 if you have the `ppoll' function. */
 /* #undef HAVE_PPOLL */
 
+/* Define to 1 if you have the `pread' function. */
+/* #undef HAVE_PREAD */
+
 /* Define to 1 if you have the `pstat' function. */
 /* #undef HAVE_PSTAT */
 
 /* Define to 1 if the PS_STRINGS thing exists. */
 /* #undef HAVE_PS_STRINGS */
 
+/* Define to 1 if you have the `pwrite' function. */
+/* #undef HAVE_PWRITE */
+
 /* Define to 1 if you have the `random' function. */
 /* #undef HAVE_RANDOM */
 
index 3a53bcf2e4b5ef1d7dbb49bf16cec1d417320730..81583d557cd1cef060f686643978cf69577c290d 100644 (file)
@@ -392,6 +392,23 @@ extern double rint(double x);
 extern int     inet_aton(const char *cp, struct in_addr *addr);
 #endif
 
+/*
+ * Windows and older Unix don't have pread(2) and pwrite(2).  We have
+ * replacement functions, but they have slightly different semantics so we'll
+ * use a name with a pg_ prefix to avoid confusion.
+ */
+#ifdef HAVE_PREAD
+#define pg_pread pread
+#else
+extern ssize_t pg_pread(int fd, void *buf, size_t nbyte, off_t offset);
+#endif
+
+#ifdef HAVE_PWRITE
+#define pg_pwrite pwrite
+#else
+extern ssize_t pg_pwrite(int fd, const void *buf, size_t nbyte, off_t offset);
+#endif
+
 #if !HAVE_DECL_STRLCAT
 extern size_t strlcat(char *dst, const char *src, size_t siz);
 #endif
diff --git a/src/port/pread.c b/src/port/pread.c
new file mode 100644 (file)
index 0000000..a22d949
--- /dev/null
@@ -0,0 +1,55 @@
+/*-------------------------------------------------------------------------
+ *
+ * pread.c
+ *       Implementation of pread(2) for platforms that lack one.
+ *
+ * Portions Copyright (c) 1996-2018, PostgreSQL Global Development Group
+ *
+ * IDENTIFICATION
+ *       src/port/pread.c
+ *
+ * Note that this implementation changes the current file position, unlike
+ * the POSIX function, so we use the name pg_pread().
+ *
+ *-------------------------------------------------------------------------
+ */
+
+
+#include "postgres.h"
+
+#ifdef WIN32
+#include <windows.h>
+#else
+#include <unistd.h>
+#endif
+
+ssize_t
+pg_pread(int fd, void *buf, size_t size, off_t offset)
+{
+#ifdef WIN32
+       OVERLAPPED      overlapped = {0};
+       HANDLE          handle;
+       DWORD           result;
+
+       handle = (HANDLE) _get_osfhandle(fd);
+       if (handle == INVALID_HANDLE_VALUE)
+       {
+               errno = EBADF;
+               return -1;
+       }
+
+       overlapped.Offset = offset;
+       if (!ReadFile(handle, buf, size, &result, &overlapped))
+       {
+               _dosmaperr(GetLastError());
+               return -1;
+       }
+
+       return result;
+#else
+       if (lseek(fd, offset, SEEK_SET) < 0)
+               return -1;
+
+       return read(fd, buf, size);
+#endif
+}
diff --git a/src/port/pwrite.c b/src/port/pwrite.c
new file mode 100644 (file)
index 0000000..f3e228c
--- /dev/null
@@ -0,0 +1,55 @@
+/*-------------------------------------------------------------------------
+ *
+ * pwrite.c
+ *       Implementation of pwrite(2) for platforms that lack one.
+ *
+ * Portions Copyright (c) 1996-2018, PostgreSQL Global Development Group
+ *
+ * IDENTIFICATION
+ *       src/port/pwrite.c
+ *
+ * Note that this implementation changes the current file position, unlike
+ * the POSIX function, so we use the name pg_write().
+ *
+ *-------------------------------------------------------------------------
+ */
+
+
+#include "postgres.h"
+
+#ifdef WIN32
+#include <windows.h>
+#else
+#include <unistd.h>
+#endif
+
+ssize_t
+pg_pwrite(int fd, const void *buf, size_t size, off_t offset)
+{
+#ifdef WIN32
+       OVERLAPPED      overlapped = {0};
+       HANDLE          handle;
+       DWORD           result;
+
+       handle = (HANDLE) _get_osfhandle(fd);
+       if (handle == INVALID_HANDLE_VALUE)
+       {
+               errno = EBADF;
+               return -1;
+       }
+
+       overlapped.Offset = offset;
+       if (!WriteFile(handle, buf, size, &result, &overlapped))
+       {
+               _dosmaperr(GetLastError());
+               return -1;
+       }
+
+       return result;
+#else
+       if (lseek(fd, offset, SEEK_SET) < 0)
+               return -1;
+
+       return write(fd, buf, size);
+#endif
+}
index 708579d9dfb8829882f8bf2c026ce39e8a95ef37..b562044fa716c5a83a4f1e334cfb47674f41e5e3 100644 (file)
@@ -97,6 +97,7 @@ sub mkvcbuild
          srandom.c getaddrinfo.c gettimeofday.c inet_net_ntop.c kill.c open.c
          erand48.c snprintf.c strlcat.c strlcpy.c dirmod.c noblock.c path.c
          dirent.c dlopen.c getopt.c getopt_long.c
+         pread.c pwrite.c
          pg_strong_random.c pgcheckdir.c pgmkdirp.c pgsleep.c pgstrcasecmp.c
          pqsignal.c mkdtemp.c qsort.c qsort_arg.c quotes.c system.c
          sprompt.c strerror.c tar.c thread.c