[Libguestfs] [PATCH nbdkit 10/13] file: Port the file plugin to Windows.

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


---
 configure.ac             |  1 +
 plugins/file/Makefile.am |  2 ++
 plugins/file/file.c      | 23 ++++++++++++++++++-----
 TODO                     |  8 +++++---
 4 files changed, 26 insertions(+), 8 deletions(-)

diff --git a/configure.ac b/configure.ac
index faae1ca7..7060f445 100644
--- a/configure.ac
+++ b/configure.ac
@@ -322,6 +322,7 @@ AC_CHECK_HEADERS([\
 	stdatomic.h \
 	syslog.h \
 	sys/endian.h \
+	sys/ioctl.h \
 	sys/mman.h \
 	sys/prctl.h \
 	sys/procctl.h \
diff --git a/plugins/file/Makefile.am b/plugins/file/Makefile.am
index a067d0fc..24c443c1 100644
--- a/plugins/file/Makefile.am
+++ b/plugins/file/Makefile.am
@@ -43,6 +43,7 @@ nbdkit_file_plugin_la_SOURCES = \
 nbdkit_file_plugin_la_CPPFLAGS = \
 	-I$(top_srcdir)/include \
 	-I$(top_srcdir)/common/include \
+	-I$(top_srcdir)/common/replacements \
 	-I$(top_srcdir)/common/utils \
 	$(NULL)
 nbdkit_file_plugin_la_CFLAGS = $(WARNINGS_CFLAGS)
@@ -52,6 +53,7 @@ nbdkit_file_plugin_la_LDFLAGS = \
 	$(NULL)
 nbdkit_file_plugin_la_LIBADD = \
 	$(top_builddir)/common/utils/libutils.la \
+	$(top_builddir)/common/replacements/libcompat.la \
 	$(IMPORT_LIBRARY_ON_WINDOWS) \
 	$(NULL)
 
diff --git a/plugins/file/file.c b/plugins/file/file.c
index 08418194..b8c72e77 100644
--- a/plugins/file/file.c
+++ b/plugins/file/file.c
@@ -39,10 +39,13 @@
 #include <inttypes.h>
 #include <fcntl.h>
 #include <unistd.h>
+#include <errno.h>
 #include <sys/types.h>
 #include <sys/stat.h>
+
+#ifdef HAVE_SYS_IOCTL_H
 #include <sys/ioctl.h>
-#include <errno.h>
+#endif
 
 #include <pthread.h>
 
@@ -59,10 +62,10 @@
 
 #include "cleanup.h"
 #include "isaligned.h"
-
-#ifndef HAVE_FDATASYNC
-#define fdatasync fsync
-#endif
+#include "fdatasync.h"
+#include "pread.h"
+#include "pwrite.h"
+#include "windows-compat.h"
 
 static char *filename = NULL;
 static char *directory = NULL;
@@ -116,6 +119,7 @@ file_config (const char *key, const char *value)
     if (!filename)
       return -1;
   }
+#ifndef WIN32
   else if (strcmp (key, "directory") == 0 ||
            strcmp (key, "dir") == 0) {
     free (directory);
@@ -123,6 +127,7 @@ file_config (const char *key, const char *value)
     if (!directory)
       return -1;
   }
+#endif
   else if (strcmp (key, "fadvise") == 0) {
     /* As this is a hint, if the kernel doesn't support the feature
      * ignore the parameter.
@@ -263,6 +268,7 @@ file_open (int readonly)
   int dfd = -1;
 
   if (directory) {
+#ifndef WIN32
     file = nbdkit_export_name ();
     if (strchr (file, '/')) {
       nbdkit_error ("exportname cannot contain /");
@@ -274,6 +280,9 @@ file_open (int readonly)
       nbdkit_error ("open %s: %m", directory);
       return NULL;
     }
+#else
+    abort ();
+#endif
   }
   else
     file = filename;
@@ -290,7 +299,11 @@ file_open (int readonly)
   else
     flags |= O_RDWR;
 
+#ifndef WIN32
   h->fd = openat (dfd, file, flags);
+#else
+  h->fd = open (file, flags);
+#endif
   if (h->fd == -1) {
     nbdkit_error ("openat: %s: %m", file);
     if (dfd != -1)
diff --git a/TODO b/TODO
index c3314d37..b2b89670 100644
--- a/TODO
+++ b/TODO
@@ -325,9 +325,11 @@ Currently many features are missing, including:
 * For possible Unix domain socket support in future see:
   https://devblogs.microsoft.com/commandline/af_unix-comes-to-windows/
 
-* The file plugin.  The current file plugin is essentially POSIX-only.
-  We would like to eventually write an alternate file plugin which
-  uses Windows APIs.
+* The current file plugin works but has limited features and doesn't
+  use the best native APIs.  It's worth considering writing an
+  alternate implementation which is used for Windows and uses all
+  native APIs and features.  Also this should be extended to allow
+  exporting block devices.
 
 * Many other plugins and filters.
 
-- 
2.27.0




More information about the Libguestfs mailing list