openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
3 lines • 1.47 kB
JavaScript
import fs from"node:fs";import path from"node:path";
//!src/infra/disk-space.ts
const PACKAGE_UPDATE_STAGING_TENTHS=44;function finiteNonNegativeNumber(e){const t=Number(e);return Number.isFinite(t)&&t>=0?t:null}function findExistingDiskSpacePath(e){let t=path.resolve(e);while(true)try{return fs.statSync(t).isDirectory()?t:path.dirname(t)}catch{const e=path.dirname(t);if(e===t)return null;t=e}}function tryReadDiskSpace(e){const t=findExistingDiskSpacePath(e);if(!t)return null;try{const n=fs.statfsSync(t);const r=finiteNonNegativeNumber(n.bsize);const i=finiteNonNegativeNumber(n.bavail);if(r===null||i===null)return null;const a=finiteNonNegativeNumber(n.blocks);return{targetPath:e,checkedPath:t,availableBytes:r*i,totalBytes:a===null?null:r*a}}catch{return null}}function formatDiskSpaceBytes(e){const t=e/1048576;const n=Math.max(0,Math.round(t));if(n<1024)return`${n} MiB`;const r=t/1024;return`${r.toFixed(r<10?1:0)} GiB`}function describePackageStagingShortfall(e,t){const n=Math.ceil(t*PACKAGE_UPDATE_STAGING_TENTHS/10);const r=tryReadDiskSpace(e);if(!r||r.availableBytes>=n)return null;return`Update refused: ${formatDiskSpaceBytes(r.availableBytes)} free on the volume holding ${r.checkedPath}; this package update needs about ${formatDiskSpaceBytes(n)}. Free ${formatDiskSpaceBytes(n-r.availableBytes)} there, then retry openclaw update. No changes were made.`}export{formatDiskSpaceBytes as n,tryReadDiskSpace as r,describePackageStagingShortfall as t};