From 65d0f6f4d1830c3e0577c92b3bf7e029cabc5ab2 Mon Sep 17 00:00:00 2001 From: Ethan Dalool Date: Fri, 23 Dec 2022 19:53:10 -0800 Subject: [PATCH] Attempt to lock source file before attempting to open dest. --- voussoirkit/spinal.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/voussoirkit/spinal.py b/voussoirkit/spinal.py index 81e3caa..29c39e5 100644 --- a/voussoirkit/spinal.py +++ b/voussoirkit/spinal.py @@ -489,15 +489,18 @@ def copy_file( else: raise + log.loud('Copying file %s', source.absolute_path) log.loud('Opening source handle.') source_handle = handlehelper(source, 'rb') - log.loud('Opening dest handle.') - destination_handle = handlehelper(destination, 'wb') - - if source_handle is None and destination_handle: - destination_handle.close() + if source_handle is None: return results + if portalocker is not None: + log.loud('Locking source file.') + portalocker.lock(source_handle, portalocker.LockFlags.EXCLUSIVE) + + log.loud('Opening dest handle.') + destination_handle = handlehelper(destination, 'wb') if destination_handle is None: source_handle.close() return results @@ -512,9 +515,6 @@ def copy_file( if dynamic_chunk_size: chunk_size = bytestring.MEBIBYTE - if portalocker is not None: - portalocker.lock(source_handle, portalocker.LockFlags.EXCLUSIVE) - while True: chunk_start = time.perf_counter()