#!/usr/bin/env node // A FAKE `findmnt`, driven by a control file the spec writes. // // `common/lib/storageVolumes.ts` asks the real binary exactly three questions, // and this honours those three call shapes and nothing else: // // 1. findmnt -J -T -o TARGET,SOURCE,FSTYPE,LABEL,UUID // → {"filesystems":[{target,source,fstype,label,uuid}]} (what mount is // this path on?) // 2. findmnt -rn -S UUID= -o TARGET // → one mountpoint per line, exit 1 when the volume is not mounted // 3. findmnt --fstab -S UUID= // → exit 1 MEANS "no such fstab entry", and the probe reads that exit // code as an answer rather than as a failure // // THE CONTROL FILE IS OPTIONAL. With no `/.fake-findmnt.json` // this reports nothing mounted and no fstab entries — which is what a spec that // never touches storage should see, and what `resetData()` leaves behind when // it wipes the tree. That "absent = knows nothing" default is deliberate: a // fake that guessed would make every unrelated spec depend on this file. // // Shape: // { // "mounts": [ // { "uuid": "...", "target": "/abs/mountpoint", // "source": "/dev/fake1", "fstype": "ext4", "label": "platter" } // ], // "fstab": ["", ...] // } import { readFileSync } from "node:fs"; import path from "node:path"; function control() { const dir = process.env.TRANSCRIPTS_DIR; if (!dir) return { mounts: [], fstab: [] }; try { const raw = readFileSync(path.join(dir, ".fake-findmnt.json"), "utf8"); const parsed = JSON.parse(raw); return { mounts: Array.isArray(parsed.mounts) ? parsed.mounts : [], fstab: Array.isArray(parsed.fstab) ? parsed.fstab : [], }; } catch { return { mounts: [], fstab: [] }; } } const argv = process.argv.slice(2); function valueAfter(flag) { const i = argv.indexOf(flag); return i >= 0 && i + 1 < argv.length ? argv[i + 1] : null; } const source = valueAfter("-S"); // "UUID=" const uuid = source && source.startsWith("UUID=") ? source.slice(5) : null; const { mounts, fstab } = control(); // 3. --fstab: is this volume in /etc/fstab? Exit 1 is the answer "no". if (argv.includes("--fstab")) { if (uuid && fstab.includes(uuid)) { process.stdout.write(`UUID=${uuid} /mnt/${uuid} ext4 defaults 0 2\n`); process.exit(0); } process.exit(1); } // 2. -S UUID= -o TARGET: where is this volume mounted right now? if (uuid) { const hit = mounts.find((m) => m.uuid === uuid); if (!hit || !hit.target) process.exit(1); process.stdout.write(`${hit.target}\n`); process.exit(0); } // 1. -T : which mount is this path on? Longest matching target wins, the // same way the kernel resolves it. if (argv.includes("-T")) { const target = valueAfter("-T"); const resolved = target ? path.resolve(target) : ""; let best = null; for (const m of mounts) { if (!m.target) continue; const mt = path.resolve(m.target); const under = resolved === mt || resolved.startsWith(`${mt}/`); if (!under) continue; if (!best || path.resolve(best.target).length < mt.length) best = m; } if (!best) { // Nothing claims it. The probe reads an empty filesystems array as // "identity unknown", which is the right fail-open answer. process.stdout.write(JSON.stringify({ filesystems: [] }) + "\n"); process.exit(0); } process.stdout.write( JSON.stringify({ filesystems: [ { target: path.resolve(best.target), source: best.source ?? "/dev/fake1", fstype: best.fstype ?? "ext4", label: best.label ?? "", uuid: best.uuid ?? "", }, ], }) + "\n", ); process.exit(0); } // Anything else (including --version) succeeds quietly: nothing in the probe // asks it, and a fake that failed loudly on an unknown shape would turn a new // call site into a mysterious "identity unknown" rather than a clear one. process.exit(0);