UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

3 lines • 4.13 kB
import{i as isMissingPathError}from"./errno-DUBt6SQQ.mjs";import{t as retainMutationAuthority}from"./mutation-authority-EJgpxwvk.mjs";import fs from"node:fs";import path from"node:path";import fs$1 from"node:fs/promises";import{FsSafeError}from"@openclaw/fs-safe/errors";import{root}from"@openclaw/fs-safe/root"; //!src/infra/fs-safe-remove.ts function isNotFoundError(e){return isMissingPathError(e)||isMissingPathError(findMappedFilesystemCause(e))}function findMappedFilesystemCause(e){const t=e instanceof FsSafeError&&e.details?.operation===`remove`&&(e.details.phase===`enumerate`||e.details.phase===`inspect`);const n=e?.code;if(n!==`path-alias`&&n!==`permission-denied`&&!t)return;const r=e.cause;const i=r?.code;return typeof i===`string`&&/^E[A-Z0-9_]+$/u.test(i)?r:void 0}function filesystemCode(e){const t=e;if(t?.code&&/^E[A-Z0-9_]+$/u.test(t.code))return t.code;if(t?.code===`not-removable`||t?.code===`not-empty`||t?.code===`permission-denied`||t?.code===`path-alias`){const e=t.cause?.code;return e&&/^E[A-Z0-9_]+$/u.test(e)?e:void 0}}function isRemovalIoError(e){return filesystemCode(e)!==void 0}function pinPath(e){return{path:e,stat:fs.lstatSync(e,{bigint:true})}}function assertPinnedPath(e,t=false){let n;try{n=fs.lstatSync(e.path,{bigint:true,throwIfNoEntry:false})}catch(t){throw new FsSafeError(`path-mismatch`,`removal path could not be verified: ${e.path}`,{cause:t instanceof Error?t:void 0})}if(!n&&t)return false;if(!n||n.dev!==e.stat.dev||n.ino!==e.stat.ino||n.isDirectory()!==e.stat.isDirectory()||n.isSymbolicLink()!==e.stat.isSymbolicLink()||process.platform===`win32`&&(n.dev===0n||n.ino===0n||e.stat.dev===0n||e.stat.ino===0n))throw new FsSafeError(`path-mismatch`,`removal path changed: ${e.path}`);return true}function capturePathRemovalGuard(e){const t=fs.lstatSync(e,{bigint:true,throwIfNoEntry:false});if(!t)return;const n={path:e,stat:t};return()=>{assertPinnedPath(n,true)}}function assertPinnedDirectory(e){if(!e.stat.isDirectory()||e.stat.isSymbolicLink())throw new FsSafeError(`symlink`,`removal parent is not a directory: ${e.path}`);assertPinnedPath(e)}async function removePathWithinRoot(e){const t=retainMutationAuthority(e.assertBeforeMutation??(()=>{}));const n=retainMutationAuthority(e=>e?.());t();const r=await root(e.rootDir);const i=e.force!==false;const run=async(e,t)=>{t();try{const n=await e();t();return n}catch(e){t();throw e}};const removeEntry=async(a,o)=>{const assertParents=()=>n(()=>o.forEach(assertPinnedDirectory));const inspectEntry=()=>{let e=false;n(()=>{o.forEach(assertPinnedDirectory);e=assertPinnedPath(a,true)});return e};const assertEntry=()=>{if(!inspectEntry())throw new FsSafeError(`not-found`,`file not found`)};try{assertEntry();const s=path.relative(r.rootReal,a.path);const c=`.${path.sep}${s}`;if(a.stat.isSymbolicLink()&&e.symlinks!==`unlink`)throw new FsSafeError(`symlink`,`symlink not allowed: ${s}`);if(e.recursive&&a.stat.isDirectory()){const e=(await run(()=>r.list(c),assertEntry)).toSorted();const t=[];for(const n of e)try{t.push(pinPath(path.join(a.path,n)))}catch(e){assertEntry();if(!i||!isNotFoundError(e))throw e}for(const e of t)await removeEntry(e,[...o,a]);assertEntry()}await run(()=>r.remove(c,{assertBeforeMutation:()=>{n(t);assertEntry()},signal:e.signal}),assertParents)}catch(e){inspectEntry();if(!i||!isNotFoundError(e))throw e}};try{const i=path.basename(e.relativePath);if(i===``||i===`.`||i===`..`)throw new FsSafeError(`invalid-path`,`removal requires a path below the root`);const a=await run(()=>r.resolve(path.dirname(e.relativePath)),n);const o=await run(async()=>r.resolve(await fs$1.realpath(a)),n);const s=[pinPath(r.rootReal)];let c=r.rootReal;for(const e of path.relative(r.rootReal,o).split(path.sep).filter(Boolean)){c=path.join(c,e);s.push(pinPath(c))}n(()=>s.forEach(assertPinnedDirectory));await removeEntry(pinPath(path.join(o,i)),s);n(t)}catch(e){n(t);if(isNotFoundError(e)){if(i)return;throw new FsSafeError(`not-found`,`file not found`,{cause:e instanceof Error?e:void 0})}throw findMappedFilesystemCause(e)??e}}export{isRemovalIoError as n,removePathWithinRoot as r,capturePathRemovalGuard as t};