diff options
author | Thomas Haller <thaller@redhat.com> | 2019-10-29 16:55:15 +0100 |
---|---|---|
committer | Thomas Haller <thaller@redhat.com> | 2019-11-07 11:34:36 +0100 |
commit | 2e91add7e40d7a110acc5de55e41f4aa011beb66 (patch) | |
tree | bf30e085fe3c11ad913645b0d9ab73f9574c6c41 /libnm/nm-libnm-utils.c | |
parent | 83d7599acce1016c005acaec5af021fc4474c427 (diff) | |
download | NetworkManager-2e91add7e40d7a110acc5de55e41f4aa011beb66.tar.gz |
libnm: move nm_permission_to_client()/nm_permission_result_to_client() to nm-libnm-utils.c
It's nicely trivial and independent. Move it to a separate place,
to avoid cluttering the more complicated code and to make it testable.
Also, use binary search to find the value by string.
Diffstat (limited to 'libnm/nm-libnm-utils.c')
-rw-r--r-- | libnm/nm-libnm-utils.c | 83 |
1 files changed, 83 insertions, 0 deletions
diff --git a/libnm/nm-libnm-utils.c b/libnm/nm-libnm-utils.c index 3d7b0ba6df..5638d0eae0 100644 --- a/libnm/nm-libnm-utils.c +++ b/libnm/nm-libnm-utils.c @@ -9,6 +9,7 @@ #include "nm-libnm-utils.h" #include "nm-glib-aux/nm-time-utils.h" +#include "nm-libnm-core-intern/nm-common-macros.h" /*****************************************************************************/ @@ -658,3 +659,85 @@ nm_utils_fixup_product_string (const char *desc) return desc_full; } + +/*****************************************************************************/ + +NMClientPermission +nm_permission_to_client (const char *nm) +{ + static const struct { + const char *name; + NMClientPermission perm; + } list[] = { + { NM_AUTH_PERMISSION_CHECKPOINT_ROLLBACK, NM_CLIENT_PERMISSION_CHECKPOINT_ROLLBACK }, + { NM_AUTH_PERMISSION_ENABLE_DISABLE_CONNECTIVITY_CHECK, NM_CLIENT_PERMISSION_ENABLE_DISABLE_CONNECTIVITY_CHECK }, + { NM_AUTH_PERMISSION_ENABLE_DISABLE_NETWORK, NM_CLIENT_PERMISSION_ENABLE_DISABLE_NETWORK }, + { NM_AUTH_PERMISSION_ENABLE_DISABLE_STATISTICS, NM_CLIENT_PERMISSION_ENABLE_DISABLE_STATISTICS }, + { NM_AUTH_PERMISSION_ENABLE_DISABLE_WIFI, NM_CLIENT_PERMISSION_ENABLE_DISABLE_WIFI }, + { NM_AUTH_PERMISSION_ENABLE_DISABLE_WIMAX, NM_CLIENT_PERMISSION_ENABLE_DISABLE_WIMAX }, + { NM_AUTH_PERMISSION_ENABLE_DISABLE_WWAN, NM_CLIENT_PERMISSION_ENABLE_DISABLE_WWAN }, + { NM_AUTH_PERMISSION_NETWORK_CONTROL, NM_CLIENT_PERMISSION_NETWORK_CONTROL }, + { NM_AUTH_PERMISSION_RELOAD, NM_CLIENT_PERMISSION_RELOAD }, + { NM_AUTH_PERMISSION_SETTINGS_MODIFY_GLOBAL_DNS, NM_CLIENT_PERMISSION_SETTINGS_MODIFY_GLOBAL_DNS }, + { NM_AUTH_PERMISSION_SETTINGS_MODIFY_HOSTNAME, NM_CLIENT_PERMISSION_SETTINGS_MODIFY_HOSTNAME }, + { NM_AUTH_PERMISSION_SETTINGS_MODIFY_OWN, NM_CLIENT_PERMISSION_SETTINGS_MODIFY_OWN }, + { NM_AUTH_PERMISSION_SETTINGS_MODIFY_SYSTEM, NM_CLIENT_PERMISSION_SETTINGS_MODIFY_SYSTEM }, + { NM_AUTH_PERMISSION_SLEEP_WAKE, NM_CLIENT_PERMISSION_SLEEP_WAKE }, + { NM_AUTH_PERMISSION_WIFI_SCAN, NM_CLIENT_PERMISSION_WIFI_SCAN }, + { NM_AUTH_PERMISSION_WIFI_SHARE_OPEN, NM_CLIENT_PERMISSION_WIFI_SHARE_OPEN }, + { NM_AUTH_PERMISSION_WIFI_SHARE_PROTECTED, NM_CLIENT_PERMISSION_WIFI_SHARE_PROTECTED }, + }; + gssize idx; + +#if NM_MORE_ASSERTS > 10 + { + static gboolean checked = FALSE; + int i, j; + + if (!checked) { + checked = TRUE; + + for (i = 0; i < G_N_ELEMENTS (list); i++) { + + nm_assert (list[i].perm != NM_CLIENT_PERMISSION_NONE); + nm_assert (list[i].name && list[i].name[0]); + if (i > 0) { + if (strcmp (list[i - 1].name, list[i].name) >= 0) { + g_error ("list is not sorted by name: #%d (%s) should be after #%d (%s)", + i - 1, list[i - 1].name, i, list[i].name); + } + } + for (j = i + 1; j < G_N_ELEMENTS (list); j++) { + nm_assert (list[i].perm != list[j].perm); + } + } + } + } +#endif + + if (nm) { + idx = nm_utils_array_find_binary_search (list, + sizeof (list[0]), + G_N_ELEMENTS (list), + &nm, + nm_strcmp_p_with_data, + NULL); + if (idx >= 0) + return list[idx].perm; + } + return NM_CLIENT_PERMISSION_NONE; +} + +NMClientPermissionResult +nm_permission_result_to_client (const char *nm) +{ + if (!nm) + return NM_CLIENT_PERMISSION_RESULT_UNKNOWN; + if (nm_streq (nm, "yes")) + return NM_CLIENT_PERMISSION_RESULT_YES; + if (nm_streq (nm, "no")) + return NM_CLIENT_PERMISSION_RESULT_NO; + if (nm_streq (nm, "auth")) + return NM_CLIENT_PERMISSION_RESULT_AUTH; + return NM_CLIENT_PERMISSION_RESULT_UNKNOWN; +} |