blockDevices: improved for older lsblk versions

This commit is contained in:
Sebastian Hildebrandt 2016-11-16 16:44:44 +01:00
parent 9418044263
commit 216f23c74e
3 changed files with 55 additions and 34 deletions

View File

@ -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 |

View File

@ -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) {

View File

@ -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