diff --git a/CHANGES.rst b/CHANGES.rst index 002826471f..09dc688897 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -124,6 +124,11 @@ Compute Storage ~~~~~~~ +- [Local Storage] Delete the partially written file when ``download_object_range`` + fails and ``delete_on_failure`` is set. + (GITHUB-2223) + [Rahul - @raamrutsagarb25-tech] + - [S3] Fix ``get_object_cdn_url`` computing invalid signatures when using a non-standard port (#2222) diff --git a/libcloud/storage/drivers/local.py b/libcloud/storage/drivers/local.py index c6fc1f83ab..b1c5ecfe49 100644 --- a/libcloud/storage/drivers/local.py +++ b/libcloud/storage/drivers/local.py @@ -505,9 +505,19 @@ def download_object_range( obj=obj, start_bytes=start_bytes, end_bytes=end_bytes ) - with open(file_path, "wb") as fp: - for chunk in iterator: - fp.write(chunk) + try: + with open(file_path, "wb") as fp: + for chunk in iterator: + fp.write(chunk) + except Exception: + if delete_on_failure and os.path.exists(file_path): + try: + os.remove(file_path) + except OSError: + # Don't let a failure to clean up the partially + # written file mask the original download exception. + pass + raise return True diff --git a/libcloud/test/storage/test_local.py b/libcloud/test/storage/test_local.py index 773124bd1a..1c9b61d489 100644 --- a/libcloud/test/storage/test_local.py +++ b/libcloud/test/storage/test_local.py @@ -579,6 +579,63 @@ def test_download_object_range_success(self): self.remove_tmp_file(tmppath) os.unlink(destination_path) + def test_download_object_range_deletes_file_on_failure(self): + container = self.driver.create_container("range-failure") + obj = self.driver.upload_object_via_stream(iter([b"payload"]), container, "object") + + destination_path = os.path.join(self.key, "download") + + error = OSError("stream failed") + + def failing_iterator(*args, **kwargs): + yield b"partial data" + raise error + + with mock.patch.object( + self.driver, "download_object_range_as_stream", side_effect=failing_iterator + ): + with self.assertRaises(OSError) as raised: + self.driver.download_object_range( + obj=obj, + destination_path=destination_path, + start_bytes=0, + delete_on_failure=True, + ) + + self.assertIs(raised.exception, error) + self.assertFalse(os.path.exists(destination_path)) + + def test_download_object_range_preserves_partial_file_when_delete_on_failure_is_false(self): + container = self.driver.create_container("range-failure-preserve") + obj = self.driver.upload_object_via_stream(iter([b"payload"]), container, "object") + + destination_path = os.path.join(self.key, "download") + + error = OSError("stream failed") + + def failing_iterator(*args, **kwargs): + yield b"partial data" + raise error + + with mock.patch.object( + self.driver, "download_object_range_as_stream", side_effect=failing_iterator + ): + with self.assertRaises(OSError) as raised: + self.driver.download_object_range( + obj=obj, + destination_path=destination_path, + start_bytes=0, + delete_on_failure=False, + ) + + self.assertIs(raised.exception, error) + self.assertTrue(os.path.exists(destination_path)) + + with open(destination_path, "rb") as fp: + self.assertEqual(fp.read(), b"partial data") + + os.unlink(destination_path) + def test_download_object_range_as_stream_success(self): content = b"0123456789123456789" tmppath = self.make_tmp_file(content=content)