summaryrefslogtreecommitdiffstats
path: root/fish/alloc.c
diff options
context:
space:
mode:
authorRichard Jones <rich@koneko.home.annexia.org>2010-03-21 19:13:43 +0000
committerRichard Jones <rich@koneko.home.annexia.org>2010-03-22 10:41:45 +0000
commit5ebf3a39340696fea0582a58074d22d11247a6e7 (patch)
tree6689dfcf243bfab0a5a3955e5f5b44a8c3992a56 /fish/alloc.c
parentb2070ae3d797a32a8227c6b9231efa249c94d330 (diff)
downloadlibguestfs-5ebf3a39340696fea0582a58074d22d11247a6e7.tar.gz
libguestfs-5ebf3a39340696fea0582a58074d22d11247a6e7.tar.xz
libguestfs-5ebf3a39340696fea0582a58074d22d11247a6e7.zip
Mac OS X: provide alternate implementation of posix_fallocate.
Diffstat (limited to 'fish/alloc.c')
-rw-r--r--fish/alloc.c19
1 files changed, 19 insertions, 0 deletions
diff --git a/fish/alloc.c b/fish/alloc.c
index ad2dccc2..28c990f1 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]);