gzz-commits
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Gzz-commits] gzz gzz/util/CopyUtil.java lava/gzz/storm/Block...


From: Benja Fallenstein
Subject: [Gzz-commits] gzz gzz/util/CopyUtil.java lava/gzz/storm/Block...
Date: Sat, 16 Nov 2002 00:59:46 -0500

CVSROOT:        /cvsroot/gzz
Module name:    gzz
Changes by:     Benja Fallenstein <address@hidden>      02/11/16 00:59:46

Modified files:
        gzz/util       : CopyUtil.java 
        lava/gzz/storm : BlockId.java 
        lava/gzz/storm/impl: DirPool.java TransientPool.java 
        lava/test/gzz/storm: StormPoolTest.java 
        lava/test/gzz/storm/impl: DirPool.test TransientPool.test 

Log message:
        Make Storm understand old-style (00-) ids

CVSWeb URLs:
http://savannah.gnu.org/cgi-bin/viewcvs/gzz/gzz/gzz/util/CopyUtil.java.diff?tr1=1.1&tr2=1.2&r1=text&r2=text
http://savannah.gnu.org/cgi-bin/viewcvs/gzz/gzz/lava/gzz/storm/BlockId.java.diff?tr1=1.4&tr2=1.5&r1=text&r2=text
http://savannah.gnu.org/cgi-bin/viewcvs/gzz/gzz/lava/gzz/storm/impl/DirPool.java.diff?tr1=1.10&tr2=1.11&r1=text&r2=text
http://savannah.gnu.org/cgi-bin/viewcvs/gzz/gzz/lava/gzz/storm/impl/TransientPool.java.diff?tr1=1.16&tr2=1.17&r1=text&r2=text
http://savannah.gnu.org/cgi-bin/viewcvs/gzz/gzz/lava/test/gzz/storm/StormPoolTest.java.diff?tr1=1.11&tr2=1.12&r1=text&r2=text
http://savannah.gnu.org/cgi-bin/viewcvs/gzz/gzz/lava/test/gzz/storm/impl/DirPool.test.diff?tr1=1.6&tr2=1.7&r1=text&r2=text
http://savannah.gnu.org/cgi-bin/viewcvs/gzz/gzz/lava/test/gzz/storm/impl/TransientPool.test.diff?tr1=1.6&tr2=1.7&r1=text&r2=text

Patches:
Index: gzz/gzz/util/CopyUtil.java
diff -u gzz/gzz/util/CopyUtil.java:1.1 gzz/gzz/util/CopyUtil.java:1.2
--- gzz/gzz/util/CopyUtil.java:1.1      Tue Aug 13 19:01:27 2002
+++ gzz/gzz/util/CopyUtil.java  Sat Nov 16 00:59:45 2002
@@ -36,22 +36,25 @@
      */
     static public int copy(InputStream from, OutputStream to, int blockSize)
                                                throws IOException {
-        byte[] buf = new byte[blockSize];
-       int bytesCopied = 0;
-        //p("start copying");
-        while(true) {
-            //p("read ");
-            int r = from.read(buf);
-            //p("check("+r+") ");
-            if(r == -1) break;
-            //p("write ");
-            to.write(buf, 0, r);
-           bytesCopied += r;
-        }
-        //p("... all read.");
-        from.close();
-       to.close();
-       return bytesCopied;
+       try {
+           byte[] buf = new byte[blockSize];
+           int bytesCopied = 0;
+           //p("start copying");
+           while(true) {
+               //p("read ");
+               int r = from.read(buf);
+               //p("check("+r+") ");
+               if(r == -1) break;
+               //p("write ");
+               to.write(buf, 0, r);
+               bytesCopied += r;
+           }
+           //p("... all read.");
+           return bytesCopied;
+       } finally {
+           from.close();
+           to.close();
+       }
     }
 
     /** Read data from an input stream into a byte array by copying it into
Index: gzz/lava/gzz/storm/BlockId.java
diff -u gzz/lava/gzz/storm/BlockId.java:1.4 gzz/lava/gzz/storm/BlockId.java:1.5
--- gzz/lava/gzz/storm/BlockId.java:1.4 Sat Nov 16 00:03:40 2002
+++ gzz/lava/gzz/storm/BlockId.java     Sat Nov 16 00:59:46 2002
@@ -58,8 +58,24 @@
 
     /** Check that the given data bytes match this id.
      */
-    public void check(byte[] data) throws IOException {
-        throw new UnsupportedOperationException("XXX!");
+    public void check(byte[] data) throws WrongIdException {
+        MessageDigest my_digest = makeMessageDigest();
+
+       if(bytes[0] == 0x00) {
+           int l = bytes.length - 20;
+           my_digest.update((byte)((l >>> 24) & 0xff));
+           my_digest.update((byte)((l >>> 16) & 0xff));
+           my_digest.update((byte)((l >>> 8) & 0xff));
+           my_digest.update((byte)(l & 0xff));
+           my_digest.update(bytes, 0, l);
+       }
+
+       my_digest.update(data);
+
+       byte[] dig = my_digest.digest();
+       for(int i=0; i<20; i++)
+           if(dig[i] != bytes[bytes.length-20+i])
+               throw new WrongIdException("Hash doesn't match");
     }
 
     /** Get an InputStream that checks whether the data read from
@@ -78,7 +94,12 @@
         final MessageDigest my_digest = makeMessageDigest();
 
        if(bytes[0] == 0x00) {
-           my_digest.update(bytes, 1, bytes.length-21);
+           int l = bytes.length - 20;
+           my_digest.update((byte)((l >>> 24) & 0xff));
+           my_digest.update((byte)((l >>> 16) & 0xff));
+           my_digest.update((byte)((l >>> 8) & 0xff));
+           my_digest.update((byte)(l & 0xff));
+           my_digest.update(bytes, 0, l);
        }
 
        InputStream dis = new DigestInputStream(in, my_digest) {
Index: gzz/lava/gzz/storm/impl/DirPool.java
diff -u gzz/lava/gzz/storm/impl/DirPool.java:1.10 
gzz/lava/gzz/storm/impl/DirPool.java:1.11
--- gzz/lava/gzz/storm/impl/DirPool.java:1.10   Sat Nov 16 00:03:40 2002
+++ gzz/lava/gzz/storm/impl/DirPool.java        Sat Nov 16 00:59:46 2002
@@ -109,16 +109,17 @@
        return new FileBlock(id); 
     }
     public void add(Block b) throws IOException {
-       byte[] bytes = gzz.util.CopyUtil.readBytes(b.getRawInputStream());
-       BlockId id = BlockId.getIdForData(bytes);
-
-       if(!id.equals(b.getId()))
-           throw new IOException("Block with bad id supplied");
-
+       File temp = gzz.util.TempFileUtil.tmpFile(dir);
+       
+       BlockId id = b.getId();
+       InputStream is = id.getCheckedInputStream(b.getRawInputStream());
        OutputStream os = 
-           new BufferedOutputStream(new FileOutputStream(getFile(id)));
-       os.write(bytes);
-       os.close();
+           new BufferedOutputStream(new FileOutputStream(temp));
+       
+       gzz.util.CopyUtil.copy(is, os);
+
+       if(!temp.renameTo(getFile(id)))
+           throw new IOException("Could not rename temporary file");
     }
     public void delete(Block b) throws IOException {
        getFile(b.getId()).delete();
Index: gzz/lava/gzz/storm/impl/TransientPool.java
diff -u gzz/lava/gzz/storm/impl/TransientPool.java:1.16 
gzz/lava/gzz/storm/impl/TransientPool.java:1.17
--- gzz/lava/gzz/storm/impl/TransientPool.java:1.16     Fri Nov 15 23:38:04 2002
+++ gzz/lava/gzz/storm/impl/TransientPool.java  Sat Nov 16 00:59:46 2002
@@ -75,21 +75,17 @@
        return (Block)blocks.get(id);
     }
     public void add(Block b) throws IOException {
-       byte[] body = gzz.util.CopyUtil.readBytes(b.getInputStream());
-       byte[] raw = gzz.util.CopyUtil.readBytes(b.getRawInputStream());
+       byte[] bytes = gzz.util.CopyUtil.readBytes(b.getRawInputStream());
 
-       InputStream is = new ByteArrayInputStream(raw);
+       BlockId id = b.getId();
+       id.check(bytes);
+
+       InputStream is = new ByteArrayInputStream(bytes);
        Header822 header = Headers822.readHeader(is);
        is.close();
 
-       Block block = new TransientBlock(BlockId.getIdForData(raw),
-                                        raw, header);
-       BlockId id = block.getId();
-
-       if(!id.equals(b.getId()))
-           throw new IOException("Block with bad id supplied");
-
-       blocks.put(block.getId(), block);
+       Block block = new TransientBlock(id, bytes, header);
+       blocks.put(id, block);
     }
     public void delete(Block b) {
        blocks.keySet().remove(b.getId());
Index: gzz/lava/test/gzz/storm/StormPoolTest.java
diff -u gzz/lava/test/gzz/storm/StormPoolTest.java:1.11 
gzz/lava/test/gzz/storm/StormPoolTest.java:1.12
--- gzz/lava/test/gzz/storm/StormPoolTest.java:1.11     Fri Nov 15 23:38:04 2002
+++ gzz/lava/test/gzz/storm/StormPoolTest.java  Sat Nov 16 00:59:46 2002
@@ -286,4 +286,65 @@
        } catch(FileNotFoundException e) {
        }
     }
+
+    /** Test adding a block with an old-style id from a different pool. */
+    public void testAddBlock00(StormPool pool) throws IOException {
+       final byte[] bytes =
+           ("X-Injected-By: address@hidden"+
+            "Date: Sun, 12 Aug 2001 16:13:26 +0000\r\n"+
+            "Content-Type: application/x-gzigzag-GZZ1\r\n"+
+            "Content-Transfer-Encoding: binary\r\n"+
+            "\r\n"+
+            "GZZ1\n1\n"+
+            
"0000000008000000E8473B08AE0004BE303AB79A49A5918F2E41123D2380983EC246EF82C8581F\n"+
+            "N\n0\nS\n0\n00\n").getBytes("US-ASCII");
+      
+       final BlockId id = new 
BlockId("storm:block:0000000008000000E8478C8E0100044374AF00D9DBBA68A5B6A3B9B0FA610AA36135EB49A3B2D1");
+       final BlockId badid = new BlockId(
+           "storm:block:01E88CEE70319F016EEF00B315C0B930C953DB7776");
+
+       Block orig = new Block() {
+               public BlockId getId() { return id; }
+               public StormPool getPool() { return null; }
+               public Header822 getHeader() { return null; }
+               public InputStream getInputStream() throws IOException { 
+                   return new 
ByteArrayInputStream("foo".getBytes("US-ASCII")); 
+               }
+               public InputStream getRawInputStream() { 
+                   return new ByteArrayInputStream(bytes);
+               }
+           };
+
+       Block badBlock = new Block() {
+               public BlockId getId() { return badid; }
+               public StormPool getPool() { return null; }
+               public Header822 getHeader() { return null; }
+               public InputStream getInputStream() throws IOException { 
+                   return new 
ByteArrayInputStream("foo".getBytes("US-ASCII")); 
+               }
+               public InputStream getRawInputStream() { 
+                   return new ByteArrayInputStream(bytes);
+               }
+           };
+
+       pool.add(orig);
+       Block b = pool.get(id);
+       
+       String s = new String(CopyUtil.readBytes(b.getInputStream()));
+       if(!s.startsWith("GZZ1\n1\n")) throw new Error();
+       if(!b.getHeader().get("X-injected-by").equals("address@hidden"))
+           throw new Error();
+
+       try {
+           pool.add(badBlock);
+           throw new Error();
+       } catch(IOException e) {
+       }
+
+       try {
+           pool.get(badid);
+           throw new Error();
+       } catch(FileNotFoundException e) {
+       }
+    }
 }
Index: gzz/lava/test/gzz/storm/impl/DirPool.test
diff -u gzz/lava/test/gzz/storm/impl/DirPool.test:1.6 
gzz/lava/test/gzz/storm/impl/DirPool.test:1.7
--- gzz/lava/test/gzz/storm/impl/DirPool.test:1.6       Sat Nov 16 00:03:40 2002
+++ gzz/lava/test/gzz/storm/impl/DirPool.test   Sat Nov 16 00:59:46 2002
@@ -31,6 +31,7 @@
 def testDelete(): s.testDelete(p)
 def testBlockId(): s.testBlockId(p)
 def testAddBlock(): s.testAddBlock(p)
+def testAddBlock00(): s.testAddBlock00(p)
 def testGetNonexistent(): s.testGetNonexistent(p)
 
 def testGetBlockWithBadId():
Index: gzz/lava/test/gzz/storm/impl/TransientPool.test
diff -u gzz/lava/test/gzz/storm/impl/TransientPool.test:1.6 
gzz/lava/test/gzz/storm/impl/TransientPool.test:1.7
--- gzz/lava/test/gzz/storm/impl/TransientPool.test:1.6 Thu Nov 14 16:25:06 2002
+++ gzz/lava/test/gzz/storm/impl/TransientPool.test     Sat Nov 16 00:59:46 2002
@@ -28,4 +28,5 @@
 def testDelete(): s.testDelete(p)
 def testBlockId(): s.testBlockId(p)
 def testAddBlock(): s.testAddBlock(p)
+def testAddBlock00(): s.testAddBlock00(p)
 def testGetNonexistent(): s.testGetNonexistent(p)




reply via email to

[Prev in Thread] Current Thread [Next in Thread]