mirror of
https://github.com/Abdess/retroarch_system.git
synced 2026-12-01 22:08:56 -06:00
fix: remove the games cache scratch on a failure
A cache write that failed left index.json.<pid>.part behind, one more on each run. The cache and the install record now write through one scratch file beside the target, removed when the write fails and named when it cannot be.
This commit is contained in:
1 parent
5d0c2fd3c6
commit
bf34e71804
8 files changed
+59
-43
No files matched your search
@@ -12,7 +12,7 @@ from pathlib import Path
|
||||
from typing import NamedTuple
|
||||
|
||||
from installer.cards import games_part
|
||||
from installer.host_fs import _is_dir
|
||||
from installer.host_fs import _is_dir, write_atomically
|
||||
from installer.network import _read_capped
|
||||
|
||||
CACHE_VARIABLE = "RETROBIOS_CACHE"
|
||||
@@ -64,9 +64,7 @@ def _cache_write(path: Path, data: bytes) -> None:
|
||||
costs a second read, never the run."""
|
||||
try:
|
||||
path.parent.mkdir(parents=True, exist_ok=True)
|
||||
scratch = path.with_name(f"{path.name}.{os.getpid()}.part")
|
||||
scratch.write_bytes(data)
|
||||
os.replace(scratch, path)
|
||||
write_atomically(path, data)
|
||||
except OSError as exc:
|
||||
if os.environ.get("RETROBIOS_DEBUG"):
|
||||
print(f" Not cached: {path} ({exc.strerror or exc})", file=sys.stderr)
|
||||
|
||||
@@ -239,6 +239,24 @@ def _replace_with_copy(src: Path, dest: Path) -> None:
|
||||
raise
|
||||
|
||||
|
||||
def write_atomically(path: Path, data: bytes) -> None:
|
||||
"""Write data at path through a scratch file beside it, removed when the
|
||||
write fails; a scratch that cannot be removed is named in the error."""
|
||||
fd, scratch = tempfile.mkstemp(dir=path.parent, prefix=f".{path.name}.", suffix=".part")
|
||||
try:
|
||||
with os.fdopen(fd, "wb") as handle:
|
||||
handle.write(data)
|
||||
os.replace(scratch, path)
|
||||
except OSError as exc:
|
||||
if os.path.lexists(scratch):
|
||||
try:
|
||||
os.unlink(scratch)
|
||||
except OSError as left:
|
||||
exc.strerror = (f"{exc.strerror or exc}, and {scratch} stays "
|
||||
f"({left.strerror or left})")
|
||||
raise
|
||||
|
||||
|
||||
# FILE_ATTRIBUTE_REPARSE_POINT: a junction, a mount point or a link on Windows.
|
||||
# Before Python 3.12 lstat does not report a junction as a link, and
|
||||
# os.walk(followlinks=False) descends into it.
|
||||
|
||||
@@ -9,14 +9,13 @@ import os
|
||||
import re
|
||||
import stat
|
||||
import sys
|
||||
import tempfile
|
||||
from collections.abc import Callable, Iterable
|
||||
from pathlib import Path, PurePosixPath
|
||||
from typing import NamedTuple
|
||||
|
||||
from installer.arguments import build_parser
|
||||
from installer.events import EVENTS
|
||||
from installer.host_fs import _clear_read_only, _is_dir, _is_link, _replace_with_copy
|
||||
from installer.host_fs import _clear_read_only, _is_dir, _is_link, _replace_with_copy, write_atomically
|
||||
from installer.invocation import _INVOCATION, _VALUED_OPTIONS, _command_rank, installer_id
|
||||
from installer.local_check import _digest_file
|
||||
from installer.offline import LOCAL
|
||||
@@ -345,23 +344,12 @@ def _save_record(folder: Path, data: dict, found: Path | None) -> tuple[Path | N
|
||||
places = [found] + [place for place in places if place != found]
|
||||
failure = "nothing could be written beside the folder"
|
||||
for path in places:
|
||||
scratch: Path | None = None
|
||||
try:
|
||||
path.parent.mkdir(exist_ok=True)
|
||||
_check_holder(path.parent)
|
||||
with tempfile.NamedTemporaryFile(
|
||||
"w", encoding="utf-8", dir=path.parent, prefix=f".{path.name}.",
|
||||
suffix=".part", delete=False) as handle:
|
||||
scratch = Path(handle.name)
|
||||
handle.write(text)
|
||||
os.replace(scratch, path)
|
||||
write_atomically(path, text.encode("utf-8"))
|
||||
except OSError as exc:
|
||||
failure = exc.strerror or str(exc)
|
||||
if scratch is not None and os.path.lexists(scratch):
|
||||
try:
|
||||
scratch.unlink()
|
||||
except OSError as left:
|
||||
failure += f", and {scratch} stays ({left.strerror or left})"
|
||||
continue
|
||||
return path, ""
|
||||
return None, failure
|
||||
|
||||
Reference in new issue
Block a user