From: Lennart Poettering Date: Wed, 21 Dec 2022 16:06:14 +0000 (+0100) Subject: format-table: add field type TABLE_PATH_BASENAME X-Git-Tag: v253-rc1~214^2~2 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=d0242ac94604aa9a862f8ca5746c70bc6719fe85;p=thirdparty%2Fsystemd.git format-table: add field type TABLE_PATH_BASENAME This is just like TABLE_PATH, but only shows the basename in regular tabular output. This is useful in systemd-repart for example --- diff --git a/src/partition/repart.c b/src/partition/repart.c index 6feb6ca2a19..6802a9c8740 100644 --- a/src/partition/repart.c +++ b/src/partition/repart.c @@ -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, diff --git a/src/shared/format-table.c b/src/shared/format-table.c index 1c1db2f43a9..4795e1ad808 100644 --- a/src/shared/format-table.c +++ b/src/shared/format-table.c @@ -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); diff --git a/src/shared/format-table.h b/src/shared/format-table.h index e835692c6fd..0e8aecffe6b 100644 --- a/src/shared/format-table.h +++ b/src/shared/format-table.h @@ -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, diff --git a/src/test/test-format-table.c b/src/test/test-format-table.c index 14341b97b4e..a1bc8b99acb 100644 --- a/src/test/test-format-table.c +++ b/src/test/test-format-table.c @@ -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); diff --git a/test/units/testsuite-58.sh b/test/units/testsuite-58.sh index d63537f8405..cf1007f69af 100755 --- a/test/units/testsuite-58.sh +++ b/test/units/testsuite-58.sh @@ -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,