fsSize() optimized parsing if there are errors

This commit is contained in:
Sebastian Hildebrandt 2022-07-07 10:01:49 +02:00
parent e7475d5ef2
commit f7022c92f0
3 changed files with 55 additions and 33 deletions

View File

@ -54,6 +54,22 @@ function fsSize(callback) {
return (linuxTmpFileSystems.includes(fs.toLowerCase()));
}
function filterLines(stdout) {
let lines = stdout.toString().split('\n');
if (stdout.toString().toLowerCase().indexOf('filesystem')) {
let removeLines = 0;
for (let i = 0; i < lines.length; i++) {
if (line[i] && lines[i].toLowerCase().startsWith('filesystem')) {
removeLines = i;
}
}
for (let i = 0; i < removeLines; i++) {
lines.shift();
}
}
return lines;
}
function parseDf(lines) {
let data = [];
lines.forEach(function (line) {
@ -104,10 +120,9 @@ function fsSize(callback) {
if (_linux) { cmd = 'df -lkPTx squashfs'; } // cmd = 'df -lkPTx squashfs | grep -E "^/|^.\\:"';
if (_freebsd || _openbsd || _netbsd) { cmd = 'df -lkPT'; }
exec(cmd, { maxBuffer: 1024 * 1024 }, function (error, stdout) {
if (!error) {
let lines = stdout.toString().split('\n');
if (lines && lines[0] && lines[0].toLowerCase().startsWith('filesystem')) { lines.shift(); }
data = parseDf(lines);
let lines = filterLines(stdout);
data = parseDf(lines);
if (!error || data.length) {
if (callback) {
callback(data);
}
@ -115,8 +130,7 @@ function fsSize(callback) {
} else {
exec('df -kPT', { maxBuffer: 1024 * 1024 }, function (error, stdout) {
if (!error) {
let lines = stdout.toString().split('\n');
if (lines && lines[0] && lines[0].toLowerCase().startsWith('filesystem')) { lines.shift(); }
let lines = filterLines(stdout);
data = parseDf(lines);
}
if (callback) {

3
lib/index.d.ts vendored
View File

@ -988,3 +988,6 @@ export function getDynamicData(srv?: string, iface?: string, cb?: (data: any) =>
export function getAllData(srv?: string, iface?: string, cb?: (data: any) => any): Promise<any>;
export function get(valuesObject: any, cb?: (data: any) => any): Promise<any>;
export function observe(valuesObject: any, interval: number, cb?: (data: any) => any): number;
export function powerShellStart(): void;
export function powerShellRelease(): void;

View File

@ -396,42 +396,47 @@ function powerShellProceedResults(data) {
}
function powerShellStart() {
_psChild = spawn('powershell.exe', ['-NoLogo', '-InputFormat', 'Text', '-NoExit', '-Command', '-'], {
stdio: 'pipe',
windowsHide: true,
maxBuffer: 1024 * 20000,
encoding: 'UTF-8',
env: util._extend({}, process.env, { LANG: 'en_US.UTF-8' })
});
if (_psChild && _psChild.pid) {
_psPersistent = true;
_psChild.stdout.on('data', function (data) {
_psResult = _psResult + data.toString('utf8');
if (data.indexOf(_psCmdSeperator) >= 0) {
powerShellProceedResults(_psResult);
_psResult = '';
}
});
_psChild.stderr.on('data', function () {
powerShellProceedResults(_psResult + _psError);
});
_psChild.on('error', function () {
powerShellProceedResults(_psResult + _psError);
});
_psChild.on('close', function () {
_psChild.kill();
if (!_psChild) {
_psChild = spawn('powershell.exe', ['-NoLogo', '-InputFormat', 'Text', '-NoExit', '-Command', '-'], {
stdio: 'pipe',
windowsHide: true,
maxBuffer: 1024 * 20000,
encoding: 'UTF-8',
env: util._extend({}, process.env, { LANG: 'en_US.UTF-8' })
});
if (_psChild && _psChild.pid) {
_psPersistent = true;
_psChild.stdout.on('data', function (data) {
_psResult = _psResult + data.toString('utf8');
if (data.indexOf(_psCmdSeperator) >= 0) {
powerShellProceedResults(_psResult);
_psResult = '';
}
});
_psChild.stderr.on('data', function () {
powerShellProceedResults(_psResult + _psError);
});
_psChild.on('error', function () {
powerShellProceedResults(_psResult + _psError);
});
_psChild.on('close', function () {
_psChild.kill();
});
}
}
}
function powerShellRelease() {
try {
_psChild.stdin.write('exit' + os.EOL);
_psChild.stdin.end();
_psPersistent = false;
if (_psChild) {
_psChild.stdin.write('exit' + os.EOL);
_psChild.stdin.end();
_psPersistent = false;
}
} catch (e) {
_psChild.kill();
}
_psChild = null;
}
function powerShell(cmd) {