format-table: add new cell type for displaying major/minor devnums

This commit is contained in:
Lennart Poettering
2023-03-29 18:27:05 +02:00
parent bd33207bb1
commit 522f12224c
2 changed files with 38 additions and 0 deletions
+37
View 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;
}
+1
View 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