aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/include/c.h2
-rw-r--r--src/include/port/win32_port.h6
-rw-r--r--src/include/port/win32ntdll.h10
-rw-r--r--src/port/fdatasync.c53
-rw-r--r--src/port/win32ntdll.c6
-rw-r--r--src/tools/msvc/Mkvcbuild.pm3
-rw-r--r--src/tools/msvc/Solution.pm2
7 files changed, 77 insertions, 5 deletions
diff --git a/src/include/c.h b/src/include/c.h
index 2cc2784750e..d35405f191a 100644
--- a/src/include/c.h
+++ b/src/include/c.h
@@ -1290,7 +1290,7 @@ typedef union PGAlignedXLogBlock
* standard C library.
*/
-#if defined(HAVE_FDATASYNC) && !HAVE_DECL_FDATASYNC
+#if !HAVE_DECL_FDATASYNC
extern int fdatasync(int fildes);
#endif
diff --git a/src/include/port/win32_port.h b/src/include/port/win32_port.h
index 5121c0c626e..5ea66528fad 100644
--- a/src/include/port/win32_port.h
+++ b/src/include/port/win32_port.h
@@ -83,6 +83,12 @@
#define HAVE_FSYNC_WRITETHROUGH
#define FSYNC_WRITETHROUGH_IS_FSYNC
+/*
+ * We have a replacement for fdatasync() in src/port/fdatasync.c, which is
+ * unconditionally used by MSVC and Mingw builds.
+ */
+#define HAVE_FDATASYNC
+
#define USES_WINSOCK
/*
diff --git a/src/include/port/win32ntdll.h b/src/include/port/win32ntdll.h
index 291b067ea4f..34cebddd542 100644
--- a/src/include/port/win32ntdll.h
+++ b/src/include/port/win32ntdll.h
@@ -23,9 +23,17 @@
#include <ntstatus.h>
#include <winternl.h>
-typedef NTSTATUS (__stdcall * RtlGetLastNtStatus_t) (void);
+#ifndef FLUSH_FLAGS_FILE_DATA_SYNC_ONLY
+#define FLUSH_FLAGS_FILE_DATA_SYNC_ONLY 0x4
+#endif
+
+typedef NTSTATUS (__stdcall *RtlGetLastNtStatus_t) (void);
+typedef ULONG (__stdcall *RtlNtStatusToDosError_t) (NTSTATUS);
+typedef NTSTATUS (__stdcall *NtFlushBuffersFileEx_t) (HANDLE, ULONG, PVOID, ULONG, PIO_STATUS_BLOCK);
extern PGDLLIMPORT RtlGetLastNtStatus_t pg_RtlGetLastNtStatus;
+extern PGDLLIMPORT RtlNtStatusToDosError_t pg_RtlNtStatusToDosError;
+extern PGDLLIMPORT NtFlushBuffersFileEx_t pg_NtFlushBuffersFileEx;
extern int initialize_ntdll(void);
diff --git a/src/port/fdatasync.c b/src/port/fdatasync.c
new file mode 100644
index 00000000000..afef853aa3c
--- /dev/null
+++ b/src/port/fdatasync.c
@@ -0,0 +1,53 @@
+/*-------------------------------------------------------------------------
+ *
+ * fdatasync.c
+ * Win32 fdatasync() replacement
+ *
+ *
+ * Portions Copyright (c) 1996-2021, PostgreSQL Global Development Group
+ *
+ * src/port/fdatasync.c
+ *
+ *-------------------------------------------------------------------------
+ */
+
+#define UMDF_USING_NTSTATUS
+
+#ifdef FRONTEND
+#include "postgres_fe.h"
+#else
+#include "postgres.h"
+#endif
+
+#include "port/win32ntdll.h"
+
+int
+fdatasync(int fd)
+{
+ IO_STATUS_BLOCK iosb;
+ NTSTATUS status;
+ HANDLE handle;
+
+ handle = (HANDLE) _get_osfhandle(fd);
+ if (handle == INVALID_HANDLE_VALUE)
+ {
+ errno = EBADF;
+ return -1;
+ }
+
+ if (initialize_ntdll() < 0)
+ return -1;
+
+ memset(&iosb, 0, sizeof(iosb));
+ status = pg_NtFlushBuffersFileEx(handle,
+ FLUSH_FLAGS_FILE_DATA_SYNC_ONLY,
+ NULL,
+ 0,
+ &iosb);
+
+ if (NT_SUCCESS(status))
+ return 0;
+
+ _dosmaperr(pg_RtlNtStatusToDosError(status));
+ return -1;
+}
diff --git a/src/port/win32ntdll.c b/src/port/win32ntdll.c
index 10c33c6a01d..eb614077543 100644
--- a/src/port/win32ntdll.c
+++ b/src/port/win32ntdll.c
@@ -20,6 +20,8 @@
#include "port/win32ntdll.h"
RtlGetLastNtStatus_t pg_RtlGetLastNtStatus;
+RtlNtStatusToDosError_t pg_RtlNtStatusToDosError;
+NtFlushBuffersFileEx_t pg_NtFlushBuffersFileEx;
typedef struct NtDllRoutine
{
@@ -28,7 +30,9 @@ typedef struct NtDllRoutine
} NtDllRoutine;
static const NtDllRoutine routines[] = {
- {"RtlGetLastNtStatus", (pg_funcptr_t *) &pg_RtlGetLastNtStatus}
+ {"RtlGetLastNtStatus", (pg_funcptr_t *) &pg_RtlGetLastNtStatus},
+ {"RtlNtStatusToDosError", (pg_funcptr_t *) &pg_RtlNtStatusToDosError},
+ {"NtFlushBuffersFileEx", (pg_funcptr_t *) &pg_NtFlushBuffersFileEx}
};
static bool initialized;
diff --git a/src/tools/msvc/Mkvcbuild.pm b/src/tools/msvc/Mkvcbuild.pm
index e4feda10fd8..cc7a908d10a 100644
--- a/src/tools/msvc/Mkvcbuild.pm
+++ b/src/tools/msvc/Mkvcbuild.pm
@@ -99,7 +99,8 @@ sub mkvcbuild
$solution = CreateSolution($vsVersion, $config);
our @pgportfiles = qw(
- chklocale.c explicit_bzero.c fls.c getpeereid.c getrusage.c inet_aton.c
+ chklocale.c explicit_bzero.c fls.c fdatasync.c
+ getpeereid.c getrusage.c inet_aton.c
getaddrinfo.c gettimeofday.c inet_net_ntop.c kill.c open.c
snprintf.c strlcat.c strlcpy.c dirmod.c noblock.c path.c
dirent.c dlopen.c getopt.c getopt_long.c link.c
diff --git a/src/tools/msvc/Solution.pm b/src/tools/msvc/Solution.pm
index 1e125aef942..3ddcd024a78 100644
--- a/src/tools/msvc/Solution.pm
+++ b/src/tools/msvc/Solution.pm
@@ -257,7 +257,7 @@ sub GenerateFiles
HAVE_EDITLINE_READLINE_H => undef,
HAVE_EXECINFO_H => undef,
HAVE_EXPLICIT_BZERO => undef,
- HAVE_FDATASYNC => undef,
+ HAVE_FDATASYNC => 1,
HAVE_FLS => undef,
HAVE_FSEEKO => 1,
HAVE_FUNCNAME__FUNC => undef,