From 216f23c74e80df759eebcdc596c7f4642113e3ec Mon Sep 17 00:00:00 2001 From: Sebastian Hildebrandt Date: Wed, 16 Nov 2016 16:44:44 +0100 Subject: [PATCH] blockDevices: improved for older lsblk versions --- CHANGELOG.md | 1 + lib/filesystem.js | 87 +++++++++++++++++++++++++++++------------------ lib/index.js | 1 + 3 files changed, 55 insertions(+), 34 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5b8a6e8..b03eb99 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -89,6 +89,7 @@ Other changes | Version | Date | Comment | | -------------- | -------------- | -------- | +| 3.11.2 | 2016-11-16 | blockDevices: improved for older lsblk versions | | 3.11.1 | 2016-11-16 | fixed small bug in blockDevices | | 3.11.0 | 2016-11-15 | blockDevices for OSX and extended blockDevices | | 3.10.2 | 2016-11-14 | bug fix fsSize on OSX | diff --git a/lib/filesystem.js b/lib/filesystem.js index 896eb18..3a54559 100644 --- a/lib/filesystem.js +++ b/lib/filesystem.js @@ -128,12 +128,48 @@ function fromTo(header, label) { for (let i = to; i < header.length && header[i] == ' '; i++) { to = i } - console.log(label + ' - ' + from + ' ' + to); return { from: from, to: to } } + +function parseBlk(lines) { + let header = lines[0]; + let ft_label = fromTo(header, 'LABEL'); + let ft_model = fromTo(header, 'MODEL'); + let ft_serial = fromTo(header, 'SERIAL'); + lines.splice(0, 1); + + let data = []; + lines.forEach(orgline => { + if (orgline != '') { + if (orgline.substr(header.indexOf('FSTYPE'), 1) == ' ') { orgline = orgline.substr(0, header.indexOf('FSTYPE')) + '-' + orgline.substr(header.indexOf('FSTYPE') + 1, 1000)} + if (orgline.substr(header.indexOf('MOUNTPOINT'), 1) == ' ') { orgline = orgline.substr(0, header.indexOf('MOUNTPOINT')) + '-' + orgline.substr(header.indexOf('MOUNTPOINT') + 1, 1000)} + if (orgline.substr(header.indexOf('UUID'), 1) == ' ') { orgline = orgline.substr(0, header.indexOf('UUID')) + '-' + orgline.substr(header.indexOf('UUID') + 1, 1000)} + if (orgline.substr(header.indexOf('TRAN'), 1) == ' ') { orgline = orgline.substr(0, header.indexOf('TRAN')) + '-' + orgline.substr(header.indexOf('TRAN') + 1, 1000)} + let line = orgline.replace(/[├─│└]+/g, ""); + line = line.replace(/ +/g, " ").trim().split(' '); + data.push({ + 'name': line[0], + 'type': line[1], + 'fstype': (line[3] == '-' ? '' : line[3]), + 'mount': (line[4] == '-' ? '' : line[4]), + 'size': parseInt(line[2]), + 'physical': (line[1] == 'disk' ? (line[6] == '0' ? 'SSD' : 'HDD') : (line[1] == 'rom' ? 'CD/DVD' : '')), + 'uuid': (line[5] == '-' ? '' : line[5]), + 'label': orgline.substring(ft_label.from, ft_label.to).trim(), + 'model': orgline.substring(ft_model.from, ft_model.to).trim(), + 'serial': (ft_serial.from >= 0 ? orgline.substring(ft_serial.from, ft_serial.to).trim() : ''), + 'protocol': (ft_serial.from >= 0 && line[8] == '-' ? '' : line[8]) + }) + } + }); + data = util.unique(data); + data = util.sortByKey(data, ['type', 'name']); + return data; +} + function blockDevices(callback) { return new Promise((resolve, reject) => { @@ -146,45 +182,28 @@ function blockDevices(callback) { 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 -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 -bo NAME,TYPE,SIZE,FSTYPE,MOUNTPOINT,UUID,ROTA,RO,TRAN,SERIAL,LABEL,MODEL,OWNER", function (error, stdout) { let data = []; if (!error) { let lines = stdout.toString().split('\n'); - let header = lines[0]; - let ft_label = fromTo(header, 'LABEL'); - let ft_model = fromTo(header, 'MODEL'); - let ft_serial = fromTo(header, 'SERIAL'); - lines.splice(0, 1); - lines.forEach(orgline => { - if (orgline != '') { - if (orgline.substr(header.indexOf('FSTYPE'), 1) == ' ') { orgline = orgline.substr(0, header.indexOf('FSTYPE')) + '-' + orgline.substr(header.indexOf('FSTYPE') + 1, 1000)} - if (orgline.substr(header.indexOf('MOUNTPOINT'), 1) == ' ') { orgline = orgline.substr(0, header.indexOf('MOUNTPOINT')) + '-' + orgline.substr(header.indexOf('MOUNTPOINT') + 1, 1000)} - if (orgline.substr(header.indexOf('UUID'), 1) == ' ') { orgline = orgline.substr(0, header.indexOf('UUID')) + '-' + orgline.substr(header.indexOf('UUID') + 1, 1000)} - if (orgline.substr(header.indexOf('TRAN'), 1) == ' ') { orgline = orgline.substr(0, header.indexOf('TRAN')) + '-' + orgline.substr(header.indexOf('TRAN') + 1, 1000)} - let line = orgline.replace(/[├─│└]+/g, ""); - line = line.replace(/ +/g, " ").trim().split(' '); - data.push({ - 'name': line[0], - 'type': line[1], - 'fstype': (line[3] == '-' ? '' : line[3]), - 'mount': (line[4] == '-' ? '' : line[4]), - 'size': parseInt(line[2]), - 'physical': (line[1] == 'disk' ? (line[6] == '0' ? 'SSD' : 'HDD') : (line[1] == 'rom' ? 'CD/DVD' : '')), - 'uuid': (line[5] == '-' ? '' : line[5]), - 'label': orgline.substring(ft_label.from, ft_label.to).trim(), - 'model': orgline.substring(ft_model.from, ft_model.to).trim(), - 'serial': orgline.substring(ft_serial.from, ft_serial.to).trim(), - 'protocol': (line[8] == '-' ? '' : line[8]) - }) + data = parseBlk(lines); + if (callback) { + callback(data) + } + resolve(data); + } else { + exec("lsblk -bo NAME,TYPE,SIZE,FSTYPE,MOUNTPOINT,UUID,ROTA,RO,LABEL,MODEL,OWNER", function (error, stdout) { + if (!error) { + let lines = stdout.toString().split('\n'); + data = parseBlk(lines); } + if (callback) { + callback(data) + } + resolve(data); }); - data = util.unique(data); - data = util.sortByKey(data, ['type', 'name']); } - if (callback) { - callback(data) - } - resolve(data); }); } if (_darwin) { diff --git a/lib/index.js b/lib/index.js index d7cc1d1..6f5838a 100644 --- a/lib/index.js +++ b/lib/index.js @@ -81,6 +81,7 @@ // -------------------------------- // // version date comment +// 3.11.2 2016-11-16 blockDevices: improved for older lsblk versions // 3.11.1 2016-11-16 fixed small bug in blockDevices // 3.11.0 2016-11-15 blockDevices for OSX and extended blockDevices // 3.10.2 2016-11-14 bug fix fsSize on OSX