Handle password store config_version update without the global gint variable.

This commit is contained in:
Andrej Kacian 2018-01-19 19:16:44 +01:00
parent a9608e4cd5
commit a96fdf2c4e
5 changed files with 28 additions and 25 deletions

View file

@ -1331,10 +1331,8 @@ int main(int argc, char *argv[])
folderview_freeze(mainwin->folderview);
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");
if ((ret = passwd_store_read_config()) < 0) {
debug_print("Password store configuration file version upgrade failed (%d), exiting\n", ret);
#ifdef G_OS_WIN32
win32_close_log();
#endif
@ -1363,6 +1361,7 @@ int main(int argc, char *argv[])
* or a failed config_version upgrade.
*/
if ((ret = folder_read_list()) < 0) {
debug_print("Folderlist read failed (%d)\n", ret);
prefs_destroy_cache();
if (ret == -2) {

View file

@ -34,10 +34,11 @@
#include "common/utils.h"
#include "passwordstore.h"
#include "password.h"
#include "prefs_common.h"
#include "prefs_gtk.h"
#include "prefs_migration.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,
@ -310,7 +311,7 @@ static gint _write_to_file(FILE *fp)
gchar *typestr, *line, *key, *pwd;
/* Write out the config_version */
line = g_strdup_printf("[config_version:%d]\n", _password_store_config_version);
line = g_strdup_printf("[config_version:%d]\n", CLAWS_CONFIG_VERSION);
if (fputs(line, fp) == EOF) {
FILE_OP_ERROR("password store, config_version", "fputs");
g_free(line);
@ -406,7 +407,7 @@ void passwd_store_write_config(void)
}
}
void passwd_store_read_config(void)
int passwd_store_read_config(void)
{
gchar *rcpath, *contents, **lines, **line, *typestr, *name;
GError *error = NULL;
@ -414,7 +415,7 @@ void passwd_store_read_config(void)
PasswordBlock *block = NULL;
PasswordBlockType type;
gboolean reading_config_version = FALSE;
gint ver = -1;
gint config_version = -1;
/* TODO: passwd_store_clear(); */
@ -427,7 +428,7 @@ void passwd_store_read_config(void)
g_warning("couldn't read password store from file: %s", error->message);
g_error_free(error);
g_free(rcpath);
return;
return -1;
}
g_free(rcpath);
@ -453,7 +454,7 @@ void passwd_store_read_config(void)
type = PWS_PLUGIN;
} else if (!strcmp(typestr, "config_version")) {
reading_config_version = TRUE;
ver = atoi(name);
config_version = atoi(name);
} else {
debug_print("Unknown password block type: '%s'\n", typestr);
g_strfreev(line);
@ -461,12 +462,13 @@ void passwd_store_read_config(void)
}
if (reading_config_version) {
if (ver < 0) {
debug_print("config_version:%d looks invalid, ignoring it\n", ver);
if (config_version < 0) {
debug_print("config_version:%d looks invalid, ignoring it\n",
config_version);
config_version = -1; /* set to default value if missing */
i++; continue;
}
debug_print("config_version in file is %d\n", ver);
_password_store_config_version = ver;
debug_print("config_version in file is %d\n", config_version);
reading_config_version = FALSE;
} else {
if ((block = _new_block(type, name)) == NULL) {
@ -494,4 +496,11 @@ void passwd_store_read_config(void)
i++;
}
g_strfreev(lines);
if (prefs_update_config_version_password_store(config_version) < 0) {
debug_print("Password store configuration file version upgrade failed\n");
return -2;
}
return g_slist_length(_password_store);
}

View file

@ -67,7 +67,7 @@ void passwd_store_reencrypt_all(const gchar *old_mpwd,
/* Writes/reads password store to/from file. */
void passwd_store_write_config(void);
void passwd_store_read_config(void);
int passwd_store_read_config(void);
/* Convenience wrappers for handling account passwords.
* (This is to save some boilerplate code converting account_id to

View file

@ -34,8 +34,6 @@
#include "prefs_common.h"
#include "alertpanel.h"
extern gint _password_store_config_version;
static gint starting_config_version = 0;
gboolean _version_check(gint ver)
@ -225,20 +223,18 @@ int prefs_update_config_version_accounts()
return 1;
}
int prefs_update_config_version_password_store()
int prefs_update_config_version_password_store(gint from_version)
{
gint ver;
gint ver = from_version;
if (_password_store_config_version == -1) {
if (ver == -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 = starting_config_version;
}
ver = _password_store_config_version;
debug_print("Starting config update at config_version %d.\n", ver);
if (!_version_check(ver))
@ -251,7 +247,6 @@ int prefs_update_config_version_password_store()
while (ver < CLAWS_CONFIG_VERSION) {
_update_config_password_store(ver++);
_password_store_config_version = ver;
}
debug_print("Config update done.\n");

View file

@ -21,7 +21,7 @@
int prefs_update_config_version_common();
int prefs_update_config_version_accounts();
int prefs_update_config_version_password_store();
int prefs_update_config_version_password_store(gint from_version);
int prefs_update_config_version_folderlist(gint from_version);
#endif /* __PREFS_MIGRATION_H__ */