talons

Fork of Claws Mail https://www.claws-mail
Log | Files | Refs | README | LICENSE

commit 534ca50699cab24e3787d8e13a2e4a2a4bfe2234
parent 4302636fb7a93c42287ffc9c37862bd21e29dc2c
Author: Andrej Kacian <ticho@claws-mail.org>
Date:   Thu, 18 Jan 2018 21:53:18 +0100

Implement config_version in passwordstorerc.

Diffstat:
Msrc/main.c | 9+++++++++
Msrc/passwordstore.c | 43++++++++++++++++++++++++++++++++++++++-----
Msrc/prefs_migration.c | 51+++++++++++++++++++++++++++++++++++++++++++++++++++
Msrc/prefs_migration.h | 1+
4 files changed, 99 insertions(+), 5 deletions(-)

diff --git a/src/main.c b/src/main.c @@ -1331,6 +1331,15 @@ int main(int argc, char *argv[]) folder_item_update_freeze(); passwd_store_read_config(); + + if (prefs_update_config_version_password_store() < 0) { + debug_print("Password store configuration file version upgrade failed, exiting\n"); +#ifdef G_OS_WIN32 + win32_close_log(); +#endif + exit(202); + } + prefs_account_init(); account_read_config_all(); diff --git a/src/passwordstore.c b/src/passwordstore.c @@ -37,6 +37,7 @@ #include "prefs_gtk.h" static GSList *_password_store; +gint _password_store_config_version = -1; /* Finds password block of given type and name in the pwdstore. */ static PasswordBlock *_get_block(PasswordBlockType block_type, @@ -308,6 +309,22 @@ static gint _write_to_file(FILE *fp) GList *keys, *eitem; gchar *typestr, *line, *key, *pwd; + /* Write out the config_version */ + line = g_strdup_printf("[config_version:%d]\n", _password_store_config_version); + if (fputs(line, fp) == EOF) { + FILE_OP_ERROR("password store, config_version", "fputs"); + g_free(line); + return -1; + } + g_free(line); + + /* Add a newline if needed */ + if (_password_store != NULL && fputs("\n", fp) == EOF) { + FILE_OP_ERROR("password store", "fputs"); + return -1; + } + + /* Write out each password store block */ for (item = _password_store; item != NULL; item = item->next) { block = (PasswordBlock*)item->data; if (block == NULL) @@ -352,6 +369,7 @@ static gint _write_to_file(FILE *fp) } g_list_free(keys); + /* Add a separating new line if there is another block remaining */ if (item->next != NULL && fputs("\n", fp) == EOF) { FILE_OP_ERROR("password store", "fputs"); return -1; @@ -395,6 +413,8 @@ void passwd_store_read_config(void) guint i = 0; PasswordBlock *block = NULL; PasswordBlockType type; + gboolean reading_config_version = FALSE; + gint ver = -1; /* TODO: passwd_store_clear(); */ @@ -431,17 +451,30 @@ void passwd_store_read_config(void) type = PWS_ACCOUNT; } else if (!strcmp(typestr, "plugin")) { type = PWS_PLUGIN; + } else if (!strcmp(typestr, "config_version")) { + reading_config_version = TRUE; + ver = atoi(name); } else { debug_print("Unknown password block type: '%s'\n", typestr); g_strfreev(line); i++; continue; } - if ((block = _new_block(type, name)) == NULL) { - debug_print("Duplicate password block, ignoring: (%d/%s)\n", - type, name); - g_strfreev(line); - i++; continue; + if (reading_config_version) { + if (ver < 0) { + debug_print("config_version:%d looks invalid, ignoring it\n", ver); + i++; continue; + } + debug_print("config_version in file is %d\n", ver); + _password_store_config_version = ver; + reading_config_version = FALSE; + } else { + if ((block = _new_block(type, name)) == NULL) { + debug_print("Duplicate password block, ignoring: (%d/%s)\n", + type, name); + g_strfreev(line); + i++; continue; + } } } g_strfreev(line); diff --git a/src/prefs_migration.c b/src/prefs_migration.c @@ -34,6 +34,8 @@ #include "prefs_common.h" #include "alertpanel.h" +extern gint _password_store_config_version; + static gint starting_config_version = 0; gboolean _version_check(gint ver) @@ -129,6 +131,22 @@ static void _update_config_account(PrefsAccount *ac_prefs, gint version) ac_prefs->config_version = version + 1; } +static void _update_config_password_store(gint version) +{ + debug_print("Password store: Updating config version from %d to %d.\n", + version, version + 1); + + switch (version) { + /* nothing here yet */ + + default: + + /* NOOP */ + + break; + } +} + int prefs_update_config_version_common() { gint ver = prefs_common_get_prefs()->config_version; @@ -190,3 +208,36 @@ int prefs_update_config_version_accounts() return 1; } + +int prefs_update_config_version_password_store() +{ + gint ver; + + if (_password_store_config_version == -1) { + /* There was no config_version stored in the config, let's assume + * config_version same as clawsrc started at, to avoid breaking + * the configuration by "upgrading" it unnecessarily. */ + debug_print("Password store: config_version not saved, using one from clawsrc: %d\n", starting_config_version); + _password_store_config_version = starting_config_version; + } + + ver = _password_store_config_version; + + debug_print("Starting config update at config_version %d.\n", ver); + + if (!_version_check(ver)) + return -1; + + if (ver == CLAWS_CONFIG_VERSION) { + debug_print("No update necessary, already at latest config_version.\n"); + return 0; /* nothing to do */ + } + + while (ver < CLAWS_CONFIG_VERSION) { + _update_config_password_store(ver++); + _password_store_config_version = ver; + } + + debug_print("Config update done.\n"); + return 1; +} diff --git a/src/prefs_migration.h b/src/prefs_migration.h @@ -21,5 +21,6 @@ int prefs_update_config_version_common(); int prefs_update_config_version_accounts(); +int prefs_update_config_version_password_store(); #endif /* __PREFS_MIGRATION_H__ */