NetworkManager r3821 - in trunk: . system-settings/plugins/ifcfg-suse system-settings/plugins/keyfile system-settings/src



Author: tambeti
Date: Wed Jul 16 07:37:10 2008
New Revision: 3821
URL: http://svn.gnome.org/viewvc/NetworkManager?rev=3821&view=rev

Log:
2008-07-16  Tambet Ingo  <tambet gmail com>

	* system-settings/src/nm-system-config-interface.c
	(nm_system_config_interface_supports_add): Implement.
	(nm_system_config_interface_add_connection): Return a boolean to notify
	of errors.

	* system-settings/src/nm-polkit-helpers.c:
	* system-settings/src/nm-polkit-helpers.h: Move error declarations to
	a separate file.

	* system-settings/src/dbus-settings.c (impl_settings_add_connection):
	Return an error when none of the plugins support add or if addition
	failed for some reason.

	* system-settings/src/nm-system-config-error.h:
	* system-settings/src/nm-system-config-error.c: New files, mostly moved
	here from nm-polkit-helpers.[ch].

	* system-settings/src/Makefile.am: Build new files.

	* system-settings/plugins/keyfile/reader.c
	(read_array_of_array_of_uint): Make it more general so that it would
	work for routes as well.

	* system-settings/plugins/keyfile/writer.c
	(write_array_of_array_of_uint): Ditto.
	Fix the netmask/prefix writing.

	* system-settings/plugins/keyfile/plugin.c (add_connection): Return
	boolean to notify errors.

	* system-settings/plugins/ifcfg-suse/nm-suse-connection.c (update):
	Return more specific error.
	(delete): Ditto.


Added:
   trunk/system-settings/src/nm-system-config-error.c
   trunk/system-settings/src/nm-system-config-error.h
Modified:
   trunk/ChangeLog
   trunk/system-settings/plugins/ifcfg-suse/nm-suse-connection.c
   trunk/system-settings/plugins/keyfile/plugin.c
   trunk/system-settings/plugins/keyfile/reader.c
   trunk/system-settings/plugins/keyfile/writer.c
   trunk/system-settings/src/Makefile.am
   trunk/system-settings/src/dbus-settings.c
   trunk/system-settings/src/nm-polkit-helpers.c
   trunk/system-settings/src/nm-polkit-helpers.h
   trunk/system-settings/src/nm-system-config-interface.c
   trunk/system-settings/src/nm-system-config-interface.h

Modified: trunk/system-settings/plugins/ifcfg-suse/nm-suse-connection.c
==============================================================================
--- trunk/system-settings/plugins/ifcfg-suse/nm-suse-connection.c	(original)
+++ trunk/system-settings/plugins/ifcfg-suse/nm-suse-connection.c	Wed Jul 16 07:37:10 2008
@@ -6,7 +6,7 @@
 #include <NetworkManager.h>
 #include "nm-suse-connection.h"
 #include "parser.h"
-#include "nm-polkit-helpers.h"
+#include "nm-system-config-error.h"
 
 G_DEFINE_TYPE (NMSuseConnection, nm_suse_connection, NM_TYPE_SYSCONFIG_CONNECTION)
 
@@ -111,7 +111,8 @@
 	   GHashTable *new_settings,
 	   GError **err)
 {	
-	g_set_error (err, NM_SYSCONFIG_SETTINGS_ERROR, NM_SYSCONFIG_SETTINGS_ERROR_GENERAL,
+	g_set_error (err, NM_SYSCONFIG_SETTINGS_ERROR,
+			   NM_SYSCONFIG_SETTINGS_ERROR_UPDATE_NOT_SUPPORTED,
 			   "%s", "Please use YaST to change this connection.");
 
 	return FALSE;
@@ -120,7 +121,8 @@
 static gboolean
 delete (NMExportedConnection *exported, GError **err)
 {
-	g_set_error (err, NM_SYSCONFIG_SETTINGS_ERROR, NM_SYSCONFIG_SETTINGS_ERROR_GENERAL,
+	g_set_error (err, NM_SYSCONFIG_SETTINGS_ERROR,
+			   NM_SYSCONFIG_SETTINGS_ERROR_DELETE_NOT_SUPPORTED,
 			   "%s", "Please use YaST to remove this connection.");
 
 	return FALSE;

Modified: trunk/system-settings/plugins/keyfile/plugin.c
==============================================================================
--- trunk/system-settings/plugins/keyfile/plugin.c	(original)
+++ trunk/system-settings/plugins/keyfile/plugin.c	Wed Jul 16 07:37:10 2008
@@ -181,10 +181,10 @@
 	return connections;
 }
 
-static void
+static gboolean
 add_connection (NMSystemConfigInterface *config, NMConnection *connection)
 {
-	write_connection (connection);
+	return write_connection (connection);
 }
 
 /* GObject */

Modified: trunk/system-settings/plugins/keyfile/reader.c
==============================================================================
--- trunk/system-settings/plugins/keyfile/reader.c	(original)
+++ trunk/system-settings/plugins/keyfile/reader.c	Wed Jul 16 07:37:10 2008
@@ -67,19 +67,14 @@
 	g_array_free ((GArray *) data, TRUE);
 }
 
-static gboolean
-read_array_of_array_of_uint (GKeyFile *file,
-                             NMSetting *setting,
-                             const char *key)
+static GPtrArray *
+read_addresses (GKeyFile *file,
+			 const char *setting_name,
+			 const char *key)
 {
 	GPtrArray *addresses;
 	int i = 0;
 
-	/* Only handle IPv4 addresses for now */
-	if (   !NM_IS_SETTING_IP4_CONFIG (setting)
-	    || strcmp (key, NM_SETTING_IP4_CONFIG_ADDRESSES))
-	    return FALSE;
-
 	addresses = g_ptr_array_sized_new (3);
 
 	/* Look for individual addresses */
@@ -92,8 +87,8 @@
 		guint32 empty = 0;
 		int j;
 
-		key_name = g_strdup_printf ("address%d", i);
-		tmp = g_key_file_get_string_list (file, setting->name, key_name, &length, NULL);
+		key_name = g_strdup_printf ("%s%d", key, i);
+		tmp = g_key_file_get_string_list (file, setting_name, key_name, &length, NULL);
 		g_free (key_name);
 
 		if (!tmp || !length)
@@ -145,10 +140,39 @@
 		g_strfreev (tmp);
 	}
 
-	g_object_set (setting, key, addresses, NULL);
+	if (addresses->len < 1) {
+		g_ptr_array_free (addresses, TRUE);
+		addresses = NULL;
+	}
+
+	return addresses;
+}
+
+static gboolean
+read_array_of_array_of_uint (GKeyFile *file,
+                             NMSetting *setting,
+                             const char *key)
+{
+	GPtrArray *addresses;
+
+	/* Only handle IPv4 addresses and routes for now */
+	if (   !NM_IS_SETTING_IP4_CONFIG (setting) ||
+		  (strcmp (key, NM_SETTING_IP4_CONFIG_ADDRESSES) &&
+		   strcmp (key, NM_SETTING_IP4_CONFIG_ROUTES)))
+	    return FALSE;
+
+	addresses = read_addresses (file, setting->name, key);
+
+	/* Work around for previous syntax */
+	if (!addresses && !strcmp (key, NM_SETTING_IP4_CONFIG_ADDRESSES))
+		addresses = read_addresses (file, setting->name, "address");
+
+	if (addresses) {
+		g_object_set (setting, key, addresses, NULL);
+		g_ptr_array_foreach (addresses, free_one_address, NULL);
+		g_ptr_array_free (addresses, TRUE);
+	}
 
-	g_ptr_array_foreach (addresses, free_one_address, NULL);
-	g_ptr_array_free (addresses, TRUE);
 	return TRUE;
 }
 

Modified: trunk/system-settings/plugins/keyfile/writer.c
==============================================================================
--- trunk/system-settings/plugins/keyfile/writer.c	(original)
+++ trunk/system-settings/plugins/keyfile/writer.c	Wed Jul 16 07:37:10 2008
@@ -71,9 +71,10 @@
 	GPtrArray *array;
 	int i, j;
 
-	/* Only handle IPv4 addresses for now */
-	if (   !NM_IS_SETTING_IP4_CONFIG (setting)
-	    || strcmp (key, NM_SETTING_IP4_CONFIG_ADDRESSES))
+	/* Only handle IPv4 addresses and routes for now */
+	if (   !NM_IS_SETTING_IP4_CONFIG (setting) ||
+		  (strcmp (key, NM_SETTING_IP4_CONFIG_ADDRESSES) &&
+		   strcmp (key, NM_SETTING_IP4_CONFIG_ROUTES)))
 	    return FALSE;
 
 	array = (GPtrArray *) g_value_get_boxed (value);
@@ -87,6 +88,7 @@
 		char *list[3] = { NULL, NULL, NULL };
 		char *key_name;
 
+		/* Address */
 		addr.s_addr = g_array_index (tuple, guint32, 0);
 		if (!inet_ntop (AF_INET, &addr, buf, sizeof (buf))) {
 			nm_warning ("%s: error converting IP4 address 0x%X",
@@ -96,15 +98,10 @@
 			list[0] = g_strdup (buf);
 		}
 
-		addr.s_addr = g_array_index (tuple, guint32, 1);
-		if (!inet_ntop (AF_INET, &addr, buf, sizeof (buf))) {
-			nm_warning ("%s: error converting IP4 address 0x%X",
-			            __func__, ntohl (addr.s_addr));
-			list[1] = NULL;
-		} else {
-			list[1] = g_strdup (buf);
-		}
+		/* Prefix */
+		list[1] = g_strdup_printf ("%d", g_array_index (tuple, guint32, 1));
 
+		/* Gateway */
 		addr.s_addr = g_array_index (tuple, guint32, 2);
 		if (addr.s_addr) {
 			if (!inet_ntop (AF_INET, &addr, buf, sizeof (buf))) {
@@ -116,7 +113,7 @@
 			}
 		}
 
-		key_name = g_strdup_printf ("address%d", j + 1);
+		key_name = g_strdup_printf ("%s%d", key, j + 1);
 		g_key_file_set_string_list (file, setting->name, key_name, (const char **) list, list[2] ? 3 : 2);
 		g_free (key_name);
 

Modified: trunk/system-settings/src/Makefile.am
==============================================================================
--- trunk/system-settings/src/Makefile.am	(original)
+++ trunk/system-settings/src/Makefile.am	Wed Jul 16 07:37:10 2008
@@ -15,6 +15,8 @@
 	main.c \
 	nm-polkit-helpers.c \
 	nm-polkit-helpers.h \
+	nm-system-config-error.c \
+	nm-system-config-error.h \
 	nm-system-config-interface.c \
 	nm-system-config-interface.h \
 	nm-system-config-hal-manager.c \

Modified: trunk/system-settings/src/dbus-settings.c
==============================================================================
--- trunk/system-settings/src/dbus-settings.c	(original)
+++ trunk/system-settings/src/dbus-settings.c	Wed Jul 16 07:37:10 2008
@@ -30,6 +30,7 @@
 #include "nm-dbus-glib-types.h"
 #include "dbus-settings.h"
 #include "nm-polkit-helpers.h"
+#include "nm-system-config-error.h"
 #include "nm-utils.h"
 
 static gboolean
@@ -431,36 +432,45 @@
 {
 	NMSysconfigSettingsPrivate *priv = NM_SYSCONFIG_SETTINGS_GET_PRIVATE (self);
 	NMConnection *connection;
+	GSList *iter;
 	GError *err = NULL, *cnfh_error = NULL;
+	gboolean success;
 
-	if (!check_polkit_privileges (priv->g_connection, priv->pol_ctx, context, &err)) {
-		dbus_g_method_return_error (context, err);
-		g_error_free (err);
-		return FALSE;
+	/* Does any of the plugins support adding? */
+	success = FALSE;
+	for (iter = priv->plugins; iter && success == FALSE; iter = iter->next) 
+		success = nm_system_config_interface_supports_add (NM_SYSTEM_CONFIG_INTERFACE (iter->data));
+
+	if (!success) {
+		err = g_error_new (NM_SYSCONFIG_SETTINGS_ERROR,
+					    NM_SYSCONFIG_SETTINGS_ERROR_ADD_NOT_SUPPORTED,
+					    "%s", "None of the registered plugins support add.");
+		goto out;
 	}
 
+	if (!check_polkit_privileges (priv->g_connection, priv->pol_ctx, context, &err))
+		goto out;
+
 	connection = nm_connection_new_from_hash (hash, &cnfh_error);
 	if (connection) {
-		GSList *iter;
-
 		/* Here's how it works:
 		   1) plugin writes a connection.
 		   2) plugin notices that a new connection is available for reading.
 		   3) plugin reads the new connection (the one it wrote in 1) and emits 'connection-added' signal.
 		   4) NMSysconfigSettings receives the signal and adds it to it's connection list.
-
-		   This does not work if none of the plugins is able to write, but that is sort of by design - 
-		   if the connection is not saved, it won't be available after reboot and that would be very
-		   inconsistent. Perhaps we should fail this call here as well, but with multiple plugins,
-		   it's not very clear which failures we can ignore and which ones we can't.
 		*/
 
-		for (iter = priv->plugins; iter; iter = iter->next)
-			nm_system_config_interface_add_connection (NM_SYSTEM_CONFIG_INTERFACE (iter->data), connection);
+		success = FALSE;
+		for (iter = priv->plugins; iter && success == FALSE; iter = iter->next)
+			success = nm_system_config_interface_add_connection (NM_SYSTEM_CONFIG_INTERFACE (iter->data),
+													   connection);
 
 		g_object_unref (connection);
-		dbus_g_method_return (context);
-		return TRUE;
+
+		if (!success)
+			err = g_error_new (NM_SYSCONFIG_SETTINGS_ERROR,
+						    NM_SYSCONFIG_SETTINGS_ERROR_ADD_FAILED,
+						    "%s", "Saving connection failed.");
 	} else {
 		/* Invalid connection hash */
 		err = g_error_new (NM_SYSCONFIG_SETTINGS_ERROR,
@@ -469,8 +479,15 @@
 					    g_type_name (nm_connection_lookup_setting_type_by_quark (cnfh_error->domain)),
 					    cnfh_error->message, cnfh_error->code);
 		g_error_free (cnfh_error);
+	}
+
+ out:
+	if (err) {
 		dbus_g_method_return_error (context, err);
 		g_error_free (err);
 		return FALSE;
+	} else {
+		dbus_g_method_return (context);
+		return TRUE;
 	}
 }

Modified: trunk/system-settings/src/nm-polkit-helpers.c
==============================================================================
--- trunk/system-settings/src/nm-polkit-helpers.c	(original)
+++ trunk/system-settings/src/nm-polkit-helpers.c	Wed Jul 16 07:37:10 2008
@@ -1,39 +1,8 @@
 /* -*- Mode: C; tab-width: 5; indent-tabs-mode: t; c-basic-offset: 5 -*- */
 
-#include "nm-polkit-helpers.h"
 #include <nm-dbus-settings.h>
-
-GQuark
-nm_sysconfig_settings_error_quark (void)
-{
-	static GQuark ret = 0;
-
-	if (ret == 0)
-		ret = g_quark_from_static_string ("nm_sysconfig_settings_error");
-
-	return ret;
-}
-
-#define ENUM_ENTRY(NAME, DESC) { NAME, "" #NAME "", DESC }
-
-GType
-nm_sysconfig_settings_error_get_type (void)
-{
-	static GType etype = 0;
-
-	if (etype == 0) {
-		static const GEnumValue values[] = {
-			ENUM_ENTRY (NM_SYSCONFIG_SETTINGS_ERROR_GENERAL, "GeneralError"),
-			ENUM_ENTRY (NM_SYSCONFIG_SETTINGS_ERROR_NOT_PRIVILEGED, "NotPrivileged"),
-			ENUM_ENTRY (NM_SYSCONFIG_SETTINGS_ERROR_INVALID_CONNECTION, "InvalidConnection"),
-			{ 0, 0, 0 }
-		};
-
-		etype = g_enum_register_static ("NMSysconfigSettingsError", values);
-	}
-
-	return etype;
-}
+#include "nm-polkit-helpers.h"
+#include "nm-system-config-error.h"
 
 static gboolean
 pk_io_watch_have_data (GIOChannel *channel, GIOCondition condition, gpointer user_data)

Modified: trunk/system-settings/src/nm-polkit-helpers.h
==============================================================================
--- trunk/system-settings/src/nm-polkit-helpers.h	(original)
+++ trunk/system-settings/src/nm-polkit-helpers.h	Wed Jul 16 07:37:10 2008
@@ -9,18 +9,6 @@
 
 #define NM_SYSCONFIG_POLICY_ACTION "org.freedesktop.network-manager-settings.system.modify"
 
-enum {
-	NM_SYSCONFIG_SETTINGS_ERROR_GENERAL = 0,
-	NM_SYSCONFIG_SETTINGS_ERROR_NOT_PRIVILEGED,
-	NM_SYSCONFIG_SETTINGS_ERROR_INVALID_CONNECTION
-};
-
-#define NM_SYSCONFIG_SETTINGS_ERROR (nm_sysconfig_settings_error_quark ())
-#define NM_TYPE_SYSCONFIG_SETTINGS_ERROR (nm_sysconfig_settings_error_get_type ())
-
-GQuark nm_sysconfig_settings_error_quark    (void);
-GType  nm_sysconfig_settings_error_get_type (void);
-
 PolKitContext *create_polkit_context   (void);
 gboolean       check_polkit_privileges (DBusGConnection *dbus_connection,
 								PolKitContext *pol_ctx,

Added: trunk/system-settings/src/nm-system-config-error.c
==============================================================================
--- (empty file)
+++ trunk/system-settings/src/nm-system-config-error.c	Wed Jul 16 07:37:10 2008
@@ -0,0 +1,39 @@
+/* -*- Mode: C; tab-width: 5; indent-tabs-mode: t; c-basic-offset: 5 -*- */
+
+#include "nm-system-config-error.h"
+
+GQuark
+nm_sysconfig_settings_error_quark (void)
+{
+	static GQuark ret = 0;
+
+	if (ret == 0)
+		ret = g_quark_from_static_string ("nm_sysconfig_settings_error");
+
+	return ret;
+}
+
+#define ENUM_ENTRY(NAME, DESC) { NAME, "" #NAME "", DESC }
+
+GType
+nm_sysconfig_settings_error_get_type (void)
+{
+	static GType etype = 0;
+
+	if (etype == 0) {
+		static const GEnumValue values[] = {
+			ENUM_ENTRY (NM_SYSCONFIG_SETTINGS_ERROR_GENERAL, "GeneralError"),
+			ENUM_ENTRY (NM_SYSCONFIG_SETTINGS_ERROR_NOT_PRIVILEGED, "NotPrivileged"),
+			ENUM_ENTRY (NM_SYSCONFIG_SETTINGS_ERROR_INVALID_CONNECTION, "InvalidConnection"),
+			ENUM_ENTRY (NM_SYSCONFIG_SETTINGS_ERROR_ADD_NOT_SUPPORTED, "AddNotSupported"),
+			ENUM_ENTRY (NM_SYSCONFIG_SETTINGS_ERROR_UPDATE_NOT_SUPPORTED, "UpdateNotSupported"),
+			ENUM_ENTRY (NM_SYSCONFIG_SETTINGS_ERROR_DELETE_NOT_SUPPORTED, "DeleteNotSupported"),
+			ENUM_ENTRY (NM_SYSCONFIG_SETTINGS_ERROR_ADD_FAILED, "AddFailed"),
+			{ 0, 0, 0 }
+		};
+
+		etype = g_enum_register_static ("NMSysconfigSettingsError", values);
+	}
+
+	return etype;
+}

Added: trunk/system-settings/src/nm-system-config-error.h
==============================================================================
--- (empty file)
+++ trunk/system-settings/src/nm-system-config-error.h	Wed Jul 16 07:37:10 2008
@@ -0,0 +1,25 @@
+/* -*- Mode: C; tab-width: 5; indent-tabs-mode: t; c-basic-offset: 5 -*- */
+
+#ifndef NM_SYSTEM_CONFIG_ERROR_H
+#define NM_SYSTEM_CONFIG_ERROR_H
+
+#include <glib/gtypes.h>
+#include <glib-object.h>
+
+enum {
+	NM_SYSCONFIG_SETTINGS_ERROR_GENERAL = 0,
+	NM_SYSCONFIG_SETTINGS_ERROR_NOT_PRIVILEGED,
+	NM_SYSCONFIG_SETTINGS_ERROR_INVALID_CONNECTION,
+	NM_SYSCONFIG_SETTINGS_ERROR_ADD_NOT_SUPPORTED,
+	NM_SYSCONFIG_SETTINGS_ERROR_UPDATE_NOT_SUPPORTED,
+	NM_SYSCONFIG_SETTINGS_ERROR_DELETE_NOT_SUPPORTED,
+	NM_SYSCONFIG_SETTINGS_ERROR_ADD_FAILED
+};
+
+#define NM_SYSCONFIG_SETTINGS_ERROR (nm_sysconfig_settings_error_quark ())
+#define NM_TYPE_SYSCONFIG_SETTINGS_ERROR (nm_sysconfig_settings_error_get_type ())
+
+GQuark nm_sysconfig_settings_error_quark    (void);
+GType  nm_sysconfig_settings_error_get_type (void);
+
+#endif /* NM_SYSTEM_CONFIG_ERROR_H */

Modified: trunk/system-settings/src/nm-system-config-interface.c
==============================================================================
--- trunk/system-settings/src/nm-system-config-interface.c	(original)
+++ trunk/system-settings/src/nm-system-config-interface.c	Wed Jul 16 07:37:10 2008
@@ -129,13 +129,25 @@
 	return NULL;
 }
 
-void
+gboolean
+nm_system_config_interface_supports_add (NMSystemConfigInterface *config)
+{
+	g_return_val_if_fail (config != NULL, FALSE);
+
+	return NM_SYSTEM_CONFIG_INTERFACE_GET_INTERFACE (config)->add_connection != NULL;
+}
+
+gboolean
 nm_system_config_interface_add_connection (NMSystemConfigInterface *config,
 								   NMConnection *connection)
 {
-	g_return_if_fail (config != NULL);
-	g_return_if_fail (NM_IS_CONNECTION (connection));
+	gboolean success = FALSE;
+
+	g_return_val_if_fail (config != NULL, FALSE);
+	g_return_val_if_fail (NM_IS_CONNECTION (connection), FALSE);
 
 	if (NM_SYSTEM_CONFIG_INTERFACE_GET_INTERFACE (config)->add_connection)
-		NM_SYSTEM_CONFIG_INTERFACE_GET_INTERFACE (config)->add_connection (config, connection);
+		success = NM_SYSTEM_CONFIG_INTERFACE_GET_INTERFACE (config)->add_connection (config, connection);
+
+	return success;
 }

Modified: trunk/system-settings/src/nm-system-config-interface.h
==============================================================================
--- trunk/system-settings/src/nm-system-config-interface.h	(original)
+++ trunk/system-settings/src/nm-system-config-interface.h	Wed Jul 16 07:37:10 2008
@@ -92,7 +92,7 @@
 	/*
 	 * Add a new connection.
 	 */
-	void     (*add_connection) (NMSystemConfigInterface *config, NMConnection *connection);
+	gboolean (*add_connection) (NMSystemConfigInterface *config, NMConnection *connection);
 
 	/* Signals */
 
@@ -112,8 +112,10 @@
 
 GSList *nm_system_config_interface_get_unmanaged_devices (NMSystemConfigInterface *config);
 
-void nm_system_config_interface_add_connection (NMSystemConfigInterface *config,
-						NMConnection *connection);
+gboolean nm_system_config_interface_supports_add (NMSystemConfigInterface *config);
+
+gboolean nm_system_config_interface_add_connection (NMSystemConfigInterface *config,
+						    NMConnection *connection);
 
 G_END_DECLS
 



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]