find: share one file handle per output path (fixes #439) - #828
Open
MsfPablo wants to merge 1 commit into
Open
Conversation
GNU find de-duplicates the files opened by -fprint, -fprintf, -fprint0 and -fls, so `find -fprint foo -fprint foo` writes each line twice. We opened a separate File per predicate, so the two handles had independent file offsets and their writes overwrote each other. Cache opened output files by the path given on the command line and hand out a shared Rc<File>, so repeated references to the same path reuse a single file offset. Fixes uutils#439
|
Commit 2081a60 has test result changes: bfs testsuite: |
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 #439.
Problem
GNU
findde-duplicates the files opened by its output predicates, sofind . -fprint foo -fprint foowrites each path tofootwice. uutilsopened a separate
Filefor every occurrence of-fprint/-fprintf/-fprint0/-fls, so each handle had its own file offset and the writesoverwrote one another — only one line per entry survived.
Fix
get_or_create_filenow caches opened output files onConfig, keyed by thepath as written on the command line (matching GNU's literal-filename
de-duplication), and hands out a shared
Rc<File>. Repeated references to thesame path therefore share a single file descriptor and a single file offset.
Printer,PrintfandLstakeOption<Rc<File>>instead ofOption<File>.The first reference still truncates, as before.
Tests
Two unit tests in
src/find/matchers/mod.rs:get_or_create_file_reuses_handle_for_same_path— the same path yields thesame
Rc, and writes through both handles append rather than overwrite.two_fprints_to_the_same_file_do_not_overwrite_each_other— end-to-endthrough
build_top_level_matcherwith-fprint out -fprint out, assertingthe entry appears twice.
Verified the regression test fails without the fix (reverting just the cache
lookup gives
left: "…abbbc\n…abbbc\n"/right: "…abbbc\n").AI disclosure
This change was written with the assistance of Claude (Anthropic). I have
reviewed the diff, and ran the build, the full test suite, clippy and rustfmt
locally as shown above.