const { statfs } = require('node:fs/promises');

async function diskSpace(directory) {
  const stats = await statfs(directory);
  const total = stats.blocks * stats.bsize;
  const free = Math.max(0, Math.min(total, stats.bfree * stats.bsize));
  const available = Math.max(0, Math.min(free, stats.bavail * stats.bsize));
  if (![total, free, available].every(Number.isSafeInteger) || total <= 0)
    throw new Error('Informations du disque indisponibles.');
  return { total, used: total - free, available, reserved: free - available };
}
module.exports = { diskSpace };
