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.
74 lines
2.1 KiB
JavaScript
74 lines
2.1 KiB
JavaScript
"use strict";
|
|
|
|
const Promise = require("bluebird");
|
|
const execAll = require("execall");
|
|
const execBinary = require("../exec-binary");
|
|
const createJSONParser = require("../text-parser-json");
|
|
|
|
const controllerFieldMapping = require("./controller-field-mapping");
|
|
|
|
function createNamespaceParser() {
|
|
return {
|
|
supportsStreams: false,
|
|
parse: function (input) {
|
|
return {
|
|
namespaces: execAll(/^\[\s*[0-9]+\]:(?:(0)|0x([0-9A-F]+))$/gm, input)
|
|
.map((match) => {
|
|
let [ idLiteral, idHex ] = match.subMatches;
|
|
|
|
if (idLiteral != null) {
|
|
/* NOTE: This is a special case for when the value is exactly 0 - and maybe there are others too, hence still doing a parseInt, so we can easily change the regex later if needed:
|
|
https://stackoverflow.com/questions/11922876/what-does-a-hash-sign-do-in-printf#comment15879638_11922887
|
|
https://github.com/linux-nvme/nvme-cli/blob/f9ebefe27b0596006d76d58f3219a9fc12e88664/nvme.c#L979
|
|
*/
|
|
return parseInt(idLiteral);
|
|
} else {
|
|
return parseInt(idHex, 16);
|
|
}
|
|
})
|
|
};
|
|
}
|
|
};
|
|
}
|
|
|
|
module.exports = {
|
|
listNamespaces: function ({ devicePath }) {
|
|
return Promise.try(() => {
|
|
return execBinary("nvme", [ "list-ns", devicePath ])
|
|
.asRoot()
|
|
.expectOnStdout(createNamespaceParser())
|
|
.execute();
|
|
}).then((output) => {
|
|
return output.result.namespaces;
|
|
});
|
|
},
|
|
identifyController: function ({ devicePath }) {
|
|
return Promise.try(() => {
|
|
return execBinary([ "nvme", "id-ctrl" ], [ devicePath ])
|
|
.asRoot()
|
|
.withFlags({ "output-format": "json" })
|
|
.requireOnStdout(createJSONParser())
|
|
.execute();
|
|
}).then(({ result }) => {
|
|
let transformed = {};
|
|
|
|
for (let key of Object.keys(result)) {
|
|
let mapping = controllerFieldMapping[key];
|
|
|
|
if (mapping != null) {
|
|
let { name, transform } = (typeof mapping === "string")
|
|
? { name: mapping, transform: (value) => value }
|
|
: { name: mapping.name, transform: mapping.transform };
|
|
|
|
transformed[name] = transform(result[key]);
|
|
}
|
|
}
|
|
|
|
// TODO: Warn on unrecognized keys
|
|
return transformed;
|
|
// }).catch((error) => {
|
|
// console.dir(error);
|
|
});
|
|
}
|
|
};
|