]> git.eshelyaron.com Git - emacs.git/commitdiff
insert-file-contents SEEK_END optimization
authorPaul Eggert <eggert@cs.ucla.edu>
Thu, 17 Jul 2025 17:46:33 +0000 (10:46 -0700)
committerEshel Yaron <me@eshelyaron.com>
Fri, 25 Jul 2025 08:11:31 +0000 (10:11 +0200)
This improves on recent fixes to Bug#77315.
* src/fileio.c (Finsert_file_contents):
Do not give up on seeking merely because SEEK_END fails.
On some files (/proc/cpuinfo, say) SEEK_END can fail
even though SEEK_SET and SEEK_CUR succeed, and the
code still works in this case.

(cherry picked from commit 1b517a4fe0a8362160c5aee403baf29952d5d559)

src/fileio.c

index 00c04b214063c6cbd09f43bdebaf943ae5953122..248a369341a91f74bcfe636dac8a80c15c784ebc 100644 (file)
@@ -4217,7 +4217,9 @@ by calling `format-decode', which see.  */)
   }
 
   /* The initial offset can be nonzero, e.g., /dev/stdin.
-     Regular files can be non-seekable, e.g., /proc/cpuinfo with SEEK_END.  */
+     If SEEK_CUR works, later code assumes SEEK_SET also works,
+     but tests SEEK_END rather than relying on it
+     as SEEK_END can fail on Linux /proc files.  */
   off_t initial_offset = emacs_fd_lseek (fd, 0, SEEK_CUR);
   bool seekable = 0 <= initial_offset;
   if (seekable && NILP (beg))
@@ -4309,10 +4311,7 @@ by calling `format-decode', which see.  */)
                {
                  off_t tailoff = emacs_fd_lseek (fd, - 3 * 1024, SEEK_END);
                  if (tailoff < 0)
-                   {
-                     seekable = false;
-                     tailoff = nread;
-                   }
+                   tailoff = nread;
 
                  /* When appending the last 3 KiB, read extra bytes
                     without trusting tailoff, as the file may be growing.  */
@@ -4502,9 +4501,7 @@ by calling `format-decode', which see.  */)
            {
              endpos = emacs_fd_lseek (fd, 0, SEEK_END);
              giveup_match_end = endpos < 0;
-             if (giveup_match_end)
-               seekable = false;
-             else
+             if (!giveup_match_end)
                {
                  /* Check that read reports EOF soon, to catch platforms
                     where SEEK_END can report wildly small offsets.  */