Files
firestar5683 ec8d7eb98b Contracts
Define persistent settings, typed schema fields and storage migrations before feature owners consume them. Keep calibration and parameter readers aligned with the shared data contracts.

Co-authored-by: Adeeb Shihadeh <adeebshihadeh@gmail.com>
Co-authored-by: Danny <dannyirwin31@yahoo.com>
Co-authored-by: Harald Schäfer <harald.the.engineer@gmail.com>
Co-authored-by: Prabhaav Pillai <prabhaav@msu.edu>
Co-authored-by: dirwin31 <83434411+dirwin31@users.noreply.github.com>
Co-authored-by: firestarsdog <229254897+firestarsdog@users.noreply.github.com>

Co-authored-by: haraschax <6804392+haraschax@users.noreply.github.com>

Co-authored-by: Dom <168790843+firestar5683@users.noreply.github.com>
Co-authored-by: prabhaavp <prabhaav@msu.edu>
2026-10-14 19:56:17 -05:00

35 lines
1.1 KiB
Python

"""Bounded saved-setting reads for the shared feature owner."""
import os
import stat
def read_saved(params, key: str, limit: int) -> tuple[bytes | None, bool]:
"""Return complete bytes/absence and readability; never authorize a prefix."""
path = params.get_param_path(key)
flags = os.O_RDONLY | os.O_NONBLOCK | getattr(os, "O_NOFOLLOW", 0)
try:
fd = os.open(path, flags)
except FileNotFoundError:
return None, True
except OSError:
return b"", False
try:
before = os.fstat(fd)
if not stat.S_ISREG(before.st_mode) or before.st_size > limit:
return b"", False
with os.fdopen(fd, "rb", closefd=False) as source:
raw = source.read(limit + 1)
after = os.fstat(fd)
current = os.stat(path, follow_symlinks=False)
if (len(raw) > limit or not stat.S_ISREG(after.st_mode) or
(before.st_dev, before.st_ino, before.st_size, before.st_mtime_ns) !=
(after.st_dev, after.st_ino, after.st_size, after.st_mtime_ns) or
(current.st_dev, current.st_ino) != (after.st_dev, after.st_ino)):
return b"", False
return raw, True
except OSError:
return b"", False
finally:
os.close(fd)