Richard W.M. Jones
2009-Nov-25 12:19 UTC
[Libguestfs] [PATCH] daemon/Win32: Replace pread on platforms that don't have this function.
-- Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones libguestfs lets you edit virtual machines. Supports shell scripting, bindings from many languages. http://et.redhat.com/~rjones/libguestfs/ See what it can do: http://et.redhat.com/~rjones/libguestfs/recipes.html -------------- next part -------------->From 81982306dd5e8beb56e193ed69d840a1cb976d62 Mon Sep 17 00:00:00 2001From: Richard Jones <rjones at redhat.com> Date: Wed, 25 Nov 2009 11:59:20 +0000 Subject: [PATCH 1/2] daemon/Win32: Replace pread on platforms that don't have this function. --- daemon/configure.ac | 1 + daemon/file.c | 20 ++++++++++++++++++++ 2 files changed, 21 insertions(+), 0 deletions(-) diff --git a/daemon/configure.ac b/daemon/configure.ac index 89bd800..e70f05e 100644 --- a/daemon/configure.ac +++ b/daemon/configure.ac @@ -180,6 +180,7 @@ AC_CHECK_FUNCS([\ lremovexattr \ mknod \ posix_fallocate \ + pread \ removexattr \ setxattr \ sync]) diff --git a/daemon/file.c b/daemon/file.c index 252c02c..e6bbc19 100644 --- a/daemon/file.c +++ b/daemon/file.c @@ -402,6 +402,7 @@ do_pread (const char *path, int count, int64_t offset, size_t *size_r) return NULL; } +#ifdef HAVE_PREAD r = pread (fd, buf, count, offset); if (r == -1) { reply_with_perror ("pread: %s", path); @@ -409,6 +410,25 @@ do_pread (const char *path, int count, int64_t offset, size_t *size_r) free (buf); return NULL; } +#else + if (lseek (fd, offset, SEEK_SET) == (off_t) -1) { + reply_with_perror ("lseek: %s", path); + close (fd); + free (buf); + return NULL; + } + r = read (fd, buf, count); + if (r == -1) { + reply_with_perror ("read: %s", path); + close (fd); + free (buf); + return NULL; + } + /* The real pread function would reset the file offset here, + * but we don't care since we're just about to close the + * file anyway. + */ +#endif /* !HAVE_PREAD */ if (close (fd) == -1) { reply_with_perror ("close: %s", path); -- 1.6.5.2
Jim Meyering
2009-Nov-25 13:44 UTC
[Libguestfs] [PATCH] daemon/Win32: Replace pread on platforms that don't have this function.
Richard W.M. Jones wrote:> Subject: [PATCH 1/2] daemon/Win32: Replace pread on platforms that don't have this function. > > --- > daemon/configure.ac | 1 + > daemon/file.c | 20 ++++++++++++++++++++ > 2 files changed, 21 insertions(+), 0 deletions(-) > > diff --git a/daemon/configure.ac b/daemon/configure.ac > index 89bd800..e70f05e 100644 > --- a/daemon/configure.ac > +++ b/daemon/configure.ac > @@ -180,6 +180,7 @@ AC_CHECK_FUNCS([\ > lremovexattr \ > mknod \ > posix_fallocate \ > + pread \ > removexattr \ > setxattr \ > sync])I'm adding a pread module to gnulib right now. Once that's in, you can drop the rest of this patch, below.> diff --git a/daemon/file.c b/daemon/file.c > index 252c02c..e6bbc19 100644 > --- a/daemon/file.c > +++ b/daemon/file.c > @@ -402,6 +402,7 @@ do_pread (const char *path, int count, int64_t offset, size_t *size_r) > return NULL; > } > > +#ifdef HAVE_PREAD > r = pread (fd, buf, count, offset); > if (r == -1) { > reply_with_perror ("pread: %s", path); > @@ -409,6 +410,25 @@ do_pread (const char *path, int count, int64_t offset, size_t *size_r) > free (buf); > return NULL; > } > +#else > + if (lseek (fd, offset, SEEK_SET) == (off_t) -1) { > + reply_with_perror ("lseek: %s", path); > + close (fd); > + free (buf); > + return NULL; > + } > + r = read (fd, buf, count); > + if (r == -1) { > + reply_with_perror ("read: %s", path); > + close (fd); > + free (buf); > + return NULL; > + } > + /* The real pread function would reset the file offset here, > + * but we don't care since we're just about to close the > + * file anyway. > + */ > +#endif /* !HAVE_PREAD */ > > if (close (fd) == -1) { > reply_with_perror ("close: %s", path);
Reasonably Related Threads
- [PATCH] daemon: xattr: factorize do_getxattr and do_lgetxattr
- 1.2.6 Spurious pread()/stream errors/index corruption
- [PATCH 1/3] daemon: pwrite/pread: Don't double close on error path.
- [PATCH] New APIs: hopen-device hopen-file hread hwrite hseek hclose hclose-all
- [PATCH v2 0/5] remove unused gnulib modules