cache: add seek()/tell() to SyncFile, use SaveFile in _write_files_cache, fixes #9390#9392
Open
mr-raj12 wants to merge 1 commit intoborgbackup:masterfrom
Open
cache: add seek()/tell() to SyncFile, use SaveFile in _write_files_cache, fixes #9390#9392mr-raj12 wants to merge 1 commit intoborgbackup:masterfrom
mr-raj12 wants to merge 1 commit intoborgbackup:masterfrom
Conversation
Codecov Report❌ Patch coverage is
Additional details and impacted files☔ View full report in Codecov by Sentry. |
ThomasWaldmann
requested changes
Feb 22, 2026
Member
ThomasWaldmann
left a comment
There was a problem hiding this comment.
good change, found some minor things.
| sf.seek(0, io.SEEK_END) | ||
| assert sf.tell() == 11 | ||
| sf.seek(5, io.SEEK_SET) | ||
| assert sf.tell() == 5 |
Member
There was a problem hiding this comment.
for completeness, do a read after that and assert.
|
|
||
| def test_syncfile_close_idempotent(): | ||
| """Calling SyncFile.close() twice does not raise.""" | ||
| with tempfile.TemporaryDirectory() as tmpdir: |
| sf.close() # must not raise | ||
|
|
||
|
|
||
| def test_savefile_with_integrity_checked_file(): |
Member
There was a problem hiding this comment.
quite similar to test_write_and_verify_with_syncfile.
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.
Description
_write_files_cache()incache.pyhas a TODO noting thatSaveFilecouldn't be used becauseSyncFilelacks.seek()and.tell()SaveFile, the files cache is written directly to the final path — a crash mid-write can leave a corrupted fileSaveFileprovides atomic writes (temp file +os.replace), but its__enter__returns aSyncFile, andIntegrityCheckedFileneeds.seek()/.tell()on its backing fd for hash finalization (hash_lengthcallsseek(0, SEEK_END)+tell())SyncFile, makeclose()idempotent (needed because theIntegrityCheckedFile→ hasher →FileLikeWrapperexit chain closes theSyncFilebeforeSaveFile.__exit__tries to close it again), then refactor_write_files_cacheto useSaveFileFixes #9390
Changes
src/borg/platform/base.pyseek()andtell()toSyncFile, delegating toself.f; makeclose()idempotent withif self.f.closed: returnguardsrc/borg/cache.py_write_files_cacheto wrap writes inSaveFile+IntegrityCheckedFile(override_fd=...)for atomic updates; remove TODO commentsrc/borg/testsuite/platform/all_test.pyseek/tell, idempotentclose, andSaveFile+IntegrityCheckedFileintegrationsrc/borg/testsuite/crypto/file_integrity_test.pyIntegrityCheckedFilewithSyncFileasoverride_fdChecklist
master