From 050b8b14d3f4e277a1fba4a86959dd4990c5c770 Mon Sep 17 00:00:00 2001 From: Felipe Fumero Date: Sat, 21 Nov 2026 16:49:49 -0600 Subject: [PATCH] fix: harden sync download finalization --- b2sdk/_internal/sync/action.py | 39 ++++++++++-------- .../transfer/inbound/downloaded_file.py | 3 +- changelog.d/+sync-finalization.fixed.md | 1 + .../unit/sync/test_regression_sync_actions.py | 40 +++++++------------ 4 files changed, 40 insertions(+), 43 deletions(-) create mode 100644 changelog.d/+sync-finalization.fixed.md diff --git a/b2sdk/_internal/sync/action.py b/b2sdk/_internal/sync/action.py index 7a7a01e00..42e5d660d 100644 --- a/b2sdk/_internal/sync/action.py +++ b/b2sdk/_internal/sync/action.py @@ -318,20 +318,27 @@ def do_action(self, bucket: Bucket, reporter: ProgressReport) -> None: bucket=bucket, file_version=self.source_path.selected_version, ) - with contextlib.ExitStack() as exit_stack: - if progress_listener: - exit_stack.enter_context(progress_listener) - downloaded_file = bucket.download_file_by_id( - self.source_path.selected_version.id_, - progress_listener=progress_listener, - encryption=encryption, - ) - downloaded_file.save_to(download_path) - - # Move the file into place - with suppress(OSError): - os.unlink(self.local_full_path) - os.rename(download_path, self.local_full_path) + try: + with contextlib.ExitStack() as exit_stack: + if progress_listener: + exit_stack.enter_context(progress_listener) + downloaded_file = bucket.download_file_by_id( + self.source_path.selected_version.id_, + progress_listener=progress_listener, + encryption=encryption, + ) + downloaded_file.save_to(download_path) + + # Move the file into place + os.replace(download_path, self.local_full_path) + except BaseException: + try: + os.unlink(download_path) + except FileNotFoundError: + pass + except OSError: + logger.warning('Could not remove temporary sync download', exc_info=True) + raise def do_report(self, bucket: Bucket, reporter: ProgressReport) -> None: """ @@ -340,7 +347,7 @@ def do_report(self, bucket: Bucket, reporter: ProgressReport) -> None: :param bucket: a Bucket object :param reporter: a place to report errors """ - reporter.print_completion('dnload ' + self.source_path.relative_path) + reporter.print_completion('dnload ' + escape_control_chars(self.source_path.relative_path)) def __str__(self) -> str: return 'b2_download(%s, %s, %s, %d)' % ( @@ -430,7 +437,7 @@ def do_report(self, bucket: Bucket, reporter: ProgressReport) -> None: :param bucket: a Bucket object :param reporter: a place to report errors """ - reporter.print_completion('copy ' + self.source_path.relative_path) + reporter.print_completion('copy ' + escape_control_chars(self.source_path.relative_path)) def __str__(self) -> str: return 'b2_copy(%s, %s, %s, %d)' % ( diff --git a/b2sdk/_internal/transfer/inbound/downloaded_file.py b/b2sdk/_internal/transfer/inbound/downloaded_file.py index b75923dc7..4f4ed721c 100644 --- a/b2sdk/_internal/transfer/inbound/downloaded_file.py +++ b/b2sdk/_internal/transfer/inbound/downloaded_file.py @@ -136,7 +136,8 @@ def __enter__(self): def __exit__(self, exc_type, exc_val, exc_tb): self.file.close() - set_file_mtime(self.path_, self.mod_time_to_set) + if exc_type is None: + set_file_mtime(self.path_, self.mod_time_to_set) def __str__(self): return str(self.path) diff --git a/changelog.d/+sync-finalization.fixed.md b/changelog.d/+sync-finalization.fixed.md new file mode 100644 index 000000000..da24777f3 --- /dev/null +++ b/changelog.d/+sync-finalization.fixed.md @@ -0,0 +1 @@ +Fixed sync download finalization and escaped control characters in sync completion messages. diff --git a/test/unit/sync/test_regression_sync_actions.py b/test/unit/sync/test_regression_sync_actions.py index e506ff0c2..435cd3e60 100644 --- a/test/unit/sync/test_regression_sync_actions.py +++ b/test/unit/sync/test_regression_sync_actions.py @@ -34,7 +34,7 @@ from b2sdk._internal.scan.path import B2Path from b2sdk._internal.sync.action import B2CopyAction, B2DownloadAction -from test.known_defect import DefectStillPresent, expect, known_defect +from test.known_defect import expect pytestmark = [pytest.mark.apiver(from_ver=2)] @@ -69,11 +69,6 @@ def _corrupt_stored_sha1(api, bucket, file_version): class TestDownloadedFileModTime: - @known_defect( - 'C3', - 'a download that fails its checksum check still gets the remote modification time', - raises=AssertionError, - ) def test_c3_failed_checksum_does_not_stamp_remote_mtime(self, api_and_bucket, tmp_path): """C3: a download that fails validation must not leave a file that looks current.""" api, bucket = api_and_bucket @@ -94,9 +89,6 @@ def test_c3_failed_checksum_does_not_stamp_remote_mtime(self, api_and_bucket, tm class TestSyncDownloadAction: - @known_defect( - 'C3', 'a failed sync download leaves the temporary file behind', raises=DefectStillPresent - ) def test_c3_failed_download_leaves_no_temp_file(self, api_and_bucket, tmp_path): """C3: after a failed sync download the .b2.sync.tmp file must not be left behind.""" api, bucket = api_and_bucket @@ -108,11 +100,16 @@ def test_c3_failed_download_leaves_no_temp_file(self, api_and_bucket, tmp_path): assert not (tmp_path / 'f.txt').exists(), 'final file was created' expect(not (tmp_path / 'f.txt.b2.sync.tmp').exists(), 'temporary file left behind') - @known_defect( - 'C17', - 'sync download deletes the old local file before renaming the new one into place', - raises=DefectStillPresent, - ) + def test_c3_cleanup_failure_preserves_download_error(self, api_and_bucket, tmp_path, caplog): + api, bucket = api_and_bucket + fv = bucket.upload_bytes(b'hello world', 'f.txt') + _corrupt_stored_sha1(api, bucket, fv) + action = _download_action(bucket, fv, tmp_path / 'f.txt') + with mock.patch('os.unlink', side_effect=PermissionError('cleanup failed')): + with pytest.raises((ChecksumMismatch, TruncatedOutput)): + action.do_action(bucket, None) + assert 'Could not remove temporary sync download' in caplog.text + def test_c17_failed_replacement_keeps_previous_local_copy(self, api_and_bucket, tmp_path): """C17: if moving the new file into place fails, the old local file must still exist. @@ -136,6 +133,7 @@ def fail(*args, **kwargs): assert injected, 'the download never tried to move the new file into place' expect(local.exists(), 'the previous local copy was removed') expect(local.read_bytes() == b'old content', 'the previous local copy was changed') + assert not (tmp_path / 'f.txt.b2.sync.tmp').exists() def test_c17_guard_successful_download_replaces_file(self, api_and_bucket, tmp_path): """Guard for C17: a normal sync download replaces the local file and leaves no temp file.""" @@ -154,24 +152,14 @@ class TestSyncMessages: def _path(self): return B2Path(self.NAME, mock.Mock(), []) - @known_defect( - 'C24', - 'the sync download message prints remote names without escaping control characters', - raises=AssertionError, - ) def test_c24_download_message_escapes_control_characters(self): """C24: remote names with control chars must be escaped in output.""" action = B2DownloadAction(self._path(), self.NAME, 'out/x', mock.Mock()) reporter = mock.Mock() action.do_report(mock.Mock(), reporter) printed = reporter.print_completion.call_args[0][0] - assert '\x1b' not in printed, repr(printed) + assert printed == r"dnload 'dir/evil\x1b[31mname'" - @known_defect( - 'C24', - 'the sync copy message prints remote names without escaping control characters', - raises=AssertionError, - ) def test_c24_copy_message_escapes_control_characters(self): """C24: same for copy actions.""" action = B2CopyAction( @@ -180,4 +168,4 @@ def test_c24_copy_message_escapes_control_characters(self): reporter = mock.Mock() action.do_report(mock.Mock(), reporter) printed = reporter.print_completion.call_args[0][0] - assert '\x1b' not in printed, repr(printed) + assert printed == r"copy 'dir/evil\x1b[31mname'"