]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
lsblk: add PARTTYPENAME column
authorKarel Zak <kzak@redhat.com>
Wed, 4 Dec 2019 11:38:57 +0000 (12:38 +0100)
committerKarel Zak <kzak@redhat.com>
Wed, 4 Dec 2019 11:38:57 +0000 (12:38 +0100)
Print also partition type in human-readable way.

Addresses: https://bugzilla.redhat.com/show_bug.cgi?id=1777261
Signed-off-by: Karel Zak <kzak@redhat.com>
misc-utils/lsblk-properties.c
misc-utils/lsblk.c
misc-utils/lsblk.h

index a1aa69531243bbe15cc814dba6d38588b2a3b550..a1c73bc7d0cd9d5cddbef47b0520597c9a068167 100644 (file)
@@ -9,6 +9,7 @@
 #include "xalloc.h"
 #include "mangle.h"
 #include "path.h"
+#include "nls.h"
 
 #include "lsblk.h"
 
@@ -311,3 +312,66 @@ void lsblk_properties_deinit(void)
        udev_unref(udev);
 #endif
 }
+
+
+
+/*
+ * Partition types
+ */
+struct lsblk_parttype {
+       unsigned int    code;           /* type as number or zero */
+       char            *name;          /* description */
+       char            *typestr;       /* type as string or NULL */
+};
+
+static const struct lsblk_parttype mbr_types[] =
+{
+       #include "pt-mbr-partnames.h"
+};
+
+#define DEF_GUID(_u, _n) \
+       { \
+               .typestr = (_u), \
+               .name = (_n),    \
+       }
+static const struct lsblk_parttype gpt_types[] =
+{
+       #include "pt-gpt-partnames.h"
+};
+
+const char *lsblk_parttype_code_to_string(const char *code, const char *pttype)
+{
+       size_t i;
+
+       if (!code || !pttype)
+               return NULL;
+
+       if (strcmp(pttype, "dos") == 0 || strcmp(pttype, "mbr") == 0) {
+               char *end = NULL;
+               unsigned int xcode;
+
+               errno = 0;
+               xcode = strtol(code, &end, 16);
+
+               if (errno || *end != '\0')
+                       return NULL;
+
+               for (i = 0; i < ARRAY_SIZE(mbr_types); i++) {
+                       const struct lsblk_parttype *t = &mbr_types[i];
+
+                       if (t->name && t->code == xcode)
+                               return t->name;
+               }
+
+       } else if (strcmp(pttype, "gpt") == 0) {
+               for (i = 0; i < ARRAY_SIZE(gpt_types); i++) {
+                       const struct lsblk_parttype *t = &gpt_types[i];
+
+                       if (t->name && t->typestr &&
+                           strcasecmp(code, t->typestr) == 0)
+                               return t->name;
+               }
+       }
+
+       return NULL;
+}
index cd9f1f823decfdffa3fb83d72863c18032f29a36..441655e24938331ae6a868f6d5f6f8fea8fb98fc 100644 (file)
@@ -79,6 +79,7 @@ enum {
        COL_PTUUID,
        COL_PTTYPE,
        COL_PARTTYPE,
+       COL_PARTTYPENAME,
        COL_PARTLABEL,
        COL_PARTUUID,
        COL_PARTFLAGS,
@@ -168,7 +169,8 @@ static struct colinfo infos[] = {
        [COL_PTUUID] = { "PTUUID",  36,  0, N_("partition table identifier (usually UUID)") },
        [COL_PTTYPE] = { "PTTYPE",  0.1, 0, N_("partition table type") },
 
-       [COL_PARTTYPE]  = { "PARTTYPE",  36,  0, N_("partition type UUID") },
+       [COL_PARTTYPE]  = { "PARTTYPE",  36,  0, N_("partition type code or UUID") },
+       [COL_PARTTYPENAME]  = { "PARTTYPENAME",  0.1,  0, N_("partition type name") },
        [COL_PARTLABEL] = { "PARTLABEL", 0.1, 0, N_("partition LABEL") },
        [COL_PARTUUID]  = { "PARTUUID",  36,  0, N_("partition UUID") },
        [COL_PARTFLAGS] = { "PARTFLAGS",  36,  0, N_("partition flags") },
@@ -826,6 +828,15 @@ static char *device_get_data(
                if (prop && prop->parttype)
                        str = xstrdup(prop->parttype);
                break;
+       case COL_PARTTYPENAME:
+               prop = lsblk_device_get_properties(dev);
+               if (prop && prop->parttype && prop->pttype) {
+                       const char *x = lsblk_parttype_code_to_string(
+                                               prop->parttype, prop->pttype);
+                       if (x)
+                               str = xstrdup(x);
+               }
+               break;
        case COL_PARTLABEL:
                prop = lsblk_device_get_properties(dev);
                if (prop && prop->partlabel)
index f922dc8bf940397c131e2741c54e13bce247f115..87221271ed080cd8e852ba7a23a5fced2c8eea05 100644 (file)
@@ -199,6 +199,8 @@ extern void lsblk_device_free_properties(struct lsblk_devprop *p);
 extern struct lsblk_devprop *lsblk_device_get_properties(struct lsblk_device *dev);
 extern void lsblk_properties_deinit(void);
 
+extern const char *lsblk_parttype_code_to_string(const char *code, const char *pttype);
+
 /* lsblk-devtree.c */
 void lsblk_reset_iter(struct lsblk_iter *itr, int direction);
 struct lsblk_device *lsblk_new_device(void);