X-Git-Url: http://git.annexia.org/?p=libguestfs.git;a=blobdiff_plain;f=daemon%2Fzero.c;h=df4ebd643e19fdf6dc42c06a834f5d7e3ba71d71;hp=2c20abc3757577e393c4d3bec3fa920f9421fca6;hb=e776a46ffcbede6d9b030dbc8f6ab32500b325ec;hpb=62df226f26bd6ac3c481a7790eb89d760d2f0386 diff --git a/daemon/zero.c b/daemon/zero.c index 2c20abc..df4ebd6 100644 --- a/daemon/zero.c +++ b/daemon/zero.c @@ -1,5 +1,5 @@ /* libguestfs - the guestfsd daemon - * Copyright (C) 2009 Red Hat Inc. + * Copyright (C) 2009 Red Hat Inc. * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -20,6 +20,7 @@ #include #include +#include #include #include #include @@ -33,8 +34,6 @@ do_zero (const char *device) int fd, i; char buf[4096]; - IS_DEVICE (device, -1); - fd = open (device, O_WRONLY); if (fd == -1) { reply_with_perror ("%s", device); @@ -44,9 +43,52 @@ do_zero (const char *device) memset (buf, 0, sizeof buf); for (i = 0; i < 32; ++i) - (void) write (fd, buf, sizeof buf); + if (write (fd, buf, sizeof buf) != sizeof buf) { + reply_with_perror ("write: %s", device); + close (fd); + return -1; + } + + if (close (fd) == -1) { + reply_with_perror ("close: %s", device); + return -1; + } + + return 0; +} + +int +do_zero_device (const char *device) +{ + int64_t size = do_blockdev_getsize64 (device); + if (size == -1) + return -1; - close (fd); + int fd = open (device, O_WRONLY); + if (fd == -1) { + reply_with_perror ("%s", device); + return -1; + } + + char buf[1024*1024]; + memset (buf, 0, sizeof buf); + + while (size > 0) { + size_t n = (size_t) size > sizeof buf ? sizeof buf : (size_t) size; + ssize_t r = write (fd, buf, n); + if (r == -1) { + reply_with_perror ("write: %s (with %" PRId64 " bytes left to write)", + device, size); + close (fd); + return -1; + } + size -= r; + } + + if (close (fd) == -1) { + reply_with_perror ("close: %s", device); + return -1; + } return 0; }