openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
3 lines • 4.09 kB
JavaScript
import{o as openRootFileSync$1,r as isRootFileMissingFailure}from"./boundary-file-read-bB9An35K.mjs";import{t as _usingCtx}from"./usingCtx-CUVyQ_FT.mjs";import{t as ConfigMutationConflictError}from"./mutation-conflict-qTMp0e4U.mjs";import{r as createConfigWriteAuthorityGuard}from"./write-authority-YzEjC_aZ.mjs";import path from"node:path";import{randomUUID}from"node:crypto";import{tempFile}from"@openclaw/fs-safe/advanced";import{replaceFileAtomicSync}from"@openclaw/fs-safe/atomic";
//!src/config/backup-rotation.ts
async function prepareConfigFileWrite(e){const{configPath:t,fsModule:n}=e;const r=createConfigWriteAuthorityGuard(e.assertCurrent);const i=createConfigWriteAuthorityGuard(e.assertBeforeMutation??r);let a=false;const warnBackupRotationFailure=()=>{if(a)return;a=true;try{e.warn?.("Config backup rotation failed; the config write is continuing without a guaranteed current backup. Before making further config changes, create and verify a backup with `openclaw backup create --verify`.")}catch{}};r?.();i();let o;try{if(e.previousRaw!==null)try{var s=_usingCtx();o=await tempFile({rootDir:path.dirname(t),prefix:`openclaw-config-backup`,fileName:`original`});r?.();const i=s.a(await n.promises.open(o.path,`wx`,384));r?.();await i.writeFile(e.previousRaw,`utf8`);r?.();if(e.durable){await i.sync();r?.()}}catch(e){s.e=e}finally{await s.d()}}catch(e){try{await o?.[Symbol.asyncDispose]()}catch(t){throw new AggregateError([e,t],`Config backup preparation and cleanup failed`,{cause:t})}o=void 0;r?.();warnBackupRotationFailure()}return{publish(){return replaceFileAtomicSync({filePath:t,content:e.content,dirMode:e.dirMode??448,mode:384,copyFallbackOnPermissionError:true,destinationHardlinks:e.destinationHardlinks,syncTempFile:e.durable,syncParentDir:e.durable,fileSystem:n,throwOnCleanupError:true,assertBeforeMutation:i,onDestinationState:e.onDestinationState,beforeRename:()=>{if(!o)return;const openBackupArtifact=e=>{const r=openRootFileSync$1({absolutePath:e,rootPath:path.dirname(t),boundaryLabel:`config backup directory`,ioFs:n});return{...r,[Symbol.dispose](){if(r.ok)n.closeSync(r.fd)}}};const captureBackupIdentity=(e,t,r)=>{const i=t===void 0?void 0:n.fstatSync(t,{bigint:true});return()=>{const a=n.lstatSync(e,{bigint:true,throwIfNoEntry:r===`source`});const o=t===void 0?void 0:n.fstatSync(t,{bigint:true});if(i?[a,o].some(e=>!e||!e.isFile()||e.nlink!==1n||e.dev!==i.dev||e.ino!==i.ino):a)throw new ConfigMutationConflictError(`config backup ${r} changed`,{retryable:false})}};const mutateBackupArtifact=(e,t)=>{i();try{try{var r=_usingCtx();const a=r.u(t?openBackupArtifact(t):void 0);if(a&&!a.ok&&!isRootFileMissingFailure(a)){warnBackupRotationFailure();return}const o=r.u(openBackupArtifact(e));if(!o.ok){if(!isRootFileMissingFailure(o))warnBackupRotationFailure();return}const s=captureBackupIdentity(o.path,o.fd,`source`);const c=t?captureBackupIdentity(t,a?.ok?a.fd:void 0,`destination`):void 0;const assertBackupCurrent=()=>{i();s();c?.()};assertBackupCurrent();if(t){n.fchmodSync(o.fd,384);assertBackupCurrent();n.renameSync(o.path,t)}else n.unlinkSync(o.path)}catch(e){r.e=e}finally{r.d()}}catch(e){i();if(e instanceof ConfigMutationConflictError)throw e;warnBackupRotationFailure()}};const e=`${t}.bak`;mutateBackupArtifact(`${e}.4`);for(let t=3;t>=0;t--)mutateBackupArtifact(t===0?e:`${e}.${t}`,`${e}.${t+1}`);mutateBackupArtifact(o.path,e)}})},async[Symbol.asyncDispose](){await o?.[Symbol.asyncDispose]()}}}const preUpdateConfigSnapshotsWritten=/* @__PURE__ */ new Set;async function createPreUpdateConfigSnapshot(e){if(!e.fs.existsSync(e.configPath))return;const t=path.resolve(e.configPath);if(preUpdateConfigSnapshotsWritten.has(t))return;preUpdateConfigSnapshotsWritten.add(t);const n=`${e.configPath}.pre-update`;const r=`${n}.${randomUUID()}.tmp`;try{const t=await e.fs.readFile(e.configPath,`utf-8`);await e.fs.writeFile(r,t,{encoding:`utf-8`,mode:384,flag:`wx`});await e.fs.rename(r,n)}catch{try{await e.fs.rm(r)}catch{}preUpdateConfigSnapshotsWritten.delete(t)}}export{prepareConfigFileWrite as n,createPreUpdateConfigSnapshot as t};