commit a88ae11ea0ac4b98bc3140a8882e488c6151cbdc from: Benjamin Stürz date: Tue May 30 12:32:07 2023 UTC read_disk(): fix handling of CDs drives with no CD in them Signed-off-by: Benjamin Stürz commit - c92da76c8585268dc922418fcf63e51c72f8a532 commit + a88ae11ea0ac4b98bc3140a8882e488c6151cbdc blob - b4e4814a224399b2a700d820b0810c584894396d blob + 65528dfcddfcb1e5160d0d81a46d39cbd679df7b --- lsblk.c +++ lsblk.c @@ -354,37 +354,41 @@ static const struct statfs *find_mount (const char *de return NULL; } -static struct my_diskinfo read_disk (const char *name) +static int read_disk (const char *name, struct my_diskinfo *disk) { - struct my_diskinfo disk; struct disklabel label; char *ppath, *letter; - bzero (&disk, sizeof disk); + bzero (disk, sizeof *disk); { // Read disklabel. size_t len; int fd; fd = opendev (name, O_RDONLY, OPENDEV_PART | OPENDEV_BLCK, &ppath); - if (fd < 0) - err (1, "opendev(%s)", name); + if (fd < 0) { + warn ("read_disk(): opendev(%s)", name); + return -1; + } - if (ioctl (fd, DIOCGDINFO, &label) < 0) - err (1, "ioctl(%s, DIOCGDINFO)", name); + if (ioctl (fd, DIOCGDINFO, &label) < 0) { + warn ("read_disk(): ioctl(%s, DIOCGDINFO)", name); + close (fd); + return -1; + } close (fd); len = strlen (ppath); letter = ppath + len - 1; } - strlcpy (disk.name, name, sizeof disk.name); - disk.size = DL_GETDSIZE (&label) * label.d_secsize; - memcpy (disk.type, label.d_typename, sizeof disk.type); - stripdisk (disk.type); - memcpy (disk.label, label.d_packname, sizeof disk.label); - memcpy (disk.duid, label.d_uid, sizeof disk.duid); - disk.num_parts = 0; + strlcpy (disk->name, name, sizeof disk->name); + disk->size = DL_GETDSIZE (&label) * label.d_secsize; + memcpy (disk->type, label.d_typename, sizeof disk->type); + stripdisk (disk->type); + memcpy (disk->label, label.d_packname, sizeof disk->label); + memcpy (disk->duid, label.d_uid, sizeof disk->duid); + disk->num_parts = 0; for (uint16_t i = 0; i < label.d_npartitions; ++i) { const struct partition *p = &label.d_partitions[i]; @@ -411,11 +415,11 @@ static struct my_diskinfo read_disk (const char *name) part.fstype = fstypenames[p->p_fstype]; if (i != 2) - disk.used += part.size; - disk.parts[disk.num_parts++] = part; + disk->used += part.size; + disk->parts[disk->num_parts++] = part; } - return disk; + return 0; } static const char *bd_statusstr (int status) { @@ -633,23 +637,32 @@ int main (int argc, char *argv[]) size_t num_disks = 0; if (argc == 0) { - for (char *disk; (disk = strsep (&names, ",")) != NULL; ) { - char *colon = strchr (disk, ':'); + for (char *name; (name = strsep (&names, ",")) != NULL; ) { + char *colon = strchr (name, ':'); + struct my_diskinfo disk; + if (colon) *colon = '\0'; - disks[num_disks++] = read_disk (disk); + if (read_disk (name, &disk) == 0) { + disks[num_disks++] = disk; + } + if (colon) *colon = ':'; } } else { for (int i = 0; i < argc; ++i) { - char *disk = basename (argv[i]); - char *last = disk + strlen (disk) - 1; + char *name = basename (argv[i]); + char *last = name + strlen (name) - 1; + struct my_diskinfo disk; + if (isalpha (*last)) { - warnx ("%s: specifying a partition is not supported, using the drive.", disk); + warnx ("%s: specifying a partition is not supported, using the drive.", name); *last = '\0'; } - disks[num_disks++] = read_disk (disk); + if (read_disk (name, &disk) == 0) { + disks[num_disks++] = disk; + } } }