|
|
5ce0c7 |
From c74aa1326061723bd87e599699096d1085472772 Mon Sep 17 00:00:00 2001
|
|
|
46b2f6 |
From: "Richard W.M. Jones" <rjones@redhat.com>
|
|
|
46b2f6 |
Date: Tue, 26 Nov 2019 12:12:45 +0000
|
|
|
46b2f6 |
Subject: [PATCH] options: Simplify selector parsing for --key options.
|
|
|
46b2f6 |
|
|
|
46b2f6 |
Refactor this code to use guestfs_int_split_string function which
|
|
|
46b2f6 |
slightly simplifies it. This should have no effect.
|
|
|
46b2f6 |
|
|
|
46b2f6 |
(cherry picked from commit 530d0beef74d48617717463a5b585f21e2ed62be
|
|
|
46b2f6 |
in libguestfs-common)
|
|
|
46b2f6 |
---
|
|
|
46b2f6 |
common/options/keys.c | 35 ++++++++++++++---------------------
|
|
|
46b2f6 |
1 file changed, 14 insertions(+), 21 deletions(-)
|
|
|
46b2f6 |
|
|
|
46b2f6 |
diff --git a/common/options/keys.c b/common/options/keys.c
|
|
|
46b2f6 |
index f783066ff..74b549731 100644
|
|
|
46b2f6 |
--- a/common/options/keys.c
|
|
|
46b2f6 |
+++ b/common/options/keys.c
|
|
|
46b2f6 |
@@ -153,49 +153,42 @@ get_key (struct key_store *ks, const char *device)
|
|
|
46b2f6 |
}
|
|
|
46b2f6 |
|
|
|
46b2f6 |
struct key_store *
|
|
|
46b2f6 |
-key_store_add_from_selector (struct key_store *ks, const char *selector_orig)
|
|
|
46b2f6 |
+key_store_add_from_selector (struct key_store *ks, const char *selector)
|
|
|
46b2f6 |
{
|
|
|
46b2f6 |
- CLEANUP_FREE char *selector = strdup (selector_orig);
|
|
|
46b2f6 |
- const char *elem;
|
|
|
46b2f6 |
- char *saveptr;
|
|
|
46b2f6 |
+ CLEANUP_FREE_STRING_LIST char **fields =
|
|
|
46b2f6 |
+ guestfs_int_split_string (':', selector);
|
|
|
46b2f6 |
struct key_store_key key;
|
|
|
46b2f6 |
|
|
|
46b2f6 |
- if (!selector)
|
|
|
46b2f6 |
- error (EXIT_FAILURE, errno, "strdup");
|
|
|
46b2f6 |
+ if (!fields)
|
|
|
46b2f6 |
+ error (EXIT_FAILURE, errno, "guestfs_int_split_string");
|
|
|
46b2f6 |
|
|
|
46b2f6 |
- /* 1: device */
|
|
|
46b2f6 |
- elem = strtok_r (selector, ":", &saveptr);
|
|
|
46b2f6 |
- if (!elem) {
|
|
|
46b2f6 |
+ if (guestfs_int_count_strings (fields) != 3) {
|
|
|
46b2f6 |
invalid_selector:
|
|
|
46b2f6 |
- error (EXIT_FAILURE, 0, "invalid selector for --key: %s", selector_orig);
|
|
|
46b2f6 |
+ error (EXIT_FAILURE, 0, "invalid selector for --key: %s", selector);
|
|
|
46b2f6 |
}
|
|
|
46b2f6 |
- key.device = strdup (elem);
|
|
|
46b2f6 |
+
|
|
|
46b2f6 |
+ /* 1: device */
|
|
|
46b2f6 |
+ key.device = strdup (fields[0]);
|
|
|
46b2f6 |
if (!key.device)
|
|
|
46b2f6 |
error (EXIT_FAILURE, errno, "strdup");
|
|
|
46b2f6 |
|
|
|
46b2f6 |
/* 2: key type */
|
|
|
46b2f6 |
- elem = strtok_r (NULL, ":", &saveptr);
|
|
|
46b2f6 |
- if (!elem)
|
|
|
46b2f6 |
- goto invalid_selector;
|
|
|
46b2f6 |
- else if (STREQ (elem, "key"))
|
|
|
46b2f6 |
+ if (STREQ (fields[1], "key"))
|
|
|
46b2f6 |
key.type = key_string;
|
|
|
46b2f6 |
- else if (STREQ (elem, "file"))
|
|
|
46b2f6 |
+ else if (STREQ (fields[1], "file"))
|
|
|
46b2f6 |
key.type = key_file;
|
|
|
46b2f6 |
else
|
|
|
46b2f6 |
goto invalid_selector;
|
|
|
46b2f6 |
|
|
|
46b2f6 |
/* 3: actual key */
|
|
|
46b2f6 |
- elem = strtok_r (NULL, ":", &saveptr);
|
|
|
46b2f6 |
- if (!elem)
|
|
|
46b2f6 |
- goto invalid_selector;
|
|
|
46b2f6 |
switch (key.type) {
|
|
|
46b2f6 |
case key_string:
|
|
|
46b2f6 |
- key.string.s = strdup (elem);
|
|
|
46b2f6 |
+ key.string.s = strdup (fields[2]);
|
|
|
46b2f6 |
if (!key.string.s)
|
|
|
46b2f6 |
error (EXIT_FAILURE, errno, "strdup");
|
|
|
46b2f6 |
break;
|
|
|
46b2f6 |
case key_file:
|
|
|
46b2f6 |
- key.file.name = strdup (elem);
|
|
|
46b2f6 |
+ key.file.name = strdup (fields[2]);
|
|
|
46b2f6 |
if (!key.file.name)
|
|
|
46b2f6 |
error (EXIT_FAILURE, errno, "strdup");
|
|
|
46b2f6 |
break;
|
|
|
46b2f6 |
--
|
|
|
5ce0c7 |
2.18.4
|
|
|
46b2f6 |
|