const fs = require('fs'); const os = require('os'); const path = require('path'); const FREE_SPACE_RESERVE_BYTES = 16 * 1024 * 1024; const UNLIMITED_BACKUP_TIMEOUT_MS = 24 * 60 * 60 * 1000; function parseBackupLimitMb(raw) { if (raw === undefined || raw === null || String(raw).trim() === '') return 0; const n = parseInt(raw, 10); if (!Number.isFinite(n) || n < 0) return 0; return n; } function formatBytes(bytes) { const n = Number(bytes) || 0; if (n >= 1024 * 1024 * 1024) return `${(n / (1024 * 1024 * 1024)).toFixed(1)} ГБ`; if (n >= 1024 * 1024) return `${Math.round(n / (1024 * 1024))} МБ`; return `${Math.round(n / 1024)} КБ`; } function freeBytes(dir) { try { const st = fs.statfsSync(dir); return Number(st.bavail) * Number(st.bsize); } catch { return null; } } function assertRestoreSpace(dir, archiveBytes) { const free = freeBytes(dir); if (free == null) return; const need = Math.max(Number(archiveBytes) * 2, Number(archiveBytes) + FREE_SPACE_RESERVE_BYTES); if (free >= need) return; const err = new Error( `Недостаточно свободного места для распаковки архива: нужно примерно ${formatBytes(need)}, свободно ${formatBytes(free)}` ); err.code = 'RESTORE_NO_SPACE'; throw err; } function assertRestoreUploadSpace(dir, archiveBytes) { const free = freeBytes(dir); if (free == null) return; const need = Math.max(Number(archiveBytes) * 3, Number(archiveBytes) + FREE_SPACE_RESERVE_BYTES); if (free >= need) return; const err = new Error( `Недостаточно свободного места для загрузки и распаковки архива: нужно примерно ${formatBytes(need)}, свободно ${formatBytes(free)}` ); err.code = 'RESTORE_NO_SPACE'; throw err; } function backupRequestTimeoutMs({ limitMb, uploadTimeoutMs, explicitMs }) { const explicit = Number(explicitMs) || 0; if (explicit > 0) return Math.max(300000, explicit); const base = Math.max(300000, Number(uploadTimeoutMs) || 0); if (Number(limitMb) > 0) return Math.max(base, Number(limitMb) * 7500); return Math.max(base, UNLIMITED_BACKUP_TIMEOUT_MS); } function isDiskFullError(e) { const code = String((e && (e.code || e.errno)) || ''); if (code === 'ENOSPC' || code === 'EDQUOT') return true; return /no space left|disk quota|not enough space|enospc/i.test(String((e && e.message) || '')); } function isEntryInside(root, entryPath) { if (!entryPath || path.isAbsolute(entryPath)) return false; const target = path.resolve(root, entryPath); const rel = path.relative(root, target); return rel === '' || (!rel.startsWith('..') && !path.isAbsolute(rel)); } function tarEntryFilter(root) { return (entryPath, entry) => { if (!isEntryInside(root, entryPath)) return false; if (entry && (entry.type === 'SymbolicLink' || entry.type === 'Link')) return false; return true; }; } function removeTempDir(dir) { try { if (dir) fs.rmSync(dir, { recursive: true, force: true }); } catch {} } function sweepRestoreTempDirs() { let names = []; try { names = fs.readdirSync(os.tmpdir()); } catch { return 0; } let removed = 0; for (const name of names) { if (!/^wido-(?:up|rst)-/.test(name)) continue; const dir = path.join(os.tmpdir(), name); try { if (!fs.statSync(dir).isDirectory()) continue; fs.rmSync(dir, { recursive: true, force: true }); removed++; } catch {} } return removed; } module.exports = { formatBytes, freeBytes, parseBackupLimitMb, assertRestoreSpace, assertRestoreUploadSpace, backupRequestTimeoutMs, isDiskFullError, isEntryInside, tarEntryFilter, removeTempDir, sweepRestoreTempDirs, };