diff --git a/backend/tests/utils/test_zip_cache.py b/backend/tests/utils/test_zip_cache.py index 8dc757d9e..1248ff857 100644 --- a/backend/tests/utils/test_zip_cache.py +++ b/backend/tests/utils/test_zip_cache.py @@ -4,6 +4,7 @@ import stat import threading import time from concurrent.futures import ThreadPoolExecutor +from pathlib import Path from zipfile import ZipFile import pytest @@ -162,15 +163,16 @@ def library(tmp_path, mocker): return lib -def build_library_zip(**overrides): - kwargs = { - "namespace": "42", - "entries": [ZipFileEntry("a.bin", "roms/a.bin", 4, 1000.0)], - "m3u_content": None, - "m3u_filename": None, - "cache_key": "key", - } - return build_cached_zip(**{**kwargs, **overrides}) +def build_library_zip( + m3u_content: bytes | None = None, m3u_filename: str | None = None +) -> Path: + return build_cached_zip( + namespace="42", + entries=[ZipFileEntry("a.bin", "roms/a.bin", 4, 1000.0)], + m3u_content=m3u_content, + m3u_filename=m3u_filename, + cache_key="key", + ) class TestBuildCachedZip: @@ -446,7 +448,7 @@ class TestEnsureZipfileWritable: class TestCachedZipReadability: - def test_build_leaves_archive_world_readable(self, library): + def test_build_leaves_archive_nginx_readable(self, library): result = build_library_zip() mode = stat.S_IMODE(result.stat().st_mode) assert mode & SERVED_FILE_MODE == SERVED_FILE_MODE @@ -466,12 +468,11 @@ class TestCachedZipReadability: assert mode & SERVED_FILE_MODE == SERVED_FILE_MODE def test_concurrent_builds_share_one_write(self, library, mocker): - # Release all three threads at the lock, so they are guaranteed to race - # for it rather than relying on a sleep to widen the window. + # Hold every thread until all three are at the lock, so the race is certain. barrier = threading.Barrier(3, timeout=10) original_flock = fcntl.flock - def synchronized_flock(fd, operation): + def synchronized_flock(fd: int, operation: int) -> None: barrier.wait() return original_flock(fd, operation) diff --git a/backend/utils/filesystem.py b/backend/utils/filesystem.py index 1946dbc41..3ce5a8ee2 100644 --- a/backend/utils/filesystem.py +++ b/backend/utils/filesystem.py @@ -12,8 +12,7 @@ COMPRESSED_FILE_EXTENSIONS: frozenset[str] = frozenset( (".7z", ".bz2", ".gz", ".rar", ".tar", ".zip", ".xz", ".tgz", ".tbz2", ".txz") ) -# Mode for files nginx serves directly: tempfile.mkstemp creates them 0600, -# which the unprivileged nginx user cannot read. +# tempfile.mkstemp creates files 0600, too narrow for the nginx user to read. SERVED_FILE_MODE = 0o644 diff --git a/backend/utils/zip_cache.py b/backend/utils/zip_cache.py index 0106a672c..1179fcb1a 100644 --- a/backend/utils/zip_cache.py +++ b/backend/utils/zip_cache.py @@ -89,11 +89,7 @@ class CachedZip(NamedTuple): def get_cached_zip(namespace: str, cache_key: str) -> CachedZip | None: - """Return the cached ZIP and the stat that found it, or None if it is missing. - - Callers get the stat so a hit costs one stat total rather than one per - caller that needs the size. - """ + """Return the cached ZIP and the stat that found it, or None if it is missing.""" path = _cache_file(namespace, cache_key) try: file_stat = os.stat(path) @@ -104,7 +100,7 @@ def get_cached_zip(namespace: str, cache_key: str) -> CachedZip | None: def _ensure_nginx_readable(path: Path, mode: int) -> None: - """Widen an archive an older build left owner-only, so nginx can read it.""" + """Widen a legacy owner-only archive so nginx can read it.""" if mode & SERVED_FILE_MODE == SERVED_FILE_MODE: return try: @@ -163,9 +159,8 @@ def build_cached_zip( ) -> Path: """Build a ZIP_STORED archive on disk and return its path. - Writes to a temp file in the same directory, then atomically renames to - the final path to prevent serving partial files. Concurrent requests for - the same namespace wait on one build. + Written to a temp file in the same directory and renamed, so a partial + file is never served. """ target = _cache_file(namespace, cache_key) target.parent.mkdir(parents=True, exist_ok=True)