Remove nested functions from device iterators.
* include/grub/arc/arc.h (grub_arc_iterate_devs_hook_t): New type. (grub_arc_iterate_devs): Add hook_data argument. * include/grub/ata.h (grub_ata_dev_iterate_hook_t): New type. (struct grub_ata_dev.iterate): Add hook_data argument. * include/grub/device.h (grub_device_iterate_hook_t): New type. (grub_device_iterate): Add hook_data argument. * include/grub/disk.h (grub_disk_dev_iterate_hook_t): New type. (struct grub_disk_dev.iterate): Add hook_data argument. (grub_disk_dev_iterate): Likewise. * include/grub/gpt_partition.h (grub_gpt_partition_map_iterate): Likewise. * include/grub/msdos_partition.h (grub_partition_msdos_iterate): Likewise. * include/grub/partition.h (grub_partition_iterate_hook_t): New type. (struct grub_partition_map.iterate): Add hook_data argument. (grub_partition_iterate): Likewise. * include/grub/scsi.h (grub_scsi_dev_iterate_hook_t): New type. (struct grub_scsi_dev.iterate): Add hook_data argument. Update all callers.
This commit is contained in:
parent
6c0314d638
commit
25239370fd
50 changed files with 1455 additions and 1165 deletions
|
@ -96,7 +96,7 @@ grub_core_cmd_insmod (struct grub_command *cmd __attribute__ ((unused)),
|
|||
}
|
||||
|
||||
static int
|
||||
grub_mini_print_devices (const char *name)
|
||||
grub_mini_print_devices (const char *name, void *data __attribute__ ((unused)))
|
||||
{
|
||||
grub_printf ("(%s) ", name);
|
||||
|
||||
|
@ -119,7 +119,7 @@ grub_core_cmd_ls (struct grub_command *cmd __attribute__ ((unused)),
|
|||
{
|
||||
if (argc < 1)
|
||||
{
|
||||
grub_device_iterate (grub_mini_print_devices);
|
||||
grub_device_iterate (grub_mini_print_devices, NULL);
|
||||
grub_xputs ("\n");
|
||||
grub_refresh ();
|
||||
}
|
||||
|
|
|
@ -85,94 +85,107 @@ grub_device_close (grub_device_t device)
|
|||
return grub_errno;
|
||||
}
|
||||
|
||||
int
|
||||
grub_device_iterate (int (*hook) (const char *name))
|
||||
struct part_ent
|
||||
{
|
||||
auto int iterate_disk (const char *disk_name);
|
||||
auto int iterate_partition (grub_disk_t disk,
|
||||
const grub_partition_t partition);
|
||||
struct part_ent *next;
|
||||
char *name;
|
||||
};
|
||||
|
||||
struct part_ent
|
||||
{
|
||||
struct part_ent *next;
|
||||
char *name;
|
||||
} *ents;
|
||||
/* Context for grub_device_iterate. */
|
||||
struct grub_device_iterate_ctx
|
||||
{
|
||||
grub_device_iterate_hook_t hook;
|
||||
void *hook_data;
|
||||
struct part_ent *ents;
|
||||
};
|
||||
|
||||
int iterate_disk (const char *disk_name)
|
||||
/* Helper for grub_device_iterate. */
|
||||
static int
|
||||
iterate_partition (grub_disk_t disk, const grub_partition_t partition,
|
||||
void *data)
|
||||
{
|
||||
struct grub_device_iterate_ctx *ctx = data;
|
||||
struct part_ent *p;
|
||||
char *part_name;
|
||||
|
||||
p = grub_malloc (sizeof (*p));
|
||||
if (!p)
|
||||
{
|
||||
grub_device_t dev;
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (hook (disk_name))
|
||||
return 1;
|
||||
part_name = grub_partition_get_name (partition);
|
||||
if (!part_name)
|
||||
{
|
||||
grub_free (p);
|
||||
return 1;
|
||||
}
|
||||
p->name = grub_xasprintf ("%s,%s", disk->name, part_name);
|
||||
grub_free (part_name);
|
||||
if (!p->name)
|
||||
{
|
||||
grub_free (p);
|
||||
return 1;
|
||||
}
|
||||
|
||||
dev = grub_device_open (disk_name);
|
||||
if (! dev)
|
||||
{
|
||||
grub_errno = GRUB_ERR_NONE;
|
||||
return 0;
|
||||
}
|
||||
p->next = ctx->ents;
|
||||
ctx->ents = p;
|
||||
|
||||
if (dev->disk)
|
||||
{
|
||||
struct part_ent *p;
|
||||
int ret = 0;
|
||||
return 0;
|
||||
}
|
||||
|
||||
ents = NULL;
|
||||
(void) grub_partition_iterate (dev->disk, iterate_partition);
|
||||
grub_device_close (dev);
|
||||
/* Helper for grub_device_iterate. */
|
||||
static int
|
||||
iterate_disk (const char *disk_name, void *data)
|
||||
{
|
||||
struct grub_device_iterate_ctx *ctx = data;
|
||||
grub_device_t dev;
|
||||
|
||||
grub_errno = GRUB_ERR_NONE;
|
||||
if (ctx->hook (disk_name, ctx->hook_data))
|
||||
return 1;
|
||||
|
||||
p = ents;
|
||||
while (p != NULL)
|
||||
{
|
||||
struct part_ent *next = p->next;
|
||||
|
||||
if (!ret)
|
||||
ret = hook (p->name);
|
||||
grub_free (p->name);
|
||||
grub_free (p);
|
||||
p = next;
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
grub_device_close (dev);
|
||||
dev = grub_device_open (disk_name);
|
||||
if (! dev)
|
||||
{
|
||||
grub_errno = GRUB_ERR_NONE;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int iterate_partition (grub_disk_t disk, const grub_partition_t partition)
|
||||
if (dev->disk)
|
||||
{
|
||||
struct part_ent *p;
|
||||
char *part_name;
|
||||
int ret = 0;
|
||||
|
||||
p = grub_malloc (sizeof (*p));
|
||||
if (!p)
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
ctx->ents = NULL;
|
||||
(void) grub_partition_iterate (dev->disk, iterate_partition, ctx);
|
||||
grub_device_close (dev);
|
||||
|
||||
part_name = grub_partition_get_name (partition);
|
||||
if (!part_name)
|
||||
grub_errno = GRUB_ERR_NONE;
|
||||
|
||||
p = ctx->ents;
|
||||
while (p != NULL)
|
||||
{
|
||||
struct part_ent *next = p->next;
|
||||
|
||||
if (!ret)
|
||||
ret = ctx->hook (p->name, ctx->hook_data);
|
||||
grub_free (p->name);
|
||||
grub_free (p);
|
||||
return 1;
|
||||
}
|
||||
p->name = grub_xasprintf ("%s,%s", disk->name, part_name);
|
||||
grub_free (part_name);
|
||||
if (!p->name)
|
||||
{
|
||||
grub_free (p);
|
||||
return 1;
|
||||
p = next;
|
||||
}
|
||||
|
||||
p->next = ents;
|
||||
ents = p;
|
||||
|
||||
return 0;
|
||||
return ret;
|
||||
}
|
||||
|
||||
/* Only disk devices are supported at the moment. */
|
||||
return grub_disk_dev_iterate (iterate_disk);
|
||||
grub_device_close (dev);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int
|
||||
grub_device_iterate (grub_device_iterate_hook_t hook, void *hook_data)
|
||||
{
|
||||
struct grub_device_iterate_ctx ctx = { hook, hook_data, NULL };
|
||||
|
||||
/* Only disk devices are supported at the moment. */
|
||||
return grub_disk_dev_iterate (iterate_disk, &ctx);
|
||||
}
|
||||
|
|
|
@ -223,7 +223,7 @@ find_free_slot (void)
|
|||
}
|
||||
|
||||
static int
|
||||
grub_util_biosdisk_iterate (int (*hook) (const char *name),
|
||||
grub_util_biosdisk_iterate (grub_disk_dev_iterate_hook_t hook, void *hook_data,
|
||||
grub_disk_pull_t pull)
|
||||
{
|
||||
unsigned i;
|
||||
|
@ -232,7 +232,7 @@ grub_util_biosdisk_iterate (int (*hook) (const char *name),
|
|||
return 0;
|
||||
|
||||
for (i = 0; i < sizeof (map) / sizeof (map[0]); i++)
|
||||
if (map[i].drive && hook (map[i].drive))
|
||||
if (map[i].drive && hook (map[i].drive, hook_data))
|
||||
return 1;
|
||||
|
||||
return 0;
|
||||
|
|
|
@ -48,8 +48,7 @@ const char *type_names[] = {
|
|||
|
||||
static int
|
||||
iterate_rec (const char *prefix, const struct grub_arc_component *parent,
|
||||
int (*hook) (const char *name,
|
||||
const struct grub_arc_component *comp),
|
||||
grub_arc_iterate_devs_hook_t hook, void *hook_data,
|
||||
int alt_names)
|
||||
{
|
||||
const struct grub_arc_component *comp;
|
||||
|
@ -67,12 +66,13 @@ iterate_rec (const char *prefix, const struct grub_arc_component *parent,
|
|||
name = grub_xasprintf ("%s%s(%lu)", prefix, cname, comp->key);
|
||||
if (!name)
|
||||
return 1;
|
||||
if (hook (name, comp))
|
||||
if (hook (name, comp, hook_data))
|
||||
{
|
||||
grub_free (name);
|
||||
return 1;
|
||||
}
|
||||
if (iterate_rec ((parent ? name : prefix), comp, hook, alt_names))
|
||||
if (iterate_rec ((parent ? name : prefix), comp, hook, hook_data,
|
||||
alt_names))
|
||||
{
|
||||
grub_free (name);
|
||||
return 1;
|
||||
|
@ -83,11 +83,11 @@ iterate_rec (const char *prefix, const struct grub_arc_component *parent,
|
|||
}
|
||||
|
||||
int
|
||||
grub_arc_iterate_devs (int (*hook) (const char *name,
|
||||
const struct grub_arc_component *comp),
|
||||
grub_arc_iterate_devs (grub_arc_iterate_devs_hook_t hook, void *hook_data,
|
||||
int alt_names)
|
||||
{
|
||||
return iterate_rec ((alt_names ? "arc" : ""), NULL, hook, alt_names);
|
||||
return iterate_rec ((alt_names ? "arc" : ""), NULL, hook, hook_data,
|
||||
alt_names);
|
||||
}
|
||||
|
||||
grub_err_t
|
||||
|
|
|
@ -59,39 +59,50 @@ grub_partition_check_containment (const grub_disk_t disk,
|
|||
return 1;
|
||||
}
|
||||
|
||||
/* Context for grub_partition_map_probe. */
|
||||
struct grub_partition_map_probe_ctx
|
||||
{
|
||||
int partnum;
|
||||
grub_partition_t p;
|
||||
};
|
||||
|
||||
/* Helper for grub_partition_map_probe. */
|
||||
static int
|
||||
probe_iter (grub_disk_t dsk, const grub_partition_t partition, void *data)
|
||||
{
|
||||
struct grub_partition_map_probe_ctx *ctx = data;
|
||||
|
||||
if (ctx->partnum != partition->number)
|
||||
return 0;
|
||||
|
||||
if (!(grub_partition_check_containment (dsk, partition)))
|
||||
return 0;
|
||||
|
||||
ctx->p = (grub_partition_t) grub_malloc (sizeof (*ctx->p));
|
||||
if (! ctx->p)
|
||||
return 1;
|
||||
|
||||
grub_memcpy (ctx->p, partition, sizeof (*ctx->p));
|
||||
return 1;
|
||||
}
|
||||
|
||||
static grub_partition_t
|
||||
grub_partition_map_probe (const grub_partition_map_t partmap,
|
||||
grub_disk_t disk, int partnum)
|
||||
{
|
||||
grub_partition_t p = 0;
|
||||
struct grub_partition_map_probe_ctx ctx = {
|
||||
.partnum = partnum,
|
||||
.p = 0
|
||||
};
|
||||
|
||||
auto int find_func (grub_disk_t d, const grub_partition_t partition);
|
||||
|
||||
int find_func (grub_disk_t dsk,
|
||||
const grub_partition_t partition)
|
||||
{
|
||||
if (partnum != partition->number)
|
||||
return 0;
|
||||
|
||||
if (!(grub_partition_check_containment (dsk, partition)))
|
||||
return 0;
|
||||
|
||||
p = (grub_partition_t) grub_malloc (sizeof (*p));
|
||||
if (! p)
|
||||
return 1;
|
||||
|
||||
grub_memcpy (p, partition, sizeof (*p));
|
||||
return 1;
|
||||
}
|
||||
|
||||
partmap->iterate (disk, find_func);
|
||||
partmap->iterate (disk, probe_iter, &ctx);
|
||||
if (grub_errno)
|
||||
goto fail;
|
||||
|
||||
return p;
|
||||
return ctx.p;
|
||||
|
||||
fail:
|
||||
grub_free (p);
|
||||
grub_free (ctx.p);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
@ -162,62 +173,71 @@ grub_partition_probe (struct grub_disk *disk, const char *str)
|
|||
return part;
|
||||
}
|
||||
|
||||
/* Context for grub_partition_iterate. */
|
||||
struct grub_partition_iterate_ctx
|
||||
{
|
||||
int ret;
|
||||
grub_partition_iterate_hook_t hook;
|
||||
void *hook_data;
|
||||
};
|
||||
|
||||
/* Helper for grub_partition_iterate. */
|
||||
static int
|
||||
part_iterate (grub_disk_t dsk, const grub_partition_t partition, void *data)
|
||||
{
|
||||
struct grub_partition_iterate_ctx *ctx = data;
|
||||
struct grub_partition p = *partition;
|
||||
|
||||
if (!(grub_partition_check_containment (dsk, partition)))
|
||||
return 0;
|
||||
|
||||
p.parent = dsk->partition;
|
||||
dsk->partition = 0;
|
||||
if (ctx->hook (dsk, &p, ctx->hook_data))
|
||||
{
|
||||
ctx->ret = 1;
|
||||
return 1;
|
||||
}
|
||||
if (p.start != 0)
|
||||
{
|
||||
const struct grub_partition_map *partmap;
|
||||
dsk->partition = &p;
|
||||
FOR_PARTITION_MAPS(partmap)
|
||||
{
|
||||
grub_err_t err;
|
||||
err = partmap->iterate (dsk, part_iterate, ctx);
|
||||
if (err)
|
||||
grub_errno = GRUB_ERR_NONE;
|
||||
if (ctx->ret)
|
||||
break;
|
||||
}
|
||||
}
|
||||
dsk->partition = p.parent;
|
||||
return ctx->ret;
|
||||
}
|
||||
|
||||
int
|
||||
grub_partition_iterate (struct grub_disk *disk,
|
||||
int (*hook) (grub_disk_t disk,
|
||||
const grub_partition_t partition))
|
||||
grub_partition_iterate_hook_t hook, void *hook_data)
|
||||
{
|
||||
int ret = 0;
|
||||
|
||||
auto int part_iterate (grub_disk_t dsk, const grub_partition_t p);
|
||||
|
||||
int part_iterate (grub_disk_t dsk,
|
||||
const grub_partition_t partition)
|
||||
{
|
||||
struct grub_partition p = *partition;
|
||||
|
||||
if (!(grub_partition_check_containment (dsk, partition)))
|
||||
return 0;
|
||||
|
||||
p.parent = dsk->partition;
|
||||
dsk->partition = 0;
|
||||
if (hook (dsk, &p))
|
||||
{
|
||||
ret = 1;
|
||||
return 1;
|
||||
}
|
||||
if (p.start != 0)
|
||||
{
|
||||
const struct grub_partition_map *partmap;
|
||||
dsk->partition = &p;
|
||||
FOR_PARTITION_MAPS(partmap)
|
||||
{
|
||||
grub_err_t err;
|
||||
err = partmap->iterate (dsk, part_iterate);
|
||||
if (err)
|
||||
grub_errno = GRUB_ERR_NONE;
|
||||
if (ret)
|
||||
break;
|
||||
}
|
||||
}
|
||||
dsk->partition = p.parent;
|
||||
return ret;
|
||||
}
|
||||
struct grub_partition_iterate_ctx ctx = {
|
||||
.ret = 0,
|
||||
.hook = hook,
|
||||
.hook_data = hook_data
|
||||
};
|
||||
const struct grub_partition_map *partmap;
|
||||
|
||||
FOR_PARTITION_MAPS(partmap)
|
||||
{
|
||||
const struct grub_partition_map *partmap;
|
||||
FOR_PARTITION_MAPS(partmap)
|
||||
{
|
||||
grub_err_t err;
|
||||
err = partmap->iterate (disk, part_iterate);
|
||||
if (err)
|
||||
grub_errno = GRUB_ERR_NONE;
|
||||
if (ret)
|
||||
break;
|
||||
}
|
||||
grub_err_t err;
|
||||
err = partmap->iterate (disk, part_iterate, &ctx);
|
||||
if (err)
|
||||
grub_errno = GRUB_ERR_NONE;
|
||||
if (ctx.ret)
|
||||
break;
|
||||
}
|
||||
|
||||
return ret;
|
||||
return ctx.ret;
|
||||
}
|
||||
|
||||
char *
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue