mirror of
https://github.com/morluto/rea.git
synced 2026-12-03 13:41:17 -05:00
d68383f98d
`isHostFile` folded every stat failure into "not a file", so a library candidate under a denied directory became a missing library, and an unprobeable apt release file or package file read as absent. `probeHostFile` reports ENOENT/ENOTDIR as absent and every other failure as unknown. Apt release discovery, the package-file check and the library walk keep those unknowns: a candidate under a directory the linker cannot search (EACCES) is kept as a last resort and reported unreadable rather than missing. Any other failure keeps its search position, since glibc's open_path continues only past ENOENT and EACCES. Fixes #9703 Squashed from #9722 and rebased onto main. Adapted on rebase: only EACCES counts as searched past (checked on glibc 2.36, where a looping symlink candidate ends the search), the probe reuses the domain LibraryFileProbe type, and a stray comment is dropped. Co-Authored-By: gensokyo-dev <342580529+gensokyo-dev@users.noreply.github.com>
116 lines
4.3 KiB
TypeScript
116 lines
4.3 KiB
TypeScript
import { stat } from "node:fs/promises";
|
|
|
|
import type { LibraryFileProbe } from "../domain/desktop/libraryPackages.js";
|
|
|
|
import { AnalysisCleanupLease } from "../artifacts/AnalysisCleanupLease.js";
|
|
import {
|
|
ArtifactFileCancelled,
|
|
ArtifactFileCleanupFailure,
|
|
artifactOperationCause,
|
|
} from "../artifacts/ArtifactFileOperation.js";
|
|
import {
|
|
RegularFileCleanupFailure,
|
|
RegularFileCancelled,
|
|
regularFileCancellation,
|
|
withOwnedRegularFile,
|
|
} from "../filesystem/OwnedRegularFile.js";
|
|
import {
|
|
NonRegularFileReadError,
|
|
RegularFileChangedError,
|
|
isSelectedRegularFileError,
|
|
} from "../filesystem/RegularFile.js";
|
|
import {
|
|
RegularFileCapacityError,
|
|
readStableFileBytes,
|
|
} from "../filesystem/readStableFileBytes.js";
|
|
|
|
const DENIED_CODES = new Set(["EACCES", "EPERM"]);
|
|
|
|
/** A read the inventory reports as unreadable rather than failing on. */
|
|
export const isRefusedRead = (cause: unknown): boolean =>
|
|
cause instanceof RegularFileCapacityError ||
|
|
cause instanceof RegularFileChangedError ||
|
|
cause instanceof NonRegularFileReadError ||
|
|
isSelectedRegularFileError(cause) ||
|
|
DENIED_CODES.has((cause as NodeJS.ErrnoException).code ?? "");
|
|
|
|
/** Preserve physical failures and normalize only cancellation recorded by the file owner. */
|
|
export const hostReadFailure = (
|
|
cause: unknown,
|
|
cancellationFailure?: (reason: unknown) => Error,
|
|
): unknown => {
|
|
if (cause instanceof RegularFileCleanupFailure)
|
|
return new ArtifactFileCleanupFailure(
|
|
cause,
|
|
new AnalysisCleanupLease(() => cause.owner.close(), [cause.path]),
|
|
);
|
|
const cancellation = regularFileCancellation(cause);
|
|
if (cancellation !== undefined && cancellationFailure !== undefined)
|
|
return cancellationFailure(cancellation.reason);
|
|
if (cause instanceof RegularFileCancelled)
|
|
return new ArtifactFileCancelled(cause);
|
|
return artifactOperationCause(cause);
|
|
};
|
|
|
|
/** A small host file's bytes, following links; `missing` when the path names nothing. */
|
|
export const readHostFile = async (
|
|
path: string,
|
|
maximumBytes: number,
|
|
signal: AbortSignal | undefined,
|
|
preserveCapacity = false,
|
|
cancellationFailure?: (reason: unknown) => Error,
|
|
): Promise<Buffer | "missing" | "unreadable"> => {
|
|
const options = {
|
|
symlinks: "follow" as const,
|
|
...(signal === undefined ? {} : { signal }),
|
|
};
|
|
try {
|
|
return await withOwnedRegularFile(path, options, ({ handle }) =>
|
|
readStableFileBytes(handle, path, maximumBytes, options),
|
|
);
|
|
} catch (cause: unknown) {
|
|
const failure = hostReadFailure(cause, cancellationFailure);
|
|
if (failure !== cause) throw failure;
|
|
if (preserveCapacity && cause instanceof RegularFileCapacityError)
|
|
throw cause;
|
|
// This caller requires the physical failure rather than an optional omission after abort.
|
|
if (cancellationFailure !== undefined && signal?.aborted === true)
|
|
throw cause;
|
|
if (!isRefusedRead(cause)) throw cause;
|
|
return (cause as NodeJS.ErrnoException).code === "ENOENT"
|
|
? "missing"
|
|
: "unreadable";
|
|
}
|
|
};
|
|
|
|
/**
|
|
* The stat failure that ld.so searches past: a candidate under a directory it
|
|
* cannot search. glibc's open_path continues only after ENOENT and EACCES, and
|
|
* fails the load on any other error (observed with glibc 2.36: a looping
|
|
* symlink candidate ends the search). An EACCES candidate is therefore not the
|
|
* object the linker loads with the same credentials, but REA's credentials may
|
|
* differ, so it stays unknown rather than absent.
|
|
*/
|
|
const UNREACHABLE_CODES = new Set(["EACCES"]);
|
|
|
|
/** The host file at `path`, with why a check could not tell it. */
|
|
export const probeHostFile = async (
|
|
path: string,
|
|
): Promise<LibraryFileProbe> => {
|
|
try {
|
|
return { kind: "file", isFile: (await stat(path)).isFile() };
|
|
} catch (cause: unknown) {
|
|
const code = (cause as NodeJS.ErrnoException).code ?? "";
|
|
// Only a missing name establishes that no file is there.
|
|
return code === "ENOENT" || code === "ENOTDIR"
|
|
? { kind: "file", isFile: false }
|
|
: { kind: "unknown", unreachable: UNREACHABLE_CODES.has(code) };
|
|
}
|
|
};
|
|
|
|
/** Whether a host path names a regular file, following links; null when the check could not tell, such as a denied parent folder. */
|
|
export const hostFileKind = async (path: string): Promise<boolean | null> => {
|
|
const probe = await probeHostFile(path);
|
|
return probe.kind === "file" ? probe.isFile : null;
|
|
};
|