[libvirt] [PATCH] Fix errno return in safezero()

Most of the safezero() implementations return -1 on error, setting errno. The safezero() impl using posix_fallocate() though returned a positive errno value on error (due to the unusual API contract of posix_fallocate() compared to most syscall APIs). * src/util/util.c: Ensure safezero() returns -1 and sets errno on error. * src/storage/storage_backend.c: Change safezero != 0 to < 0 for detecting errors --- src/storage/storage_backend.c | 4 ++-- src/util/util.c | 6 +++++- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/src/storage/storage_backend.c b/src/storage/storage_backend.c index d269e4c..63e3005 100644 --- a/src/storage/storage_backend.c +++ b/src/storage/storage_backend.c @@ -331,7 +331,7 @@ createRawFile(int fd, virStorageVolDefPtr vol, if (bytes > remain) bytes = remain; - if (safezero(fd, 0, vol->allocation - remain, bytes) != 0) { + if (safezero(fd, 0, vol->allocation - remain, bytes) < 0) { ret = -errno; virReportSystemError(errno, _("cannot fill file '%s'"), vol->target.path); @@ -340,7 +340,7 @@ createRawFile(int fd, virStorageVolDefPtr vol, remain -= bytes; } } else { /* No progress bars to be shown */ - if (safezero(fd, 0, 0, remain) != 0) { + if (safezero(fd, 0, 0, remain) < 0) { ret = -errno; virReportSystemError(errno, _("cannot fill file '%s'"), vol->target.path); diff --git a/src/util/util.c b/src/util/util.c index d00f065..dbfe2ee 100644 --- a/src/util/util.c +++ b/src/util/util.c @@ -136,7 +136,11 @@ safewrite(int fd, const void *buf, size_t count) #ifdef HAVE_POSIX_FALLOCATE int safezero(int fd, int flags ATTRIBUTE_UNUSED, off_t offset, off_t len) { - return posix_fallocate(fd, offset, len); + int ret = posix_fallocate(fd, offset, len); + if (ret == 0) + return 0; + errno = ret; + return -1; } #else -- 1.7.4.4

On 06/14/2011 08:58 AM, Daniel P. Berrange wrote:
Most of the safezero() implementations return -1 on error, setting errno. The safezero() impl using posix_fallocate() though returned a positive errno value on error (due to the unusual API contract of posix_fallocate() compared to most syscall APIs).
* src/util/util.c: Ensure safezero() returns -1 and sets errno on error. * src/storage/storage_backend.c: Change safezero != 0 to < 0 for detecting errors --- src/storage/storage_backend.c | 4 ++-- src/util/util.c | 6 +++++- 2 files changed, 7 insertions(+), 3 deletions(-)
ACK. How'd you spot this? -- Eric Blake eblake@redhat.com +1-801-349-2682 Libvirt virtualization library http://libvirt.org

On Tue, Jun 14, 2011 at 09:05:55AM -0600, Eric Blake wrote:
On 06/14/2011 08:58 AM, Daniel P. Berrange wrote:
Most of the safezero() implementations return -1 on error, setting errno. The safezero() impl using posix_fallocate() though returned a positive errno value on error (due to the unusual API contract of posix_fallocate() compared to most syscall APIs).
* src/util/util.c: Ensure safezero() returns -1 and sets errno on error. * src/storage/storage_backend.c: Change safezero != 0 to < 0 for detecting errors --- src/storage/storage_backend.c | 4 ++-- src/util/util.c | 6 +++++- 2 files changed, 7 insertions(+), 3 deletions(-)
ACK. How'd you spot this?
I was calling it in some other code I have, and wondering why nothing happened to the file on disk. It turns out I was passing an invalid file descriptor, and I never knew because it was not correctly reporting the errors. Daniel -- |: http://berrange.com -o- http://www.flickr.com/photos/dberrange/ :| |: http://libvirt.org -o- http://virt-manager.org :| |: http://autobuild.org -o- http://search.cpan.org/~danberr/ :| |: http://entangle-photo.org -o- http://live.gnome.org/gtk-vnc :|
participants (2)
-
Daniel P. Berrange
-
Eric Blake