openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
70 lines (69 loc) • 2.96 kB
JavaScript
import fs from "node:fs";
import path from "node:path";
import { copyFileHandle, openRootFileSync, overwriteFileHandle as overwriteFileHandle$1, writeFileWindowFully } from "@openclaw/fs-safe/advanced";
import { sha256FileSync } from "@openclaw/fs-safe/durability";
//#region src/infra/file-descriptor.ts
/** Strict field equality; callers own any platform-specific identity tolerance. */
function sameFileMutationFingerprint(e, t) {
return e.birthtimeNs === t.birthtimeNs && e.ctimeNs === t.ctimeNs && e.dev === t.dev && e.ino === t.ino && e.mtimeNs === t.mtimeNs && e.size === t.size;
}
/** Link churn changes ctime on Windows and POSIX; admitting it still requires matching bytes. */
function sameFileMutationMetadata(e, t) {
const n = (process.platform === "linux" || process.platform === "android") && e.birthtimeNs === e.ctimeNs && t.birthtimeNs === t.ctimeNs;
return sameFileMutationFingerprint(e, {
...t,
ctimeNs: e.ctimeNs,
birthtimeNs: n ? e.birthtimeNs : t.birthtimeNs
}) && e.mode === t.mode && e.uid === t.uid && e.gid === t.gid;
}
/** Hash a pinned regular file without accepting replacement or non-link metadata changes. */
function hashFileMutationSnapshotSync(e, t) {
const changed = () => /* @__PURE__ */ new Error(`File changed while hashing snapshot: ${e}`);
const n = openRootFileSync({
absolutePath: e,
rootPath: path.dirname(e),
boundaryLabel: "file snapshot",
maxBytes: Number(t.size),
rejectHardlinks: false
});
if (!n.ok) throw new Error(`Cannot open file snapshot: ${e}`, { cause: n.error });
const assertStat = (e) => {
if (!e.isFile() || !sameFileMutationMetadata(t, e)) throw changed();
return e;
};
try {
let r;
for (let i = 0; i < 3; i += 1) {
const i = assertStat(fs.fstatSync(n.fd, { bigint: true }));
const a = hashFileDescriptorSync(n.fd, Number(t.size));
const o = assertStat(fs.fstatSync(n.fd, { bigint: true }));
const s = assertStat(fs.lstatSync(e, { bigint: true }));
if (a.sizeBytes !== Number(t.size) || r !== void 0 && a.sha256 !== r) throw changed();
r = a.sha256;
if (sameFileMutationFingerprint(i, o) && sameFileMutationFingerprint(o, s)) return r;
}
throw changed();
} finally {
fs.closeSync(n.fd);
}
}
/** Maps the borrowed-descriptor digest to OpenClaw's persisted artifact fields. */
function hashFileDescriptorSync(e, t) {
const { digest: n, bytes: r } = sha256FileSync(e, { maxBytes: t });
return {
sha256: n,
sizeBytes: r
};
}
/** Flush an owned writable file before hashing it and always close the descriptor. */
function flushAndHashFileSync(e) {
const t = fs.openSync(e, "r+");
try {
fs.fsyncSync(t);
return hashFileDescriptorSync(t);
} finally {
fs.closeSync(t);
}
}
//#endregion
export { overwriteFileHandle$1 as a, writeFileWindowFully as c, hashFileMutationSnapshotSync as i, flushAndHashFileSync as n, sameFileMutationFingerprint as o, hashFileDescriptorSync as r, sameFileMutationMetadata as s, copyFileHandle as t };