]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.acorn/src/org/simantics/acorn/HeadState.java
Fixed bad logical bug from Acorn's MainState.load rollback
[simantics/platform.git] / bundles / org.simantics.acorn / src / org / simantics / acorn / HeadState.java
index c20d8e878a4eec04ebaa26ab892efa1790aea431..a6a1622c80027d68b11b05a7de6b6d1891bb26f5 100644 (file)
@@ -1,7 +1,6 @@
 package org.simantics.acorn;
 
 import java.io.ByteArrayInputStream;
-import java.io.File;
 import java.io.IOException;
 import java.io.OutputStream;
 import java.nio.file.Files;
@@ -11,13 +10,22 @@ import java.security.NoSuchAlgorithmException;
 import java.util.ArrayList;
 import java.util.Arrays;
 
+import org.simantics.acorn.exception.InvalidHeadStateException;
 import org.simantics.databoard.Bindings;
+import org.simantics.databoard.adapter.AdapterConstructionException;
 import org.simantics.databoard.binding.mutable.MutableVariant;
 import org.simantics.databoard.serialization.Serializer;
 import org.simantics.databoard.util.binary.BinaryMemory;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 public class HeadState {
 
+    private static transient final Logger LOGGER = LoggerFactory.getLogger(HeadState.class);
+    
+    public static final String HEAD_STATE = "head.state";
+    public static final String SHA_1 = "SHA-1";
+    
     public int headChangeSetId = 0;
     public long transactionId = 1;
     public long reservedIds = 3;
@@ -28,14 +36,15 @@ public class HeadState {
     public ArrayList<String> cs = new ArrayList<>();
 //    public ArrayList<String> ccs = new ArrayList<String>();
 
+    public long tailChangeSetId = 1;
+
     public static HeadState load(Path directory) throws InvalidHeadStateException {
-        Path f = directory.resolve("head.state");
+        Path f = directory.resolve(HEAD_STATE);
         
         try {
             byte[] bytes = Files.readAllBytes(f);
-            MessageDigest sha1 = MessageDigest.getInstance("SHA-1");
+            MessageDigest sha1 = MessageDigest.getInstance(SHA_1);
             int digestLength = sha1.getDigestLength();
-            
             sha1.update(bytes, digestLength, bytes.length - digestLength);
             byte[] newChecksum = sha1.digest();
             if (!Arrays.equals(newChecksum, Arrays.copyOfRange(bytes, 0, digestLength))) {
@@ -43,11 +52,16 @@ public class HeadState {
                         "Checksum " + Arrays.toString(newChecksum) + " does not match excpected "
                                 + Arrays.toString(Arrays.copyOfRange(bytes, 0, digestLength)) + " for " + f.toAbsolutePath());
             }
-            
-            HeadState object = (HeadState) org.simantics.databoard.Files.readFile(new ByteArrayInputStream(bytes, digestLength, bytes.length - digestLength), Bindings.getBindingUnchecked(HeadState.class));
-            return object;
-            
+            try (ByteArrayInputStream bais = new ByteArrayInputStream(bytes, digestLength, bytes.length - digestLength)) {
+                HeadState object = (HeadState) org.simantics.databoard.Files.readFile(bais, Bindings.getBindingUnchecked(HeadState.class));
+                return object;
+            }
         } catch (IOException i) {
+               Throwable cause = i.getCause();
+               if(cause instanceof AdapterConstructionException) {
+                       HeadState1 old = HeadState1.load(directory);
+                       return old.migrate();
+               }
             return new HeadState();
 //            throw new InvalidHeadStateException(i);
         } catch (NoSuchAlgorithmException e) {
@@ -58,7 +72,7 @@ public class HeadState {
     }
     
     public void save(Path directory) throws IOException {
-        Path f = directory.resolve("head.state");
+        Path f = directory.resolve(HEAD_STATE);
         try {
             BinaryMemory rf = new BinaryMemory(4096);
             try {
@@ -71,7 +85,7 @@ public class HeadState {
             
             byte[] bytes = rf.toByteBuffer().array();
             
-            MessageDigest sha1 = MessageDigest.getInstance("SHA-1");
+            MessageDigest sha1 = MessageDigest.getInstance(SHA_1);
             sha1.update(bytes);
             byte[] checksum = sha1.digest();
             
@@ -85,20 +99,25 @@ public class HeadState {
         }
     }
 
-    public static void validateHeadStateIntegrity(Path headState) throws InvalidHeadStateException, IOException {
+    public static boolean validateHeadStateIntegrity(Path headState) {
         try {
             byte[] bytes = Files.readAllBytes(headState);
-            MessageDigest sha1 = MessageDigest.getInstance("SHA-1");
+            MessageDigest sha1 = MessageDigest.getInstance(SHA_1);
             int digestLength = sha1.getDigestLength();
             sha1.update(bytes, digestLength, bytes.length - digestLength);
             byte[] newChecksum = sha1.digest();
             if (!Arrays.equals(newChecksum, Arrays.copyOfRange(bytes, 0, digestLength))) {
-                throw new InvalidHeadStateException(
-                        "Checksum " + Arrays.toString(newChecksum) + " does not match excpected "
-                                + Arrays.toString(Arrays.copyOfRange(bytes, 0, digestLength)) + " for " + headState.toAbsolutePath());
+                LOGGER.error("Checksum " + Arrays.toString(newChecksum) + " does not match excpected " + Arrays.toString(Arrays.copyOfRange(bytes, 0, digestLength)) + " for " + headState.toAbsolutePath());
+                return false;
             }
+            return true;
+        } catch (IOException e) {
+            LOGGER.error("An I/O error occured while validating integrity of head.state", e);
+            return false;
         } catch (NoSuchAlgorithmException e) {
+            LOGGER.error("SHA-1 digest not found, should not happen", e);
             throw new Error("SHA-1 digest not found, should not happen", e);
         }
     }
+
 }