]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
format-table: add field type TABLE_PATH_BASENAME
authorLennart Poettering <lennart@poettering.net>
Wed, 21 Dec 2022 16:06:14 +0000 (17:06 +0100)
committerLennart Poettering <lennart@poettering.net>
Fri, 23 Dec 2022 16:35:21 +0000 (17:35 +0100)
This is just like TABLE_PATH, but only shows the basename in regular
tabular output.

This is useful in systemd-repart for example

src/partition/repart.c
src/shared/format-table.c
src/shared/format-table.h
src/test/test-format-table.c
test/units/testsuite-58.sh

index 6feb6ca2a1941d7fd2bde941e8106e620bfe72c4..6802a9c8740b9bd8fbfd4d88c376d7b7e1b4973f 100644 (file)
@@ -2461,7 +2461,7 @@ static int context_dump_partitions(Context *context) {
                                 TABLE_STRING, gpt_partition_type_uuid_to_string_harder(p->type.uuid, uuid_buffer),
                                 TABLE_STRING, empty_to_null(label) ?: "-", TABLE_SET_COLOR, empty_to_null(label) ? NULL : ansi_grey(),
                                 TABLE_UUID, p->new_uuid_is_set ? p->new_uuid : p->current_uuid,
-                                TABLE_STRING, p->definition_path ? basename(p->definition_path) : "-", TABLE_SET_COLOR, p->definition_path ? NULL : ansi_grey(),
+                                TABLE_PATH_BASENAME, p->definition_path, TABLE_SET_COLOR, p->definition_path ? NULL : ansi_grey(),
                                 TABLE_STRING, partname ?: "-", TABLE_SET_COLOR, partname ? NULL : ansi_highlight(),
                                 TABLE_UINT64, p->offset,
                                 TABLE_UINT64, p->current_size == UINT64_MAX ? 0 : p->current_size,
index 1c1db2f43a97bcbea4be85d4624ddeaee8dd6375..4795e1ad808f3a0bbe8b88f9fbb7f62ba3a73a4f 100644 (file)
@@ -281,6 +281,7 @@ static size_t table_data_size(TableDataType type, const void *data) {
 
         case TABLE_STRING:
         case TABLE_PATH:
+        case TABLE_PATH_BASENAME:
         case TABLE_FIELD:
         case TABLE_HEADER:
                 return strlen(data) + 1;
@@ -513,7 +514,7 @@ int table_add_cell_stringf_full(Table *t, TableCell **ret_cell, TableDataType dt
         int r;
 
         assert(t);
-        assert(IN_SET(dt, TABLE_STRING, TABLE_PATH, TABLE_FIELD, TABLE_HEADER));
+        assert(IN_SET(dt, TABLE_STRING, TABLE_PATH, TABLE_PATH_BASENAME, TABLE_FIELD, TABLE_HEADER));
 
         va_start(ap, format);
         r = vasprintf(&buffer, format, ap);
@@ -873,6 +874,7 @@ int table_add_many_internal(Table *t, TableDataType first_type, ...) {
 
                 case TABLE_STRING:
                 case TABLE_PATH:
+                case TABLE_PATH_BASENAME:
                 case TABLE_FIELD:
                 case TABLE_HEADER:
                         data = va_arg(ap, const char *);
@@ -1281,6 +1283,7 @@ static int cell_data_compare(TableData *a, size_t index_a, TableData *b, size_t
                         return strcmp(a->string, b->string);
 
                 case TABLE_PATH:
+                case TABLE_PATH_BASENAME:
                         return path_compare(a->string, b->string);
 
                 case TABLE_STRV:
@@ -1453,15 +1456,24 @@ static const char *table_data_format(Table *t, TableData *d, bool avoid_uppercas
 
         case TABLE_STRING:
         case TABLE_PATH:
+        case TABLE_PATH_BASENAME:
         case TABLE_FIELD:
-        case TABLE_HEADER:
+        case TABLE_HEADER: {
+                _cleanup_free_ char *bn = NULL;
+                const char *s;
+
+                if (d->type == TABLE_PATH_BASENAME)
+                        s = path_extract_filename(d->string, &bn) < 0 ? d->string : bn;
+                else
+                        s = d->string;
+
                 if (d->uppercase && !avoid_uppercasing) {
-                        d->formatted = new(char, strlen(d->string) + (d->type == TABLE_FIELD) + 1);
+                        d->formatted = new(char, strlen(s) + (d->type == TABLE_FIELD) + 1);
                         if (!d->formatted)
                                 return NULL;
 
                         char *q = d->formatted;
-                        for (char *p = d->string; *p; p++)
+                        for (const char *p = s; *p; p++)
                                 *(q++) = (char) toupper((unsigned char) *p);
 
                         if (d->type == TABLE_FIELD)
@@ -1470,14 +1482,20 @@ static const char *table_data_format(Table *t, TableData *d, bool avoid_uppercas
                         *q = 0;
                         return d->formatted;
                 } else if (d->type == TABLE_FIELD) {
-                        d->formatted = strjoin(d->string, ":");
+                        d->formatted = strjoin(s, ":");
                         if (!d->formatted)
                                 return NULL;
 
                         return d->formatted;
                 }
 
+                if (bn) {
+                        d->formatted = TAKE_PTR(bn);
+                        return d->formatted;
+                }
+
                 return d->string;
+        }
 
         case TABLE_STRV:
                 if (strv_isempty(d->strv))
@@ -2544,6 +2562,7 @@ static int table_data_to_json(TableData *d, JsonVariant **ret) {
 
         case TABLE_STRING:
         case TABLE_PATH:
+        case TABLE_PATH_BASENAME:
         case TABLE_FIELD:
         case TABLE_HEADER:
                 return json_variant_new_string(ret, d->string);
index e835692c6fdb9db19ac1fcae46c460f869ebf011..0e8aecffe6bd93b0fafcf75bae3864c293d19c89 100644 (file)
@@ -17,6 +17,7 @@ typedef enum TableDataType {
         TABLE_STRV,
         TABLE_STRV_WRAPPED,
         TABLE_PATH,
+        TABLE_PATH_BASENAME,       /* like TABLE_PATH, but display only last path element (i.e. the "basename") in regular output */
         TABLE_BOOLEAN,
         TABLE_BOOLEAN_CHECKMARK,
         TABLE_TIMESTAMP,
index 14341b97b4ea3960f43692efa2132863303025d8..a1bc8b99acbe6978b771236af96e2a8fb94a508b 100644 (file)
@@ -565,6 +565,24 @@ TEST(vertical) {
         assert_se(json_variant_equal(a, b));
 }
 
+TEST(path_basename) {
+        _cleanup_(table_unrefp) Table *t = NULL;
+        _cleanup_free_ char *formatted = NULL;
+
+        assert_se(t = table_new("x"));
+
+        table_set_header(t, false);
+
+        assert_se(table_add_many(t,
+                                 TABLE_PATH_BASENAME, "/foo/bar",
+                                 TABLE_PATH_BASENAME, "/quux/bar",
+                                 TABLE_PATH_BASENAME, "/foo/baz") >= 0);
+
+        assert_se(table_format(t, &formatted) >= 0);
+
+        assert_se(streq(formatted, "bar\nbar\nbaz\n"));
+}
+
 static int intro(void) {
         assert_se(setenv("SYSTEMD_COLORS", "0", 1) >= 0);
         assert_se(setenv("COLUMNS", "40", 1) >= 0);
index d63537f8405fbe9d74486cac6c1addc02876c64a..cf1007f69aff85339aca09ef600995c1d1d1ce76 100755 (executable)
@@ -381,7 +381,7 @@ EOF
                "type" : "swap",
                "label" : "label2",
                "uuid" : "837c3d67-21b3-478e-be82-7e7f83bf96d3",
-               "file" : "root.conf",
+               "file" : "$defs/root.conf",
                "node" : "$imgs/zzz1",
                "offset" : 1048576,
                "old_size" : 0,
@@ -441,7 +441,7 @@ EOF
                "type" : "swap",
                "label" : "label1",
                "uuid" : "7b93d1f2-595d-4ce3-b0b9-837fbd9e63b0",
-               "file" : "root1.conf",
+               "file" : "$defs/1/root1.conf",
                "node" : "$imgs/zzz1",
                "offset" : 1048576,
                "old_size" : 0,
@@ -456,7 +456,7 @@ EOF
                "type" : "swap",
                "label" : "label2",
                "uuid" : "837c3d67-21b3-478e-be82-7e7f83bf96d3",
-               "file" : "root2.conf",
+               "file" : "$defs/2/root2.conf",
                "node" : "$imgs/zzz2",
                "offset" : 34603008,
                "old_size" : 0,