Skip to content

Commit d7a78f0

Browse files
Lucenx9maria-rcks
andauthored
fix(shared): allow atomic saves through 40 symlinks (pingdotgg#25604)
Co-authored-by: Simone <[email protected]> Co-authored-by: maria-rcks <[email protected]>
1 parent 4f990c4 commit d7a78f0

2 files changed

Lines changed: 32 additions & 1 deletion

File tree

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

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

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