From f7022c92f0abe41b92120d54f7b8fa4f48a918bf Mon Sep 17 00:00:00 2001 From: Sebastian Hildebrandt Date: Thu, 7 Jul 2022 10:01:49 +0200 Subject: [PATCH] fsSize() optimized parsing if there are errors --- lib/filesystem.js | 26 ++++++++++++++++----- lib/index.d.ts | 3 +++ lib/util.js | 59 +++++++++++++++++++++++++---------------------- 3 files changed, 55 insertions(+), 33 deletions(-) diff --git a/lib/filesystem.js b/lib/filesystem.js index 4dd4730..3e9ec84 100644 --- a/lib/filesystem.js +++ b/lib/filesystem.js @@ -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) { diff --git a/lib/index.d.ts b/lib/index.d.ts index f0efaeb..b0cc802 100644 --- a/lib/index.d.ts +++ b/lib/index.d.ts @@ -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; export function get(valuesObject: any, cb?: (data: any) => any): Promise; export function observe(valuesObject: any, interval: number, cb?: (data: any) => any): number; + +export function powerShellStart(): void; +export function powerShellRelease(): void; diff --git a/lib/util.js b/lib/util.js index 6232022..8ee0a61 100644 --- a/lib/util.js +++ b/lib/util.js @@ -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) {