openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
85 lines (84 loc) • 3.97 kB
JavaScript
import { n as ignoreMissingPath } from "./errno-DFcoqIT9.mjs";
import { o as sameFileMutationFingerprint } from "./file-descriptor-DQ-v5bLt.mjs";
import fs from "node:fs/promises";
import { sameFileIdentity } from "@openclaw/fs-safe/advanced";
//#region src/infra/backup-sqlite-source-groups.ts
function hasKnownIdentity(e) {
return process.platform !== "win32" || e.dev !== 0n && e.ino !== 0n;
}
async function readRegularSidecar(e) {
const t = await fs.lstat(e, { bigint: true }).catch(ignoreMissingPath);
if (!t) return null;
if (!t.isFile()) throw new Error(`SQLite hardlink sidecar must be a regular file: ${e}`);
return t;
}
async function assertGroupPaths(e) {
for (const t of e.sources) {
const n = await fs.lstat(t.path, { bigint: true });
if (!n.isFile() || !sameFileIdentity(t.identity, n) || e.sources.length > 1 && !hasKnownIdentity(n) || await fs.realpath(t.path) !== t.path) throw new Error(`SQLite backup source identity changed: ${t.path}`);
if (n.nlink !== BigInt(e.sources.length)) throw new Error(`SQLite hardlink journal owner may be outside the backup inventory: ${t.path} has ${n.nlink} links, but ${e.sources.length} paths were admitted. Include every database hardlink before retrying.`);
}
}
async function readGroupWalOwners(e) {
const t = [];
for (const n of e.sources) {
const e = await readRegularSidecar(`${n.path}-wal`);
const r = await readRegularSidecar(`${n.path}-journal`);
await readRegularSidecar(`${n.path}-shm`);
if (r && r.size > 0n) throw new Error(`SQLite hardlink journal ownership cannot be established safely while a rollback journal is present: ${n.path}. Close the database cleanly before retrying.`);
if (e && e.size > 0n) {
if (!hasKnownIdentity(e)) throw new Error(`SQLite hardlink WAL identity cannot be verified: ${n.path}-wal`);
t.push({
path: n.path,
identity: e
});
}
}
if (t.length > 1) throw new Error(`Ambiguous SQLite hardlink journal ownership: multiple non-empty WAL files for ${e.sources.map((e) => e.path).join(", ")}. Close the database writers before retrying.`);
return t;
}
async function assertBackupSqliteSourceGroup(e) {
await assertGroupPaths(e);
if (e.sources.length === 1) return;
const [t] = await readGroupWalOwners(e);
if (e.walIdentity ? !t || t.path !== e.sourcePath || !sameFileIdentity(e.walIdentity, t.identity) : t !== void 0) throw new Error(`SQLite hardlink journal ownership changed during backup: ${e.sourcePath}`);
if (!sameFileMutationFingerprint(e.databaseFingerprint, await fs.stat(e.sourcePath, { bigint: true }))) throw new Error(`SQLite hardlink database changed during backup: ${e.sourcePath}. Close database writers cleanly before retrying.`);
}
/** Resolve journal ownership after canonical role validation and physical path resolution. */
async function planBackupSqliteSourceGroups(e) {
const t = /* @__PURE__ */ new Map();
for (const n of e) {
const e = hasKnownIdentity(n.identity);
if (!e && n.identity.nlink > 1n) throw new Error(`SQLite hardlink identity cannot be verified: ${n.path}`);
const r = e ? `${n.identity.dev}:${n.identity.ino}` : n.path;
const i = t.get(r);
if (i) {
if (!i.sources.some((e) => e.path === n.path)) i.sources.push(n);
} else t.set(r, {
sources: [n],
sourcePath: n.path,
walIdentity: null,
databaseFingerprint: await fs.stat(n.path, { bigint: true })
});
}
const n = /* @__PURE__ */ new Map();
for (const e of t.values()) {
await assertGroupPaths(e);
if (e.sources.length > 1) {
const [t] = await readGroupWalOwners(e);
if (t) {
e.sourcePath = t.path;
e.walIdentity = t.identity;
}
}
for (const t of e.sources) n.set(t.path, e);
}
return n;
}
async function captureBackupSqliteSourceGroup(e, t) {
await assertBackupSqliteSourceGroup(e);
await t();
await assertBackupSqliteSourceGroup(e);
}
//#endregion
export { captureBackupSqliteSourceGroup as n, planBackupSqliteSourceGroups as r, assertBackupSqliteSourceGroup as t };