Adapt other cd-device query functions to new world order, fix

default cd device (assume 'a' partition).
This commit is contained in:
martin 2018-11-08 20:29:37 +00:00
parent 375f4ceb14
commit 1c2e5b02c0
3 changed files with 96 additions and 85 deletions

View File

@ -1,4 +1,4 @@
/* $NetBSD: defs.h,v 1.21 2018/11/07 21:20:23 martin Exp $ */
/* $NetBSD: defs.h,v 1.22 2018/11/08 20:29:37 martin Exp $ */
/*
* Copyright 1997 Piermont Information Systems Inc.
@ -475,6 +475,9 @@ void toplevel(void);
/* from disks.c */
bool get_default_cdrom(char *, size_t);
int find_disks(const char *);
bool enumerate_disks(void *state,bool (*func)(void *state, const char *dev));
bool is_cdrom_device(const char *dev);
struct menudesc;
void fmt_fspart(struct menudesc *, int, void *);
void disp_cur_fspart(int, int);

View File

@ -1,4 +1,4 @@
/* $NetBSD: disks.c,v 1.21 2018/11/08 11:56:56 martin Exp $ */
/* $NetBSD: disks.c,v 1.22 2018/11/08 20:29:37 martin Exp $ */
/*
* Copyright 1997 Piermont Information Systems Inc.
@ -110,8 +110,6 @@ static int fsck_preen(const char *, int, const char *, bool silent);
static void fixsb(const char *, const char *, char);
static bool is_gpt(const char *);
static int incoregpt(pm_devs_t *, partinfo *);
static bool enumerate_disks(void *state,
bool (*func)(void *state, const char *dev));
static bool tmpfs_on_var_shm(void);
@ -371,7 +369,11 @@ get_default_cdrom_helper(void *state, const char *dev)
{
struct default_cdrom_data *data = state;
if (!is_cdrom_device(dev))
return true;
strlcpy(data->device, dev, data->max_len);
strlcat(data->device, "a", data->max_len); /* default to partition a */
data->found = true;
return false; /* one is enough, stop iteration */
@ -510,7 +512,7 @@ is_ffs_wedge(const char *dev)
/*
* Does this device match an entry in our default CDROM device list?
*/
static bool
bool
is_cdrom_device(const char *dev)
{
static const char *cdrom_devices[] = { CD_NAMES, 0 };
@ -528,7 +530,7 @@ is_cdrom_device(const char *dev)
* Stop iteration when the callback returns false.
* Return true when iteration actually happend, false on error.
*/
static bool
bool
enumerate_disks(void *state, bool (*func)(void *state, const char *dev))
{
static const int mib[] = { CTL_HW, HW_DISKNAMES };

View File

@ -1,4 +1,4 @@
/* $NetBSD: util.c,v 1.14 2018/11/07 21:20:23 martin Exp $ */
/* $NetBSD: util.c,v 1.15 2018/11/08 20:29:37 martin Exp $ */
/*
* Copyright 1997 Piermont Information Systems Inc.
@ -412,6 +412,83 @@ get_iso9660_volname(int dev, int sess, char *volname)
return -1;
}
/*
* Local state while iterating CDs and collecting volumes
*/
struct get_available_cds_state {
struct cd_info *info;
size_t count;
};
/*
* Callback function: if this is a CD, enumerate all volumes on it
*/
static bool
get_available_cds_helper(void *arg, const char *device)
{
struct get_available_cds_state *state = arg;
char dname[16], volname[80];
struct disklabel label;
int part, dev, error, sess, ready;
if (!is_cdrom_device(device))
return true;
sprintf(dname, "/dev/r%s%c", device, 'a'+RAW_PART);
dev = open(dname, O_RDONLY, 0);
if (dev == -1)
return true;
ready = 0;
error = ioctl(dev, DIOCTUR, &ready);
if (error != 0 || ready == 0) {
close(dev);
return true;
}
error = ioctl(dev, DIOCGDINFO, &label);
close(dev);
if (error != 0)
return true;
for (part = 0; part < label.d_npartitions; part++) {
if (label.d_partitions[part].p_fstype == FS_UNUSED
|| label.d_partitions[part].p_size == 0)
continue;
if (label.d_partitions[part].p_fstype == FS_ISO9660) {
sess = label.d_partitions[part].p_cdsession;
sprintf(dname, "/dev/r%s%c", device, 'a'+part);
dev = open(dname, O_RDONLY, 0);
if (dev == -1)
continue;
error = get_iso9660_volname(dev, sess, volname);
close(dev);
if (error)
continue;
sprintf(state->info->device_name,
"%s%c", device, 'a'+part);
sprintf(state->info->menu, "%s (%s)",
state->info->device_name, volname);
} else {
/*
* All install CDs use partition
* a for the sets.
*/
if (part > 0)
continue;
sprintf(state->info->device_name,
"%s%c", device, 'a'+part);
strcpy(state->info->menu, state->info->device_name);
}
state->info++;
if (++state->count >= MAX_CD_INFOS)
return false;
}
return true;
}
/*
* Get a list of all available CD media (not drives!), return
* the number of entries collected.
@ -419,85 +496,14 @@ get_iso9660_volname(int dev, int sess, char *volname)
static int
get_available_cds(void)
{
static const char *cdrom_devices[] = { CD_NAMES, 0 };
char dname[16], volname[80], fmt[80], tmp[80], *star;
struct cd_info *info = cds;
struct disklabel label;
int i, part, dev, error, sess, ready, count = 0;
struct get_available_cds_state data;
for (const char **dev_pat = cdrom_devices; *dev_pat; dev_pat++) {
for (i = 0; i < MAX_CD_DEVS; i++) {
strcpy(fmt, *dev_pat);
star = strchr(fmt, '*');
if (star) {
strcpy(star, "%d");
sprintf(tmp, "/dev/r%s%%c", fmt);
sprintf(dname, tmp, i, 'a'+RAW_PART);
} else {
sprintf(dname, "/dev/r%s%c", fmt,
'a'+RAW_PART);
}
dev = open(dname, O_RDONLY, 0);
if (dev == -1)
continue;
ready = 0;
error = ioctl(dev, DIOCTUR, &ready);
if (error != 0 || ready == 0) {
close(dev);
continue;
}
error = ioctl(dev, DIOCGDINFO, &label);
close(dev);
if (error == 0) {
for (part = 0; part < label.d_npartitions;
part++) {
if (label.d_partitions[part].p_fstype
== FS_UNUSED
|| label.d_partitions[part].p_size == 0)
continue;
if (label.d_partitions[part].p_fstype
== FS_ISO9660) {
sess = label.d_partitions[part]
.p_cdsession;
sprintf(dname, "/dev/rcd%d%c", i,
'a'+part);
dev = open(dname, O_RDONLY, 0);
if (dev == -1)
continue;
error = get_iso9660_volname(dev, sess,
volname);
close(dev);
if (error) continue;
sprintf(info->device_name, "cd%d%c",
i, 'a'+part);
sprintf(info->menu, "%s (%s)",
info->device_name,
volname);
} else {
/*
* All install CDs use partition
* a for the sets.
*/
if (part > 0)
continue;
sprintf(info->device_name, "cd%d%c",
i, 'a'+part);
strcpy(info->menu, info->device_name);
}
info++;
if (++count >= MAX_CD_INFOS)
break;
}
}
if (++count >= MAX_CD_INFOS)
break;
if (!star)
break;
}
if (++count >= MAX_CD_INFOS)
break;
}
return count;
data.info = cds;
data.count = 0;
enumerate_disks(&data, get_available_cds_helper);
return data.count;
}
static int