lib/config: don't overwrite database.path if the caller passed it

If the user passed a path, and we opened it, then we consider that
definitive definition of "database.path". This makes libnotmuch
respond more gracefully to certain erroneous combinations of
NOTMUCH_CONFIG settings and config file contents.
This commit is contained in:
David Bremner 2021-10-27 22:34:19 -03:00
parent d467444085
commit 59aac9cef3
2 changed files with 16 additions and 2 deletions

View file

@ -259,7 +259,15 @@ _notmuch_config_load_from_database (notmuch_database_t *notmuch)
for (; notmuch_config_list_valid (list); notmuch_config_list_move_to_next (list)) {
const char *key = notmuch_config_list_key (list);
char *normalized_val = _expand_path (list, key, notmuch_config_list_value (list));
char *normalized_val = NULL;
/* If we opened from a given path, do not overwrite it */
if (strcmp (key, "database.path") == 0 &&
(notmuch->params & NOTMUCH_PARAM_DATABASE) &&
notmuch->xapian_db)
continue;
normalized_val = _expand_path (list, key, notmuch_config_list_value (list));
_notmuch_string_map_append (notmuch->config, key, normalized_val);
talloc_free (normalized_val);
}
@ -432,6 +440,13 @@ _notmuch_config_load_from_file (notmuch_database_t *notmuch,
status = NOTMUCH_STATUS_FILE_ERROR;
goto DONE;
}
/* If we opened from a given path, do not overwrite it */
if (strcmp (absolute_key, "database.path") == 0 &&
(notmuch->params & NOTMUCH_PARAM_DATABASE) &&
notmuch->xapian_db)
continue;
normalized_val = _expand_path (notmuch, absolute_key, val);
_notmuch_string_map_set (notmuch->config, absolute_key, normalized_val);
g_free (val);

View file

@ -953,7 +953,6 @@ EOF
test_expect_equal_file EXPECTED OUTPUT
test_begin_subtest "open: database parameter overrides implicit config"
test_subtest_known_broken
notmuch config set database.path ${MAIL_DIR}/nonexistent
cat c_head3 - c_tail3 <<'EOF' | test_C ${MAIL_DIR}
const char *path = NULL;