#!/usr/bin/env python3 """Observe byte counts and positions in disposable files, not crash recovery.""" import errno import hashlib import json import os import platform import resource import signal import subprocess import sys import tempfile from datetime import datetime, timezone from pathlib import Path def short_child(): # Restrict only this fresh child, never the calling shell or the parent. signal.signal(signal.SIGXFSZ, signal.SIG_IGN) with tempfile.TemporaryDirectory(prefix="hesela-short-") as directory: path = Path(directory) / "payload" fd = os.open(path, os.O_CREAT | os.O_EXCL | os.O_RDWR, 0o600) try: resource.setrlimit(resource.RLIMIT_FSIZE, (4, 4)) payload = b"ABCDEF" count = os.write(fd, payload) try: next_count = os.write(fd, payload[count:]) error = None except OSError as exc: next_count = None error = errno.errorcode.get(exc.errno, str(exc.errno)) return {"requestedBytes": len(payload), "returnedBytes": count, "remainingBytes": len(payload) - count, "retryReturnedBytes": next_count, "retryError": error, "readback": os.pread(fd, 16, 0).decode("ascii")} finally: os.close(fd) def position_case(directory, label, append, positional): path = Path(directory) / label fd = os.open(path, os.O_CREAT | os.O_EXCL | os.O_RDWR, 0o600) try: if os.write(fd, b"ABCD") != 4: raise RuntimeError("Fixture write did not complete") finally: os.close(fd) fd = os.open(path, os.O_RDWR | (os.O_APPEND if append else 0)) try: os.lseek(fd, 1, os.SEEK_SET) try: count = os.pwrite(fd, b"xy", 1) if positional else os.write(fd, b"xy") error = None except OSError as exc: count = None error = errno.errorcode.get(exc.errno, str(exc.errno)) current = os.lseek(fd, 0, os.SEEK_CUR) return {"case": label, "append": append, "positional": positional, "requestedOffset": 1, "offsetBefore": 1, "offsetAfter": current, "returnedBytes": count, "error": error, "readback": os.pread(fd, 16, 0).decode("ascii")} finally: os.close(fd) def observe(): if sys.platform != "linux": raise RuntimeError("This observation is scoped to Linux") child = subprocess.run([sys.executable, str(Path(__file__).resolve()), "--short-child"], check=True, capture_output=True, text=True, timeout=15) with tempfile.TemporaryDirectory(prefix="hesela-position-") as directory: fs = subprocess.run(["stat", "-f", "-c", "%T", directory], check=True, capture_output=True, text=True).stdout.strip() rows = [position_case(directory, "positional", False, True), position_case(directory, "append-write", True, False), position_case(directory, "append-positional", True, True)] return {"schemaVersion": 1, "generatedAt": datetime.now(timezone.utc).isoformat(), "sourceSha256": hashlib.sha256(Path(__file__).read_bytes()).hexdigest(), "environment": {"python": platform.python_version(), "kernelRelease": platform.release(), "libc": list(platform.libc_ver()), "temporaryFilesystem": fs}, "method": "Three sequential Python os write-position probes and one isolated RLIMIT_FSIZE child.", "limits": ["No fsync or power interruption.", "No concurrent writer or NFS test.", "No raw syscall or RWF_NOAPPEND test; os.pwrite uses the runtime library.", "Filesystem label does not describe all mount options.", "Resource limit is intentional, not a measured device fault."], "shortWrite": json.loads(child.stdout), "positions": rows} if __name__ == "__main__": print(json.dumps(short_child() if sys.argv[1:] == ["--short-child"] else observe(), indent=2) + "\n", end="")