@kya-os/cli
Version:
CLI for MCP-I setup and management
20 lines • 981 B
JavaScript
import { renameSync, rmSync, writeFileSync } from "fs";
/**
* Atomic, symlink-safe file replace.
*
* For `mcpi dco`, the destination and its temp sibling live in the current
* working tree, which may be an untrusted cloned repo. A tracked symlink
* planted at the predictable `<path>.mcpi-tmp` sibling would otherwise make
* a plain writeFileSync follow it and clobber an arbitrary file the user can
* write. So: unlink any pre-existing temp (rmSync removes the link itself,
* never its target), then create it with O_EXCL (`wx`) so a planted or
* racing symlink makes the write fail closed instead of being followed.
* renameSync replaces the destination without following a symlink there.
*/
export function writeFileAtomicSafe(path, content, mode = 0o644) {
const tempPath = `${path}.mcpi-tmp`;
rmSync(tempPath, { force: true });
writeFileSync(tempPath, content, { flag: "wx", mode });
renameSync(tempPath, path);
}
//# sourceMappingURL=fs-safe.js.map