Fix nob_read_entire_file for procfs files and pipes#261
Open
NeikiDev wants to merge 1 commit into
Open
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #242
nob_read_entire_filegets the file size viafseek/ftelland then reads exactly that many bytes. Files on virtual file systems like procfs report size 0, so the function "succeeds" but the string builder stays empty:Pipes and FIFOs are worse,
fseekfails on them so you cannot read them at all:[ERROR] Could not read file pipe: Illegal seekThe patch keeps the
fseek/ftellsize but only as a hint to preallocate the string builder. The actual reading is afreadloop until EOF, so the real content ends up in the builder no matter what size was reported. If the file is not seekable the preallocation is simply skipped. On failuresb->countis restored, so a failed read does not leave partial content in the builder, which matches the old behavior of not touchingcounton errors.After the patch:
I also added
tests/read_entire_file.cwhich covers regular files, appending to a non empty builder, a missing file, and reading from a FIFO (that last part is skipped on Windows).Tested on Linux (gcc 14, both C and C++ mode) and macOS (clang), the whole test suite passes on both. One note: reading endless files like
/dev/zeronow keeps reading forever instead of returning an empty builder, but that is the same thingcatdoes.