]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
fdisk: add the 'i'nfo command
authorJean-Loup 'clippix' Bogalho <clippix@lse.epita.fr>
Sat, 9 May 2015 21:15:23 +0000 (23:15 +0200)
committerKarel Zak <kzak@redhat.com>
Mon, 11 May 2015 10:26:30 +0000 (12:26 +0200)
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>
disk-utils/fdisk-menu.c
disk-utils/fdisk.c
disk-utils/fdisk.h

index a77a13f80d0d7c82e494d23d83dbe57035d2b5ff..6c7f583207428259f678db1b05408c78dd098ddc 100644 (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 */
index a1e7259bfef1ec3809a32284c9d1a444be69447c..2b045743db0a607051114ad8cfdea6058e2a9d5b 100644 (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;
index 4bd47339a25e55061b95961d57ff4057d5cdf687..6a62c2497478e46ca7439ccef5164415a6599e6c 100644 (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);