Преглед изворни кода

util: Refactor util_is_valid_knet_*_model

Instead of having two very similar functions, move the string
concatenation functionality into a common helper function
that accepts an array of strings.

The previous functions now only fetch the knet list and
copy the name field to a new array, which is then passed
to the new helper.

Signed-off-by: Jan Friesse <jfriesse@redhat.com>
Reviewed-by: Christine Caulfield <ccaulfie@redhat.com>
Jan Friesse пре 2 недеља
родитељ
комит
8a8976ffdc
1 измењених фајлова са 57 додато и 61 уклоњено
  1. 57 61
      exec/util.c

+ 57 - 61
exec/util.c

@@ -215,22 +215,25 @@ static int safe_strcat(char *dst, size_t dst_len, const char *src)
 	return (0);
 }
 
+#define UTILS_IS_VALID_KNET_LIST_MAX_ITEMS		256
+
 /*
- * val - knet crypto model to find
- * crypto_list_str - string with concatenated list of available crypto models - can be NULL
+ * val - string in items array to find
+ * items - array of const char * of items
+ * no_items - size of array
+ * list_str - string with concatenated list of items - can be NULL
  * machine_parseable_str - 0 - split strings by space, 1 - use human form (split by "," and last item with "or")
  * error_string_prefix - Prefix to add into error string
  * error_string - Complete error string
  */
-int util_is_valid_knet_crypto_model(const char *val,
+static int util_is_valid_knet_list_helper(const char *val,
+	const char **items, size_t no_items,
 	const char **list_str, int machine_parseable_str,
 	const char *error_string_prefix, const char **error_string)
 {
-	size_t entries;
-	struct knet_crypto_info crypto_list[16];
-	size_t zi;
 	static char local_error_str[512];
 	static char local_list_str[256];
+	size_t zi;
 	int model_found = 0;
 
 	if (list_str != NULL) {
@@ -242,24 +245,9 @@ int util_is_valid_knet_crypto_model(const char *val,
 
 	safe_strcat(local_error_str, sizeof(local_error_str), error_string_prefix);
 
-	if (knet_get_crypto_list(NULL, &entries) != 0) {
-		*error_string = "internal error - cannot get knet crypto list";
-		return (-1);
-	}
-
-	if (entries > sizeof(crypto_list) / sizeof(crypto_list[0])) {
-		*error_string = "internal error - too many knet crypto list entries";
-		return (-1);
-	}
-
-	if (knet_get_crypto_list(crypto_list, &entries) != 0) {
-		*error_string = "internal error - cannot get knet crypto list";
-		return (-1);
-	}
-
-	for (zi = 0; zi < entries; zi++) {
+	for (zi = 0; zi < no_items; zi++) {
 		if (zi == 0) {
-		} else if (zi == entries - 1) {
+		} else if (zi == no_items - 1) {
 			if (machine_parseable_str) {
 				(void)safe_strcat(local_list_str, sizeof(local_list_str), " ");
 			} else {
@@ -273,9 +261,9 @@ int util_is_valid_knet_crypto_model(const char *val,
 			}
 		}
 
-		(void)safe_strcat(local_list_str, sizeof(local_list_str), crypto_list[zi].name);
+		(void)safe_strcat(local_list_str, sizeof(local_list_str), items[zi]);
 
-		if (val != NULL && strcmp(val, crypto_list[zi].name) == 0) {
+		if (val != NULL && strcmp(val, items[zi]) == 0) {
 			model_found = 1;
 		}
 	}
@@ -288,25 +276,56 @@ int util_is_valid_knet_crypto_model(const char *val,
 	return (model_found);
 }
 
-int util_is_valid_knet_compress_model(const char *val,
+/*
+ * val - knet crypto model to find
+ * crypto_list_str - string with concatenated list of available crypto models - can be NULL
+ * machine_parseable_str - 0 - split strings by space, 1 - use human form (split by "," and last item with "or")
+ * error_string_prefix - Prefix to add into error string
+ * error_string - Complete error string
+ */
+int util_is_valid_knet_crypto_model(const char *val,
 	const char **list_str, int machine_parseable_str,
 	const char *error_string_prefix, const char **error_string)
 {
 	size_t entries;
-	struct knet_compress_info compress_list[16];
+	struct knet_crypto_info crypto_list[UTILS_IS_VALID_KNET_LIST_MAX_ITEMS];
+	const char *items[UTILS_IS_VALID_KNET_LIST_MAX_ITEMS];
 	size_t zi;
-	static char local_error_str[512];
-	static char local_list_str[256];
-	int model_found = 0;
 
-	if (list_str != NULL) {
-		*list_str = local_list_str;
+	if (knet_get_crypto_list(NULL, &entries) != 0) {
+		*error_string = "internal error - cannot get knet crypto list";
+		return (-1);
 	}
 
-	memset(local_error_str, 0, sizeof(local_error_str));
-	memset(local_list_str, 0, sizeof(local_list_str));
+	if (entries > sizeof(crypto_list) / sizeof(crypto_list[0])) {
+		*error_string = "internal error - too many knet crypto list entries";
+		return (-1);
+	}
 
-	safe_strcat(local_error_str, sizeof(local_error_str), error_string_prefix);
+	if (knet_get_crypto_list(crypto_list, &entries) != 0) {
+		*error_string = "internal error - cannot get knet crypto list";
+		return (-1);
+	}
+
+	for (zi = 0; zi < entries; zi++) {
+		items[zi] = crypto_list[zi].name;
+	}
+
+	return (util_is_valid_knet_list_helper(val, items, entries, list_str,
+	    machine_parseable_str, error_string_prefix, error_string));
+}
+
+/*
+ * Similar to util_is_valid_knet_crypto_model
+ */
+int util_is_valid_knet_compress_model(const char *val,
+	const char **list_str, int machine_parseable_str,
+	const char *error_string_prefix, const char **error_string)
+{
+	size_t entries;
+	struct knet_compress_info compress_list[UTILS_IS_VALID_KNET_LIST_MAX_ITEMS];
+	const char *items[UTILS_IS_VALID_KNET_LIST_MAX_ITEMS];
+	size_t zi;
 
 	if (knet_get_compress_list(NULL, &entries) != 0) {
 		*error_string = "internal error - cannot get knet compress list";
@@ -324,34 +343,11 @@ int util_is_valid_knet_compress_model(const char *val,
 	}
 
 	for (zi = 0; zi < entries; zi++) {
-		if (zi == 0) {
-		} else if (zi == entries - 1) {
-			if (machine_parseable_str) {
-				(void)safe_strcat(local_list_str, sizeof(local_list_str), " ");
-			} else {
-				(void)safe_strcat(local_list_str, sizeof(local_list_str), " or ");
-			}
-		} else {
-			if (machine_parseable_str) {
-				(void)safe_strcat(local_list_str, sizeof(local_list_str), " ");
-			} else {
-				(void)safe_strcat(local_list_str, sizeof(local_list_str), ", ");
-			}
-		}
-
-		(void)safe_strcat(local_list_str, sizeof(local_list_str), compress_list[zi].name);
-
-		if (val != NULL && strcmp(val, compress_list[zi].name) == 0) {
-			model_found = 1;
-		}
+		items[zi] = compress_list[zi].name;
 	}
 
-	if (!model_found) {
-		(void)safe_strcat(local_error_str, sizeof(local_error_str), local_list_str);
-		*error_string = local_error_str;
-	}
-
-	return (model_found);
+	return (util_is_valid_knet_list_helper(val, items, entries, list_str,
+	    machine_parseable_str, error_string_prefix, error_string));
 }
 
 int