fix(reference): verify symlink identity around target reads

Bracket symlink target observation with the identity captured during enumeration. Report replacements and disappearing links as changed, and keep cancellation and I/O mapping at one owner boundary.

Remove duplicate target-error wrappers and unreachable external-target handling. Validation: reference reader filesystem suite passes, including deterministic replacement during observation.
This commit is contained in:
morluto
2026-10-12 04:33:47 +08:00
parent 09b6828516
commit 3d83135987
2 changed files with 114 additions and 69 deletions
@@ -1,10 +1,11 @@
import { mkdir, symlink, writeFile } from "node:fs/promises";
import { lstat, mkdir, symlink, unlink, writeFile } from "node:fs/promises";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { describe, expect, it } from "vitest";
import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js";
import { readStableSymlinkTarget } from "../../../src/reference/ReferenceSourceReaderEntries.js";
import { readReferenceSource } from "../../support/referenceSourceResourceScope.js";
describe("readReferenceSource entries", () => {
@@ -89,6 +90,39 @@ describe("readReferenceSource entries", () => {
);
});
it("marks a symlink changed when its path is replaced during target observation", async () => {
const root = await createTestTempDirectory("rea-reference-symlink-race-");
const link = join(root, "source-link");
await symlink("original-target", link);
const expected = await lstat(link, { bigint: true });
const result = await readStableSymlinkTarget(
link,
"source-link",
expected,
undefined,
async () => {
await unlink(link);
await symlink("replacement-target", link);
return {
status: "read",
kind: "symlink",
path: "source-link",
target: "original-target",
targetState: "missing",
};
},
);
expect(result).toEqual({
status: "failed",
kind: "symlink",
path: "source-link",
code: "changed",
message: "Symbolic link changed while its target was read",
});
});
it("reads a source file larger than the former 16 MiB ceiling", async () => {
const root = await createTestTempDirectory("rea-reference-large-");
const size = 16 * 1024 * 1024 + 1;