Move block device existence check

feature/node-rewrite
Sven Slootweg 1 year ago
parent be5317183c
commit 15577b2908

@ -4,6 +4,7 @@ const DataLoader = require("dataloader");
const fs = require("fs").promises; const fs = require("fs").promises;
const matchValue = require("match-value"); const matchValue = require("match-value");
const memoizee = require("memoizee"); const memoizee = require("memoizee");
const { InvalidObject } = require("dlayer");
const unreachable = require("@joepie91/unreachable")("@sysquery/block-devices"); const unreachable = require("@joepie91/unreachable")("@sysquery/block-devices");
const dlayerSource = require("../dlayer-source"); const dlayerSource = require("../dlayer-source");
@ -13,6 +14,7 @@ const mapFromSource = require("../map-from-source");
const treeMapAsync = require("../tree-map-async"); const treeMapAsync = require("../tree-map-async");
const treeFind = require("../tree-find"); const treeFind = require("../tree-find");
function makePredicate({ path, name }) { function makePredicate({ path, name }) {
if (path != null) { if (path != null) {
return (device) => device.path === path; return (device) => device.path === path;
@ -36,6 +38,7 @@ module.exports = {
}); });
return { return {
// FIXME: This is probably broken; {path,name} selectors are always unique keys!
lsblk: new DataLoader(async function (selectors) { lsblk: new DataLoader(async function (selectors) {
let blockDeviceTree = await lsblkOnce(); let blockDeviceTree = await lsblkOnce();
@ -50,46 +53,47 @@ module.exports = {
}; };
}, },
types: { types: {
"sysquery.blockDevices.BlockDevice": function ({ name, path }) { "sysquery.blockDevices.BlockDevice": async function ({ name, path }, { lsblk }) {
return { let entry = await lsblk.load({ name, path });
... dlayerSource("lsblk", {
[dlayerSource.ID]: { name, path }, if (entry != null) {
name: "name", return {
path: (device) => fs.realpath(device.path), ... dlayerSource("lsblk", {
type: (device) => matchValue(device.type, { [dlayerSource.ID]: { name, path },
partition: "PARTITION", name: "name",
disk: "DISK", path: (device) => fs.realpath(device.path),
loopDevice: "LOOP_DEVICE" type: (device) => matchValue(device.type, {
}), partition: "PARTITION",
size: "size", disk: "DISK",
mountpoint: "mountpoint", // FIXME: Isn't this obsoleted by `mounts`? loopDevice: "LOOP_DEVICE"
deviceNumber: "deviceNumber", }),
removable: "removable", size: "size",
readOnly: "readOnly", mountpoint: "mountpoint", // FIXME: Isn't this obsoleted by `mounts`?
children: (device, { $make }) => device.children.map((child) => { deviceNumber: "deviceNumber",
return $make("sysquery.blockDevices.BlockDevice", { name: child.name }); removable: "removable",
readOnly: "readOnly",
children: (device, { $make }) => device.children.map((child) => {
return $make("sysquery.blockDevices.BlockDevice", { name: child.name });
})
}) })
}) };
}; } else {
return InvalidObject;
}
} }
}, },
extensions: { extensions: {
"sysquery.mounts.Mount": { "sysquery.mounts.Mount": {
sourceDevice: async function (_, { lsblk, $make, $getProperty }) { sourceDevice: async function (_, { $make, $getProperty }) {
let sourceDevicePath = await $getProperty(this, "sourceDevicePath"); let sourceDevicePath = await $getProperty(this, "sourceDevicePath");
if (sourceDevicePath == null) { if (sourceDevicePath == null) {
// This occurs when the mount is not backed by a device, eg. an sshfs FUSE mount // This occurs when the mount is not backed by a device, eg. an sshfs FUSE mount
return null; return null;
} else { } else {
let realSourcePath = await fs.realpath(sourceDevicePath); return $make("sysquery.blockDevices.BlockDevice", {
path: await fs.realpath(sourceDevicePath)
if (await lsblk.load({ path: realSourcePath }) != null) { });
return $make("sysquery.blockDevices.BlockDevice", { path: realSourcePath });
} else {
// This occurs when the `sourceDevice` is a valid device, but it is not a *block* device, eg. `/dev/fuse`
return null;
}
} }
} }
} }

Loading…
Cancel
Save