From: Paul Eggert Date: Thu, 17 Jul 2025 17:46:33 +0000 (-0700) Subject: insert-file-contents SEEK_END optimization X-Git-Url: http://git.eshelyaron.com/gitweb/?a=commitdiff_plain;h=bf3ef2a3516d7e4ab3a36045e89b264d4a98c14b;p=emacs.git insert-file-contents SEEK_END optimization 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) --- diff --git a/src/fileio.c b/src/fileio.c index 00c04b21406..248a369341a 100644 --- a/src/fileio.c +++ b/src/fileio.c @@ -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. */