Implement config_version in passwordstorerc.
authorAndrej Kacian <ticho@claws-mail.org>
Thu, 18 Jan 2018 20:53:18 +0000 (21:53 +0100)
committerAndrej Kacian <ticho@claws-mail.org>
Thu, 18 Jan 2018 20:53:18 +0000 (21:53 +0100)
src/main.c
src/passwordstore.c
src/prefs_migration.c
src/prefs_migration.h

index 317b196..ed7bf5c 100644 (file)
@@ -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();
 
index 68f4b51..84119c5 100644 (file)
@@ -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);
index 6c8041b..251ba19 100644 (file)
@@ -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;
+}
index 31dc892..f1c5f15 100644 (file)
@@ -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__ */