"""Reading the tail of a file that may be arbitrarily large. Both the audit log and an application's captured output are append-only files with no upper bound in M0, and both are read by verbs that must stay cheap. Reading either whole — ``readlines()`` on a gigabyte of Chromium stderr — would turn a ``status`` into an out-of-memory event on a machine that already has no resource caps (DESIGN.md §3.3). So every tail read is bounded in bytes, and says when it truncated. """ from __future__ import annotations import os from pathlib import Path #: The most any single tail read will pull into memory. DEFAULT_TAIL_BYTES = 1 << 20 # 1 MiB def tail_lines( path: Path | str, limit: int, *, max_bytes: int = DEFAULT_TAIL_BYTES ) -> tuple[list[str], bool, int]: """The last ``limit`` lines of ``path``. Returns ``(lines, truncated, size)``. ``truncated`` is true when the window did not reach the start of the file, so a caller can say so rather than implying it showed everything. """ if limit <= 0: try: return [], False, os.stat(path).st_size except OSError: return [], False, 0 try: with open(path, "rb") as fh: size = fh.seek(0, os.SEEK_END) window = min(size, max_bytes) fh.seek(size - window) data = fh.read(window) except OSError: return [], False, 0 partial = window < size if partial: # The window almost certainly starts mid-line; drop that fragment # rather than reporting a half line as if it were whole. newline = data.find(b"\n") data = data[newline + 1 :] if newline != -1 else b"" lines = data.decode("utf-8", "replace").splitlines() truncated = partial or len(lines) > limit return lines[-limit:], truncated, size