From 38a79ca5cd0366c3c6bac82c963b2d7aace2e4b0 Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Mon, 4 May 2020 16:33:59 +0200 Subject: cli: split parsing from nmc_utils_read_passwd_file() Makes it easier testable. --- clients/common/nm-client-utils.c | 25 ++++++++++++++++++------- clients/common/nm-client-utils.h | 4 ++++ 2 files changed, 22 insertions(+), 7 deletions(-) diff --git a/clients/common/nm-client-utils.c b/clients/common/nm-client-utils.c index a296a3b9dc..97885291d6 100644 --- a/clients/common/nm-client-utils.c +++ b/clients/common/nm-client-utils.c @@ -611,16 +611,11 @@ nmc_utils_read_passwd_file (const char *passwd_file, GError **error) { nm_auto_clear_secret_ptr NMSecretPtr contents = { 0 }; - gs_unref_hashtable GHashTable *pwds_hash = NULL; - const char *contents_str; - gsize contents_line; - - pwds_hash = g_hash_table_new_full (nm_str_hash, g_str_equal, g_free, (GDestroyNotify) nm_free_secret); NM_SET_OUT (out_error_line, -1); if (!passwd_file) - return g_steal_pointer (&pwds_hash); + return g_hash_table_new_full (nm_str_hash, g_str_equal, g_free, (GDestroyNotify) nm_free_secret); if (!nm_utils_file_get_contents (-1, passwd_file, @@ -632,7 +627,23 @@ nmc_utils_read_passwd_file (const char *passwd_file, error)) return NULL; - contents_str = contents.str; + return nmc_utils_parse_passwd_file (contents.str, out_error_line, error); +} + +GHashTable * +nmc_utils_parse_passwd_file (char *contents /* will be modified */, + gssize *out_error_line, + GError **error) +{ + gs_unref_hashtable GHashTable *pwds_hash = NULL; + const char *contents_str; + gsize contents_line; + + pwds_hash = g_hash_table_new_full (nm_str_hash, g_str_equal, g_free, (GDestroyNotify) nm_free_secret); + + NM_SET_OUT (out_error_line, -1); + + contents_str = contents; contents_line = 0; while (contents_str[0]) { nm_auto_free_secret char *l_hash_key = NULL; diff --git a/clients/common/nm-client-utils.h b/clients/common/nm-client-utils.h index f4f67544e7..8983e6c89b 100644 --- a/clients/common/nm-client-utils.h +++ b/clients/common/nm-client-utils.h @@ -43,6 +43,10 @@ const char *nmc_password_subst_char (void); void nmc_print_qrcode (const char *str); +GHashTable *nmc_utils_parse_passwd_file (char *contents, + gssize *out_error_line, + GError **error); + GHashTable *nmc_utils_read_passwd_file (const char *passwd_file, gssize *out_error_line, GError **error); -- cgit v1.2.1