fix: give data tree files the umask mode

Verified bytes reach their place through one helper that sets the mode a
plain open() gives and clears a read-only attribute. A data tree member
skipped both and landed 0600, unreadable by the emulator after a sudo
install. The expected digests of an entry are read by one function.
This commit is contained in:
Abdessamad Derraz committed 2026-11-21 06:04:39 +01:00
1 parent 83b1976903
commit c41fb9e609
10 files changed
+66 -48

No files matched your search

+1 -1
View File
@@ -16,7 +16,7 @@
# and Invoke-WebRequest throws on a cut transfer before the hash is read:
# the site's build rewrites this line there (scripts/sitebootstrap.py).
$mismatchAdvice = "The download was most likely cut short: try again. Within minutes of an update of this project the two files can come from caches of different ages. Waiting five minutes settles that."
$defaultInstallSha256 = "fef479226ae2eb2d35e83e78357c4a0fe1931133400ed2e82070c5910be2da74"
$defaultInstallSha256 = "85408a5c448b08de3f1a45ec5f830826f9cd079e1f18973c3cc117b62866c259"
# The pin above refuses any changed byte: this cap only stops a body that
# cannot be the installer (a sign-in portal's page) before it is hashed.
$maximumInstallerBytes = 8MB
+20 -16
View File
@@ -1794,6 +1794,15 @@ def _clear_read_only(dest: Path) -> None:
pass # the replace that follows reports the refusal with its reason
def _ready_to_place(written: Path, dest: Path, executable: bool) -> None:
"""Give verified bytes the mode a plain open() gives, executable where
the repository file is, and clear the read-only attribute of the file
they replace. The temporary file is private, and a folder written with
sudo has to stay readable by the emulator's user."""
os.chmod(written, (0o777 if executable else 0o666) & ~_UMASK)
_clear_read_only(dest)
def _replace_with_copy(src: Path, dest: Path) -> None:
"""Put a copy of src at dest's name without following what is there."""
fd, scratch = tempfile.mkstemp(dir=dest.parent, prefix=f".{dest.name}.", suffix=".part")
@@ -2780,6 +2789,11 @@ def fat32_drive(folder: Path) -> bool:
# --- installer/local_check.py ------------------------------------------------
def expected_hashes(entry: dict) -> dict[str, str]:
"""The SHA-256 and SHA-1 an entry records, lowercased."""
return {name: entry[name].lower() for name in ("sha256", "sha1") if entry.get(name)}
def _digest_file(path: Path, algorithms: tuple[str, ...]) -> dict[str, str]:
"""Compute several digests of a file in a single read.
@@ -2850,11 +2864,7 @@ def head_reads_done() -> None:
def _classify_local(entry: dict, dest: Path, remember: bool = False) -> str:
"""Return 'up_to_date' or 'mismatched' for a file already on disk.
remember keeps what was read for the check after the line menu."""
expected = {
name: entry.get(name, "").lower()
for name in ("sha256", "sha1")
if entry.get(name)
}
expected = expected_hashes(entry)
try:
is_file = dest.is_file()
except OSError:
@@ -9018,8 +9028,7 @@ def _place_verified(f: dict, bios_path: Path, dest: Path, tmp_path: Path, size:
for name, label in (("sha256", "SHA-256"), ("sha1", "SHA-1")):
if name in expected and actual[name] != expected[name]:
raise ValueError(f"{label} mismatch in {held_in}")
os.chmod(tmp_path, (0o777 if f.get("executable") else 0o666) & ~_UMASK)
_clear_read_only(dest)
_ready_to_place(tmp_path, dest, bool(f.get("executable")))
KEPT.keep(*_kept_slot(*_entry_place(bios_path, f)), dest, expected, tmp_path)
os.replace(tmp_path, dest)
@@ -9036,7 +9045,7 @@ def _write_verified(
are told apart from the write errors: a copy that could not be read
was reported as a folder not writable. named is the source in the
reason then, held_in when empty."""
expected = {name: f[name].lower() for name in ("sha256", "sha1") if f.get(name)}
expected = expected_hashes(f)
tmp_path: Path | None = None
counted = 0
@@ -16456,9 +16465,7 @@ def _stream_response(job: _FileFetch, resp: http.client.HTTPResponse,
def _place_fetched(job: _FileFetch, written: Path) -> None:
"""Check the bytes written against the list, then give them the file's name."""
f, dest = job.f, job.dest
expected = {
name: f[name].lower() for name in ("sha256", "sha1") if f.get(name)
}
expected = expected_hashes(f)
# A large file is hashed through the handle that holds its lock.
# On Windows that lock is mandatory: reopening the path to read
# it failed with a sharing violation, which read as an unwritable
@@ -16474,11 +16481,7 @@ def _place_fetched(job: _FileFetch, written: Path) -> None:
job.mismatch = "SHA-1 mismatch"
raise _Corrupt()
# The mode a plain open() gives, executable where the repository
# file is: the temporary file is private, and a BIOS folder
# written with sudo has to stay readable by the emulator's user.
os.chmod(written, (0o777 if f.get("executable") else 0o666) & ~_UMASK)
_clear_read_only(dest)
_ready_to_place(written, dest, bool(f.get("executable")))
# The file in place, a personal dump or another revision, is
# moved aside first: replaced, it was gone for good.
KEPT.keep(*_kept_slot(*_entry_place(job.bios_path, f)), dest, expected, written)
@@ -17137,6 +17140,7 @@ def _install_data_member(
listed = sha1 == meta["sha1"] and size == meta["size"]
if size != expected or not (listed or follow is not None):
return "changed", None
_ready_to_place(scratch, target, executable=False)
try:
KEPT.keep(*_kept_slot(bios_path, slot), target, {"sha1": sha1})
except _NotKept as exc:
+1 -1
View File
@@ -23,7 +23,7 @@ DEFAULT_INSTALL_URL="https://raw.githubusercontent.com/Abdess/retrobios/main/ins
# curl or wget stop on a cut transfer before the hash is read: the site's
# build rewrites this line there (scripts/sitebootstrap.py).
MISMATCH_ADVICE="The download was most likely cut short: try again. Within minutes of an update of this project the two files can come from caches of different ages. Waiting five minutes settles that."
DEFAULT_INSTALL_SHA256="fef479226ae2eb2d35e83e78357c4a0fe1931133400ed2e82070c5910be2da74"
DEFAULT_INSTALL_SHA256="85408a5c448b08de3f1a45ec5f830826f9cd079e1f18973c3cc117b62866c259"
# The pin above refuses any changed byte: this cap only stops a body that
# cannot be the installer (a sign-in portal's page) before it is hashed.
MAX_INSTALLER_BYTES=8388608
+8 -8
View File
@@ -27,8 +27,8 @@
{
"dest": "roms/ports/retrobios/install.py",
"repo_path": "install.py",
"size": 1450003,
"sha256": "fef479226ae2eb2d35e83e78357c4a0fe1931133400ed2e82070c5910be2da74",
"size": 1450226,
"sha256": "85408a5c448b08de3f1a45ec5f830826f9cd079e1f18973c3cc117b62866c259",
"executable": false
},
{
@@ -71,8 +71,8 @@
"muos": {
"archive": {
"dest": "ARCHIVE/RetroBIOS.muxapp",
"size": 1503628,
"sha256": "89ff71e3f740d54e774513bec1ad88c5fff566074dcf45bb38632f75a789e1fa"
"size": 1503851,
"sha256": "b094342bb630abe551ea6858f44cc0db586642878f03ec1ff107757a18a9b442"
},
"members": [
{
@@ -92,8 +92,8 @@
{
"name": "RetroBIOS/install.py",
"repo_path": "install.py",
"size": 1450003,
"sha256": "fef479226ae2eb2d35e83e78357c4a0fe1931133400ed2e82070c5910be2da74",
"size": 1450226,
"sha256": "85408a5c448b08de3f1a45ec5f830826f9cd079e1f18973c3cc117b62866c259",
"executable": false
},
{
@@ -166,8 +166,8 @@
{
"dest": ".config/modules/retrobios/install.py",
"repo_path": "install.py",
"size": 1450003,
"sha256": "fef479226ae2eb2d35e83e78357c4a0fe1931133400ed2e82070c5910be2da74",
"size": 1450226,
"sha256": "85408a5c448b08de3f1a45ec5f830826f9cd079e1f18973c3cc117b62866c259",
"executable": false
},
{
+2
View File
@@ -23,6 +23,7 @@ from installer.host_fs import (
_Interrupted,
_drive_full,
_folder_writable,
_ready_to_place,
_stop,
_write_refused,
_writing,
@@ -461,6 +462,7 @@ def _install_data_member(
listed = sha1 == meta["sha1"] and size == meta["size"]
if size != expected or not (listed or follow is not None):
return "changed", None
_ready_to_place(scratch, target, executable=False)
try:
KEPT.keep(*_kept_slot(bios_path, slot), target, {"sha1": sha1})
except _NotKept as exc:
+4 -11
View File
@@ -24,19 +24,18 @@ from installer.host_fs import (
NOT_WRITABLE,
READ_ONLY,
_SHARING_VIOLATIONS,
_UMASK,
_Interrupted,
_clear_read_only,
_drive_full,
_folder_writable,
_is_file,
_read_only_file,
_ready_to_place,
_stop,
_write_refused,
_writing,
)
from installer.invocation import offline_requested
from installer.local_check import _digest_file, _digest_handle, unreachable_words
from installer.local_check import _digest_file, _digest_handle, expected_hashes, unreachable_words
from installer.network import _retry_delay, network_words
from installer.offline import LOCAL, offline_words
from installer.outcome import HttpReason, NetworkReason, OfflineReason, _transfer_failure
@@ -382,9 +381,7 @@ def _stream_response(job: _FileFetch, resp: http.client.HTTPResponse,
def _place_fetched(job: _FileFetch, written: Path) -> None:
"""Check the bytes written against the list, then give them the file's name."""
f, dest = job.f, job.dest
expected = {
name: f[name].lower() for name in ("sha256", "sha1") if f.get(name)
}
expected = expected_hashes(f)
# A large file is hashed through the handle that holds its lock.
# On Windows that lock is mandatory: reopening the path to read
# it failed with a sharing violation, which read as an unwritable
@@ -400,11 +397,7 @@ def _place_fetched(job: _FileFetch, written: Path) -> None:
job.mismatch = "SHA-1 mismatch"
raise _Corrupt()
# The mode a plain open() gives, executable where the repository
# file is: the temporary file is private, and a BIOS folder
# written with sudo has to stay readable by the emulator's user.
os.chmod(written, (0o777 if f.get("executable") else 0o666) & ~_UMASK)
_clear_read_only(dest)
_ready_to_place(written, dest, bool(f.get("executable")))
# The file in place, a personal dump or another revision, is
# moved aside first: replaced, it was gone for good.
KEPT.keep(*_kept_slot(*_entry_place(job.bios_path, f)), dest, expected, written)
+9
View File
@@ -217,6 +217,15 @@ def _clear_read_only(dest: Path) -> None:
pass # the replace that follows reports the refusal with its reason
def _ready_to_place(written: Path, dest: Path, executable: bool) -> None:
"""Give verified bytes the mode a plain open() gives, executable where
the repository file is, and clear the read-only attribute of the file
they replace. The temporary file is private, and a folder written with
sudo has to stay readable by the emulator's user."""
os.chmod(written, (0o777 if executable else 0o666) & ~_UMASK)
_clear_read_only(dest)
def _replace_with_copy(src: Path, dest: Path) -> None:
"""Put a copy of src at dest's name without following what is there."""
fd, scratch = tempfile.mkstemp(dir=dest.parent, prefix=f".{dest.name}.", suffix=".part")
+6 -5
View File
@@ -15,6 +15,11 @@ if TYPE_CHECKING:
from installer.terminal import Terminal
def expected_hashes(entry: dict) -> dict[str, str]:
"""The SHA-256 and SHA-1 an entry records, lowercased."""
return {name: entry[name].lower() for name in ("sha256", "sha1") if entry.get(name)}
def _digest_file(path: Path, algorithms: tuple[str, ...]) -> dict[str, str]:
"""Compute several digests of a file in a single read.
@@ -85,11 +90,7 @@ def head_reads_done() -> None:
def _classify_local(entry: dict, dest: Path, remember: bool = False) -> str:
"""Return 'up_to_date' or 'mismatched' for a file already on disk.
remember keeps what was read for the check after the line menu."""
expected = {
name: entry.get(name, "").lower()
for name in ("sha256", "sha1")
if entry.get(name)
}
expected = expected_hashes(entry)
try:
is_file = dest.is_file()
except OSError:
+4 -6
View File
@@ -19,17 +19,16 @@ from installer.console import OUTPUT
from installer.host_fs import (
NOT_WRITABLE,
READ_ONLY,
_UMASK,
_Interrupted,
_clear_read_only,
_is_file,
_is_link,
_read_only_file,
_ready_to_place,
_stop,
_write_refused,
)
from installer.invocation import _INVOCATION
from installer.local_check import _digest_file
from installer.local_check import _digest_file, expected_hashes
from installer.paths import _MOVED_FOLDERS, _entry_path, _entry_place, _note_spelling, _safe_relative_path, spelled
from installer.text import _plural, format_size
@@ -386,8 +385,7 @@ def _place_verified(f: dict, bios_path: Path, dest: Path, tmp_path: Path, size:
for name, label in (("sha256", "SHA-256"), ("sha1", "SHA-1")):
if name in expected and actual[name] != expected[name]:
raise ValueError(f"{label} mismatch in {held_in}")
os.chmod(tmp_path, (0o777 if f.get("executable") else 0o666) & ~_UMASK)
_clear_read_only(dest)
_ready_to_place(tmp_path, dest, bool(f.get("executable")))
KEPT.keep(*_kept_slot(*_entry_place(bios_path, f)), dest, expected, tmp_path)
os.replace(tmp_path, dest)
@@ -404,7 +402,7 @@ def _write_verified(
are told apart from the write errors: a copy that could not be read
was reported as a folder not writable. named is the source in the
reason then, held_in when empty."""
expected = {name: f[name].lower() for name in ("sha256", "sha1") if f.get(name)}
expected = expected_hashes(f)
tmp_path: Path | None = None
counted = 0
+11
View File
@@ -192,6 +192,17 @@ class InstalledFileModes(unittest.TestCase):
finally:
os.umask(umask)
def test_a_data_tree_member_too(self):
data = b"cheat"
meta = {"size": len(data), "sha1": hashlib.sha1(data).hexdigest()}
with tempfile.TemporaryDirectory() as tmp:
target = Path(tmp) / "sub" / "file.ini"
target.parent.mkdir()
status, _ = install._install_data_member(
lambda: io.BytesIO(data), meta, target, Path(tmp), "data", "file.ini", [])
self.assertEqual(status, "installed")
self.assertEqual(target.stat().st_mode & 0o777, 0o666 & ~install._UMASK)
class ProgramsAreMarked(unittest.TestCase):
"""The manifest says which files are programs, read from their header: