| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
import os |
| 4 |
import stat |
| 5 |
import sys |
| 6 |
from typing import TYPE_CHECKING, Final |
| 7 |
|
| 8 |
import pytest |
| 9 |
|
| 10 |
from filelock._util import break_lock_file, raise_on_not_writable_file |
| 11 |
|
| 12 |
if TYPE_CHECKING: |
| 13 |
from pathlib import Path |
| 14 |
|
| 15 |
from pytest_mock import MockerFixture |
| 16 |
|
| 17 |
_SKIP_AS_ROOT: Final[pytest.MarkDecorator] = pytest.mark.skipif( |
| 18 |
sys.platform != "win32" and os.geteuid() == 0, |
| 19 |
reason="root can write a 0o444 file", |
| 20 |
) |
| 21 |
|
| 22 |
|
| 23 |
def test_break_lock_file_unlinks_unchanged_file(tmp_path: Path) -> None: |
| 24 |
lock = tmp_path / "test.lock" |
| 25 |
lock.write_text("stale", encoding="utf-8") |
| 26 |
st = os.lstat(lock) |
| 27 |
break_lock_file(str(lock), st.st_mtime, st.st_ino) |
| 28 |
assert not lock.exists() |
| 29 |
assert list(tmp_path.glob("test.lock.break.*")) == [] |
| 30 |
|
| 31 |
|
| 32 |
def test_break_lock_file_preserves_file_when_mtime_advanced(tmp_path: Path) -> None: |
| 33 |
lock = tmp_path / "test.lock" |
| 34 |
lock.write_text("live", encoding="utf-8") |
| 35 |
# An mtime_before older than the file's real mtime models a peer recreating the lock after our stale read. |
| 36 |
# break_lock_file renames the live file aside but must not unlink it, so we never end with two live holders. |
| 37 |
break_lock_file(str(lock), mtime_before=0.0, ino_before=os.lstat(lock).st_ino) |
| 38 |
assert not lock.exists() |
| 39 |
leftover = list(tmp_path.glob("test.lock.break.*")) |
| 40 |
assert len(leftover) == 1 |
| 41 |
assert leftover[0].read_text(encoding="utf-8") == "live" |
| 42 |
|
| 43 |
|
| 44 |
def test_break_lock_file_preserves_file_when_inode_changed(tmp_path: Path) -> None: |
| 45 |
lock = tmp_path / "test.lock" |
| 46 |
lock.write_text("stale", encoding="utf-8") |
| 47 |
st = os.lstat(lock) |
| 48 |
# Model a coarse-granularity filesystem (NFS, FAT) where a peer broke and recreated the lock with a new inode |
| 49 |
# but the same mtime second. Creating the replacement while the original still exists guarantees a fresh inode. |
| 50 |
other = tmp_path / "recreated" |
| 51 |
other.write_text("live", encoding="utf-8") |
| 52 |
os.utime(other, ns=(st.st_atime_ns, st.st_mtime_ns)) |
| 53 |
assert os.lstat(other).st_ino != st.st_ino |
| 54 |
other.replace(lock) |
| 55 |
break_lock_file(str(lock), st.st_mtime, st.st_ino) |
| 56 |
leftover = list(tmp_path.glob("test.lock.break.*")) |
| 57 |
assert len(leftover) == 1 |
| 58 |
assert leftover[0].read_text(encoding="utf-8") == "live" |
| 59 |
|
| 60 |
|
| 61 |
def test_break_lock_file_aborts_if_break_path_vanishes(tmp_path: Path, mocker: MockerFixture) -> None: |
| 62 |
lock = tmp_path / "test.lock" |
| 63 |
lock.write_text("x", encoding="utf-8") |
| 64 |
ino = os.lstat(lock).st_ino |
| 65 |
mocker.patch("filelock._util.os.lstat", side_effect=FileNotFoundError) |
| 66 |
break_lock_file(str(lock), 0.0, ino) |
| 67 |
assert not lock.exists() |
| 68 |
assert len(list(tmp_path.glob("test.lock.break.*"))) == 1 |
| 69 |
|
| 70 |
|
| 71 |
def test_break_lock_file_missing_source_raises(tmp_path: Path) -> None: |
| 72 |
with pytest.raises(FileNotFoundError): |
| 73 |
break_lock_file(str(tmp_path / "nope.lock"), 0.0, 0) |
| 74 |
|
| 75 |
|
| 76 |
def test_break_lock_file_break_path_not_targetable_by_a_peer(tmp_path: Path, mocker: MockerFixture) -> None: |
| 77 |
lock = tmp_path / "test.lock" |
| 78 |
lock.write_text("stale", encoding="utf-8") |
| 79 |
st = os.lstat(lock) |
| 80 |
|
| 81 |
# A second breaker in the same process independently computes this name (no random token). If break_lock_file |
| 82 |
# used it too, the peer could rename a freshly recreated live lock onto our break path in the window between the |
| 83 |
# re-verify lstat and the unlink, and we would delete a live lock the inode check just approved. |
| 84 |
predictable = tmp_path / f"test.lock.break.{os.getpid()}" |
| 85 |
real_lstat = os.lstat |
| 86 |
|
| 87 |
def lstat_hook(path: str) -> os.stat_result: |
| 88 |
result = real_lstat(path) |
| 89 |
# break_lock_file lstats the break path exactly once, so this guard only ever runs its body (once). |
| 90 |
if ".break." in path and not predictable.exists(): # pragma: no branch # the peer recreates a live lock |
| 91 |
lock.write_text("live", encoding="utf-8") |
| 92 |
lock.rename(predictable) |
| 93 |
return result |
| 94 |
|
| 95 |
mocker.patch("filelock._util.os.lstat", side_effect=lstat_hook) |
| 96 |
break_lock_file(str(lock), st.st_mtime, st.st_ino) |
| 97 |
|
| 98 |
assert predictable.read_text(encoding="utf-8") == "live" |
| 99 |
|
| 100 |
|
| 101 |
@pytest.mark.skipif(sys.platform == "win32", reason="symlink-to-dir raises IsADirectoryError only on Unix") |
| 102 |
def test_raise_on_not_writable_file_does_not_follow_symlink_to_dir(tmp_path: Path) -> None: # pragma: win32 no cover |
| 103 |
target = tmp_path / "targetdir" |
| 104 |
target.mkdir() |
| 105 |
link = tmp_path / "my.lock" |
| 106 |
link.symlink_to(target) |
| 107 |
# Following the symlink would see a directory and raise IsADirectoryError; lstat sees the link itself. |
| 108 |
raise_on_not_writable_file(str(link)) |
| 109 |
assert stat.S_ISLNK(os.lstat(link).st_mode) |
| 110 |
|
| 111 |
|
| 112 |
@pytest.mark.skipif(sys.platform == "win32", reason="symlink + 0o444 semantics differ on Windows") |
| 113 |
@_SKIP_AS_ROOT # pragma: win32 no cover |
| 114 |
def test_raise_on_not_writable_file_does_not_follow_symlink_to_readonly(tmp_path: Path) -> None: |
| 115 |
target = tmp_path / "readonly" |
| 116 |
target.write_text("x", encoding="utf-8") |
| 117 |
target.chmod(0o444) |
| 118 |
link = tmp_path / "my.lock" |
| 119 |
link.symlink_to(target) |
| 120 |
# Following the symlink would see a read-only file and raise PermissionError; the link itself is writable. |
| 121 |
raise_on_not_writable_file(str(link)) |
| 122 |
|
| 123 |
|
| 124 |
@pytest.mark.skipif(sys.platform == "win32", reason="real dir raises PermissionError on Windows") |
| 125 |
def test_raise_on_not_writable_file_still_rejects_real_directory(tmp_path: Path) -> None: # pragma: win32 no cover |
| 126 |
path = tmp_path / "a_dir" |
| 127 |
path.mkdir() |
| 128 |
with pytest.raises(IsADirectoryError): # pragma: win32 no cover |
| 129 |
raise_on_not_writable_file(str(path)) |
| 130 |
|
| 131 |
|
| 132 |
@pytest.mark.skipif(sys.platform == "win32", reason="Windows does not have read only files in the same way") |
| 133 |
@_SKIP_AS_ROOT |
| 134 |
def test_raise_on_not_writable_file_still_rejects_readonly_file(tmp_path: Path) -> None: # pragma: win32 no cover |
| 135 |
path = tmp_path / "ro.lock" |
| 136 |
path.write_text("x", encoding="utf-8") |
| 137 |
path.chmod(0o444) |
| 138 |
try: # pragma: win32 no cover |
| 139 |
with pytest.raises(PermissionError): # pragma: win32 no cover |
| 140 |
raise_on_not_writable_file(str(path)) |
| 141 |
finally: |
| 142 |
path.chmod(0o644) |
| 143 |
|
| 144 |
|
| 145 |
# Writability follows the mode bits, so a read-only file is rejected whatever its mtime. The far-future row pins that |
| 146 |
# a later patch cannot narrow the check back to a single mtime range. The check reads the mode rather than probing |
| 147 |
# real access, so it holds for root too and needs no euid skip. |
| 148 |
@pytest.mark.parametrize("mtime", [0, 2_000_000_000], ids=["mtime-zero", "mtime-future"]) |
| 149 |
def test_raise_on_not_writable_file_rejects_readonly_file_any_mtime(tmp_path: Path, mtime: int) -> None: |
| 150 |
path = tmp_path / "ro.lock" |
| 151 |
path.write_text("x", encoding="utf-8") |
| 152 |
path.chmod(0o444) |
| 153 |
try: |
| 154 |
os.utime(path, (mtime, mtime)) |
| 155 |
with pytest.raises(PermissionError): |
| 156 |
raise_on_not_writable_file(str(path)) |
| 157 |
finally: |
| 158 |
path.chmod(0o644) |
| 159 |
|
| 160 |
|
| 161 |
@pytest.mark.skipif(sys.platform == "win32", reason="a real directory raises PermissionError on Windows") |
| 162 |
def test_raise_on_not_writable_file_rejects_directory_with_mtime_zero(tmp_path: Path) -> None: # pragma: win32 no cover |
| 163 |
path = tmp_path / "a_dir" |
| 164 |
path.mkdir() |
| 165 |
os.utime(path, (0, 0)) |
| 166 |
with pytest.raises(IsADirectoryError): # pragma: win32 no cover |
| 167 |
raise_on_not_writable_file(str(path)) |