Make lock_source_file optional.

This commit is contained in:
voussoir 2025-11-08 11:53:39 -08:00
parent 74cd6f3641
commit f279f0a75d

View file

@ -81,6 +81,7 @@ def copy_directory(
file_progressbar=None, file_progressbar=None,
files_per_second=None, files_per_second=None,
hash_class=None, hash_class=None,
lock_source_file=True,
overwrite=OVERWRITE_OLD, overwrite=OVERWRITE_OLD,
precalcsize=False, precalcsize=False,
skip_symlinks=True, skip_symlinks=True,
@ -160,6 +161,9 @@ def copy_directory(
hash_class: hash_class:
Passed into each `copy_file` as `hash_class`. Passed into each `copy_file` as `hash_class`.
lock_source_file:
Passed into each `copy_file` as `lock_source_file`.
overwrite: overwrite:
Passed into each `copy_file` as `overwrite`. Passed into each `copy_file` as `overwrite`.
@ -304,6 +308,7 @@ def copy_directory(
chunk_size=chunk_size, chunk_size=chunk_size,
dry_run=dry_run, dry_run=dry_run,
hash_class=hash_class, hash_class=hash_class,
lock_source_file=lock_source_file,
overwrite=overwrite, overwrite=overwrite,
progressbar=file_progressbar, progressbar=file_progressbar,
verify_hash=verify_hash, verify_hash=verify_hash,
@ -345,6 +350,7 @@ def copy_file(
destination_new_root=None, destination_new_root=None,
dry_run=False, dry_run=False,
hash_class=None, hash_class=None,
lock_source_file=True,
overwrite=OVERWRITE_OLD, overwrite=OVERWRITE_OLD,
progressbar=None, progressbar=None,
verify_hash=False, verify_hash=False,
@ -400,6 +406,10 @@ def copy_file(
needing overwrite, this won't be set, so be prepared to handle None. needing overwrite, this won't be set, so be prepared to handle None.
If None, the hash will not be calculated. If None, the hash will not be calculated.
lock_source_file:
If True, attempt to lock the source file from being modified while we
are copying it, to prevent corruption.
overwrite: overwrite:
This option decides what to do when the destination file already exists. This option decides what to do when the destination file already exists.
If OVERWRITE_ALL, the file will be overwritten. If OVERWRITE_ALL, the file will be overwritten.
@ -499,9 +509,19 @@ def copy_file(
if source_handle is None: if source_handle is None:
return results return results
if portalocker is not None: source_file_lock = None
if lock_source_file and portalocker is not None:
log.loud('Locking source file.') log.loud('Locking source file.')
portalocker.lock(source_handle, portalocker.LockFlags.EXCLUSIVE) try:
source_file_lock = portalocker.Lock(
source.absolute_path,
mode='rb',
flags=portalocker.LockFlags.EXCLUSIVE | portalocker.LockFlags.NON_BLOCKING,
timeout=10,
)
source_file_lock.acquire()
except portalocker.exceptions.LockException:
pass
log.loud('Opening dest handle.') log.loud('Opening dest handle.')
destination_handle = handlehelper(destination, 'wb') destination_handle = handlehelper(destination, 'wb')
@ -550,8 +570,8 @@ def copy_file(
chunk_time = time.perf_counter() - chunk_start chunk_time = time.perf_counter() - chunk_start
chunk_size = dynamic_chunk_sizer(chunk_size, chunk_time, IDEAL_CHUNK_TIME) chunk_size = dynamic_chunk_sizer(chunk_size, chunk_time, IDEAL_CHUNK_TIME)
if portalocker is not None: if source_file_lock is not None:
portalocker.unlock(source_handle) source_file_lock.release()
progressbar.done() progressbar.done()