summary refs log tree commit diff
path: root/src/settings/plugins/keyfile/writer.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/settings/plugins/keyfile/writer.c')
-rw-r--r--src/settings/plugins/keyfile/writer.c65
1 files changed, 31 insertions, 34 deletions
diff --git a/src/settings/plugins/keyfile/writer.c b/src/settings/plugins/keyfile/writer.c
index b6a8786d..db00b061 100644
--- a/src/settings/plugins/keyfile/writer.c
+++ b/src/settings/plugins/keyfile/writer.c
@@ -27,15 +27,11 @@
 #include <errno.h>
 #include <string.h>
 
-#include "nm-glib-compat.h"
-
-#include "nm-logging.h"
+#include "nm-default.h"
 #include "writer.h"
-#include "common.h"
 #include "utils.h"
 #include "nm-keyfile-internal.h"
 
-
 typedef struct {
 	const char *keyfile_dir;
 } WriteInfo;
@@ -233,17 +229,18 @@ _internal_write_connection (NMConnection *connection,
                             uid_t owner_uid,
                             pid_t owner_grp,
                             const char *existing_path,
+                            gboolean force_rename,
                             char **out_path,
                             GError **error)
 {
 	GKeyFile *key_file;
-	char *data;
+	gs_free char *data = NULL;
 	gsize len;
-	gboolean success = FALSE;
-	char *path;
+	gs_free char *path = NULL;
 	const char *id;
 	WriteInfo info = { 0 };
 	GError *local_err = NULL;
+	int errsv;
 
 	g_return_val_if_fail (!out_path || !*out_path, FALSE);
 	g_return_val_if_fail (keyfile_dir && keyfile_dir[0] == '/', FALSE);
@@ -267,7 +264,7 @@ _internal_write_connection (NMConnection *connection,
 	/* If we have existing file path, use it. Else generate one from
 	 * connection's ID.
 	 */
-	if (existing_path != NULL) {
+	if (existing_path != NULL && !force_rename) {
 		path = g_strdup (existing_path);
 	} else {
 		char *filename_escaped = nm_keyfile_plugin_utils_escape_filename (id);
@@ -312,8 +309,7 @@ _internal_write_connection (NMConnection *connection,
 				/* this really should not happen, we tried hard to find an unused name... bail out. */
 				g_set_error (error, NM_SETTINGS_ERROR, NM_SETTINGS_ERROR_FAILED,
 				                    "could not find suitable keyfile file name (%s already used)", path);
-				g_free (path);
-				goto out;
+				return FALSE;
 			}
 			/* Both our preferred path based on connection id and id-uuid are taken.
 			 * Fallback to @existing_path */
@@ -331,49 +327,49 @@ _internal_write_connection (NMConnection *connection,
 	g_file_set_contents (path, data, len, &local_err);
 	if (local_err) {
 		g_set_error (error, NM_SETTINGS_ERROR, NM_SETTINGS_ERROR_FAILED,
-		             "%s.%d: error writing to file '%s': %s", __FILE__, __LINE__,
+		             "error writing to file '%s': %s",
 		             path, local_err->message);
 		g_error_free (local_err);
-		g_free (path);
-		goto out;
+		return FALSE;
 	}
 
 	if (chown (path, owner_uid, owner_grp) < 0) {
+		errsv = errno;
 		g_set_error (error, NM_SETTINGS_ERROR, NM_SETTINGS_ERROR_FAILED,
-		             "%s.%d: error chowning '%s': %d", __FILE__, __LINE__,
-		             path, errno);
+		             "error chowning '%s': %s (%d)",
+		             path, g_strerror (errsv), errsv);
 		unlink (path);
-	} else {
-		if (chmod (path, S_IRUSR | S_IWUSR) < 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);
-		} else {
-			if (out_path && g_strcmp0 (existing_path, path)) {
-				*out_path = path;  /* pass path out to caller */
-				path = NULL;
-			}
-			success = TRUE;
-		}
+		return FALSE;
 	}
-	g_free (path);
 
-out:
-	g_free (data);
-	return success;
+	if (chmod (path, S_IRUSR | S_IWUSR) < 0) {
+		errsv = errno;
+		g_set_error (error, NM_SETTINGS_ERROR, NM_SETTINGS_ERROR_FAILED,
+		             "error setting permissions on '%s': %s (%d)",
+		             path, g_strerror (errsv), errsv);
+		unlink (path);
+		return FALSE;
+	}
+
+	if (out_path && g_strcmp0 (existing_path, path)) {
+		*out_path = path;  /* pass path out to caller */
+		path = NULL;
+	}
+	return TRUE;
 }
 
 gboolean
 nm_keyfile_plugin_write_connection (NMConnection *connection,
                                     const char *existing_path,
+                                    gboolean force_rename,
                                     char **out_path,
                                     GError **error)
 {
 	return _internal_write_connection (connection,
-	                                   KEYFILE_DIR,
+	                                   nm_keyfile_plugin_get_path (),
 	                                   0, 0,
 	                                   existing_path,
+	                                   force_rename,
 	                                   out_path,
 	                                   error);
 }
@@ -390,6 +386,7 @@ nm_keyfile_plugin_write_test_connection (NMConnection *connection,
 	                                   keyfile_dir,
 	                                   owner_uid, owner_grp,
 	                                   NULL,
+	                                   FALSE,
 	                                   out_path,
 	                                   error);
 }