openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
134 lines (133 loc) • 5.18 kB
JavaScript
import { i as timeWorktreePreparationPhase } from "./preparation-timing-YO2wGpby.mjs";
import { t as nativeWorktreeFilesystem } from "./filesystem-native-DXQMqCd5.mjs";
import path from "node:path";
import { createHash } from "node:crypto";
import fs from "node:fs/promises";
import { isUtf8 } from "node:buffer";
import { setImmediate, setTimeout } from "node:timers/promises";
//#region src/agents/worktrees/checkout-apfs.ts
function parseIndex(e) {
const t = ["sha1", "sha256"].find((t) => {
const n = t === "sha1" ? 20 : 32;
return e.length >= 12 + n && createHash(t).update(e.subarray(0, -n)).digest().equals(e.subarray(-n));
});
if (!t || e.toString("ascii", 0, 4) !== "DIRC" || ![2, 3].includes(e.readUInt32BE(4))) return;
const n = t === "sha1" ? 20 : 32;
const r = e.length - n;
const i = [];
let a = 12;
let o;
for (let t = 0; t < e.readUInt32BE(8); t++) {
const t = a;
const s = t + 42 + n;
if (s >= r || (e.readUInt16BE(s - 2) & 61440) !== 0) return;
const c = e.indexOf(0, s);
if (c < s || c >= r) return;
const l = e.subarray(s, c);
const u = l.toString("utf8");
if ((e.readUInt16BE(s - 2) & 4095) !== Math.min(l.length, 4095) || !isUtf8(l) || o && Buffer.compare(o, l) >= 0 || u.split("/").some((e) => !e || e === "." || e === ".." || e.toLowerCase() === ".git")) return;
o = l;
a = t + Math.ceil((c + 1 - t) / 8) * 8;
if (a > r || !e.subarray(c, a).every((e) => e === 0)) return;
i.push({
offset: t,
name: u
});
}
const s = a;
const c = [];
while (a < r) {
if (a + 8 > r || e[a] < 65 || e[a] > 90) return;
const t = a + 8 + e.readUInt32BE(a + 4);
if (t > r) return;
if (e.toString("ascii", a, a + 4) === "TREE") c.push(e.subarray(a, t));
a = t;
}
return {
algorithm: t,
hashSize: n,
entries: i,
entriesEnd: s,
extensions: c
};
}
function low32(e) {
return Number(e & 4294967295n);
}
function matchesSource(e, t, n) {
const r = low32(n.size) || (n.size ? 2147483648 : 0);
return n.type === 1 && e.readUInt32BE(t) === n.ctimeSec >>> 0 && e.readUInt32BE(t + 4) === n.ctimeNs && e.readUInt32BE(t + 8) === n.mtimeSec >>> 0 && e.readUInt32BE(t + 12) === n.mtimeNs && e.readUInt32BE(t + 16) === n.dev && e.readUInt32BE(t + 20) === low32(n.ino) && (e.readUInt32BE(t + 24) & 64) === (n.mode & 64) && e.readUInt32BE(t + 28) === n.uid && e.readUInt32BE(t + 32) === n.gid && e.readUInt32BE(t + 36) === r;
}
/** Translate only clone-proven stat identities; Git still validates the result. */
async function copyApfsCloneIndex(e, t, n, r, i) {
const a = await fs.open(n, "r");
const { data: o, stamp: s } = await (async () => {
try {
const e = await a.stat({ bigint: true });
const t = await a.readFile();
const n = await a.stat({ bigint: true });
return {
data: e.mtimeNs === n.mtimeNs && e.ctimeNs === n.ctimeNs && e.size === n.size ? t : void 0,
stamp: e
};
} finally {
await a.close();
}
})();
const c = o && parseIndex(o);
if (!o || !c) return false;
const l = (Math.floor(i.cloneCompletedAtMs / 1e3) + 1) * 1e3;
const u = Math.min(1e3, l - Date.now());
if (u > 0) await timeWorktreePreparationPhase("indexCloneWait", () => setTimeout(u, void 0, { signal: i.signal }));
const d = c.entriesEnd + c.extensions.reduce((e, t) => e + t.length, 0);
const f = Buffer.allocUnsafe(d + c.hashSize);
o.copy(f, 0, 0, c.entriesEnd);
let p = c.entriesEnd;
for (const e of c.extensions) {
e.copy(f, p);
p += e.length;
}
const m = path.join(e, ".") + path.sep;
const h = path.join(t, ".") + path.sep;
const g = Number(s.mtimeNs / 1000000000n);
for (let e = 0; e < c.entries.length; e += 4096) {
const t = [];
const n = Math.min(e + 4096, c.entries.length);
for (let r = e; r < n; r++) {
const e = c.entries[r];
if ((o.readUInt32BE(e.offset + 24) & 61440) === 32768 && o.readUInt32BE(e.offset + 8) < g) t.push(e);
}
i.signal?.throwIfAborted();
i.commitGuard();
if (t.length === 0) {
await setImmediate();
continue;
}
const r = Math.floor(Date.now() / 1e3);
const a = [];
for (const e of t) a.push(m + e.name, h + e.name);
const s = await timeWorktreePreparationPhase("indexMetadata", () => nativeWorktreeFilesystem.readMetadata(a, i));
i.signal?.throwIfAborted();
i.commitGuard();
for (let e = 0; e < t.length; e++) {
const n = t[e].offset;
const i = s[e * 2];
const a = s[e * 2 + 1];
if (!i || !a || !i.exactIdentity || !a.exactIdentity || !matchesSource(o, n, i) || a.type !== 1 || a.ctimeSec >= r || !i.cloneId || i.cloneId !== a.cloneId || i.exactIdentity.dev !== a.exactIdentity.dev || i.exactIdentity.ino === a.exactIdentity.ino || i.size !== a.size || i.mtimeSec !== a.mtimeSec || i.mtimeNs !== a.mtimeNs || (i.mode & 64) !== (a.mode & 64)) continue;
f.writeUInt32BE(a.ctimeSec >>> 0, n);
f.writeUInt32BE(a.ctimeNs, n + 4);
f.writeUInt32BE(a.dev, n + 16);
f.writeUInt32BE(low32(a.ino), n + 20);
f.writeUInt32BE(a.uid, n + 28);
f.writeUInt32BE(a.gid, n + 32);
}
}
const _ = f.subarray(0, d);
i.signal?.throwIfAborted();
i.commitGuard();
createHash(c.algorithm).update(_).digest().copy(f, d);
await fs.writeFile(r, f);
return true;
}
//#endregion
export { copyApfsCloneIndex };