Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Raise when trying to acquire in R/O or missing folder #96

Merged
merged 5 commits into from
Sep 30, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 9 additions & 2 deletions src/filelock/_soft.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import os
import sys
from errno import EACCES, ENOENT, EPERM

from ._api import BaseFileLock

PermissionError = PermissionError if sys.version_info[0] == 3 else OSError


class SoftFileLock(BaseFileLock):
"""Simply watches the existence of the lock file."""
Expand All @@ -10,8 +14,11 @@ def _acquire(self):
open_mode = os.O_WRONLY | os.O_CREAT | os.O_EXCL | os.O_TRUNC
try:
fd = os.open(self._lock_file, open_mode)
except OSError:
pass
except OSError as exception:
if exception.errno in (EPERM, EACCES, ENOENT):
raise
if sys.platform == "win32" and not os.access(self._lock_file, os.W_OK):
raise PermissionError("Permission denied")
gaborbernat marked this conversation as resolved.
Show resolved Hide resolved
else:
self._lock_file_fd = fd

Expand Down
6 changes: 4 additions & 2 deletions src/filelock/_windows.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os
from errno import EACCES, ENOENT, EPERM

from ._api import BaseFileLock

Expand All @@ -15,8 +16,9 @@ def _acquire(self):
open_mode = os.O_RDWR | os.O_CREAT | os.O_TRUNC
try:
fd = os.open(self._lock_file, open_mode)
except OSError:
pass
except OSError as exception:
if exception.errno in (EPERM, EACCES, ENOENT):
raise
else:
try:
msvcrt.locking(fd, msvcrt.LK_NBLCK, 1)
Expand Down
50 changes: 50 additions & 0 deletions tests/test_filelock.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,15 @@
import logging
import sys
import threading
from contextlib import contextmanager
from stat import S_IWGRP, S_IWOTH, S_IWUSR

import pytest

from filelock import FileLock, SoftFileLock, Timeout

PermissionError = PermissionError if sys.version_info[0] == 3 else OSError


@pytest.mark.parametrize("lock_type", [FileLock, SoftFileLock])
def test_simple(lock_type, tmp_path, caplog):
Expand All @@ -29,6 +33,52 @@ def test_simple(lock_type, tmp_path, caplog):
assert [r.levelno for r in caplog.records] == [logging.DEBUG, logging.DEBUG, logging.DEBUG, logging.DEBUG]


@contextmanager
def make_ro(path):
write = S_IWUSR | S_IWGRP | S_IWOTH
path.chmod(path.stat().st_mode & ~write)
yield
path.chmod(path.stat().st_mode | write)


@pytest.fixture()
def tmp_path_ro(tmp_path):
with make_ro(tmp_path):
yield tmp_path


@pytest.mark.parametrize("lock_type", [FileLock, SoftFileLock])
@pytest.mark.skipif(sys.platform == "win32", reason="Windows does not have read only folders")
def test_ro_folder(lock_type, tmp_path_ro):
lock = lock_type(str(tmp_path_ro / "a"))
with pytest.raises(PermissionError, match="Permission denied"):
lock.acquire()


@pytest.fixture()
def tmp_file_ro(tmp_path):
filename = tmp_path / "a"
filename.write_text("")
with make_ro(filename):
yield filename


@pytest.mark.parametrize("lock_type", [FileLock, SoftFileLock])
def test_ro_file(lock_type, tmp_file_ro):
lock = lock_type(str(tmp_file_ro))
with pytest.raises(PermissionError, match="Permission denied"):
lock.acquire()


@pytest.mark.parametrize("lock_type", [FileLock, SoftFileLock])
def test_missing_directory(lock_type, tmp_path_ro):
lock_path = tmp_path_ro / "a" / "b"
lock = lock_type(str(lock_path))

with pytest.raises(OSError, match="No such file or directory:"):
lock.acquire()


@pytest.mark.parametrize("lock_type", [FileLock, SoftFileLock])
def test_nested_context_manager(lock_type, tmp_path):
# lock is not released before the most outer with statement that locked the lock, is left
Expand Down
7 changes: 7 additions & 0 deletions whitelist.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,18 @@ autodoc
autosectionlabel
caplog
creat
eacces
enoent
eperm
exc
fcntl
filelock
fmt
intersphinx
intervall
iwgrp
iwoth
iwusr
levelno
lk
lockfile
Expand All @@ -18,6 +24,7 @@ nitpicky
param
pygments
rdwr
ro
skipif
src
tmp
Expand Down