From dd3b6a9456518ec11f08573c3f9694be588800d3 Mon Sep 17 00:00:00 2001 From: Mike Major Date: Mon, 25 Jan 2021 12:00:13 +0000 Subject: [PATCH 1/2] [jsondb] Restore zero byte files from backup Signed-off-by: Mike Major --- .../org/openhab/core/storage/json/internal/JsonStorage.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bundles/org.openhab.core.storage.json/src/main/java/org/openhab/core/storage/json/internal/JsonStorage.java b/bundles/org.openhab.core.storage.json/src/main/java/org/openhab/core/storage/json/internal/JsonStorage.java index b2590a33a23..2c33a7873e7 100644 --- a/bundles/org.openhab.core.storage.json/src/main/java/org/openhab/core/storage/json/internal/JsonStorage.java +++ b/bundles/org.openhab.core.storage.json/src/main/java/org/openhab/core/storage/json/internal/JsonStorage.java @@ -97,7 +97,7 @@ public JsonStorage(File file, @Nullable ClassLoader classLoader, int maxBackupFi commitTimer = new Timer(); Map inputMap = null; - if (file.exists()) { + if (file.exists() && file.length() > 0) { // Read the file inputMap = readDatabase(file); } From 89b418e462b0af14bb403e75d84d9eb0ce165a3b Mon Sep 17 00:00:00 2001 From: Mike Major Date: Mon, 25 Jan 2021 17:34:29 +0000 Subject: [PATCH 2/2] Also handle zero byte backup files Signed-off-by: Mike Major --- .../openhab/core/storage/json/internal/JsonStorage.java | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/bundles/org.openhab.core.storage.json/src/main/java/org/openhab/core/storage/json/internal/JsonStorage.java b/bundles/org.openhab.core.storage.json/src/main/java/org/openhab/core/storage/json/internal/JsonStorage.java index 2c33a7873e7..ca14e4e6653 100644 --- a/bundles/org.openhab.core.storage.json/src/main/java/org/openhab/core/storage/json/internal/JsonStorage.java +++ b/bundles/org.openhab.core.storage.json/src/main/java/org/openhab/core/storage/json/internal/JsonStorage.java @@ -97,7 +97,7 @@ public JsonStorage(File file, @Nullable ClassLoader classLoader, int maxBackupFi commitTimer = new Timer(); Map inputMap = null; - if (file.exists() && file.length() > 0) { + if (file.exists()) { // Read the file inputMap = readDatabase(file); } @@ -216,6 +216,11 @@ public Collection getKeys() { @SuppressWarnings("unchecked") private @Nullable Map readDatabase(File inputFile) { + if (inputFile.length() == 0) { + logger.warn("Json storage file at '{}' is empty - ignoring corrupt file.", inputFile.getAbsolutePath()); + return null; + } + try { final Map inputMap = new ConcurrentHashMap<>();