X-Git-Url: http://git.annexia.org/?a=blobdiff_plain;f=daemon%2Ffallocate.c;h=7f17f8b85d6824dba4e9604468a682bec93ad2bc;hb=f5d52fa3c00e525cd5d86089b85f970a5c4e7c21;hp=47e84c6970b5162e51c1c82357ccf569b6331816;hpb=0e6e32025885286e2de39efca34ced2aaf26b3a3;p=libguestfs.git diff --git a/daemon/fallocate.c b/daemon/fallocate.c index 47e84c6..7f17f8b 100644 --- a/daemon/fallocate.c +++ b/daemon/fallocate.c @@ -28,27 +28,45 @@ #include "actions.h" int -do_fallocate (char *path, int len) +do_fallocate (const char *path, int len) { - int fd, r; - - NEED_ROOT (-1); - ABS_PATH (path, -1); + int fd; CHROOT_IN; fd = open (path, O_WRONLY | O_CREAT | O_TRUNC | O_NOCTTY, 0666); CHROOT_OUT; if (fd == -1) { - reply_with_perror (path); + reply_with_perror ("open: %s", path); return -1; } +#ifdef HAVE_POSIX_FALLOCATE + int r; + r = posix_fallocate (fd, 0, len); if (r == -1) { - reply_with_perror ("posix_fallocate: %s", path); + reply_with_perror ("%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);