UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

3 lines • 2.08 kB
import fs from"node:fs";import path from"node:path";import{copyFileHandle,openRootFileSync,writeFileWindowFully}from"@openclaw/fs-safe/advanced";import{sha256FileSync}from"@openclaw/fs-safe/durability"; //!src/infra/file-descriptor.ts 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}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}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)}}function hashFileDescriptorSync(e,t){const{digest:n,bytes:r}=sha256FileSync(e,{maxBytes:t});return{sha256:n,sizeBytes:r}}function flushAndHashFileSync(e){const t=fs.openSync(e,`r+`);try{fs.fsyncSync(t);return hashFileDescriptorSync(t)}finally{fs.closeSync(t)}}export{sameFileMutationFingerprint as a,hashFileMutationSnapshotSync as i,flushAndHashFileSync as n,sameFileMutationMetadata as o,hashFileDescriptorSync as r,writeFileWindowFully as s,copyFileHandle as t};