fdisk: add the 'i'nfo command

Add the 'i'nfo command to fdisk that prints details about a specific partition.
Details are everything the function 'fdisk_label_get_field' can return.

Signed-off-by: Jean-Loup 'clippix' Bogalho <clippix@lse.epita.fr>
This commit is contained in:
Jean-Loup 'clippix' Bogalho 2015-05-09 23:15:23 +02:00 committed by Karel Zak
parent 71af1c7239
commit 3e3b51b3b1
3 changed files with 50 additions and 0 deletions

View File

@ -99,6 +99,7 @@ struct menu menu_generic = {
MENU_BENT ('p', N_("print the partition table")),
MENU_ENT ('t', N_("change a partition type")),
MENU_BENT_E('v', N_("verify the partition table"), FDISK_DISKLABEL_BSD),
MENU_ENT ('i', N_("print information about a partition")),
MENU_XENT('d', N_("print the raw data of the first sector from the device")),
MENU_XENT('D', N_("print the raw data of the disklabel from the device")),
@ -557,6 +558,9 @@ static int generic_menu_cb(struct fdisk_context **cxt0,
case 'v':
rc = fdisk_verify_disklabel(cxt);
break;
case 'i':
rc = print_partition_info(cxt);
break;
}
/* expert mode */

View File

@ -564,6 +564,50 @@ void change_partition_type(struct fdisk_context *cxt)
fdisk_unref_partition(pa);
}
int print_partition_info(struct fdisk_context *cxt)
{
struct fdisk_partition *pa = NULL;
int rc = 0, details;
size_t i, nfields;
int *fields = NULL;
struct fdisk_label *lb = fdisk_get_label(cxt, NULL);
if ((rc = fdisk_ask_partnum(cxt, &i, FALSE)))
return rc;
if ((rc = fdisk_get_partition(cxt, i, &pa))) {
fdisk_warnx(cxt, _("Partition %zu does not exist yet!"), i + 1);
return rc;
}
details = fdisk_is_details(cxt);
fdisk_enable_details(cxt, 1);
if ((rc = fdisk_label_get_fields_ids(lb, cxt, &fields, &nfields)))
goto clean_data;
fdisk_enable_details(cxt, details);
for (i = 0; i < nfields; ++i) {
int id = fields[i];
char *data = NULL;
const struct fdisk_field *fd = fdisk_label_get_field(lb, id);
if (!fd)
continue;
rc = fdisk_partition_to_string(pa, cxt, id, &data);
if (rc < 0)
goto clean_data;
fdisk_info(cxt, _("%15s: %s"), fdisk_field_get_name(fd), data);
free(data);
}
clean_data:
fdisk_unref_partition(pa);
free(fields);
return rc;
}
static size_t skip_empty(const unsigned char *buf, size_t i, size_t sz)
{
size_t next;

View File

@ -36,6 +36,8 @@ extern int process_fdisk_menu(struct fdisk_context **cxt);
extern int ask_callback(struct fdisk_context *cxt, struct fdisk_ask *ask,
void *data __attribute__((__unused__)));
extern int print_partition_info(struct fdisk_context *cxt);
/* prototypes for fdisk.c */
extern void dump_firstsector(struct fdisk_context *cxt);
extern void dump_disklabel(struct fdisk_context *cxt);