You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

86 lines
2.4 KiB
JavaScript

"use strict";
const Promise = require("bluebird");
const path = require("path");
const errorChain = require("error-chain");
const createPegParser = require("../text-parser-pegjs");
const execBinary = require("../exec-binary");
const itemsToObject = require("../items-to-object");
/* FIXME: Error handling, eg. device not found errors */
// TODO: Handle this case: "Read NVMe Identify Controller failed: scsi error medium or hardware error (serious)" in a more specific manner
let AttributesError = errorChain.create("AttributesError", {});
let InfoError = errorChain.create("InfoError", {});
function outputParser(parserPath) {
return createPegParser({
grammarFile: path.join(__dirname, parserPath)
});
}
let attributesParser = outputParser("./parsers/commands/attributes.pegjs");
let infoParser = outputParser("./parsers/commands/info.pegjs");
let scanParser = outputParser("./parsers/commands/scan.pegjs");
module.exports = {
AttributesError: AttributesError,
InfoError: InfoError,
attributes: function ({ devicePath }) {
return Promise.try(() => {
return attributesParser;
}).then((parser) => {
return execBinary("smartctl", [devicePath])
.asRoot()
.withFlags({ attributes: true })
.withAllowedExitCodes([ 0, 2 ])
.requireOnStdout(parser)
.execute();
}).then((output) => {
let { error, attributes } = output.result;
if (error != null) {
throw new AttributesError(`smartctl returned an error: ${error}`);
} else {
// NOTE: Ignore the header, for now
return attributes;
}
});
},
info: function ({ devicePath }) {
return Promise.try(() => {
return infoParser;
}).then((parser) => {
return execBinary("smartctl", [devicePath])
.asRoot()
.withFlags({ info: true })
.withAllowedExitCodes([ 0, 2 ])
.requireOnStdout(parser)
.execute();
}).then((output) => {
let { error, fields } = output.result;
if (error != null) {
throw new InfoError(`smartctl returned an error: ${error}`);
} else {
// NOTE: Ignore the header, for now
return itemsToObject(fields);
}
});
},
scan: function () {
return Promise.try(() => {
return scanParser;
}).then((parser) => {
return execBinary("smartctl")
.asRoot()
.withFlags({ scan: true })
.requireOnStdout(parser)
.execute();
}).then((output) => {
// NOTE: Ignore the header, for now
return output.result.devices;
});
}
};