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
|
@ -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);
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue