X-Git-Url: https://gerrit.simantics.org/r/gitweb?p=simantics%2Fplatform.git;a=blobdiff_plain;f=bundles%2Forg.simantics.acorn%2Fsrc%2Forg%2Fsimantics%2Facorn%2FHeadState.java;h=a6a1622c80027d68b11b05a7de6b6d1891bb26f5;hp=dd8703c1fc689e0ed0bbfc1968e6730913d74750;hb=d5ca4ed76bc83af27f2ade59ce49e35750aa4177;hpb=36282d240b9ac295894a6705672efa11eb26e4d2 diff --git a/bundles/org.simantics.acorn/src/org/simantics/acorn/HeadState.java b/bundles/org.simantics.acorn/src/org/simantics/acorn/HeadState.java index dd8703c1f..a6a1622c8 100644 --- a/bundles/org.simantics.acorn/src/org/simantics/acorn/HeadState.java +++ b/bundles/org.simantics.acorn/src/org/simantics/acorn/HeadState.java @@ -12,12 +12,17 @@ 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"; @@ -31,6 +36,8 @@ public class HeadState { public ArrayList cs = new ArrayList<>(); // public ArrayList ccs = new ArrayList(); + public long tailChangeSetId = 1; + public static HeadState load(Path directory) throws InvalidHeadStateException { Path f = directory.resolve(HEAD_STATE); @@ -38,7 +45,6 @@ public class HeadState { byte[] bytes = Files.readAllBytes(f); 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))) { @@ -51,6 +57,11 @@ public class HeadState { 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) { @@ -88,7 +99,7 @@ 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); @@ -96,12 +107,17 @@ public class HeadState { 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); } } + }