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
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
## 0.1.0 (2026/11/05)

- Create every missing parent directory of a lock with the requested group,
owner and mode `0o777`, so that other users can create locks in nested
directories.
- Require Python 3.11 or newer.

## 0.0.10 (2024/12/31)

- Address incompatibility with FileLock 3.15.3 causing the timeout parameter to
Expand Down
19 changes: 16 additions & 3 deletions multiuserfilelock/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,7 @@ def __init__(self, *args, user=None, group=None, chmod=0o666, **kwargs):
self._filelock = FileLock(*args, **kwargs)
self._lock_file_path = Path(self._filelock.lock_file)
parent = self._lock_file_path.parent
# Even though the "other write" permissions are enabled
# it seems that the operating systems disables that for the /tmp dir
parent.mkdir(mode=0o777, parents=True, exist_ok=True)
self._make_directories(parent)

if self._group is not None and parent.group() != self._group:
shutil.chown(parent, group=self._group)
Expand All @@ -59,6 +57,21 @@ def __init__(self, *args, user=None, group=None, chmod=0o666, **kwargs):
if self._user is not None and parent.owner() != self._user:
shutil.chown(parent, user=self._user)

def _make_directories(self, directory):
if directory.is_dir():
return
self._make_directories(directory.parent)
try:
directory.mkdir(mode=0o777)
except FileExistsError:
return
if os.name != 'nt':
directory.chmod(0o777)
if self._group is not None:
shutil.chown(directory, group=self._group)
if self._user is not None:
shutil.chown(directory, user=self._user)

def acquire(self, *args, **kwargs):
self._filelock.acquire(*args, **kwargs)
# once the lock has been acquired, we are more guaranteed that the
Expand Down
21 changes: 21 additions & 0 deletions multiuserfilelock/tests/test_acquire.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import os
import grp
from multiuserfilelock import MultiUserFileLock, Timeout
from threading import Thread
from queue import Queue
Expand Down Expand Up @@ -63,3 +65,22 @@ def thread_lock_release(lock):
assert not lock_thread2.is_alive()
lock2 = results.get(timeout=1)
assert lock2.is_locked


@pytest.mark.skipif(os.name == 'nt', reason='no unix groups')
def test_nested_directories_get_group_and_mode(tmp_path):
groups = [g for g in os.getgroups() if g != tmp_path.stat().st_gid]
if not groups:
pytest.skip('needs membership in a second group')
group = grp.getgrgid(groups[0]).gr_name
lock_filename = tmp_path / 'a' / 'b' / 'test.lock'
old_umask = os.umask(0o022)
try:
lock = MultiUserFileLock(lock_filename, group=group)
finally:
os.umask(old_umask)
lock.acquire()
lock.release()
for directory in [tmp_path / 'a', tmp_path / 'a' / 'b']:
assert directory.group() == group
assert directory.stat().st_mode & 0o777 == 0o777
Loading