|
1 | 1 | import syncFs, { type Stats } from "node:fs"; |
2 | 2 | import fs, { type FileHandle } from "node:fs/promises"; |
| 3 | +import { FsSafeError } from "./errors.js"; |
| 4 | +import { sameFileIdentity } from "./file-identity.js"; |
3 | 5 |
|
4 | | -type AsyncTempFileSystem = Pick<typeof fs, "open" | "writeFile">; |
| 6 | +type AsyncTempFileSystem = Pick<typeof fs, "lstat" | "open" | "writeFile">; |
5 | 7 | type SyncTempFileSystem = Pick< |
6 | 8 | typeof syncFs, |
7 | | - "closeSync" | "fstatSync" | "fsyncSync" | "openSync" | "writeFileSync" |
| 9 | + "closeSync" | "fstatSync" | "fsyncSync" | "lstatSync" | "openSync" | "writeFileSync" |
8 | 10 | >; |
9 | 11 |
|
10 | 12 | export type SyncFchmod = (fd: number, mode: number) => void; |
11 | 13 |
|
| 14 | +function directoryOpenFlags(): number { |
| 15 | + return ( |
| 16 | + syncFs.constants.O_RDONLY | |
| 17 | + syncFs.constants.O_DIRECTORY | |
| 18 | + syncFs.constants.O_NOFOLLOW | |
| 19 | + syncFs.constants.O_NONBLOCK |
| 20 | + ); |
| 21 | +} |
| 22 | + |
| 23 | +function assertDirectory(identity: Stats, dirPath: string): void { |
| 24 | + if (identity.isSymbolicLink() || !identity.isDirectory()) { |
| 25 | + throw new FsSafeError("not-file", `Atomic replace parent must be a real directory: ${dirPath}`); |
| 26 | + } |
| 27 | +} |
| 28 | + |
| 29 | +function assertSameDirectory(expected: Stats, opened: Stats, dirPath: string): void { |
| 30 | + assertDirectory(opened, dirPath); |
| 31 | + if (!sameFileIdentity(expected, opened)) { |
| 32 | + throw new FsSafeError( |
| 33 | + "path-mismatch", |
| 34 | + `Atomic replace parent changed before its mode could be applied: ${dirPath}`, |
| 35 | + ); |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +export async function applyDirectoryMode(params: { |
| 40 | + fsModule: AsyncTempFileSystem; |
| 41 | + dirPath: string; |
| 42 | + mode: number; |
| 43 | +}): Promise<void> { |
| 44 | + // Node does not enforce POSIX directory modes on Windows, and its directory |
| 45 | + // descriptors are not consistently openable. mkdir(mode) remains the only |
| 46 | + // bounded behavior there; never fall back to a pathname chmod. |
| 47 | + if (process.platform === "win32") { |
| 48 | + return; |
| 49 | + } |
| 50 | + |
| 51 | + const expected = await params.fsModule.lstat(params.dirPath); |
| 52 | + assertDirectory(expected, params.dirPath); |
| 53 | + const handle = await params.fsModule.open(params.dirPath, directoryOpenFlags()); |
| 54 | + try { |
| 55 | + assertSameDirectory(expected, await handle.stat(), params.dirPath); |
| 56 | + await handle.chmod(params.mode); |
| 57 | + } finally { |
| 58 | + await handle.close(); |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +export function applyDirectoryModeSync(params: { |
| 63 | + fsModule: SyncTempFileSystem; |
| 64 | + dirPath: string; |
| 65 | + mode: number; |
| 66 | + fchmodSync?: SyncFchmod; |
| 67 | +}): void { |
| 68 | + if (process.platform === "win32" || !params.fchmodSync) { |
| 69 | + return; |
| 70 | + } |
| 71 | + |
| 72 | + const expected = params.fsModule.lstatSync(params.dirPath); |
| 73 | + assertDirectory(expected, params.dirPath); |
| 74 | + const fd = params.fsModule.openSync(params.dirPath, directoryOpenFlags()); |
| 75 | + try { |
| 76 | + assertSameDirectory(expected, params.fsModule.fstatSync(fd), params.dirPath); |
| 77 | + params.fchmodSync(fd, params.mode); |
| 78 | + } finally { |
| 79 | + params.fsModule.closeSync(fd); |
| 80 | + } |
| 81 | +} |
| 82 | + |
12 | 83 | export async function writeTempFile(params: { |
13 | 84 | fsModule: AsyncTempFileSystem; |
14 | 85 | tempPath: string; |
|
0 commit comments