From 0c123d7d14d0fa7a5061e9a91dbad83df7e2448f Mon Sep 17 00:00:00 2001 From: Federico Fissore Date: Mon, 4 May 2015 17:44:16 +0200 Subject: [PATCH] Deleting json files if they are some how corrupted. Fixes #3015 --- arduino-core/src/processing/app/BaseNoGui.java | 16 +++++++++++++--- .../src/processing/app/helpers/FileUtils.java | 13 ++++++++++++- 2 files changed, 25 insertions(+), 4 deletions(-) diff --git a/arduino-core/src/processing/app/BaseNoGui.java b/arduino-core/src/processing/app/BaseNoGui.java index dccad8e96..7d4ca1e71 100644 --- a/arduino-core/src/processing/app/BaseNoGui.java +++ b/arduino-core/src/processing/app/BaseNoGui.java @@ -7,6 +7,7 @@ import cc.arduino.contributions.packages.ContributionsIndexer; import cc.arduino.files.DeleteFilesOnShutdown; import cc.arduino.packages.DiscoveryManager; import cc.arduino.packages.Uploader; +import com.fasterxml.jackson.core.JsonProcessingException; import org.apache.commons.logging.impl.LogFactoryImpl; import org.apache.commons.logging.impl.NoOpLog; import processing.app.debug.Compiler; @@ -597,9 +598,13 @@ public class BaseNoGui { try { indexer.parseIndex(); + } catch (JsonProcessingException e) { + FileUtils.deleteIfExists(indexFile); + FileUtils.deleteIfExists(indexSignatureFile); + throw e; } catch (SignatureVerificationFailedException e) { - indexFile.delete(); - indexSignatureFile.delete(); + FileUtils.deleteIfExists(indexFile); + FileUtils.deleteIfExists(indexSignatureFile); throw e; } indexer.syncWithFilesystem(getHardwareFolder()); @@ -631,7 +636,12 @@ public class BaseNoGui { } } } - librariesIndexer.parseIndex(); + try { + librariesIndexer.parseIndex(); + } catch (JsonProcessingException e) { + FileUtils.deleteIfExists(librariesIndexFile); + throw e; + } } static protected void initPlatform() { diff --git a/arduino-core/src/processing/app/helpers/FileUtils.java b/arduino-core/src/processing/app/helpers/FileUtils.java index eaaeb6102..99fb95031 100644 --- a/arduino-core/src/processing/app/helpers/FileUtils.java +++ b/arduino-core/src/processing/app/helpers/FileUtils.java @@ -85,7 +85,7 @@ public class FileUtils { recursiveDelete(current); } } - file.delete(); + deleteIfExists(file); } public static File createTempFolder() throws IOException { @@ -269,5 +269,16 @@ public class FileUtils { return result; } + public static boolean deleteIfExists(File file) { + if (file == null) { + return true; + } + + if (!file.exists()) { + return false; + } + + return file.delete(); + } }