[PATCH v3] config: do not overwrite symlinks when saving config file

Jani Nikula jani at nikula.org
Sun Mar 3 06:18:30 PST 2013


Use realpath to canonicalize the config path before writing.

Previously 'notmuch setup' and 'notmuch config set' overwrote the
config file even if it was a symbolic link.

---

v3: compared to v2, as suggested by Tomi on IRC:

-       if (strcmp (filename, config->filename)) {
+       if (strcmp (filename, config->filename) != 0) {
---
 notmuch-config.c |   24 ++++++++++++++++++++----
 1 file changed, 20 insertions(+), 4 deletions(-)

diff --git a/notmuch-config.c b/notmuch-config.c
index b5c2066..8adb404 100644
--- a/notmuch-config.c
+++ b/notmuch-config.c
@@ -461,7 +461,7 @@ int
 notmuch_config_save (notmuch_config_t *config)
 {
     size_t length;
-    char *data;
+    char *data, *filename;
     GError *error = NULL;
 
     data = g_key_file_to_data (config->key_file, &length, NULL);
@@ -470,14 +470,30 @@ notmuch_config_save (notmuch_config_t *config)
 	return 1;
     }
 
-    if (! g_file_set_contents (config->filename, data, length, &error)) {
-	fprintf (stderr, "Error saving configuration to %s: %s\n",
-		 config->filename, error->message);
+    /* Try not to overwrite symlinks. */
+    filename = realpath (config->filename, NULL);
+    if (! filename) {
+	fprintf (stderr, "Error canonicalizing %s: %s\n", config->filename,
+		 strerror (errno));
+	g_free (data);
+	return 1;
+    }
+
+    if (! g_file_set_contents (filename, data, length, &error)) {
+	if (strcmp (filename, config->filename) != 0) {
+	    fprintf (stderr, "Error saving configuration to %s (-> %s): %s\n",
+		     config->filename, filename, error->message);
+	} else {
+	    fprintf (stderr, "Error saving configuration to %s: %s\n",
+		     filename, error->message);
+	}
 	g_error_free (error);
+	free (filename);
 	g_free (data);
 	return 1;
     }
 
+    free (filename);
     g_free (data);
     return 0;
 }
-- 
1.7.10.4



More information about the notmuch mailing list