Skip to content

Commit e04696f

Browse files
committed
fix(shared): allow atomic saves through 40 symlinks
1 parent becf853 commit e04696f

2 files changed

Lines changed: 33 additions & 1 deletion

File tree

‎packages/shared/src/atomicWrite.test.ts‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,37 @@ import { writeFileStringAtomically } from "./atomicWrite.ts";
1414
import * as HostProcess from "./HostProcess.ts";
1515

1616
it.layer(NodeServices.layer)("writeFileStringAtomically", (it) => {
17+
for (const hops of [39, 40, 41]) {
18+
it.effect.skipIf(HostProcess.Platform.defaultValue() !== "linux")(
19+
`matches native file access for a chain of ${hops} symlinks`,
20+
() =>
21+
Effect.gen(function* () {
22+
const fs = yield* FileSystem.FileSystem;
23+
const path = yield* Path.Path;
24+
const root = yield* fs.makeTempDirectoryScoped({ prefix: "t3-atomic-write-" });
25+
const destination = path.join(root, "settings.json");
26+
const first = path.join(root, "link-0");
27+
yield* fs.writeFileString(destination, "before");
28+
for (let index = hops - 1; index >= 0; index--) {
29+
const target = index === hops - 1 ? "settings.json" : `link-${index + 1}`;
30+
yield* fs.symlink(target, path.join(root, `link-${index}`));
31+
}
32+
33+
const read = yield* Effect.exit(fs.readFileString(first));
34+
const write = yield* Effect.exit(
35+
writeFileStringAtomically({ filePath: first, contents: "after" }),
36+
);
37+
38+
assert.strictEqual(Exit.isSuccess(write), Exit.isSuccess(read));
39+
assert.strictEqual(
40+
yield* fs.readFileString(destination),
41+
Exit.isSuccess(read) ? "after" : "before",
42+
);
43+
assert.strictEqual(yield* fs.readLink(first), "link-1");
44+
}),
45+
);
46+
}
47+
1748
it.effect("keeps a symlinked file linked and rewrites its destination", () =>
1849
Effect.gen(function* () {
1950
const fs = yield* FileSystem.FileSystem;

‎packages/shared/src/symlink.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ export const resolveSymlinkTarget = Effect.fn("symlink.resolveSymlinkTarget")(fu
2323
const fs = yield* FileSystem.FileSystem;
2424
const path = yield* Path.Path;
2525
let current = path.resolve(filePath);
26-
for (let hop = 0; hop < MAX_SYMLINK_HOPS; hop++) {
26+
// The terminal file needs a probe after the last permitted link is followed.
27+
for (let hop = 0; hop <= MAX_SYMLINK_HOPS; hop++) {
2728
const link = yield* fs.readLink(current).pipe(
2829
Effect.asSome,
2930
Effect.catchIf(isNotASymlink, () => Effect.succeedNone),

0 commit comments

Comments
 (0)