aboutsummaryrefslogtreecommitdiffstats
path: root/src/fileio.c (follow)
Commit message (Collapse)AuthorAgeFilesLines
* Port better to Linux with 1 MiB pagesPaul Eggert2025-08-021-1/+1
| | | | | | * src/lisp.h: Include <sys-limits.h>. (MAX_RW_COUNT): Remove, as it is the same thing as sys-limits.h’s SYS_BUFSIZE_MAX. All uses replaced by SYS_BUFSIZE_MAX.
* Don’t read before BEG in insert-file-contentsPaul Eggert2025-07-231-4/+12
| | | | | * src/fileio.c (Finsert_file_contents): Don’t sample bytes before BEG. Give up optimizing if the file size shrinks to less than BEG.
* Simplify file end finding in insert-file-contentsPaul Eggert2025-07-231-19/+18
| | | | | | | * 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.
* insert-file-contents respect END betterPaul Eggert2025-07-211-40/+72
| | | | | | | | | This fixes some longstanding bugs, along with bugs recently introduced as part of fixes for Bug#77315. * src/fileio.c (Finsert_file_contents): Arrange for END_OFFSET to never be less than BEG_OFFSET, to simplify the code. Do not go past END_OFFSET when sampling or optimizing reading. Avoid a few unnecessary lseeks.
* insert-file-contents shrinking Solaris file fixPaul Eggert2025-07-211-2/+3
| | | | | | | | This is related to recent fixes for Bug#77315. * src/fileio.c (Finsert_file_contents): Defend against a file shrinking to be smaller than its initial offset. This can happen only on platforms like Solaris where the initial offset can be positive.
* Refactor insert-file-contents overlap checkPaul Eggert2025-07-211-10/+7
| | | | | * src/fileio.c (Finsert_file_contents): Simplify and clarify file overlap check.
* insert-file-contents refactor xlseekPaul Eggert2025-07-211-9/+10
| | | | | * src/fileio.c (xlseek): New arg WHENCE. All uses changed. This generalizes xlseek and should simplify future changes.
* Say why quit is captured in insert-file-contentsPaul Eggert2025-07-211-2/+4
| | | | * src/fileio.c (Finsert_file_contents): Add comment.
* insert-file-contents read size increasePaul Eggert2025-07-211-6/+10
| | | | | | | | | | This increases the max read size from 0.25 to 1 MiB, which is a bit better for Emacs in my measurements. The old value was taken from coreutils, but was for a different purpose. * src/fileio.c (INSERT_READ_SIZE_MAX): New constant, replacing IO_BUFSIZE. All uses changed. (Finsert_file_contents): Do not use a read buffer that is larger than INSERT_READ_SIZE_MAX.
* Fix insert-file-contents infloop with unlucky ENDPaul Eggert2025-07-211-37/+30
| | | | | | | | | | | This partially reverts 55f41ca3aa8fe487d10730708a7396137a2c9d18, which was a partial fix for Bug#77315. * src/fileio.c (Finsert_file_contents): Go back to accepting partial reads when doing the optimized way when code conversion is not needed. This avoids an unlikely bug when END happens to lie at a buffer boundary, causing an infloop. For consistency, do the same when doing code conversion (though there’s no bug in then), as there’s not a real need for emacs_full_read there either.
* insert-file-contents from seekable non-regularPaul Eggert2025-07-211-1/+1
| | | | | | | | | | * src/fileio.c (Finsert_file_contents): Be more conservative when reading from seekable non-regular files when END is given. The code assumed that in this case, reads would succeed or fail quickly so there was no need to call internal_condition_case_1. However, in some cases, e.g., /dev/random, it could take a while. This fixes a glitch introduced in commit cb4579ed6ba45c81ee7ec627bf197e1def121f24 dated 2022-06-11.
* insert-file-contents beware buffer relocationPaul Eggert2025-07-211-2/+15
| | | | | | | | | | This fixes a potential (albeit quite unlikely) bug introduced during recent fixes to Bug#77315. * src/fileio.c (union read_non_regular.s.inserted): Bring back this member. (read_non_regular, Finsert_file_contents): Work in the unlikely case when internal_condition_case_1 relocates the buffer before calling read_non_regular.
* insert-file-contents 1 KiB seek fixPaul Eggert2025-07-211-46/+47
| | | | | | | | | | | | | | | This improves on recent fixes to Bug#77315. When sampling the first 1 KiB and last 3 KiB, do not seek before BEG if given. Instead, sample starting at BEG, to be consistent with the non-optimized version. * src/fileio.c (xlseek): Return POS, for convenience. (Finsert_file_contents): Sample the first 1 KiB correctly when BEG. In a CURPOS local, keep track of the input file offset, or for nonseekable files the number of bytes read, while this value is important. This lets us avoid some unnecessary seeks. Report an error earlier if the file is not seekable and BEG is nonzero, to save work and simplify the code. When sampling, discard less data, as this is simpler and there’s little point to discarding it.
* insert-file-contents seek refactoringPaul Eggert2025-07-211-16/+15
| | | | | | * src/fileio.c (xlseek): New function. (Finsert_file_contents): Use it instead of doing things by hand. (write_region): Match diagnostic wording to xlseek’s.
* Fix insert-file-contents optimization glitchPaul Eggert2025-07-211-7/+2
| | | | | | | | This improves on recent fixes to Bug#77315. * src/fileio.c (Finsert_file_contents): When optimizing do not seek to 0 if the initial file offset is positive, as that causes the optimized code to behave differently from the nonoptimized.
* insert-file-contents SEEK_END optimizationPaul Eggert2025-07-211-8/+5
| | | | | | | | | 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.
* insert-file-contents st_size cleanupPaul Eggert2025-07-211-22/+12
| | | | | | | | * src/fileio.c (Finsert_file_contents): Store st_size, not file_size_hint, into current_buffer->modtime_size, since modtime_size is compared to a later st_size, not to the actual file size. Simplify buffer overflow detection from file size, since this occurred only on regular files.
* Don’t optimize insert-file-contents for GNUPaul Eggert2025-07-161-8/+6
| | | | | * src/fileio.c (Finsert_file_contents): Remove a microoptimization for GNU/Linux and Android.
* insert-file-contents small gap bugPaul Eggert2025-07-161-1/+2
| | | | | | Problem reported by Zhengyi Fu (Bug#79031). * src/fileio.c (Finsert_file_contents): Do not read more than requested, even when the gap is so small that we use a local buffer.
* insert-file-contents likely-end memory objectsPaul Eggert2025-07-161-16/+12
| | | | | | * src/fileio.c (Finsert_file_contents): Treat memory objects like regular files when inferring likely end. Simplify likely end computation.
* Port insert-file-contents dir behavior to UnixPaul Eggert2025-07-161-3/+15
| | | | | | * src/fileio.c (Finsert_file_contents): Port better to traditional Unix platforms like Solaris, where in some cases 'read' can succeed on directories.
* Fix insert-file-contents overlap checkPaul Eggert2025-07-151-19/+47
| | | | | | | | | | | In commit 61a8ce0280668980a4452b112103c9bc879e1f49 I mistakenly focused on overlap in the buffer. The code also needs to check for overlap in the inserted file data. Problem reported by Gerd Möllmann (Bug#79020). * src/fileio.c (Finsert_file_contents): Prevent overlap in both the buffer head and tail, and in the inserted file’s head and tail. Also, shrink the file’s head if the file shrank to be smaller than its head.
* Fix the Android portPo Lu2025-07-141-2/+2
| | | | | * src/fileio.c (emacs_full_read): Retype FD param to `emacs_fd'. (Fcopy_file): Label newsize MAYBE_UNUSED.
* ; Fix indentation.Paul Eggert2025-07-131-1/+2
|
* Speed up insert-file-contents readsPaul Eggert2025-07-131-4/+6
| | | | | | | | | This change makes insert-file-contents-literally 30% faster on my platform, when inserting xdisp.c. * src/fileio.c (READ_BUF_SIZE): Remove, replacing with ... (IO_BUFSIZE): ... this new constant from Coreutils. All uses of READ_BUF_SIZE changed to either MAX_ALLOCA or IO_BUFSIZE.
* Avoid some tiny /proc file readsPaul Eggert2025-07-131-22/+29
| | | | | | | | | * src/fileio.c (union read_non_regular): New members buf and bufsize replace inserted and trytry. (read_non_regular): Adjust to this new, simpler interface. (Finsert_file_contents): If the gap is smaller than read_buf and we want to read more than the gap, read into read_buf first, to avoid lots of tiny reads from /proc files.
* insert-file-contents file shrinkage in scanbackPaul Eggert2025-07-131-79/+87
| | | | | | | | | * src/fileio.c (emacs_full_read): New function. (Fcopy_file, Finsert_file_contents): Use it. (Finsert_file_contents): Check for partial reads when scanning backwards through the file’s tail, as this indicates the file shrank while we read it. Also, use emacs_full_read in other situations where it’s simpler and should be a bit faster.
* int → ptrdiff_t for read resultsPaul Eggert2025-07-131-1/+1
| | | | | | | * src/fileio.c (Finsert_file_contents): Use ptrdiff_t, not int, to store results returned by emacs_fd_read, which return ptrdiff_t. This doesn’t fix any bugs since the values happen to fit in int, but is a bit clearer and may simplify future changes.
* insert-file-contents unexpected end shrinkagePaul Eggert2025-07-131-0/+5
| | | | | | * src/fileio.c (Finsert_file_contents): If the file unexpectedly shrinks while reading its tail backwards, give up searching for the match end.
* fileio.c comment spelling fixPaul Eggert2025-07-131-1/+1
|
* insert-file-contents byte vs char confusionPaul Eggert2025-07-131-1/+1
| | | | | | * src/fileio.c (Finsert_file_contents): Use del_range_byte, not del_range_1, since we are passing byte counts, not character counts. Problem reported by Pip Cet (Bug#77315).
* Fix insert-file-contents overlap calculationPaul Eggert2025-07-131-13/+1
| | | | | | | * src/fileio.c (Finsert_file_contents): Overlap is impossible, so omit incorrect overflow adjustments that caused later index calculations to go off the rails (Bug#77315). Change a ‘ZV_BYTE’ to ‘same_at_end’ for clarity; they have the same value here.
* insert-file-contents do not rely on st_sizePaul Eggert2025-07-131-28/+4
| | | | | | | This fix was prompted by Bug#77315. * src/fileio.c (Finsert_file_contents): Do not rely on st_size for anything other than a hint about the file size. Trust only the file size as revealed by a read that returns 0.
* insert-file-contents file size hint improvementPaul Eggert2025-07-131-0/+4
| | | | | | * src/fileio.c (Finsert_file_contents): When reading yields 0, update the file size hint to match. This should improve its accuracy.
* insert-file-contents file end EOF fixesPaul Eggert2025-07-131-5/+33
| | | | | | | | | | * src/fileio.c (Finsert_file_contents): When counting bytes at file end, don’t trust lseek to seek to the end of the file. Instead, read a bit after lseeking, to make sure we get to the end, give up if we don’t get to EOF. This works around problems on /proc filesystems where lseek pretends the file is empty. Update file_size_hint when reaching EOF.
* Fix insert-file-contents integer overflowsPaul Eggert2025-07-131-4/+10
| | | | | | * src/fileio.c (Finsert_file_contents): Change ‘total’ from ptrdiff_t to off_t since it might not fit in ptrdiff_t. Check for overflow when estimating the insertion size.
* Remove ineffective overflow checkPaul Eggert2025-07-131-4/+1
| | | | | | * src/fileio.c (Finsert_file_contents): Remove check for overflow. The overflow is not possible, and the check was incorrectly written and would not have caught it anyway.
* insert-file-contents END enforcementPaul Eggert2025-07-131-13/+18
| | | | | * src/fileio.c (Finsert_file_contents): When matching text at buffer start, don’t go past END if specified.
* insert-file-contents initial offset fixPaul Eggert2025-07-131-10/+18
| | | | | | * src/fileio.c (Finsert_file_contents): Don’t assume that a newly-opened file is at offset 0, even if it is a regular file. This might not be true for files like /dev/stdin.
* insert-file-contents end offset infinityPaul Eggert2025-07-131-11/+10
| | | | | * src/fileio.c (Finsert_file_contents): Simplify end_offset calculation by defaulting to effectively infinity.
* Refactor negative file size checkingPaul Eggert2025-07-131-10/+14
| | | | | | * src/fileio.c (Finsert_file_contents): Check for negative file sizes earlier, as it’s easy, avoids some unnecessary work, and will simplify later changes.
* Non-nil orig_filename in insert-file-contentsPaul Eggert2025-07-131-2/+1
| | | | | * src/fileio.c (Finsert_file_contents): Don’t let orig_filename be nil, as that might mess up later diagnostics.
* Improve inserted file coding system findingPaul Eggert2025-07-131-5/+31
| | | | | | | * src/fileio.c (Finsert_file_contents): When inserting a file into a nonempty buffer, improve the heuristic for determining the file’s coding system by not trusting lseek+SEEK_END, which is unreliable in /proc or when the file is mutating.
* Improve insert-file-contents on non-regular filesPaul Eggert2025-07-131-34/+34
| | | | | | | | | | | | | | | | This is part of a fix for Bug#77315, and improves on the fix for Bug#71258. * src/fileio.c (Finsert_file_contents): Do not pretend that directories are regular files. Instead, signal an error when attempting to read from them in the usual case where the OS prohibits that; and otherwise read from them. However, when visiting a directory report an error right away rather than waiting until later, as this function is documented to not allow visiting non-regular files. Nest the struct stat into a small code block, to keep the code simpler and so that the compiler can see what parts are used and can issue better diagnostics if uninitialized storage is accessed. Be more skeptical of st_size, when the file is not regular.
* Don’t trust st_size when scanning file head+tailPaul Eggert2025-07-131-15/+12
| | | | | | | * src/fileio.c (Finsert_file_contents): Do not look at st_size when scanning the file’s head and tail for a coding system comment. Instead, just use read and SEEK_END and don’t worry if the latter fails. st_size and lseek might not work in a /proc file system.
* copy-file no longer trusts st_sizePaul Eggert2025-07-131-42/+22
| | | | | | | | | | | | | | In copy-file, do not trust st_size, since it might change as we run, or we might be in a /proc system where it is unreliable anyway. Also, fix some other unlikely copy-file bugs while we’re here. * src/fileio.c (Fcopy_file): Use O_TRUNC when opening a destination that already exists. This saves us the trouble of having to call ftruncate with a possibly-bogus st_size; the old motivation for using ftruncate is no longer compelling. Do not assume ptrdiff_t is as wide as ssize_t; although this is true on all known platforms, it’s easy to not assume it. Don’t trust st_size. Prefer SSIZE_MAX to TYPE_MAXIMUM (ssize_t). Always read+write, regardless of whether copy_file_range failed.
* READ_BUF_SIZE → sizeof read_bufPaul Eggert2025-07-131-2/+2
| | | | | | * src/fileio.c (Finsert_file_contents): Minor refactoring. This should help future patches which distinguish read_buf’s size from other uses of READ_BUF_SIZE which may change.
* insert-file-contents errno confusionPaul Eggert2025-07-131-11/+15
| | | | | | * src/fileio.c (read_non_regular): Return negation of errno on failure, instead of -1. (Finsert_file_contents): Signal with correct errno when a read fails.
* ; * src/fileio.c: Remove version from commentary (bug#78983).Eli Zaretskii2025-07-101-4/+4
|
* (Finsert_file_contents): Refine commit d07af40d8826Stefan Monnier2025-06-271-12/+19
| | | | | | | | * src/fileio.c (Finsert_file_contents): Inhibit ask-supersession only if we're VISITing in a non-narrowed buffer (bug#78866). * test/src/fileio-tests.el (ert--tests-dir): New var. (fileio-tests--insert-file-contents-supersession): New test.