[izpack-changes] r1735 - in izpack-src/trunk: . src/lib/com/izforge/izpack/io

noreply at berlios.de noreply at berlios.de
Fri Feb 16 10:22:06 CET 2007


Author: dreil
Date: 2007-02-16 10:22:03 +0100 (Fri, 16 Feb 2007)
New Revision: 1735

Modified:
   izpack-src/trunk/Versions.txt
   izpack-src/trunk/src/lib/com/izforge/izpack/io/FileSpanningInputStream.java
   izpack-src/trunk/src/lib/com/izforge/izpack/io/FileSpanningOutputStream.java
Log:
Added corrupt volume detection



Modified: izpack-src/trunk/Versions.txt
===================================================================
--- izpack-src/trunk/Versions.txt	2007-02-16 08:41:43 UTC (rev 1734)
+++ izpack-src/trunk/Versions.txt	2007-02-16 09:22:03 UTC (rev 1735)
@@ -5,6 +5,7 @@
 - Added Conditional expressions (Dennis Reil)
 - Fixed selection of default language in LanguageSelectionDialog (Dennis Reil)
 - Fixed RegularExpressionValidator (Dennis Reil)
+- Added corrupt volume detection (Dennis Reil)
 
   > 3.10.0 (build 2007.01.29)
 

Modified: izpack-src/trunk/src/lib/com/izforge/izpack/io/FileSpanningInputStream.java
===================================================================
--- izpack-src/trunk/src/lib/com/izforge/izpack/io/FileSpanningInputStream.java	2007-02-16 08:41:43 UTC (rev 1734)
+++ izpack-src/trunk/src/lib/com/izforge/izpack/io/FileSpanningInputStream.java	2007-02-16 09:22:03 UTC (rev 1735)
@@ -50,6 +50,8 @@
 
     protected GZIPInputStream zippedinputstream;
 
+    protected byte[] magicnumber;
+
     public FileSpanningInputStream(File volume, int volumestotal) throws IOException
     {
         fileinputstream = new FileInputStream(volume);
@@ -58,7 +60,14 @@
         volumename = volume.getAbsolutePath();
         this.volumestotal = volumestotal;
         filepointer = 0;
-        Debug.trace("Opening stream to " + volume);
+
+        // read magic number
+        this.magicnumber = new byte[FileSpanningOutputStream.MAGIC_NUMER_LENGTH];
+        zippedinputstream.read(this.magicnumber);
+        // this.read(this.magicnumber);
+        Debug.trace("Opening stream to " + volume + " magicnr is " + magicnumber);
+        // reset filepointer
+        filepointer = 0;
     }
 
     public FileSpanningInputStream(String volumename, int volumestotal) throws IOException
@@ -67,6 +76,58 @@
     }
 
     /**
+     * checks if the MagicNumber of this stream is valid. The stream has to be opened right before.
+     * 
+     * @return
+     * @throws IOException
+     */
+    private boolean isMagicNumberValid() throws IOException
+    {
+        Debug.trace("trying to read magic number");
+        boolean valid = false;
+        byte[] magicnumberofvolume = new byte[FileSpanningOutputStream.MAGIC_NUMER_LENGTH];
+        long oldfilepointer = this.filepointer;
+        // this.read(magicnumberofvolume);
+        this.zippedinputstream.read(magicnumberofvolume);
+        this.filepointer = oldfilepointer;
+        Debug.trace("MagicNr is " + magicnumberofvolume);
+        if ((magicnumberofvolume != null) && (this.magicnumber != null))
+        {
+            if (magicnumberofvolume.length != this.magicnumber.length)
+            {
+                // magicnumbers aren't valid
+                valid = false;
+            }
+            else
+            {
+                boolean errorfound = false;
+                // check if magicnumbers are identical
+                for (int i = 0; i < magicnumberofvolume.length; i++)
+                {
+                    byte op1 = magicnumberofvolume[i];
+                    byte op2 = this.magicnumber[i];
+                    if (op1 != op2)
+                    {
+                        errorfound = true;
+                        break;
+                    }
+                }
+                if (errorfound)
+                {
+                    // there was an error
+                    valid = false;
+                }
+                else
+                {
+                    // magic number is valid
+                    valid = true;
+                }
+            }
+        }
+        return valid;
+    }
+
+    /**
      * creates an inputstream to the next volume
      * 
      * @return true - an inputstream to the next volume has been created false - the last volume was
@@ -79,7 +140,7 @@
         // have we reached the last volume?
         if (currentvolumeindex >= volumestotal)
         {
-            Debug.error("last volume reached.");
+            Debug.trace("last volume reached.");
             return false;
         }
         // the next volume name
@@ -97,6 +158,17 @@
         // try to open new stream to next volume
         fileinputstream = new FileInputStream(nextvolumefile);
         zippedinputstream = new GZIPInputStream(fileinputstream);
+        // check magic number
+        if (!this.isMagicNumberValid())
+        {
+            currentvolumeindex--;
+            nextvolumenotfound = true;
+            Debug
+                    .trace("volume found, but magic number incorrect. Maybe not a volume of the same version.");
+            throw new CorruptVolumeException(nextvolumename
+                    + "was found, but has magic number error. Maybe not the right version?",
+                    nextvolumename);
+        }
         // everything fine
         nextvolumenotfound = false;
         return true;
@@ -298,4 +370,4 @@
         return filepointer;
     }
 
-}
+}
\ No newline at end of file

Modified: izpack-src/trunk/src/lib/com/izforge/izpack/io/FileSpanningOutputStream.java
===================================================================
--- izpack-src/trunk/src/lib/com/izforge/izpack/io/FileSpanningOutputStream.java	2007-02-16 08:41:43 UTC (rev 1734)
+++ izpack-src/trunk/src/lib/com/izforge/izpack/io/FileSpanningOutputStream.java	2007-02-16 09:22:03 UTC (rev 1735)
@@ -21,6 +21,8 @@
 import java.io.FileOutputStream;
 import java.io.IOException;
 import java.io.OutputStream;
+import java.util.Date;
+import java.util.Random;
 import java.util.zip.GZIPOutputStream;
 
 import com.izforge.izpack.util.Debug;
@@ -34,9 +36,9 @@
 public class FileSpanningOutputStream extends OutputStream
 {
 
-    public static final long KB = 1024;
+    public static final long KB = 1000;
 
-    public static final long MB = 1024 * KB;
+    public static final long MB = 1000 * KB;
 
     // the default size of a volume
     public static final long DEFAULT_VOLUME_SIZE = 650 * MB;
@@ -47,7 +49,7 @@
     public static final long DEFAULT_ADDITIONAL_FIRST_VOLUME_FREE_SPACE_SIZE = 0;
 
     // the default volume name
-    protected static final String DEFAULT_VOLUME_NAME = "installer";
+    protected static final String DEFAULT_VOLUME_NAME = "rdpack";
 
     protected static final long FILE_NOT_AVAILABLE = -1;
 
@@ -56,9 +58,9 @@
 
     // the addition free space of volume 0
     protected long firstvolumefreespacesize = DEFAULT_ADDITIONAL_FIRST_VOLUME_FREE_SPACE_SIZE;
-    
-    public static final String VOLUMES_INFO = "/volumes.info";  
 
+    public static final int MAGIC_NUMER_LENGTH = 10;
+
     // the current file this stream writes to
     protected File currentfile;
 
@@ -73,6 +75,9 @@
 
     private GZIPOutputStream zippedoutputstream;
 
+    // 
+    private byte[] magicnumber;
+
     // the current position in the open file
     protected long filepointer;
 
@@ -113,9 +118,31 @@
     protected FileSpanningOutputStream(File volume, long maxvolumesize, int currentvolume)
             throws IOException
     {
+        this.generateMagicNumber();
         this.createVolumeOutputStream(volume, maxvolumesize, currentvolume);
     }
 
+    private void generateMagicNumber()
+    {
+        // only create a magic number, if not already done
+        if (magicnumber == null)
+        {
+            // create empty magic number
+            magicnumber = new byte[MAGIC_NUMER_LENGTH];
+            Date currenttime = new Date();
+            long currenttimeseconds = currenttime.getTime();
+            // create random number generator
+            Random random = new Random(currenttimeseconds);
+            random.nextBytes(magicnumber);
+            Debug.trace("created new magic number for FileOutputstream: "
+                    + new String(magicnumber));
+            for (int i = 0; i < magicnumber.length; i++)
+            {
+                Debug.trace(i + " - " + magicnumber[i]);
+            }
+        }
+    }
+
     /**
      * Actually creates the outputstream for writing a volume with index currentvolume and a maximum
      * of maxvolumesize
@@ -145,6 +172,11 @@
         {
             volumename = volabsolutePath;
         }
+        long oldfilepointer = filepointer;
+        // write magic number into output stream
+        this.write(magicnumber);
+        // reset filepointer
+        filepointer = oldfilepointer;
     }
 
     /**




More information about the izpack-changes mailing list