---
configure.ac | 2 +
common/replacements/Makefile.am | 4 ++
common/replacements/pread.h | 44 ++++++++++++++++++
common/replacements/pwrite.h | 44 ++++++++++++++++++
common/replacements/pread.c | 79 +++++++++++++++++++++++++++++++++
common/replacements/pwrite.c | 77 ++++++++++++++++++++++++++++++++
6 files changed, 250 insertions(+)
diff --git a/configure.ac b/configure.ac
index c21855d8..dd536258 100644
--- a/configure.ac
+++ b/configure.ac
@@ -346,6 +346,8 @@ AC_REPLACE_FUNCS([\
getline \
openlog \
poll \
+ pread \
+ pwrite \
realpath \
strndup \
syslog \
diff --git a/common/replacements/Makefile.am b/common/replacements/Makefile.am
index bd884e0a..cd62fbfc 100644
--- a/common/replacements/Makefile.am
+++ b/common/replacements/Makefile.am
@@ -51,6 +51,10 @@ EXTRA_DIST = \
openlog.c \
poll.c \
poll.h \
+ pread.c \
+ pread.h \
+ pwrite.c \
+ pwrite.h \
realpath.c \
realpath.h \
strndup.c \
diff --git a/common/replacements/pread.h b/common/replacements/pread.h
new file mode 100644
index 00000000..cf90c330
--- /dev/null
+++ b/common/replacements/pread.h
@@ -0,0 +1,44 @@
+/* nbdkit
+ * Copyright (C) 2019-2020 Red Hat Inc.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+ *
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ *
+ * * Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * * Neither the name of Red Hat nor the names of its contributors may be
+ * used to endorse or promote products derived from this software without
+ * specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY RED HAT AND CONTRIBUTORS ''AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
+ * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
+ * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL RED HAT OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
+ * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+ * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
+ * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#ifndef NBDKIT_PREAD_H
+#define NBDKIT_PREAD_H
+
+#include <config.h>
+
+#include <unistd.h>
+
+#ifndef HAVE_PREAD
+extern ssize_t pread (int fd, void *buf, size_t count, off_t offset);
+#endif
+
+#endif /* NBDKIT_PREAD_H */
diff --git a/common/replacements/pwrite.h b/common/replacements/pwrite.h
new file mode 100644
index 00000000..bf02c7df
--- /dev/null
+++ b/common/replacements/pwrite.h
@@ -0,0 +1,44 @@
+/* nbdkit
+ * Copyright (C) 2019-2020 Red Hat Inc.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+ *
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ *
+ * * Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * * Neither the name of Red Hat nor the names of its contributors may be
+ * used to endorse or promote products derived from this software without
+ * specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY RED HAT AND CONTRIBUTORS ''AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
+ * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
+ * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL RED HAT OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
+ * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+ * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
+ * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#ifndef NBDKIT_PWRITE_H
+#define NBDKIT_PWRITE_H
+
+#include <config.h>
+
+#include <unistd.h>
+
+#ifndef HAVE_PWRITE
+extern ssize_t pwrite (int fd, const void *buf, size_t count, off_t offset);
+#endif
+
+#endif /* NBDKIT_PWRITE_H */
diff --git a/common/replacements/pread.c b/common/replacements/pread.c
new file mode 100644
index 00000000..065503ed
--- /dev/null
+++ b/common/replacements/pread.c
@@ -0,0 +1,79 @@
+/* nbdkit
+ * Copyright (C) 2019-2020 Red Hat Inc.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+ *
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ *
+ * * Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * * Neither the name of Red Hat nor the names of its contributors may be
+ * used to endorse or promote products derived from this software without
+ * specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY RED HAT AND CONTRIBUTORS ''AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
+ * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
+ * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL RED HAT OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
+ * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+ * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
+ * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+/* Replacement for pread for platforms which lack this function. */
+
+#include <config.h>
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <assert.h>
+#include <errno.h>
+
+#ifndef HAVE_PREAD
+
+#include "pread.h"
+
+#ifdef WIN32
+
+/* Replacement pread for Win32. */
+
+#include <windows.h>
+
+ssize_t
+pread (int fd, void *buf, size_t count, off_t offset)
+{
+ DWORD r;
+ OVERLAPPED ovl;
+
+ memset (&ovl, 0, sizeof ovl);
+ /* Seriously WTF Windows? */
+ ovl.Offset = offset & 0xffffffff;
+ ovl.OffsetHigh = offset >> 32;
+
+ /* XXX Will fail weirdly if count is larger than 32 bits. */
+ if (!ReadFile (_get_osfhandle (fd), buf, count, &r, &ovl)) {
+ if (GetLastError () == ERROR_HANDLE_EOF)
+ return 0;
+ nbdkit_debug ("ReadFile: error %d", GetLastError ());
+ errno = EIO;
+ return -1;
+ }
+
+ return r;
+}
+
+#else /* !WIN32 */
+#error "no replacement pread is available on this platform"
+#endif
+
+#endif /* !HAVE_PREAD */
diff --git a/common/replacements/pwrite.c b/common/replacements/pwrite.c
new file mode 100644
index 00000000..69cffbab
--- /dev/null
+++ b/common/replacements/pwrite.c
@@ -0,0 +1,77 @@
+/* nbdkit
+ * Copyright (C) 2019-2020 Red Hat Inc.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+ *
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ *
+ * * Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * * Neither the name of Red Hat nor the names of its contributors may be
+ * used to endorse or promote products derived from this software without
+ * specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY RED HAT AND CONTRIBUTORS ''AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
+ * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
+ * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL RED HAT OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
+ * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+ * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
+ * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+/* Replacement for pwrite for platforms which lack this function. */
+
+#include <config.h>
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <assert.h>
+#include <errno.h>
+
+#ifndef HAVE_PWRITE
+
+#include "pwrite.h"
+
+#ifdef WIN32
+
+/* Replacement pwrite for Win32. */
+
+#include <windows.h>
+
+ssize_t
+pwrite (int fd, const void *buf, size_t count, off_t offset)
+{
+ DWORD r;
+ OVERLAPPED ovl;
+
+ memset (&ovl, 0, sizeof ovl);
+ /* Seriously WTF Windows? */
+ ovl.Offset = offset & 0xffffffff;
+ ovl.OffsetHigh = offset >> 32;
+
+ /* XXX Will fail weirdly if count is larger than 32 bits. */
+ if (!WriteFile (_get_osfhandle (fd), buf, count, &r, &ovl)) {
+ nbdkit_debug ("WriteFile: error %d", GetLastError ());
+ errno = EIO;
+ return -1;
+ }
+
+ return r;
+}
+
+#else /* !WIN32 */
+#error "no replacement pwrite is available on this platform"
+#endif
+
+#endif /* !HAVE_PWRITE */
--
2.27.0