[Libguestfs] [PATCH nbdkit 06/13] common/replacements: Add replacement functions for fsync and fdatasync.

Richard W.M. Jones rjones at redhat.com
Thu Aug 20 11:37:39 UTC 2020


---
 configure.ac                        |  2 +
 common/replacements/Makefile.am     |  4 ++
 plugins/partitioning/Makefile.am    |  2 +
 common/replacements/fdatasync.h     | 44 +++++++++++++++++++
 common/replacements/fsync.h         | 44 +++++++++++++++++++
 common/replacements/fdatasync.c     | 51 ++++++++++++++++++++++
 common/replacements/fsync.c         | 66 +++++++++++++++++++++++++++++
 plugins/partitioning/partitioning.c |  5 +--
 8 files changed, 214 insertions(+), 4 deletions(-)

diff --git a/configure.ac b/configure.ac
index 190d35d2..c21855d8 100644
--- a/configure.ac
+++ b/configure.ac
@@ -339,6 +339,8 @@ AC_CHECK_FUNCS([\
 dnl Replacement functions that we provide for some platforms.
 AC_CONFIG_LIBOBJ_DIR([common/replacements])
 AC_REPLACE_FUNCS([\
+	fdatasync \
+	fsync \
 	get_current_dir_name \
 	getdelim \
 	getline \
diff --git a/common/replacements/Makefile.am b/common/replacements/Makefile.am
index 9d1d34fa..bd884e0a 100644
--- a/common/replacements/Makefile.am
+++ b/common/replacements/Makefile.am
@@ -39,6 +39,10 @@ libcompat_la_SOURCES =
 libcompat_la_LIBADD = $(LTLIBOBJS)
 
 EXTRA_DIST = \
+	fdatasync.c \
+	fdatasync.h \
+	fsync.c \
+	fsync.h \
 	get_current_dir_name.c \
 	get_current_dir_name.h \
 	getdelim.c \
diff --git a/plugins/partitioning/Makefile.am b/plugins/partitioning/Makefile.am
index d8cf9e29..9540d6f6 100644
--- a/plugins/partitioning/Makefile.am
+++ b/plugins/partitioning/Makefile.am
@@ -49,6 +49,7 @@ nbdkit_partitioning_plugin_la_CPPFLAGS = \
 	-I$(top_srcdir)/common/gpt \
 	-I$(top_srcdir)/common/include \
 	-I$(top_srcdir)/common/regions \
+	-I$(top_srcdir)/common/replacements \
 	-I$(top_srcdir)/common/utils \
 	-I. \
 	$(NULL)
@@ -61,6 +62,7 @@ nbdkit_partitioning_plugin_la_LIBADD = \
 	$(top_builddir)/common/gpt/libgpt.la \
 	$(top_builddir)/common/regions/libregions.la \
 	$(top_builddir)/common/utils/libutils.la \
+	$(top_builddir)/common/replacements/libcompat.la \
 	$(NULL)
 
 if HAVE_POD
diff --git a/common/replacements/fdatasync.h b/common/replacements/fdatasync.h
new file mode 100644
index 00000000..426e0612
--- /dev/null
+++ b/common/replacements/fdatasync.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_FDATASYNC_H
+#define NBDKIT_FDATASYNC_H
+
+#include <config.h>
+
+#include <unistd.h>
+
+#ifndef HAVE_FDATASYNC
+extern int fdatasync (int fd);
+#endif
+
+#endif /* NBDKIT_FDATASYNC_H */
diff --git a/common/replacements/fsync.h b/common/replacements/fsync.h
new file mode 100644
index 00000000..5561205d
--- /dev/null
+++ b/common/replacements/fsync.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_FSYNC_H
+#define NBDKIT_FSYNC_H
+
+#include <config.h>
+
+#include <unistd.h>
+
+#ifndef HAVE_FSYNC
+extern int fsync (int fd);
+#endif
+
+#endif /* NBDKIT_FSYNC_H */
diff --git a/common/replacements/fdatasync.c b/common/replacements/fdatasync.c
new file mode 100644
index 00000000..1487aad8
--- /dev/null
+++ b/common/replacements/fdatasync.c
@@ -0,0 +1,51 @@
+/* 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 fdatasync for platforms which lack this function. */
+
+#include <config.h>
+
+#include <stdio.h>
+#include <stdlib.h>
+
+#ifndef HAVE_FDATASYNC
+
+#include "fsync.h"
+#include "fdatasync.h"
+
+int
+fdatasync (int fd)
+{
+  return fsync (fd);
+}
+
+#endif /* !HAVE_FDATASYNC */
diff --git a/common/replacements/fsync.c b/common/replacements/fsync.c
new file mode 100644
index 00000000..090532cd
--- /dev/null
+++ b/common/replacements/fsync.c
@@ -0,0 +1,66 @@
+/* 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 fsync for platforms which lack this function. */
+
+#include <config.h>
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <errno.h>
+
+#ifndef HAVE_FSYNC
+
+#include "fsync.h"
+
+#ifdef WIN32
+
+/* Replacement fsync for Win32. */
+
+#include <windows.h>
+
+int
+fsync (int fd)
+{
+  if (!FlushFileBuffers (_get_osfhandle (fd))) {
+    nbdkit_debug ("FlushFileBuffers: error %d", GetLastError ());
+    errno = EIO;
+    return -1;
+  }
+  return 0;
+}
+
+#else /* !WIN32 */
+#error "no replacement fsync is available on this platform"
+#endif
+
+#endif /* !HAVE_FSYNC */
diff --git a/plugins/partitioning/partitioning.c b/plugins/partitioning/partitioning.c
index 5e963026..79b56dd6 100644
--- a/plugins/partitioning/partitioning.c
+++ b/plugins/partitioning/partitioning.c
@@ -50,6 +50,7 @@
 
 #include "ascii-string.h"
 #include "byte-swapping.h"
+#include "fdatasync.h"
 #include "isaligned.h"
 #include "iszero.h"
 #include "rounding.h"
@@ -59,10 +60,6 @@
 #include "regions.h"
 #include "virtual-disk.h"
 
-#ifndef HAVE_FDATASYNC
-#define fdatasync fsync
-#endif
-
 /* Debug flag: -D partitioning.regions=1: Print the regions table. */
 int partitioning_debug_regions;
 
-- 
2.27.0




More information about the Libguestfs mailing list