commit 0cbb98a0e47f0642bf7bd4bedcb329b3a48845a2
parent 2ce579cb32865e0bc92195e223f80847c553b6f3
Author: I Mean I'm Just Saying <imeanimjustsaying@kiwifarms.st>
Date: Fri, 30 Oct 2026 21:11:40 -0400
umtool: a frame link at the filesystem's cap becomes one copy (EMLINK)
Identical frames are deduplicated onto one inode -- a cut's blank fx frames
are ~53,000 links to one PNG -- and each take built --from another links
them again, so the inode reaches ext4's 65,000-link cap within a few takes.
The Ember cut's draft-17 refused at its first fx frame. makeLinker()
(report-to-video/link-or-copy.mjs) links, and on EMLINK or EXDEV copies the
source once and links the rest of that inode's frames to the copy. Used by
--from's frame and file links and by the chrome frame reuse.
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Diffstat:
5 files changed, 138 insertions(+), 6 deletions(-)
diff --git a/umtool/lib/report/takeFrom.mjs b/umtool/lib/report/takeFrom.mjs
@@ -60,11 +60,12 @@
//
// Plain node; the CLI runs it with bare node.
-import { link, lstat, mkdir, readFile, readdir, rename, rm, writeFile } from "node:fs/promises";
+import { lstat, mkdir, readFile, readdir, rename, rm, writeFile } from "node:fs/promises";
import path from "node:path";
import { DEFAULT_VARIANT, SEGMENT_FILE_SUFFIXES, VARIANTS, XFADE_FROM_FILE } from "umtool-report-to-video/build-video";
import { CHUNKS_DIR, CHUNKS_RECORD, HASH_MEMO_FILE } from "umtool-report-to-video/chunked-encode";
import { LADDER_CHUNKS_DIR, LADDER_DIR, isLadderChunkFile } from "umtool-report-to-video/ladder-chunks";
+import { makeLinker } from "umtool-report-to-video/link-or-copy";
import { KEEP_FRAMES_DIR, keptFramesDir } from "umtool-report-to-video/prune-frames";
import { seedLadderFit } from "umtool-report-to-video/variants";
import { takesDirOf } from "./takes.mjs";
@@ -82,7 +83,7 @@ async function linkFile(src, dst) {
if (b && b.ino === a.ino && b.dev === a.dev) return false;
const tmp = path.join(path.dirname(dst), `.${path.basename(dst)}.${process.pid}.link`);
await rm(tmp, { force: true });
- await link(src, tmp);
+ await makeLinker()(src, tmp);
await rename(tmp, dst);
return true;
}
@@ -92,10 +93,12 @@ async function linkFrames(src, dst) {
await rm(dst, { recursive: true, force: true });
await mkdir(dst, { recursive: true });
let n = 0;
+ // A deduplicated frame may be at the filesystem's link cap: one copy stands in (link-or-copy.mjs).
+ const linkOne = makeLinker();
for (const f of await names(src)) {
const p = path.join(src, f);
if (!(await isFile(p))) continue;
- await link(p, path.join(dst, f));
+ await linkOne(p, path.join(dst, f));
n += 1;
}
return n;
diff --git a/umtool/report-to-video/compose-chrome.mjs b/umtool/report-to-video/compose-chrome.mjs
@@ -31,12 +31,13 @@
// and the crossfade -- so the build writes the schedule and this reads it.
// Recomputing it here would be a second implementation of segmentOffsets() and
// would drift the first time the transition changed.
-import { copyFile, link, mkdir, readdir, readFile, rename, rm, stat, writeFile } from "node:fs/promises";
+import { copyFile, mkdir, readdir, readFile, rename, rm, stat, writeFile } from "node:fs/promises";
import { execFile, spawn } from "node:child_process";
import { promisify } from "node:util";
import path from "node:path";
import { dateKey } from "./ledger-totals.mjs";
+import { makeLinker } from "./link-or-copy.mjs";
import { selectVariant } from "./build-video.mjs";
import { ensureWriteDir } from "../lib/report/storage.mjs";
import { chromeCacheKey, frameCount, pageDuration, hyperframesCommand, sha256, transitionOf } from "./deck.mjs";
@@ -1230,8 +1231,10 @@ async function reuseFrames({ base, target, stem, fk, key, html, region, rate, hf
await rm(next, { recursive: true, force: true });
await mkdir(next, { recursive: true });
// (In batches: a whole cut is ~50,000 links, one after another a few seconds.)
+ // A frame deduplicated onto one inode may be at the filesystem's link cap: one copy stands in (link-or-copy.mjs).
+ const linkOne = makeLinker();
for (let i = 0; i < fk.length; i += 512) {
- await Promise.all(pick.slice(i, i + 512).map((src, j) => link(src, path.join(next, frameName(i + j + 1)))));
+ await Promise.all(pick.slice(i, i + 512).map((src, j) => linkOne(src, path.join(next, frameName(i + j + 1)))));
}
await writeFrameKeys(next, fk);
await writeFile(path.join(next, ".key"), key + "\n", "utf8");
diff --git a/umtool/report-to-video/link-or-copy.mjs b/umtool/report-to-video/link-or-copy.mjs
@@ -0,0 +1,60 @@
+// A hard link where the filesystem allows one, else a copy.
+//
+// Identical frames are deduplicated onto one inode (a cut's blank fx frames
+// are ~50,000 links to a single PNG), and every take built `--from` another
+// links them again, so the inode reaches ext4's cap of 65,000 links within a
+// few takes and link() fails EMLINK -- measured 2026-10-30, the Ember cut's
+// draft-17 refused at its first fx frame. A link that cannot be made
+// becomes ONE copy per source inode: the first refusal copies the file, and
+// every later link of that inode in the same linker goes to the copy (a fresh
+// inode, with its own 65,000), so 50,000 refusals cost one file, not 50,000.
+// EXDEV (another filesystem) is handled the same way.
+//
+// Plain node.
+
+import { copyFile, link, stat } from "node:fs/promises";
+
+const FALLBACK = new Set(["EMLINK", "EXDEV"]);
+
+/**
+ * A `link(src, dst)` that copies when the link is refused for its count or
+ * its filesystem. One linker remembers the copies it made, by source inode;
+ * use one per batch of links (a frame directory, a take).
+ * @returns {((src: string, dst: string) => Promise<"link" | "copy">) & { copies: () => number }}
+ */
+export function makeLinker() {
+ /** @type {Map<string, Promise<string>>} source inode -> the copy linked in its place */
+ const stand = new Map();
+ let copies = 0;
+ const fn = async (src, dst) => {
+ try {
+ await link(src, dst);
+ return "link";
+ } catch (err) {
+ if (!FALLBACK.has(/** @type {any} */ (err)?.code)) throw err;
+ }
+ const s = await stat(src);
+ const key = `${s.dev}:${s.ino}`;
+ const prior = stand.get(key);
+ if (prior) {
+ const copy = await prior;
+ try {
+ await link(copy, dst);
+ return "link";
+ } catch (err) {
+ if (!FALLBACK.has(/** @type {any} */ (err)?.code)) throw err;
+ }
+ }
+ // This dst becomes the stand-in: later links of the same inode go to it.
+ const made = copyFile(src, dst).then(() => dst);
+ stand.set(key, made);
+ await made;
+ copies += 1;
+ return "copy";
+ };
+ fn.copies = () => copies;
+ return fn;
+}
+
+/** One link with the fallback, for a caller with no batch to share a linker over. */
+export const linkOrCopy = (src, dst) => makeLinker()(src, dst);
diff --git a/umtool/report-to-video/link-or-copy.test.mjs b/umtool/report-to-video/link-or-copy.test.mjs
@@ -0,0 +1,65 @@
+import { test } from "node:test";
+import assert from "node:assert/strict";
+import { mkdtemp, readFile, rm, stat, writeFile } from "node:fs/promises";
+import fs from "node:fs/promises";
+import os from "node:os";
+import path from "node:path";
+import { syncBuiltinESMExports } from "node:module";
+import { makeLinker } from "./link-or-copy.mjs";
+
+test("links when it can", async () => {
+ const dir = await mkdtemp(path.join(os.tmpdir(), "loc-"));
+ try {
+ await writeFile(path.join(dir, "a"), "frame");
+ const linkOne = makeLinker();
+ assert.equal(await linkOne(path.join(dir, "a"), path.join(dir, "b")), "link");
+ assert.equal((await stat(path.join(dir, "a"))).ino, (await stat(path.join(dir, "b"))).ino);
+ assert.equal(linkOne.copies(), 0);
+ } finally {
+ await rm(dir, { recursive: true, force: true });
+ }
+});
+
+test("a source at the link cap is copied once; later links of it go to the copy", async (t) => {
+ const dir = await mkdtemp(path.join(os.tmpdir(), "loc-"));
+ const capped = path.join(dir, "blank.png");
+ await writeFile(capped, "blank");
+ const realLink = fs.link;
+ // Simulate ext4's cap: any link of the capped inode fails EMLINK.
+ const cappedIno = (await stat(capped)).ino;
+ t.mock.method(fs, "link", async (src, dst) => {
+ if ((await stat(src)).ino === cappedIno) throw Object.assign(new Error("EMLINK: too many links"), { code: "EMLINK" });
+ return realLink(src, dst);
+ });
+ syncBuiltinESMExports(); // the named `link` import follows the mock
+ const fresh = makeLinker;
+ try {
+ const linkOne = fresh();
+ const outs = ["f1", "f2", "f3", "f4"].map((f) => path.join(dir, f));
+ const how = await Promise.all(outs.map((o) => linkOne(capped, o)));
+ assert.equal(how.filter((h) => h === "copy").length, linkOne.copies());
+ assert.ok(linkOne.copies() >= 1);
+ for (const o of outs) assert.equal(await readFile(o, "utf8"), "blank");
+ // sequentially, after the first copy, everything links to the stand-in
+ const seq = fresh();
+ const a = path.join(dir, "s1"), b = path.join(dir, "s2"), c = path.join(dir, "s3");
+ assert.equal(await seq(capped, a), "copy");
+ assert.equal(await seq(capped, b), "link");
+ assert.equal(await seq(capped, c), "link");
+ assert.equal((await stat(b)).ino, (await stat(a)).ino);
+ assert.equal(seq.copies(), 1);
+ } finally {
+ t.mock.restoreAll();
+ syncBuiltinESMExports();
+ await rm(dir, { recursive: true, force: true });
+ }
+});
+
+test("other errors still throw", async () => {
+ const dir = await mkdtemp(path.join(os.tmpdir(), "loc-"));
+ try {
+ await assert.rejects(makeLinker()(path.join(dir, "missing"), path.join(dir, "x")), { code: "ENOENT" });
+ } finally {
+ await rm(dir, { recursive: true, force: true });
+ }
+});
diff --git a/umtool/report-to-video/package.json b/umtool/report-to-video/package.json
@@ -70,6 +70,7 @@
"./transition": "./transition.mjs",
"./variants": "./variants.mjs",
"./verify-build": "./verify-build.mjs",
- "./youtube": "./youtube.mjs"
+ "./youtube": "./youtube.mjs",
+ "./link-or-copy": "./link-or-copy.mjs"
}
}