df: run async

df hangs on some systems and this brings down the box code

happens on erroneous cifs/sshfs volumes
This commit is contained in:
Girish Ramakrishnan
2024-01-30 12:14:40 +01:00
parent 176baa075f
commit 479946173f
+13 -5
View File
@@ -8,7 +8,9 @@ exports = module.exports = {
const assert = require('assert'),
BoxError = require('./boxerror.js'),
safe = require('safetydance');
debug = require('debug')('box:df'),
safe = require('safetydance'),
shell = require('./shell.js');
// binary units (non SI) 1024 based
function prettyBytes(bytes) {
@@ -35,8 +37,11 @@ function parseLine(line) {
}
async function disks() {
const output = safe.child_process.execSync('df -B1 --output=source,fstype,size,used,avail,pcent,target', { encoding: 'utf8' });
if (!output) throw new BoxError(BoxError.FS_ERROR, `Error running df: ${safe.error.message}`);
const [error, output] = await safe(shell.promises.exec('disks', 'df -B1 --output=source,fstype,size,used,avail,pcent,target'));
if (error) {
debug(`disks: df command failed. error: ${error}\n stdout: ${error.stdout}\n stderr: ${error.stderr}`);
throw new BoxError(BoxError.FS_ERROR, `Error running df: ${error.message}`);
}
const lines = output.trim().split('\n').slice(1); // discard header
const result = [];
@@ -49,8 +54,11 @@ async function disks() {
async function file(filename) {
assert.strictEqual(typeof filename, 'string');
const output = safe.child_process.execSync(`df -B1 --output=source,fstype,size,used,avail,pcent,target ${filename}`, { encoding: 'utf8' });
if (!output) throw new BoxError(BoxError.FS_ERROR, `Error running df: ${safe.error.message}`);
const [error, output] = await safe(shell.promises.exec('file', `df -B1 --output=source,fstype,size,used,avail,pcent,target ${filename}`));
if (error) {
debug(`file: df command failed. error: ${error}\n stdout: ${error.stdout}\n stderr: ${error.stderr}`);
throw new BoxError(BoxError.FS_ERROR, `Error running df: ${error.message}`);
}
const lines = output.trim().split('\n').slice(1); // discard header
return parseLine(lines[0]);