From: AsamK Date: Wed, 5 May 2021 17:02:22 +0000 (+0200) Subject: Add version to account file X-Git-Tag: v0.8.2~21 X-Git-Url: https://git.nmode.ca/signal-cli/commitdiff_plain/3d361d54bbe5cfb289fcdc781bec120b3a921070?ds=inline Add version to account file --- diff --git a/lib/src/main/java/org/asamk/signal/manager/storage/SignalAccount.java b/lib/src/main/java/org/asamk/signal/manager/storage/SignalAccount.java index 6671bc76..15dfb200 100644 --- a/lib/src/main/java/org/asamk/signal/manager/storage/SignalAccount.java +++ b/lib/src/main/java/org/asamk/signal/manager/storage/SignalAccount.java @@ -64,6 +64,9 @@ public class SignalAccount implements Closeable { 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; @@ -281,6 +284,15 @@ public class SignalAccount implements Closeable { 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(); @@ -558,7 +570,8 @@ public class SignalAccount implements Closeable { 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)