Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 23 additions & 16 deletions b2sdk/_internal/sync/action.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
"""
Expand All @@ -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)' % (
Expand Down Expand Up @@ -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)' % (
Expand Down
3 changes: 2 additions & 1 deletion b2sdk/_internal/transfer/inbound/downloaded_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions changelog.d/+sync-finalization.fixed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fixed sync download finalization and escaped control characters in sync completion messages.
40 changes: 14 additions & 26 deletions test/unit/sync/test_regression_sync_actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)]

Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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.

Expand All @@ -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."""
Expand All @@ -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(
Expand All @@ -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'"
Loading