mirror of
https://github.com/PR0M3TH3AN/SeedPass.git
synced 2025-09-09 15:58:48 +00:00
Add shared file lock and concurrency test
This commit is contained in:
@@ -4,7 +4,7 @@ import logging
|
||||
import traceback
|
||||
|
||||
try:
|
||||
from .file_lock import exclusive_lock
|
||||
from .file_lock import exclusive_lock, shared_lock
|
||||
from .key_derivation import derive_key_from_password, derive_key_from_parent_seed
|
||||
from .checksum import calculate_checksum, verify_checksum
|
||||
from .password_prompt import prompt_for_password
|
||||
@@ -20,5 +20,6 @@ __all__ = [
|
||||
"calculate_checksum",
|
||||
"verify_checksum",
|
||||
"exclusive_lock",
|
||||
"shared_lock",
|
||||
"prompt_for_password",
|
||||
]
|
||||
|
@@ -22,3 +22,25 @@ def exclusive_lock(
|
||||
lock = portalocker.Lock(str(path), mode="a+b", timeout=timeout)
|
||||
with lock as fh:
|
||||
yield fh
|
||||
|
||||
|
||||
@contextmanager
|
||||
def shared_lock(
|
||||
path: Path, timeout: Optional[float] = None
|
||||
) -> Generator[None, None, None]:
|
||||
"""Context manager that locks *path* with a shared lock.
|
||||
|
||||
The function opens the file in binary read/write mode and obtains a
|
||||
shared lock using ``portalocker``. If ``timeout`` is provided, acquiring
|
||||
the lock will wait for at most that many seconds before raising
|
||||
``portalocker.exceptions.LockException``.
|
||||
"""
|
||||
path = Path(path)
|
||||
path.parent.mkdir(parents=True, exist_ok=True)
|
||||
path.touch(exist_ok=True)
|
||||
lock = portalocker.Lock(
|
||||
str(path), mode="r+b", timeout=timeout, flags=portalocker.LockFlags.SHARED
|
||||
)
|
||||
with lock as fh:
|
||||
fh.seek(0)
|
||||
yield fh
|
||||
|
Reference in New Issue
Block a user