Disk.fromLsblk constructor

Disk.fromLsblk(
  1. Map<String, dynamic> device
)

Implementation

factory Disk.fromLsblk(Map<String, dynamic> device) {
  String? getDeviceName(String? value) {
    if (value != null) {
      return !path.isAbsolute(value) ? path.absolute("/dev", value) : value;
    }

    return null;
  }

  PartitionTableType? getPartitionTableType(String? value) => value == "gpt"
      ? PartitionTableType.gpt
      : value == "dos"
          ? PartitionTableType.mbr
          : null;

  String getDescription() {
    var description = <String>[
      device["label"] ?? "",
      device["vendor"] ?? "",
      device["model"] ?? "",
    ];
    if (device["children"] != null) {
      final subLabels = (device["children"] as List)
          .where((children) => children["label"] != null
              ? children["label"] != device["label"]
              : children["mountpoint"] != null)
          .map((children) => children["label"] ?? children["mountpoint"]);
      if (subLabels.isNotEmpty) {
        description.add(subLabels.join(", "));
      }
    }
    return description.join(" ").replaceAll(r"/\s+/g", " ").trim();
  }

  final name = getDeviceName(device["name"]);
  final kname = getDeviceName(device["kname"]);
  final virtual =
      RegExp(r"/^(block)$/i").hasMatch(device["subsystems"] ?? "");
  final scsi =
      RegExp(r"/^(sata|scsi|ata|ide|pci)$/i").hasMatch(device["tran"] ?? "");
  final usb = RegExp(r"/^(usb)$/i").hasMatch(device["tran"] ?? "");
  final readonly = device["ro"] == 1;
  final removable = device["rm"] == 1 || device["hotplug"] == 1 || virtual;

  return Disk(
    busType: (device["tran"] ?? "UNKNOWN").toUpperCase(),
    device: name ?? "",
    raw: kname ?? name ?? "",
    description: getDescription(),
    size: device["size"],
    blockSize: device["phy-sec"] ?? 512,
    logicalBlockSize: device["log-sec"] ?? 512,
    mountpoints: ((device["children"] ?? [device]) as List)
        .where((mountpoint) => mountpoint["mountpoint"] != null)
        .map((mountpoint) => Mountpoint.fromLsblk(mountpoint))
        .toList(growable: false),
    readOnly: readonly,
    system: !removable && !virtual,
    virtual: virtual,
    removable: removable,
    scsi: scsi,
    usb: usb,
    partitionTableType: getPartitionTableType(device["pttype"]),
  );
}