Files
romm/backend/utils/filesystem.py
Georges-Antoine Assi dd7f46b52c fix: make metadata exports and matching follow the library structure
Testing the custom library structure against a live library turned up four
defects, all of them a path being reduced to a bare file name.

Both exporters wrote a rom's `fs_name` alone, so every game a template nests
was exported as if it sat in the platform folder: ES-DE and Pegasus could not
find any of them. They now write the rom's path relative to the platform
folder, the folder the metadata files themselves live in, and copy exported
media into the matching subfolder so two identically named roms keep their own
artwork.

Merging into an existing gamelist.xml / metadata.pegasus.txt keyed entries by
file name too, so identically named roms in different folders both merged into
the first entry and produced duplicates. Entries are now keyed by path, with a
Pegasus block matched by the folder its listed files sit in.

Reading a gamelist.xml had the same collision: two roms sharing a file name
both took the metadata of whichever entry was parsed last. A rom's own path now
wins, and a bare file name stays a fallback key for the tools that write those,
dropped when two entries share it.

Default discovery surfaced hidden folders as multi-file games, unlike
structured discovery which skips them. exclude_multi_roms now owns that rule
for both.

Also validate `exclude`'s exclusion_type: an unknown value reached
`Config.__getattribute__` and returned a 500 rather than a 422.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-09-08 07:31:05 -04:00

150 lines
5.2 KiB
Python

import errno
import os
import re
import shutil
from collections.abc import Iterator
from pathlib import Path
# Container file extensions treated as compressed archives across modules
# (roms_handler for hashing decisions, rahasher for skipping disc-platform
# buffer-hash attempts, feeds for PKGi passthrough).
COMPRESSED_FILE_EXTENSIONS: frozenset[str] = frozenset(
(".7z", ".bz2", ".gz", ".rar", ".tar", ".zip", ".xz", ".tgz", ".tbz2", ".txz")
)
# tempfile.mkstemp creates files 0600, too narrow for the nginx user to read.
SERVED_FILE_MODE = 0o644
def iter_files(path: str, recursive: bool = False) -> Iterator[tuple[Path, str]]:
"""List files in a directory.
Yields tuples where the first element is the path to the directory where the file is located,
and the second element is the name of the file.
"""
for root, _, files in os.walk(path, topdown=True):
for file in files:
yield Path(root), file
if not recursive:
break
def iter_directories(path: str, recursive: bool = False) -> Iterator[tuple[Path, str]]:
"""List directories in a directory.
Yields tuples where the first element is the path to the directory where the directory is located,
and the second element is the name of the directory.
"""
for root, dirs, _ in os.walk(path, topdown=True):
for directory in dirs:
yield Path(root), directory
if not recursive:
break
# errno values that mean "hardlink not possible here, fall back to copy".
# EXDEV: cross-device link. EPERM: filesystem doesn't permit/support hardlinks
# (e.g. FAT32, exFAT, some network mounts). EOPNOTSUPP/ENOTSUP: same, on BSD/macOS.
# EMLINK: source already has the maximum number of hardlinks for the filesystem.
_LINK_FALLBACK_ERRNOS: frozenset[int] = frozenset(
e
for e in (
getattr(errno, "EXDEV", None),
getattr(errno, "EPERM", None),
getattr(errno, "EOPNOTSUPP", None),
getattr(errno, "ENOTSUP", None),
getattr(errno, "EMLINK", None),
getattr(errno, "EACCES", None),
)
if e is not None
)
def rel_platform_folder(fs_path: str, platform_fs_path: str) -> str:
"""The part of a rom's folder that sits below its platform folder.
Args:
fs_path: The rom's folder.
platform_fs_path: The platform folder holding the metadata export files.
Returns:
`Disks/Set A` for a rom nested by a custom library structure, empty for one
sitting directly in the platform folder.
"""
rel = fs_path.removeprefix(f"{platform_fs_path}/")
return "" if rel in (fs_path, platform_fs_path) else rel
def join_rel_path(*parts: str) -> str:
"""Join the non-empty parts of a path written into an export file."""
return "/".join(part for part in parts if part)
def link_or_copy_file(source: Path, dest: Path) -> None:
"""Place ``source`` at ``dest`` via hardlink (preferred) or copy (fallback),
atomically replacing ``dest`` if it already exists. Caller is responsible
for creating ``dest.parent``.
Hardlinking is preferred because it's instantaneous and uses no extra disk
space, but only works within a single filesystem. If linking isn't possible,
we transparently fall back to ``shutil.copy2`` (preserving metadata).
Overwriting is atomic: we link/copy to a tempfile in dest's directory, then
rename it onto dest, which mirrors shutil.copy2's overwrite-on-exists
behavior.
"""
tmp_path = dest.parent / f".romm_link_tmp_{os.urandom(8).hex()}"
try:
try:
os.link(source, tmp_path)
except OSError as exc:
if exc.errno not in _LINK_FALLBACK_ERRNOS:
raise
shutil.copy2(source, tmp_path)
os.replace(tmp_path, dest)
except BaseException:
try:
os.unlink(tmp_path)
except OSError:
pass
raise
INVALID_CHARS_HYPHENS = re.compile(r"[\\/:|]")
INVALID_CHARS_EMPTY = re.compile(r'[*?"<>+]')
def sanitize_filename(filename: str) -> str:
"""
Replace invalid characters in the filename to make it valid across common filesystems
and prevent path-traversal attacks.
Args:
- filename (str): The filename to sanitize.
Returns:
- str: The sanitized filename (always a bare name, never a path).
Raises:
- ValueError: If the filename is empty or a reserved traversal name.
"""
# Strip directory components first to neutralise path-traversal payloads
sanitized_filename = os.path.basename(filename)
# Replace some invalid characters with hyphen
sanitized_filename = INVALID_CHARS_HYPHENS.sub("-", sanitized_filename)
# Remove other invalid characters
sanitized_filename = INVALID_CHARS_EMPTY.sub("", sanitized_filename)
# Ensure null bytes are not included (ZFS allows any characters except null bytes)
sanitized_filename = sanitized_filename.replace("\0", "")
# Remove leading/trailing whitespace
sanitized_filename = sanitized_filename.strip()
# Reject empty, current-directory, or parent-directory names
if not sanitized_filename or sanitized_filename in (".", ".."):
raise ValueError("Filename cannot be empty after sanitization")
return sanitized_filename