[Libguestfs] [PATCH 1/4] Mac OS X: provide alternate implementation of posix_fallocate.

Richard W.M. Jones rjones at redhat.com
Sun Mar 21 19:34:31 UTC 2010


-- 
Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones
libguestfs lets you edit virtual machines.  Supports shell scripting,
bindings from many languages.  http://et.redhat.com/~rjones/libguestfs/
See what it can do: http://et.redhat.com/~rjones/libguestfs/recipes.html
-------------- next part --------------
>From 78ae8a9c13debf40f6a55efeb69ef1287907b2e4 Mon Sep 17 00:00:00 2001
From: Richard Jones <rich at koneko.home.annexia.org>
Date: Sun, 21 Mar 2010 19:13:43 +0000
Subject: [PATCH 1/4] Mac OS X: provide alternate implementation of posix_fallocate.

---
 configure.ac |    3 +++
 fish/alloc.c |   19 +++++++++++++++++++
 2 files changed, 22 insertions(+), 0 deletions(-)

diff --git a/configure.ac b/configure.ac
index b13eaf9..cfad704 100644
--- a/configure.ac
+++ b/configure.ac
@@ -134,6 +134,9 @@ AC_DEFINE_UNQUOTED([host_cpu],["$host_cpu"],[Host architecture.])
 dnl Headers.
 AC_CHECK_HEADERS([errno.h sys/types.h sys/un.h sys/wait.h sys/socket.h endian.h byteswap.h])
 
+dnl Functions.
+AC_CHECK_FUNCS([posix_fallocate])
+
 dnl Build the daemon?
 AC_MSG_CHECKING([if we should build the daemon])
 AC_ARG_ENABLE([daemon],
diff --git a/fish/alloc.c b/fish/alloc.c
index ad2dccc..28c990f 100644
--- a/fish/alloc.c
+++ b/fish/alloc.c
@@ -54,12 +54,31 @@ do_alloc (const char *cmd, int argc, char *argv[])
     return -1;
   }
 
+#ifdef HAVE_POSIX_FALLOCATE
   if (posix_fallocate (fd, 0, size) == -1) {
     perror ("fallocate");
     close (fd);
     unlink (argv[0]);
     return -1;
   }
+#else
+  /* Slow emulation of posix_fallocate on platforms which don't have it. */
+  char buffer[BUFSIZ];
+  memset (buffer, 0, sizeof buffer);
+
+  size_t remaining = size;
+  while (remaining > 0) {
+    size_t n = remaining > sizeof buffer ? sizeof buffer : remaining;
+    ssize_t r = write (fd, buffer, n);
+    if (r == -1) {
+      perror ("write");
+      close (fd);
+      unlink (argv[0]);
+      return -1;
+    }
+    remaining -= r;
+  }
+#endif
 
   if (close (fd) == -1) {
     perror (argv[0]);
-- 
1.6.4.1



More information about the Libguestfs mailing list