X-Git-Url: https://git.notmuchmail.org/git?p=notmuch;a=blobdiff_plain;f=notmuch-config.c;h=6845e3c3db5d3af6f5a9d25f39d4eb8060a1e41a;hp=247fbe4ba1e394c0d4dc34b7142dd2c2f9da3f57;hb=95ee9ed643add869f47940fccaad7b92bec4943f;hpb=e76f6517de020783d828be59f461f1d4f465c4b4 diff --git a/notmuch-config.c b/notmuch-config.c index 247fbe4b..6845e3c3 100644 --- a/notmuch-config.c +++ b/notmuch-config.c @@ -395,39 +395,28 @@ notmuch_config_open (void *ctx, * the configuration file, we add some comments to help the user * understand what can be done. */ if (config->is_new) - { g_key_file_set_comment (config->key_file, NULL, NULL, toplevel_config_comment, NULL); - } if (! file_had_database_group) - { g_key_file_set_comment (config->key_file, "database", NULL, database_config_comment, NULL); - } if (! file_had_new_group) - { g_key_file_set_comment (config->key_file, "new", NULL, new_config_comment, NULL); - } if (! file_had_user_group) - { g_key_file_set_comment (config->key_file, "user", NULL, user_config_comment, NULL); - } if (! file_had_maildir_group) - { g_key_file_set_comment (config->key_file, "maildir", NULL, maildir_config_comment, NULL); - } - if (! file_had_search_group) { + if (! file_had_search_group) g_key_file_set_comment (config->key_file, "search", NULL, search_config_comment, NULL); - } return config; } @@ -455,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); @@ -464,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; } @@ -690,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" @@ -705,14 +719,8 @@ _item_split (char *item, char **group, char **key) } static int -notmuch_config_command_get (void *ctx, char *item) +notmuch_config_command_get (notmuch_config_t *config, char *item) { - notmuch_config_t *config; - - config = notmuch_config_open (ctx, NULL, FALSE); - if (config == NULL) - return 1; - if (strcmp(item, "database.path") == 0) { printf ("%s\n", notmuch_config_get_database_path (config)); } else if (strcmp(item, "user.name") == 0) { @@ -756,25 +764,17 @@ notmuch_config_command_get (void *ctx, char *item) g_strfreev (value); } - notmuch_config_close (config); - return 0; } static int -notmuch_config_command_set (void *ctx, char *item, int argc, char *argv[]) +notmuch_config_command_set (notmuch_config_t *config, char *item, int argc, char *argv[]) { - notmuch_config_t *config; char *group, *key; - int ret; if (_item_split (item, &group, &key)) return 1; - config = notmuch_config_open (ctx, NULL, FALSE); - if (config == NULL) - return 1; - /* With only the name of an item, we clear it from the * configuration file. * @@ -795,23 +795,15 @@ notmuch_config_command_set (void *ctx, char *item, int argc, char *argv[]) break; } - ret = notmuch_config_save (config); - notmuch_config_close (config); - - return ret; + return notmuch_config_save (config); } static int -notmuch_config_command_list (void *ctx) +notmuch_config_command_list (notmuch_config_t *config) { - notmuch_config_t *config; char **groups; size_t g, groups_length; - config = notmuch_config_open (ctx, NULL, FALSE); - if (config == NULL) - return 1; - groups = g_key_file_get_groups (config->key_file, &groups_length); if (groups == NULL) return 1; @@ -841,13 +833,11 @@ notmuch_config_command_list (void *ctx) g_strfreev (groups); - notmuch_config_close (config); - return 0; } int -notmuch_config_command (void *ctx, int argc, char *argv[]) +notmuch_config_command (notmuch_config_t *config, int argc, char *argv[]) { argc--; argv++; /* skip subcommand argument */ @@ -862,16 +852,16 @@ notmuch_config_command (void *ctx, int argc, char *argv[]) "one argument.\n"); return 1; } - return notmuch_config_command_get (ctx, argv[1]); + return notmuch_config_command_get (config, argv[1]); } else if (strcmp (argv[0], "set") == 0) { if (argc < 2) { fprintf (stderr, "Error: notmuch config set requires at least " "one argument.\n"); return 1; } - return notmuch_config_command_set (ctx, argv[1], argc - 2, argv + 2); + return notmuch_config_command_set (config, argv[1], argc - 2, argv + 2); } else if (strcmp (argv[0], "list") == 0) { - return notmuch_config_command_list (ctx); + return notmuch_config_command_list (config); } fprintf (stderr, "Unrecognized argument for notmuch config: %s\n",