From dbf90c61457e20c91c1c39196bec0135707cfaad Mon Sep 17 00:00:00 2001 From: Thomas Debesse Date: Thu, 18 Nov 2021 15:02:41 +0100 Subject: [PATCH] smartctlUtil: do not reference error when not failing, do not return empty data if none (#221) The `smartctl -j --scan` command may not fail and list drives properly while `smartctl -j --info /dev/sda` may fail because of missing permissions. This error was making the extension on fail entirely when smartctl was selected as a disk temperature sensor provider while the user does not have enogh permission to do so. Also makes sure smartctl can be properly called if path contains a whitespace. --- .../smartctlUtil.js | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/freon@UshakovVasilii_Github.yahoo.com/smartctlUtil.js b/freon@UshakovVasilii_Github.yahoo.com/smartctlUtil.js index 03d469b..53e8981 100644 --- a/freon@UshakovVasilii_Github.yahoo.com/smartctlUtil.js +++ b/freon@UshakovVasilii_Github.yahoo.com/smartctlUtil.js @@ -4,7 +4,7 @@ const Me = imports.misc.extensionUtils.getCurrentExtension(); const ByteArray = imports.byteArray; function getSmartData (argv){ const smartctl = GLib.find_program_in_path('smartctl') - return JSON.parse(ByteArray.toString( GLib.spawn_command_line_sync(`${smartctl} ${argv} -j`)[1] )) + return JSON.parse(ByteArray.toString( GLib.spawn_command_line_sync(`'${smartctl}' ${argv} -j`)[1] )) } var smartctlUtil = class { @@ -12,10 +12,9 @@ var smartctlUtil = class { this._smartDevices = []; try { this._smartDevices = getSmartData("--scan")["devices"] - global.log('[FREON] test devices: ' + e); } catch (e) { global.log('[FREON] Unable to find smart devices: ' + e); - } + } this._updated = true; } @@ -33,11 +32,19 @@ var smartctlUtil = class { get temp() { return this._smartDevices.map(device => { + const info = getSmartData(`--info ${device["name"]}`); + if (info["smartctl"]["exit_status"] != 0) + return null; + + const attributes = getSmartData(`--attributes ${device["name"]}`); + if (attributes["smartctl"]["exit_status"] != 0) + return null; + return { - label: getSmartData(`--info ${device["name"]}`)["model_name"], - temp: parseFloat(getSmartData(`--attributes ${device["name"]}`).temperature.current) + label: info["model_name"], + temp: parseFloat(attributes.temperature.current) } - }) + }).filter(entry => entry != null); } destroy(callback) {