]> git.eshelyaron.com Git - emacs.git/commitdiff
Simplify file end finding in insert-file-contents
authorPaul Eggert <eggert@cs.ucla.edu>
Thu, 24 Jul 2025 00:38:32 +0000 (17:38 -0700)
committerEshel Yaron <me@eshelyaron.com>
Fri, 25 Jul 2025 08:12:46 +0000 (10:12 +0200)
* src/fileio.c (Finsert_file_contents): Don’t give up finding the
file’s end merely because SEEK_END fails.  Try a small test read
anyway.  This might work in non-POSIX file systems where SEEK_END
is not allowed, and trying is easy and simplifies the code.

(cherry picked from commit 4c791549496d1edbed7ba792994381e6cdc01184)

src/fileio.c

index ead833a6ac07a572ee9c34a34bd8c4fa4efa2fa5..c042089ac8575cd27d1d0074147a5eb5982b65e5 100644 (file)
@@ -4525,27 +4525,26 @@ by calling `format-decode', which see.  */)
          if (endpos == TYPE_MAXIMUM (off_t))
            {
              endpos = emacs_fd_lseek (fd, 0, SEEK_END);
-             giveup_match_end = endpos < 0;
-             if (!giveup_match_end)
-               {
-                 /* Check that read reports EOF soon, to catch platforms
-                    where SEEK_END can report wildly small offsets.  */
-                 ptrdiff_t n = emacs_full_read (fd, read_buf, sizeof read_buf);
-                 if (n < 0)
-                   report_file_error ("Read error", orig_filename);
-                 curpos = endpos += n;
+             if (endpos < 0)
+               endpos = curpos;
 
-                 /* Give up if the file grew more than even the test read.  */
-                 giveup_match_end = n == sizeof read_buf;
+             /* Check that read reports EOF soon, to catch platforms
+                where SEEK_END fails or reports too-small offsets.  */
+             ptrdiff_t n = emacs_full_read (fd, read_buf, sizeof read_buf);
+             if (n < 0)
+               report_file_error ("Read error", orig_filename);
+             curpos = endpos += n;
 
-                 if (!giveup_match_end)
-                   {
-                     /* Shrink the file's head if the file shrank to
-                        be smaller than its head.  */
-                     off_t offset_from_beg = endpos - beg_offset;
-                     if (offset_from_beg < same_at_start - BEGV_BYTE)
-                       same_at_start = max (0, offset_from_beg) + BEGV_BYTE;
-                   }
+             /* Give up if the file extends past the test read.  */
+             giveup_match_end = n == sizeof read_buf;
+
+             if (!giveup_match_end)
+               {
+                 /* Shrink the file's head if the file shrank to
+                    be smaller than its head.  */
+                 off_t offset_from_beg = endpos - beg_offset;
+                 if (offset_from_beg < same_at_start - BEGV_BYTE)
+                   same_at_start = max (0, offset_from_beg) + BEGV_BYTE;
                }
            }
        }