Files
rea/src/ghidra/GhidraExecutableHeader.ts
morluto a891bb80ae refactor(filesystem): preserve admitted file facts
Return the validated descriptor and its original metadata from regular-file
admission. Propagate that contract through owned reads, hashes, snapshots,
artifact inventory and provider file workflows instead of replacing initial
facts with another stat. Keep arbitrary output-handle operations separate.

Preserve final descriptor/path identity proofs, borrowed-source verification,
BigInt precision, cancellation provenance and failed-close ownership. Remove
the obsolete source and owned-ZIP baseline stages and update their consumers
and regressions. Tie read completion faults to actual reads instead of the
retired duplicate stat; instrument real handles without fake handle wrappers.

Validation: fresh runtime/catalog build; focused source, lifecycle, filesystem,
CLI/MCP and authored provider-fixture tests; full static checks; docs checks;
test discovery; duplication check. Platform-specific cases remain skipped on
macOS. No installed provider or device behavior was verified.
2026-10-25 04:09:11 +08:00

89 lines
3.2 KiB
TypeScript

import { createHash } from "node:crypto";
import type { GhidraExecutableHeaderProbe } from "./GhidraInstallation.js";
import type { GhidraInstallationFileOptions } from "./GhidraInstallationProperties.js";
import { err, ok, type Result } from "../domain/result.js";
import type { AnalysisError } from "../domain/analysisErrorBase.js";
import { AnalysisResourceConstraintError } from "../domain/analysisErrorCore.js";
import { withArtifactRegularFile } from "../artifacts/ArtifactFileOperation.js";
import { readStableFilePrefix } from "../filesystem/readStableFileBytes.js";
import { ghidraFileFailure } from "./GhidraFileArtifacts.js";
/** Only this prefix is examined; the remainder is never retained or read. */
export const GHIDRA_EXECUTABLE_HEADER_BYTES = 4096;
/** Retain complete prefix observations independently of final descriptor cleanup. */
export const readGhidraExecutableHeader = async (
path: string,
options: GhidraInstallationFileOptions,
): Promise<Result<GhidraExecutableHeaderProbe, AnalysisError>> => {
let completedHeader: GhidraExecutableHeaderProbe | undefined;
try {
return ok(
await options.resourceOwner.run(() =>
withArtifactRegularFile(
path,
{
retain: (resource) =>
options.resourceOwner.retain(
{ close: () => resource.lease.release() },
resource.resource,
),
},
options.signal,
async ({ handle: file }) => {
const observed = await readStableFilePrefix(
file,
path,
GHIDRA_EXECUTABLE_HEADER_BYTES,
{
symlinks: "follow",
...(options.signal === undefined
? {}
: { signal: options.signal }),
},
);
if (observed.size > BigInt(Number.MAX_SAFE_INTEGER))
throw new AnalysisResourceConstraintError(
"ghidra:installation",
"file-size",
"Executable extent cannot be represented exactly by the header parser",
{
maximum_bytes: Number.MAX_SAFE_INTEGER,
observed_bytes: observed.size.toString(),
},
{
diagnostics: { path, stage: "native_decompiler_header" },
retryable: false,
},
);
completedHeader = {
bytes: observed.bytes,
size: Number(observed.size),
};
return completedHeader;
},
"follow",
),
),
);
} catch (cause: unknown) {
return err(
ghidraFileFailure(cause, "ghidra:installation", {
path,
stage: "native_decompiler_header",
...(completedHeader === undefined
? {}
: {
completed_header: {
file_bytes: completedHeader.size,
examined_bytes: completedHeader.bytes.length,
prefix_sha256: createHash("sha256")
.update(completedHeader.bytes)
.digest("hex"),
},
}),
}),
);
}
};