"""Fixed root-owned locations shared by installer and runtime bootstrap.""" import json import os from pathlib import Path CODE = Path("/usr/lib/mission-core-node/insta360") SHARE = Path("/usr/share/mission-core-node/insta360") STATE = Path("/var/lib/mission-core-insta360") CONTROL = Path("/run/mission-core-x4-control") def trusted(path, directory=False): info = path.lstat() if path.is_symlink() or info.st_uid != 0 or info.st_mode & 0o022: raise RuntimeError("Untrusted camera installation path") if path.is_dir() != directory: raise RuntimeError("Unexpected camera installation entry") return path def manifest(): trusted(SHARE, True) return json.loads(trusted(SHARE / "bundle.json").read_text()) def active(): trusted(STATE, True) ident = trusted(STATE / "active.path").read_text().strip() if len(ident) != 24 or any(c not in "0123456789abcdef" for c in ident): raise RuntimeError("Invalid installed runtime revision") return trusted(STATE / "runtime", True) / ident def directory(path, mode=0o755): path.mkdir(mode=mode, exist_ok=True) trusted(path, True) def write(path, data, mode=0o644): import tempfile if path.exists() or path.is_symlink(): trusted(path) fd, temporary = tempfile.mkstemp(prefix=".x4-", dir=path.parent) try: with os.fdopen(fd, "wb") as stream: os.fchmod(stream.fileno(), mode) stream.write(data) stream.flush() os.fsync(stream.fileno()) os.replace(temporary, path) folder = os.open(path.parent, os.O_RDONLY) try: os.fsync(folder) finally: os.close(folder) finally: if os.path.exists(temporary): os.unlink(temporary)