Author: j16sdiz
Date: 2008-07-10 09:11:37 +0000 (Thu, 10 Jul 2008)
New Revision: 21018

Modified:
   
branches/saltedhashstore/freenet/src/freenet/store/saltedhash/SaltedHashFreenetStore.java
Log:
Migrate old (pre-r21000) SaltedHashFreenetStore store format

Modified: 
branches/saltedhashstore/freenet/src/freenet/store/saltedhash/SaltedHashFreenetStore.java
===================================================================
--- 
branches/saltedhashstore/freenet/src/freenet/store/saltedhash/SaltedHashFreenetStore.java
   2008-07-10 06:57:53 UTC (rev 21017)
+++ 
branches/saltedhashstore/freenet/src/freenet/store/saltedhash/SaltedHashFreenetStore.java
   2008-07-10 09:11:37 UTC (rev 21018)
@@ -9,6 +9,7 @@
 import java.io.RandomAccessFile;
 import java.nio.ByteBuffer;
 import java.nio.channels.FileChannel;
+import java.text.DecimalFormat;
 import java.util.Arrays;
 import java.util.LinkedList;
 import java.util.List;
@@ -524,6 +525,42 @@
                                        digestedRoutingKey = 
cipherManager.getDigestedKey(plainRoutingKey);
                        return digestedRoutingKey;
                }
+               
+
+               // XXX Old Format, to be removed in next build
+               public void readOldFormat(ByteBuffer in) {
+                       assert in.remaining() == entryTotalLength;
+
+                       digestedRoutingKey = new byte[0x20];
+                       in.get(digestedRoutingKey);
+
+                       dataEncryptIV = new byte[0x10];
+                       in.get(dataEncryptIV);
+
+                       flag = in.getLong();
+                       storeSize = in.getLong();
+
+                       if ((flag & ENTRY_FLAG_PLAINKEY) != 0) {
+                               plainRoutingKey = new byte[0x20];
+                               in.get(plainRoutingKey);
+                       }
+
+                       in.position(0x60);
+                       generation = in.get();
+
+                       // reserved bytes
+                       in.position((int) ENTRY_HEADER_LENGTH);
+
+                       header = new byte[headerBlockLength];
+                       in.get(header);
+
+                       data = new byte[dataBlockLength];
+                       in.get(data);
+
+                       assert in.remaining() == entryPaddingLength;
+
+                       isEncrypted = true;
+               }
        }

        /**
@@ -844,6 +881,11 @@
                                        } catch (InterruptedException e) {
                                                Logger.debug(this, 
"interrupted", e);
                                        }
+
+                                       if (cleanerGlobalLock.tryLock()) {
+                                               migrateFromOldSaltedHash(); // 
XXX Old Format, to be removed in next build
+                                               cleanerGlobalLock.unlock();
+                                       }

                                        long _prevStoreSize;
                                        configLock.readLock().lock();
@@ -1428,4 +1470,57 @@
                        e.printStackTrace();
                }
        }
+       
+       // XXX Old Format, to be removed in next build
+       private final static int FILE_SPLIT = 0x04;
+       private final long ENTRY_HEADER_LENGTH = 0x70L;
+       private long entryPaddingLength;
+       private long entryTotalLength;
+
+       public void migrateFromOldSaltedHash() {
+               long length = ENTRY_HEADER_LENGTH + headerBlockLength + 
dataBlockLength;
+               entryPaddingLength = 0x200L - (length % 0x200L);
+               entryTotalLength = length + entryPaddingLength;
+
+        DecimalFormat fmt = new DecimalFormat("000");
+        int c = 0;
+               for (int i = 0; i < FILE_SPLIT; i++) {
+                       File storeFiles = new File(baseDir, name + ".data-" + 
fmt.format(i));
+                       if (!storeFiles.exists())
+                               continue;
+                       
+                       try {
+                               RandomAccessFile storeRAF = new 
RandomAccessFile(storeFiles, "rw");
+                               storeRAF.seek(0);
+
+                               byte[] b = new byte[(int) entryTotalLength];
+
+                               while (true) {
+                                       int status = storeRAF.read(b);
+                                       if (status != entryTotalLength)
+                                               break;
+                                       
+                                       ByteBuffer bf = ByteBuffer.wrap(b);
+                                       Entry e = new Entry();
+                                       e.readOldFormat(bf);
+                                       e.generation = generation;
+                                       
+                                       if (!e.isFree()) {
+                                               if (c++ % 1024 == 0)
+                                                       System.out.println(name 
+ ": old salt hash-->new salt hash migrated: " + c + " keys");
+                                               
cleanerThread.resolveOldEntry(e);
+                                       }
+                               }
+                               
+                               try {
+                                       storeRAF.close();
+                               } catch (IOException e) {
+                               }
+                       } catch (IOException ioe) {
+                       }
+                       storeFiles.delete();    
+               }
+
+               System.out.println(name + ": old salt hash-->new salt hash 
migrated: " + c + " keys(done)");
+       }
 }


Reply via email to