fix: Chapter-4/p6.c Hole spanning across buffers#15
Open
Invinc-Z wants to merge 2 commits intoMeiK2333:masterfrom
Open
fix: Chapter-4/p6.c Hole spanning across buffers#15Invinc-Z wants to merge 2 commits intoMeiK2333:masterfrom
Invinc-Z wants to merge 2 commits intoMeiK2333:masterfrom
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.
The issue lies in the current solution:
Each time a buffer is read, both
fandlare reset to 0, this means each buffer is processed independently, making it impossible to correctly handle holes that span buffer boundaries.In practice, when the code encounters zero bytes, it does not skip writing them, but continues processing as normal.
test:
compile the
make_sparse_file.cbygcc make_sparse_file.cto get sparse filesparse_filecopy file with
./a.out sparse_file destll sparse_flie destanddu sparse_file destthis implies that this solution copy a file containing holes, with writing the bytes of 0 to the output file, however, this is not the effect we want to get.
Some improvements are as follows:
Fix the cross-buffer boundary issue: Use persistent state variables (
in_hole,data_start,hole_start) to track the position in the file and ensure correct handling of holes that span multiple buffers.Optimize the write logic: Use
lseekfor positioning only when switching from a hole to data, and write data blocks when switching from data to a hole, avoiding unnecessary write operations.Ensure the correct file size: Use
ftruncateto make the target file the same size as the source file.