Files
rea/src/filesystem/readStableFileBytes.ts
T
morluto 5112fe07ae fix(ghidra): admit installation metadata through owned bounded reads
Reject special files without blocking, cap the properties payload at 1 MiB before allocation, and verify descriptor extent and the selected name. Preserve Latin-1 data, selected symlinks, physical filesystem facts, completed observations and exact failed-close ownership.

Reuse the stable bounded reader for profile artifacts and consolidate Ghidra file failure projection. Distinguish missing-at-open from disappearance or read failure, and attribute typed prerequisite failures at the consuming operation.

Validation: 384 focused tests across 14 files, 42 seed/extension controls, 32 final metadata/profile/CLI controls, check:fast and docs:check. Controlled macOS file/process fixtures; no real Ghidra/JVM or Windows/Linux lifecycle verification.

Bounded FIFO reproduction: baseline direct and public doctor required external cleanup around 2.2s; candidate returned independently around 0.4/0.57s, all fixture groups verified empty. Warm normal doctor baseline/candidate: elapsed 1.211/1.230s, CPU 1.239/1.247s, sampled family RSS 524/524MB, reply tokens 3116/3116. No performance improvement claimed; true cold and whole-agent costs unmeasured.

Refs #4672
2026-10-17 12:02:26 +08:00

76 lines
2.4 KiB
TypeScript

import { lstat, stat, type FileHandle } from "node:fs/promises";
import {
assertRegularFileActive,
RegularFileChangedError,
sameRegularFileState,
} from "./RegularFile.js";
/** Resource refusal carries the observed extent before allocating file bytes. */
export class RegularFileCapacityError extends Error {
readonly _tag = "RegularFileCapacityError";
constructor(
readonly path: string,
readonly maximumBytes: number,
readonly observedBytes: bigint,
) {
super(`Selected file must be at most ${maximumBytes} bytes: ${path}`);
this.name = this._tag;
}
}
/** Read an admitted descriptor completely, checking its extent and selected name. */
export const readStableFileBytes = async (
file: FileHandle,
path: string,
maximumBytes: number,
options: {
readonly signal?: AbortSignal;
readonly symlinks: "follow" | "reject";
},
): Promise<Buffer> => {
if (!Number.isSafeInteger(maximumBytes) || maximumBytes < 0)
throw new RangeError(
"Maximum file bytes must be a nonnegative safe integer",
);
assertRegularFileActive(options.signal);
const before = await file.stat({ bigint: true });
if (before.size > BigInt(maximumBytes))
throw new RegularFileCapacityError(path, maximumBytes, before.size);
assertRegularFileActive(options.signal);
const bytes = Buffer.alloc(Number(before.size));
let offset = 0;
while (offset < bytes.length) {
assertRegularFileActive(options.signal);
const read = await file.read(bytes, offset, bytes.length - offset, offset);
if (read.bytesRead === 0) throw new RegularFileChangedError(path);
offset += read.bytesRead;
}
assertRegularFileActive(options.signal);
const extra = Buffer.alloc(1);
const named = async () => {
try {
return await (options.symlinks === "follow" ? stat : lstat)(path, {
bigint: true,
});
} catch (cause: unknown) {
if (
cause instanceof Error &&
"code" in cause &&
(cause.code === "ENOENT" ||
cause.code === "ENOTDIR" ||
cause.code === "ELOOP")
)
throw new RegularFileChangedError(path, { cause });
throw cause;
}
};
if (
(await file.read(extra, 0, 1, offset)).bytesRead !== 0 ||
!sameRegularFileState(before, await file.stat({ bigint: true })) ||
!sameRegularFileState(before, await named())
)
throw new RegularFileChangedError(path);
assertRegularFileActive(options.signal);
return bytes;
};