gzip: port to DMF file systems
authorPaul Eggert <eggert@cs.ucla.edu>
Mon, 25 Feb 2013 19:24:14 +0000 (11:24 -0800)
committerPaul Eggert <eggert@cs.ucla.edu>
Mon, 25 Feb 2013 19:25:55 +0000 (11:25 -0800)
* util.c (read_buffer): When reading a file with O_NONBLOCK, if
the read fails with errno==EAGAIN, clear O_NONBLOCK and try again.
Problem reported by Vitezslav Cizek in
<http://lists.gnu.org/archive/html/bug-gzip/2013-02/msg00030.html>.

util.c

diff --git a/util.c b/util.c
index cfd4e6cf5988aba6c2335ba22757e46a57abcdc1..c4c7f70532d6b14ff492dcecec25f542f500003f 100644 (file)
--- a/util.c
+++ b/util.c
@@ -132,16 +132,35 @@ int fill_inbuf(eof_ok)
 }
 
 /* Like the standard read function, except do not attempt to read more
-   than SSIZE_MAX bytes at a time.  */
+   than INT_MAX bytes at a time.  */
 int
 read_buffer (fd, buf, cnt)
      int fd;
      voidp buf;
      unsigned int cnt;
 {
+  int len;
   if (INT_MAX < cnt)
     cnt = INT_MAX;
-  return read (fd, buf, cnt);
+  len = read (fd, buf, cnt);
+
+#if defined F_SETFL && O_NONBLOCK && defined EAGAIN
+  /* Input files are opened O_NONBLOCK for security reasons.  On some
+     file systems this can cause read to fail with errno == EAGAIN.  */
+  if (len < 0 && errno == EAGAIN)
+    {
+      int flags = fcntl (fd, F_GETFL);
+      if (0 <= flags)
+        {
+          if (! (flags & O_NONBLOCK))
+            errno = EAGAIN;
+          else if (fcntl (fd, F_SETFL, flags & ~O_NONBLOCK) != -1)
+            len = read (fd, buf, cnt);
+        }
+    }
+#endif
+
+  return len;
 }
 
 /* Likewise for 'write'.  */