The best way to control Claude Code, Codex and more from any computer or phone

main
ts 36 lines 1.61 KB
Raw
1 // @effect-diagnostics nodeBuiltinImport:off
2 // The service launcher shares this and runs without an Effect runtime.
3 import * as NodeChildProcess from "node:child_process";
4 import * as NodeFSP from "node:fs/promises";
5
6 const BTRFS_SUPER_MAGIC = 0x9123683e;
7
8 async function onBtrfs(target: string): Promise<boolean> {
9 // oxlint-disable-next-line t3code/no-global-process-runtime -- Shared with the standalone launcher.
10 if (process.platform !== "linux") return false;
11 const stats = await NodeFSP.statfs(target).catch(() => undefined);
12 return stats?.type === BTRFS_SUPER_MAGIC;
13 }
14
15 const run = (command: string, args: ReadonlyArray<string>) =>
16 new Promise<string | undefined>((resolve) => {
17 NodeChildProcess.execFile(command, args, (error, stdout) =>
18 resolve(error === null ? stdout : undefined),
19 );
20 });
21
22 /**
23 * SQLite rewrites pages in place, which fragments copy-on-write files on btrfs. Files created
24 * in a No_COW directory inherit the flag, as systemd-journald does for its journals. Node has no
25 * ioctl, so this runs chattr. Btrfs refuses reflinks between No_COW and copy-on-write files.
26 */
27 export async function markNoCopyOnWrite(directory: string): Promise<void> {
28 if (await onBtrfs(directory)) await run("chattr", ["+C", directory]);
29 }
30
31 export async function isNoCopyOnWrite(file: string): Promise<boolean> {
32 if (!(await onBtrfs(file))) return false;
33 // lsattr skips symlinks; its first field is the flag string, e.g. "---------------C------".
34 const output = await run("lsattr", ["-d", await NodeFSP.realpath(file).catch(() => file)]);
35 return output?.trimStart().split(/\s/, 1)[0]?.includes("C") ?? false;
36 }