X-Git-Url: https://git.notmuchmail.org/git?p=notmuch;a=blobdiff_plain;f=notmuch-config.c;h=6845e3c3db5d3af6f5a9d25f39d4eb8060a1e41a;hp=66a1cdfbf2bf8fd438bc752984528e8a00ad2bcc;hb=95ee9ed643add869f47940fccaad7b92bec4943f;hpb=bf3436669343b935a119fd37c4469c8948610121 diff --git a/notmuch-config.c b/notmuch-config.c index 66a1cdfb..6845e3c3 100644 --- a/notmuch-config.c +++ b/notmuch-config.c @@ -444,7 +444,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); @@ -453,14 +453,39 @@ 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) { + if (errno == ENOENT) { + filename = strdup (config->filename); + if (! filename) { + fprintf (stderr, "Out of memory.\n"); + g_free (data); + return 1; + } + } else { + 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; } @@ -679,7 +704,7 @@ _item_split (char *item, char **group, char **key) *group = item; - period = index (item, '.'); + period = strchr (item, '.'); if (period == NULL || *(period+1) == '\0') { fprintf (stderr, "Invalid configuration name: %s\n"