diff options
author | Jani Nikula <jani@nikula.org> | 2013-04-07 20:15:03 +0300 |
---|---|---|
committer | David Bremner <bremner@debian.org> | 2013-04-14 18:38:07 -0300 |
commit | 58ed67992d0ec1fa505026105218fa449f7980b0 (patch) | |
tree | 91cbf5686c6260b6718f04b9ddeb08cde2747249 | |
parent | 7defbcdcec8bd89723dcd3548d2bfc7d58f0275d (diff) |
cli: config: do not overwrite symlinks when saving config file
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.
-rw-r--r-- | notmuch-config.c | 24 | ||||
-rwxr-xr-x | test/config | 1 |
2 files changed, 20 insertions, 5 deletions
diff --git a/notmuch-config.c b/notmuch-config.c index 66a1cdfb..d9c2eb3f 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,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; } diff --git a/test/config b/test/config index 344eced3..ca4cf330 100755 --- a/test/config +++ b/test/config @@ -78,7 +78,6 @@ test_expect_equal "$(notmuch --config=alt-config-link config get user.name)" \ "Link Name" test_begin_subtest "Writing config file through symlink follows symlink" -test_subtest_known_broken test_expect_equal "$(readlink alt-config-link)" "alt-config" test_done |