diff options
author | Richard W.M. Jones <rjones@redhat.com> | 2011-05-17 22:06:00 +0100 |
---|---|---|
committer | Richard W.M. Jones <rjones@redhat.com> | 2011-05-17 22:06:00 +0100 |
commit | 598484729632d1207dc3e1493f27499de18d2242 (patch) | |
tree | d67ef79604272e75c07fb1f2f24fd3abdd60a469 /daemon | |
parent | ef6f877b9df261fad1fbf361b8ce7af1055dfbb7 (diff) | |
download | libguestfs-598484729632d1207dc3e1493f27499de18d2242.tar.gz libguestfs-598484729632d1207dc3e1493f27499de18d2242.tar.xz libguestfs-598484729632d1207dc3e1493f27499de18d2242.zip |
New APIs: is-zero and is-zero-device, to test if file or device is all zeroes.
Diffstat (limited to 'daemon')
-rw-r--r-- | daemon/zero.c | 75 |
1 files changed, 74 insertions, 1 deletions
diff --git a/daemon/zero.c b/daemon/zero.c index 8fbd963d..c8e79ae9 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; +} |