diff options
Diffstat (limited to 'src/settings/plugins/keyfile/writer.c')
| -rw-r--r-- | src/settings/plugins/keyfile/writer.c | 515 |
1 files changed, 156 insertions, 359 deletions
diff --git a/src/settings/plugins/keyfile/writer.c b/src/settings/plugins/keyfile/writer.c index 5f3ebe25..d342e3f7 100644 --- a/src/settings/plugins/keyfile/writer.c +++ b/src/settings/plugins/keyfile/writer.c @@ -19,7 +19,8 @@ * Copyright (C) 2008 - 2012 Red Hat, Inc. */ -#include <config.h> +#include "config.h" + #include <stdlib.h> #include <sys/stat.h> #include <unistd.h> @@ -40,7 +41,6 @@ #include <nm-utils.h> #include <string.h> #include <arpa/inet.h> -#include <netinet/ether.h> #include "nm-dbus-glib-types.h" #include "nm-glib-compat.h" @@ -98,75 +98,77 @@ write_array_of_uint (GKeyFile *file, } static void -ip4_dns_writer (GKeyFile *file, - const char *keyfile_dir, - const char *uuid, - NMSetting *setting, - const char *key, - const GValue *value) +dns_writer (GKeyFile *file, + const char *keyfile_dir, + const char *uuid, + NMSetting *setting, + const char *key, + const GValue *value) { - GArray *array; char **list; - int i, num = 0; - g_return_if_fail (G_VALUE_HOLDS (value, DBUS_TYPE_G_UINT_ARRAY)); - - array = (GArray *) g_value_get_boxed (value); - if (!array || !array->len) - return; - - list = g_new0 (char *, array->len + 1); - - for (i = 0; i < array->len; i++) { - char *buf = g_new (char, INET_ADDRSTRLEN); - guint32 addr; - - addr = g_array_index (array, guint32, i); - nm_utils_inet4_ntop (addr, buf); - list[num++] = buf; + list = g_value_get_boxed (value); + if (list && list[0]) { + nm_keyfile_plugin_kf_set_string_list (file, nm_setting_get_name (setting), key, + (const char **) list, g_strv_length (list)); } - - nm_keyfile_plugin_kf_set_string_list (file, nm_setting_get_name (setting), key, (const char **) list, num); - g_strfreev (list); } static void -write_ip4_values (GKeyFile *file, - const char *setting_name, - GPtrArray *array, - gboolean is_route) +write_ip_values (GKeyFile *file, + const char *setting_name, + GPtrArray *array, + const char *gateway, + gboolean is_route) { GString *output; - int i; - guint32 addr, gw, plen, metric; + int family, i; + const char *addr, *gw; + guint32 plen, metric; char key_name[30], *key_name_idx; if (!array->len) return; + family = !strcmp (setting_name, NM_SETTING_IP4_CONFIG_SETTING_NAME) ? AF_INET : AF_INET6; + strcpy (key_name, is_route ? "route" : "address"); key_name_idx = key_name + strlen (key_name); output = g_string_sized_new (2*INET_ADDRSTRLEN + 10); for (i = 0; i < array->len; i++) { - GArray *tuple = g_ptr_array_index (array, i); + if (is_route) { + NMIPRoute *route = array->pdata[i]; + + addr = nm_ip_route_get_dest (route); + plen = nm_ip_route_get_prefix (route); + gw = nm_ip_route_get_next_hop (route); + metric = MAX (0, nm_ip_route_get_metric (route)); + } else { + NMIPAddress *address = array->pdata[i]; - addr = g_array_index (tuple, guint32, 0); - plen = g_array_index (tuple, guint32, 1); - gw = g_array_index (tuple, guint32, 2); - metric = is_route ? g_array_index (tuple, guint32, 3) : 0; + addr = nm_ip_address_get_address (address); + plen = nm_ip_address_get_prefix (address); + gw = i == 0 ? gateway : NULL; + metric = 0; + } g_string_set_size (output, 0); - g_string_append_printf (output, "%s/%u", - nm_utils_inet4_ntop (addr, NULL), - (unsigned) plen); + g_string_append_printf (output, "%s/%u", addr, plen); if (metric || gw) { /* Older versions of the plugin do not support the form * "a.b.c.d/plen,,metric", so, we always have to write the - * gateway, even if it's 0.0.0.0. - * The current version support reading of the above form. */ - g_string_append_c (output, ','); - g_string_append (output, nm_utils_inet4_ntop (gw, NULL)); + * gateway, even if there isn't one. + * The current version supports reading of the above form. + */ + if (!gw) { + if (family == AF_INET) + gw = "0.0.0.0"; + else + gw = "::"; + } + + g_string_append_printf (output, ",%s", gw); if (metric) g_string_append_printf (output, ",%lu", (unsigned long) metric); } @@ -178,21 +180,20 @@ write_ip4_values (GKeyFile *file, } static void -ip4_addr_writer (GKeyFile *file, - const char *keyfile_dir, - const char *uuid, - NMSetting *setting, - const char *key, - const GValue *value) +addr_writer (GKeyFile *file, + const char *keyfile_dir, + const char *uuid, + NMSetting *setting, + const char *key, + const GValue *value) { GPtrArray *array; const char *setting_name = nm_setting_get_name (setting); - - g_return_if_fail (G_VALUE_HOLDS (value, DBUS_TYPE_G_ARRAY_OF_ARRAY_OF_UINT)); + const char *gateway = nm_setting_ip_config_get_gateway (NM_SETTING_IP_CONFIG (setting)); array = (GPtrArray *) g_value_get_boxed (value); if (array && array->len) - write_ip4_values (file, setting_name, array, FALSE); + write_ip_values (file, setting_name, array, gateway, FALSE); } static void @@ -207,223 +208,30 @@ ip4_addr_label_writer (GKeyFile *file, } static void -ip4_route_writer (GKeyFile *file, - const char *keyfile_dir, - const char *uuid, - NMSetting *setting, - const char *key, - const GValue *value) -{ - GPtrArray *array; - const char *setting_name = nm_setting_get_name (setting); - - g_return_if_fail (G_VALUE_HOLDS (value, DBUS_TYPE_G_ARRAY_OF_ARRAY_OF_UINT)); - - array = (GPtrArray *) g_value_get_boxed (value); - if (array && array->len) - write_ip4_values (file, setting_name, array, TRUE); -} - -static void -ip6_dns_writer (GKeyFile *file, +gateway_writer (GKeyFile *file, const char *keyfile_dir, const char *uuid, NMSetting *setting, const char *key, const GValue *value) { - GPtrArray *array; - GByteArray *byte_array; - char **list; - int i, num = 0; - - g_return_if_fail (G_VALUE_HOLDS (value, DBUS_TYPE_G_ARRAY_OF_ARRAY_OF_UCHAR)); - - array = (GPtrArray *) g_value_get_boxed (value); - if (!array || !array->len) - return; - - list = g_new0 (char *, array->len + 1); - - for (i = 0; i < array->len; i++) { - char *buf = g_new (char, INET6_ADDRSTRLEN); - - byte_array = g_ptr_array_index (array, i); - nm_utils_inet6_ntop ((const struct in6_addr *) byte_array->data, buf); - list[num++] = buf; - } - - nm_keyfile_plugin_kf_set_string_list (file, nm_setting_get_name (setting), key, (const char **) list, num); - g_strfreev (list); -} - -static void -ip6_array_to_addr (GValueArray *values, - guint32 idx, - char *buf, - struct in6_addr *out_addr) -{ - GByteArray *byte_array; - GValue *addr_val; - const struct in6_addr *addr; - - addr_val = g_value_array_get_nth (values, idx); - byte_array = g_value_get_boxed (addr_val); - addr = (const struct in6_addr *) byte_array->data; - - nm_utils_inet6_ntop (addr, buf); - - if (out_addr) - *out_addr = *addr; -} - -static char * -ip6_array_to_addr_prefix (GValueArray *values, gboolean force_write_gateway) -{ - GValue *prefix_val; - char *ret = NULL; - GString *ip6_str; - char buf[INET6_ADDRSTRLEN]; - struct in6_addr addr; - - /* address */ - ip6_array_to_addr (values, 0, buf, NULL); - - /* Enough space for the address, '/', and the prefix */ - ip6_str = g_string_sized_new ((INET6_ADDRSTRLEN * 2) + 5); - - /* prefix */ - g_string_append (ip6_str, buf); - prefix_val = g_value_array_get_nth (values, 1); - g_string_append_printf (ip6_str, "/%u", g_value_get_uint (prefix_val)); - - ip6_array_to_addr (values, 2, buf, &addr); - if (force_write_gateway || !IN6_IS_ADDR_UNSPECIFIED (&addr)) - g_string_append_printf (ip6_str, ",%s", buf); - - ret = ip6_str->str; - g_string_free (ip6_str, FALSE); - - return ret; -} - -static void -ip6_addr_writer (GKeyFile *file, - const char *keyfile_dir, - const char *uuid, - NMSetting *setting, - const char *key, - const GValue *value) -{ - GPtrArray *array; - const char *setting_name = nm_setting_get_name (setting); - int i, j; - - g_return_if_fail (G_VALUE_HOLDS (value, DBUS_TYPE_G_ARRAY_OF_IP6_ADDRESS)); - - array = (GPtrArray *) g_value_get_boxed (value); - if (!array || !array->len) - return; - - for (i = 0, j = 1; i < array->len; i++) { - GValueArray *values = g_ptr_array_index (array, i); - char *key_name, *ip6_addr; - - if (values->n_values != 3) { - nm_log_warn (LOGD_SETTINGS, "%s: error writing IP6 address %d (address array " - "length %d is not 3)", __func__, i, values->n_values); - continue; - } - - /* we allow omitting the gateway if it's :: */ - ip6_addr = ip6_array_to_addr_prefix (values, FALSE); - /* Write it out */ - key_name = g_strdup_printf ("address%d", j++); - nm_keyfile_plugin_kf_set_string (file, setting_name, key_name, ip6_addr); - g_free (key_name); - g_free (ip6_addr); - } + /* skip */ } static void -ip6_route_writer (GKeyFile *file, - const char *keyfile_dir, - const char *uuid, - NMSetting *setting, - const char *key, - const GValue *value) +route_writer (GKeyFile *file, + const char *keyfile_dir, + const char *uuid, + NMSetting *setting, + const char *key, + const GValue *value) { GPtrArray *array; const char *setting_name = nm_setting_get_name (setting); - GString *output; - int i, j; - - g_return_if_fail (G_VALUE_HOLDS (value, DBUS_TYPE_G_ARRAY_OF_IP6_ROUTE)); array = (GPtrArray *) g_value_get_boxed (value); - if (!array || !array->len) - return; - - for (i = 0, j = 1; i < array->len; i++) { - GValueArray *values = g_ptr_array_index (array, i); - char *key_name; - char *addr_str; - guint metric; - - output = g_string_new (""); - - /* Metric */ - value = g_value_array_get_nth (values, 3); - metric = g_value_get_uint (value); - - /* Address, prefix and next hop - * We allow omitting the gateway ::, if we also omit the metric - * and force writing of the gateway, if we add a non zero metric. - * The current version of the reader also supports the syntax - * "a:b:c::/plen,,metric" for a gateway ::. - * As older versions of the plugin, cannot read this form, - * we always write the gateway, whenever we also write the metric. - * But if possible, we omit them both (",::,0") or only the metric - * (",0"). - **/ - addr_str = ip6_array_to_addr_prefix (values, metric != 0); - g_string_append (output, addr_str); - g_free (addr_str); - - if (metric != 0) - g_string_append_printf (output, ",%u", metric); - - /* Write it out */ - key_name = g_strdup_printf ("route%d", j++); - nm_keyfile_plugin_kf_set_string (file, setting_name, key_name, output->str); - g_free (key_name); - - g_string_free (output, TRUE); - } -} - - -static void -mac_address_writer (GKeyFile *file, - const char *keyfile_dir, - const char *uuid, - NMSetting *setting, - const char *key, - const GValue *value) -{ - GByteArray *array; - const char *setting_name = nm_setting_get_name (setting); - char *mac; - - g_return_if_fail (G_VALUE_HOLDS (value, DBUS_TYPE_G_UCHAR_ARRAY)); - - array = (GByteArray *) g_value_get_boxed (value); - if (!array || !array->len) - return; - - mac = nm_utils_hwaddr_ntoa_len (array->data, array->len); - nm_keyfile_plugin_kf_set_string (file, setting_name, key, mac); - g_free (mac); + if (array && array->len) + write_ip_values (file, setting_name, array, NULL, TRUE); } static void @@ -472,24 +280,29 @@ ssid_writer (GKeyFile *file, const char *key, const GValue *value) { - GByteArray *array; + GBytes *bytes; + const guint8 *ssid_data; + gsize ssid_len; const char *setting_name = nm_setting_get_name (setting); gboolean new_format = TRUE; unsigned int semicolons = 0; int i, *tmp_array; char *ssid; - g_return_if_fail (G_VALUE_HOLDS (value, DBUS_TYPE_G_UCHAR_ARRAY)); + g_return_if_fail (G_VALUE_HOLDS (value, G_TYPE_BYTES)); - array = (GByteArray *) g_value_get_boxed (value); - if (!array || !array->len) + bytes = g_value_get_boxed (value); + if (!bytes) + return; + ssid_data = g_bytes_get_data (bytes, &ssid_len); + if (ssid_len == 0) return; /* Check whether each byte is printable. If not, we have to use an * integer list, otherwise we can just use a string. */ - for (i = 0; i < array->len; i++) { - char c = array->data[i] & 0xFF; + for (i = 0; i < ssid_len; i++) { + char c = ssid_data[i] & 0xFF; if (!g_ascii_isprint (c)) { new_format = FALSE; break; @@ -499,26 +312,26 @@ ssid_writer (GKeyFile *file, } if (new_format) { - ssid = g_malloc0 (array->len + semicolons + 1); + ssid = g_malloc0 (ssid_len + semicolons + 1); if (semicolons == 0) - memcpy (ssid, array->data, array->len); + memcpy (ssid, ssid_data, ssid_len); else { /* Escape semicolons with backslashes to make strings * containing ';', such as '16;17;' unambiguous */ int j = 0; - for (i = 0; i < array->len; i++) { - if (array->data[i] == ';') + for (i = 0; i < ssid_len; i++) { + if (ssid_data[i] == ';') ssid[j++] = '\\'; - ssid[j++] = array->data[i]; + ssid[j++] = ssid_data[i]; } } nm_keyfile_plugin_kf_set_string (file, setting_name, key, ssid); g_free (ssid); } else { - tmp_array = g_new (gint, array->len); - for (i = 0; i < array->len; i++) - tmp_array[i] = (int) array->data[i]; - nm_keyfile_plugin_kf_set_integer_list (file, setting_name, key, tmp_array, array->len); + tmp_array = g_new (gint, ssid_len); + for (i = 0; i < ssid_len; i++) + tmp_array[i] = (int) ssid_data[i]; + nm_keyfile_plugin_kf_set_integer_list (file, setting_name, key, tmp_array, ssid_len); g_free (tmp_array); } } @@ -555,7 +368,7 @@ typedef struct ObjectType { NMSetting8021xCKScheme (*scheme_func) (NMSetting8021x *setting); NMSetting8021xCKFormat (*format_func) (NMSetting8021x *setting); const char * (*path_func) (NMSetting8021x *setting); - const GByteArray * (*blob_func) (NMSetting8021x *setting); + GBytes * (*blob_func) (NMSetting8021x *setting); } ObjectType; static const ObjectType objtypes[10] = { @@ -612,7 +425,8 @@ static const ObjectType objtypes[10] = { static gboolean write_cert_key_file (const char *path, - const GByteArray *data, + const guint8 *data, + gsize data_len, GError **error) { char *tmppath; @@ -627,7 +441,7 @@ write_cert_key_file (const char *path, errno = 0; fd = mkstemp (tmppath); if (fd < 0) { - g_set_error (error, KEYFILE_PLUGIN_ERROR, 0, + g_set_error (error, NM_SETTINGS_ERROR, NM_SETTINGS_ERROR_FAILED, "Could not create temporary file for '%s': %d", path, errno); goto out; @@ -638,18 +452,18 @@ write_cert_key_file (const char *path, if (fchmod (fd, S_IRUSR | S_IWUSR) != 0) { close (fd); unlink (tmppath); - g_set_error (error, KEYFILE_PLUGIN_ERROR, 0, + g_set_error (error, NM_SETTINGS_ERROR, NM_SETTINGS_ERROR_FAILED, "Could not set permissions for temporary file '%s': %d", path, errno); goto out; } errno = 0; - written = write (fd, data->data, data->len); - if (written != data->len) { + written = write (fd, data, data_len); + if (written != data_len) { close (fd); unlink (tmppath); - g_set_error (error, KEYFILE_PLUGIN_ERROR, 0, + g_set_error (error, NM_SETTINGS_ERROR, NM_SETTINGS_ERROR_FAILED, "Could not write temporary file for '%s': %d", path, errno); goto out; @@ -662,7 +476,7 @@ write_cert_key_file (const char *path, success = TRUE; else { unlink (tmppath); - g_set_error (error, KEYFILE_PLUGIN_ERROR, 0, + g_set_error (error, NM_SETTINGS_ERROR, NM_SETTINGS_ERROR_FAILED, "Could not rename temporary file to '%s': %d", path, errno); } @@ -714,13 +528,16 @@ cert_writer (GKeyFile *file, nm_keyfile_plugin_kf_set_string (file, setting_name, key, path); } else if (scheme == NM_SETTING_802_1X_CK_SCHEME_BLOB) { - const GByteArray *blob; + GBytes *blob; + const guint8 *blob_data; + gsize blob_len; gboolean success; GError *error = NULL; char *new_path; blob = objtype->blob_func (NM_SETTING_802_1X (setting)); g_assert (blob); + blob_data = g_bytes_get_data (blob, &blob_len); if (objtype->format_func) { /* Get the extension for a private key */ @@ -729,7 +546,7 @@ cert_writer (GKeyFile *file, ext = "p12"; } else { /* DER or PEM format certificate? */ - if (blob->len > 2 && blob->data[0] == 0x30 && blob->data[1] == 0x82) + if (blob_len > 2 && blob_data[0] == 0x30 && blob_data[1] == 0x82) ext = "der"; } @@ -739,7 +556,7 @@ cert_writer (GKeyFile *file, new_path = g_strdup_printf ("%s/%s-%s.%s", keyfile_dir, uuid, objtype->suffix, ext); g_assert (new_path); - success = write_cert_key_file (new_path, blob, &error); + success = write_cert_key_file (new_path, blob_data, blob_len, &error); if (success) { /* Write the path value to the keyfile */ nm_keyfile_plugin_kf_set_string (file, setting_name, key, new_path); @@ -774,54 +591,33 @@ static KeyWriter key_writers[] = { { NM_SETTING_CONNECTION_SETTING_NAME, NM_SETTING_CONNECTION_TYPE, setting_alias_writer }, - { NM_SETTING_BRIDGE_SETTING_NAME, - NM_SETTING_BRIDGE_MAC_ADDRESS, - mac_address_writer }, { NM_SETTING_IP4_CONFIG_SETTING_NAME, - NM_SETTING_IP4_CONFIG_ADDRESSES, - ip4_addr_writer }, + NM_SETTING_IP_CONFIG_ADDRESSES, + addr_writer }, { NM_SETTING_IP4_CONFIG_SETTING_NAME, "address-labels", ip4_addr_label_writer }, { NM_SETTING_IP6_CONFIG_SETTING_NAME, - NM_SETTING_IP6_CONFIG_ADDRESSES, - ip6_addr_writer }, + NM_SETTING_IP_CONFIG_ADDRESSES, + addr_writer }, { NM_SETTING_IP4_CONFIG_SETTING_NAME, - NM_SETTING_IP4_CONFIG_ROUTES, - ip4_route_writer }, + NM_SETTING_IP_CONFIG_GATEWAY, + gateway_writer }, { NM_SETTING_IP6_CONFIG_SETTING_NAME, - NM_SETTING_IP6_CONFIG_ROUTES, - ip6_route_writer }, + NM_SETTING_IP_CONFIG_GATEWAY, + gateway_writer }, { NM_SETTING_IP4_CONFIG_SETTING_NAME, - NM_SETTING_IP4_CONFIG_DNS, - ip4_dns_writer }, + NM_SETTING_IP_CONFIG_ROUTES, + route_writer }, { NM_SETTING_IP6_CONFIG_SETTING_NAME, - NM_SETTING_IP6_CONFIG_DNS, - ip6_dns_writer }, - { NM_SETTING_WIRED_SETTING_NAME, - NM_SETTING_WIRED_MAC_ADDRESS, - mac_address_writer }, - { NM_SETTING_WIRED_SETTING_NAME, - NM_SETTING_WIRED_CLONED_MAC_ADDRESS, - mac_address_writer }, - { NM_SETTING_WIRELESS_SETTING_NAME, - NM_SETTING_WIRELESS_MAC_ADDRESS, - mac_address_writer }, - { NM_SETTING_WIRELESS_SETTING_NAME, - NM_SETTING_WIRELESS_CLONED_MAC_ADDRESS, - mac_address_writer }, - { NM_SETTING_WIRELESS_SETTING_NAME, - NM_SETTING_WIRELESS_BSSID, - mac_address_writer }, - { NM_SETTING_BLUETOOTH_SETTING_NAME, - NM_SETTING_BLUETOOTH_BDADDR, - mac_address_writer }, - { NM_SETTING_INFINIBAND_SETTING_NAME, - NM_SETTING_INFINIBAND_MAC_ADDRESS, - mac_address_writer }, - { NM_SETTING_WIMAX_SETTING_NAME, - NM_SETTING_WIMAX_MAC_ADDRESS, - mac_address_writer }, + NM_SETTING_IP_CONFIG_ROUTES, + route_writer }, + { NM_SETTING_IP4_CONFIG_SETTING_NAME, + NM_SETTING_IP_CONFIG_DNS, + dns_writer }, + { NM_SETTING_IP6_CONFIG_SETTING_NAME, + NM_SETTING_IP_CONFIG_DNS, + dns_writer }, { NM_SETTING_WIRELESS_SETTING_NAME, NM_SETTING_WIRELESS_SSID, ssid_writer }, @@ -896,7 +692,8 @@ write_setting_value (NMSetting *setting, if (pspec && (pspec->flags & NM_SETTING_PARAM_SECRET) && !NM_IS_SETTING_VPN (setting)) { NMSettingSecretFlags secret_flags = NM_SETTING_SECRET_FLAG_NONE; - nm_setting_get_secret_flags (setting, key, &secret_flags, NULL); + if (!nm_setting_get_secret_flags (setting, key, &secret_flags, NULL)) + g_assert_not_reached (); if (secret_flags != NM_SETTING_SECRET_FLAG_NONE) return; } @@ -926,49 +723,53 @@ write_setting_value (NMSetting *setting, numstr = g_strdup_printf ("%" G_GUINT64_FORMAT, g_value_get_uint64 (value)); nm_keyfile_plugin_kf_set_value (info->keyfile, setting_name, key, numstr); g_free (numstr); + } else if (type == G_TYPE_INT64) { + char *numstr; + + numstr = g_strdup_printf ("%" G_GINT64_FORMAT, g_value_get_int64 (value)); + nm_keyfile_plugin_kf_set_value (info->keyfile, setting_name, key, numstr); + g_free (numstr); } else if (type == G_TYPE_BOOLEAN) { nm_keyfile_plugin_kf_set_boolean (info->keyfile, setting_name, key, g_value_get_boolean (value)); } else if (type == G_TYPE_CHAR) { nm_keyfile_plugin_kf_set_integer (info->keyfile, setting_name, key, (int) g_value_get_schar (value)); - } else if (type == DBUS_TYPE_G_UCHAR_ARRAY) { - GByteArray *array; + } else if (type == G_TYPE_BYTES) { + GBytes *bytes; + const guint8 *data; + gsize len = 0; - array = (GByteArray *) g_value_get_boxed (value); - if (array && array->len > 0) { + bytes = g_value_get_boxed (value); + data = bytes ? g_bytes_get_data (bytes, &len) : NULL; + + if (data != NULL && len > 0) { int *tmp_array; int i; - tmp_array = g_new (gint, array->len); - for (i = 0; i < array->len; i++) - tmp_array[i] = (int) array->data[i]; + tmp_array = g_new (gint, len); + for (i = 0; i < len; i++) + tmp_array[i] = (int) data[i]; - nm_keyfile_plugin_kf_set_integer_list (info->keyfile, setting_name, key, tmp_array, array->len); + nm_keyfile_plugin_kf_set_integer_list (info->keyfile, setting_name, key, tmp_array, len); g_free (tmp_array); } - } else if (type == DBUS_TYPE_G_LIST_OF_STRING) { - GSList *list; - GSList *iter; - - list = (GSList *) g_value_get_boxed (value); - if (list) { - char **array; - int i = 0; + } else if (type == G_TYPE_STRV) { + char **array; - array = g_new (char *, g_slist_length (list)); - for (iter = list; iter; iter = iter->next) - array[i++] = iter->data; - - nm_keyfile_plugin_kf_set_string_list (info->keyfile, setting_name, key, (const gchar **const) array, i); - g_free (array); - } - } else if (type == DBUS_TYPE_G_MAP_OF_STRING) { + array = (char **) g_value_get_boxed (value); + nm_keyfile_plugin_kf_set_string_list (info->keyfile, setting_name, key, (const gchar **const) array, g_strv_length (array)); + } else if (type == G_TYPE_HASH_TABLE) { write_hash_of_string (info->keyfile, setting, key, value); - } else if (type == DBUS_TYPE_G_UINT_ARRAY) { + } else if (type == G_TYPE_ARRAY) { if (!write_array_of_uint (info->keyfile, setting, key, value)) { nm_log_warn (LOGD_SETTINGS, "Unhandled setting property type (write) '%s/%s' : '%s'", setting_name, key, g_type_name (type)); } - } else { + } else if (G_VALUE_HOLDS_FLAGS (value)) { + /* Flags are guint but GKeyFile has no uint reader, just uint64 */ + nm_keyfile_plugin_kf_set_uint64 (info->keyfile, setting_name, key, (guint64) g_value_get_flags (value)); + } else if (G_VALUE_HOLDS_ENUM (value)) + nm_keyfile_plugin_kf_set_integer (info->keyfile, setting_name, key, (gint) g_value_get_enum (value)); + else { nm_log_warn (LOGD_SETTINGS, "Unhandled setting property type (write) '%s/%s' : '%s'", setting_name, key, g_type_name (type)); } @@ -1021,11 +822,7 @@ _internal_write_connection (NMConnection *connection, } id = nm_connection_get_id (connection); - if (!id) { - g_set_error (error, KEYFILE_PLUGIN_ERROR, 0, - "%s.%d: connection had no ID", __FILE__, __LINE__); - return FALSE; - } + g_assert (id && *id); info.keyfile = key_file = g_key_file_new (); info.keyfile_dir = keyfile_dir; @@ -1065,7 +862,7 @@ _internal_write_connection (NMConnection *connection, * is edited to contain the same ID as the other one. * Give up. */ - g_set_error (error, KEYFILE_PLUGIN_ERROR, 0, + g_set_error (error, NM_SETTINGS_ERROR, NM_SETTINGS_ERROR_FAILED, "%s.%d: could not find suitable keyfile file name (%s already used)", __FILE__, __LINE__, path); g_free (path); @@ -1082,7 +879,7 @@ _internal_write_connection (NMConnection *connection, g_file_set_contents (path, data, len, &local_err); if (local_err) { - g_set_error (error, KEYFILE_PLUGIN_ERROR, 0, + g_set_error (error, NM_SETTINGS_ERROR, NM_SETTINGS_ERROR_FAILED, "%s.%d: error writing to file '%s': %s", __FILE__, __LINE__, path, local_err->message); g_error_free (local_err); @@ -1091,13 +888,13 @@ _internal_write_connection (NMConnection *connection, } if (chown (path, owner_uid, owner_grp) < 0) { - g_set_error (error, KEYFILE_PLUGIN_ERROR, 0, + g_set_error (error, NM_SETTINGS_ERROR, NM_SETTINGS_ERROR_FAILED, "%s.%d: error chowning '%s': %d", __FILE__, __LINE__, path, errno); unlink (path); } else { if (chmod (path, S_IRUSR | S_IWUSR) < 0) { - g_set_error (error, KEYFILE_PLUGIN_ERROR, 0, + g_set_error (error, NM_SETTINGS_ERROR, NM_SETTINGS_ERROR_FAILED, "%s.%d: error setting permissions on '%s': %d", __FILE__, __LINE__, path, errno); unlink (path); |