X-Git-Url: http://git.annexia.org/?a=blobdiff_plain;f=daemon%2Fzero.c;h=c8e79ae94c169b443409adbbfaca3d78ad524952;hb=f2c7df589c7e161f33b46d05a943b5489dc2cffe;hp=8fbd963d8e5cbb6ea0e076003f8a50eb6a50c300;hpb=133a92be6948ff7e65bfecb7be70b15f4336d2af;p=libguestfs.git diff --git a/daemon/zero.c b/daemon/zero.c index 8fbd963..c8e79ae 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-2011 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 @@ -105,3 +105,76 @@ do_zero_device (const char *device) return 0; } + +static char zero[BUFSIZ]; + +int +do_is_zero (const char *path) +{ + int fd; + char buf[BUFSIZ]; + ssize_t r; + + CHROOT_IN; + fd = open (path, O_RDONLY); + CHROOT_OUT; + + if (fd == -1) { + reply_with_perror ("open: %s", path); + return -1; + } + + while ((r = read (fd, buf, sizeof buf)) > 0) { + if (memcmp (buf, zero, r) != 0) { + close (fd); + return 0; + } + } + + if (r == -1) { + reply_with_perror ("read: %s", path); + close (fd); + return -1; + } + + if (close (fd) == -1) { + reply_with_perror ("close: %s", path); + return -1; + } + + return 1; +} + +int +do_is_zero_device (const char *device) +{ + int fd; + char buf[BUFSIZ]; + ssize_t r; + + fd = open (device, O_RDONLY); + if (fd == -1) { + reply_with_perror ("open: %s", device); + return -1; + } + + while ((r = read (fd, buf, sizeof buf)) > 0) { + if (memcmp (buf, zero, r) != 0) { + close (fd); + return 0; + } + } + + if (r == -1) { + reply_with_perror ("read: %s", device); + close (fd); + return -1; + } + + if (close (fd) == -1) { + reply_with_perror ("close: %s", device); + return -1; + } + + return 1; +}