fix(artifacts): normalize source loss during root hashing

This commit is contained in:
64579525
2026-10-12 16:52:05 +08:00
parent e6d4d7fb84
commit 9b8916152b
3 changed files with 74 additions and 22 deletions
+3 -14
View File
@@ -4,19 +4,12 @@ import {
nesContainerFormat,
parseInesLayout,
} from "../domain/inesContainer.js";
import {
access,
lstat,
realpath,
stat,
type FileHandle,
} from "node:fs/promises";
import { access, realpath, stat, type FileHandle } from "node:fs/promises";
import { extname, isAbsolute, resolve } from "node:path";
import { BinaryTargetError } from "../domain/configurationErrors.js";
import {
openRegularFile,
sameRegularFileState,
verifyRegularFileState,
} from "../filesystem/RegularFile.js";
import { AnalysisCancelledError } from "../domain/analysisErrorCore.js";
@@ -390,12 +383,8 @@ const sha256Handle = async (
throw new BinaryTargetError(path, "target grew while being read");
}
throwIfTargetResolutionCancelled(signal);
const [opened, currentPath] = await Promise.all([handle.stat(), lstat(path)]);
if (
!sameRegularFileState(initial, opened) ||
!sameRegularFileState(initial, currentPath) ||
position !== initial.size
)
await verifyRegularFileState(path, { handle, initial });
if (position !== initial.size)
throw new BinaryTargetError(path, "target changed while being read");
return hash.digest("hex");
};
@@ -1,4 +1,3 @@
import { lstat } from "node:fs/promises";
import type { Stats } from "node:fs";
import type { FileHandle } from "node:fs/promises";
import { readFileHandleChunks } from "../../filesystem/readFileHandleChunks.js";
@@ -9,7 +8,8 @@ import {
NonRegularFileReadError,
RegularFileAdmissionFailure,
openRegularFile,
sameRegularFileState,
RegularFileChangedError,
verifyRegularFileState,
} from "../../filesystem/RegularFile.js";
import {
abortIfNeeded,
@@ -105,12 +105,18 @@ export const hashStableRootArtifactHandle = async (
signal,
);
abortIfNeeded(signal);
const [opened, currentPath] = await Promise.all([handle.stat(), lstat(path)]);
if (
!sameRegularFileState(initial, opened) ||
!sameRegularFileState(initial, currentPath) ||
digest.bytes !== initial.size
)
try {
await verifyRegularFileState(path, { handle, initial });
} catch (cause: unknown) {
if (cause instanceof RegularFileChangedError)
throw new ArtifactReaderFailure(
"integrity",
`Root artifact changed during inventory: ${path}`,
{ cause: cause.cause ?? cause },
);
throw cause;
}
if (digest.bytes !== initial.size)
throw new ArtifactReaderFailure(
"integrity",
`Root artifact changed during inventory: ${path}`,