fs: match Node's lchmod error on macOS and make its test deterministic (#4010)

* - On macOS a failed lchmod reports the same syscall and message as Node.

- The fs-lchmod test prints the temp directory as a placeholder and the error syscall, so it compares the same on every run.

* Open a link for lchmod with write access, as Node does

* Answer a FIFO path for lchmod without parking the event loop
This commit is contained in:
Chris Tate
2026-11-21 09:07:14 -06:00
committed by GitHub
parent 20e4632079
commit 4e42a16319
3 changed files with 45 additions and 1 deletions
View File
+18
View File
@@ -3276,7 +3276,25 @@ static void scr_fs_lchmod_apply(const ScrDyn *path, const ScrDyn *mode) {
return;
}
double m = mode->kind == SCR_DYN_NUM ? mode->v.num : (double)strtol(mode->v.str->data, NULL, 8);
/* Node reaches a symbolic link's own entry on macOS by opening the path
* with O_SYMLINK and then fchmod(2), so a failure reports the step it
* happened in: a missing path is Node's "ENOENT: ..., open 'x'". The
* write access is Node's own (a writable but unreadable link is reached,
* and a directory fails the open with EISDIR rather than being chmodded).
* O_NONBLOCK keeps the inline call from waiting for a writer when the
* path is a FIFO: the open answers ENXIO instead of parking the loop. */
#ifdef O_SYMLINK
int fd = open(p->data, O_WRONLY | O_SYMLINK | O_NONBLOCK);
if (fd < 0) {
scr_fs_throw(errno, "open", p);
scr_str_release(p);
return;
}
if (fchmod(fd, (mode_t)m) != 0) scr_fs_throw(errno, "fchmod", p);
close(fd);
#else
if (lchmod(p->data, (mode_t)m) != 0) scr_fs_throw(errno, "lchmod", p);
#endif
scr_str_release(p);
}
#endif
+27 -1
View File
@@ -15,12 +15,38 @@ const { tmpdir } = require("node:os");
["file", () => fs.promises.lchmod(file, 0o600)],
["bad mode", () => fs.promises.lchmod(file, Number("nope"))],
];
// The temp directory's name is per process: messages print it as <dir> so
// the Node oracle and the compiled program compare equal.
const scrub = (text) => String(text).split(dir).join("<dir>");
for (const [label, run] of attempts) {
try {
await run();
console.log("lchmod", label, "ok");
} catch (err) {
console.log("lchmod", label, err.name, err.code, err.message);
console.log("lchmod", label, err.name, err.code, err.syscall, scrub(err.message));
}
}
if (process.platform === "darwin") {
// The operation is reached through an open with write access: a writable
// but unreadable link answers ok, and a directory fails that open.
const link = join(dir, "link.bin");
fs.symlinkSync(file, link);
fs.chmodSync(file, 0o200);
try {
await fs.promises.lchmod(link, 0o600);
console.log("lchmod no-read", "ok");
} catch (err) {
console.log("lchmod no-read", err.name, err.code, err.syscall, scrub(err.message));
}
fs.chmodSync(file, 0o600);
const sub = join(dir, "sub");
fs.mkdirSync(sub);
try {
await fs.promises.lchmod(sub, 0o600);
console.log("lchmod dir", "ok");
} catch (err) {
console.log("lchmod dir", err.name, err.code, err.syscall, scrub(err.message));
}
}