]> nmode's Git Repositories - signal-cli/commitdiff
Add version to account file
authorAsamK <asamk@gmx.de>
Wed, 5 May 2021 17:02:22 +0000 (19:02 +0200)
committerAsamK <asamk@gmx.de>
Wed, 5 May 2021 17:02:22 +0000 (19:02 +0200)
lib/src/main/java/org/asamk/signal/manager/storage/SignalAccount.java

index 6671bc76e4a96943271af3601b3afc65bebb49c9..15dfb2002d19052a4d1eeb9655833f054cbcb86f 100644 (file)
@@ -64,6 +64,9 @@ public class SignalAccount implements Closeable {
 
     private final static Logger logger = LoggerFactory.getLogger(SignalAccount.class);
 
 
     private final static Logger logger = LoggerFactory.getLogger(SignalAccount.class);
 
+    private static final int MINIMUM_STORAGE_VERSION = 1;
+    private static final int CURRENT_STORAGE_VERSION = 2;
+
     private final ObjectMapper jsonProcessor = Utils.createStorageObjectMapper();
 
     private final FileChannel fileChannel;
     private final ObjectMapper jsonProcessor = Utils.createStorageObjectMapper();
 
     private final FileChannel fileChannel;
@@ -281,6 +284,15 @@ public class SignalAccount implements Closeable {
             rootNode = jsonProcessor.readTree(Channels.newInputStream(fileChannel));
         }
 
             rootNode = jsonProcessor.readTree(Channels.newInputStream(fileChannel));
         }
 
+        if (rootNode.hasNonNull("version")) {
+            var accountVersion = rootNode.get("version").asInt(1);
+            if (accountVersion > CURRENT_STORAGE_VERSION) {
+                throw new IOException("Config file was created by a more recent version!");
+            } else if (accountVersion < MINIMUM_STORAGE_VERSION) {
+                throw new IOException("Config file was created by a no longer supported older version!");
+            }
+        }
+
         username = Utils.getNotNullNode(rootNode, "username").asText();
         password = Utils.getNotNullNode(rootNode, "password").asText();
         registered = Utils.getNotNullNode(rootNode, "registered").asBoolean();
         username = Utils.getNotNullNode(rootNode, "username").asText();
         password = Utils.getNotNullNode(rootNode, "password").asText();
         registered = Utils.getNotNullNode(rootNode, "registered").asBoolean();
@@ -558,7 +570,8 @@ public class SignalAccount implements Closeable {
     private void save() {
         synchronized (fileChannel) {
             var rootNode = jsonProcessor.createObjectNode();
     private void save() {
         synchronized (fileChannel) {
             var rootNode = jsonProcessor.createObjectNode();
-            rootNode.put("username", username)
+            rootNode.put("version", CURRENT_STORAGE_VERSION)
+                    .put("username", username)
                     .put("uuid", uuid == null ? null : uuid.toString())
                     .put("deviceId", deviceId)
                     .put("isMultiDevice", isMultiDevice)
                     .put("uuid", uuid == null ? null : uuid.toString())
                     .put("deviceId", deviceId)
                     .put("isMultiDevice", isMultiDevice)