]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
id128: refuse --app-specific= if we're listing GPT types
authorMike Yuan <me@yhndnzj.com>
Tue, 16 Jul 2024 19:10:46 +0000 (21:10 +0200)
committerLennart Poettering <lennart@poettering.net>
Wed, 17 Jul 2024 07:35:58 +0000 (09:35 +0200)
Prompted by #33737

The intention of b37e8184a5a376749fbf68674ed6d7a4fc9901aa
is to expose sd_id128_get_app_specific() on command line.
But combining that with GPT type list makes little sense.

src/id128/id128.c

index a9ea6c5deec1cbd5a0caa9b790920e530410182f..6d62538da95c5dd214311665051b9e0a07beb695 100644 (file)
@@ -16,7 +16,7 @@
 #include "verbs.h"
 
 static Id128PrettyPrintMode arg_mode = ID128_PRINT_ID128;
-static sd_id128_t arg_app = {};
+static sd_id128_t arg_app = SD_ID128_NULL;
 static bool arg_value = false;
 static PagerFlags arg_pager_flags = 0;
 static bool arg_legend = true;
@@ -72,15 +72,12 @@ static int verb_invocation_id(int argc, char **argv, void *userdata) {
 }
 
 static int show_one(Table **table, const char *name, sd_id128_t uuid, bool first) {
-        sd_id128_t u;
         int r;
 
         assert(table);
 
-        if (sd_id128_is_null(arg_app))
-                u = uuid;
-        else
-                assert_se(sd_id128_get_app_specific(uuid, arg_app, &u) == 0);
+        if (!name)
+                name = "XYZ";
 
         if (arg_mode == ID128_PRINT_PRETTY) {
                 _cleanup_free_ char *id = NULL;
@@ -91,7 +88,7 @@ static int show_one(Table **table, const char *name, sd_id128_t uuid, bool first
 
                 ascii_strupper(id);
 
-                r = id128_pretty_print_sample(id, u);
+                r = id128_pretty_print_sample(id, uuid);
                 if (r < 0)
                         return r;
                 if (!first)
@@ -100,19 +97,19 @@ static int show_one(Table **table, const char *name, sd_id128_t uuid, bool first
         }
 
         if (arg_value)
-                return id128_pretty_print(u, arg_mode);
+                return id128_pretty_print(uuid, arg_mode);
 
         if (!*table) {
                 *table = table_new("name", "id");
                 if (!*table)
                         return log_oom();
+
                 table_set_width(*table, 0);
         }
 
         return table_add_many(*table,
                               TABLE_STRING, name,
-                              arg_mode == ID128_PRINT_ID128 ? TABLE_ID128 : TABLE_UUID,
-                              u);
+                              arg_mode == ID128_PRINT_ID128 ? TABLE_ID128 : TABLE_UUID, uuid);
 }
 
 static int verb_show(int argc, char **argv, void *userdata) {
@@ -120,23 +117,26 @@ static int verb_show(int argc, char **argv, void *userdata) {
         int r;
 
         argv = strv_skip(argv, 1);
-        if (strv_isempty(argv))
+        if (strv_isempty(argv)) {
+                if (!sd_id128_is_null(arg_app))
+                        return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
+                                               "'show --app-specific=' can only be used with explicit UUID input.");
+
                 for (const GptPartitionType *e = gpt_partition_type_table; e->name; e++) {
                         r = show_one(&table, e->name, e->uuid, e == gpt_partition_type_table);
                         if (r < 0)
                                 return r;
                 }
-        else
+        else
                 STRV_FOREACH(p, argv) {
                         sd_id128_t uuid;
-                        bool have_uuid;
-                        const char *id;
+                        const char *id = NULL;
 
                         /* Check if the argument is an actual UUID first */
-                        have_uuid = sd_id128_from_string(*p, &uuid) >= 0;
+                        bool is_uuid = sd_id128_from_string(*p, &uuid) >= 0;
 
-                        if (have_uuid)
-                                id = gpt_partition_type_uuid_to_string(uuid) ?: "XYZ";
+                        if (is_uuid)
+                                id = gpt_partition_type_uuid_to_string(uuid);
                         else {
                                 GptPartitionType type;
 
@@ -148,6 +148,9 @@ static int verb_show(int argc, char **argv, void *userdata) {
                                 id = *p;
                         }
 
+                        if (!sd_id128_is_null(arg_app))
+                                assert_se(sd_id128_get_app_specific(uuid, arg_app, &uuid) >= 0);
+
                         r = show_one(&table, id, uuid, p == argv);
                         if (r < 0)
                                 return r;