From c8a9e675829d87eb2cd688efc74cef3aade621f4 Mon Sep 17 00:00:00 2001
From: felmer <felmer>
Date: Tue, 28 Feb 2012 08:31:43 +0000
Subject: [PATCH] LMS-2782 CRC32 checksum check added.

SVN: 24580
---
 .../shared/utils/SegmentedStoreUtils.java     | 25 +++++++++++++++++--
 1 file changed, 23 insertions(+), 2 deletions(-)

diff --git a/datastore_server/source/java/ch/systemsx/cisd/openbis/dss/generic/shared/utils/SegmentedStoreUtils.java b/datastore_server/source/java/ch/systemsx/cisd/openbis/dss/generic/shared/utils/SegmentedStoreUtils.java
index 4c25fe711c6..231a3c0578c 100644
--- a/datastore_server/source/java/ch/systemsx/cisd/openbis/dss/generic/shared/utils/SegmentedStoreUtils.java
+++ b/datastore_server/source/java/ch/systemsx/cisd/openbis/dss/generic/shared/utils/SegmentedStoreUtils.java
@@ -31,6 +31,7 @@ import java.util.regex.Pattern;
 
 import org.apache.commons.io.FileUtils;
 
+import ch.systemsx.cisd.base.exceptions.CheckedExceptionTunnel;
 import ch.systemsx.cisd.base.utilities.OSUtilities;
 import ch.systemsx.cisd.common.exceptions.ConfigurationFailureException;
 import ch.systemsx.cisd.common.exceptions.EnvironmentFailureException;
@@ -356,7 +357,7 @@ public class SegmentedStoreUtils
         if (source.isFile())
         {
             assertFile(destination);
-            return assertSameSize(source, destination);
+            return assertSameSizeAndCheckSum(source, destination);
         } else
         {
             assertDirectory(destination);
@@ -384,7 +385,7 @@ public class SegmentedStoreUtils
         }
     }
 
-    private static long assertSameSize(File source, File destination)
+    private static long assertSameSizeAndCheckSum(File source, File destination)
     {
         long sourceSize = source.length();
         long destinationSize = destination.length();
@@ -395,8 +396,28 @@ public class SegmentedStoreUtils
                     + " but source file '" + source.getAbsolutePath() + "' has size " + sourceSize
                     + ".");
         }
+        long sourceChecksum = calculateCRC(source);
+        long destinationChecksum = calculateCRC(destination);
+        if (sourceChecksum != destinationChecksum)
+        {
+            throw new EnvironmentFailureException("Destination file '"
+                    + destination.getAbsolutePath() + "' has checksum " + destinationChecksum
+                    + " but source file '" + source.getAbsolutePath() + "' has checksum " + sourceChecksum
+                    + ".");
+        }
         return sourceSize;
     }
+    
+    private static long calculateCRC(File file)
+    {
+        try
+        {
+            return FileUtils.checksumCRC32(file);
+        } catch (IOException ex)
+        {
+            throw CheckedExceptionTunnel.wrapIfNecessary(ex);
+        }
+    }
 
     private static void assertSameName(File source, File destination)
     {
-- 
GitLab