refactor: restore large files with one copy

model.largefiles.restore_verified replaces generate_db _restore and
release restore _copy_verified: a scratch copy beside the target,
hashed, then renamed into place with a plain mode.

generate_db now refuses to put back a cached asset whose copy does not
hash to its database entry. The entry is still kept, as for an asset
the cache cannot supply. The restore script copies once instead of
through a second scratch.
This commit is contained in:
Abdessamad Derraz committed 2026-11-20 20:03:26 +01:00
1 parent ab0805e6cb
commit 4dc4dc0429
5 files changed
+51 -61

No files matched your search

+29 -1
View File
@@ -11,6 +11,7 @@ import json
import os
import sys
import re
import shutil
import tempfile
import time
import urllib.error
@@ -21,7 +22,7 @@ from collections.abc import Iterable
from pathlib import Path
from core.artifacts import set_plain_mode
from core.hashing import compute_hashes, parse_hash_list
from core.hashing import compute_hashes, file_digest, parse_hash_list
from core.paths import REPO_ROOT
@@ -160,6 +161,33 @@ def fetch_large_file(
return None
def restore_verified(source: str, path: str, sha1: str) -> bool:
"""Copy a cached asset to its registered path, only if the copy hashes to sha1.
The cache was hashed before the copy, and another run can replace a
cached name in between (a re-uploaded asset): the copy is what counts.
Whole or absent: a scan running beside a plain copy hashed the truncated
file and published that in the database.
"""
directory = os.path.dirname(os.path.abspath(path))
os.makedirs(directory, exist_ok=True)
handle, scratch = tempfile.mkstemp(
dir=directory, prefix=f".{os.path.basename(path)}.", suffix=".tmp"
)
os.close(handle)
try:
shutil.copy2(source, scratch)
if file_digest(Path(scratch)) != sha1:
print(f"Changed in the cache while restoring: {source}", file=sys.stderr)
return False
set_plain_mode(scratch)
os.replace(scratch, path)
return True
finally:
with contextlib.suppress(FileNotFoundError):
os.unlink(scratch)
def _download(name: str, tmp_path: str) -> bool:
"""Stream the first release URL of *name* that answers into *tmp_path*."""
# Only a name no candidate answers is reported: a spaced name is