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.

43 lines
1.2 KiB
JavaScript

"use strict";
const Promise = require("bluebird");
const execAll = require("execall");
const execBinary = require("../exec-binary");
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.sub;
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;
});
}
};