X-Git-Url: http://git.annexia.org/?a=blobdiff_plain;f=fish%2Falloc.c;h=93cd8afdfe219278241fe70c948939b492e7c486;hb=d5f98968a48cb8edd269e4865c4a851d187b98e3;hp=ad2dccc2ab875c84ce1dde112eb0467c513e02cd;hpb=e1f5472395b08033c60054e8f87f3c61126c4fa4;p=libguestfs.git diff --git a/fish/alloc.c b/fish/alloc.c index ad2dccc..93cd8af 100644 --- a/fish/alloc.c +++ b/fish/alloc.c @@ -24,6 +24,7 @@ #include #include #include +#include #include "fish.h" @@ -54,12 +55,33 @@ do_alloc (const char *cmd, int argc, char *argv[]) return -1; } - if (posix_fallocate (fd, 0, size) == -1) { +#ifdef HAVE_POSIX_FALLOCATE + int err = posix_fallocate (fd, 0, size); + if (err != 0) { + errno = err; 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]);