]> Savannah Git Hosting - gnulib.git/commitdiff
lseek: port around macOS SEEK_HOLE glitch
authorPaul Eggert <eggert@cs.ucla.edu>
Tue, 16 Nov 2021 06:17:44 +0000 (22:17 -0800)
committerPaul Eggert <eggert@cs.ucla.edu>
Tue, 16 Nov 2021 06:21:13 +0000 (22:21 -0800)
Problem reported by Sudhip Nashi (Bug#51857#47).
* lib/lseek.c (rpl_lseek): Work around macOS lseek+SEEK_HOLE
returning -1 with ENXIO if there are no holes before EOF,
contrary to the macOS documentation.

ChangeLog
lib/lseek.c

index 71a22657065265fd115bb797e4dca5e5b73bcd4f..efc3a3887e10f3b177198efdddc14f4540094ba8 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,11 @@
 2021-11-15  Paul Eggert  <eggert@cs.ucla.edu>
 
+       lseek: port around macOS SEEK_HOLE glitch
+       Problem reported by Sudhip Nashi (Bug#51857#47).
+       * lib/lseek.c (rpl_lseek): Work around macOS lseek+SEEK_HOLE
+       returning -1 with ENXIO if there are no holes before EOF,
+       contrary to the macOS documentation.
+
        lseek: port around macOS SEEK_DATA glitch
        Problem reported by Sudhip Nashi (Bug#51857).
        * doc/posix-functions/lseek.texi (lseek): Mention macOS SEEK_DATA
index 7dcd6c9da703bfac2c065abed94861c5ecb41c30..e9a96ad20a0740e61cf1aeefec772d20cce2faf9 100644 (file)
@@ -61,10 +61,12 @@ rpl_lseek (int fd, off_t offset, int whence)
          data followed by a possibly-empty hole.  To work around this
          portability glitch, check whether OFFSET is within data by
          using lseek+SEEK_HOLE, and if so return to OFFSET by using
-         lseek+SEEK_SET.  */
+         lseek+SEEK_SET.  Also, contrary to the macOS documentation,
+         lseek+SEEK_HOLE can fail with ENXIO if there are no holes on
+         or after OFFSET.  What a mess!  */
       off_t next_hole = lseek (fd, offset, SEEK_HOLE);
       if (next_hole < 0)
-        return next_hole;
+        return errno == ENXIO ? offset : next_hole;
       if (next_hole != offset)
         whence = SEEK_SET;
     }