]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
format-table: add new cell type for displaying major/minor devnums
authorLennart Poettering <lennart@poettering.net>
Mon, 27 Mar 2023 16:14:39 +0000 (18:14 +0200)
committerLennart Poettering <lennart@poettering.net>
Wed, 29 Mar 2023 16:27:05 +0000 (18:27 +0200)
src/shared/format-table.c
src/shared/format-table.h

index 43e6510c4eedf6ff2ed6728ac8a70b59040ac5e8..204e8b68b6d5903c9b066862e60b68990816e819 100644 (file)
@@ -7,6 +7,7 @@
 #include "sd-id128.h"
 
 #include "alloc-util.h"
+#include "devnum-util.h"
 #include "fd-util.h"
 #include "fileio.h"
 #include "format-table.h"
@@ -108,6 +109,7 @@ typedef struct TableData {
                 gid_t gid;
                 pid_t pid;
                 mode_t mode;
+                dev_t devnum;
                 /* … add more here as we start supporting more cell data types … */
         };
 } TableData;
@@ -352,6 +354,9 @@ static size_t table_data_size(TableDataType type, const void *data) {
         case TABLE_MODE_INODE_TYPE:
                 return sizeof(mode_t);
 
+        case TABLE_DEVNUM:
+                return sizeof(dev_t);
+
         default:
                 assert_not_reached();
         }
@@ -869,6 +874,7 @@ int table_add_many_internal(Table *t, TableDataType first_type, ...) {
                         gid_t gid;
                         pid_t pid;
                         mode_t mode;
+                        dev_t devnum;
                 } buffer;
 
                 switch (type) {
@@ -1029,6 +1035,11 @@ int table_add_many_internal(Table *t, TableDataType first_type, ...) {
                         data = &buffer.mode;
                         break;
 
+                case TABLE_DEVNUM:
+                        buffer.devnum = va_arg(ap, dev_t);
+                        data = &buffer.devnum;
+                        break;
+
                 case TABLE_SET_MINIMUM_WIDTH: {
                         size_t w = va_arg(ap, size_t);
 
@@ -1276,6 +1287,8 @@ int table_hide_column_from_display_internal(Table *t, ...) {
 }
 
 static int cell_data_compare(TableData *a, size_t index_a, TableData *b, size_t index_b) {
+        int r;
+
         assert(a);
         assert(b);
 
@@ -1383,6 +1396,13 @@ static int cell_data_compare(TableData *a, size_t index_a, TableData *b, size_t
                 case TABLE_MODE_INODE_TYPE:
                         return CMP(a->mode, b->mode);
 
+                case TABLE_DEVNUM:
+                        r = CMP(major(a->devnum), major(b->devnum));
+                        if (r != 0)
+                                return r;
+
+                        return CMP(minor(a->devnum), minor(b->devnum));
+
                 default:
                         ;
                 }
@@ -1893,6 +1913,15 @@ static const char *table_data_format(Table *t, TableData *d, bool avoid_uppercas
 
                 return inode_type_to_string(d->mode);
 
+        case TABLE_DEVNUM:
+                if (devnum_is_zero(d->devnum))
+                        return table_ersatz_string(t);
+
+                if (asprintf(&d->formatted, DEVNUM_FORMAT_STR, DEVNUM_FORMAT_VAL(d->devnum)) < 0)
+                        return NULL;
+
+                break;
+
         default:
                 assert_not_reached();
         }
@@ -2713,6 +2742,14 @@ static int table_data_to_json(TableData *d, JsonVariant **ret) {
 
                 return json_variant_new_unsigned(ret, d->mode);
 
+        case TABLE_DEVNUM:
+                if (devnum_is_zero(d->devnum))
+                        return json_variant_new_null(ret);
+
+                return json_build(ret, JSON_BUILD_ARRAY(
+                                                  JSON_BUILD_UNSIGNED(major(d->devnum)),
+                                                  JSON_BUILD_UNSIGNED(minor(d->devnum))));
+
         default:
                 return -EINVAL;
         }
index dbed6cdee9f92bd186e7e187742237bb919a1a45..148418c70f6143a1ad0d3db7d6086ba8185ffc6b 100644 (file)
@@ -53,6 +53,7 @@ typedef enum TableDataType {
         TABLE_SIGNAL,
         TABLE_MODE,            /* as in UNIX file mode (mode_t), in typical octal output */
         TABLE_MODE_INODE_TYPE, /* also mode_t, but displays only the inode type as string */
+        TABLE_DEVNUM,          /* a dev_t, displayed in the usual major:minor way */
         _TABLE_DATA_TYPE_MAX,
 
         /* The following are not really data types, but commands for table_add_cell_many() to make changes to