[Libguestfs] [PATCH] daemon: Alternate implementation of posix_fallocate.

Richard W.M. Jones rjones at redhat.com
Wed Nov 25 11:43:23 UTC 2009


-- 
Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones
New in Fedora 11: Fedora Windows cross-compiler. Compile Windows
programs, test, and build Windows installers. Over 70 libraries supprt'd
http://fedoraproject.org/wiki/MinGW http://www.annexia.org/fedora_mingw
-------------- next part --------------
>From f6df273d53ba46042856ebe6b2c640c9b715a51b Mon Sep 17 00:00:00 2001
From: Richard Jones <rjones at redhat.com>
Date: Tue, 24 Nov 2009 16:16:08 +0000
Subject: [PATCH 3/4] daemon: Alternate implementation of posix_fallocate.

If the posix_fallocate function is not available [ie. Windows]
use an alternate implementation that just loops and writes.
---
 daemon/configure.ac |    1 +
 daemon/fallocate.c  |   23 ++++++++++++++++++++++-
 2 files changed, 23 insertions(+), 1 deletions(-)

diff --git a/daemon/configure.ac b/daemon/configure.ac
index 54d2a4c..7bcdd7e 100644
--- a/daemon/configure.ac
+++ b/daemon/configure.ac
@@ -179,6 +179,7 @@ AC_CHECK_FUNCS([\
         lsetxattr \
         lremovexattr \
         mknod \
+        posix_fallocate \
         removexattr \
         setxattr])
 
diff --git a/daemon/fallocate.c b/daemon/fallocate.c
index 20a75e6..1800292 100644
--- a/daemon/fallocate.c
+++ b/daemon/fallocate.c
@@ -30,7 +30,7 @@
 int
 do_fallocate (const char *path, int len)
 {
-  int fd, r;
+  int fd;
 
   CHROOT_IN;
   fd = open (path, O_WRONLY | O_CREAT | O_TRUNC | O_NOCTTY, 0666);
@@ -40,12 +40,33 @@ do_fallocate (const char *path, int len)
     return -1;
   }
 
+#ifdef HAVE_POSIX_FALLOCATE
+  int r;
+
   r = posix_fallocate (fd, 0, len);
   if (r == -1) {
     reply_with_perror ("posix_fallocate: %s", path);
     close (fd);
     return -1;
   }
+#else
+  ssize_t r;
+  char buf[BUFSIZ];
+  const size_t len_sz = (size_t) len;
+  size_t n;
+
+  memset (buf, 0, BUFSIZ);
+  n = 0;
+  while (n < len_sz) {
+    r = write (fd, buf, len_sz - n < BUFSIZ ? len_sz - n : BUFSIZ);
+    if (r == -1) {
+      reply_with_perror ("write: %s", path);
+      close (fd);
+      return -1;
+    }
+    n += r;
+  }
+#endif
 
   if (close (fd) == -1) {
     reply_with_perror ("close: %s", path);
-- 
1.6.5.2



More information about the Libguestfs mailing list