Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Linux/Darwin don't crash on spawn EBADF errors #913

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 32 additions & 8 deletions lib/filesystem.js
Original file line number Diff line number Diff line change
Expand Up @@ -569,13 +569,13 @@ function blkStdoutToObject(stdout) {

function blockDevices(callback) {

return new Promise((resolve) => {
return new Promise((resolve, reject) => {
process.nextTick(() => {
let data = [];
if (_linux) {
// see https://wiki.ubuntuusers.de/lsblk/
// exec("lsblk -bo NAME,TYPE,SIZE,FSTYPE,MOUNTPOINT,UUID,ROTA,RO,TRAN,SERIAL,LABEL,MODEL,OWNER,GROUP,MODE,ALIGNMENT,MIN-IO,OPT-IO,PHY-SEC,LOG-SEC,SCHED,RQ-SIZE,RA,WSAME", function (error, stdout) {
exec('lsblk -bPo NAME,TYPE,SIZE,FSTYPE,MOUNTPOINT,UUID,ROTA,RO,RM,TRAN,SERIAL,LABEL,MODEL,OWNER 2>/dev/null', { maxBuffer: 1024 * 1024 }, function (error, stdout) {
const process = exec('lsblk -bPo NAME,TYPE,SIZE,FSTYPE,MOUNTPOINT,UUID,ROTA,RO,RM,TRAN,SERIAL,LABEL,MODEL,OWNER 2>/dev/null', { maxBuffer: 1024 * 1024 }, function (error, stdout) {
if (!error) {
let lines = blkStdoutToObject(stdout).split('\n');
data = parseBlk(lines);
Expand All @@ -586,7 +586,7 @@ function blockDevices(callback) {
}
resolve(data);
} else {
exec('lsblk -bPo NAME,TYPE,SIZE,FSTYPE,MOUNTPOINT,UUID,ROTA,RO,RM,LABEL,MODEL,OWNER 2>/dev/null', { maxBuffer: 1024 * 1024 }, function (error, stdout) {
const process2 = exec('lsblk -bPo NAME,TYPE,SIZE,FSTYPE,MOUNTPOINT,UUID,ROTA,RO,RM,LABEL,MODEL,OWNER 2>/dev/null', { maxBuffer: 1024 * 1024 }, function (error, stdout) {
if (!error) {
let lines = blkStdoutToObject(stdout).split('\n');
data = parseBlk(lines);
Expand All @@ -597,11 +597,19 @@ function blockDevices(callback) {
}
resolve(data);
});

process2.on('error', function (err) {
reject(err);
});
}
});

process.on('error', function (err) {
reject(err);
});
}
if (_darwin) {
exec('diskutil info -all', { maxBuffer: 1024 * 1024 }, function (error, stdout) {
const process = exec('diskutil info -all', { maxBuffer: 1024 * 1024 }, function (error, stdout) {
if (!error) {
let lines = stdout.toString().split('\n');
// parse lines into temp array of devices
Expand All @@ -613,6 +621,10 @@ function blockDevices(callback) {
}
resolve(data);
});

process.on('error', function (err) {
reject(err);
});
}
if (_sunos) {
if (callback) { callback(data); }
Expand Down Expand Up @@ -726,7 +738,7 @@ function calcFsSpeed(rx, wx) {

function fsStats(callback) {

return new Promise((resolve) => {
return new Promise((resolve, reject) => {
process.nextTick(() => {
if (_windows || _freebsd || _openbsd || _netbsd || _sunos) {
return resolve(null);
Expand All @@ -747,7 +759,7 @@ function fsStats(callback) {
if ((_fs_speed && !_fs_speed.ms) || (_fs_speed && _fs_speed.ms && Date.now() - _fs_speed.ms >= 500)) {
if (_linux) {
// exec("df -k | grep /dev/", function(error, stdout) {
exec('lsblk -r 2>/dev/null | grep /', { maxBuffer: 1024 * 1024 }, function (error, stdout) {
const process = exec('lsblk -r 2>/dev/null | grep /', { maxBuffer: 1024 * 1024 }, function (error, stdout) {
if (!error) {
let lines = stdout.toString().split('\n');
let fs_filter = [];
Expand All @@ -759,7 +771,7 @@ function fsStats(callback) {
});

let output = fs_filter.join('|');
exec('cat /proc/diskstats | egrep "' + output + '"', { maxBuffer: 1024 * 1024 }, function (error, stdout) {
const process2 = exec('cat /proc/diskstats | egrep "' + output + '"', { maxBuffer: 1024 * 1024 }, function (error, stdout) {
if (!error) {
let lines = stdout.toString().split('\n');
lines.forEach(function (line) {
Expand All @@ -778,16 +790,24 @@ function fsStats(callback) {
}
resolve(result);
});

process2.on('error', function (err) {
reject(err);
});
} else {
if (callback) {
callback(result);
}
resolve(result);
}
});

process.on('error', function (err) {
reject(err);
});
}
if (_darwin) {
exec('ioreg -c IOBlockStorageDriver -k Statistics -r -w0 | sed -n "/IOBlockStorageDriver/,/Statistics/p" | grep "Statistics" | tr -cd "01234567890,\n"', { maxBuffer: 1024 * 1024 }, function (error, stdout) {
const process = exec('ioreg -c IOBlockStorageDriver -k Statistics -r -w0 | sed -n "/IOBlockStorageDriver/,/Statistics/p" | grep "Statistics" | tr -cd "01234567890,\n"', { maxBuffer: 1024 * 1024 }, function (error, stdout) {
if (!error) {
let lines = stdout.toString().split('\n');
lines.forEach(function (line) {
Expand All @@ -806,6 +826,10 @@ function fsStats(callback) {
}
resolve(result);
});

process.on('error', function (err) {
reject(err);
});
}
} else {
result.ms = _fs_speed.last_ms;
Expand Down